1-- Concrete complex numbers as ordered pairs of Dedekind reals.2-- 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.Complex.Core8import Real.Squares910def complexCarrier : Set:= ℝ × ℝ11def complexZero : Set:= (realZero, realZero)12def complexOne : Set:= (realOne, realZero)13def complexAdd : Set:= {p ∈ (complexCarrier × complexCarrier) × complexCarrier |∃ a ∈ ℝ, ∃ b ∈ ℝ, ∃ c ∈ ℝ, ∃ d ∈ ℝ, p = (((a, b), (c, d)), (realAdd(a, c), realAdd(b, d)))}14def complexNeg : Set:= {p ∈ complexCarrier × complexCarrier |∃ a ∈ ℝ, ∃ b ∈ ℝ, p = ((a, b), (realNeg(a), realNeg(b)))}15def complexMul : Set:= {p ∈ (complexCarrier × complexCarrier) × complexCarrier |∃ a ∈ ℝ, ∃ b ∈ ℝ, ∃ c ∈ ℝ, ∃ d ∈ ℝ, p = (((a, b), (c, d)), (realAdd(realMul(a, c), realNeg(realMul(b, d))), realAdd(realMul(a, d), realMul(b, c))))}1617theorem complexCarrier.pair (a b : Real)18shows (a, b) ∈ complexCarrier19proof20unfold complexCarrier21apply product.pair22 . typecheck23 . typecheck2425theorem complexCarrier.cases (x : Set)26requires member : x ∈ complexCarrier27shows∃ a, Real(a) ∧∃ b, Real(b) ∧ x = (a, b)28proof29rw complexCarrier at member30obtain⟨bounded, a, ha, b, hb, shape⟩:= product.membership.forward with member31have aReal : Real(a) bytypecheck32have bReal : Real(b) bytypecheck33refine⟨a, aReal, b, bReal, shape⟩3435theorem complexAdd.relates (a b c d : Real)36shows relates(complexAdd, ((a, b), (c, d)), (a + c, b + d))37proof38have aIn : a ∈ ℝ bytypecheck39have bIn : b ∈ ℝ bytypecheck40have cIn : c ∈ ℝ bytypecheck41have dIn : d ∈ ℝ bytypecheck42have first := complexCarrier.pair at a, b with*43have second := complexCarrier.pair at c, d with*44have input := product.pair with first, second45have output : (a + c, b + d) ∈ complexCarrier byapply complexCarrier.pair at a + c, b + d46have bounded := product.pair with input, output47unfold relates48exact complexAdd.membership.backward with⟨bounded, a, aIn, b, bIn, c, cIn, d, dIn, rfl⟩4950theorem complexAdd.factors (x y : Set)51requires related : relates(complexAdd, x, y)52shows∃ a, Real(a) ∧∃ b, Real(b) ∧∃ c, Real(c) ∧∃ d, Real(d) ∧ x = ((a, b), (c, d)) ∧ y = (a + c, b + d)53proof54obtain⟨bounded, a, aIn, b, bIn, c, cIn, d, dIn, shape⟩:= complexAdd.membership.forward at (x, y) with related55have pair := orderedPair.injective with shape56have aReal : Real(a) bytypecheck57have bReal : Real(b) bytypecheck58have cReal : Real(c) bytypecheck59have dReal : Real(d) bytypecheck60refine⟨a, aReal, b, bReal, c, cReal, d, dReal, pair⟩6162theorem complexAdd.function63shows complexAdd : complexCarrier × complexCarrier → complexCarrier64proof65constructor66intros p member67have data := complexAdd.membership.forward with member68exact data.left69 . constructor70intros x y first z second71obtain⟨a, aReal, b, bReal, c, cReal, d, dReal, input, output⟩:= complexAdd.factors with first72obtain⟨e, eReal, f, fReal, g, gReal, h, hReal, input2, output2⟩:= complexAdd.factors with second73have same : ((a, b), (c, d)) = ((e, f), (g, h)) bycc74have outer := orderedPair.injective with same75haveleft:= orderedPair.injective with outer.left76haveright:= orderedPair.injective with outer.right77cc78 . intros x member79obtain⟨bound, u, hu, v, hv, shape⟩:= product.membership.forward with member80obtain⟨a, aReal, b, bReal, first⟩:= complexCarrier.cases with hu81obtain⟨c, cReal, d, dReal, second⟩:= complexCarrier.cases with hv82rw [first, second] at shape83witness (a + c, b + d)84constructor85 . apply complexCarrier.pair at a + c, b + d86 . rw shape87exact complexAdd.relates with*8889theorem complexAdd.pair (a b c d : Real)90shows complexAdd((a, b), (c, d)) = (a + c, b + d)91proof92have first := complexCarrier.pair at a, b with*93have second := complexCarrier.pair at c, d with*94have input := product.pair with first, second95have related := complexAdd.relates at a, b, c, d with*96have value := FunctionBetween.apply_value with complexAdd.function, input, related97simp [call2]9899theorem complexNeg.relates (a b : Real)100shows relates(complexNeg, (a, b), (-a, -b))101proof102have aIn : a ∈ ℝ bytypecheck103have bIn : b ∈ ℝ bytypecheck104have first := complexCarrier.pair at a, b with*105have input := first106have output : (-a, -b) ∈ complexCarrier byapply complexCarrier.pair at -a, -b107have bounded := product.pair with input, output108unfold relates109exact complexNeg.membership.backward with⟨bounded, a, aIn, b, bIn, rfl⟩110111theorem complexNeg.factors (x y : Set)112requires related : relates(complexNeg, x, y)113shows∃ a, Real(a) ∧∃ b, Real(b) ∧ x = (a, b) ∧ y = (-a, -b)114proof115unfold relates at related116obtain⟨bounded, a, aIn, b, bIn, shape⟩:= complexNeg.membership.forward with related117have pair := orderedPair.injective with shape118have aReal : Real(a) bytypecheck119have bReal : Real(b) bytypecheck120refine⟨a, aReal, b, bReal, pair⟩121122theorem complexNeg.function123shows complexNeg : complexCarrier → complexCarrier124proof125constructor126intros p member127have data := complexNeg.membership.forward with member128exact data.left129 . constructor130intros x y first z second131obtain⟨a, aReal, b, bReal, input, output⟩:= complexNeg.factors with first132obtain⟨e, eReal, f, fReal, input2, output2⟩:= complexNeg.factors with second133have same : (a, b) = (e, f) bycc134have coordinates := orderedPair.injective with same135cc136 . intros x member137obtain⟨a, aReal, b, bReal, shape⟩:= complexCarrier.cases with member138witness (-a, -b)139constructor140 . apply complexCarrier.pair at -a, -b141 . rw shape142exact complexNeg.relates with*143144theorem complexNeg.pair (a b : Real)145shows complexNeg((a, b)) = (-a, -b)146proof147have first := complexCarrier.pair at a, b with*148have input := first149have related := complexNeg.relates at a, b with*150have value := FunctionBetween.apply_value with complexNeg.function, input, related151assumption152153theorem complexMul.relates (a b c d : Real)154shows relates(complexMul, ((a, b), (c, d)), (a * c + -(b * d), a * d + b * c))155proof156have aIn : a ∈ ℝ bytypecheck157have bIn : b ∈ ℝ bytypecheck158have cIn : c ∈ ℝ bytypecheck159have dIn : d ∈ ℝ bytypecheck160have first := complexCarrier.pair at a, b with*161have second := complexCarrier.pair at c, d with*162have input := product.pair with first, second163have output : (a * c + -(b * d), a * d + b * c) ∈ complexCarrier byapply complexCarrier.pair164have bounded := product.pair with input, output165unfold relates166exact complexMul.membership.backward with⟨bounded, a, aIn, b, bIn, c, cIn, d, dIn, rfl⟩167168theorem complexMul.factors (x y : Set)169requires related : relates(complexMul, x, y)170shows∃ a, Real(a) ∧∃ b, Real(b) ∧∃ c, Real(c) ∧∃ d, Real(d) ∧ x = ((a, b), (c, d)) ∧ y = (a * c + -(b * d), a * d + b * c)171proof172unfold relates at related173obtain⟨bounded, a, aIn, b, bIn, c, cIn, d, dIn, shape⟩:= complexMul.membership.forward with related174have pair := orderedPair.injective with shape175have aReal : Real(a) bytypecheck176have bReal : Real(b) bytypecheck177have cReal : Real(c) bytypecheck178have dReal : Real(d) bytypecheck179refine⟨a, aReal, b, bReal, c, cReal, d, dReal, pair⟩180181theorem complexMul.function182shows complexMul : complexCarrier × complexCarrier → complexCarrier183proof184constructor185intros p member186have data := complexMul.membership.forward with member187exact data.left188 . constructor189intros x y first z second190obtain⟨a, aReal, b, bReal, c, cReal, d, dReal, input, output⟩:= complexMul.factors with first191obtain⟨e, eReal, f, fReal, g, gReal, h, hReal, input2, output2⟩:= complexMul.factors with second192have same : ((a, b), (c, d)) = ((e, f), (g, h)) bycc193have outer := orderedPair.injective with same194haveleft:= orderedPair.injective with outer.left195haveright:= orderedPair.injective with outer.right196cc197 . intros x member198obtain⟨bound, u, hu, v, hv, shape⟩:= product.membership.forward with member199obtain⟨a, aReal, b, bReal, first⟩:= complexCarrier.cases with hu200obtain⟨c, cReal, d, dReal, second⟩:= complexCarrier.cases with hv201rw [first, second] at shape202witness (a * c + -(b * d), a * d + b * c)203constructor204 . apply complexCarrier.pair at a * c + -(b * d), a * d + b * c205 . rw shape206exact complexMul.relates with*207208theorem complexMul.pair (a b c d : Real)209shows complexMul((a, b), (c, d)) = (a * c + -(b * d), a * d + b * c)210proof211have first := complexCarrier.pair at a, b with*212have second := complexCarrier.pair at c, d with*213have input := product.pair with first, second214have related := complexMul.relates at a, b, c, d with*215have value := FunctionBetween.apply_value with complexMul.function, input, related216simp [call2]217