type_theory

本手册记录的是 MoonBit 0.10 上 Luna-Flow/type_theory 的 0.2.0 版本。

概述

Luna-Flow/type_theory 是 Luna Flow 各符号计算包的语义基底:为每个含变量的 AST 共享一套关于名字、绑定、避免捕获的代换和重写的定义,并提供无类型与简单类型 λ 演算的参考实现,兼具小步归约与基于求值的范式化。

这些包构成层次;每个包只依赖于其上方的包:

core
 └─ syntax
     ├─ substitution
     ├─ rewrite ── eval
     │    └─ debruijn ── utlc/nbe
     └─ utlc/lambda (substitution, eval)
          └─ stlc (debruijn, utlc/lambda)

下游 AST 通过实现 trait BindingSyntax 从 syntax 接入,然后无需转换为本库自己的项类型即可使用泛型代换与重写。指南语义架构说明了各层如何组合在一起,以及各范式化器之间的关系。

安装

moon add Luna-Flow/type_theory@0.4.1

然后在你的 moon.pkg 中导入所需的包,例如:

import {
  "Luna-Flow/type_theory/core",
  "Luna-Flow/type_theory/syntax",
  "Luna-Flow/type_theory/substitution",
}

代码需要 MoonBit 工具链 0.10.20 或更高版本(moonc ≥ 0.10.20),并可在所有目标(wasm-gc、wasm、js、native)上构建。本库不依赖任何 Luna Flow 包;moonbitlang/quickcheck 仅用于其测试。

页面

每个包都有 API 参考(可以调用什么)、教程(如何使用)和设计说明(其背后的数学与决策)。每个包的 pkg.generated.mbti 文件是其公开名字的权威列表。

部分教程API设计
core:名字、新名字、上下文、伸缩式、重命名教程API设计
syntax:Term[T]、自由变量、α-等价、BindingSyntax教程API设计
substitution:同时、避免捕获的代换教程API设计
rewrite:带规则名与路径的单步重写、范式化、轨迹教程API设计
eval:正规序、应用序、弱头教程API设计
debruijn:无名项、转换、作用域、移位、β教程API设计
utlc/lambda:无类型 λ 演算,β 与 η教程API设计
utlc/nbe:受燃料限制的无类型基于求值的范式化教程API设计
stlc:双向类型检查、η-长形式的有类型 NbE教程API设计
adapter:针对下游 BindingSyntax AST 的契约测试教程API设计
指南:各层如何组合在一起语义架构

导出项

名字、项与代换

  • core: Name, fresh_name, Context, Telescope, Renaming
  • syntax: Term, free_variables, all_names, alpha_equal, Term::rename_free, Term::alpha_rename_bound, Term::map_values
  • syntax,供下游 AST 使用:BindingView、BindingSyntax、generic_free_variables、generic_all_names、generic_alpha_rename_bound
  • substitution: Substitution, GenericSubstitution, from_renaming

重写与策略

  • rewrite: RuleName, RuleNameError, ReductionFrame, ReductionPath, StepResult, NormalizationResult, ReductionTrace, top_down_once, bottom_up_once, normalize, trace
  • rewrite,供下游 AST 使用:GenericStepResult、GenericNormalizationResult、generic_top_down_once、generic_normalize
  • eval: Strategy, reduce_once, evaluate, trace

λ 演算

  • debruijn: DbTerm, ScopeError, from_named, to_named, validate, shift, substitute_bound, instantiate, reduce_once, normalize, DbStepResult, DbNormalizationResult
  • utlc/lambda: abstraction, application, beta_rule, eta_rule, beta_eta_rule, normalize
  • utlc/nbe: Semantic, eval, quote, normalize, reflect_free, reflect_level, EvaluationResult, QuoteResult, NbeResult
  • stlc: Atom, Term, Ty, Signature, TypeContext, TypeError, infer, check, normalize_checked, normalize_eta_long

已弃用

  • Context::extend、Telescope::extend(core)以及 Signature::extend、TypeContext::extend(stlc):请改用 extend_with。extend 自 MoonBit 0.10 起是保留字;旧名字作为已弃用的别名保留。
  • 初次使用本库:从 syntax 教程开始,然后是 substitution 和 rewrite。这三者足以安全地操作带绑定子的项。对于 λ 演算,请依次继续阅读 utlc/lambda、eval、debruijn、utlc/nbe 和 stlc。
  • 在库中使用:阅读 adapter 教程,它为一个小型表达式语言实现 BindingSyntax,并在其上使用泛型代换与重写。请把 API 页面放在手边,并参考 adapter 设计了解你的实现必须满足的定律。
  • 参与贡献:阅读设计页面;它们以数学方式定义每个操作,推导其定律,并说明每个包刻意不做什么。正确性检查清单记录了经审计的不变式与已知问题;对代换、α-等价、移位、β 实例化、quote 或燃料计量的修改都必须更新它。

验证

推荐的发布检查(在仓库根目录运行):

moon check --target all
./run_test.sh
moon info

./run_test.sh 在 wasm-gc、js、native 和 wasm 上运行 moon test。

使用者

诸如 luna-poly 和 floating 等下游 Luna Flow 仓库构建于这些包之上;它们各自的手册说明了具体方式。