"""Infer arguments for explicitly chosen function and algebra rewrites."""

from std.testing import assert_equal
from judgment import Session, arrow, forall
from arithmetic_setup import arithmetic_setup


def main() raises:
    var s = Session()
    var proof = s.theorem("left_inverse_implies_injective")
    var A = proof.type_var("A")
    var B = proof.type_var("B")
    var f = proof.var("f", arrow(A, B))
    var g = proof.var("g", arrow(B, A))
    var t = proof.bound_var("t", A)
    var left_inverse = proof.given("left_inverse", forall(t, g(f(t)) == t))
    var x = proof.var("x", A)
    var y = proof.var("y", A)
    var same_image = proof.given("same_image", f(x) == f(y))
    proof.goal(x == y)

    _ = proof.lhs().rw(left_inverse, reverse=True)
    _ = proof.lhs().at(f(x)).rw(same_image)
    _ = proof.lhs().rw(left_inverse)
    proof.rfl()
    var theorem = proof.finish()
    assert_equal(len(theorem.axioms()), 0)
    print(theorem)

    var algebra = arithmetic_setup(s)
    var commute = s.fact("mul_comm")
    var distribute = s.fact("left_distrib")
    var right = s.theorem("right_distrib_inferred")
    var a = right.var("a", algebra.kind)
    var b = right.var("b", algebra.kind)
    var c = right.var("c", algebra.kind)
    right.goal((a + b) * c == a * c + b * c)

    _ = right.lhs().rw(commute)
    _ = right.lhs().rw(distribute)
    _ = right.lhs().at(c * a).rw(commute)
    _ = right.lhs().at(c * b).rw(commute)
    right.rfl()
    theorem = right.finish()
    assert_equal(len(theorem.axioms()), 2)
    print(theorem)
