Part 3 — Types & type classes

3.4 Class hierarchies, parameterized subsets, and custom ACU

Tutorial sequence · Previous: 3.3 Specifications, overloading, and classes

Goal: layer classes with superclass and class_relation, define a parameterized subset type via SubsetBuilder, and register a custom ACU operator with the kernel's conversion engine.

Hierarchies, generic subsets, and ACU operators

  1. Class hierarchies (ClassBuilder.superclass) and relations (Session.class_relation):
  2. mon_b.superclass(semigroup) makes semigroup a superclass of monoid; monoid.intro() requires both semigroup(a) and the monoid unit law, and every monoid instance implies semigroup.
  3. s.class_relation(monoid, pointed, fact) registers a checked implication showing that every monoid also satisfies an independently declared class pointed.
  4. Parameterized subset types (SubsetBuilder):
  5. Calling s.subset("IdentityFn") without arguments opens a SubsetBuilder where type parameters (sb.type_var("A")) precede the predicate and nonemptiness witness. id_subset.constructor() yields a unary TypeConstructor (IdentityFn(N), IdentityFn(Prop), …) with zero axioms.
  6. Custom ACU operators (Session.acu_operator):
  7. Given a binary symbol combine, an optional unit empty, and checked associativity, commutativity, and left-unit facts, s.acu_operator(...) registers combine for canonical conversion so equalities such as combine(x, combine(empty, y)) = combine(y, x) close by rfl().

Mojo

classes_and_acu.mojo Download
"""Build class hierarchies, class relations, parameterized subsets, and custom ACU operators."""

from std.testing import assert_equal, assert_true
from judgment import Session, arrow, exists, forall, implies, lambda_


def main() raises:
    var s = Session()
    var N = s.declare_type("N")

    # 1. Class hierarchy (monoid extends semigroup) and a checked class relation to pointed.
    var sg_b = s.class_("semigroup")
    var a1 = sg_b.type_var("a")
    var op = sg_b.parameter("op", arrow(a1, arrow(a1, a1)))
    var x1 = sg_b.bound_var("x", a1)
    var y1 = sg_b.bound_var("y", a1)
    var z1 = sg_b.bound_var("z", a1)
    var op1 = op.symbol([a1])
    var semigroup = sg_b.finish(forall(x1, forall(y1, forall(z1, op1(op1(x1, y1), z1) == op1(x1, op1(y1, z1))))))

    var mon_b = s.class_("monoid")
    var a2 = mon_b.type_var("a")
    mon_b.superclass(semigroup)
    var unit = mon_b.parameter("unit", a2)
    var x2 = mon_b.bound_var("m", a2)
    var monoid = mon_b.finish(forall(x2, op.symbol([a2])(unit.symbol([a2]), x2) == x2))

    var pt_b = s.class_("pointed")
    _ = pt_b.type_var("a")
    var pointed = pt_b.finish(s.true_())

    # Prove every monoid is pointed and register the class relation.
    var rel = s.theorem("monoid_is_pointed")
    var r = rel.type_var("r")
    rel.goal(implies(monoid.membership(r), pointed.membership(r)))
    _ = rel.intro("hm")
    var goals = rel.apply(pointed.intro())
    goals[0].exact(goals[0].truth())
    s.class_relation(monoid, pointed, rel.finish())

    # Register N as a monoid; semigroup and pointed membership follow automatically.
    var op_n = op.symbol([N])
    var u_n = unit.symbol([N])
    var ax_assoc = s.axiom("N_assoc")
    var ns = ax_assoc.vars("x y z", N)
    var n_assoc = ax_assoc.finish(op_n(op_n(ns[0], ns[1]), ns[2]) == op_n(ns[0], op_n(ns[1], ns[2])))
    var ax_unit = s.axiom("N_unit")
    var nx = ax_unit.var("x", N)
    var n_unit = ax_unit.finish(op_n(u_n, nx) == nx)

    var n_sg = s.theorem("N_semigroup", semigroup.membership(N))
    var sg_goals = n_sg.apply(semigroup.intro())
    sg_goals[0].exact(n_assoc)
    s.instance(N, semigroup, n_sg.finish())

    var n_mon = s.theorem("N_monoid", monoid.membership(N))
    var mon_goals = n_mon.apply(monoid.intro())
    var parts = mon_goals[0].split()
    parts[0].exact(s.of_class(N, semigroup))
    parts[1].exact(n_unit)
    s.instance(N, monoid, n_mon.finish())
    assert_true(s.of_class(N, pointed).statement().same_as(pointed.membership(N)))
    print("Derived class membership via superclass and relation: pointed(N)")

    # 2. Parameterized subset type IdentityFn(A) carved from A -> A with zero axioms.
    var wit = s.theorem("id_fn_nonempty")
    var wa = wit.type_var("A")
    var wv = wit.bound_var("v", wa)
    var id_w = lambda_(wv, wv)
    var wf = wit.bound_var("f", arrow(wa, wa))
    wit.goal(exists(wf, wf == id_w))
    wit.witness(id_w)
    wit.rfl()
    var sb = s.subset("IdentityFn")
    var sa = sb.type_var("A")
    var sv = sb.bound_var("v", sa)
    var sf = sb.bound_var("f", arrow(sa, sa))
    var id_subset = sb.finish(lambda_(sf, sf == lambda_(sv, sv)), wit.finish())
    var id_ctor = id_subset.constructor()
    assert_equal(id_ctor.arity(), 1)
    assert_equal(len(id_subset.abs_rep().axioms()), 0)
    print("Parameterized subset type:", id_ctor(N).format())

    # 3. Custom ACU operator: kernel conversion normalizes reordering and unit removal.
    var s2 = Session()
    var M = s2.declare_type("M")
    var empty = s2.symbol("empty", M)
    var combine = s2.symbol("combine", arrow(M, arrow(M, M)))
    var ca = s2.axiom("combine_assoc")
    var ms = ca.vars("x y z", M)
    var c_assoc = ca.finish(combine(combine(ms[0], ms[1]), ms[2]) == combine(ms[0], combine(ms[1], ms[2])))
    var cc = s2.axiom("combine_comm")
    var cs = cc.vars("x y", M)
    var c_comm = cc.finish(combine(cs[0], cs[1]) == combine(cs[1], cs[0]))
    var cu = s2.axiom("combine_unit")
    var mx = cu.var("x", M)
    var c_unit = cu.finish(combine(empty, mx) == mx)
    _ = s2.acu_operator(combine, Optional(empty), c_assoc, c_comm, Optional(c_unit))

    var norm = s2.theorem("combine_normalize")
    var xy = norm.vars("x y", M)
    norm.goal(combine(xy[0], combine(empty, xy[1])) == combine(xy[1], xy[0]))
    norm.rfl()
    var proved = norm.finish()
    assert_equal(len(proved.axioms()), 3)
    print("Custom ACU reflexivity:", proved.statement().format())

Run from the repository root pixi run core-run docs/examples/library/classes_and_acu.mojo

Output

Derived class membership via superclass and relation: pointed(N)
Parameterized subset type: IdentityFn(N)
Custom ACU reflexivity: ∀x:M. ∀y:M. combine(x, combine(empty, y)) = combine(y, x)

Python

classes_and_acu.py Download
"""Build class hierarchies, class relations, parameterized subsets, and custom ACU operators."""

from judgment import Session, arrow, exists, forall, implies, lambda_


def main():
    s = Session()
    N = s.declare_type("N")

    # 1. Class hierarchy (monoid extends semigroup) and a checked class relation to pointed.
    sg_b = s.class_("semigroup")
    a1 = sg_b.type_var("a")
    op = sg_b.parameter("op", arrow(a1, arrow(a1, a1)))
    x1 = sg_b.bound_var("x", a1)
    y1 = sg_b.bound_var("y", a1)
    z1 = sg_b.bound_var("z", a1)
    op1 = op.symbol([a1])
    semigroup = sg_b.finish(forall(x1, forall(y1, forall(z1, op1(op1(x1, y1), z1) == op1(x1, op1(y1, z1))))))

    mon_b = s.class_("monoid")
    a2 = mon_b.type_var("a")
    mon_b.superclass(semigroup)
    unit = mon_b.parameter("unit", a2)
    x2 = mon_b.bound_var("m", a2)
    monoid = mon_b.finish(forall(x2, op.symbol([a2])(unit.symbol([a2]), x2) == x2))

    pt_b = s.class_("pointed")
    pt_b.type_var("a")
    pointed = pt_b.finish(s.true_())

    # Prove every monoid is pointed and register the class relation.
    rel = s.theorem("monoid_is_pointed")
    r = rel.type_var("r")
    rel.goal(implies(monoid.membership(r), pointed.membership(r)))
    rel.intro("hm")
    goals = rel.apply(pointed.intro())
    goals[0].exact(goals[0].truth())
    s.class_relation(monoid, pointed, rel.finish())

    # Register N as a monoid; semigroup and pointed membership follow automatically.
    op_n = op.symbol([N])
    u_n = unit.symbol([N])
    ax_assoc = s.axiom("N_assoc")
    ns = ax_assoc.vars("x y z", N)
    n_assoc = ax_assoc.finish(op_n(op_n(ns[0], ns[1]), ns[2]) == op_n(ns[0], op_n(ns[1], ns[2])))
    ax_unit = s.axiom("N_unit")
    nx = ax_unit.var("x", N)
    n_unit = ax_unit.finish(op_n(u_n, nx) == nx)

    n_sg = s.theorem("N_semigroup", semigroup.membership(N))
    sg_goals = n_sg.apply(semigroup.intro())
    sg_goals[0].exact(n_assoc)
    s.instance(N, semigroup, n_sg.finish())

    n_mon = s.theorem("N_monoid", monoid.membership(N))
    mon_goals = n_mon.apply(monoid.intro())
    parts = mon_goals[0].split()
    parts[0].exact(s.of_class(N, semigroup))
    parts[1].exact(n_unit)
    s.instance(N, monoid, n_mon.finish())
    assert s.of_class(N, pointed).statement().same_as(pointed.membership(N))
    print("Derived class membership via superclass and relation: pointed(N)")

    # 2. Parameterized subset type IdentityFn(A) carved from A -> A with zero axioms.
    wit = s.theorem("id_fn_nonempty")
    wa = wit.type_var("A")
    wv = wit.bound_var("v", wa)
    id_w = lambda_(wv, wv)
    wf = wit.bound_var("f", arrow(wa, wa))
    wit.goal(exists(wf, wf == id_w))
    wit.witness(id_w)
    wit.rfl()
    sb = s.subset("IdentityFn")
    sa = sb.type_var("A")
    sv = sb.bound_var("v", sa)
    sf = sb.bound_var("f", arrow(sa, sa))
    id_subset = sb.finish(lambda_(sf, sf == lambda_(sv, sv)), wit.finish())
    id_ctor = id_subset.constructor()
    assert id_ctor.arity() == 1
    assert len(id_subset.abs_rep().axioms()) == 0
    print("Parameterized subset type:", id_ctor(N).format())

    # 3. Custom ACU operator: kernel conversion normalizes reordering and unit removal.
    s2 = Session()
    M = s2.declare_type("M")
    empty = s2.symbol("empty", M)
    combine = s2.symbol("combine", arrow(M, arrow(M, M)))
    ca = s2.axiom("combine_assoc")
    ms = ca.vars("x y z", M)
    c_assoc = ca.finish(combine(combine(ms[0], ms[1]), ms[2]) == combine(ms[0], combine(ms[1], ms[2])))
    cc = s2.axiom("combine_comm")
    cs = cc.vars("x y", M)
    c_comm = cc.finish(combine(cs[0], cs[1]) == combine(cs[1], cs[0]))
    cu = s2.axiom("combine_unit")
    mx = cu.var("x", M)
    c_unit = cu.finish(combine(empty, mx) == mx)
    s2.acu_operator(combine, empty, c_assoc, c_comm, c_unit)

    norm = s2.theorem("combine_normalize")
    xy = norm.vars("x y", M)
    norm.goal(combine(xy[0], combine(empty, xy[1])) == combine(xy[1], xy[0]))
    norm.rfl()
    proved = norm.finish()
    assert len(proved.axioms()) == 3
    print("Custom ACU reflexivity:", proved.statement().format())


if __name__ == "__main__":
    main()

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

Output

Derived class membership via superclass and relation: pointed(N)
Parameterized subset type: IdentityFn(N)
Custom ACU reflexivity: ∀x:M. ∀y:M. combine(x, combine(empty, y)) = combine(y, x)

The three law facts (combine_assoc, combine_comm, combine_unit) remain recorded in proved.axioms() unless proved as theorems from prior definitions.

Exercise

In classes_and_acu, pass None for the unit and unit-law arguments in s.acu_operator (registering an AC operator without a unit). What happens to combine(x, y) == combine(y, x) vs. combine(x, combine(empty, y)) == combine(y, x)?

Answer

combine(x, y) == combine(y, x) still closes by rfl() under AC conversion, while combine(x, combine(empty, y)) == combine(y, x) fails because empty is no longer eliminated by conversion.

Next: 4.1 Compute exact arithmetic and polynomial literals.

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