liveBook · Manning

GHCi

As explained in Learn Haskell by Example

GHCi is the interactive interpreter and read-evaluate-print environment for Haskell provided with the Glasgow Haskell Compiler.

GHCi is the interactive, interpreted mode of the Glasgow Haskell Compiler: a read–eval–print loop (REPL) for trying Haskell expressions, definitions, and queries immediately. In a Stack project, `stack repl` starts GHCi with project modules and definitions loaded, making it a practical companion to editing `src/Lib.hs`. section 2.1.2

stack repl

Start an interactive session from the project directory with Stack. section 2.1.2

At the `ghci>` prompt, each input is evaluated. Bind names to values, evaluate those names later, and attach explicit types with `::`. An assignment itself produces no result, while evaluating a bound variable prints its value. Untyped arithmetic such as `1 + 1` may produce type-defaulting warnings; `:set -Wno-type-defaults` disables them. section 2.1.2

Try bindings and a function

Define `a`, `b`, and `c`, then evaluate `c` to inspect the result. A function can be written as `square x = x * x` and called as `square 2`. For a multiline declaration entered directly at the prompt, wrap the definition between `:{` and `:}` before running it. section 2.1.2

ghci> :{               #1
ghci| square :: Int -> Int
ghci| square x = x * x
ghci| :}       #2
ghci> square 2
4

Enter a typed multiline function definition, then evaluate it without leaving GHCi. section 2.1.2

Use GHCi to inspect code saved in project files as well as expressions typed at the prompt. After changing definitions in `Lib.hs`, run `:reload` (or `:r`) to re-import changed modules. Reloading clears locally entered definitions and values, so treat it like closing and reopening the session. section 2.3.1 section 2.6

Inspect an alias after reloading

After defining `lowerAlphabet`, `upperAlphabet`, and `digits` as `Alphabet` values, reload and evaluate them to see their characters. Use `:type lowerAlphabet` (or `:t`) to confirm that the value has type `Alphabet`, even though it prints as a `String`. Use `:info` (or `:i`) to obtain information such as the definition of `FilePath`. section 2.3.1 section 2.6

ghci> :type lowerAlphabet
lowerAlphabet :: Alphabet
ghci> :type upperAlphabet
upperAlphabet :: Alphabet
ghci> :type digits
digits :: Alphabet

Query the types of loaded alphabet definitions with `:type`. section 2.3.1

Core interactive commands are:

How Learn Haskell by Example covers it

How deep it goes
Demonstrates startup, evaluation, multiline definitions, reloading, and `:type` and `:info` queries without describing interpreter internals.
Examples
Uses arithmetic bindings, a `square` function, alphabet aliases, and `FilePath` inspection at the prompt.
What it assumes
Teaches REPL use from basic arithmetic and function definitions while assuming a Stack-created project for automatic module loading.
What you can do after
Operate/configure it to run project code, reload edited modules, and query types and definitions interactively.
How it connects
Connects GHCi with Stack, type annotations, type synonyms, functions, modules, and lists.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.