How can I persist the environment between GHCi reloads?
Basically when I :load name.hs
the variables and bindings are gone.
Is there some option to tell g开发者_JS百科hci keep it all?
To load a new module, you can use
Prelude> :m + Mymodule
But reloading and keeping interactive bindings is not generally possible. Reloading is essentially forgetting all modules and loading them again. The bindings could depend on already loaded modules. The dependency logic dictates that when GHCI forgets a module, it also needs to forget everything that depends on it, including interactive bindings.
GHCI could e.g. store the text of commands that where used to create the bindings, and try to re-run those commands on reload, knowing that some of them might fail. But this can get hairy very quickly, so it's not being done.
I am adding yet another answer as this question was first google hit for me, whereas the real solution was further away (and I just hadn't checked rest of the hits, and only because of helpful people on IRC I have found it):
http://chrisdone.com/posts/ghci-reload
Basically, it's library that allows you to keep your data under a stable pointer with some C code underneath that survives GHCi reloads.
The example given on that blogpost is fairly complex, for someone that just wants to have some data ready in GHCi session the README from the library itself is better:
https://github.com/chrisdone/foreign-store/blob/master/README.md
It saved me a lots of time but my scenario was trivial: I wanted to have some assets loaded and ready while I manipulated the rest of the code. The types and structure for those assets is not changing at all.
I don't think this can be done with GHCi.
Googling, I found a readme which states that :reload maintained variables, but keeping bindings doesn't work for me:
*Main> let x = 1
*Main> :show bindings
x :: forall t. (Num t) => t = _
*Main> x
1
*Main> :reload
Ok, modules loaded: Main.
*Main> :show bindings
*Main> x
<interactive>:1:0: Not in scope: `x'
精彩评论