------------------------------------------------------------------------ -- The Agda standard library -- -- Core lemmas for division and modulus operations ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Nat.DivMod.Core where open import Agda.Builtin.Nat using () renaming (div-helper to divₕ; mod-helper to modₕ) open import Data.Nat.Base using (zero; suc; _+_; _*_; _∸_; _≤_; _<_; _≥_; z≤n; s≤s) open import Data.Nat.Properties open import Data.Sum.Base using (_⊎_; inj₁; inj₂) open import Data.Product.Base using (_×_; _,_) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong; sym; subst; trans) open import Relation.Nullary.Decidable using (yes; no) open import Relation.Nullary.Negation using (contradiction) open ≤-Reasoning ------------------------------------------------------------------------ -- Helper lemmas that have no interpretation for _%_, only for modₕ private mod-cong₃ : ∀ {c n a₁ a₂ b} → a₁ ≡ a₂ → modₕ c n a₁ b ≡ modₕ c n a₂ b mod-cong₃ refl = refl modₕ-skipTo0 : ∀ acc n a b → modₕ acc n (b + a) a ≡ modₕ (a + acc) n b 0 modₕ-skipTo0 acc n zero b = cong (λ v → modₕ acc n v 0) (+-identityʳ b) modₕ-skipTo0 acc n (suc a) b = begin-equality modₕ acc n (b + suc a) (suc a) ≡⟨ mod-cong₃ (+-suc b a) ⟩ modₕ acc n (suc b + a) (suc a) ≡⟨⟩ modₕ (suc acc) n (b + a) a ≡⟨ modₕ-skipTo0 (suc acc) n a b ⟩ modₕ (a + suc acc) n b 0 ≡⟨ cong (λ v → modₕ v n b 0) (+-suc a acc) ⟩ modₕ (suc a + acc) n b 0 ∎ ------------------------------------------------------------------------ -- Lemmas for modₕ that also have an interpretation for _%_ a[modₕ]1≡0 : ∀ a → modₕ 0 0 a 0 ≡ 0 a[modₕ]1≡0 zero = refl a[modₕ]1≡0 (suc a) = a[modₕ]1≡0 a n[modₕ]n≡0 : ∀ acc v → modₕ acc (acc + v) (suc v) v ≡ 0 n[modₕ]n≡0 acc v = modₕ-skipTo0 acc (acc + v) v 1 a[modₕ]n0 mod≢0) mod≤1+j , j≤k)) divₕ-offsetEq d (suc n) (suc j) (suc k) j≤d k≤d (inj₃ (eq , k