Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6fee884
add fast implementation of integers and their properties
hexwell Jul 23, 2025
78890fa
move properties of min and max over `ℕ` to the appropriate file
LorenzoMolena Jul 25, 2025
ece58b1
reorganize and format code
LorenzoMolena Jul 30, 2025
62ae02c
Merge branch 'agda:master' into fast-int
hexwell Aug 4, 2025
e89d311
remove `--safe` flag
LorenzoMolena Aug 4, 2025
031af52
remove `--safe` flag
LorenzoMolena Aug 4, 2025
56810c7
Fast int (#2)
LorenzoMolena Aug 7, 2025
ead0553
add fast implementation of integers and their properties
hexwell Jul 23, 2025
99aeb91
move properties of min and max over `ℕ` to the appropriate file
LorenzoMolena Jul 25, 2025
7ddc9bd
reorganize and format code
LorenzoMolena Jul 30, 2025
80cf10b
remove `--safe` flag
LorenzoMolena Aug 4, 2025
af4df40
remove `--safe` flag
LorenzoMolena Aug 4, 2025
4954b30
remove `--safe` flag
LorenzoMolena Aug 4, 2025
ab8bec0
Fast int (#2)
LorenzoMolena Aug 7, 2025
08be7df
Merge branch 'fast-int' of https://github.com/LorenzoMolena/cubical i…
LorenzoMolena Oct 8, 2025
20c3b5f
fix compatibility with the library
LorenzoMolena Oct 8, 2025
1e37974
Merge branch 'agda:master' into fast-int
LorenzoMolena Oct 17, 2025
931075b
fix compatibility after rebase, start proving properties of fast inte…
LorenzoMolena Oct 21, 2025
2b2d23a
Merge branch 'agda:master' into fast-int
LorenzoMolena Oct 23, 2025
bc0973f
Merge branch 'agda:master' into fast-int
LorenzoMolena Oct 24, 2025
89246b9
add instances of `Nat` and `Int` as `Pseudolattice`, and of `Int` as …
LorenzoMolena Oct 24, 2025
0f6037e
fix typo, add better make from Posets to Pseudolattices
LorenzoMolena Oct 24, 2025
4a415d5
update `makePseudolatticeFromPoset` and instances built using it
LorenzoMolena Oct 25, 2025
561308e
move `maxLUB` and `minGLB` to `Data.Nat.Order`, remove unnecessary im…
LorenzoMolena Oct 25, 2025
44cfa14
rewrite properties from `minAssoc` to `-max`, add temporary lemmas in…
LorenzoMolena Oct 26, 2025
d63b50c
fix naming clash on import
LorenzoMolena Oct 26, 2025
8b60736
rewrite properties from `pos+posLposMin` to `·DistNegsucLMax`
LorenzoMolena Oct 29, 2025
50a6d3a
start work on Order: adapt properties from `isProp≤` to `≤SumLeftPos`…
LorenzoMolena Oct 30, 2025
c7cfd31
use more efficient implementation of quotient and remainder in `Int.F…
LorenzoMolena Oct 30, 2025
e2ac053
Merge pull request #20 from LorenzoMolena/fast-int
LorenzoMolena Oct 30, 2025
d14b373
adapt properties from `pred-≤-pred` to `0≤o→≤-·o`, move import of `Bo…
LorenzoMolena Oct 30, 2025
be459af
adapt properties from `<-·o` to `predℤ-≤-predℤ`
LorenzoMolena Oct 31, 2025
9f3d854
adapt properties from `¬m+posk<m` to `<-+pos-trans`
LorenzoMolena Oct 31, 2025
bd8a8f5
adapt properties from `<-pos+-trans` to `0<o→≤-·o-cancel`
LorenzoMolena Oct 31, 2025
6ed800d
adapt properties from `≤-o·-cancel` to `≤→min` ; add conversions betw…
LorenzoMolena Oct 31, 2025
23143d3
adapt properties from `≤MonotoneMin` to `_≟'_` ; add faster `_≟_` and…
LorenzoMolena Oct 31, 2025
ed85e07
edit `_≤'_` using recursive order, add `_≤ᵇ_` and `_≤''_`
LorenzoMolena Nov 5, 2025
42c0212
update `minIdem`
LorenzoMolena Nov 8, 2025
2484f5f
clean comments, reorganize `Fast` modules, add more properties in `In…
LorenzoMolena Nov 8, 2025
47725ea
Merge branch 'fast-int-with-pl-and-ocr-instances' into ocr+pl-instances
LorenzoMolena Nov 13, 2025
9b39beb
Revert "Merge branch 'fast-int-with-pl-and-ocr-instances' into ocr+pl…
LorenzoMolena Nov 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cubical/Algebra/OrderedCommRing/Base.agda
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Cubical.Algebra.OrderedCommRing.Base where
{-
Definition of an commutative ordered ring.
Definition of an ordered commutative ring.
-}

open import Cubical.Foundations.Prelude
Expand Down
83 changes: 83 additions & 0 deletions Cubical/Algebra/OrderedCommRing/Instances/Int.agda
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise for this lemma

Copy link
Contributor Author

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 IsOrderedCommRing record.
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 as maxAssoc).

Other than that, I agree with the other comments and I will apply those changes.

Copy link
Contributor

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 in Cubical.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, and maxIdem should not be in a private where-block in Semilattice.Instances.NatMax

Copy link
Contributor Author

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 <ℤ:

isOrderedCommRingℤ : IsOrderedCommRing 0 1 _+ℤ_ _·ℤ_ -ℤ_ _<ℤ_ _≤ℤ_
    [...]
    isOrderedCommRingℤ .posSum→pos∨pos  = λ x y 0<x+y →
      subst ((0 <ℤ x) L.⊔′_)
        (hPropExt (isProp< {n = x +ℤ y}) (isProp< {0})
                  (<-o+-cancel {m = 0})  (<-o+ {m = 0}))
        (Z<SO.is-weakly-linear 0 (x +ℤ y) x 0<x+y)
      where open module Z<SO = StrictOrderStr (ℤ<StrictOrder .snd)
    [...]

More in general, this field can be derived from:

  • isStrictOrder
  • isCommRing
  • +MonoR<

The first provides is-prop-valued and weak linearity, while the other two allow deriving <-o+ and <-o+-cancel from +MonoR< using easy algebraic manipulations, such as commutativity of +.

I don't think we need to remove the field from the IsOrderedCommRing record, 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.

Copy link
Contributor

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 either 0 < x or x < x + y, and in the second case we can subtract x on both sides to get 0 < y.

Copy link
Contributor

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.Base

Copy link
Contributor Author

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∨pos field can be derived from the others, giving an alternative makeIsOrderedCommRing', which is then used to build the instance of Int as 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 of 0 < x or 0 < y is 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

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≤
14 changes: 14 additions & 0 deletions Cubical/Data/Nat/Order.agda
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ min-≤-right {zero} {n} = zero-≤
min-≤-right {suc m} {zero} = ≤-refl
min-≤-right {suc m} {suc n} = subst (_≤ _) (sym minSuc) $ suc-≤-suc $ min-≤-right {m} {n}

maxLUB : {x} m ≤ x n ≤ x max m n ≤ x
maxLUB {zero} {n} _ n≤x = n≤x
maxLUB {suc m} {zero} sm≤x _ = sm≤x
maxLUB {suc m} {suc n} sm≤x sn≤x with m <ᵇ n
... | false = sm≤x
... | true = sn≤x

minGLB : {x} x ≤ m x ≤ n x ≤ min m n
minGLB {zero} {n} x≤0 _ = x≤0
minGLB {suc m} {zero} _ x≤0 = x≤0
minGLB {suc m} {suc n} x≤sm x≤sn with m <ᵇ n
... | false = x≤sn
... | true = x≤sm

-- Boolean order relations and their conversions to/from ≤ and <

_≤ᵇ_ : Bool
Expand Down
33 changes: 33 additions & 0 deletions Cubical/Relation/Binary/Order/Pseudolattice/Base.agda
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Cubical.Relation.Binary.Order.Pseudolattice.Base where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.Function
open import Cubical.Foundations.Equiv
open import Cubical.Foundations.HLevels
open import Cubical.Foundations.SIP

open import Cubical.Relation.Binary.Base
Expand Down Expand Up @@ -57,3 +60,33 @@ makeIsPseudolattice {_≤_ = _≤_} is-setL is-prop-valued is-refl is-trans is-a
PS : IsPseudolattice _≤_
PS .IsPseudolattice.isPoset = isposet is-setL is-prop-valued is-refl is-trans is-antisym
PS .IsPseudolattice.isPseudolattice = is-meet-semipseudolattice , is-join-semipseudolattice

module _
(P : Poset ℓ ℓ') (_∧_ _∨_ : ⟨ P ⟩ → ⟨ P ⟩ → ⟨ P ⟩) where
open PosetStr (str P) renaming (_≤_ to infix 8 _≤_)
module _
(π₁ : ∀ {a b} → a ∧ b ≤ a)
(π₂ : ∀ {a b} → a ∧ b ≤ b)
(ϕ : ∀ {a b x} → x ≤ a → x ≤ b → x ≤ a ∧ b)
(ι₁ : ∀ {a b} → a ≤ a ∨ b)
(ι₂ : ∀ {a b} → b ≤ a ∨ b)
(ψ : ∀ {a b x} → a ≤ x → b ≤ x → a ∨ b ≤ x) where

makePseudolatticeFromPoset : Pseudolattice ℓ ℓ'
makePseudolatticeFromPoset .fst = ⟨ P ⟩
makePseudolatticeFromPoset .snd .PseudolatticeStr._≤_ = (str P) .PosetStr._≤_
makePseudolatticeFromPoset .snd .PseudolatticeStr.is-pseudolattice = isPL where
isPL : IsPseudolattice _≤_
isPL .IsPseudolattice.isPoset = isPoset
isPL .IsPseudolattice.isPseudolattice .fst a b .fst = a ∧ b
isPL .IsPseudolattice.isPseudolattice .fst a b .snd x = propBiimpl→Equiv
(is-prop-valued _ _)
(isProp× (is-prop-valued _ _) (is-prop-valued _ _))
(λ x≤a∧b → is-trans _ _ _ x≤a∧b π₁ , is-trans _ _ _ x≤a∧b π₂)
(uncurry ϕ)
isPL .IsPseudolattice.isPseudolattice .snd a b .fst = a ∨ b
isPL .IsPseudolattice.isPseudolattice .snd a b .snd x = propBiimpl→Equiv
(is-prop-valued _ _)
(isProp× (is-prop-valued _ _) (is-prop-valued _ _))
(λ a∨b≤x → is-trans _ _ _ ι₁ a∨b≤x , is-trans _ _ _ ι₂ a∨b≤x)
(uncurry ψ)
18 changes: 18 additions & 0 deletions Cubical/Relation/Binary/Order/Pseudolattice/Instances/Int.agda
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 Cubical/Relation/Binary/Order/Pseudolattice/Instances/Nat.agda
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