diff --git a/CHANGELOG.md b/CHANGELOG.md index 5891740538..3954dd8fc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -901,7 +901,7 @@ Major improvements for them still live in `Data.Nat.DivMod` (which also publicly re-exports them to provide backwards compatability). -* Beneficieries of this change include `Data.Rational.Unnormalised.Base` whose +* Beneficiaries of this change include `Data.Rational.Unnormalised.Base` whose dependencies are now significantly smaller. ### Moved raw bundles from Data.X.Properties to Data.X.Base @@ -2200,6 +2200,11 @@ Other minor changes suc-injective : Injective _≡_ _≡_ suc ``` +* Added a new pattern synonym to `Data.Nat.Divisibility.Core`: + ```agda + pattern divides-refl q = divides q refl + ``` + * Added new definitions and proofs to `Data.Nat.Primality`: ```agda Composite : ℕ → Set diff --git a/src/Data/Nat/Coprimality.agda b/src/Data/Nat/Coprimality.agda index 6f126a80d6..f07901cff7 100644 --- a/src/Data/Nat/Coprimality.agda +++ b/src/Data/Nat/Coprimality.agda @@ -9,8 +9,6 @@ module Data.Nat.Coprimality where open import Data.Empty -open import Data.Fin.Base using (toℕ; fromℕ<) -open import Data.Fin.Properties using (toℕ-fromℕ<) open import Data.Nat.Base open import Data.Nat.Divisibility open import Data.Nat.GCD @@ -45,8 +43,8 @@ coprime⇒GCD≡1 : ∀ {m n} → Coprime m n → GCD m n 1 coprime⇒GCD≡1 {m} {n} c = GCD.is (1∣ m , 1∣ n) (∣-reflexive ∘ c) GCD≡1⇒coprime : ∀ {m n} → GCD m n 1 → Coprime m n -GCD≡1⇒coprime g cd with GCD.greatest g cd -... | divides q eq = m*n≡1⇒n≡1 q _ (P.sym eq) +GCD≡1⇒coprime g cd with divides q eq ← GCD.greatest g cd + = m*n≡1⇒n≡1 q _ (P.sym eq) coprime⇒gcd≡1 : ∀ {m n} → Coprime m n → gcd m n ≡ 1 coprime⇒gcd≡1 coprime = GCD.unique (gcd-GCD _ _) (coprime⇒GCD≡1 coprime) @@ -111,9 +109,9 @@ recompute {n} {d} c = Nullary.recompute (coprime? n d) c Bézout-coprime : ∀ {i j d} .{{_ : NonZero d}} → Bézout.Identity d (i * d) (j * d) → Coprime i j -Bézout-coprime {d = suc _} (Bézout.+- x y eq) (divides q₁ refl , divides q₂ refl) = +Bézout-coprime {d = suc _} (Bézout.+- x y eq) (divides-refl q₁ , divides-refl q₂) = lem₁₀ y q₂ x q₁ eq -Bézout-coprime {d = suc _} (Bézout.-+ x y eq) (divides q₁ refl , divides q₂ refl) = +Bézout-coprime {d = suc _} (Bézout.-+ x y eq) (divides-refl q₁ , divides-refl q₂) = lem₁₀ x q₁ y q₂ eq -- Coprime numbers satisfy Bézout's identity. diff --git a/src/Data/Nat/DivMod.agda b/src/Data/Nat/DivMod.agda index b4c225d6a4..c879cec098 100644 --- a/src/Data/Nat/DivMod.agda +++ b/src/Data/Nat/DivMod.agda @@ -22,8 +22,6 @@ open import Relation.Binary.PropositionalEquality open import Relation.Nullary.Decidable using (yes; no) open import Relation.Nullary.Decidable using (False; toWitnessFalse) -import Algebra.Properties.CommutativeSemigroup *-commutativeSemigroup as *-CS - open ≤-Reasoning ------------------------------------------------------------------------ @@ -91,7 +89,8 @@ m*n≤o⇒[o∸m*n]%n≡o%n m {n} {o} m*n≤o = begin-equality m∣n⇒o%n%m≡o%m : ∀ m n o .⦃ _ : NonZero m ⦄ .⦃ _ : NonZero n ⦄ → m ∣ n → o % n % m ≡ o % m -m∣n⇒o%n%m≡o%m m n o (divides p refl) = begin-equality +m∣n⇒o%n%m≡o%m m n@.(p * m) o (divides-refl p) = begin-equality + o % n % m ≡⟨⟩ o % pm % m ≡⟨ %-congˡ (m%n≡m∸m/n*n o pm) ⟩ (o ∸ o / pm * pm) % m ≡˘⟨ cong (λ # → (o ∸ #) % m) (*-assoc (o / pm) p m) ⟩ (o ∸ o / pm * p * m) % m ≡⟨ m*n≤o⇒[o∸m*n]%n≡o%n (o / pm * p) lem ⟩ @@ -171,7 +170,8 @@ m<[1+n%d]⇒m≤[n%d] {m} n (suc d-1) = k<1+a[modₕ]n⇒k≤a[modₕ]n 0 m n d- m′ * n′ + (m′ * j + (n′ + j * d) * k) * d ∎ %-remove-+ˡ : ∀ {m} n {d} .{{_ : NonZero d}} → d ∣ m → (m + n) % d ≡ n % d -%-remove-+ˡ {m} n {d@(suc d-1)} (divides p refl) = begin-equality +%-remove-+ˡ {m@.(p * d)} n {d@(suc _)} (divides-refl p) = begin-equality + (m + n) % d ≡⟨⟩ (p * d + n) % d ≡⟨ cong (_% d) (+-comm (p * d) n) ⟩ (n + p * d) % d ≡⟨ [m+kn]%n≡m%n n p d ⟩ n % d ∎ @@ -191,7 +191,7 @@ m<[1+n%d]⇒m≤[n%d] {m} n (suc d-1) = k<1+a[modₕ]n⇒k≤a[modₕ]n 0 m n d- /-congʳ refl = refl 0/n≡0 : ∀ n .{{_ : NonZero n}} → 0 / n ≡ 0 -0/n≡0 (suc n-1) = refl +0/n≡0 (suc _) = refl n/1≡n : ∀ n → n / 1 ≡ n n/1≡n n = a[divₕ]1≡a 0 n @@ -203,7 +203,7 @@ m*n/n≡m : ∀ m n .{{_ : NonZero n}} → m * n / n ≡ m m*n/n≡m m (suc n-1) = a*n[divₕ]n≡a 0 m n-1 m/n*n≡m : ∀ {m n} .{{_ : NonZero n}} → n ∣ m → m / n * n ≡ m -m/n*n≡m {_} {n@(suc n-1)} (divides q refl) = cong (_* n) (m*n/n≡m q n) +m/n*n≡m {_} {n@(suc _)} (divides-refl q) = cong (_* n) (m*n/n≡m q n) m*[n/m]≡n : ∀ {m n} .{{_ : NonZero m}} → m ∣ n → m * (n / m) ≡ n m*[n/m]≡n {m} m∣n = trans (*-comm m (_ / m)) (m/n*n≡m m∣n) @@ -261,14 +261,16 @@ m≥n⇒m/n>0 {m@(suc _)} {n@(suc _)} m≥n = begin +-distrib-/-∣ˡ : ∀ {m} n {d} .{{_ : NonZero d}} → d ∣ m → (m + n) / d ≡ m / d + n / d -+-distrib-/-∣ˡ {m} n {d} (divides p refl) = +-distrib-/ m n (begin-strict ++-distrib-/-∣ˡ {m@.(p * d)} n {d} (divides-refl p) = +-distrib-/ m n (begin-strict + m % d + n % d ≡⟨⟩ p * d % d + n % d ≡⟨ cong (_+ n % d) (m*n%n≡0 p d) ⟩ n % d <⟨ m%n