运行和编译“Hello,World!'在哈斯克尔

时间:2016-09-07 21:55:44

标签: haskell ghc

我是Haskell的新手,遇到运行以下代码的问题:

module Main (
main
) where 
main = putStrLn "Hello, world!"

SublimeHaskell尝试使用

编译并运行上述内容
runhaskell hello.hs

返回错误

hello.o: getModificationTime: invalid argument (The system cannot find the file specified.)

我也尝试使用

运行它
ghc --make hello.hs

有同样的错误。当我尝试在使用命令行

运行之前手动编译代码时
ghc -c hello.hs

我得到了一个不同的错误:

CreateDirectory ".": invalid argument (Cannot create a file when that file already exists.)

另一方面,我可以通过REPL运行程序而不会出现问题:

ghci
GHCi, version 8.0.1: http://www.haskel.org/ghc/  :? for help
Prelude> putStrLn "Hello, world!"
Hello, world!

如果相关,我使用的是Windows 7,并且我使用Haskell平台安装程序安装了GHC。

***编辑使用-v标志运行上述命令会产生以下结果:

ghc -v hello.hs

Glasgow Haskell Compiler, Version 8.0.1, stage 2 booted by GHC version     7.10.2
Using binary package database: C:\Program Files\Haskell      Platform\8.0.1\lib\package.conf.d\package.cache
loading package database C:\Program Files\Haskell    Platform\8.0.1\lib\package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.5.0.0
wired-in package integer-gmp mapped to integer-gmp-1.0.0.1
wired-in package base mapped to base-4.9.0.0
wired-in package rts mapped to rts
wired-in package template-haskell mapped to template-haskell-2.11.0.0
wired-in package ghc mapped to ghc-8.0.1
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags:
loading package database C:\Program Files\Haskell     Platform\8.0.1\lib\package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.5.0.0
wired-in package integer-gmp mapped to integer-gmp-1.0.0.1
wired-in package base mapped to base-4.9.0.0
wired-in package rts mapped to rts-1.0
wired-in package template-haskell mapped to template-haskell-2.11.0.0
wired-in package ghc mapped to ghc-8.0.1
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Chasing dependencies:
Chasing modules from: *hello.hs
*** Deleting temp files:
Deleting:
*** Deleting temp dirs:
Deleting:
hello.o: getModificationTime: invalid argument (The system cannot find the file specified.)

ghc -c -v hello.hs

Glasgow Haskell Compiler, Version 8.0.1, stage 2 booted by GHC version 7.10.2
Using binary package database: C:\Program Files\Haskell     Platform\8.0.1\lib\package.conf.d\package.cache
loading package database C:\Program Files\Haskell    Platform\8.0.1\lib\package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.5.0.0
wired-in package integer-gmp mapped to integer-gmp-1.0.0.1
wired-in package base mapped to base-4.9.0.0
wired-in package rts mapped to rts
wired-in package template-haskell mapped to template-haskell-2.11.0.0
wired-in package ghc mapped to ghc-8.0.1
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags:
*** Checking old interface for Main:
*** Parser [Main]:
!!! Parser [Main]: finished in 0.00 milliseconds, allocated 0.067 megabytes
*** Renamer/typechecker [Main]:
!!! Renamer/typechecker [Main]: finished in 46.80 milliseconds, allocated  15.720 megabytes
*** Desugar [Main]:
Result size of Desugar (after optimization)
= {terms: 13, types: 6, coercions: 0}
!!! Desugar [Main]: finished in 0.00 milliseconds, allocated 0.204 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 17, types: 8, coercions: 0}
Result size of Simplifier = {terms: 17, types: 8, coercions: 0}
!!! Simplifier [Main]: finished in 0.00 milliseconds, allocated 0.171  megabytes
*** CoreTidy [Main]:
Result size of Tidy Core = {terms: 17, types: 8, coercions: 0}
!!! CoreTidy [Main]: finished in 0.00 milliseconds, allocated 2.558  megabytes
*** Deleting temp files:
Deleting:
*** Deleting temp dirs:
Deleting:
CreateDirectory ".": invalid argument (Cannot create a file when that file already exists.)

环顾四周后,似乎已弃用了列为未找到的两个软件包(dph-seqdph-par)。

1 个答案:

答案 0 :(得分:0)

GHC要求将文件命名为与模块相同的文件以进行编译。 但是,无论文件名如何,都可以将任何模块加载到GHCi中。这就是为什么repl工作但编译没有。

因此,在您的情况下,您应该重命名Main.hs文件