"""Transport types, terms, and checked proofs between theories over a common base snapshot."""
from judgment import (
    Budget, JudgmentError, LibErrorKind, Session, TypeImage, arrow, forall, lambda_,
    prepare_interpretation, reuse, transport_term, transport_type,
)


def main():
    budget = Budget(20_000_000)

    # Seal an explicit common base snapshot shared by the abstract and concrete theories.
    base_session = Session()
    base = base_session.seal()

    # 1. Abstract source theory: a carrier C, an operator op : C -> C, an involution axiom,
    #    and a derived theorem proving op(op(op(op(x)))) = x.
    source = Session.open(base)
    Carrier = source.type_constructor("Carrier", 0)
    C = Carrier()
    op = source.symbol("op", arrow(C, C))
    sx = source.bound_var("x", C)
    invol = source.axiom("invol", forall(sx, op(op(sx)) == sx))

    quad_proof = source.theorem("quad", forall(sx, op(op(op(op(sx)))) == sx))
    x_var = quad_proof.intro_var("x")
    chain = quad_proof.calc(op(op(op(op(x_var)))))
    chain.eq(op(op(x_var)), invol, "outer_invol")
    chain.eq(x_var, invol, "inner_invol")
    quad_proof.exact(chain.finish())
    quad = quad_proof.finish()
    assert len(quad.axioms()) == 1

    # 2. Concrete target theory: a type Concrete where op is interpreted as the identity function.
    target = Session.open(base)
    Concrete = target.declare_type("Concrete")
    ty = target.bound_var("y", Concrete)
    id_fn = lambda_(ty, ty)
    target_invol_proof = target.theorem("id_invol", forall(ty, id_fn(id_fn(ty)) == ty))
    target_invol_proof.intro_var("y")
    target_invol_proof.rfl()
    target_invol = target_invol_proof.finish()
    assert len(target_invol.axioms()) == 0

    # 3. Transport syntax (types and terms) and reuse a prepared interpretation across operands.
    type_map = [TypeImage(Carrier, [], Concrete)]
    translated_ty = transport_type(source, target, base, C, type_map, budget)
    assert translated_ty.same_as(Concrete)
    translated_stmt = transport_term(source, target, base, quad.statement(), [(op, id_fn)], budget, type_images=type_map)

    stx = source.transaction()
    ttx = target.transaction()
    prepared = prepare_interpretation(source, target, base, [(op, id_fn)], budget, type_images=type_map)
    prepared_ty = prepared.type_(source, target, base, C, budget)
    prepared_stmt = prepared.term(source, target, base, quad.statement(), budget)
    stx.discard()
    retained = ttx.commit(types=[prepared_ty], exprs=[prepared_stmt])
    assert retained.types()[0].same_as(Concrete)
    assert retained.expressions()[0].same_as(translated_stmt)
    print("Transported type:", translated_ty.format(), "| prepared syntax matches:", retained.expressions()[0].same_as(translated_stmt))

    # 4. Replay the source derivation in the target session, discharging the source axiom completely.
    transported = reuse(
        source, target, base, quad, "concrete.quad_id",
        [(op, id_fn)], [(invol, target_invol)], budget, type_images=type_map,
    )
    assert transported.statement().same_as(translated_stmt)
    assert len(transported.axioms()) == 0
    assert len(transported.admissions()) == 0
    print("Source axioms:", len(quad.axioms()), "| transported target axioms:", len(transported.axioms()))

    # 5. Omitting a required source axiom obligation is refused atomically.
    refused = False
    try:
        reuse(source, target, base, quad, "concrete.missing_obligation", [(op, id_fn)], [], budget, type_images=type_map)
    except JudgmentError as e:
        assert e.kind == LibErrorKind.NO_MATCH
        refused = True
    assert refused
    print("Incomplete obligation refused:", refused)


if __name__ == "__main__":
    main()
