Haskell 为什么我能';如果代码包含模块定义,是否使用GHC编译?

Haskell 为什么我能';如果代码包含模块定义,是否使用GHC编译?,haskell,compiler-errors,ghc,Haskell,Compiler Errors,Ghc,我正在尝试用ghc编译一个非常小的haskell代码: 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 ++ "," +

我正在尝试用ghc编译一个非常小的haskell代码:

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
模块逗号,其中
导入系统.IO
main=do

内容如果文件中有
main
定义,并且希望将其编译为可执行文件,则只能在其中使用
module main

GHC将函数
Main.Main
编译为可执行文件的入口点。当您省略模块声明时,
modulemain,其中
为您隐式插入

但是,当您明确地将它命名为除
Main
ghc之外的其他名称时,它找不到入口点

我通常的工作流程是使用
ghci
(或ghci+emacs)来代替这些代码片段,让您完全绕过这个问题。或者,您可以使用
-main is Comma
进行编译,以明确告知ghc使用逗号模块

没有生成任何文件

你确定吗?我希望至少生成
Comma.o
Comma.hi
。前者包含准备链接到可执行文件中的编译代码,后者包含ghc用于对导入模块的模块进行类型检查的接口信息

但是,如果存在主函数,ghc将只将编译后的模块链接到可执行文件中。默认情况下,这意味着名为
main
的模块中名为
main
的函数。如果没有输入明确的模块名称,则假定名称为
Main
,这就是删除
module逗号where
行时测试工作的原因

要编译和链接
Comma.hs
文件,您可以使用
module Main where
而不是
module Comma where
,也可以使用
-Main is
标志告知ghc
Comma.Main
将成为主要功能:

ghc --make -main-is Comma Comma.hs
或:


使用
-main是
标志,
ghc-main是逗号
(从7.0开始,ghc就不需要
--make
)。另外,您的程序相当于这样:
main=interact$interlate“,”。行
只是为了向将来的读者澄清,
main
不是Haskell中的函数。
ghc --make -main-is Comma.main Comma.hs