Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure GHCi to automatically import modules

Tags:

haskell

ghci

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> 
like image 852
Joey Adams Avatar asked Aug 19 '10 03:08

Joey Adams


People also ask

How do I import modules into 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 to load a file in GHCi?

GHCi is the interactive interface to GHC. 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 does GHCi work?

GHCi will discover which modules are required, directly or indirectly, by the topmost module, and load them all in dependency order. If you started up GHCi from the command line then GHCi's current directory is the same as the current directory of the shell from which it was started.

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.


2 Answers

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 
like image 163
Joey Adams Avatar answered Sep 28 '22 00:09

Joey Adams


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.

like image 30
Magnus Avatar answered Sep 27 '22 23:09

Magnus