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:
- `stack repl` starts GHCi with a Stack project loaded. section 2.1.2
- `:reload` or `:r` reloads loaded modules after file changes and clears local REPL bindings. section 2.3.1 section 2.6
- `:type` or `:t` reports the type of a value or function. section 2.3.1 section 2.6
- `:info` or `:i` reports information about a named definition or type. section 2.3.1 section 2.6
