Skip to content

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:

import std.syntax.lambda
open std.syntax.lambda
open notation LambdaNotation

Lambda (syntax)

syntax Lambda

Constructors:

  • App: App : Term, Term -> Term
  • Arrow: Arrow : Type, Type -> Type
  • Base: Base : Name -> Type
  • Bind: Bind : Term, (x : Term).Term -> Term
  • Const: Const : Name, Type -> Term
  • Fst: Fst : Term -> Term
  • Lam: Lam : (x : Term).Term -> Term
  • Monadic: Monadic : Type -> Type
  • Pair: Pair : Term, Term -> Term
  • Product: Product : Type, Type -> Type
  • Return: Return : Term -> Term
  • Snd: Snd : Term -> Term
  • Unknown: 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.

example
expect Lambda.Term.normalize(App(Lam(Lambda.Term.var(0)), Const("c", Base("e")))) == Const("c", Base("e"))

Example: a projection of a pair.

example
expect Lambda.Term.normalize(Fst(Pair(Const("a", Base("e")), Const("b", Base("e"))))) == Const("a", Base("e"))

Example: printing through the notation.

example
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.

example
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.

example
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.

example
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.