Reputation: 22106
How could I do a reload and then evaluate last expression inside GHCI
?
By last expression
I mean the last thing you typed in GHCI which was not a GHCI command. For example: 42
, "I am an expression"
, etc.
Example usage, let's say the reload and then evaluate last expression GHCI command is :x
:
>>> 42
42
>>> :x
42
>>>
In example above :x
does a reload and then evaluates 42
again.
Is there any GHCI
command that evaluates last expression?
If there was some GHCI command that evaluates last expression, let's say it was :last
, I could have done the reload and evaluate last expression by adding following to .ghci
:
:def re const $ return $ Data.List.unlines [":reload", ":last"]
In the guide it says that :
does Repeat the previous command.
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html
But I tried that in cabal repl
and it did not work. Just prints a list of macros:
Upvotes: 2
Views: 381
Reputation: 3615
I wasn't able to find any description of this functionality in GHCi, and the fact that this is implemented using a wrapper arount GHCi in the Atom package IDE-Haskell-REPL leads me to believe that this is because GHCi can't do it on its own. GHCi also doesn't allow other processes to access its history during a session, since its history is only saved to a file (~/.ghc/ghci_history
on unix) at the end of a session, so scripting a GHCi session is tricky (but see below).
I think the closest you can get is by starting GHCi with a wrapper script using expect
, have it listen to a specific keypress sequence, and then send the ":r enter up up enter" keys for you:
#!/usr/bin/env expect
log_user 0
spawn ghci
log_user 1
while 1 {
interact "##" {send ":r\r\[A\[A\r"} #see note
exit
}
This way, whenever you hit ##
, GHCi will reload and run/evaluate whatever command or expression is first in the history. This is a admittedly a crude solution, and it might not be the safest, but I wanted to at least come up with an alternative.
Note: it looks like the control characters in the code block above don't render correctly, so you might have to get them from the markdown source of this answer, or from the output of the autoexpect command.
Upvotes: 1