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

For*_*ato 8 haskell ghc

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

module Main (
main
) where 
main = putStrLn "Hello, world!"
Run Code Online (Sandbox Code Playgroud)

SublimeHaskell尝试编译并运行上面的使用

runhaskell hello.hs
Run Code Online (Sandbox Code Playgroud)

它返回错误

hello.o: getModificationTime: invalid argument (The system cannot find the file specified.)
Run Code Online (Sandbox Code Playgroud)

我也试过用它来运行它

ghc --make hello.hs
Run Code Online (Sandbox Code Playgroud)

有同样的错误.当我尝试在使用命令行运行之前手动编译代码

ghc -c hello.hs
Run Code Online (Sandbox Code Playgroud)

我得到一个不同的错误:

CreateDirectory ".": invalid argument (Cannot create a file when that file already exists.)
Run Code Online (Sandbox Code Playgroud)

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

ghci
GHCi, version 8.0.1: http://www.haskel.org/ghc/  :? for help
Prelude> putStrLn "Hello, world!"
Hello, world!
Run Code Online (Sandbox Code Playgroud)

如果它是相关的,我使用的是Windows 7,并且我使用Haskell Platform安装程序安装了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.)
Run Code Online (Sandbox Code Playgroud)

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

小智 1

GHC 要求文件的名称与模块的名称相同才能编译它。但是,任何模块都可以加载到 GHCi 中,无论文件名如何。这就是为什么 repl 有效但编译无效的原因。

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