147pm
147pm

Reputation: 2237

Multiple import lines produces error in 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 :{ :}.

Upvotes: 9

Views: 250

Answers (2)

Ben
Ben

Reputation: 71400

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

Upvotes: 2

alias
alias

Reputation: 30418

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.

Upvotes: 5

Related Questions