-- Positions below a sum m + n: a position is below m or is m + j for a -- position j below n, the two cases are exclusive, m is included in m + n -- by the identity, and the offset j ↦ m + j is a function from n into -- m + n. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.Nat.Offsets import OrderLemmas import Set.Function import Set.Tuple -- The cases of membership in m + n, and their exclusivity. theorem membership.add_cases (m n : Nat) (k : Set) requires member : k ∈ add(m, n) shows k ∈ m ∨ ∃ j ∈ n, k = add(m, j) proof have mNat : Nat(m) by typecheck have nNat : Nat(n) by typecheck have kNat := natural.member at add(m, n) with member have below := Less.of_membership at k, add(m, n) with member have split := Less.or_LessEqual at k, m cases split with small | large . left exact membership.of_Less with kNat, mNat, small . right obtain ⟨j, jNat, equation⟩ := LessEqual.exists_add at m, k with large have jBelow : j < n rw equation at below exact Less.add_left_cancel at j, n, m with below have jIn := membership.of_Less with jNat, nNat, jBelow witness j exact ⟨jIn, equation⟩ theorem membership.ne_add (m : Nat) (i j : Set) requires member : i ∈ m requires natural : Nat(j) shows i ≠ add(m, j) proof have below := Less.of_membership at i, m with member have above := LessEqual.add_right at m, j intro equal rw equal at below have refuted := LessEqual.not_Less with above exact refuted with below theorem membership.add_offset (m n : Nat) (j : Set) requires index : j ∈ n shows add(m, j) ∈ add(m, n) proof have sumNat := add.closed at n, m have below := Less.of_membership at j, n with index have shifted := Less.add_monotone_left at j, n, m with below exact membership.of_Less with (add.closed at j, m), sumNat, shifted -- The inclusion of m into m + n, as a subset and as the identity map. theorem natural.subset_add (m n : Nat) shows m ⊆ add(m, n) proof have sumNat := add.closed at n, m have bound := LessEqual.add_right at m, n intros i hi have iNat := natural.member at m with hi have below := Less.of_membership at i, m with hi have under := Less.transitive_LessEqual with below, bound exact membership.of_Less with iNat, sumNat, under theorem identity.into_add (m n : Nat) shows identity(m) : m → add(m, n) proof have subset := natural.subset_add at m, n exact identity.inclusion with subset -- The offset j ↦ m + j, a function from n into m + n. def offsetMap (m n : Set) : Set := { p ∈ n × add(m, n) | snd(p) = add(m, fst(p)) } theorem offsetMap.function (m n : Nat) shows offsetMap(m, n) : n → add(m, n) proof constructor . intros p member exact member.left constructor . intros x y relatedY z relatedZ obtain ⟨inY, shapeY⟩ := relatedY obtain ⟨_, shapeZ⟩ := relatedZ simp [fst.pair, snd.pair] at shapeY, shapeZ cc . intros j input have member := membership.add_offset at m, n, j with input refine ⟨add(m, j), member, _⟩ apply offsetMap.membership.backward refine ⟨(product.pair with input, member), _⟩ simp [fst.pair, snd.pair] theorem offsetMap.apply (m n : Nat) (j : Set) requires index : j ∈ n shows offsetMap(m, n)(j) = add(m, j) proof have function := offsetMap.function at m, n have shape := FunctionBetween.relates_apply with function, index have data := offsetMap.membership.forward with shape simp [fst.pair, snd.pair] at data exact data.right