Part 3 — Types & type classes

3.1 Instantiate polymorphic theorems

Tutorial sequence · Previous: 2.7 Admissions and sealed theories · Next: 3.2 Define nonempty subset types

Prove ∀x:α. x = x, save it once, and use it at two different types. Along the way, construct the identity function λx:α. x and observe its application reduce to its argument.

This example connects three distinct mechanisms: a parameter in a theorem's header, a bound variable in an expression, and a type parameter of a theorem. It builds on definitions and reuse.

Mojo

quantified_theorem.mojo Download
"""Build a lambda and reuse a quantified theorem at different types."""

from std.testing import assert_true
from judgment import Session, arrow, forall, lambda_
from judgment_shell import LibErrorKind


def main() raises:
    var s = Session()
    var alpha = s.type_var("a")
    var bx = s.bound_var("x", alpha)
    var identity = lambda_(bx, bx)

    var proof = s.theorem("identity_refl")
    var a = proof.type_var("a")
    var x = proof.var("x", a)
    var v = proof.bound_var("v", a)
    var id_a = lambda_(v, v)
    var applied = id_a(x)
    assert_true(applied.same_as(x))
    proof.goal(applied == x)
    proof.rfl()
    var saved = proof.finish()
    var params = saved.type_params()
    assert_true(len(params) == 1)
    assert_true(len(saved.axioms()) == 0)
    var py = s.bound_var("y", params[0])
    assert_true(saved.statement().same_as(forall(py, py == py)))

    var root = s.base()
    var prop = s.prop_type()
    var truth = s.true_()
    var instance = s.use(root, saved, terms=[truth], type_args=[prop])
    assert_true(instance.statement().same_as(truth == truth))

    var functions = arrow(alpha, alpha)
    var at_function = s.use(root, saved, terms=[identity], type_args=[functions])
    assert_true(at_function.statement().same_as(identity == identity))

    var rejected = False
    try:
        _ = s.use(root, saved, terms=[truth])
    except e:
        rejected = e.kind == LibErrorKind.KERNEL or e.kind == LibErrorKind.SHAPE
    assert_true(rejected)
    print("Lambda application: (lambda x. x)(x) = x")
    print("Saved scheme: for every x of type a, x = x")
    print("Reused at Prop and at a function type")
    print("Missing type argument: rejected")

Run from the repository root pixi run mojo run -I src docs/examples/quantified_theorem.mojo

Output

Lambda application: (lambda x. x)(x) = x
Saved scheme: for every x of type a, x = x
Reused at Prop and at a function type
Missing type argument: rejected

Python

quantified_theorem.py Download
"""Build a lambda and reuse a quantified theorem at different types."""

from judgment import Session, arrow, forall, lambda_, JudgmentError
from judgment import LibErrorKind


def main():
    s = Session()
    alpha = s.type_var("a")
    bx = s.bound_var("x", alpha)
    identity = lambda_(bx, bx)

    proof = s.theorem("identity_refl")
    a = proof.type_var("a")
    x = proof.var("x", a)
    v = proof.bound_var("v", a)
    id_a = lambda_(v, v)
    applied = id_a(x)
    assert applied.same_as(x)
    proof.goal(applied == x)
    proof.rfl()
    saved = proof.finish()
    params = saved.type_params()
    assert len(params) == 1
    assert len(saved.axioms()) == 0
    py = s.bound_var("y", params[0])
    assert saved.statement().same_as(forall(py, py == py))

    root = s.base()
    prop = s.prop_type()
    truth = s.true_()
    instance = s.use(root, saved, terms=[truth], type_args=[prop])
    assert instance.statement().same_as(truth == truth)

    functions = arrow(alpha, alpha)
    at_function = s.use(root, saved, terms=[identity], type_args=[functions])
    assert at_function.statement().same_as(identity == identity)

    rejected = False
    try:
        _ = s.use(root, saved, terms=[truth])
    except JudgmentError as e:
        rejected = e.kind == LibErrorKind.KERNEL or e.kind == LibErrorKind.SHAPE
    assert rejected
    print("Lambda application: (lambda x. x)(x) = x")
    print("Saved scheme: for every x of type a, x = x")
    print("Reused at Prop and at a function type")
    print("Missing type argument: rejected")


if __name__ == "__main__":
    main()

Run from the repository root PYTHONPATH=bindings/python:docs/examples/python pixi run python docs/examples/python/quantified_theorem.py

Output

Lambda application: (lambda x. x)(x) = x
Saved scheme: for every x of type a, x = x
Reused at Prop and at a function type
Missing type argument: rejected

From a parameter to a binder

s.type_var("a") creates the type variable α in the session, and s.bound_var("x", alpha) a variable for binders. lambda_(bx, bx) binds it, producing the closed identity function. A bound variable is syntax only: every free occurrence must be bound before the expression is used as a goal or evidence.

s.theorem("identity_refl") starts a theorem whose header is declared before its goal. proof.type_var("a") declares the theorem's own type parameter, and proof.var("x", a) an arbitrary term parameter, which finishing the theorem universally quantifies. proof.bound_var("v", a) is another syntax variable, owned by the proof.

Applying λv. v to x performs beta reduction during construction, so the result is the same expression as x, which same_as confirms. This is computation, not an inference that an arbitrary proposition holds.

proof.goal(applied == x) fixes the statement, rfl closes it by reflexivity, and finish() declares the theorem ∀x:α. x = x. The example builds that proposition independently and checks that no axiom was used.

Instantiate types and values separately

The saved theorem is generic: type_params reports its type parameters in the order use expects. Here the list has just α.

s.use(frame, fact, terms=..., type_args=...) instantiates a fact at a frame; s.base() is the session's base frame, here its root, with no local variables or assumptions. Its two argument lists have different meanings:

Arguments Substitution in this example
type_args=[prop], terms=[truth] Set α to Prop, then instantiate the quantified x with True
type_args=[functions], terms=[identity] Set α to the function type α → α, then instantiate x with the identity function

The second case uses a function as a value, which simple higher-order logic supports. It proves the function equals itself; it does not use function extensionality to infer equality from pointwise behavior.

Omitting the type argument is refused. This call expects explicit type instantiation even when a human could infer it from a term argument. For several type parameters, consult type_params rather than guessing an order from their printed names.

A header premise declared with given(name, proposition) becomes an implication of the finished theorem, so reusing the theorem still requires evidence for it. See the checked transformation for a conditional theorem. For a type built from a predicate on values, continue with nonempty subset types.

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub