"""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()
