Automatic reload of variable state in GHCi when reloading file

When I am developing some data analysis in pipelines in haskell, it is often helpful to preload the state of the variable into GHCi after loading.

What I am doing now is copying and pasting parts of the script one at a time into emacs just to inspect and inspect the output of some intermediate processing. I can't even jot down the copying code because line breaks aren't being passed (at least in Emacs Interactive-Haskell mode)

Is there a way to do this?

EDIT: Just loading / reloading the .hs file does not do the trick because afaik has no way to bind "<-" at the top level.

+3


source to share


2 answers


I suggest you take a look at foreign-store . It allows you to refer to variables by numbers, which persist across reboots. Here's an example:

λ: :set -XScopedTypeVariables 
λ: import Foreign.Store
λ: st <- newStore "example"
Loading package foreign-store-0.2 ... linking ... done.
λ: readStore st
"example"
λ: st
Store 0
λ: :r
Ok, modules loaded: none.
λ: st
<interactive>:8:1:
    Not in scope: ‘st’
    Perhaps you meant ‘fst’ (imported from Prelude)
λ: Just (st :: Store String) <- lookupStore 0
λ: readStore st
"example"

      



Alternatively, you can also put all your definitions in one hs file and reload them. You can use unsafePerformIO to get around the limitation that you cannot use <-

at the top level. I think this is ok in this case, since you only use it for interactive:

module Example where

import System.IO.Unsafe

example :: String 
example = "example"

file :: String
file = unsafePerformIO $ readFile "/tmp/example.hs"

      

+3


source


There are two main ways to do this:

  • Use the :l [filename]

    GHCi command to reload the file without exiting GHCi.
  • Make a ~/.ghci

    note of the variables in your file , which will be loaded when you open GHCi.


If you don't know what to put in ~/.ghci

, here's what I have in mine:

:set prompt "\955 "
:set prompt2 "| "

:def hoogle \x -> return $ ":!hoogle --info \"" ++ x ++ "\""

let f `on` x = \a b -> (f a) `x` (f b)
let break (f,g) = \a -> (f a, f g)

      

0


source







All Articles