How to configure GHCi to automatically import modules

假如想象 提交于 2019-11-28 18:35:19

问题


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>

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/3518619/how-to-configure-ghci-to-automatically-import-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!