Problem with Data.HashMap - unable to insert value - haskell

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

Related

Running and compiling 'Hello, World!' in 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

Attoparsec `many` method not found

I tried running the tests for Bryan O'Sullivan's Attoparsec-based HTTP parser (http://www.serpentine.com/blog/2010/03/03/whats-in-a-parser-attoparsec-rewired-2/), and I got this error:
> runhaskell TestRFC2616.hs
TestRFC2616.hs:13:30:
Not in scope: `many'
Perhaps you meant one of these:
`any' (imported from Prelude),
`B.any' (imported from Data.ByteString.Char8),
many' (imported from Data.Attoparsec)
Surprised, I ran ghci and got this:
> ghci
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> :m +Data.Attoparsec
Prelude Data.Attoparsec> :t many
<interactive>:1:1:
Not in scope: `many'
Perhaps you meant one of these:
`any' (imported from Prelude),
many' (imported from Data.Attoparsec),
`many1' (imported from Data.Attoparsec)
Prelude Data.Attoparsec>
Can anyone tell me what's going on?
This example is 4 years old. In version 0.8.0 there was a implementation of many in the Data.Attoparsec.Combinator module, you can check the source here.
The current version of the library doesn't implements a many function, but it implements a many' function (source here). That's why you ghci give you many' as a suggestion.
The many that was implemented in Data.Attoparsec.Combinator is the same that is implemented in Control.Applicative (see here the implementation of many in the Alternative type class). You probably need to import the Control.Applicative. If that works I would suggest that you make a pull request to solve that issue (the library repository is here)

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.

How to resolve issues when getting error: Not in scope

*Main> :t putStrn
<interactive>:1:1:
Not in scope: `putStrn'
Perhaps you meant one of these:
`putStr' (imported from Prelude),
`putStrLn' (imported from Prelude)
Please note that I am practising haskell programming in emacs with haskell mode.
Even while using terminal, I am getting following error:
optimight#optimight:~$ ghci
GHCi, version 7.4.1: 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> :set prompt ghci>
ghci>:t putStrln
<interactive>:1:1:
Not in scope: `putStrln'
Perhaps you meant one of these:
`putStrLn' (imported from Prelude),
`putStr' (imported from Prelude)
The I/O action you are looking for is putStrLn. Note the capital L — Haskell symbols are case-sensitive — just before the final n, as in “put a string on a line of its own.”.

Haskell : loading ALL files in current directory path

The command (in GHCi)
:load abc
Loads the functions in the file abc (which must exist in the current directory path). How would I load all the files in the current directory path? Thanks
----------------------------------------------------------------------------------
[RESPONSE TO POST BELOW]
Hi Rotskoff, thanks I tried your suggestion but I could not get it to work, so I think I must have misunderstood something.
I created 3 files test.hs, test1.hs, and test2.hs as follows :
->
--test.hs
import NecessaryModule
->
--test1.hs
module NecessaryModule where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
->
--test2.hs
module NecessaryModule where
addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b
Then when I did :
:load test
I got the error message :
test.hs:1:8:
Could not find module `NecessaryModule':
Use -v to see a list of the files searched for.
Thanks
---------------------------------------------------------------------------------
Thanks. This is what I did to get it to work (following Rotskoff's suggestion) :
->
--test.hs
import NecessaryModule1
import NecessaryModule2
->
--NecessaryModule1.hs
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
->
--NecessaryModule2.hs
addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b
Presumably you mean Haskell source files, because you can't use the :load command in GHCi for anything else.
At the top of the source file that you load, include the line:
import NecessaryModule
For each of the source files, make sure to name the modules, e.g.,
module NecessaryModule where
should appear. GHCi will automatically link all the files.
If you're trying to import data, take a look at System.Directory in the documentation.
It's better if the filenames and the names of the modules will be the same:
➤ mv test1.hs NecessaryModule.hs
➤ 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.
Prelude> :load test
[1 of 2] Compiling NecessaryModule ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.
since the :load command load module(s) (by filenames) and their dependents (as you can read by typing :help or :? in the GHCi prompt).
Also the :load command erase all previous declarations which was defined in the current GHCi session, so for loading all files in the current directory you can do something like this:
Prelude> :q
Leaving GHCi.
➤ ghci *.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.
<no location info>:
module `main:NecessaryModule' is defined in multiple files: NecessaryModule.hs
test2.hs
Failed, modules loaded: none.
Prelude> :q
Leaving GHCi.
➤ rm test2.hs
➤ ghci *.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 2] Compiling NecessaryModule ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.
*NecessaryModule>

Resources