Skip to content

std.syntax.types

Simple types of std.syntax.lambda: their unification, and the typing judgment of the calculus.

A type with unknowns, Unknown("a"), stands for every type it can be unified to. unify computes the most general substitution that makes two types equal, binding unknowns to types, and refuses a binding that would make a type contain itself (the occurs check), so it terminates on every input and its result, when there is one, is a solution every other solution refines (Robinson 1965). A substitution is a finite map from unknowns to types, kept in triangular form: resolve applies it completely.

Ty is the typing judgment of the calculus: a context of the types of the free variables, a term, and its type. It runs as a checker and an inferencer on the host, through Program.judgments["Ty"]. A constant Const(n, A) carries its type, so it has that type in every context.

The examples on this page run in this module, which opens std.syntax.types with the imports and opens the module itself makes:

import std.syntax.types
open std.syntax.types
import std.data as D
import std.syntax.lambda as L
open L

Imports: std.data, std.syntax.lambda

Substitution (family)

family Substitution : Type

Constructors:

  • Bindings: constructor Bindings : D.FiniteMap[String, Lambda.Type] -> Substitution

A substitution of types for unknowns.

Example: a binding resolves its unknown.

example
expect resolve(s=bind(s=empty(), name="a", t=Base("e")), t=Arrow(Unknown("a"), Unknown("b"))) == Arrow(Base("e"), Unknown("b"))

Ty (judgment)

judgment Ty(ctx : Context[Type] in, t : Term in, ty : Type out)

The typing judgment of the calculus: in a context of the types of the free variables, a term has a type. A constant has the type it carries.

bind (computation)

define bind(s : Substitution, name : String, t : Lambda.Type) : Substitution !{}

The substitution with the unknown name bound to t; name must be unbound in it.

Example: binding an unknown.

example
expect resolve(s=bind(s=empty(), name="a", t=Base("e")), t=Unknown("a")) == Base("e")

empty (computation)

define empty() : Substitution !{}

The substitution that binds nothing.

Example: nothing bound.

example
expect resolve(s=empty(), t=Unknown("a")) == Unknown("a")

occurs (computation)

define occurs(s : Substitution, name : String, t : Lambda.Type) : Bool !{}

Whether the unknown name occurs in t once s is applied.

Example: an unknown inside an arrow.

example
expect occurs(s=empty(), name="a", t=Arrow(Base("e"), Unknown("a")))
expect not occurs(s=empty(), name="a", t=Monadic(Unknown("b")))

resolve (computation)

define resolve(s : Substitution, t : Lambda.Type) : Lambda.Type !{}

The type with every bound unknown replaced, until none is left.

Example: a chain of bindings.

example
expect resolve(s=bind(s=bind(s=empty(), name="a", t=Unknown("b")), name="b", t=Base("e")), t=Arrow(Unknown("a"), Unknown("c"))) == Arrow(Base("e"), Unknown("c"))

unify (computation)

define unify(a : Lambda.Type, b : Lambda.Type, s : Substitution) : D.Option[Substitution] !{}

The most general extension of s that makes a and b equal, if there is one. Unifying an unknown with a type that contains it fails.

Example: unifying two arrows.

example
expect resolve(s=D.option_get_or(A=Substitution, o=unify(a=Arrow(Unknown("a"), Base("t")), b=Arrow(Base("e"), Unknown("b")), s=empty()), default=empty()), t=Product(Unknown("a"), Unknown("b"))) == Product(Base("e"), Base("t"))

Example: the occurs check.

example
expect not D.is_some(A=Substitution, o=unify(a=Unknown("a"), b=Arrow(Unknown("a"), Base("e")), s=empty()))

Example: a clash of constructors.

example
expect not D.is_some(A=Substitution, o=unify(a=Base("e"), b=Monadic(Base("e")), s=empty()))