1-- Canonical finite-set sums from enumeration independence, without Choice.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.Algebra.Ring.FiniteSetSum8import IndexedSum910theorem WeightedValues.of_functions (w f V A n : Set)11requires weights : w : V → A12requires enumeration : f : n → V13shows WeightedValues(w, f, A, n)14proof15intros i hi16have member := FunctionBetween.apply_closed at f, n, V, i with*17exact FunctionBetween.apply_closed at w, V, A, f(i) with*1819defunique finiteRingSum (V A add zero neg mul one w : Set) (sum ∈ A) :=20∃ n, Nat(n) ∧∃ f, BijectiveBetween(f, n, V) ∧ sum = indexedRingSum(w, f, add, zero, n)21requiresring : IsCommRing(A, add, zero, neg, mul, one)22requires finite : Finite(V)23requires weights : w : V → A24proof25obtain⟨n, natural, equivalent⟩:= finite26obtain⟨f, bijective⟩:= equivalent27have enumeration := converse.bijective with bijective28have function := converse.function_of_bijective with bijective29have values := WeightedValues.of_functions with weights, function30have closure := indexedRingSum.closed at A, add, zero, neg, mul, one, w, converse(f, V, n), n with*31have sumIn := closure with values32witness indexedRingSum(w, converse(f, V, n), add, zero, n)33refine⟨sumIn, n, natural, converse(f, V, n), enumeration, rfl⟩34uniqueness35intros u uIn first v vIn second36obtain⟨n, naturalN, f, enumerationF, valueF⟩:= first37obtain⟨m, naturalM, g, enumerationG, valueG⟩:= second38have same := indexedRingSum.reindex at A, add, zero, neg, mul, one, w, V, f, g, n, m with*39cc4041notationscoped finiteRingSum(V, R, w) => finiteRingSum(V, R.carrier, R.add, R.zero, R.neg, R.mul, R.one, w)4243theorem finiteRingSum.closed (V A add zero neg mul one w : Set)44requiresring : IsCommRing(A, add, zero, neg, mul, one)45requires finite : Finite(V)46requires weights : w : V → A47shows finiteRingSum(V, A, add, zero, neg, mul, one, w) ∈ A48proof49have selected := finiteRingSum.selected at V, A, add, zero, neg, mul, one, w with*50exact selected.left5152theorem finiteRingSum.enumeration (V A add zero neg mul one w f : Set, n : Nat)53requiresring : IsCommRing(A, add, zero, neg, mul, one)54requires weights : w : V → A55requires enumeration : BijectiveBetween(f, n, V)56shows finiteRingSum(V, A, add, zero, neg, mul, one, w) = indexedRingSum(w, f, add, zero, n)57proof58have reverse := converse.bijective with enumeration59have cardinality : HasCardinality(V, n)60unfold HasCardinality61refine⟨assumption, ?_⟩62unfold Equipotent63refine⟨converse(f, n, V), reverse⟩64have finite := Finite.from_cardinality with cardinality65have selected := finiteRingSum.selected at V, A, add, zero, neg, mul, one, w with*66obtain⟨m, naturalM, g, enumerationG, value⟩:= selected.right67have same := indexedRingSum.reindex at A, add, zero, neg, mul, one, w, V, g, f, m, n with*68cc6970theorem indexedRingSum.congr (w v f g add zero : Set, n : Nat)71shows (∀ i ∈ n, w(f(i)) = v(g(i))) → indexedRingSum(w, f, add, zero, n) = indexedRingSum(v, g, add, zero, n)72proof73induction n74| zero =>75intro values76simp [indexedRingSum]77| succ(k) using ih =>78intro values79haveprefix : ∀ i ∈ k, w(f(i)) = v(g(i))80intros i before81have included := membership.into_successor with*82exact values with i, included83have last := values with k, membership.self_successor at k with*84have previous := ih withprefix85rw [indexedRingSum, indexedRingSum, previous, last]86rfl8788theorem finiteRingSum.congr (V A add zero neg mul one w v : Set)89requiresring : IsCommRing(A, add, zero, neg, mul, one)90requires finite : Finite(V)91requires weightsW : w : V → A92requires weightsV : v : V → A93requires values : ∀ x ∈ V, w(x) = v(x)94shows finiteRingSum(V, A, add, zero, neg, mul, one, w) = finiteRingSum(V, A, add, zero, neg, mul, one, v)95proof96have equal := FunctionBetween.ext with weightsW, weightsV, values97rw equal98rfl99100theorem indexedRingSum.zero (A add zero neg mul one w f : Set, n : Nat)101requiresring : IsCommRing(A, add, zero, neg, mul, one)102shows (∀ i ∈ n, w(f(i)) = zero) → indexedRingSum(w, f, add, zero, n) = zero103proof104induction n105| zero =>106intro values107rw indexedRingSum108rfl109| succ(k) using ih =>110intro values111haveprefix : ∀ i ∈ k, w(f(i)) = zero112intros i before113have included := membership.into_successor with*114exact values with i, included115have last := values with k, membership.self_successor at k with*116have previous := ih withprefix117rw [indexedRingSum, previous, last]118ring119120theorem finiteRingSum.zero (V A add zero neg mul one w : Set)121requiresring : IsCommRing(A, add, zero, neg, mul, one)122requires finite : Finite(V)123requires weights : w : V → A124requires values : ∀ x ∈ V, w(x) = zero125shows finiteRingSum(V, A, add, zero, neg, mul, one, w) = zero126proof127have selected := finiteRingSum.selected at V, A, add, zero, neg, mul, one, w with*128obtain⟨n, natural, f, enumeration, equation⟩:= selected.right129have injective := Bijective.injective with enumeration130have function := Injective.function with injective131have vanishing : ∀ i ∈ n, w(f(i)) = zero132intros i hi133have member := FunctionBetween.apply_closed with*134exact values with f(i), member135have zeroSum := indexedRingSum.zero at A, add, zero, neg, mul, one, w, f, n with*136rw equation137exact zeroSum with vanishing138139section Linearity140variable (V A add zero neg mul one w : Set)141hypothesisring : IsCommRing(A, add, zero, neg, mul, one)142hypothesis finite : Finite(V)143hypothesis weights : w : V → A144notationring R := (A, add, zero, neg, mul, one)145146theorem finiteRingSum.add (v : Set)147requires weightsV : v : V → A148shows finiteRingSum(V, A, add, zero, neg, mul, one, coefficientAdd(V, A, add, zero, neg, mul, one, w, v)) =149 finiteRingSum(V, A, add, zero, neg, mul, one, w) + finiteRingSum(V, A, add, zero, neg, mul, one, v)150proof151have selected := finiteRingSum.selected at V, A, add, zero, neg, mul, one, w with*152obtain⟨n, natural, f, enumeration, equationW⟩:= selected.right153have inj := Bijective.injective with enumeration154have function := Injective.function with inj155have valuesW := WeightedValues.of_functions with weights, function156have valuesV := WeightedValues.of_functions with weightsV, function157have combined := coefficientAdd.function at V, A, add, zero, neg, mul, one, w, v with*158have equationV := finiteRingSum.enumeration at V, A, add, zero, neg, mul, one, v, f, n with*159have equationSum := finiteRingSum.enumeration at V, A, add, zero, neg, mul, one, coefficientAdd(V, A, add, zero, neg, mul, one, w, v), f, n with*160have equations : ∀ i ∈ n, coefficientAdd(V, A, add, zero, neg, mul, one, w, v)(f(i)) = w(f(i)) + v(f(i))161intros i hi162have member := FunctionBetween.apply_closed with*163exact coefficientAdd.value at V, A, add, zero, neg, mul, one, w, v, f(i) with*164have distribute := indexedRingSum.add at A, add, zero, neg, mul, one, w, v, coefficientAdd(V, A, add, zero, neg, mul, one, w, v), f, n with*165rw [equationSum, equationW, equationV]166exact distribute with valuesW, valuesV, equations167168theorem finiteRingSum.scale (a : Set)169requires scalar : a ∈ A170shows finiteRingSum(V, A, add, zero, neg, mul, one, coefficientScale(V, A, add, zero, neg, mul, one, a, w)) =171 a * finiteRingSum(V, A, add, zero, neg, mul, one, w)172proof173have selected := finiteRingSum.selected at V, A, add, zero, neg, mul, one, w with*174obtain⟨n, natural, f, enumeration, equationW⟩:= selected.right175have inj := Bijective.injective with enumeration176have function := Injective.function with inj177have valuesW := WeightedValues.of_functions with weights, function178have scaled := coefficientScale.function at V, A, add, zero, neg, mul, one, a, w with*179have equationScale := finiteRingSum.enumeration at V, A, add, zero, neg, mul, one, coefficientScale(V, A, add, zero, neg, mul, one, a, w), f, n with*180have equations : ∀ i ∈ n, coefficientScale(V, A, add, zero, neg, mul, one, a, w)(f(i)) = a * w(f(i))181intros i hi182have member := FunctionBetween.apply_closed at f, n, V, i with*183exact coefficientScale.value at V, A, add, zero, neg, mul, one, a, w, f(i) with*184have distribute := indexedRingSum.scale at A, add, zero, neg, mul, one, w, a, coefficientScale(V, A, add, zero, neg, mul, one, a, w), f, n with*185rw [equationScale, equationW]186exact distribute with valuesW, equations187188end Linearity189