A Few ghci Tips

I recently ran across something new to me: there’s no need to use let in ghci since GHC 8.0.1 (which was released in 2016). You can just type x = 7 directly! I’ve been typing let every time for years…. This is a good opportunity to provide a few tips and tricks that make using ghci a little nicer.

:load and :reload (along with their shortened forms, :l and :r) load and reload modules. One nice feature is :reload!, which reloads what it can from a module (ignoring functions with type errors in their signatures, for example). This is super useful when you’re in the middle of writing a module and want to use part of it in ghci without having to fix every compiler error first.

The :show, :browse, and :info commands are useful for gaining information. :show provides general information—use it to see what’s currently imported, or which language extensions are in use. :show bindings gives the current bindings in scope of this ghci session. :browse lets you see what’s in a module. For example, :browse Data.Maybe will list out the functions and data constructors in the Data.Maybe module. :info is useful for things are in scope. If you’ve imported Data.Maybe, :info Maybe will show you the data constructor and instances that Maybe has.

I also recommend hooking in hoogle by adding a line like :def hoogle \x -> return $ ":!hoogle \"" ++ x ++ "\"" to your .ghci dotfile.

Use :set +t to see the types of expressions automatically. This can save you from having to type :t it all the time.

If you’ve accidentally imported some module you’d like to remove from scope, you can do so with :module -<module_name> For example, :module -Data.List will get rid of Data.List for you. This can help remove extra names that are polluting your environment.

Multiline input with ghci is actually pretty nice. Use :{ to begin a block and :} to close it. You can also do :set +m if you’d like a multiline input mode that’s automatically exited out of for you—just input a newline. :set +m will apply to all future commands, so if you enter lots of multiline commands you might consider leaving it on. You can always turn it off with :set -m

Finally, use :main arg1 arg2 to run main with arguments. You can also run other functions with specific arguments. To run f with argument arg1, do the command :run f arg1.


❮ Hyperintrospection
Facts learnable just by looking inside
Don't Forget Training ❯
Differences between learning, practice, and training