From 911f1a0e38f536ade664a3b8a9de6ece418f03d9 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Sat, 28 Nov 2026 09:22:53 +0000 Subject: [PATCH] Speed up MovingSofa certificate compilation --- .../Foundations/AreaCertificateReplay.lean | 256 ++++++++++++++++++ .../Foundations/CapAreaCertificate.lean | 13 + .../Foundations/NicheAreaCertificate.lean | 17 ++ .../GerverSofa/KernelOnly/Core/Bundle003.lean | 39 ++- .../KernelOnly/Core/ProductCellBounds.lean | 55 ++++ .../PartE/Certificates/Bundle001.lean | 5 +- .../PartE/Certificates/Bundle002.lean | 5 +- .../PartE/Certificates/Bundle003.lean | 5 +- .../PartE/Certificates/Bundle004.lean | 5 +- .../PartE/Certificates/Bundle005.lean | 5 +- .../PartE/Certificates/Bundle006.lean | 5 +- .../PartE/Certificates/Bundle007.lean | 5 +- .../PartE/Certificates/Bundle008.lean | 5 +- .../PartE/Certificates/Bundle009.lean | 5 +- .../PartE/Certificates/Bundle010.lean | 5 +- .../PartE/Certificates/Bundle011.lean | 5 +- .../PartE/Certificates/Bundle012.lean | 5 +- .../PartE/Certificates/Bundle013.lean | 5 +- .../PartE/Certificates/Bundle014.lean | 5 +- .../PartE/Certificates/Bundle015.lean | 5 +- .../PartE/Certificates/Bundle016.lean | 5 +- .../PartE/Certificates/Bundle017.lean | 5 +- .../PartE/Certificates/Bundle018.lean | 5 +- .../PartE/Certificates/Bundle019.lean | 5 +- .../PartE/Certificates/Bundle020.lean | 5 +- .../PartE/Certificates/Bundle021.lean | 5 +- .../PartE/Certificates/Bundle022.lean | 5 +- .../PartE/Certificates/Bundle023.lean | 5 +- .../PartE/Certificates/Bundle024.lean | 5 +- .../PartE/Certificates/Bundle025.lean | 5 +- .../PartE/Certificates/Bundle026.lean | 5 +- .../PartE/Certificates/Bundle027.lean | 5 +- .../PartE/Certificates/Bundle028.lean | 5 +- .../PartE/Certificates/Bundle029.lean | 5 +- .../PartE/Certificates/Bundle030.lean | 5 +- .../KernelOnly/PartE/FixedPointResiduals.lean | 206 ++++++++++++++ LeanPool/MovingSofa/Imports.lean | 1 + 37 files changed, 672 insertions(+), 65 deletions(-) create mode 100644 LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean diff --git a/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean b/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean new file mode 100644 index 0000000000..e17ed3c2e2 --- /dev/null +++ b/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean @@ -0,0 +1,256 @@ +/- +Copyright (c) 2026 Dean Cureton and contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Dean Cureton, The Moving Sofa contributors, OpenAI +-/ +module + +public import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateInputs +import Mathlib.Algebra.Order.Ring.Int +import Lean.Elab.Tactic.Omega -- shake: keep + +/-! +# Area certificate replay + +A nonnegative squared angle determines which two endpoint products are needed +in each Taylor step. Combining the two outward divisions gives the same +integer interval endpoints as the original recurrence. +-/ + +@[expose] public section + +namespace MovingSofa.GerverAreaCert.AreaReplay + +open SI + +/-- Fast product on the area grid for a nonnegative right interval. -/ +def multiply (left right : SI) : SI := + let lo := left.lo * (if 0 ≤ left.lo then right.lo else right.hi) + let hi := left.hi * (if 0 ≤ left.hi then right.hi else right.lo) + ⟨lo / M, -((-hi) / M)⟩ + +private theorem imin_eq (a b : ℤ) : imin a b = min a b := by + unfold imin + split + next h => exact (min_eq_left h).symm + next h => exact (min_eq_right (le_of_not_ge h)).symm + +private theorem imax_eq (a b : ℤ) : imax a b = max a b := by + unfold imax + split + next h => exact (max_eq_right h).symm + next h => exact (max_eq_left (le_of_not_ge h)).symm + +private theorem lower_eq (value lo hi : ℤ) (ordered : lo ≤ hi) : + min (value * lo) (value * hi) = value * (if 0 ≤ value then lo else hi) := by + split + next nonnegative => exact min_eq_left (mul_le_mul_of_nonneg_left ordered nonnegative) + next negative => exact min_eq_right (mul_le_mul_of_nonpos_left ordered (le_of_not_ge negative)) + +private theorem upper_eq (value lo hi : ℤ) (ordered : lo ≤ hi) : + max (value * lo) (value * hi) = value * (if 0 ≤ value then hi else lo) := by + split + next nonnegative => exact max_eq_right (mul_le_mul_of_nonneg_left ordered nonnegative) + next negative => exact max_eq_left (mul_le_mul_of_nonpos_left ordered (le_of_not_ge negative)) + +private theorem multiply_eq (left right : SI) (orderedLeft : left.lo ≤ left.hi) + (orderedRight : right.lo ≤ right.hi) (nonnegative : 0 ≤ right.lo) : + multiply left right = SI.mul left right := by + have low := mul_le_mul_of_nonneg_right orderedLeft nonnegative + have high := mul_le_mul_of_nonneg_right orderedLeft (nonnegative.trans orderedRight) + simp only [multiply, SI.mul, imin_eq, imax_eq] + rw [min_eq_left (min_le_min low high), max_eq_right (max_le_max low high), + lower_eq _ _ _ orderedRight, upper_eq _ _ _ orderedRight] + +private theorem round_ordered (low high denominator : ℤ) (ordered : low ≤ high) + (positive : 0 < denominator) : low / denominator ≤ -((-high) / denominator) := by + apply (Int.ediv_le_ediv positive ordered).trans + have h₁ := Int.ediv_mul_le high positive.ne' + have h₂ := Int.ediv_mul_le (-high) positive.ne' + have h := add_le_add h₁ h₂ + rw [← add_mul, add_neg_cancel] at h + have sumNonpositive := nonpos_of_mul_nonpos_left h positive + omega + +private theorem multiply_ordered (left right : SI) (orderedLeft : left.lo ≤ left.hi) + (orderedRight : right.lo ≤ right.hi) (nonnegative : 0 ≤ right.lo) : + (multiply left right).lo ≤ (multiply left right).hi := by + have low := mul_le_mul_of_nonneg_right orderedLeft nonnegative + have high := mul_le_mul_of_nonneg_right orderedLeft (nonnegative.trans orderedRight) + apply round_ordered _ _ _ _ (by decide) + rw [← lower_eq _ _ _ orderedRight, ← upper_eq _ _ _ orderedRight] + exact (min_le_left _ _).trans (low.trans (le_max_left _ _)) + +private theorem square_nonnegative (input : SI) (nonnegative : 0 ≤ input.lo) : + 0 ≤ (multiply input input).lo := by + simp only [multiply, ite_eq_left nonnegative] + exact Int.ediv_nonneg (mul_self_nonneg _) (by decide) + +/-- Negate and divide an interval product, combining the two outward divisions. -/ +def multiplyDivideNeg (term square : SI) (divisor : ℤ) : SI := + let lo := term.lo * (if 0 ≤ term.lo then square.lo else square.hi) + let hi := term.hi * (if 0 ≤ term.hi then square.hi else square.lo) + let combined := M * divisor + ⟨(-hi) / combined, -(lo / combined)⟩ + +private theorem multiplyDivideNeg_eq (term square : SI) (divisor : ℤ) : + multiplyDivideNeg term square divisor = neg (divn (multiply term square) divisor) := by + simp only [multiplyDivideNeg, multiply, SI.neg, SI.divn, neg_neg, + Int.ediv_ediv_of_nonneg (by decide : 0 ≤ M)] + +private theorem step_ordered (term square : SI) (denominator : ℤ) (positive : 0 < denominator) + (termOrdered : term.lo ≤ term.hi) (squareOrdered : square.lo ≤ square.hi) + (squareNonnegative : 0 ≤ square.lo) : + (multiplyDivideNeg term square denominator).lo ≤ + (multiplyDivideNeg term square denominator).hi := by + rw [multiplyDivideNeg_eq] + simp only [SI.neg, SI.divn, neg_le_neg_iff] + exact round_ordered _ _ _ (multiply_ordered term square termOrdered + squareOrdered squareNonnegative) positive + +/-- Taylor iteration on the area grid using the nonnegative square. -/ +def iterate (square input : SI) : ℕ → SI × SI × SI × SI + | 0 => (input, one, input, one) + | n + 1 => + let state := iterate square input n + let sine := multiplyDivideNeg state.1 square (((2 * n + 2) * (2 * n + 3) : ℕ) : ℤ) + let cosine := multiplyDivideNeg state.2.1 square (((2 * n + 1) * (2 * n + 2) : ℕ) : ℤ) + (sine, cosine, add state.2.2.1 sine, add state.2.2.2 cosine) + +private theorem iterate_eq (square input : SI) (squareOrdered : square.lo ≤ square.hi) + (squareNonnegative : 0 ≤ square.lo) (inputOrdered : input.lo ≤ input.hi) (n : ℕ) : + iterate square input n = trigIter square input n ∧ + (iterate square input n).1.lo ≤ (iterate square input n).1.hi ∧ + (iterate square input n).2.1.lo ≤ (iterate square input n).2.1.hi := by + induction n with + | zero => + exact ⟨rfl, inputOrdered, le_rfl⟩ + | succ n ih => + have first : 0 < 2 * n + 1 := by omega + have second : 0 < 2 * n + 2 := by omega + have third : 0 < 2 * n + 3 := by omega + have sineDenominator : 0 < (((2 * n + 2) * (2 * n + 3) : ℕ) : ℤ) := + Int.natCast_pos.mpr (Nat.mul_pos second third) + have cosineDenominator : 0 < (((2 * n + 1) * (2 * n + 2) : ℕ) : ℤ) := + Int.natCast_pos.mpr (Nat.mul_pos first second) + refine ⟨?_, step_ordered _ _ _ sineDenominator ih.2.1 squareOrdered squareNonnegative, + step_ordered _ _ _ cosineDenominator ih.2.2 squareOrdered squareNonnegative⟩ + simp only [iterate, trigIter, multiplyDivideNeg_eq] + rw [multiply_eq _ _ ih.2.1 squareOrdered squareNonnegative, + multiply_eq _ _ ih.2.2 squareOrdered squareNonnegative, ih.1] + +/-- Compute the same area-grid trigonometric intervals using two endpoint products. -/ +def trig (input : SI) : SI × SI := + if 0 ≤ input.lo ∧ input.lo ≤ input.hi then + let state := iterate (multiply input input) input 20 + (⟨state.2.2.1.lo - 1, state.2.2.1.hi + 1⟩, ⟨state.2.2.2.lo - 1, state.2.2.2.hi + 1⟩) + else trigZ input + +/-- The specialized iteration computes the original area-grid interval endpoints. -/ +theorem trig_eq (input : SI) : trig input = trigZ input := by + unfold trig + split + next hi => + have h := (iterate_eq _ _ (multiply_ordered input input hi.2 hi.2 hi.1) + (square_nonnegative input hi.1) hi.2 20).1 + rw [h, multiply_eq _ _ hi.2 hi.2 hi.1] + rfl + next => rfl + +/-- Evaluate the phase and contact formulas using supplied trigonometric intervals. -/ +def evalFromTrig (stage : ℕ) (t : SI) (kind : ℕ) (sc : SI × SI) : SI × SI := + let s := sc.1 + let c := sc.2 + let data : SI × SI × SI × SI × SI × SI := + if stage = 1 then + (add (add (mul a1Z c) (mul a2Z s)) (ratI (-1) 1), + add (add (mul (neg a2Z) c) (mul a1Z s)) (ratI (-1) 2), + add (add (mul (imul (-2) a1Z) s) (mul (imul 2 a2Z) c)) (ratI 1 2), + add (add (mul (imul 2 a1Z) c) (mul (imul 2 a2Z) s)) (ratI (-1) 1), + k11Z, k12Z) + else if stage = 2 then + let f := add (add (divn (neg (mul t t)) 4) (mul b1Z t)) b2Z + (f, add (add (divn t 2) (neg b1Z)) (ratI (-1) 1), + add (add (ratI 1 1) (imul 2 b1Z)) (neg t), + add f (ratI 1 2), k21Z, k22Z) + else if stage = 3 then + let f := sub c1Z t + let g := add c2Z t + (f, g, add (ratI (-1) 1) (neg g), add (ratI 1 1) f, k31Z, k32Z) + else if stage = 4 then + let g := add (add (divn (neg (mul t t)) 4) (mul d1Z t)) d2Z + (add (add (neg (divn t 2)) d1Z) (ratI (-1) 1), g, + add (neg g) (ratI (-1) 2), + add (add (imul 2 d1Z) (ratI (-1) 1)) (neg t), k41Z, k42Z) + else + (add (add (mul e1Z c) (mul e2Z s)) (ratI (-1) 2), + add (add (mul (neg e2Z) c) (mul e1Z s)) (ratI (-1) 1), + add (add (ratI 1 1) (mul (imul (-2) e1Z) s)) (mul (imul 2 e2Z) c), + add (add (mul (imul 2 e1Z) c) (mul (imul 2 e2Z) s)) (ratI (-1) 2), + k51Z, k52Z) + let f := data.1 + let g := data.2.1 + let al := data.2.2.1 + let be := data.2.2.2.1 + let kx := data.2.2.2.2.1 + let ky := data.2.2.2.2.2 + let x0 := add (sub (mul c f) (mul s g)) kx + let y0 := add (add (mul s f) (mul c g)) ky + let x1 := if kind = 1 ∨ kind = 2 then sub x0 (mul al s) else x0 + let y1 := if kind = 1 ∨ kind = 2 then add y0 (mul al c) else y0 + let x2 := if kind = 1 then add x1 c else x1 + let y2 := if kind = 1 then add y1 s else y1 + let x3 := if kind = 3 ∨ kind = 4 then sub x2 (mul be c) else x2 + let y3 := if kind = 3 ∨ kind = 4 then sub y2 (mul be s) else y2 + (if kind = 3 then sub x3 s else x3, if kind = 3 then add y3 c else y3) + +private theorem evalFromTrig_eq (stage : ℕ) (t : SI) (kind : ℕ) : + evalFromTrig stage t kind (trigZ t) = evalZ stage t kind := rfl + +/-- Evaluate the original contact formulas using the specialized area-grid iteration. -/ +def eval (stage : ℕ) (t : SI) (kind : ℕ) : SI × SI := + evalFromTrig stage t kind (trig t) + +private theorem eval_eq (stage : ℕ) (t : SI) (kind : ℕ) : + eval stage t kind = evalZ stage t kind := by + rw [eval, trig_eq, evalFromTrig_eq] + +/-- An area-grid contact at a sampled stage time. -/ +def contact (index kind : ℕ) : SI × SI := eval (stageOf index) (ttZ index) kind + +private theorem contact_eq (index kind : ℕ) : contact index kind = contactZ index kind := + eval_eq _ _ _ + +/-- Contact at the requested fan position. -/ +def fan (index : ℕ) : SI × SI := contact (fanIdx index) (fanKind index) + +/-- The fan anchor contact. -/ +def anchor : SI × SI := contact (5 * NN) 3 + +/-- Compute the original fan determinant with the specialized contact evaluations. -/ +def cross (index : ℕ) : SI := + sub (mul (sub (fan index).1 anchor.1) (sub (fan (index + 1)).2 anchor.2)) + (mul (sub (fan index).2 anchor.2) (sub (fan (index + 1)).1 anchor.1)) + +/-- The specialized fan determinant has the same endpoints as the original. -/ +theorem cross_eq (index : ℕ) : cross index = crossZ index := by + simp only [cross, fan, anchor, contact_eq, crossZ, fanZ, anchorZ] + +/-- Compute the original covering rectangle area using the specialized evaluations. -/ +def rectangle (row column : ℕ) : ℤ := + let left := rowBase row column + let right := left + 1 + let kind := rowKind row + let leftContact := contact left kind + let rightContact := contact right kind + let lo := if rowFwd row then leftContact.1.lo else rightContact.1.lo + let hi := if rowFwd row then rightContact.1.hi else leftContact.1.hi + let height := imax (eval (stageOf right) ⟨(ttZ left).lo, (ttZ right).hi⟩ kind).2.hi + leftContact.2.hi + imax 0 (hi - lo) * imax 0 height + +/-- The specialized computation recovers the same signed integer rectangle area. -/ +theorem rectangle_eq (row column : ℕ) : rectangle row column = rectAreaZ row column := by + simp only [rectangle, contact_eq, eval_eq, rectAreaZ, rectLoZ, rectHiZ, rectHZ] + +end MovingSofa.GerverAreaCert.AreaReplay diff --git a/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean b/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean index aef5f44aa5..687a92b720 100644 --- a/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean +++ b/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean @@ -6,6 +6,7 @@ Authors: Dean Cureton, The Moving Sofa contributors module public import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateInputs +import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateReplay import Mathlib.Tactic.NormNum /-! @@ -68,6 +69,18 @@ private theorem blockSum_split (summand : ℕ → ℤ) : blockSum_append _ 512 64 63] simp only [Nat.reduceAdd, add_assoc] +private def replayLower (index : ℕ) : ℤ := (AreaReplay.cross index).lo + +private theorem replayLower_eq : replayLower = capLower := by + funext index + rw [replayLower, capLower, AreaReplay.cross_eq] + +@[instance_reducible] private def decideBlock (start length : ℕ) (expected : ℤ) : + Decidable (blockSum capLower start length = expected) := + decidable_of_iff (blockSum replayLower start length = expected) (by rw [replayLower_eq]) + +attribute [local instance 1100] decideBlock + private theorem capBlock0 : blockSum capLower 0 64 = -333692128541 := by decide +kernel diff --git a/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean b/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean index a69044fa3f..21cc8e5999 100644 --- a/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean +++ b/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean @@ -6,6 +6,7 @@ Authors: Dean Cureton, The Moving Sofa contributors module public import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateInputs +import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateReplay import Mathlib.Tactic.NormNum import LeanPool.MovingSofa.KernelCertificate -- shake: keep @@ -40,6 +41,22 @@ private theorem rowSumZ_halves (row : ℕ) : change ((List.range' 0 (32 + 32)).map (rectAreaZ row)).sum = _ rw [← List.range'_append_1, List.map_append, List.sum_append] +private def rectangleBlockReplay (row start length : ℕ) : ℤ := + ((List.range' start length).map (AreaReplay.rectangle row)).sum + +private theorem rectangleBlockReplay_eq (row start length : ℕ) : + rectangleBlockReplay row start length = rectangleBlock row start length := by + have row_eq : AreaReplay.rectangle row = rectAreaZ row := + funext (AreaReplay.rectangle_eq row) + simp only [rectangleBlockReplay, rectangleBlock, row_eq] + +@[instance_reducible] private def decideRectangleBlock (row start length : ℕ) (expected : ℤ) : + Decidable (rectangleBlock row start length = expected) := + decidable_of_iff (rectangleBlockReplay row start length = expected) + (by rw [rectangleBlockReplay_eq]) + +attribute [local instance 1100] decideRectangleBlock + private theorem nicheRow0First : rectangleBlock 0 0 32 = 8845072297334670692780361894254852519218486529319646026 := by gerver_kernel_decide diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/Bundle003.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/Bundle003.lean index b63ac32d7b..6d61ffbadb 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/Bundle003.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/Bundle003.lean @@ -6135,11 +6135,40 @@ private def blockRow (block : Fin 4) (offset : Fin 16) : Cell := private abbrev BlockPasses (block : Fin 4) : Prop := ∀ offset : Fin 16, RoundedRowPasses (blockRow block offset) --- Release the kernel's reduction cache between blocks. -private theorem roundedBlock0 : BlockPasses 0 := by decide +kernel -private theorem roundedBlock1 : BlockPasses 1 := by decide +kernel -private theorem roundedBlock2 : BlockPasses 2 := by decide +kernel -private theorem roundedBlock3 : BlockPasses 3 := by decide +kernel +-- A single rectangle contains the rounded path for every possible second cell. +private def roundedPathBox : ProductCellBounds.GridInterval × ProductCellBounds.GridInterval := + (⟨-125663, 1029⟩, ⟨-8, 68400⟩) + +private theorem roundedPathBox_covers : ∀ j : Cell, + roundedPathBox.1.Covers (roundedCell j).1 ∧ + roundedPathBox.2.Covers (roundedCell j).2.1 := by + decide +kernel + +private theorem roundedPathBox_passes : ∀ i : Cell, + ProductCellBounds.passes scaledTarget ((roundedCell i).1, (roundedCell i).2.1) + roundedPathBox (roundedCell i).2.2.1 (roundedCell i).2.2.2 = true := by + decide +kernel + +private theorem roundedBlock_from_box (block : Fin 4) : BlockPasses block := by + intro offset j + let i := blockRow block offset + have ordered := roundedPathBox_covers i + exact ProductCellBounds.passes_of_second_covered scaledTarget + ((roundedCell i).1, (roundedCell i).2.1) ((roundedCell j).1, (roundedCell j).2.1) + roundedPathBox (roundedCell i).2.2.1 (roundedCell i).2.2.2 + ⟨ordered.1.2.1, ordered.2.2.1⟩ (roundedPathBox_covers j) (roundedPathBox_passes i) + +private theorem roundedBlock0 : BlockPasses 0 := by + as_aux_lemma => exact roundedBlock_from_box 0 + +private theorem roundedBlock1 : BlockPasses 1 := by + as_aux_lemma => exact roundedBlock_from_box 1 + +private theorem roundedBlock2 : BlockPasses 2 := by + as_aux_lemma => exact roundedBlock_from_box 2 + +private theorem roundedBlock3 : BlockPasses 3 := by + as_aux_lemma => exact roundedBlock_from_box 3 private theorem roundedBlocksPass (block : Fin 4) : BlockPasses block := by fin_cases block diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/ProductCellBounds.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/ProductCellBounds.lean index fc711373d1..634528aaf2 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/ProductCellBounds.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/Core/ProductCellBounds.lean @@ -191,4 +191,59 @@ theorem sound {threshold : ℚ} {scaledThreshold : ℤ} {first second : RatInter ExactReplay.oneI.lo) (mul_lo_le yCovered cosCovered) exact ⟨lowerU.trans_le uBound, lowerV.trans_le vBound⟩ +/-- The endpoints of the inner interval lie in the outer interval. -/ +abbrev GridInterval.Covers (outer inner : GridInterval) : Prop := + outer.lo ≤ inner.lo ∧ inner.lo ≤ inner.hi ∧ inner.hi ≤ outer.hi + +private theorem minProduct_le (left value right factor : ℤ) + (low : left ≤ value) (high : value ≤ right) : + min (left * factor) (right * factor) ≤ value * factor := by + rcases le_or_gt 0 factor with nonnegative | negative + · exact (min_le_left _ _).trans (mul_le_mul_of_nonneg_right low nonnegative) + · exact (min_le_right _ _).trans (mul_le_mul_of_nonpos_right high negative.le) + +private theorem GridInterval.productLower_mono {outer inner factor : GridInterval} + (covered : outer.Covers inner) : outer.productLower factor ≤ inner.productLower factor := by + have regrouped : outer.productLower factor = + min (min (outer.lo * factor.lo) (outer.hi * factor.lo)) + (min (outer.lo * factor.hi) (outer.hi * factor.hi)) := by + unfold productLower + ac_rfl + rw [regrouped, productLower] + simp only [le_min_iff] + exact ⟨⟨(min_le_left _ _).trans (minProduct_le _ _ _ _ covered.1 + (covered.2.1.trans covered.2.2)), + (min_le_right _ _).trans (minProduct_le _ _ _ _ covered.1 + (covered.2.1.trans covered.2.2))⟩, + ⟨(min_le_left _ _).trans (minProduct_le _ _ _ _ + (covered.1.trans covered.2.1) covered.2.2), + (min_le_right _ _).trans (minProduct_le _ _ _ _ + (covered.1.trans covered.2.1) covered.2.2)⟩⟩ + +private theorem GridInterval.sub_covers_second (first : GridInterval) + {outer inner : GridInterval} (ordered : first.lo ≤ first.hi) + (covered : outer.Covers inner) : (first.sub outer).Covers (first.sub inner) := + ⟨sub_le_sub_left covered.2.2 _, sub_le_sub ordered covered.2.1, + sub_le_sub_left covered.1 _⟩ + +private theorem imageLower_mono {wideX wideY narrowX narrowY : GridInterval} + (x : wideX.Covers narrowX) (y : wideY.Covers narrowY) (cosine sine : GridInterval) : + imageLower wideX wideY cosine sine ≤ imageLower narrowX narrowY cosine sine := by + unfold imageLower + exact add_le_add (add_le_add_right (GridInterval.productLower_mono x) _) + (GridInterval.productLower_mono y) + +/-- A product-cell check for a covering rectangle certifies each covered second contact. -/ +theorem passes_of_second_covered (threshold : ℤ) + (first inner outer : GridInterval × GridInterval) (cosine sine : GridInterval) + (firstOrdered : first.1.lo ≤ first.1.hi ∧ first.2.lo ≤ first.2.hi) + (covered : outer.1.Covers inner.1 ∧ outer.2.Covers inner.2) + (checked : passes threshold first outer cosine sine = true) : + passes threshold first inner cosine sine = true := by + have x := first.1.sub_covers_second firstOrdered.1 covered.1 + have y := first.2.sub_covers_second firstOrdered.2 covered.2 + simp only [passes, decide_eq_true_eq] at checked ⊢ + exact ⟨checked.1.trans_le (imageLower_mono x y cosine sine), + checked.2.trans_le (imageLower_mono x y sine.neg cosine)⟩ + end GerverSofa.PartB.ProductCellBounds diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean index f4128ef206..d2266c860d 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean @@ -18,8 +18,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch004`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean index 8c8aab3271..1a941b4c07 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch014`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean index 8c5a5e6527..bfb8d352a3 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch012`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean index 79a4c3b9d4..84a4a70935 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch027`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean index 28f6dba364..39f21350d5 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch028`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean index 49423678bb..4461ca223d 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch023`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean index 8336c0b85f..9e0f3c04dc 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch026`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean index 085c321fab..2f4215a105 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch024`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean index 8bade538f9..84bc29f3b2 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch029`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean index 3e0b8444ac..f577723c09 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch013`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean index be2f61fc98..3ea26b607c 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch019`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean index f1ef738fe9..b4163b630a 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch021`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean index 043024752c..c5629a1ae0 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean @@ -17,8 +17,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch044`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean index e65161c398..85ab480ad6 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean @@ -15,8 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch020`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean index 53a51d8e29..1f9dc5500d 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean @@ -15,8 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch022`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean index 6c7f7a65e2..11d8723411 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean @@ -15,8 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch025`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean index ec8cd0d9dc..ae8442e7c9 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean @@ -15,8 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch030`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean index 8bfe73394f..0c91041d6e 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean @@ -15,8 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch031`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean index 6dd588ab00..c32dc61651 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean @@ -15,8 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch032`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean index ffaa78e2b9..f701eedf08 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean @@ -18,8 +18,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch043`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean index d1747e43af..170e56fdc9 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean @@ -18,8 +18,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch034`. -/ --- Try the binary-grid enclosure first for closed cell rejections. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean index 6558be416a..a471c47e8e 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch035`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean index 245ee286cf..32a08cb157 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch036`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean index 6496a5b8a3..386eca4a34 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch037`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean index 245d97cb5f..ee0f82ad28 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch038`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean index a2fbf91c37..49080339d4 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch039`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean index 0861c4c8e1..10394f7e08 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch040`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean index 23b6e50f4f..dc5afe6b07 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean @@ -17,8 +17,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle007 * `GerverSofa.KernelOnly.PartE.Certificates.Batch041`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean index c48409aba8..85aaac2b71 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean @@ -16,8 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch042`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean index cd1412bbcb..186a3d48bd 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean @@ -60,8 +60,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle007 * `GerverSofa.KernelOnly.PartE.Certificates.Reconstruction`. -/ --- Try the fixed-point residual test before exact replay. -attribute [local instance 1100] GerverSofa.PartE.FixedPointResiduals.BinaryGrid.decidableRejected +-- Replay closed rejections with the exact shift-based binary-grid evaluator. +attribute [local instance 1100] + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean index 85ff161d7c..cff878b885 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean @@ -1053,6 +1053,212 @@ inconclusive. -/ isTrue (by exact rejected_of_check cell checked) else decidableRejectedFast cell +/-! ## Binary grid arithmetic for kernel replay -/ + +namespace ShiftedGrid + +/-- Test whether an integer is nonnegative by inspecting its constructor. -/ +def nonnegative : Int → Bool + | .ofNat _ => true + | .negSucc _ => false + +private theorem nonnegative_spec (value : Int) : nonnegative value = true ↔ 0 ≤ value := by + cases value <;> simp [nonnegative] + +/-- Test strict positivity without subtracting the two sides of a comparison. -/ +def positive : Int → Bool + | .ofNat n => n != 0 + | .negSucc _ => false + +private theorem positive_spec (value : Int) : positive value = true ↔ 0 < value := by + cases value <;> simp [positive, Nat.pos_iff_ne_zero] + +-- Comparing the magnitudes directly avoids constructing an integer difference in the kernel. +/-- Compare signed endpoints by comparing their natural magnitudes. -/ +def endpointLE : Int → Int → Bool + | .ofNat a, .ofNat b => Nat.ble a b + | .negSucc a, .negSucc b => Nat.ble b a + | .negSucc _, .ofNat _ => true + | .ofNat _, .negSucc _ => false + +private theorem endpointLE_spec (a b : Int) : endpointLE a b = true ↔ a ≤ b := by + cases a <;> cases b <;> simp [endpointLE] <;> omega + +/-- Select the lower of two endpoints for a product enclosure. -/ +def lowerEndpoint (a b : ℤ) : ℤ := bif endpointLE a b then a else b + +/-- Select the higher of two endpoints for a product enclosure. -/ +def upperEndpoint (a b : ℤ) : ℤ := bif endpointLE a b then b else a + +private theorem lowerEndpoint_eq (a b : ℤ) : lowerEndpoint a b = SI.imin a b := by + simp only [lowerEndpoint, SI.imin, Bool.cond_eq_ite, endpointLE_spec] + +private theorem upperEndpoint_eq (a b : ℤ) : upperEndpoint a b = SI.imax a b := by + simp only [upperEndpoint, SI.imax, Bool.cond_eq_ite, endpointLE_spec] + +/-- Round a product down at its lower endpoint and up at its upper endpoint. -/ +def round (lo hi : ℤ) : SI := ⟨lo >>> (30 : Nat), -((-hi) >>> (30 : Nat))⟩ + +private theorem shift_eq (value : ℤ) : value >>> (30 : Nat) = value / denominator := by + rw [Int.shiftRight_eq_div_pow] + rfl + +/-- Multiply on the binary grid when the right interval is nonnegative. -/ +def mulRight (left right : SI) : SI := + round + (left.lo * (bif nonnegative left.lo then right.lo else right.hi)) + (left.hi * (bif nonnegative left.hi then right.hi else right.lo)) + +private theorem mulRight_eq (left right : SI) : + mulRight left right = mulNonnegativeRight left right := by + simp only [mulRight, round, shift_eq, mulNonnegativeRight, Bool.cond_eq_ite, nonnegative_spec] + +private theorem mulRight_lower_nonnegative {left right : SI} + (leftNonneg : 0 ≤ left.lo) (rightNonneg : 0 ≤ right.lo) : 0 ≤ (mulRight left right).lo := by + rw [mulRight_eq] + change 0 ≤ left.lo * (if 0 ≤ left.lo then right.lo else right.hi) / denominator + rw [ite_eq_left leftNonneg] + exact Int.ediv_nonneg (mul_nonneg leftNonneg rightNonneg) (by decide) + +/-- Multiply on the binary grid with shifts for outward rounding. -/ +def mul (left right : SI) : SI := + bif nonnegative right.lo then mulRight left right + else bif nonnegative left.lo then mulRight right left + else + let ll := left.lo * right.lo + let lh := left.lo * right.hi + let hl := left.hi * right.lo + let hh := left.hi * right.hi + round (lowerEndpoint (lowerEndpoint ll lh) (lowerEndpoint hl hh)) + (upperEndpoint (upperEndpoint ll lh) (upperEndpoint hl hh)) + +private theorem mul_eq (left right : SI) : mul left right = BinaryGrid.mul left right := by + simp only [mul, BinaryGrid.mul, Bool.cond_eq_ite, nonnegative_spec, mulRight_eq, + round, lowerEndpoint_eq, upperEndpoint_eq, shift_eq, mulFull] + +/-- Evaluate a polynomial using the two-product rule for a nonnegative input. -/ +def horner (coefficients : List SI) (input : SI) : SI := + coefficients.foldr (fun coefficient value => SI.add coefficient (mulRight value input)) + (point 0) + +private theorem mulRight_eq_mul (left right : SI) (nonnegative : 0 ≤ right.lo) : + mulRight left right = BinaryGrid.mul left right := by + rw [mulRight_eq, BinaryGrid.mul, ite_eq_left nonnegative] + +private theorem horner_eq (coefficients : List SI) (input : SI) (nonnegative : 0 ≤ input.lo) : + horner coefficients input = BinaryGrid.horner coefficients input := by + induction coefficients with + | nil => rfl + | cons coefficient tail ih => + change SI.add coefficient (mulRight (horner tail input) input) = + SI.add coefficient (BinaryGrid.mul (BinaryGrid.horner tail input) input) + rw [ih, mulRight_eq_mul _ _ nonnegative] + +/-- The upper endpoint of the Taylor error enclosure. +For nonnegative upper endpoints through `796018440` (about 0.74135 radians), +the scaled eleventh-power term rounds down to zero. -/ +def radius (upper : ℤ) : ℤ := + bif endpointLE upper 796018440 then 1 else (remainder upper).hi + +private theorem radius_eq (upper : ℤ) (nonnegative : 0 ≤ upper) : + radius upper = (remainder upper).hi := by + simp only [radius, Bool.cond_eq_ite, endpointLE_spec] + split + next bounded => + have positive : 0 < remainderDenominator := by decide + have upperBound : upper ^ 11 / remainderDenominator ≤ 0 := + (Int.ediv_le_ediv positive (pow_le_pow_left₀ nonnegative bounded 11)).trans + (by decide +kernel) + have lowerBound : 0 ≤ upper ^ 11 / remainderDenominator := + Int.ediv_nonneg (pow_nonneg nonnegative _) positive.le + change 1 = upper ^ 11 / remainderDenominator + 1 + rw [le_antisymm upperBound lowerBound, zero_add] + next => rfl + +/-- Replay the binary-grid Taylor enclosure for an ordered nonnegative argument. -/ +def trig (input : SI) : SI × SI := + let square := mulRight input input + let radius := radius input.hi + let error : SI := ⟨-radius, radius⟩ + (SI.add (horner cosineCoefficients square) error, + SI.add (mulRight (horner sineCoefficients square) input) error) + +private theorem trig_eq (input : SI) (nonnegative : 0 ≤ input.lo) + (ordered : input.lo ≤ input.hi) : trig input = BinaryGrid.trig input := by + have squareNonnegative := mulRight_lower_nonnegative nonnegative nonnegative + unfold trig BinaryGrid.trig + dsimp only + rw [horner_eq _ _ squareNonnegative, horner_eq _ _ squareNonnegative, + mulRight_eq_mul _ _ nonnegative, mulRight_eq_mul _ _ nonnegative, + radius_eq _ (nonnegative.trans ordered)] + rfl + +/-- Scale the interval on the binary grid, using shifts for the two dyadic fractions +in the residual formulas. -/ +def scale (value : ℚ) (interval : SI) : SI := + let lo := value.num * interval.lo + let hi := value.num * interval.hi + match value.den with + | 1 => ⟨lo, hi⟩ + | 2 => ⟨lo >>> (1 : Nat), -((-hi) >>> (1 : Nat))⟩ + | 4 => ⟨lo >>> (2 : Nat), -((-hi) >>> (2 : Nat))⟩ + | _ => scaleNonnegative value interval + +private theorem scale_eq (value : ℚ) (interval : SI) : + scale value interval = scaleNonnegative value interval := by + unfold scale + dsimp only + split + next eq => + simp [scaleNonnegative, SI.divn, eq] + next eq => + simp [Int.shiftRight_eq_div_pow, scaleNonnegative, SI.divn, eq] + next eq => + simp [Int.shiftRight_eq_div_pow, scaleNonnegative, SI.divn, eq] + next => rfl + +/-- Test whether the interval lies strictly on one side of zero. -/ +def separated (interval : SI) : Bool := + !nonnegative interval.hi || positive interval.lo + +private theorem separated_eq (interval : SI) : separated interval = excludesZero interval := by + apply Bool.eq_iff_iff.mpr + simp only [separated, excludesZero, Bool.or_eq_true, Bool.not_eq_true', + ← Bool.not_eq_true, nonnegative_spec, not_le, positive_spec, decide_eq_true_eq] + +/-- Compute the binary-grid rejection check using the specialized operations. -/ +def rejects (cell : AngleCell) : Bool := + let phi := ofRat cell.phiI + let theta := ofRat cell.thetaI + bif positive phi.lo && positive theta.lo then + let phiTrig := trig phi + let thetaTrig := trig theta + let bounds := residualsFromGrid point (ofRat kernelPiInterval) scale + mul phi theta phiTrig.1 phiTrig.2 thetaTrig.1 thetaTrig.2 + separated bounds.1 || separated bounds.2 + else false + +private theorem rejects_eq (cell : AngleCell) : rejects cell = BinaryGrid.rejects cell := by + unfold rejects BinaryGrid.rejects + simp only [Bool.cond_eq_ite, Bool.and_eq_true, positive_spec] + split + next positive => + have mulFunction : mul = BinaryGrid.mul := funext fun a => funext (mul_eq a) + have scaleFunction : scale = scaleNonnegative := funext fun a => funext (scale_eq a) + simp only [trig_eq _ positive.1.le (ofRat_encloses _).ordered, + trig_eq _ positive.2.le (ofRat_encloses _).ordered, scaleFunction, mulFunction, + separated_eq] + next => rfl + +/-- Decide closed rejections with the exact binary-grid check and its existing finer fallback. -/ +@[instance_reducible] def decidableRejected (cell : AngleCell) : + Decidable (cell.rejected = true) := + if checked : rejects cell = true then + isTrue (by exact BinaryGrid.rejected_of_check cell (rejects_eq cell ▸ checked)) + else decidableRejectedFast cell + +end ShiftedGrid + end BinaryGrid end GerverSofa.PartE.FixedPointResiduals diff --git a/LeanPool/MovingSofa/Imports.lean b/LeanPool/MovingSofa/Imports.lean index ecb303c795..1b825bc990 100644 --- a/LeanPool/MovingSofa/Imports.lean +++ b/LeanPool/MovingSofa/Imports.lean @@ -11,6 +11,7 @@ public import LeanPool.MovingSofa.Development.Geometry.Applications.Development0 public import LeanPool.MovingSofa.Development.Geometry.Applications.Development002 public import LeanPool.MovingSofa.Development.Geometry.Applications.Development003 public import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateInputs +public import LeanPool.MovingSofa.Development.Geometry.Foundations.AreaCertificateReplay public import LeanPool.MovingSofa.Development.Geometry.Foundations.CapAreaCertificate public import LeanPool.MovingSofa.Development.Geometry.Foundations.Development001 public import LeanPool.MovingSofa.Development.Geometry.Foundations.Development002