LambdaLens 是一个可视化函数式语言解释器与类型推导系统,其包含
- 一个迷你函数式语言 (lambda 演算 + let + if + 基本类型)
- Hindley–Milner 类型推导(算法 W)
- β-归约过程可视化
src/LambdaLens/
├── Syntax.hs # AST 定义(Expr, Type, Value)
├── Parser.hs # Megaparsec 解析器
├── TypeInfer.hs # HM 类型推导(算法 W)
├── Eval.hs # 大步语义求值器 (用于直接求值)
├── Stepper.hs # 单步规约追踪
├── Visualize.hs # 可视化输出
├── Error.hs # 错误类型
└── Api.hs # HTTP JSON API(Scotty)
在启动服务器后,提供了以下接口:
POST /api/tracePOST /api/evalPOST /api/typecheck
{ "expr": "(\\x -> x + 1) 3" }{
"steps": [
{ "index": 0, "expr": "((\\x -> x + 1)) 3", "rule": null },
{ "index": 1, "expr": "3 + 1", "rule": "β-reduction: x" },
{ "index": 2, "expr": "4", "rule": "δ-reduction: +" }
],
"type": "Int"
}{ "expr": "(\\x -> x + 1) 3" }{ "type": "Int" }{ "expr": "(\\x -> x + 1) 3" }{ "value": "4", "type": "Int" }所有接口在失败时返回 HTTP 400:
{ "error": "Cannot unify Int with Bool" }curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | shSet-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { & ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -Interactive -DisableCurl } catch { Write-Error $_ }或安装 WSL2(Windows Subsystem for Linux),再使用上面的 Linux 安装步骤。
# git
git clone https://github.com/X12101XX/lambdaLens.git
# 或 GitHub CLI
gh repo clone X12101XX/lambdaLens也可以使用 GitHub Desktop 来 clone。
cabal build
cabal run lambdaLens服务器启动后监听 http://localhost:3000。
# 单步追踪
curl -s -X POST http://localhost:3000/api/trace \
-H "Content-Type: application/json" \
-d '{"expr": "(\\x -> x + 1) 3"}'
# 类型推导
curl -s -X POST http://localhost:3000/api/typecheck \
-H "Content-Type: application/json" \
-d '{"expr": "\\x -> x + 1"}'
# 求值
curl -s -X POST http://localhost:3000/api/eval \
-H "Content-Type: application/json" \
-d '{"expr": "(\\x -> x + 1) 3"}'