1-- Integer operations lifted from natural-pair representatives2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Int.Operations8import Core910def integerPairSum (a b c d : Set) : Set:= (add(a, c), add(b, d))11def integerClassSum (a b c d : Set) : Set:= integerClass(add(a, c), add(b, d))12def integerClassNeg (a b : Set) : Set:= integerClass(b, a)1314theorem integerPairSum.pair (a b c d : Nat)15shows integerPairSum(a, b, c, d) ∈ integerPairs16proof17unfold integerPairSum18have leftNat := add.closed at c, a with*19have rightNat := add.closed at d, b with*20exact integerPairs.pair with leftNat, rightNat2122theorem integerPairSum.cross_sums (a b a2 b2 c d c2 d2 : Nat)23requires leftEquivalent : add(a, b2) = add(a2, b)24requires rightEquivalent : add(c, d2) = add(c2, d)25shows add(add(a, c), add(b2, d2)) = add(add(a2, c2), add(b, d))26proof27ring2829theorem integerClassSum.well_defined (a b a2 b2 c d c2 d2 : Nat)30requires leftEquivalent : add(a, b2) = add(a2, b)31requires rightEquivalent : add(c, d2) = add(c2, d)32shows integerClassSum(a, b, c, d) = integerClassSum(a2, b2, c2, d2)33proof34unfold integerClassSum35have sums := integerPairSum.cross_sums at a, b, a2, b2, c, d, c2, d2 with*36exact integerClass.eq with sums3738theorem integerClassSum.integer (a b c d : Nat)39shows Integer(integerClassSum(a, b, c, d))40proof41unfold integerClassSum42have leftNat := add.closed at c, a with*43have rightNat := add.closed at d, b with*44exact integerClass.integer with leftNat, rightNat4546theorem integerClassNeg.well_defined (a b c d : Nat)47requires equivalent : add(a, d) = add(c, b)48shows integerClassNeg(a, b) = integerClassNeg(c, d)49proof50unfold integerClassNeg51have reverse := integerEquivalent.cross_symmetric with*52have leftCommute := add.commutative at c, b with*53have rightCommute := add.commutative at d, a with*54have negatedSums := reverse55rewrite← leftCommute at negatedSums56rewrite rightCommute at negatedSums57exact integerClass.eq with*5859theorem integerClassSum.zero_right (a b : Nat)60shows integerClassSum(a, b, 0, 0) = integerClass(a, b)61proof62simp [integerClassSum, add.right_unit]6364theorem integerClassSum.commutative (a b c d : Nat)65shows integerClassSum(a, b, c, d) = integerClassSum(c, d, a, b)66proof67unfold integerClassSum68have positive := add.commutative at c, a with*69have negative := add.commutative at d, b with*70rewrite positive71rewrite negative72rfl7374theorem integerClassSum.neg_right (a b : Nat)75shows integerClassSum(a, b, b, a) = integerZero76proof77unfold integerClassSum78unfold integerZero79have sumNat := add.closed at b, a with*80have reverseNat := add.closed at a, b with*81have commute := add.commutative at b, a with*82have classes := integerClass.eq with sumNat, reverseNat, Naturals.constructor.zero, Naturals.constructor.zero83apply classes84simp [add, add.right_unit, commute]8586-- Multiplication of differences: (a-b)(c-d) = (ac+bd)-(ad+bc).87def integerClassMul (a b c d : Nat) : Integer :=88 integerClass(a * c + b * d, a * d + b * c)8990theorem integerClassMul.integer (a b c d : Nat)91shows Integer(integerClassMul(a, b, c, d))92proof93unfold integerClassMul94exact integerClass.integer with*9596theorem integerClassMul.commutative (a b c d : Nat)97shows integerClassMul(a, b, c, d) = integerClassMul(c, d, a, b)98proof99unfold integerClassMul100apply integerClass.eq with*{4}101ring102103theorem integerClassMul.transport_left (a b a2 b2 c d : Nat)104requires equivalent : a + b2 = a2 + b105shows integerClassMul(a, b, c, d) = integerClassMul(a2, b2, c, d)106proof107have combined : (a + b2) * c + (a2 + b) * d = (a2 + b) * c + (a + b2) * d108 . rw equivalent109rfl110unfold integerClassMul111apply integerClass.eq with*{4}112ring113114theorem integerClassMul.well_defined (a b a2 b2 c d c2 d2 : Nat)115requires leftEquivalent : a + b2 = a2 + b116requires rightEquivalent : c + d2 = c2 + d117shows integerClassMul(a, b, c, d) = integerClassMul(a2, b2, c2, d2)118proof119have first := integerClassMul.transport_left at a, b, a2, b2, c, d with*120have swap := integerClassMul.commutative at a2, b2, c, d with*121have second := integerClassMul.transport_left at c, d, c2, d2, a2, b2 with*122have back := integerClassMul.commutative at c2, d2, a2, b2 with*123simp124125theorem integerClassNeg.integer (a b : Nat)126shows Integer(integerClassNeg(a, b))127proof128unfold integerClassNeg129exact integerClass.integer with*130