-
Notifications
You must be signed in to change notification settings - Fork 156
Instances for Pseudolattice and OrderedCommRing
#1268
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
Open
LorenzoMolena
wants to merge
4
commits into
agda:master
Choose a base branch
from
LorenzoMolena:ocr+pl-instances
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89246b9
add instances of `Nat` and `Int` as `Pseudolattice`, and of `Int` as …
LorenzoMolena 0f6037e
fix typo, add better make from Posets to Pseudolattices
LorenzoMolena 4a415d5
update `makePseudolatticeFromPoset` and instances built using it
LorenzoMolena 561308e
move `maxLUB` and `minGLB` to `Data.Nat.Order`, remove unnecessary im…
LorenzoMolena 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| module Cubical.Algebra.OrderedCommRing.Instances.Int where | ||
|
|
||
| open import Cubical.Foundations.Prelude | ||
| open import Cubical.Foundations.Function | ||
| open import Cubical.Foundations.Equiv | ||
|
|
||
| import Cubical.Functions.Logic as L | ||
|
|
||
| open import Cubical.Data.Sum | ||
| open import Cubical.Data.Empty as ⊥ | ||
|
|
||
| open import Cubical.HITs.PropositionalTruncation | ||
|
|
||
| open import Cubical.Data.Int as ℤ | ||
| renaming (_+_ to _+ℤ_ ; _-_ to _-ℤ_; -_ to -ℤ_ ; _·_ to _·ℤ_) | ||
| open import Cubical.Data.Int.Order | ||
| renaming (_<_ to _<ℤ_ ; _≤_ to _≤ℤ_) | ||
| open import Cubical.Data.Nat using (ℕ ; zero ; suc) | ||
|
|
||
| open import Cubical.Algebra.CommRing | ||
| open import Cubical.Algebra.CommRing.Instances.Int | ||
|
|
||
| open import Cubical.Algebra.OrderedCommRing | ||
|
|
||
| open import Cubical.Relation.Nullary | ||
|
|
||
| open import Cubical.Relation.Binary.Order.StrictOrder | ||
| open import Cubical.Relation.Binary.Order.StrictOrder.Instances.Int | ||
|
|
||
| open import Cubical.Relation.Binary.Order.Pseudolattice | ||
| open import Cubical.Relation.Binary.Order.Pseudolattice.Instances.Int | ||
|
|
||
| open CommRingStr | ||
| open OrderedCommRingStr | ||
| open PseudolatticeStr | ||
| open StrictOrderStr | ||
|
|
||
| private | ||
| lemma0<+ : ∀ x y → 0 <ℤ x +ℤ y → (0 <ℤ x) L.⊔′ (0 <ℤ y) | ||
| lemma0<+ (pos zero) (pos zero) = ⊥.rec ∘ isIrrefl< | ||
| lemma0<+ (pos zero) (pos (suc n)) = ∣_∣₁ ∘ inr ∘ subst (0 <ℤ_) (sym $ pos0+ _) | ||
| lemma0<+ (pos (suc m)) (pos n) = λ _ → ∣ inl (suc-≤-suc zero-≤pos) ∣₁ | ||
| lemma0<+ (pos zero) (negsuc n) = ⊥.rec ∘ ¬pos≤negsuc ∘ subst (0 <ℤ_) | ||
| (sym $ pos0+ (negsuc n)) | ||
| lemma0<+ (pos (suc m)) (negsuc n) = λ _ → ∣ inl (suc-≤-suc zero-≤pos) ∣₁ | ||
| lemma0<+ (negsuc m) (pos zero) = ⊥.rec ∘ ¬pos≤negsuc | ||
| lemma0<+ (negsuc m) (pos (suc n)) = λ _ → ∣ inr (suc-≤-suc zero-≤pos) ∣₁ | ||
| lemma0<+ (negsuc m) (negsuc n) = ⊥.rec ∘ ¬pos≤negsuc ∘ subst (0 <ℤ_) | ||
| (sym $ neg+ (suc m) (suc n)) | ||
|
|
||
| ℤOrderedCommRing : OrderedCommRing ℓ-zero ℓ-zero | ||
| fst ℤOrderedCommRing = ℤ | ||
| 0r (snd ℤOrderedCommRing) = 0 | ||
| 1r (snd ℤOrderedCommRing) = 1 | ||
| _+_ (snd ℤOrderedCommRing) = _+ℤ_ | ||
| _·_ (snd ℤOrderedCommRing) = _·ℤ_ | ||
| -_ (snd ℤOrderedCommRing) = -ℤ_ | ||
| _<_ (snd ℤOrderedCommRing) = _<ℤ_ | ||
| _≤_ (snd ℤOrderedCommRing) = _≤ℤ_ | ||
| isOrderedCommRing (snd ℤOrderedCommRing) = isOrderedCommRingℤ | ||
| where | ||
| open IsOrderedCommRing | ||
|
|
||
| isOrderedCommRingℤ : IsOrderedCommRing 0 1 _+ℤ_ _·ℤ_ -ℤ_ _<ℤ_ _≤ℤ_ | ||
| isOrderedCommRingℤ .isCommRing = ℤCommRing .snd .isCommRing | ||
| isOrderedCommRingℤ .isPseudolattice = ℤ≤Pseudolattice .snd .is-pseudolattice | ||
| isOrderedCommRingℤ .isStrictOrder = ℤ<StrictOrder .snd .isStrictOrder | ||
| isOrderedCommRingℤ .<-≤-weaken = λ _ _ → <-weaken | ||
| isOrderedCommRingℤ .≤≃¬> = λ x y → | ||
| propBiimpl→Equiv isProp≤ (isProp¬ (y <ℤ x)) | ||
| (λ x≤y y<x → isIrrefl< (≤<-trans x≤y y<x)) | ||
| (λ ¬y<x → case x ≟ y return (λ _ → x ≤ℤ y) of λ { | ||
| (lt x<y) → <-weaken x<y ; | ||
| (eq x≡y) → subst (x ≤ℤ_) x≡y isRefl≤ ; | ||
| (gt y<z) → ⊥.rec (¬y<x y<z) }) | ||
| isOrderedCommRingℤ .+MonoR≤ = λ _ _ z → ≤-+o {o = z} | ||
| isOrderedCommRingℤ .+MonoR< = λ _ _ z → <-+o {o = z} | ||
| isOrderedCommRingℤ .posSum→pos∨pos = lemma0<+ | ||
| isOrderedCommRingℤ .<-≤-trans = λ _ _ _ → <≤-trans | ||
| isOrderedCommRingℤ .≤-<-trans = λ _ _ _ → ≤<-trans | ||
| isOrderedCommRingℤ .·MonoR≤ = λ _ _ _ → 0≤o→≤-·o | ||
| isOrderedCommRingℤ .·MonoR< = λ _ _ _ → 0<o→<-·o | ||
| isOrderedCommRingℤ .0<1 = isRefl≤ | ||
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
18 changes: 18 additions & 0 deletions
18
Cubical/Relation/Binary/Order/Pseudolattice/Instances/Int.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,18 @@ | ||
| module Cubical.Relation.Binary.Order.Pseudolattice.Instances.Int where | ||
|
|
||
| open import Cubical.Foundations.Prelude | ||
|
|
||
| open import Cubical.Data.Int | ||
| open import Cubical.Data.Int.Order renaming (_≤_ to _≤ℤ_) | ||
|
|
||
| open import Cubical.Relation.Binary.Order.Poset.Instances.Int | ||
| open import Cubical.Relation.Binary.Order.Pseudolattice | ||
|
|
||
| ℤ≤Pseudolattice : Pseudolattice ℓ-zero ℓ-zero | ||
| ℤ≤Pseudolattice = makePseudolatticeFromPoset ℤ≤Poset min max | ||
| min≤ | ||
| (λ {a b} → subst (_≤ℤ b) (minComm b a) min≤) | ||
| (λ {a b} x≤a x≤b → subst (_≤ℤ min a b) (minIdem _) (≤MonotoneMin x≤a x≤b)) | ||
| ≤max | ||
| (λ {a b} → subst (b ≤ℤ_) (maxComm b a) ≤max) | ||
| (λ {a b} a≤x b≤x → subst (max a b ≤ℤ_) (maxIdem _) (≤MonotoneMax a≤x b≤x)) |
18 changes: 18 additions & 0 deletions
18
Cubical/Relation/Binary/Order/Pseudolattice/Instances/Nat.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,18 @@ | ||
| module Cubical.Relation.Binary.Order.Pseudolattice.Instances.Nat where | ||
|
|
||
| open import Cubical.Foundations.Prelude | ||
|
|
||
| open import Cubical.Data.Nat | ||
| open import Cubical.Data.Nat.Order renaming (_≤_ to _≤ℕ_) | ||
|
|
||
| open import Cubical.Relation.Binary.Order.Poset.Instances.Nat | ||
| open import Cubical.Relation.Binary.Order.Pseudolattice | ||
|
|
||
| ℕ≤Pseudolattice : Pseudolattice ℓ-zero ℓ-zero | ||
| ℕ≤Pseudolattice = makePseudolatticeFromPoset ℕ≤Poset min max | ||
| min-≤-left | ||
| (λ {a b} → min-≤-right {a} {b}) | ||
| minGLB | ||
| left-≤-max | ||
| (λ {a b} → right-≤-max {b} {a}) | ||
| maxLUB |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
likewise for this lemma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with making it public in
Data.Int.Order, but I'm not sure what would be the best name for it.It's quite a specific lemma only needed for one of the fields in the
IsOrderedCommRingrecord.Also, I noticed that in other modules (for example
Semilattice.Instances.NatMax) the required fields are proved locally when constructing the instance (even for something as basic asmaxAssoc).Other than that, I agree with the other comments and I will apply those changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon closer inspection, this seems to be a special case of cotransitivity of
<ℤwhich would definitely be useful elsewhere and surprisingly does not already seem to be inCubical.Data.Int, so perhaps you could prove it in more generality there?Also, this is just my opinion, but I don't like having private lemmas, and I think
maxAssoc,maxRId, andmaxIdemshould not be in a private where-block in Semilattice.Instances.NatMaxThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion, but I don't see how it follows from cotransitivity, as it seems to have the arguments in the opposite order.
After reasoning a bit about it, I found out that this is actually a consequence of weak linearity of
<ℤ:More in general, this field can be derived from:
isStrictOrderisCommRing+MonoR<The first provides
is-prop-valuedandweak linearity, while the other two allow deriving<-o+and<-o+-cancelfrom+MonoR<using easy algebraic manipulations, such as commutativity of +.I don't think we need to remove the field from the
IsOrderedCommRingrecord, as the defintion is adapted from a standard source: the definition of an Ordered Field from the HoTT Book (Definition 11.2.7), which includes both weak linearity and the condition 0 < x + y ⇒ 0 < x ∨ 0 < y.Still, if required, I have no objection to removing the field itself.
I could add a helper function to avoid filling in this redundant field manually in most cases, while still allowing it to be specified explicitly when convenient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think the helper function would be useful, and it is also how its done for other algebraic structures.
Also, let me explain what I meant by "special case of cotransitivity": Suppose
0 < x + y. Then by cotransitivity we have either0 < xorx < x + y, and in the second case we can subtractxon both sides to get0 < y.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait I feel stupid 😅 What I was calling "cotransitivity" is what you were calling "weak linearity", my bad. I didn't notice they were two different things in
Relation.Binary.BaseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just found a preprint which says that
<on the reals is cotransitive (actually meaning weakly linear under the convention used in the Cubical Agda library), so it might be that the naming is not entirely standard, assuming of course that it's not just an error.Coming back to the helper function, Here it is shown more generally that the
posSum→pos∨posfield can be derived from the others, giving an alternativemakeIsOrderedCommRing', which is then used to build the instance ofIntas OCR.However, after talking with @marcinjangrzybowski, I was thinking it might be preferable to use
lemma0<+, as it is not inductive and therefore faster when deciding which of0 < xor0 < yis satisfied. The weak linearity proof, on the other hand, relies on the trichotomy of<, which is less efficient in the current implementation.If that sounds reasonable, I think this PR could be in good shape to merge as it is. I can add the helper later on, together with some additional properties for
OrderedCommRing, in a follow-up PR.What are the maintainers’ thoughts on this? @felixwellen @mortberg