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.

PartTutorialAPIDesign
core: names, fresh names, contexts, telescopes, renamingstutorialAPIdesign
syntax: Term[T], free variables, alpha-equivalence, BindingSyntaxtutorialAPIdesign
substitution: simultaneous capture-avoiding substitutiontutorialAPIdesign
rewrite: single steps with rule names and paths, normalization, tracestutorialAPIdesign
eval: normal order, applicative order, weak headtutorialAPIdesign
debruijn: nameless terms, conversion, scope, shifting, betatutorialAPIdesign
utlc/lambda: untyped lambda calculus, beta and etatutorialAPIdesign
utlc/nbe: fuel-bounded untyped normalization by evaluationtutorialAPIdesign
stlc: bidirectional type checking, eta-long typed NbEtutorialAPIdesign
adapter: contract tests for downstream BindingSyntax ASTstutorialAPIdesign
Guide: how the layers fit togetherSemantic architecture

Exported items

Names, terms and substitution

  • 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, for downstream ASTs: BindingView, BindingSyntax, generic_free_variables, generic_all_names, generic_alpha_rename_bound
  • substitution: Substitution, GenericSubstitution, from_renaming

Rewriting and strategies

  • rewrite: RuleName, RuleNameError, ReductionFrame, ReductionPath, StepResult, NormalizationResult, ReductionTrace, top_down_once, bottom_up_once, normalize, trace
  • rewrite, for downstream ASTs: GenericStepResult, GenericNormalizationResult, generic_top_down_once, generic_normalize
  • eval: Strategy, reduce_once, evaluate, trace

Lambda calculi

  • 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

Deprecated

  • Context::extend, Telescope::extend (core) and Signature::extend, TypeContext::extend (stlc): use extend_with. extend is a reserved word since MoonBit 0.10; the old names remain as deprecated aliases.
  • 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 BindingSyntax for 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.