GHCi交互环境 #

一、GHCi简介 #

GHCi是GHC的交互式环境(REPL),允许你:

  • 交互式执行Haskell表达式
  • 加载和测试模块
  • 检查类型
  • 调试代码

1.1 启动GHCi #

bash
ghci

启动后显示:

text
GHCi, version 9.4.8: https://www.haskell.org/ghc/  :? for help
Prelude>

1.2 退出GHCi #

haskell
Prelude> :quit
-- 或简写
Prelude> :q
-- 或使用Ctrl+D

二、基本使用 #

2.1 表达式求值 #

haskell
Prelude> 1 + 2
3

Prelude> 2 * 3 + 4
10

Prelude> "Hello" ++ " World"
"Hello World"

Prelude> [1..10]
[1,2,3,4,5,6,7,8,9,10]

2.2 函数调用 #

haskell
Prelude> succ 5
6

Prelude> max 3 7
7

Prelude> min 3 7
3

Prelude> take 5 [1..]
[1,2,3,4,5]

Prelude> sum [1..10]
55

2.3 定义变量 #

haskell
Prelude> let x = 5
Prelude> x
5

Prelude> let y = x + 3
Prelude> y
8

Prelude> let double n = n * 2
Prelude> double 10
20

2.4 多行输入 #

使用 :{:} 输入多行代码:

haskell
Prelude> :{
Prelude| let factorial n
Prelude|     | n <= 0    = 1
Prelude|     | otherwise = n * factorial (n - 1)
Prelude| :}
Prelude> factorial 5
120

三、常用命令 #

3.1 帮助命令 #

haskell
-- 显示帮助
Prelude> :?
Prelude> :help

-- 显示命令列表
Prelude> :commands

3.2 类型相关命令 #

haskell
-- 查看类型
Prelude> :type "hello"
"hello" :: [Char]

Prelude> :t 42
42 :: Num a => a

Prelude> :t (+)
(+) :: Num a => a -> a -> a

-- 查看类型信息
Prelude> :info Int
type Int :: *
data Int = GHC.Types.I# GHC.Prim.Int#
...

Prelude> :i Maybe
type Maybe :: * -> *
data Maybe a = Nothing | Just a
...

3.3 模块相关命令 #

haskell
-- 加载模块
Prelude> :load MyModule.hs
Prelude> :l MyModule.hs

-- 重新加载
Prelude> :reload
Prelude> :r

-- 添加模块
Prelude> :module + Data.List
Prelude> :m + Data.List

-- 移除模块
Prelude> :m - Data.List

-- 设置上下文模块
Prelude> :m Data.List

3.4 浏览命令 #

haskell
-- 浏览模块
Prelude> :browse Data.List
(++) :: [a] -> [a] -> [a]
head :: [a] -> a
...

-- 浏览当前模块
Prelude> :browse

-- 查看定义
Prelude> :info map
map :: (a -> b) -> [a] -> [b]
...

3.5 其他实用命令 #

haskell
-- 执行Shell命令
Prelude> :! ls
Prelude> :! pwd

-- 显示当前目录
Prelude> :show paths

-- 设置选项
Prelude> :set +t    -- 自动显示类型
Prelude> :set +s    -- 显示时间和内存

-- 查看设置
Prelude> :show

-- 编辑文件
Prelude> :edit MyModule.hs
Prelude> :e MyModule.hs

四、调试功能 #

4.1 跟踪求值 #

haskell
-- 启用跟踪
Prelude> :set +t

-- 每个表达式后显示类型
Prelude> 5
5
it :: Num a => a

Prelude> "hello"
"hello"
it :: [Char]

4.2 时间和内存统计 #

haskell
-- 启用统计
Prelude> :set +s

-- 执行表达式后显示统计
Prelude> sum [1..1000000]
500000500000
(0.10 secs, 64,032,136 bytes)

4.3 断点调试 #

haskell
-- 加载带调试信息的模块
Prelude> :load MyModule.hs

-- 设置断点
Prelude> :break MyModule 10

-- 运行到断点
Prelude> :trace main

-- 继续执行
Prelude> :continue

-- 查看局部变量
Prelude> :show bindings

-- 单步执行
Prelude> :step

4.4 检查代码 #

haskell
-- 查看Core代码
Prelude> :set -ddump-simpl
Prelude> let f x = x + 1

-- 查看汇编代码
Prelude> :set -ddump-asm

五、GHCi配置 #

5.1 配置文件 #

创建 ~/.ghci 文件:

text
-- 显示类型
:set +t

-- 显示提示符
:set prompt "λ> "

-- 常用导入
:m + Data.List
:m + Data.Char
:m + Data.Maybe

-- 定义常用函数
let ll = load
let r = reload

5.2 启动选项 #

bash
-- 启动时执行命令
ghci -e "putStrLn \"Hello\""

-- 加载特定模块
ghci MyModule.hs

-- 设置包标志
ghci -package mypackage

5.3 自定义提示符 #

haskell
-- 在GHCi中设置
Prelude> :set prompt "ghci> "

-- 或在配置文件中
:set prompt-cont "   | "

六、实用技巧 #

6.1 快速测试 #

haskell
-- 测试函数
Prelude> let quicksort [] = []
Prelude|     quicksort (x:xs) = quicksort smaller ++ [x] ++ quicksort larger
Prelude|         where
Prelude|             smaller = [a | a <- xs, a <= x]
Prelude|             larger = [b | b <- xs, b > x]
Prelude| 
Prelude> quicksort [3,1,4,1,5,9,2,6]
[1,1,2,3,4,5,6,9]

6.2 使用it变量 #

haskell
Prelude> 1 + 2
3
Prelude> it * 2
6
Prelude> it + 10
16

6.3 历史记录 #

haskell
-- 使用上下箭头浏览历史
-- Ctrl+R 搜索历史

6.4 Tab补全 #

haskell
Prelude> map<tab>
map      mapM      mapM_      mapAccumL      mapAccumR

6.5 查找函数 #

haskell
-- 查找包含特定字符串的函数
Prelude> :! grep -r "sort" ~/.ghcup/...

七、常见问题 #

7.1 模块找不到 #

haskell
-- 安装包
Prelude> :! cabal install mypackage

-- 或使用Stack
Prelude> :! stack build mypackage

7.2 类型错误 #

haskell
-- 使用:type检查
Prelude> :type myFunction

-- 启用类型调试
Prelude> :set -fprint-explicit-foralls

7.3 内存不足 #

haskell
-- 增加堆大小
Prelude> :set +RTS -H128m -RTS

八、GHCi命令速查表 #

命令 简写 描述
:help :? 显示帮助
:quit :q 退出GHCi
:load :l 加载模块
:reload :r 重新加载
:type :t 显示类型
:info :i 显示信息
:browse :b 浏览模块
:module :m 设置模块
:edit :e 编辑文件
:! 执行Shell命令
:set 设置选项
:show 显示设置
:kind :k 显示类型种类
:doc 显示文档

九、总结 #

GHCi使用要点:

  1. 交互式求值:直接输入表达式
  2. 类型检查:使用 :t 查看类型
  3. 模块加载:使用 :l 加载文件
  4. 调试功能:设置断点和跟踪
  5. 配置优化:创建 .ghci 配置文件

熟练使用GHCi将大大提高Haskell开发效率。让我们继续学习Haskell的数据类型。

最后更新:2026-03-27