type_theory
This manual documents version 0.2.0 of Luna-Flow/type_theory on MoonBit 0.10.
Overview
Luna-Flow/type_theory is the semantic substrate of Luna Flow’s symbolic
packages: one definition of names, binding, capture-avoiding substitution and
rewriting, shared by every AST that has variables, plus reference
implementations of the untyped and simply typed lambda calculi with both
small-step reduction and normalization by evaluation.
The packages form layers; each depends only on the ones above it:
core
└─ syntax
├─ substitution
├─ rewrite ── eval
│ └─ debruijn ── utlc/nbe
└─ utlc/lambda (substitution, eval)
└─ stlc (debruijn, utlc/lambda)
Downstream ASTs enter at syntax by implementing the trait BindingSyntax
and then use generic substitution and rewriting without converting to the
library’s own term type. The guide Semantic architecture
explains how the layers fit together and how the normalizers relate.
Install
moon add Luna-Flow/type_theory@0.4.1
Then import the packages you need in your moon.pkg, for example:
import {
"Luna-Flow/type_theory/core",
"Luna-Flow/type_theory/syntax",
"Luna-Flow/type_theory/substitution",
}
The code needs the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20) and
builds on all targets (wasm-gc, wasm, js, native). The library has no
Luna Flow dependencies; moonbitlang/quickcheck is used by its tests only.
Pages
Every package has an API reference (what you can call), a tutorial (how to use
it) and a design note (the mathematics and the decisions behind it). The
pkg.generated.mbti file of each package is the authoritative list of its
public names.
| Part | Tutorial | API | Design |
|---|---|---|---|
core: names, fresh names, contexts, telescopes, renamings | tutorial | API | design |
syntax: Term[T], free variables, alpha-equivalence, BindingSyntax | tutorial | API | design |
substitution: simultaneous capture-avoiding substitution | tutorial | API | design |
rewrite: single steps with rule names and paths, normalization, traces | tutorial | API | design |
eval: normal order, applicative order, weak head | tutorial | API | design |
debruijn: nameless terms, conversion, scope, shifting, beta | tutorial | API | design |
utlc/lambda: untyped lambda calculus, beta and eta | tutorial | API | design |
utlc/nbe: fuel-bounded untyped normalization by evaluation | tutorial | API | design |
stlc: bidirectional type checking, eta-long typed NbE | tutorial | API | design |
adapter: contract tests for downstream BindingSyntax ASTs | tutorial | API | design |
| Guide: how the layers fit together | Semantic architecture |
Exported items
Names, terms and substitution
core:Name,fresh_name,Context,Telescope,Renamingsyntax:Term,free_variables,all_names,alpha_equal,Term::rename_free,Term::alpha_rename_bound,Term::map_valuessyntax, for downstream ASTs:BindingView,BindingSyntax,generic_free_variables,generic_all_names,generic_alpha_rename_boundsubstitution:Substitution,GenericSubstitution,from_renaming
Rewriting and strategies
rewrite:RuleName,RuleNameError,ReductionFrame,ReductionPath,StepResult,NormalizationResult,ReductionTrace,top_down_once,bottom_up_once,normalize,tracerewrite, for downstream ASTs:GenericStepResult,GenericNormalizationResult,generic_top_down_once,generic_normalizeeval:Strategy,reduce_once,evaluate,trace
Lambda calculi
debruijn:DbTerm,ScopeError,from_named,to_named,validate,shift,substitute_bound,instantiate,reduce_once,normalize,DbStepResult,DbNormalizationResultutlc/lambda:abstraction,application,beta_rule,eta_rule,beta_eta_rule,normalizeutlc/nbe:Semantic,eval,quote,normalize,reflect_free,reflect_level,EvaluationResult,QuoteResult,NbeResultstlc:Atom,Term,Ty,Signature,TypeContext,TypeError,infer,check,normalize_checked,normalize_eta_long
Deprecated
Context::extend,Telescope::extend(core) andSignature::extend,TypeContext::extend(stlc): useextend_with.extendis a reserved word since MoonBit 0.10; the old names remain as deprecated aliases.
Where to read next
- New to the library: start with the syntax tutorial, then substitution and rewrite. These three are enough to manipulate terms with binders safely. For the lambda calculi, continue with utlc/lambda, eval, debruijn, utlc/nbe and stlc in that order.
- Using it in a library: read the adapter tutorial,
which implements
BindingSyntaxfor a small expression language and uses generic substitution and rewriting on it. Keep the API pages at hand, and the adapter design for the laws your implementation must satisfy. - Contributing: read the design pages; they define every operation mathematically, derive its laws, and state what each package deliberately does not do. The correctness checklist records the audited invariants and known issues; changes to substitution, alpha-equivalence, shifting, beta instantiation, quote or fuel accounting must update it.
Validation
Recommended release checks, from the repository root:
moon check --target all
./run_test.sh
moon info
./run_test.sh runs moon test on wasm-gc, js, native and wasm.
Used by
Downstream Luna Flow repositories such as luna-poly and floating build on these packages; their own manuals describe how.