import proveit
from proveit import defaults, Variable
from proveit import p, a_star, b_star
from proveit.logic import InSet, Equals
from proveit.numbers import (zero, two, NaturalPos,
Rational, RationalNonZero, RationalPos,
Abs, greater, Mult, frac, Divides, GCD, Exp, sqrt)
%proving sqrt2_is_not_rational
sqrt_2_is_rational = InSet(sqrt(two), Rational)
defaults.assumptions = [sqrt_2_is_rational]
($\sqrt{2} \in \mathbb{Q^+}$ as well by convention, but this proof is really about showing that no squared rational number is equal to $2$.)
sqrt(two).deduce_not_zero()
abs_sqrt_2_is_rational_pos = InSet(Abs(sqrt(two)), RationalPos).prove()
abs_sqrt_2_is_rational_pos.choose_reduced_rational_fraction(a_star, b_star)
sqrt_2_equation = Equals(Abs(sqrt(two)), frac(a_star, b_star)).prove()
sqrt_2_equation = sqrt_2_equation.right_mult_both_sides(b_star)
sqrt_2_equation = sqrt_2_equation.square_both_sides()
two_bStar_sqrd__eq__a_star_sqrd = sqrt_2_equation.inner_expr().lhs.distribute()
InSet(Exp(b_star, two), NaturalPos).prove()
two_divides_two_bStar_sqrd = Divides(two, Mult(two, Exp(b_star, two))).prove()
two_bStar_sqrd__eq__a_star_sqrd.sub_right_side_into(two_divides_two_bStar_sqrd)
two_divides_a_star = Divides(two, a_star).prove()
two_sqrd__divides__a_star_sqrd = \
two_divides_a_star.introduce_common_exponent(two, auto_simplify=False)
two_sqrd__divides__two_bStar_sqrd = \
two_bStar_sqrd__eq__a_star_sqrd.sub_left_side_into(
two_sqrd__divides__a_star_sqrd)
two_sqrd__eq__two_times_two = Exp(two, two).expansion(auto_simplify=False)
two_sqrd__eq__two_times_two.sub_right_side_into(
two_sqrd__divides__two_bStar_sqrd)
Divides(two, Exp(b_star, two)).prove()
two_divides_bStar = Divides(two, b_star).prove()
a_star_bStar_relatively_prime = GCD(a_star, b_star).deduce_relatively_prime()
two_does_not_divide_both = a_star_bStar_relatively_prime.instantiate({p:two})
contradiction_with_choices = two_does_not_divide_both.derive_contradiction()
contradiction = contradiction_with_choices.eliminate(a_star, b_star)
sqrt2_impl_F = contradiction.as_implication(hypothesis=sqrt_2_is_rational)
sqrt2_impl_F.deny_antecedent(assumptions=[])
%qed