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
>
Related
I have recently started learning Haskell and had some questions about ghci> prompt vs prelude> prompt?
When typing ghci i get this:
ghci
GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help
ghci>
when checking documentation I see things like this:
$ ghci
GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude>
also in addition to this I do not get into a module once I've loaded it
ghci> :load main
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
ghci>
should look something like this?
ghci> :load main
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
*Main>
I have tried looking online, tried compiling the program
GHCi is less verbose since version 9.0. This is because after a while the list of modules became very long, making the shell less effective.
Therefore since version 9.0, it keeps showing the ghci> prompt by default. You can set the prompt with %s> [ghc-doc] to show the loaded modules instead:
ghci> :set prompt "%s> "
Prelude> import Data.List
Prelude Data.List>
This was changed in GHC 9.0.1. Earlier versions showed the modules that were loaded, but now it just shows ghci>.
See the release notes for GHC 9.0.1:
2.1.2.3. GHCi
GHCi prompt no longer lists loaded modules. The previous behavior can be restored with :set prompt "%s> " and :set prompt-cont "%s| ".
A made a module Timeit. I can't import it to GHCi.
Module:
module Timeit (timeit, timeCatch) where
import Data.Time.Clock
timeit::IO ()->IO (Float)
timeit io=do
(time,())<-timeCatch io
return time
timeCatch::IO (a)->IO (Float,a)
timeCatch io=do
start <-getCurrentTime
result <-io
end <-getCurrentTime
return $! (realToFrac (diffUTCTime end start), result)
test=do
putStrLn "What is your name?"
name <- getLine
putStrLn $ "Your name is "++name++"."
GHCi:
theking#ChrisLaptopUbuntu1304:~/Desktop/Haskell$ cd ~/Desktop/Haskell/; 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> import Timeit
<no location info>:
Could not find module `Timeit'
Perhaps you meant Time (needs flag -package haskell98-2.0.0.2)
I am able to import it into my other programs, just not GHCi.
Note: Am I a haskell noob.
In order for a module to be imported by GHCi, you have to make sure a few things are true.
First, are you in the same directory? By default GHCi will only search the current directory for modules.
Second, have you added the module header? Your code should start with
module Timeit where
...
Third, your file must actually be named Timeit.hs (with that capitalization).
By default Haskell inserts module Main where, which is a problem if your module isn't main.
Last but not least, GHCi seems to require that you use :l Timeit at least once. I'm not sure why this is, but once loaded you can remove it from scope with :m and then import it to your hearts content.
If you've done these things it should import just fine.
To import modules from ghci don't use import, rather say
:m +TimeIt
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)
*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.”.
When I use GHCi, I almost always end up importing Control.Applicative, Data.List, etc. . Is there a way to configure GHCi to automatically import those modules.
Also, after importing them, how do I keep the prompt from being insanely long?
Prelude Control.Applicative Data.List Database.HDBC Database.HDBC.Sqlite3 System.Directory>
GHCi looks for its configuration file at
~/.ghc/ghci.conf on Unix-like systems.
%APPDATA%\ghc\ghci.conf on Windows.
The configuration file syntax is simple: it's a list of GHCi commands to execute on startup.
For example, your ghci.conf could contain:
import Control.Applicative
import Data.Char
import Data.List
:set prompt "> "
The last line sets the prompt to "> " so it won't show all the modules you imported on the command line.
Now you can get to work right away:
GHCi, version 6.12.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.
> toLower <$> "Hello, world!"
"hello, world!"
>
Also, if you decide you don't want Data.Char in the middle of a GHCi session, you can remove it with:
:m -Data.Char
and if you decide you don't want anything but Prelude during a session:
:m
GHC will also load any .ghci file it finds in the current directory. It's very useful to do per-project configuration of GHCi.
This is an example from a project I work on:
:set -isrc:dist/build/autogen
:set -hide-package mtl
The first is there to make sure that the modules generated by Cabal are easy to import. The second hides mtl since this particular project uses transformers.