Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running and compiling 'Hello, World!' in Haskell

Tags:

haskell

ghc

I am new to Haskell and am having an issue running the following code:

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

SublimeHaskell tries to compile and run the above using

runhaskell hello.hs

which returns the error

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

I have also tried to run it using

ghc --make hello.hs

with the same error. When I try to manually compile the code before running using the command line

ghc -c hello.hs

I get a different error:

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

On the other hand, I can run the program via the REPL without issue:

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

If it's relevant, I am using Windows 7, and I installed GHC using the Haskell Platform installer.

***edit running the above commands with the -v flag gives the following results:

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.)

After looking around, it seems that the two packages that are listed as not found (dph-seq and dph-par) have been deprecated.

like image 463
Fortunato Avatar asked Sep 07 '16 21:09

Fortunato


1 Answers

GHC requires that the file be named the same as the module in order to compile it. However, any module can be loaded into GHCi regardless of file name. This is why repl worked but compiling did not.

In your case, therefore, you should rename the file Main.hs

like image 134
Adam Beddoe Avatar answered Sep 20 '22 11:09

Adam Beddoe