Running and compiling 'Hello, World!' in Haskell - haskell

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.

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

Related

How to resolve Haskell error in loading module?

I am getting the following error.
(base) Pauls-MBP-2:~ reiners$ ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
Prelude> import Euterpea
<no location info>: error:
Could not find module ‘Euterpea’
It is not a module in the current program, or in any known package.
Prelude>
I had previously installed Euterpea with the command
cabal install Euterpea
In fact, the import actually worked. However, for other reasons, I closed that command prompt, and now it's not working.
I also tried running it from the cabal repl and got the same result:
(base) Pauls-MBP-2:~ reiners$ cabal repl
Resolving dependencies...
Build profile: -w ghc-8.8.4 -O1
In order, the following will be built (use -v for more details):
- fake-package-0 (lib) (first run)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /var/folders/8k/74p15mf15zsg82l4r7xz28t00000gn/T/cabal-repl.-19576/setcwd.ghci
Prelude> import Euterpia
<no location info>: error:
Could not find module ‘Euterpia’
It is not a module in the current program, or in any known package.
Prelude>
I'm using macOS Catalina (10.15.7).

ghc: can't find a package database

Im trying to run the ghci REPL but this happens
# ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
ghc: can't find a package database at /usr/lib/ghc/package.conf.d
when trying to list installed modules:
# ghc-pkg list
ghc-pkg: /var/lib/ghc/package.conf.d: openFile: does not exist (No such file or directory)
when trying to install openFile:
# cabal install openFile
cabal: ghc-pkg: /var/lib/ghc/package.conf.d: openFile: does not exist (No such
file or directory)

GHCI: I found a duplicate definition for symbol _fps_count

tl;dr: It seems like Network.HTTP and Data.Aeson are not compatible with each other. How would I begin working around this?
Long version: I think I narrowed this down as much as possible. I started with a fresh cabal sandbox and now am getting a runtime linker fatal error.
Here's my code:
-- test.hs
import Network.HTTP (simpleHTTP, getRequest, getResponseBody, postRequestWithBody)
import Data.Aeson
main = print "hi"
And here's how I get the error, from the very first command:
$ cabal --version
cabal-install version 1.20.0.3
$ cabal sandbox init
...
$ cabal install aeson
...
$ cabal repl
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package primitive-0.5.0.1 ... linking ... done.
Loading package vector-0.10.0.1 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package mtl-2.1.2 ... linking ... done.
Loading package text-0.11.3.1 ... linking ... done.
Loading package parsec-3.1.3 ... linking ... done.
Loading package old-locale-1.0.0.5 ... linking ... done.
Loading package time-1.4.0.1 ... linking ... done.
Loading package unix-2.6.0.1 ... linking ... done.
Loading package network-2.4.1.2 ... linking ... done.
Loading package old-time-1.1.0.1 ... linking ... done.
Loading package HTTP-4000.2.8 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package bytestring-0.10.4.0 ...
GHCi runtime linker: fatal error: I found a duplicate definition for symbol
_fps_count
whilst processing object file
/Users/tapwater/p/haskell/stellar/.cabal-sandbox/lib/x86_64-osx-ghc-7.6.3/bytestring-0.10.4.0/libHSbytestring-0.10.4.0.a
This could be caused by:
* Loading two different object files which export the same symbol
* Specifying the same object file twice on the GHCi command line
* An incorrect `package.conf' entry, causing some object to be
loaded twice.
GHCi cannot safely continue in this situation. Exiting now. Sorry.
If I comment out either one of the import statements, everything works fine.
Am I (hopefully) doing something wrong? Or are these two libraries entirely incompatible?
This ended up being the solution:
$ cabal install http
I'm guessing this solved it because of cabal sandbox. I guess I need to do this for any library that comes with haskell platform that causes conflicts like this? I feel like I'm still doing something wrong.

Typeclass instance is not resolved after module installed with Cabal

I created simple project to demonstrate the issue: https://github.com/jdevelop/testcabal
If I compile and install the module with 'cabal install', I can not serialize TestData with Binary:
> ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
ghci> :m +TestBinary.Test Data.Binary
ghci> randomData . decode $ encode emptyTest
<interactive>:1:24:
No instance for (Binary TTestData)
arising from a use of `encode'
Possible fix: add an instance declaration for (Binary TTestData)
In the second argument of `($)', namely `encode emptyTest'
In the expression: randomData . decode $ encode emptyTest
In an equation for `it':
it = randomData . decode $ encode emptyTest
If I load the Test.hs into ghci directly - everything works as expected,
> ghci TestBinary/Test.hs
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling TestBinary.Test ( TestBinary/Test.hs, interpreted )
Ok, modules loaded: TestBinary.Test.
ghci> randomData . decode $ encode emptyTest
Loading package array-0.3.0.2 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package binary-0.5.1.0 ... linking ... done.
"123456"
Version of Haskell compiler:
> ghci --version
The Glorious Glasgow Haskell Compilation System, version 7.0.4
In your .cabal file, you have
Build-depends: base < 5, ghc-binary >= 0.5, bytestring >= 0.9.1
Usually, ghc-binary is not exposed, and it is not intended to be used except by GHC itself. When you load Data.Binary into ghci, it loads the module from the binary package, and the Binary class from that package is a different one than the one from ghc-binary, hence TTestData has no instance.
If you load the file from source, ghci doesn't care about the .cabal file and uses the class from binary directly, hence it works.
You should change the dependency to the binary package.

Problem with Data.HashMap - unable to insert value

I'm really newbie with Haskell and I have serious problems with Data.HashMap. What I'm doing wrong?
Here is what I have tried:
> ghci -v
GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help
Glasgow Haskell Compiler, Version 6.12.1, for Haskell 98, stage 2 booted by GHC version 6.12.1
Using binary package database: /usr/lib/ghc-6.12.1/package.conf.d/package.cache
Using binary package database: /home/joni/.ghc/x86_64-linux-6.12.1/package.conf.d/package.cache
package utf8-string-0.3.4-15837bafc9579c596819d4381db0f19b is shadowed by package utf8-string-0.3.4-75a948ddecbeae79ab3ec3d9f9bcdb65
package vimirc-0.0.0.1-d41d8cd98f00b204e9800998ecf8427e is unusable due to missing or recursive dependencies:
utf8-string-0.3.4-15837bafc9579c596819d4381db0f19b
hiding package base-3.0.3.2 to avoid conflict with later version base-4.2.0.0
hiding package bytestring-0.9.1.5 to avoid conflict with later version bytestring-0.9.1.10
wired-in package ghc-prim mapped to ghc-prim-0.2.0.0-9d35c97e886f807a1e6d024aaa91dcec
wired-in package integer-gmp mapped to integer-gmp-0.2.0.0-9a51ffb34a83618a1a3d4e472b9977a0
wired-in package base mapped to base-4.2.0.0-2cc27b7e43511c4ca001642a7f77a8f6
wired-in package rts mapped to builtin_rts
wired-in package haskell98 mapped to haskell98-1.0.1.1-0fdaf3b26bc38c43ce8371edf538dbf6
wired-in package template-haskell mapped to template-haskell-2.4.0.0-bbc7c61990d2fe9d20be2deb924f833c
wired-in package dph-seq mapped to dph-seq-0.4.0-52cfd6db5fc09a2abf793cd6a856a392
wired-in package dph-par mapped to dph-par-0.4.0-b4f339fed900d7bc4b3db61526caf863
Hsc static flags: -static
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> t <- Data.HashTable.new (==) Data.HashTable.hashString
*** Parser:
*** Desugar:
*** Simplify:
*** CorePrep:
*** ByteCodeGen:
Prelude> Data.HashTable.insert t "Joni" 1
*** Parser:
<interactive>:1:31:
No instance for (Num GHC.Prim.Any)
arising from the literal `1' at <interactive>:1:31
Possible fix: add an instance declaration for (Num GHC.Prim.Any)
In the third argument of `Data.HashTable.insert', namely `1'
In the expression: Data.HashTable.insert t "Joni" 1
In the definition of `it': it = Data.HashTable.insert t "Joni" 1
Prelude> Data.HashTable.insert t "Joni" "1"
*** Parser:
<interactive>:1:31:
Couldn't match expected type `GHC.Prim.Any'
against inferred type `[Char]'
In the third argument of `Data.HashTable.insert', namely `"1"'
In the expression: Data.HashTable.insert t "Joni" "1"
In the definition of `it': it = Data.HashTable.insert t "Joni" "1"
You have to tell it which type you want to store in the hashtable. This works:
t <- Data.HashTable.new (==) Data.HashTable.hashString :: IO (Data.HashTable.HashTable String Integer)
Data.HashTable.insert t "Joni" 1

Resources