diff --git a/CHANGELOG.md b/CHANGELOG.md index efab9e7df7..47d0a49c0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -396,6 +396,14 @@ Additions to existing modules viaList : (List A → List B) → (DiffList A → DiffList B) ``` +* In `Data.Bool.Properties`: + ```agda + T-to-≡ : ∀ {x} → T x → x ≡ true + ≡-to-T : ∀ {x} → x ≡ true → T x + T-not-to-≡ : ∀ {x} → T (not x) → x ≡ false + ≡-to-T-not : ∀ {x} → x ≡ false → T (not x) + ``` + * In `Data.DifferenceList.Properties`: ```agda fromList-++ : ∀ xs ys → fromList (xs List.++ ys) ≗ fromList xs ++ fromList ys @@ -404,9 +412,55 @@ Additions to existing modules viaList⁺ : (f : List A → List B) → xs ∼ dxs → f xs ∼ viaList f dxs ``` +* In `Data.Integer.Base`: + ``` + _<ᵇ_ : ℤ → ℤ → Bool + -≤-⁻¹ : ∀ {m} {n} → -[1+ m ] ≤ -[1+ n ] → n ℕ.≤ m + +≤+⁻¹ : ∀ {m} {n} → + m ≤ + n → m ℕ.≤ n + -<-⁻¹ : ∀ {m} {n} → -[1+ m ] < -[1+ n ] → n ℕ.< m + +<+⁻¹ : ∀ {m} {n} → + m < + n → m ℕ.< n + ``` + +* In `Data.Integer.DivMod`: + ```agda + i/ℕ1≡i : ∀ i → i /ℕ 1 ≡ i + i/1≡i : ∀ i → i / + 1 ≡ i + /ℕ-congʳ : ∀ i {m} {n} .{{_ : ℕ.NonZero m}} → .{{_ : ℕ.NonZero n}} → + m ≡ n → i /ℕ m ≡ i /ℕ n + nonNeg[i]⇒i/ℕd : ∀ i d .{{_ : ℕ.NonZero d}} .{{_ : NonNegative i}} → + i /ℕ d ≡ + (∣ i ∣ ℕ./ d) + neg[i]∧∣i∣%d≡0⇒i/ℕd : ∀ i {d} .{{_ : ℕ.NonZero d}} .{{_ : Negative i}} → + ∣ i ∣ ℕ.% d ≡ 0 → i /ℕ d ≡ - (+ (∣ i ∣ ℕ./ d)) + neg[i]∧∣i∣%d≢0⇒i/ℕd : ∀ i d .{{_ : ℕ.NonZero d}} .{{_ : Negative i}} + .{{_ : ℕ.NonZero (∣ i ∣ ℕ.% d)}} → i /ℕ d ≡ -[1+ ∣ i ∣ ℕ./ d ] + *-cancelˡ-/ℕ : ∀ m i n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (m ℕ.* n)}} → + (+ m * i) /ℕ (m ℕ.* n) ≡ i /ℕ n + *-cancelʳ-/ℕ : ∀ i m n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (n ℕ.* m)}} → + (i * + m) /ℕ (n ℕ.* m) ≡ i /ℕ n + *-cancelˡ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (i * k)}} → + .{{_ : NonNegative i}} → (i * j) / (i * k) ≡ j / k + *-cancelʳ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (k * j)}} → + .{{_ : NonNegative j}} → (i * j) / (k * j) ≡ i / k + ``` + * In `Data.Integer.GCD`: ```agda - gcd[i,i]≡∣i∣ : ∀ i → gcd i i ≡ + ∣i∣ + gcd[i,i]≡∣i∣ : ∀ i → gcd i i ≡ + ∣i∣ + ``` + +* In `Data.Integer.Properties`: + ``` + <ᵇ⇒< : T (i <ᵇ j) → i < j + <⇒<ᵇ : i < j → T (i <ᵇ j) + nonZero⁻¹ : ∀ i → .{{NonZero i}} → i ≢ 0ℤ + nonNeg∧nonZero⇒Pos : ∀ i → .{{NonNegative i}} → .{{NonZero i}} → Positive i + ∣i-j∣≡0⇒i≡j : ∀ {i} {j} → ∣ i - j ∣ ≡ 0 → i ≡ j + i*j≢0⇒i≢0 : ∀ i {j} .{{_ : NonZero (i * j)}} → NonZero i + i*j≢0⇒j≢0 : ∀ i {j} .{{_ : NonZero (i * j)}} → NonZero j + i≥0∧j≥0⇒i*j≥0 : ∀ i j → .{{NonNegative i}} → .{{NonNegative j}} → + NonNegative (i * j) + i>0∧j<0⇒i*j<0 : ∀ i j → .{{Positive i}} → .{{Negative j}} → + Negative (i * j) ``` * In `Data.List.Membership.Propositional.Properties`: @@ -437,6 +491,8 @@ Additions to existing modules * In `Data.Nat.DivMod`: ```agda m0∧j<0⇒i*j<0 (+ m) i + helper : (+ m * i) /ℕ (m ℕ.* n) ≡ i /ℕ n + helper with ∣ + m * i ∣ ℕ.% (m ℕ.* n) in ∣m*i∣%[m*n] + ... | ℕ.zero = begin-equality + (+ m * i) /ℕ (m ℕ.* n) + ≡⟨ neg[i]∧∣i∣%d≡0⇒i/ℕd (+ m * i) ∣m*i∣%[m*n] ⟩ + - (+ (∣ + m * i ∣ ℕ./ (m ℕ.* n))) + ≡⟨ cong (-_ ∘′ +_ ∘′ (ℕ._/ _)) (∣i*j∣≡∣i∣*∣j∣ (+ m) i) ⟩ + - (+ ((m ℕ.* ∣ i ∣) ℕ./ (m ℕ.* n))) + ≡⟨ cong (-_ ∘′ +_) (ℕ.m*n/m*o≡n/o m ∣ i ∣ n) ⟩ + - (+ (∣ i ∣ ℕ./ n)) ≡⟨ neg[i]∧∣i∣%d≡0⇒i/ℕd i ∣i∣%m≡0 ⟨ + i /ℕ n ∎ + where + m*[∣i∣%n]≡0 : m ℕ.* (∣ i ∣ ℕ.% n) ≡ 0 + m*[∣i∣%n]≡0 = trans m*[∣i∣%n]≡∣m*i∣%[m*n] ∣m*i∣%[m*n] + ∣i∣%m≡0 : ∣ i ∣ ℕ.% n ≡ 0 + ∣i∣%m≡0 = ℕ.m*n≡0⇒n≡0 m _ m*[∣i∣%n]≡0 + ... | ℕ.suc _ = begin-equality + (+ m * i) /ℕ (m ℕ.* n) + ≡⟨ neg[i]∧∣i∣%d≢0⇒i/ℕd (+ m * i) (m ℕ.* n) ⟩ + -[1+ ∣ + m * i ∣ ℕ./ (m ℕ.* n) ] + ≡⟨ cong (-[1+_] ∘′ (ℕ._/ (m ℕ.* n))) (∣i*j∣≡∣i∣*∣j∣ (+ m) i) ⟩ + -[1+ (m ℕ.* ∣ i ∣) ℕ./ (m ℕ.* n) ] + ≡⟨ cong -[1+_] (ℕ.m*n/m*o≡n/o m ∣ i ∣ n) ⟩ + -[1+ ∣ i ∣ ℕ./ n ] ≡⟨ neg[i]∧∣i∣%d≢0⇒i/ℕd i n ⟨ + i /ℕ n ∎ + where instance + ∣m*i∣%[m*n]≢0 : ℕ.NonZero (∣ + m * i ∣ ℕ.% (m ℕ.* n)) + ∣m*i∣%[m*n]≢0 rewrite ∣m*i∣%[m*n] = _ + m*[∣i∣%n]≢0 : ℕ.NonZero (m ℕ.* (∣ i ∣ ℕ.% n)) + m*[∣i∣%n]≢0 rewrite m*[∣i∣%n]≡∣m*i∣%[m*n] | ∣m*i∣%[m*n] = _ + ∣i∣%n≢0 : ℕ.NonZero (∣ i ∣ ℕ.% n) + ∣i∣%n≢0 = ℕ.m*n≢0⇒n≢0 m + +*-cancelʳ-/ℕ : ∀ i m n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (n ℕ.* m)}} → + (i * + m) /ℕ (n ℕ.* m) ≡ i /ℕ n +*-cancelʳ-/ℕ i m n rewrite *-comm i (+ m) | ℕ.*-comm n m = *-cancelˡ-/ℕ m i n + +*-cancelˡ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (i * k)}} → + .{{_ : NonNegative i}} → (i * j) / (i * k) ≡ j / k +*-cancelˡ-/ (+ i) j k = begin-equality + (sign (+ i * k) ◃ 1) * ((+ i * j) /ℕ ∣ + i * k ∣) + ≡⟨ cong (λ x → (x ◃ 1) * ((+ i * j) /ℕ _)) (sign-* (+ i) k)⟩ + (sign k ◃ 1) * ((+ i * j) /ℕ ∣ + i * k ∣) + ≡⟨ cong ((sign k ◃ 1) *_) (/ℕ-congʳ (+ i * j) (∣i*j∣≡∣i∣*∣j∣ (+ i) k)) ⟩ + (sign k ◃ 1) * ((+ i * j) /ℕ (∣ + i ∣ ℕ.* ∣ k ∣)) + ≡⟨ cong ((sign k ◃ 1) *_) (*-cancelˡ-/ℕ i j ∣ k ∣) ⟩ + j / k ∎ + where + instance + _ : NonZero (+ i) + _ = i*j≢0⇒i≢0 (+ i) + _ : ℕ.NonZero (∣ + i ∣ ℕ.* ∣ k ∣) + _ = ℕ.m*n≢0 ∣ + i ∣ ∣ k ∣ + +*-cancelʳ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (k * j)}} → + .{{_ : NonNegative j}} → (i * j) / (k * j) ≡ i / k +*-cancelʳ-/ i j k rewrite *-comm i j | *-comm k j = *-cancelˡ-/ j i k + ------------------------------------------------------------------------ -- DEPRECATED NAMES ------------------------------------------------------------------------ diff --git a/src/Data/Integer/Properties.agda b/src/Data/Integer/Properties.agda index 9740d50686..ec00f1ef95 100644 --- a/src/Data/Integer/Properties.agda +++ b/src/Data/Integer/Properties.agda @@ -36,12 +36,12 @@ open import Relation.Binary.Structures open import Relation.Binary.Definitions using (DecidableEquality; Reflexive; Transitive; Antisymmetric; Total; Decidable; Irrelevant; Irreflexive; Asymmetric; LeftTrans; RightTrans; Trichotomous; tri≈; tri<; tri>) open import Relation.Binary.PropositionalEquality.Core - using (_≡_; refl; cong; cong₂; sym; _≢_; subst; subst₂; resp₂; trans) + using (_≡_; refl; cong; cong₂; sym; _≢_; ≢-sym; subst; subst₂; resp₂; trans) open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning; setoid; decSetoid; isEquivalence) open import Relation.Nullary.Decidable.Core using (yes; no) import Relation.Nullary.Reflects as Reflects -open import Relation.Nullary.Negation.Core using (¬_; contradiction) +open import Relation.Nullary.Negation.Core using (¬_; contradiction; contraposition) import Relation.Nullary.Decidable as Dec open import Algebra.Definitions {A = ℤ} _≡_ @@ -407,9 +407,16 @@ nonPositive⁻¹ -[1+ n ] = -≤+ nonNegative⁻¹ : ∀ i → .{{NonNegative i}} → i ≥ 0ℤ nonNegative⁻¹ (+ n) = +≤+ z≤n +nonZero⁻¹ : ∀ i → .{{NonZero i}} → i ≢ 0ℤ +nonZero⁻¹ (+ n) = contraposition +-injective (ℕ.≢-nonZero⁻¹ n) +nonZero⁻¹ -[1+ n ] () + negative j *-cancelʳ-<-neg {i} {j} n = *-cancelʳ-<-nonPos -[1+ n ] +------------------------------------------------------------------------ +-- Properties of _*_ and Positive/NonPositive/Negative/NonNegative + +i≥0∧j≥0⇒i*j≥0 : ∀ i j → .{{NonNegative i}} → .{{NonNegative j}} → NonNegative (i * j) +i≥0∧j≥0⇒i*j≥0 i j = nonNegative (*-monoʳ-≤-nonNeg j (nonNegative⁻¹ i)) + +i>0∧j<0⇒i*j<0 : ∀ i j → .{{Positive i}} → .{{Negative j}} → Negative (i * j) +i>0∧j<0⇒i*j<0 i j = negative (*-monoʳ-<-neg j (positive⁻¹ i)) + ------------------------------------------------------------------------ -- Properties of _*_ and ∣_∣ diff --git a/src/Data/Nat/DivMod.agda b/src/Data/Nat/DivMod.agda index 477acf0e14..5db943535d 100644 --- a/src/Data/Nat/DivMod.agda +++ b/src/Data/Nat/DivMod.agda @@ -450,6 +450,11 @@ m%n*o≡m*o%[n*o] m n o = begin-equality m * o ∸ m * o / (n * o) * (n * o) ≡⟨ m%n≡m∸m/n*n (m * o) (n * o) ⟨ m * o % (n * o) ∎ +m*n%o≡m*n%[m*o] : ∀ m n o .{{_ : NonZero o}} .{{_ : NonZero (m * o)}} → + m * (n % o) ≡ (m * n) % (m * o) +m*n%o≡m*n%[m*o] m n o rewrite *-comm m (n % o) | *-comm m n | *-comm m o + = m%n*o≡m*o%[n*o] n o m + [m*n+o]%[p*n]≡[m*n]%[p*n]+o : ∀ m {n o} p .{{_ : NonZero (p * n)}} → o < n → (m * n + o) % (p * n) ≡ (m * n) % (p * n) + o [m*n+o]%[p*n]≡[m*n]%[p*n]+o m {n} {o} p@(suc p-1) o1⇒1/p<1 {p} p>1 = lemma′ p (p>1⇒p≢0 p>1) p>1 1/q≥0 : NonNegative 1/q 1/q≥0 = pos⇒nonNeg 1/q {{1/pos⇒pos q}} +------------------------------------------------------------------------ +-- Properties of _/_ + +/-cancelʳ-< : ∀ {i} {j} d .{{_ : ℕ.NonZero d}} → i / d < j / d → i ℤ.< j +/-cancelʳ-< {i} {j} d@(suc _) (*<* i*d ∘ ≰ᵇ⇒≰ ∘ ≡-to-T-not) leq) + q≤ᵇ0≡false : (q ≤ᵇ 0ℚᵘ) ≡ false + q≤ᵇ0≡false = T-not-to-≡ (≰⇒≰ᵇ (<⇒≱ 00⇒round[-q]≡round[q] : ∀ {q} → q > 0ℚᵘ → round (- q) ≡ ℤ.- round q + q>0⇒round[-q]≡round[q] {q} q>0 = begin + round (- q) ≡⟨ if-cong -q≤0 ⟩ + ⌈ - q - ½ ⌉ ≡⟨ cong ceiling (neg-distrib-+ q ½) ⟨ + ⌈ - (q + ½) ⌉ ≡⟨ ⌈-q⌉≡-⌊q⌋ (q + ½) ⟩ + ℤ.- ⌊ q + ½ ⌋ ≡⟨ cong ℤ.-_ (if-cong q≥0) ⟨ + ℤ.- round q ∎ + where + open ≡-Reasoning + -q≤0 : (- q ≤ᵇ 0ℚᵘ) ≡ true + -q≤0 = T-to-≡ (≤⇒≤ᵇ (<⇒≤ (neg-mono-< q>0))) + q≥0 : (q ≤ᵇ 0ℚᵘ) ≡ false + q≥0 = T-not-to-≡ (≰⇒≰ᵇ (<⇒≱ q>0)) + +round[-q]≡-round[q] : ∀ q → round (- q) ≡ ℤ.- (round q) +round[-q]≡-round[q] q@record{} with <-cmp q 0ℚᵘ +... | tri< a ¬b ¬c = begin + round (- q) ≡⟨ ℤ.neg-involutive (round (- q)) ⟨ + ℤ.- (ℤ.- round (- q)) ≡⟨ cong ℤ.-_ (q>0⇒round[-q]≡round[q] (neg-mono-< a)) ⟨ + ℤ.- round (- (- q)) ≡⟨ cong (ℤ.-_ ∘ round) (neg-involutive-≡ q) ⟩ + ℤ.- round q ∎ where open ≡-Reasoning +... | tri≈ ¬a b ¬c = begin + round (- q) ≡⟨ round-cong (-‿cong b) ⟩ + ℤ.- 0ℤ ≡⟨ cong ℤ.-_ (round-cong b) ⟨ + ℤ.- round q ∎ where open ≡-Reasoning +... | tri> ¬a ¬b c = q>0⇒round[-q]≡round[q] c + +------------------------------------------------------------------------ +-- Bounds of ⌊_⌋ and ⌈_⌉ + +⌊q⌋≤q : ∀ q → ⌊ q ⌋ / 1 ≤ q +⌊q⌋≤q q@record{} = *≤* (begin + ⌊ q ⌋ ℤ.* (↧ q) ≤⟨ ℤ.[n/d]*d≤n (↥ q) (↧ q) ⟩ + (↥ q) ≡⟨ ℤ.*-identityʳ (↥ q) ⟨ + (↥ q) ℤ.* (↧ (⌊ q ⌋ / 1)) ∎) where open ℤ.≤-Reasoning + +q<⌊q⌋+1 : ∀ q → q < ⌊ q ⌋ / 1 + 1ℚᵘ +q<⌊q⌋+1 q@record{} = let n = ↥ q; d = ↧ q in *<* ( begin-strict + n ℤ.* 1ℤ ≡⟨ ℤ.*-identityʳ n ⟩ + n ≡⟨ ℤ.a≡a%n+[a/n]*n n d ⟩ + ℤ.+ (n ℤ.% d) ℤ.+ ⌊ q ⌋ ℤ.* d <⟨ ℤ.+-monoˡ-< (⌊ q ⌋ ℤ.* d) (ℤ.+<+ (ℤ.n%d