ghci
Run Haskell code interactively
TLDR
Start a REPL (interactive shell)
Start a REPL and load the specified Haskell source file
Start a REPL and enable a language option
Start a REPL and enable some level of compiler warnings (e.g. all or compact)
Start a REPL with a colon-separated list of directories for finding source files
SYNOPSIS
ghci [OPTIONS...] [FILES...]
Note: Many options accepted by ghc (the compiler) are also recognized by ghci.
PARAMETERS
--version
Display the GHC/GHCi version information and exit.
--help
Display a summary of common command-line options and exit.
-v[N]
Verbose output. Controls the verbosity level, showing more about what GHCi is doing internally.
-idir
Add dir to the list of directories searched for Haskell source files, interface files, and libraries. Multiple -i options can be used.
-package pkg
Make the specified pkg (e.g., containers, text) available during the session. This is an alternative to specifying packages in a .cabal or stack.yaml file.
-Xextension
Enable a Haskell language extension (e.g., OverloadedStrings, DataKinds). Multiple -X flags can be specified.
-Wall
Enable all standard warnings.
-Werror
Treat all warnings as errors.
FILES...
One or more Haskell source files (.hs, .lhs) to load into the interactive session upon startup. These files will be compiled and their definitions will be available.
DESCRIPTION
ghci is the GHC interactive environment, a powerful Read-Eval-Print Loop (REPL) for the Haskell programming language. It is part of the Glasgow Haskell Compiler (GHC) suite. ghci allows developers to interactively evaluate Haskell expressions, experiment with code, load and reload source files, and debug programs without the need for explicit compilation steps.
It's an indispensable tool for rapid prototyping, understanding language features, and testing small code snippets. You can define functions, evaluate expressions, inspect types, and trace execution. When you load a Haskell source file, ghci compiles it in memory and makes its definitions available for interactive use. This immediate feedback loop significantly accelerates the development process, making ghci a cornerstone of most Haskell workflows, alongside the compiler itself and build tools like Cabal or Stack.
CAVEATS
Performance: Code executed in ghci is often interpreted or compiled with less aggressive optimizations than fully compiled executables. This can lead to slower execution times for computationally intensive tasks.
Module Reloading: While ghci supports reloading modules (:r), changes to dependencies or complex module hierarchies can sometimes lead to unexpected behavior or require a full restart of the session.
Interactive Commands: ghci's power lies significantly in its internal commands (e.g., :load, :type, :set). Users should be aware of the distinction between command-line options (passed on startup) and these interactive commands (typed within the REPL).
INTERACTIVE COMMANDS
ghci provides a rich set of interactive commands (prefixed with :) to manage the session:
:load files... (or :l): Load (and reload) specified Haskell source files.
:reload (or :r): Reload the currently loaded modules, useful after making changes to source files.
:type expression (or :t): Show the type of an expression.
:info name (or :i): Show information about a declared name (e.g., type, definition, modules).
:set option (or :s): Set options for the current session (e.g., +s for timing, -XExtension for language extensions).
:show context: Show various session settings (e.g., :show packages, :show languages).
:quit (or :q): Exit the ghci session.
:cd dir: Change the current working directory.
:! command: Execute a shell command.
CONFIGURATION FILES
ghci can be configured using files named .ghci. When ghci starts, it looks for and executes commands from ~/.ghci (global) and ./.ghci (local to the current directory). This allows for custom startup settings, function definitions, or common imports.
HISTORY
ghci has been an integral part of the Glasgow Haskell Compiler (GHC) since its early versions. Its development has closely tracked GHC's evolution, benefiting from new compiler features and performance improvements. Initially a simpler interactive environment, it has grown into a sophisticated REPL with advanced debugging, profiling, and introspection capabilities, becoming the de facto standard interactive tool for Haskell development. Its deep integration with GHC ensures that language features and optimizations are immediately available for interactive experimentation.