I'am trying to compile a very small haskell code with ghc:
module Comma where
import System.IO
main = do
contents <- getContents
putStr (comma contents)
comma input =
let allLines = lines input
addcomma [x] = x
addcomma (x:xs) = x ++ "," ++ (addcomma xs)
result = addcomma allLines
in result
The command i'm using to compile is :
ghc --make Comma.hs
And i'm getting this answer:
[1 of 1] Compiling Comma ( Comma.hs, Comma.o )
No file is generated, and there is no warning or errors messages.
If i comment the "module Comma where" line from code it compiles correctly:
[1 of 1] Compiling Main ( Comma.hs, Comma.o ) Linking Comma ...
I don't understand what is happening. I'm using ghc 7,4,1 (Glasgow Haskell Compiler, Version 7.4.1, stage 2 booted by GHC version 7.4.1) and ubuntu linux.
I appreciate if anyone could tell why doesn't compile with the module definition
GHC supports multiple backend code generators. This is the part of the compiler responsible for taking the last intermediate representation that GHC uses (a form called Cmm that is a simple, C like language) and compiling it to executable code.
Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)
The compiler (written in Haskell), translates Haskell to C, assembly, LLVM bitcode and other formats. The strategy it uses is described best here: Implementing lazy functional languages on stock hardware:the Spineless Tagless G-machine.
GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.
GHC compiles the function Main.main
to be the entry point of an executable. When you omit the module declaration, Module Main where
is implicitly inserted for you.
But when you explicitly name it something other than Main
ghc doesn't find an entry point.
My usual workflow is to use ghci
(or ghci + emacs) instead for these snippets which let's you bypass this issue entirely. Alternatively, you could compile with -main-is Comma
to explicitly tell ghc to use the Comma module.
No file is generated
Are you sure? I would expect that at least Comma.o
and Comma.hi
are generated. The former contains the compiled code ready to be linked into an executable, and the latter contains interface information that ghc uses to typecheck modules that import the module Comma
.
However, ghc will only link the compiled modules into an executable if there is a main function. By default, that means a function named main
in a module named Main
. If you don't put an explicit module name, the name Main
is assumed, and that's why your test works when you delete the module Comma where
line.
To compile and link the Comma.hs
file you can either use module Main where
instead of module Comma where
, or you can use the -main-is
flag to tell ghc that Comma.main
is to be the main function:
ghc --make -main-is Comma Comma.hs
Or:
ghc --make -main-is Comma.main Comma.hs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With