-
Notifications
You must be signed in to change notification settings - Fork 259
Continuing with generic GCD. #1392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
08d1270
Continuing with generic GCD.
mechvel 9f88991
Continuing with the generic GCD project.
mechvel e9d7b03
Merge remote-tracking branch 'upstream/master'
MatthewDaggitt 3315368
Fix
MatthewDaggitt 8bdb3ab
Merge remote-tracking branch 'upstream/master'
MatthewDaggitt bf0e128
Continuing general GCD project.
mechvel e308865
Fixing the beginning of the generic GCD project.
mechvel 062054c
Merge branch 'master' into master
MatthewDaggitt 19226dc
Removed GCD content of PR
MatthewDaggitt 1938536
Fixed whitespace
MatthewDaggitt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Algebra/Properties/CancellativeCommutativeSemiring.agda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| ------------------------------------------------------------------------ | ||
| -- The Agda standard library | ||
| -- | ||
| -- Some properties of operations in CancellativeCommutativeSemiring. | ||
| ------------------------------------------------------------------------ | ||
|
|
||
| {-# OPTIONS --without-K --safe #-} | ||
|
|
||
| open import Algebra using (CancellativeCommutativeSemiring) | ||
| open import Algebra.Definitions using (AlmostRightCancellative) | ||
| open import Data.Product using (_,_) | ||
| open import Data.Sum.Base using (_⊎_; inj₁; inj₂) | ||
| open import Relation.Binary using (Decidable) | ||
| import Relation.Binary.Reasoning.Setoid as EqReasoning | ||
| open import Relation.Nullary using (yes; no) | ||
| open import Relation.Nullary.Negation using (contradiction) | ||
|
|
||
| module Algebra.Properties.CancellativeCommutativeSemiring | ||
| {a ℓ} (R : CancellativeCommutativeSemiring a ℓ) | ||
| (open CancellativeCommutativeSemiring R) where | ||
|
|
||
| open EqReasoning setoid | ||
| open import Algebra.Consequences.Setoid setoid | ||
| using (comm+cancelˡ-nonZero⇒cancelʳ-nonZero) | ||
| open import Algebra.Properties.CommutativeSemigroup *-commutativeSemigroup | ||
| using (x∙yz≈y∙xz) | ||
| open import Algebra.Properties.Semiring.Divisibility semiring | ||
|
|
||
| *-cancelʳ-nonZero : AlmostRightCancellative _≈_ 0# _*_ | ||
MatthewDaggitt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| *-cancelʳ-nonZero = comm+cancelˡ-nonZero⇒cancelʳ-nonZero *-comm 0# *-cancelˡ-nonZero | ||
|
|
||
| xy≈0⇒x≈0∨y≈0 : Decidable _≈_ → ∀ {x y} → x * y ≈ 0# → x ≈ 0# ⊎ y ≈ 0# | ||
| xy≈0⇒x≈0∨y≈0 _≟_ {x} {y} xy≈0 with x ≟ 0# | y ≟ 0# | ||
| ... | yes x≈0 | _ = inj₁ x≈0 | ||
| ... | no _ | yes y≈0 = inj₂ y≈0 | ||
| ... | no x≉0 | no y≉0 = contradiction y≈0 y≉0 | ||
| where | ||
| xy≈x*0 = trans xy≈0 (sym (zeroʳ x)); y≈0 = *-cancelˡ-nonZero y 0# x≉0 xy≈x*0 | ||
|
|
||
| x≉0∧y≉0⇒xy≉0 : Decidable _≈_ → ∀ {x y} → x ≉ 0# → y ≉ 0# → x * y ≉ 0# | ||
| x≉0∧y≉0⇒xy≉0 _≟_ x≉0 y≉0 xy≈0 with xy≈0⇒x≈0∨y≈0 _≟_ xy≈0 | ||
| ... | inj₁ x≈0 = x≉0 x≈0 | ||
| ... | inj₂ y≈0 = y≉0 y≈0 | ||
|
|
||
| xy∣x⇒y∣1 : ∀ {x y} → x ≉ 0# → x * y ∣ x → y ∣ 1# | ||
| xy∣x⇒y∣1 {x} {y} x≉0 (q , q*xy≈x) = q , *-cancelˡ-nonZero (q * y) 1# x≉0 (begin | ||
| x * (q * y) ≈⟨ x∙yz≈y∙xz x q y ⟩ | ||
| q * (x * y) ≈⟨ q*xy≈x ⟩ | ||
| x ≈˘⟨ *-identityʳ x ⟩ | ||
| x * 1# ∎) | ||
103 changes: 103 additions & 0 deletions
103
src/Algebra/Properties/CancellativeCommutativeSemiring/GCD.agda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| ------------------------------------------------------------------------ | ||
| -- The Agda standard library | ||
| -- | ||
| -- Properties of the Greatest Common Divisor in | ||
| -- CancellativeCommutativeSemiring. | ||
| ------------------------------------------------------------------------ | ||
|
|
||
| {-# OPTIONS --without-K --safe #-} | ||
|
|
||
| open import Algebra using (CancellativeCommutativeSemiring) | ||
| open import Data.Product using (_,_; proj₁; proj₂) | ||
| open import Data.Sum.Base using (_⊎_) | ||
| open import Relation.Binary using (Decidable) | ||
| import Relation.Binary.Reasoning.Setoid as EqReasoning | ||
| open import Relation.Nullary using (Dec; yes; no) | ||
|
|
||
| module Algebra.Properties.CancellativeCommutativeSemiring.GCD | ||
| {a ℓ} (R : CancellativeCommutativeSemiring a ℓ) | ||
| (open CancellativeCommutativeSemiring R) | ||
| where | ||
|
|
||
| open import Algebra.Properties.Semiring.Primality semiring using (Coprime) | ||
| open import Algebra.Properties.Semiring.Divisibility semiring | ||
| open EqReasoning setoid | ||
| import Algebra.Properties.CommutativeSemigroup *-commutativeSemigroup as Of*CSemig | ||
| open import Algebra.Properties.CommutativeSemigroup.Divisibility | ||
| *-commutativeSemigroup using (x∣xy; x∣y∧z∣x/y⇒xz∣y; x∣y⇒zx∣zy) | ||
| open import Algebra.Properties.CancellativeCommutativeSemiring R using (xy∣x⇒y∣1) | ||
|
|
||
| --------------------------------------------------------------------------- | ||
| -- Re-exporting definition of GCD and its properties in semiring | ||
|
|
||
| open import Algebra.Properties.Semiring.GCD semiring public | ||
|
|
||
| --------------------------------------------------------------------------- | ||
| -- Properties of GCD | ||
|
|
||
| x≉0⊎y≉0⇒Coprime[x/gcd,y/gcd] : ∀ {x y d} → x ≉ 0# ⊎ y ≉ 0# → | ||
| ((mkIsGCD (q₁ , _) (q₂ , _) _) : IsGCD x y d) → | ||
| Coprime q₁ q₂ | ||
| x≉0⊎y≉0⇒Coprime[x/gcd,y/gcd] x≉0∨y≉0 gcd@(mkIsGCD d∣x d∣y greatest) x/d∣z y/d∣z = | ||
| xy∣x⇒y∣1 (x≉0∨y≉0⇒gcd≉0 gcd x≉0∨y≉0) (greatest | ||
| (x∣y∧z∣x/y⇒xz∣y d∣x x/d∣z) | ||
| (x∣y∧z∣x/y⇒xz∣y d∣y y/d∣z)) | ||
|
|
||
| ------------------------------------------------------------------------------ | ||
| -- gcd-distr is an important lemma of the gcd distributivity: | ||
| -- gcd (c*a) (c*b) is division-equivalent to c * (gcd a b). | ||
|
|
||
| gcd-distr : Decidable _≈_ → ∀ {a b c d d'} → IsGCD a b d → | ||
| IsGCD (c * a) (c * b) d' → d' ∣∣ (c * d) | ||
| gcd-distr _≟_ {a} {b} {c} {d} {d'} | ||
| isGCD[a,b,d]@(mkIsGCD (a' , a'd≈a) (b' , b'd≈b) _) | ||
| isGCD[ca,cb,d']@(mkIsGCD d'∣ca d'∣cb _) = aux (c ≟ 0#) | ||
| where | ||
| d∣a = (a' , a'd≈a); d∣b = (b' , b'd≈b) | ||
|
|
||
| aux : Dec (c ≈ 0#) → d' ∣∣ (c * d) | ||
| aux (yes c≈0) = d'∣cd , cd∣d' -- A trivial case. The goal is reduced to 0 ∣∣ 0. | ||
| where | ||
| cd≈0 = trans (*-congʳ c≈0) (zeroˡ d) | ||
| d'∣cd = ∣-respʳ (sym cd≈0) (_∣0 d') -- the first part of the goal | ||
|
|
||
| ca≈0 = trans (*-congʳ c≈0) (zeroˡ a) | ||
| ca∣∣0 = ∣∣-reflexive ca≈0 | ||
| cb≈0 = trans (*-congʳ c≈0) (zeroˡ b) | ||
| cb∣∣0 = ∣∣-reflexive cb≈0 | ||
| d'∣∣0 = GCD-unique ca∣∣0 cb∣∣0 isGCD[ca,cb,d'] (isGCD[0,x,x] 0#) | ||
| d'≈0 = 0∣x⇒x≈0 (proj₂ d'∣∣0) | ||
| cd∣0 = _∣0 (c * d) | ||
| cd∣d' = ∣-respʳ (sym d'≈0) cd∣0 -- the second part of the goal | ||
|
|
||
| aux (no c≉0) = -- General case. First prove cd ∣ d' | ||
| let | ||
| cd∣ca = x∣y⇒zx∣zy c d∣a | ||
| cd∣cb = x∣y⇒zx∣zy c d∣b | ||
| cd∣d' = IsGCD.greatest isGCD[ca,cb,d'] cd∣ca cd∣cb | ||
|
|
||
| -- It remains to prove d' ∣ cd | ||
| c∣ca = x∣xy c a | ||
| c∣cb = x∣xy c b -- hence xc ≈ gcd ca cb = d' for some x | ||
| c∣d'@(x , xc≈d') = IsGCD.greatest isGCD[ca,cb,d'] c∣ca c∣cb | ||
| xc∣ca@(y , _) = ∣-respˡ (sym xc≈d') d'∣ca | ||
| xc∣cb@(z , _) = ∣-respˡ (sym xc≈d') d'∣cb | ||
| ca≈c*yx = begin | ||
| c * a ≈⟨ sym (proj₂ xc∣ca) ⟩ | ||
| y * (x * c) ≈⟨ Of*CSemig.x∙yz≈z∙xy y x c ⟩ | ||
| c * (y * x) ∎ | ||
| cb≈c*zx = begin | ||
| c * b ≈⟨ sym (proj₂ xc∣cb) ⟩ | ||
| z * (x * c) ≈⟨ Of*CSemig.x∙yz≈z∙xy z x c ⟩ | ||
| c * (z * x) ∎ | ||
|
|
||
| yx≈a = *-cancelˡ-nonZero {c} (y * x) a c≉0 (sym ca≈c*yx) | ||
| zx≈b = *-cancelˡ-nonZero {c} (z * x) b c≉0 (sym cb≈c*zx) | ||
| x∣a = y , yx≈a | ||
| x∣b = z , zx≈b | ||
| x∣d = IsGCD.greatest isGCD[a,b,d] x∣a x∣b | ||
| cx∣cd = x∣y⇒zx∣zy c x∣d | ||
| cx≈d' = trans (*-comm c x) xc≈d' | ||
| d'∣cd = ∣-respˡ cx≈d' cx∣cd | ||
| in | ||
| d'∣cd , cd∣d' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.