Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple import lines produces error in ghci

Tags:

haskell

ghci

This is what I've tried at the ghci REPL (stack ghci 8.10.7)

λ> :{
| import Data.List
| import Data.Ratio
| :}
error: expecting a single import declaration

Why can't I do more than one import at a time? BTW, can a complete module definition be entered this way, i.e.,

λ> :{
 | module STAL where
 | import Data.List
 | import Data.Ratio
 | import Data.Decimal
 | :}

My motivation is I'm using Emacs org-mode's babel for Haskell which only works with multiple-lined code when it's surrounded in :{ :}.

like image 592
147pm Avatar asked Dec 16 '21 16:12

147pm


People also ask

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.

How to import package 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 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 does GHCi mean in Haskell?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.


Video Answer


2 Answers

This sort of multiple-imports are currently not supported. However, there's a closed ticket asking for the same https://gitlab.haskell.org/ghc/ghc/-/issues/20473, and a merged patch that implements what you're asking for: https://gitlab.haskell.org/ghc/ghc/-/commit/7850142c09090a2eef1e1b0281acd641e843356a

I tested with GHC 9.2.1, which responded in the same way as you reported, so apparently the patch didn't make it to that release. But I suppose the next version coming out will have support for multiple imports like this.

like image 141
alias Avatar answered Oct 05 '22 20:10

alias


I just checked, and it turns out (on GHC 9.0.1 at least) that you can use ghci's :module command (:m for short) in multiline mode.

Prelude
λ :{
| :m Data.List Data.Ratio
| :}
Prelude Data.List Data.Ratio

This also allows :m + ... to add some modules, or :m - ... to remove some modules. And if you're talking about your own modules (source code available), you can use put a star in front of any module name to bring its internal stuff into scope.

You can't do more advanced imports though (qualified, hiding, or only import certain things).

like image 37
Ben Avatar answered Oct 05 '22 21:10

Ben