1-- Core field theory2-- 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.Algebra.Field.Core8import Defs9import Ring.Core1011-- Division is multiplication by the inverse. Packaging that familiar12-- operation as a function graph lets notation select it through the same13-- instance machinery as addition and multiplication.14def fieldDiv (A mul inv : Set) : Set:=15 { p ∈ ((A × A) × A) |16∃ x ∈ A, ∃ y ∈ A,17 p = ((x, y) ↦ call2(mul, x, inv(y))) }1819theorem fieldDiv.relates (A mul inv x y : Set)20requires mulFunction : mul : A × A → A21requires invFunction : inv : A → A22requires xInA : x ∈ A23requires yInA : y ∈ A24shows relates(fieldDiv(A, mul, inv), (x, y),25 call2(mul, x, inv(y)))26proof27have inverseInA : inv(y) ∈ A28exact FunctionBetween.apply_closed with invFunction, yInA29have inputInProduct := product.pair with xInA, inverseInA30have quotientInA : call2(mul, x, inv(y)) ∈ A31unfold call232exact FunctionBetween.apply_closed with mulFunction, inputInProduct33unfold relates34apply fieldDiv.membership.backward35constructor36 . apply product.pair at (x, y), call2(mul, x, inv(y)), A × A, A37 . exact product.pair with xInA, yInA38 . exact quotientInA39 . refine⟨x, xInA, y, yInA, rfl⟩4041theorem fieldDiv.factors (A mul inv input output : Set)42requires mapped : relates(fieldDiv(A, mul, inv), input, output)43shows∃ x ∈ A, ∃ y ∈ A,44 input = (x, y) ∧ output = call2(mul, x, inv(y))45proof46unfold relates at mapped47simp [fieldDiv.membership] at mapped48rcases mapped with⟨bounded, x, xInA, y, yInA, shape⟩49obtain⟨inputShape, outputShape⟩:= orderedPair.injective with shape50refine⟨x, xInA, y, yInA, inputShape, outputShape⟩5152theorem fieldDiv.total (A mul inv : Set)53requires mulFunction : mul : A × A → A54requires invFunction : inv : A → A55shows TotalBetween(fieldDiv(A, mul, inv), A × A, A)56proof57intros input inputInProduct58obtain⟨bounded, x, xInA, y, yInA, inputShape⟩:= product.membership.forward with inputInProduct59have inverseInA : inv(y) ∈ A60exact FunctionBetween.apply_closed with invFunction, yInA61have mulInput := product.pair with xInA, inverseInA62have quotientInA : call2(mul, x, inv(y)) ∈ A63unfold call264exact FunctionBetween.apply_closed with mulFunction, mulInput65witness call2(mul, x, inv(y))66constructor67 . exact quotientInA68 . rw inputShape69exact fieldDiv.relates with mulFunction, invFunction, xInA, yInA7071theorem fieldDiv.functional (A mul inv : Set)72shows Functional(fieldDiv(A, mul, inv))73proof74intros input first inputFirst second inputSecond75obtain⟨x, xInA, y, yInA, firstInput, firstOutput⟩:= fieldDiv.factors with inputFirst76obtain⟨u, uInA, v, vInA, secondInput, secondOutput⟩:= fieldDiv.factors with inputSecond77rw firstInput at secondInput78obtain⟨sameLeft, sameRight⟩:= orderedPair.injective with secondInput79rw [firstOutput, secondOutput, sameLeft, sameRight]80rfl8182theorem fieldDiv.function (A mul inv : Set)83requires mulFunction : mul : A × A → A84requires invFunction : inv : A → A85shows fieldDiv(A, mul, inv) : A × A → A86proof87constructor88intros p hp89simp [fieldDiv.membership] at hp90rcases hp with⟨bounded, witnesses⟩91exact bounded92 . have functional := fieldDiv.functional at A, mul, inv93refine⟨functional, _⟩94exact fieldDiv.total with mulFunction, invFunction9596theorem DivisionSemiring.semiring (A add zero mul one inv : Set)97requires h : DivisionSemiring(A, add, zero, mul, one, inv)98shows IsSemiring(A, add, zero, mul, one)99proof100simp [DivisionSemiring]101102theorem Semifield.comm_semiring (A add zero mul one inv : Set)103requires h : Semifield(A, add, zero, mul, one, inv)104shows CommSemiring(A, add, zero, mul, one)105proof106simp [Semifield]107108theorem DivisionRing.ring (A add zero neg mul one inv : Set)109requires h : DivisionRing(A, add, zero, neg, mul, one, inv)110shows IsRing(A, add, zero, neg, mul, one)111proof112simp [DivisionRing]113114theorem DivisionRing.nontrivial (A add zero neg mul one inv : Set)115requires h : DivisionRing(A, add, zero, neg, mul, one, inv)116shows Nontrivial(A, zero, one)117proof118simp [DivisionRing]119120theorem DivisionRing.inverse (A add zero neg mul one inv : Set)121requires h : DivisionRing(A, add, zero, neg, mul, one, inv)122shows MultiplicativeInverse(A, mul, zero, one, inv)123proof124simp [DivisionRing]125126theorem IsFieldWithInverse.comm_ring (A add zero neg mul one inv : Set)127requires h : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)128shows IsCommRing(A, add, zero, neg, mul, one)129proof130simp [IsFieldWithInverse]131132theorem IsFieldWithInverse.nontrivial (A add zero neg mul one inv : Set)133requires h : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)134shows Nontrivial(A, zero, one)135proof136simp [IsFieldWithInverse]137138theorem IsFieldWithInverse.inverse (A add zero neg mul one inv : Set)139requires h : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)140shows MultiplicativeInverse(A, mul, zero, one, inv)141proof142simp [IsFieldWithInverse]143144theorem IsFieldWithInverse.one_in_carrier (A add zero neg mul one inv : Set)145requires field : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)146shows one ∈ A147proof148have commRing := IsFieldWithInverse.comm_ring with field149havering:= IsCommRing.ring with commRing150have monoid := IsRing.multiplicative withring151exact IsMonoid.unit_in_carrier with monoid152153theorem IsFieldWithInverse.inverse_closed (A add zero neg mul one inv x : Set)154requires field : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)155requires xInA : x ∈ A156shows inv(x) ∈ A157proof158obtain⟨function, laws⟩:= IsFieldWithInverse.inverse with field159exact FunctionBetween.apply_closed with function, xInA160161theorem IsFieldWithInverse.inverse_left (A add zero neg mul one inv x : Set)162requires field : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)163requires xInA : x ∈ A164requires nonzero : x ≠ zero165shows call2(mul, inv(x), x) = one166proof167obtain⟨function, laws⟩:= IsFieldWithInverse.inverse with field168obtain⟨left, right⟩:= laws with x, xInA, nonzero169exactleft170171theorem IsFieldWithInverse.inverse_right (A add zero neg mul one inv x : Set)172requires field : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)173requires xInA : x ∈ A174requires nonzero : x ≠ zero175shows call2(mul, x, inv(x)) = one176proof177obtain⟨function, laws⟩:= IsFieldWithInverse.inverse with field178obtain⟨left, right⟩:= laws with x, xInA, nonzero179exactright180181theorem IsFieldWithInverse.division_ring (A add zero neg mul one inv : Set)182requires h : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)183shows DivisionRing(A, add, zero, neg, mul, one, inv)184proof185rcases h with⟨commRing, division⟩186rcases commRing with⟨ring, commutative⟩187refine⟨ring, division⟩188189theorem IsFieldWithInverse.integral_domain (A add zero neg mul one inv : Set)190requires h : IsFieldWithInverse(A, add, zero, neg, mul, one, inv)191requires noZero : NoZeroDivisors(A, mul, zero)192shows IsIntegralDomain(A, add, zero, neg, mul, one)193proof194rcases h with⟨commRing, nontrivial, inverse⟩195refine⟨commRing, nontrivial, noZero⟩196