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 :{ :}
.
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.
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.
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.)
Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With