ghci vs prelude Haskell - haskell

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

Related

Importing documentCreateElement function from Webkit

I am trying to import documentGetElementById function for use in Reflex FRP. I tried the import below but can't find the function (which according to hackage should be there):
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Test.hs, interpreted )
Ok, modules loaded: Main.
*Main> import Graphics.UI.Gtk.WebKit.DOM.Document
*Main Graphics.UI.Gtk.WebKit.DOM.Document> :t documentGetElementById
<interactive>:1:1: error:
Variable not in scope: documentGetElementById
I will appreciate help with resolving this. I am running latest Reflex platform build with ghc 8.0.1 on mac where webkit works now. I see the same problem on Linux as well. So, it doesn't seem to be platform-specific.
I also did github code search on ghcjs-dom but documentGetElementById doesn't show up. So, perhaps it has been moved somewhere else?
documentGetElementById has been renamed to getElementById after ghcjs-dom refactoring. So, an import like below would work:
*Main> import Graphics.UI.Gtk.WebKit.DOM.Document
*Main Graphics.UI.Gtk.WebKit.DOM.Document> :t getElementById
getElementById
:: (DocumentClass self, System.Glib.UTFString.GlibString string,
MonadIO m) =>
self -> string -> m (Maybe Graphics.UI.Gtk.WebKit.Types.Element)

Haskell : unload module in WinGHCi

I loaded two modules (NecessaryModule1.hs and NecessaryModule2.hs as outlinked in Haskell : loading ALL files in current directory path). Now I want to unload NecessaryModule2.hs. I found an 'unload' function in System.Plugins.Load however but it did not work in WinGHCi. The error message I got was :
>unload NecessaryModule2
<interactive>:1:1: Not in scope: `unload'
<interactive>:1:8:
Not in scope: data constructor `NecessaryModule2'
I tried
import System.Plugins.Load
but that did not work. Is there a way to unload modules in the manner described above?
---------------------------------------------------------------------------------------
[RESPONSE TO Riccardo]
Hi Riccardo, I tried your suggestion but I could not get it to work in WinGHCi. I had a file NecessaryModule1.hs as follows :
module NecessaryModule1 where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
I went to the location of the file via the ':cd' command, and then did :
> :module +NecessaryModule1
<no location info>:
Could not find module `NecessaryModule1':
it is not a module in the current program, or in any known package.
Is this correct? Thanks [EDIT : see below for correction]
---------------------------------------------------------------------------------------
[CORRECTION TO ABOVE]
Just to explain why the above is incorrect (as explained by Riccardo), what needs to be done is the following :
If we have a file NecessaryModule1.hs as follows :
--NecessaryModule1.hs
module NecessaryModule1 where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
then we do :
> :load NecessaryModule1
[1 of 1] Compiling NecessaryModule1 ( NecessaryModule1.hs, interpreted )
Ok, modules loaded: NecessaryModule1.
> addNumber1 4 5
9
> :module -NecessaryModule1
> addNumber1 4 5
<interactive>:1:1: Not in scope: `addNumber1'
Installed modules
You have to use ghci's commands in order to load (:module +My.Module) and unload (:module -My.Module) installed modules. You can also use :m instead of :module in order to write less, like this:
Prelude> :m +Data.List
Prelude Data.List> sort [3,1,2]
[1,2,3]
Prelude Data.List> :m -Data.List
Prelude> sort [3,1,2]
<interactive>:1:1: Not in scope: `sort'
Remember that the ghci prompt always reminds you the module currently imported: you can have a look at that in order to know what to unload with :m -Module.To.Unload.
Specific files
If the module you're trying to load isn't installed in the system (e.g. you wrote the source and simply saved the file somewhere), you need to use a different command, :load filename.hs. A quicker way is to pass the path to the file directly as a command-line argument to ghci, e.g ghci filename.hs. If you run winghci and you associated it to the .hs extension, simply double-click the file.
In both cases you will get a ghci prompt with the specified module correctly loaded AND imported in scope (provided you don't have get compilation errors instead). As before, you can now use :m [+/-] My.Module to load and unload modules, but please note that this is different from :load because :module assumes you already :loaded what you're trying to get in/out of scope.
E.g., if you have test.hs
module MyModule where
import Data.List
f x = sort x
you may load it by double-clicking it (on windows with winghci), by typing ghci test.hs in a console, or by loading ghci and typing :load test.hs (beware of relative/absolute paths).
Another useful ghci command is :reload, which will recompile the module you loaded before. Use it when you change the source file and you want to quickly update the module loaded in ghci.
Prelude> :load test.hs
[1 of 1] Compiling MyModule ( test.hs, interpreted )
Ok, modules loaded: MyModule.
*MyModule> let xs = [1,2,3] in sort xs == f xs
True
*MyModule> :reload
Ok, modules loaded: MyModule.
:help will give you a complete list of all available commands.

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
>

How to configure GHCi to automatically import modules

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.

How can I import a Haskell module in GHCi?

I am trying to teach myself Haskell from the book Learn You A Haskell for Great Good. I got up to the last section of chapter 7 (Modules), where it tells how to create your own module. I did a copy and paste of the Geometry module given in the book at the beginning of the section. The name of the file is Geometry.hs, as the book suggested, and the file is in the bin directory for ghci, which is where I previously was able to successfully do a load using :l for another .hs file.
When I type the following command in GHCi
import Geometry
I get the following error:
Could not find module 'Geometry' It is not a module in the current
program or in any known package
I must be doing something that is obviously wrong, but I can't figure out what it is.
When you use import ModuleName in GHCi, it works (mostly) in the same way import Data.List works: GHC checks your local package database for the module, loads it, and brings its (exported) contents into scope.
However, Geometry isn't a module of a package installed with ghc-pkg. Therefore, GHC doesn't know that a module Geometry exists at all. Neither does it interactive variant GHCi.
But if you :load a program, things change. GHC will take its used modules into account:
-- Foo.hs
module Foo where
foo :: IO ()
foo = putStrLn "Hello from foo!"
-- Main.hs
module Main where
import Foo (foo)
main :: IO ()
main = foo
$ cd /path/to/your/files
$ ghci
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> import Foo
<no location info>:
Could not find module ‘Foo’
It is not a module in the current program, or in any known package.
Prelude> :l Main.hs
[1 of 2] Compiling Foo ( Foo.hs, interpreted )
[2 of 2] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main, Foo.
*Main> :l Main.hs
*Main> foo
Hello from foo!
*Main> import Foo
*Main Foo> -- module now loaded
As you can see, importing Foo first failed. However, after we've actually loaded the program that uses Foo, we were able to use import Foo in GHCi.
So if you want to use import in GHCi, make sure that GHC can find your module, either by including it in a wrapper or installing it. If you just want to load the module itself, use :load.
TLDR: the Learn you a Haskell book fails to mention that you have to :load the Geometry.hs file first. Then :m to go back to Prelude and then import Geometry works.
It is now also possible to add the lib flag when installing packages, i.e. to run cabal install --lib packagename and then to import the corresponding package directly in GHCi. In the present case, for example cabal install --lib hgeometry would facilitate importing modules from this geometry package.

Resources