How to resolve issues when getting error: Not in scope - haskell

*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.”.

Related

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.

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>

A ghci session without Prelude

This question arose on #haskell irc chat:
How can I start ghci without importing prelude?
The possible answer seemed obvious:
ghci -XNoImplicitPrelude, or load a file with import Prelude ()
The latter seems to work, while the former strangely does not. However, import Prelude () imports the declared instances from Prelude, right? Is there a better way of creating a ghci session without loading Prelude at all?
% 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> :m -Prelude
> :i map
Top level: Not in scope: `map'
> :i Eq
Top level: Not in scope: data constructor `Eq'
However, I'm not sure about the instances and how ghci deals with them.
Is there a particular instance that you're concerned about?
The accepted answer doesn't seem to work anymore. This does work in ghci 8.0.2.
Prelude> :set -XNoImplicitPrelude
Prelude> :m -Prelude
>

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