Object languages: syntax, equations, and judgments¶
A syntax declaration defines a language of terms with binding, such as the lambda calculus or a
logic of meanings. Its equations block states which terms are equal and how to compute normal forms,
and a judgment declaration defines a relation over its terms, such as a typing relation. The
mathematics is in Tree and graph autoencoders §1,
Equations and normalization, and
Judgments; this page gives the surface.
Syntax declarations¶
syntax Lambda
sorts
Term : object
Type : object
Name : data
constructors
Base : Name -> Type
Arr : Type, Type -> Type
Const : Name -> Term
App : Term, Term -> Term
Lam : (x : Term).Term -> Term
Let : Term, (x : Term).Term -> Term
TLam : (ty : Type), (x : Term : ty).Term -> Term
Each argument of a constructor is one of:
| Form | Meaning |
|---|---|
S |
an argument of sort S |
(l : S) |
an argument of sort S labeled l, which a later bound variable may name as its annotation |
(x_1 : V_1, ..., x_n : V_n).S |
an argument of sort S in whose scope the variables x_1, ..., x_n of sorts V_i are bound; the last is the innermost |
(x : V : l).S |
a bound variable annotated by the earlier labeled argument l |
The binding arity is per argument, so Let binds x in its second argument only. Bound variable
names are display names: terms are stored with de Bruijn indices, so alpha-equivalent terms are
equal. A constructor with no arguments is written C : -> S. The sorts block gives each sort the
kind object, data (with an optional vocabulary [vocab=["a", "b"]]), or index.
Terms of a syntax may also be written in user notation, [| λx. x |], with binders by name; see
Notation and quotation.
Use syntax when you want named binders in source and a separate typing judgment.
For an intrinsically typed representation, an indexed family can encode the typing
rules in its constructors. The weighted CCG example
uses the standard-library term builder
with its own index of semantic types. The family Bound(g, t) selects a binder
of type t in context g. Here and There
are defined once for all variables. Each Lam extends the context, and repeated
There steps reach any enclosing binder. QVR infers their static arguments from
the surrounding term.
import std.syntax.typed_lambda as L
open L
define first() : Term(Empty, Arr(Base, Arr(Next(Base), Base))) !{} =
return Lam(Lam(Var(V1())))
This builds \(\lambda x.\,\lambda y.\,x\). The type of x is Base, and the
type of the skipped binder y is Next(Base).
std.syntax.typed_lambda provides
define_named, a declaration splice that resolves named binders before
checking the term.
Here is the same definition written with names and a locally declared index.
import std.syntax.typed_lambda as Terms
$Terms.declare_terms(([| index TypeCode = Base | Next(TypeCode) |] : SynDecl))
$Terms.define_named(([|
define first() : Term(Empty, Arr(Base, Arr(Next(Base), Base))) !{} =
return x -> y -> x
|] : SynDecl))
Run the file defining the local index as the root module when using these splices. References introduced by a splice after module flattening use that root scope.
The families of a syntax¶
Each object sort S of a syntax Σ is a checked family Σ.S whose constructors are the syntax's
constructors into S, plus the variable constructor Σ.S.var(index : Int). A program, define,
encoder, or decoder uses these values like any other family: it builds them with constructors, takes
them apart with case, and passes them as arguments.
define size(t : Lambda.Term) : Int !{} =
case t motive => Int
Lambda.Term.var(i) =>
return 1
Const(n) =>
return 1
App(f, a) =>
let left <- size(f)
let right <- size(a)
return left + right + 1
Lam(body) =>
let inner <- size(body)
return inner + 1
Let(value, body) =>
let left <- size(value)
let right <- size(body)
return left + right + 1
TLam(ty, body) =>
let inner <- size(body)
return inner + 1
A bound argument holds its body, whose variable 0 is the innermost bound variable. On the host,
Program.run accepts and returns Term values for these
families, and Program.syntaxes maps each syntax to its
Signature.
Generated term operations¶
For each object sort S of a syntax Σ, the module contains these checked computations, generated
from the declaration:
| Computation | Meaning |
|---|---|
Σ.S.shift(t, cutoff, amount) |
add amount to every free index of t at or above cutoff |
Σ.S.subst.V(t, index, value) |
substitute value of sort V for the variable index, weakening value under binders and lowering the indices above index |
Σ.S.instantiate.V(t, value) |
substitute for the innermost variable of a scope body |
Σ.S.occurs(t, index) |
whether the variable index is free in t |
Σ.S.eq(a, b) |
alpha-equality, which is structural equality on de Bruijn terms |
Σ.S.ord(a, b) |
a total order agreeing with eq, as -1, 0, or 1 |
Σ.S.hash(t) |
a hash agreeing with eq |
Σ.S.show(t) |
the term printed with fresh variable names |
eq, ord, and hash are generated when every data sort of the syntax holds one type of value, and
show when every data sort holds strings or integers; Σ.Names then holds the display names of the
bound variables. Each operation agrees with its host counterpart in
quivers.structural.binding.
Equations¶
syntax Lambda
sorts
Term : object
Name : data
constructors
Const : Name -> Term
Lam : (x : Term).Term -> Term
App : Term, Term -> Term
Pair : Term, Term -> Term
Fst : Term -> Term
equations [strategy=nodewise]
beta : App(Lam(x. M[x]), N) ~> M[N]
fst : Fst(Pair(A, B)) ~> A
assume terminating
| Form | Meaning |
|---|---|
name : l ~> r |
an oriented rule, used left to right; l must be a Miller pattern |
name : l = r |
a law, used in both directions |
x y. body |
a scope binding x and y in body, as an argument of a constructor that binds them |
M[a, b] |
the metavariable M of arity two, instantiated at a and b |
C(n) at a data position |
the data metavariable n: it matches the data value there, takes no arguments, and must not also stand for a term |
assume terminating |
record termination of the rules as an assumed law instead of proving it |
The block's options are:
| Option | Values | Meaning |
|---|---|---|
strategy |
innermost (default), outermost, nodewise |
where rewriting contracts first |
cost |
size, depth |
the extraction cost for laws decided by equality saturation |
typing |
a judgment name | the typing judgment that eta, surjective pairing, and monad laws are normalized over |
At compile time the rules are checked: their termination is proved by a recursive path order or assumed, every critical pair must join, and a law is routed to normalization by evaluation or to equality saturation by its shape (Equations and normalization). A failure is reported at the syntax or the equation, naming the rule and, for a critical pair, the peak and the two normal forms.
The checked theory is Program.theories[name], a
SignatureTheory with normalize and equal_modulo.
When the rules are in the supported fragment, the module also contains Σ.S.rewrite,
Σ.S.normalize, Σ.S.equal_modulo, Σ.S.step under strategy=outermost, and the normalizing
builders Σ.S.build.C under strategy=nodewise
(Equations and normalization §8).
Judgments¶
A judgment over the Lambda syntax above:
judgment Ty(ctx : Context[Type] in, t : Term in, ty : Type out)
rule var : lookup(ctx, i, A) |- Ty(ctx, Term.var(i), A)
rule lam : Ty((ctx, x : A), M[x], B) |- Ty(ctx, Lam(x. M[x]), Arr(A, B))
rule app : Ty(ctx, F, Arr(A, B)), Ty(ctx, N, A) |- Ty(ctx, App(F, N), B)
rule c : |- Ty(ctx, Const("c"), Base("o")) #[weight=0.5]
rule id[a] : |- Ty(ctx, Const("id"), Arr(a, a))
| Form | Meaning |
|---|---|
a : S in, a : S out |
an argument of sort S, given or computed |
ctx : Context[T] in |
a context of T terms for the free variables of the scoped argument |
(ctx, x : A) |
ctx extended by a variable x with entry A |
lookup(ctx, i, A) |
the entry A of variable i |
S.var(i) |
a variable of the scoped sort, with index i |
Const(n) |
n at a data position stands for the data value there; the rule passes it on to its premises and outputs |
rule r[a, b] : ... |
a rule whose names a, b are schematic, renamed apart at each use |
#[weight=w] |
the rule's weight on the chart |
The option [lexicon=G] after the argument list types every lexical entry of the deduction G in the empty context at
compile time, and an entry without a typing fails qvr check at the entry. The words judgment and
out are keywords only where a judgment declaration or a mode can start, and syntax only where a
declaration can start, so each can still name a program, a parameter, or an atom.
The compiled judgment is Program.judgments[name], a
JudgmentSystem with solve, infer, and chart
(Judgments).
The standard library defines unannotated lambda terms in
std.syntax.lambda and their separate
Curry-style typing judgment in std.syntax.types.
Deduction binders¶
A deduction's binders line names the constructors of its logical forms that bind a variable in
their first argument, as in Lam(x, App(bark, Var(x))). Each bound variable is renamed to the
canonical name of its de Bruijn level, so alpha-equivalent logical forms are one chart item.