Allow multiple imports and mixing of imports and declarations in GHCi
Motivation
Currently, in GHCi when an import is mixed with multiple declarations/other imports, it will spit out the following:
GHCi, version 9.2.0.20210821: https://www.haskell.org/ghc/ :? for help
ghci> :{
ghci| import Data.List
ghci|
ghci| xs = sort [2,1]
ghci| :}
error: expecting a single import declaration
This is an annoyance when pasting in a large snippet of code that includes import declarations, as they need to be run separately. It's also not a great error message.
(This can especially be an annoyance when using something like mniip's Eval bot on Discord.)
This behavior seems to have originated from #10663 (closed).
Proposal
Lift this restriction.
I already have a working patch that does this and makes the above code compile. It's also simpler than the current method. Rather than attempting to:
- Handle a single import
- Otherwise, check if there are imports present (to show the above error message)
- Otherwise, attempt to parse a module and only handle the declarations
The patch just parses a module and then handles both the imports and declarations.
One thing to note is that imports still need to come before declarations:
ghci> xs = [1,2]; import Data.List
<interactive>:1:13: error: parse error on input ‘import’
But this is already expected behavior when it comes to modules (perhaps the error message could be improved in the future, and the improvement would propagate to here).