Search code examples
haskellghci

Can GHCI's :add command accept a path that is relative to one's home directory?


This is the GHC manual documentation for :add:

:add[*] ⟨module⟩

Add ⟨module⟩(s) to the current target set, and perform a reload. Normally pre-compiled code for the module will be loaded if available, or otherwise the module will be compiled to byte-code. Using the * prefix forces the module to be loaded as byte-code.

I can tell from experimentation that ⟨module⟩ doesn't actually have to be the name of a module; it can also be a file path. And if it is not an absolute path, it seems to be resolved relative to the current directory from which the REPL was launched.

Is it possible to refer :add a path that is relative to one's home directory?

For example, I'd like to include $HOME/.ghci/imports.hs from my global GHCI config. (For the motivation behind this, see Can a GHCI config file use CPP macros?)

I've tried the obvious

:add $HOME/.ghc/imports.hs

but it doesn't work:

<no location info>: error: can't find file: $HOME/.ghc/imports.hs
Failed, modules loaded: none.

Solution

  • Use ~ instead of $HOME.

    λ :add ~/Documents/Code/Haskell/Ackermann
    [1 of 1] Compiling Ackermann        ( /Users/rampion/Documents/Code/Haskell/Ackermann.hs, interpreted )
    Ok, modules loaded: Ackermann.