Skip to content
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

Upper natural numbers #602

Merged
merged 68 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
f505a25
start to port david mayer's work
felixwellen Sep 15, 2021
461fa05
Define addition of upper naturals
felixwellen Sep 21, 2021
db2f573
Define multiplication of upper naturals
felixwellen Sep 21, 2021
7b8339f
introduce helper for paths in subtypes
felixwellen Sep 21, 2021
6b22ccb
Addition on upper naturals commutes
felixwellen Sep 21, 2021
a9af6c3
Refactor and prove more semiring-identites
felixwellen Sep 22, 2021
d3c3885
associative
felixwellen Sep 23, 2021
31b3eb9
refactor
felixwellen Sep 23, 2021
f1bec7f
Define ordered commutative monoids
felixwellen Sep 24, 2021
485ff4f
Move order monoids
felixwellen Sep 28, 2021
97ea885
Examples of ordered monoids, needed for the construction of the upper…
felixwellen Sep 28, 2021
d337eb1
Prove distributivity, restructure
felixwellen Oct 21, 2021
f3cf431
Boilerplate
felixwellen Oct 21, 2021
132b95b
Prove that PropCompletions are Posets
felixwellen Oct 21, 2021
1208477
Show compatability and define ordered comm monoid structure on prop c…
felixwellen Oct 21, 2021
535efe9
Specialize the PropCompletion to an ordered structure, show that boun…
felixwellen Oct 21, 2021
52677e1
get closer to 80-lines
felixwellen Nov 12, 2021
e01aeae
[ ci ] Only htmlize on master branch
ice1000 Jan 5, 2022
f57e686
Merge branch 'master' into upper-nat
felixwellen Jan 5, 2022
1834b03
Clarify comment
felixwellen Jan 5, 2022
7aea899
Define commutative semirings
felixwellen Jan 5, 2022
2e1ab0c
Merge branch 'QAQ' into upper-nat
felixwellen Jan 5, 2022
1fe25e4
Fix defnition
felixwellen Jan 5, 2022
92aaab9
Realize that only bounded upper naturals form a semiring
felixwellen Jan 5, 2022
cf3dec5
Merge branch 'master' into upper-nat
felixwellen Jan 5, 2022
ffd963e
Merge branch 'master' into upper-nat
felixwellen Apr 26, 2022
eccf9bd
explain bounded upper naturals
felixwellen Apr 26, 2022
ca519b2
turn boundedness into proposition
felixwellen Apr 26, 2022
0e0e2ef
WIP: extend everything to bounded upper naturals
felixwellen Apr 29, 2022
491e64a
Merge branch 'master' into upper-nat
felixwellen May 25, 2022
ba6f4cc
finish constructor for submonoids
felixwellen May 25, 2022
65a2f1e
fix renamings
felixwellen May 25, 2022
20e80f8
keep up with naming conventions
felixwellen May 25, 2022
c782378
constructor for ordered submonoids/proof that nothing is to do
felixwellen May 25, 2022
4aa9728
convenience function
felixwellen May 25, 2022
26292c2
fix renamings
felixwellen May 25, 2022
b9e2bab
add export
felixwellen May 25, 2022
e624c6d
define bounded substructure of prop completion
felixwellen May 25, 2022
24b6195
long lines
felixwellen May 25, 2022
3935a09
fix renamings
felixwellen May 25, 2022
1185c8d
start using bounded construction
felixwellen May 25, 2022
bf9bc25
Merge branch 'master' into upper-nat
felixwellen May 25, 2022
5dcc830
Merge branch 'master' into upper-nat
felixwellen May 31, 2022
f37ffe6
set property extractor
felixwellen May 31, 2022
6ec93c2
WIP
felixwellen May 31, 2022
508eb2a
Merge remote-tracking branch 'my-fork/upper-nat' into upper-nat
felixwellen May 31, 2022
bcfc5ef
prove embedding into PropCompletion is monoidal
felixwellen May 31, 2022
b0511f7
correct direction of leq in PropCompletion
felixwellen May 31, 2022
ad4e7c8
complete: UpperNat is a Semiring
felixwellen May 31, 2022
b1b7a89
move upper naturals
felixwellen Jun 1, 2022
ba01e41
remove obsolete
felixwellen Jun 1, 2022
08fdcc1
Merge branch 'master' into upper-nat
felixwellen Jun 1, 2022
db795f0
remove obsolete
felixwellen Jun 2, 2022
77f2a43
Revert "remove obsolete"
felixwellen Jun 2, 2022
f975911
anders' requests
felixwellen Jun 2, 2022
9052fcf
naming
felixwellen Jun 2, 2022
5251faf
anders' requests
felixwellen Jun 2, 2022
e713117
anders' requests
felixwellen Jun 2, 2022
48029eb
anders' requests
felixwellen Jun 2, 2022
8170484
anders' requests
felixwellen Jun 2, 2022
42d8f83
more copatterns...
felixwellen Jun 2, 2022
f400dfb
oops
felixwellen Jun 2, 2022
ed64cbc
typo
felixwellen Jun 2, 2022
283b8b8
more copatterns
felixwellen Jun 2, 2022
123dbeb
do not open public...
felixwellen Jun 2, 2022
5367c5f
Merge branch 'master' into upper-nat
felixwellen Jun 8, 2022
37f6f6f
remove public, use proper level names
felixwellen Jun 8, 2022
218a033
remove remaining 'public'
felixwellen Jun 8, 2022
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
12 changes: 12 additions & 0 deletions Cubical/Algebra/CommMonoid/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ CommMonoidStr→MonoidStr (commmonoidstr _ _ H) = monoidstr _ _ (IsCommMonoid.is
CommMonoid→Monoid : CommMonoid ℓ → Monoid ℓ
CommMonoid→Monoid (_ , commmonoidstr _ _ M) = _ , monoidstr _ _ (IsCommMonoid.isMonoid M)

isSetFromIsCommMonoid :
{M : Type ℓ} {ε : M} {_·_ : M → M → M}
(isCommMonoid : IsCommMonoid ε _·_)
→ isSet M
isSetFromIsCommMonoid isCommMonoid =
isSetFromIsMonoid (MonoidStr.isMonoid (CommMonoidStr→MonoidStr (commmonoidstr _ _ isCommMonoid)))
mortberg marked this conversation as resolved.
Show resolved Hide resolved

isSetCommMonoid : (M : CommMonoid ℓ) → isSet ⟨ M ⟩
isSetCommMonoid M =
let open CommMonoidStr (snd M)
in isSetFromIsCommMonoid isCommMonoid

CommMonoidHom : (L : CommMonoid ℓ) (M : CommMonoid ℓ') → Type (ℓ-max ℓ ℓ')
CommMonoidHom L M = MonoidHom (CommMonoid→Monoid L) (CommMonoid→Monoid M)

Expand Down
28 changes: 27 additions & 1 deletion Cubical/Algebra/CommMonoid/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,33 @@ open import Cubical.Algebra.CommMonoid.Base

private
variable
ℓ : Level
ℓ ℓ' : Level

module _
(M : CommMonoid ℓ)
(P : ⟨ M ⟩ → hProp ℓ')
where
open CommMonoidStr (snd M)
module _
(·Closed : (x y : ⟨ M ⟩) → ⟨ P x ⟩ → ⟨ P y ⟩ → ⟨ P (x · y) ⟩)
(εContained : ⟨ P ε ⟩)
where
private
subtype = Σ[ x ∈ ⟨ M ⟩ ] ⟨ P x ⟩

makeSubCommMonoid : CommMonoid _
fst makeSubCommMonoid = subtype
CommMonoidStr.ε (snd makeSubCommMonoid) = ε , εContained
CommMonoidStr._·_ (snd makeSubCommMonoid) (x , xContained) (y , yContained) =
(x · y) , ·Closed x y xContained yContained
IsCommMonoid.isMonoid (CommMonoidStr.isCommMonoid (snd makeSubCommMonoid)) =
makeIsMonoid
(isOfHLevelΣ 2 (isSetFromIsCommMonoid isCommMonoid) λ _ → isProp→isSet (snd (P _)))
(λ x y z → Σ≡Prop (λ _ → snd (P _)) (assoc (fst x) (fst y) (fst z)))
(λ x → Σ≡Prop (λ _ → snd (P _)) (rid (fst x)))
λ x → Σ≡Prop (λ _ → snd (P _)) (lid (fst x))
IsCommMonoid.comm (CommMonoidStr.isCommMonoid (snd makeSubCommMonoid)) =
λ x y → Σ≡Prop (λ _ → snd (P _)) (comm (fst x) (fst y))

module CommMonoidTheory (M' : CommMonoid ℓ) where
open CommMonoidStr (snd M')
Expand Down
4 changes: 4 additions & 0 deletions Cubical/Algebra/CommSemiring.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{-# OPTIONS --safe #-}
module Cubical.Algebra.CommSemiring where

open import Cubical.Algebra.CommSemiring.Base public
60 changes: 60 additions & 0 deletions Cubical/Algebra/CommSemiring/Base.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{-# OPTIONS --safe #-}
module Cubical.Algebra.CommSemiring.Base where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.SIP
felixwellen marked this conversation as resolved.
Show resolved Hide resolved

open import Cubical.Algebra.CommMonoid
open import Cubical.Algebra.Monoid

private
variable
ℓ ℓ' : Level

record IsCommSemiring {R : Type ℓ}
(0r 1r : R) (_+_ _·_ : R → R → R) : Type ℓ where

field
+IsCommMonoid : IsCommMonoid 0r _+_
·IsCommMonoid : IsCommMonoid 1r _·_
·LDist+ : (x y z : R) → x · (y + z) ≡ (x · y) + (x · z)
0LAnnihil : (x : R) → 0r · x ≡ 0r
felixwellen marked this conversation as resolved.
Show resolved Hide resolved

open IsCommMonoid +IsCommMonoid public
renaming
( assoc to +Assoc
; identity to +Identity
; lid to +Lid
; rid to +Rid
; comm to +Comm
; isSemigroup to +IsSemigroup
; isMonoid to +IsMonoid)

open IsCommMonoid ·IsCommMonoid public
renaming
( assoc to ·Assoc
; identity to ·Identity
; lid to ·Lid
; rid to ·Rid
; comm to ·Comm
; isSemigroup to ·IsSemigroup
; isMonoid to ·IsMonoid)
hiding
( is-set ) -- We only want to export one proof of this
mortberg marked this conversation as resolved.
Show resolved Hide resolved

record CommSemiringStr (A : Type ℓ) : Type (ℓ-suc ℓ) where

field
0r : A
1r : A
_+_ : A → A → A
_·_ : A → A → A
isCommSemiring : IsCommSemiring 0r 1r _+_ _·_

infixl 7 _·_
infixl 6 _+_

open IsCommSemiring isCommSemiring public

CommSemiring : ∀ ℓ → Type (ℓ-suc ℓ)
CommSemiring ℓ = TypeWithStr ℓ CommSemiringStr
205 changes: 205 additions & 0 deletions Cubical/Algebra/CommSemiring/Instances/UpperNat.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{-# OPTIONS --safe #-}
module Cubical.Algebra.CommSemiring.Instances.UpperNat where
{-
based on:
https://github.com/DavidJaz/Cohesion/blob/master/UpperNaturals.agda

and the slides here (for arithmetic operation):
https://felix-cherubini.de/myers-slides-II.pdf
-}

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.HLevels

open import Cubical.Functions.Logic
open import Cubical.Functions.Embedding

open import Cubical.Algebra.CommMonoid
open import Cubical.Algebra.OrderedCommMonoid
open import Cubical.Algebra.OrderedCommMonoid.PropCompletion
open import Cubical.Algebra.OrderedCommMonoid.Instances
open import Cubical.Algebra.CommSemiring

open import Cubical.Data.Nat using (ℕ; ·-distribˡ)
open import Cubical.Data.Nat.Order
open import Cubical.Data.Empty hiding (⊥)
open import Cubical.Data.Sigma

open import Cubical.HITs.Truncation
open import Cubical.HITs.PropositionalTruncation renaming (rec to propTruncRec)

private
variable
ℓ : Level

{- The Upper Naturals
An upper natural is an upward closed proposition on natural numbers.
The interpretation is that an upper natural is a natural ``defined by its upper bounds'', in the
sense that for the proposition N holding of a natural n means that n is an upper bound of N.
The important bit about upper naturals is that they satisfy the well-ordering principle,
constructively (no proof of that is given here).

Example Application:
The degree of a polynomial P=∑_{i=0}^{n} aᵢ · Xⁱ may be defined as an upper natural:

deg(P) :≡ (λ n → all non-zero indices have index smaller n)

Or: deg(∑_{i=0}^{n} aᵢ · Xⁱ) = λ (k : ℕ) → ∀ (k+1 ≤ i ≤ n) aᵢ≡0

This works even if a constructive definition of polynomial is used.

However the upper naturals are a bit too unconstraint and do not even form a semiring,
since they include 'infinity' elements like the proposition that is always false.

This is different for the subtype of *bounded* upper naturals ℕ↑b.
-}

module ConstructionUnbounded where
ℕ↑-+ = PropCompletion ℕ≤+
ℕ↑-· = PropCompletion ℕ≤·

open OrderedCommMonoidStr (snd ℕ↑-+)
using ()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This one is actuall (still) needed - do you think it is better to use hiding and list the things that would lead to a name clash?

renaming (assoc to +Assoc; comm to +Comm; rid to +Rid;
_·_ to _+_; ε to 0↑)

open OrderedCommMonoidStr (snd ℕ≤·)
using (MonotoneL; MonotoneR)

open OrderedCommMonoidStr (snd ℕ≤+)
using ()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should I also hide things instead?

renaming (_·_ to _+ℕ_;
MonotoneL to +MonotoneL; MonotoneR to +MonotoneR;
comm to ℕ+Comm)

open OrderedCommMonoidStr ⦃...⦄
using (_·_)
renaming (assoc to ·Assoc; comm to ·Comm; rid to ·Rid)

private
instance
_ : OrderedCommMonoidStr _ _
_ = snd ℕ↑-·
_ : OrderedCommMonoidStr _ _
_ = snd ℕ≤·

ℕ↑ : Type₁
ℕ↑ = fst ℕ↑-+

open PropCompletion ℓ-zero ℕ≤+
using (typeAt; pathFromImplications)

open <-Reasoning using (_≤⟨_⟩_)

+LDist· : (x y z : ℕ↑) →
x · (y + z) ≡ (x · y) + (x · z)
+LDist· x y z =
pathFromImplications
(x · (y + z)) ((x · y) + (x · z))
(⇒) ⇐
where
⇒ : (n : ℕ) → typeAt n (x · (y + z)) → typeAt n ((x · y) + (x · z))
⇒ n = propTruncRec
mortberg marked this conversation as resolved.
Show resolved Hide resolved
isPropPropTrunc
λ {((a , b) , xa , (y+zb , a·b≤n)) →
propTruncRec
isPropPropTrunc
(λ {((a' , b') , ya' , (zb' , a'+b'≤b))
→ ∣ ((a · a') , (a · b')) ,
∣ (a , a') , (xa , (ya' , ≤-refl)) ∣₁ ,
(∣ (a , b') , (xa , (zb' , ≤-refl)) ∣₁ ,
subst (_≤ n) (sym (·-distribˡ a a' b'))
(≤-trans (MonotoneL {z = a} a'+b'≤b) a·b≤n)) ∣₁ })
y+zb}
⇐ : (n : ℕ) → _
⇐ n =
propTruncRec
mortberg marked this conversation as resolved.
Show resolved Hide resolved
isPropPropTrunc
λ {((a , b) , x·ya , (x·zb , a+b≤n))
→ propTruncRec
isPropPropTrunc
(λ {((a' , b') , a'x , (b'y , a'·b'≤a))
→ propTruncRec
isPropPropTrunc
(λ {((a″ , b″) , a″x , (zb″ , a″·b″≤b))
→ ∣ ≤CaseInduction {n = a'} {m = a″}
(λ a'≤a″ →
(a' , (b' +ℕ b″)) , a'x ,

(∣ (b' , b″) , (b'y , (zb″ , ≤-refl)) ∣₁ ,
(a' · (b' +ℕ b″) ≤⟨ subst
(_≤ (a' · b') +ℕ (a' · b″))
(·-distribˡ a' b' b″) ≤-refl ⟩
(a' · b') +ℕ (a' · b″) ≤⟨ +MonotoneR a'·b'≤a ⟩
a +ℕ (a' · b″) ≤⟨ +MonotoneL
(≤-trans (MonotoneR a'≤a″) a″·b″≤b) ⟩
a+b≤n ))
)
(λ a″≤a' →
(a″ , (b' +ℕ b″)) , (a″x ,
(∣ (b' , b″) , (b'y , (zb″ , ≤-refl)) ∣₁ ,
((a″ · (b' +ℕ b″)) ≤⟨ subst
(_≤ (a″ · b') +ℕ (a″ · b″))
(·-distribˡ a″ b' b″) ≤-refl ⟩
(a″ · b') +ℕ (a″ · b″) ≤⟨ +MonotoneR
((a″ · b') ≤⟨ MonotoneR a″≤a' ⟩ a'·b'≤a) ⟩
a +ℕ (a″ · b″) ≤⟨ +MonotoneL a″·b″≤b ⟩
a+b≤n)))
)
∣₁})
x·zb})
x·ya}


module ConstructionBounded where
ℕ↑-+b = BoundedPropCompletion ℕ≤+
ℕ↑-·b = BoundedPropCompletion ℕ≤·

open OrderedCommMonoidStr (snd ℕ≤+)
using ()
felixwellen marked this conversation as resolved.
Show resolved Hide resolved
renaming (_·_ to _+ℕ_;
MonotoneL to +MonotoneL; MonotoneR to +MonotoneR; rid to +Rid;
comm to ℕ+Comm)
open OrderedCommMonoidStr (snd ℕ↑-+b)
using ()
felixwellen marked this conversation as resolved.
Show resolved Hide resolved
renaming (assoc to +Assoc; comm to +Comm;
_·_ to _+_; ε to 0↑)

open OrderedCommMonoidStr (snd ℕ↑-·b)
using (_·_)

open PropCompletion ℓ-zero ℕ≤+
using (typeAt; pathFromImplications)

ℕ↑b : Type₁
ℕ↑b = fst ℕ↑-+b

0LAnnihil : (x : ℕ↑b) → 0↑ · x ≡ 0↑
0LAnnihil x =
Σ≡Prop (λ s → PropCompletion.isPropIsBounded ℓ-zero ℕ≤+ s)
(pathFromImplications (fst (0↑ · x)) (fst 0↑) (⇒) ⇐)
where
⇒ : (n : ℕ) → typeAt n (fst (0↑ · x)) → typeAt n (fst 0↑)
⇒ n _ = n , ℕ+Comm n 0
⇐ : (n : ℕ) → typeAt n (fst 0↑) → typeAt n (fst (0↑ · x))
⇐ n _ =
propTruncRec
isPropPropTrunc
(λ {(m , mIsUpperBound) → ∣ (0 , m) , ((0 , refl) , (mIsUpperBound , n , +Rid n)) ∣₁})
(snd x)


asCommSemiring : CommSemiring (ℓ-suc ℓ-zero)
fst asCommSemiring = ℕ↑b
CommSemiringStr.0r (snd asCommSemiring) = 0↑
CommSemiringStr.1r (snd asCommSemiring) = OrderedCommMonoidStr.ε (snd ℕ↑-·b)
CommSemiringStr._+_ (snd asCommSemiring) = _+_
CommSemiringStr._·_ (snd asCommSemiring) = _·_
IsCommSemiring.+IsCommMonoid (CommSemiringStr.isCommSemiring (snd asCommSemiring)) =
OrderedCommMonoidStr.isCommMonoid (snd ℕ↑-+b)
IsCommSemiring.·IsCommMonoid (CommSemiringStr.isCommSemiring (snd asCommSemiring)) =
OrderedCommMonoidStr.isCommMonoid (snd ℕ↑-·b)
IsCommSemiring.·LDist+ (CommSemiringStr.isCommSemiring (snd asCommSemiring)) =
λ x y z → Σ≡Prop (λ s → PropCompletion.isPropIsBounded ℓ-zero ℕ≤+ s)
(ConstructionUnbounded.+LDist· (fst x) (fst y) (fst z))
IsCommSemiring.0LAnnihil (CommSemiringStr.isCommSemiring (snd asCommSemiring)) = 0LAnnihil
7 changes: 6 additions & 1 deletion Cubical/Algebra/Monoid/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ record IsMonoidHom {A : Type ℓ} {B : Type ℓ'}
MonoidHom : (L : Monoid ℓ) (M : Monoid ℓ') → Type (ℓ-max ℓ ℓ')
MonoidHom L M = Σ[ f ∈ (⟨ L ⟩ → ⟨ M ⟩) ] IsMonoidHom (L .snd) f (M .snd)

isSetFromIsMonoid :
{M : Type ℓ} {ε : M} {_·_ : M → M → M}
(isMonoid : IsMonoid ε _·_)
→ isSet M
isSetFromIsMonoid isMonoid = IsSemigroup.is-set (IsMonoid.isSemigroup isMonoid)
felixwellen marked this conversation as resolved.
Show resolved Hide resolved

IsMonoidEquiv : {A : Type ℓ} {B : Type ℓ'} (M : MonoidStr A) (e : A ≃ B) (N : MonoidStr B)
→ Type (ℓ-max ℓ ℓ')
IsMonoidEquiv M e N = IsMonoidHom M (e .fst) N
Expand Down Expand Up @@ -148,4 +154,3 @@ module MonoidTheory {ℓ} (M : Monoid ℓ) where
(y · x) · z ≡⟨ cong (λ - → - · z) left-inverse ⟩
ε · z ≡⟨ lid z ⟩
z ∎

5 changes: 5 additions & 0 deletions Cubical/Algebra/OrderedCommMonoid.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# OPTIONS --safe #-}
module Cubical.Algebra.OrderedCommMonoid where

open import Cubical.Algebra.OrderedCommMonoid.Base public
open import Cubical.Algebra.OrderedCommMonoid.Properties public
Loading