Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module loading options in GHCi

Tags:

haskell

ghci

Why does GHCi have so many different commands for loading modules into a session?

Here are ones I'm aware of, together with their rather confusing explanations from :help in ghci:

  • add [*]<mod> -- add module(s) to the current target set
  • :module [+/-] [*]<mod> -- set the context for expression evaluation
  • :load [*]<mod> -- load module(s) and their dependents
  • :reload <mod> -- reload the current module set. (Note: :help doesn't say that this can take a module argument, but it seems that it can.)
  • import Mod

What do they each do? How does the asterisk change it? Why are there so many? :(

like image 574
hdgarrood Avatar asked Jan 08 '14 21:01

hdgarrood


People also ask

How do I load a module in Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

How do I load files into Ghci?

From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.

How do I load a Haskell file into Ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

What is Prelude in Ghci?

Prelude is a module that contains a small set of standard definitions and is included automatically into all Haskell modules.


1 Answers

There are essentially two different concepts at work here: target set and evaluation context.

You work with the target set with :add, :load and :reload, and you work with the evaluation context with :module and import.

The target set is the list of locally available modules that ghci is reading and compiling. Generally you would use this to specify the source that you're working on. ghci will load and compile each of these modules and any dependencies it needs.

You use :load to reset the target set to precisely the given set of modules, and :add to add the given modules to the existing target set.

If you specify modules in the target set with * then they will always be "bytecode-interpreted" - which means they load quickly but don't run as fast as compiled code - otherwise ghci will use a compiled object file if available and bytecode interpret if not.

:reload on its own reloads the entire target set and dependencies. I'm not quite sure exactly what :reload <mod> does but I think it reloads <mod> and dependencies and leaves the rest untouched.

The evaluation context controls what names are in scope at the prompt, and is fully documented here. Modules for adding to the evaluation context can either be local ones in the target set+dependencies, or be "package" ones that have been previously registered with ghc using the ghc-pkg tool (or cabal which calls that eventually). import Foo is just another way of writing :module +Foo.

NOTE: this answer is a mixture of my intuitive understanding from my experience of using ghci and just reading the documentation and trying to integrate the two in my mind. So I may well not have got this 100% accurate!

like image 181
GS - Apologise to Monica Avatar answered Sep 17 '22 16:09

GS - Apologise to Monica