std.syntax.lambda¶
The simply typed lambda calculus with products and a monad.
Lambda is a syntax with binding: its terms are stored with
de Bruijn indices, so alpha-equivalent terms are one value, and its declaration
generates substitution, shifting, equality, hashing, and printing for
each sort (Lambda.Term.subst.Term, Lambda.Term.eq, ...). Its
equations orient the computation rules of the calculus: beta for
functions, the projections of pairs, and the three monad laws for
Return and Bind; they compile to the normalizer
Lambda.Term.normalize. Types are a sort of the same syntax, so a
judgment over the calculus (std.syntax.types) relates terms and types of
one language.
LambdaNotation writes terms as on paper: λx. f x, ⟨a, b⟩, π₁ p,
η a for Return(a), and m >>= λx. n for Bind(m, x. n).
The examples on this page run in this module, which opens std.syntax.lambda with the imports and opens the module itself makes:
Lambda (syntax)¶
syntax Lambda
Constructors:
App:App : Term, Term -> TermArrow:Arrow : Type, Type -> TypeBase:Base : Name -> TypeBind:Bind : Term, (x : Term).Term -> TermConst:Const : Name, Type -> TermFst:Fst : Term -> TermLam:Lam : (x : Term).Term -> TermMonadic:Monadic : Type -> TypePair:Pair : Term, Term -> TermProduct:Product : Type, Type -> TypeReturn:Return : Term -> TermSnd:Snd : Term -> TermUnknown:Unknown : Name -> Type
The terms and simple types of the calculus. A constant carries its type, as in a Church-style calculus, so a typing judgment can read it.
Example: beta reduction to a constant.
expect Lambda.Term.normalize(App(Lam(Lambda.Term.var(0)), Const("c", Base("e")))) == Const("c", Base("e"))
Example: a projection of a pair.
expect Lambda.Term.normalize(Fst(Pair(Const("a", Base("e")), Const("b", Base("e"))))) == Const("a", Base("e"))
Example: printing through the notation.
expect Lambda.Term.show(Lam(Lam(App(Lambda.Term.var(1), Lambda.Term.var(0))))) == "λx0. λx1. x0 x1"
expect Lambda.Term.show(Lam(Bind(Lambda.Term.var(0), Return(Pair(Lambda.Term.var(0), Lambda.Term.var(1)))))) == "λx0. x0 >>= λx1. η ⟨x1, x0⟩"
Example: the left unit law of the monad.
expect Lambda.Term.normalize(Bind(Return(Const("a", Base("e"))), Return(Lambda.Term.var(0)))) == Return(Const("a", Base("e")))
Example: the right unit and associativity laws.
expect Lambda.Term.normalize(Bind(Const("m", Monadic(Base("e"))), Return(Lambda.Term.var(0)))) == Const("m", Monadic(Base("e")))
expect Lambda.Term.normalize(Bind(Bind(Const("m", Monadic(Base("e"))), Const("n", Base("e"))), Const("p", Base("e")))) == Bind(Const("m", Monadic(Base("e"))), Bind(Const("n", Base("e")), Const("p", Base("e"))))
Example: substitution and equality are generated.
expect Lambda.Term.eq(Lambda.Term.instantiate.Term(App(Lambda.Term.var(0), Lambda.Term.var(0)), Const("c", Base("e"))), App(Const("c", Base("e")), Const("c", Base("e"))))
expect Lambda.Term.occurs(Lam(Lambda.Term.var(1)), 0)
expect not Lambda.Term.occurs(Lam(Lambda.Term.var(0)), 0)
LambdaNotation (notation)¶
notation LambdaNotation for Lambda
The paper notation of the calculus's terms and types.