3.2 Define nonempty subset types¶
Tutorial sequence · Previous: 3.1 Instantiate polymorphic theorems
Define Singleton from the propositions equal to True, then use the
generated theorem abs(rep(s)) = s. This demonstrates HOL's requirement
that a new type have at least one inhabitant.
The predicate is P(p) = (p = True). Its witness is the proposition
True itself: reflexivity proves True = True. The program proves the
existential statement as a named theorem and passes that fact when
defining the type. It adds no new axiom.
Mojo
"""Define a singleton type using a proved nonemptiness witness."""
from std.testing import assert_true
from judgment import Session, exists, lambda_
def main() raises:
var s = Session()
var prop = s.prop_type()
var truth = s.true_()
var witness = s.theorem("singleton_exists")
var p = witness.bound_var("p", prop)
var predicate = lambda_(p, p == truth)
witness.goal(exists(p, p == truth))
witness.witness(truth)
witness.rfl()
var inhabited = witness.finish()
assert_true(len(inhabited.axioms()) == 0)
var singleton = s.subset(
"Singleton", predicate, inhabited,
abs_name="abs_singleton", rep_name="rep_singleton",
)
var element = singleton.abstract(truth)
var round_trip = s.forall_elim(s.cite(singleton.abs_rep()), element)
var expected = singleton.abstract(singleton.represent(element)) == element
assert_true(round_trip.statement().same_as(expected))
assert_true(len(round_trip.hypotheses()) == 0)
print("Proved: there exists p:Prop with p = True")
print("Defined Singleton from that nonempty subset")
print("Used generated fact: abs(rep(s)) = s")
Run from the repository root pixi run core-run docs/examples/subset_type.mojo
Output
Proved: there exists p:Prop with p = True
Defined Singleton from that nonempty subset
Used generated fact: abs(rep(s)) = s
Python
"""Define a singleton type using a proved nonemptiness witness."""
from judgment import Session, exists, lambda_
def main():
s = Session()
prop = s.prop_type()
truth = s.true_()
witness = s.theorem("singleton_exists")
p = witness.bound_var("p", prop)
predicate = lambda_(p, p == truth)
witness.goal(exists(p, p == truth))
witness.witness(truth)
witness.rfl()
inhabited = witness.finish()
assert len(inhabited.axioms()) == 0
singleton = s.subset(
"Singleton", predicate, inhabited,
abs_name="abs_singleton", rep_name="rep_singleton",
)
element = singleton.abstract(truth)
round_trip = s.forall_elim(s.cite(singleton.abs_rep()), element)
expected = singleton.abstract(singleton.represent(element)) == element
assert round_trip.statement().same_as(expected)
assert len(round_trip.hypotheses()) == 0
print("Proved: there exists p:Prop with p = True")
print("Defined Singleton from that nonempty subset")
print("Used generated fact: abs(rep(s)) = s")
if __name__ == "__main__":
main()
Run from the repository root PYTHONPATH=bindings/python:docs/examples/python pixi run python docs/examples/python/subset_type.py
Output
Proved: there exists p:Prop with p = True
Defined Singleton from that nonempty subset
Used generated fact: abs(rep(s)) = s
Establish nonemptiness¶
s.subset(name, predicate, inhabited) needs a fact whose statement is
exactly ∃p:Prop. P(p). A checked element or a Boolean returned by a Mojo
function is not a substitute for that logical witness.
The theorem singleton_exists states exists(p, p == truth) as its goal.
witness.witness(truth) reduces the existential goal to the predicate at
the explicit witness, True = True, which rfl closes. finish() checks
the proof and declares the theorem, and the assertion confirms that it
rests on no axiom. Existential introduction is derived from the core's
definition of ∃; it is not an additional primitive rule.
Use the new type and its facts¶
The declaration creates the type, an abstraction function
abs: Prop → Singleton, and a representation function
rep: Singleton → Prop, named here abs_singleton and rep_singleton.
The returned Subset exposes them with kind(), abstraction(), and
representation(), applies them with abstract and represent, and
supplies two laws:
abs_rep() is the first and rep_abs() the second. The second equality
is between propositions. It only gives the familiar representation round
trip for a p satisfying the predicate. abs is total, but the law does
not say every arbitrary p belongs to the subset.
The program cites abs_rep(), instantiates it with forall_elim at
abs(True), and asserts the exact resulting equation. These equations are
used as theorems; abstraction and representation do not disappear during
conversion.
The subset predicate constrains which values represent the new type. It does not make the type depend on an arbitrary run-time term. See simple types and HOL compatibility and type definitions, overloading, and classes.