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

[ refactor ] (Re)define (Is)TightApartness and (Is)HeytingCommutativeRing/(Is)HeytingField #2588

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
63 changes: 62 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Bug-fixes
---------

* In `Algebra.Apartness.Structures`, renamed `sym` from `IsApartnessRelation`
to `#-sym` in order to avoid overloaded projection.
to `#-sym` in order to avoid overloaded projection. The field names
`irrefl` and `cotrans` are similarly renamed for the sake of consistency.

Non-backwards compatible changes
Expand All @@ -21,6 +21,20 @@ Non-backwards compatible changes
significantly faster. However, its reduction behaviour on open terms may have
changed.

* The definitions of `Algebra.Structures.IsHeyting*` and
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is a draft, but in the final version it would be good to explain broadly what the refactorings actually are here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See latest commits, hopefully enough, but not too much, detail now!

`Algebra.Structures.IsHeyting*` have been refactored, together
with that of `Relation.Binary.Definitions.Tight` on which they depend.
Specifically:
- `Tight _≈_ _#_` has been redefined as `∀ x y → ¬ x # y → x ≈ y`,
dropping the redundant second conjunct, because it is equivalent to
`Irreflexive _≈_ _#_`.
- new definitions: `(Is)TightApartnessRelation` structure/bundle, exploiting
the above redefinition.
- the definition of `HeytingCommutativeRing` now drops the properties of
invertibility, in favour of moving them to `HeytingField`.
- both `Heyting*` algebraic structure/bundles have been redefined to base
off an underlying `TightApartnessRelation`.

Minor improvements
------------------

Expand All @@ -30,6 +44,12 @@ Deprecated modules
Deprecated names
----------------

* In `Algebra.Apartness.Properties.HeytingCommutativeRing`:
```agda
x-0≈x ↦ Algebra.Properties.Ring.x-0#≈x
#-sym ↦ Algebra.Apartness.Structures.IsHeytingCommutativeRing.#-sym
```

* In `Algebra.Definitions.RawMagma`:
```agda
_∣∣_ ↦ _∥_
Expand Down Expand Up @@ -95,13 +115,26 @@ Deprecated names
New modules
-----------

* `Algebra.Apartness.Properties.HeytingField`, refactoring the existing
`Algebra.Apartness.Properties.HeytingCommutativeRing`.

* `Data.List.Base.{and|or|any|all}` have been lifted out into `Data.Bool.ListAction`.

* `Data.List.Base.{sum|product}` and their properties have been lifted out into `Data.Nat.ListAction` and `Data.Nat.ListAction.Properties`.

Additions to existing modules
-----------------------------

* In `Algebra.Apartness.Bundles.HeytingCommutativeRing`:
```agda
TightApartnessRelation c ℓ₁ ℓ₂ : Set _
```

* In `Algebra.Apartness.Structures.IsHeytingCommutativeRing`:
```agda
IsTightApartnessRelation _≈_ _#_ : Set _
```

* In `Algebra.Construct.Pointwise`:
```agda
isNearSemiring : IsNearSemiring _≈_ _+_ _*_ 0# →
Expand Down Expand Up @@ -131,6 +164,16 @@ Additions to existing modules
commutativeRing : CommutativeRing c ℓ → CommutativeRing (a ⊔ c) (a ⊔ ℓ)
```

* In `Algebra.Properties.AbelianGroup`:
```agda
x-ε≈x : RightIdentity ε _-_
```

* In `Algebra.Properties.RingWithoutOne`:
```agda
x-0#≈x : RightIdentity 0# _-_
```

* In `Data.List.Properties`:
```agda
map-applyUpTo : ∀ (f : ℕ → A) (g : A → B) n → map g (applyUpTo f n) ≡ applyUpTo (g ∘ f) n
Expand All @@ -143,3 +186,21 @@ Additions to existing modules
```agda
filter-↭ : ∀ (P? : Pred.Decidable P) → xs ↭ ys → filter P? xs ↭ filter P? ys
```

* In `Data.Rational.Unnormalised.Properties`:
```agda
≄-apartnessRelation : ApartnessRelation _ _ _
≄-isTightApartnessRelation : IsTightApartnessRelation _≃_ _≄_
≄-tightApartnessRelation : TightApartnessRelation _ _ _
```

* In `Relation.Binary.Properties.DecSetoid`:
```agda
≉-isTightApartnessRelation : IsTightApartnessRelation _≈_ _#_
≉-tightApartnessRelation : TightApartnessRelation _ _ _
```
Additionally,
```agda
≉-tight : Tight _≈_ _≉_
```
has been redefined to reflect the change in the definition of `Tight`.
13 changes: 8 additions & 5 deletions src/Algebra/Apartness/Bundles.agda
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Algebra.Apartness.Bundles where

open import Level using (_⊔_; suc)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.Bundles using (ApartnessRelation)
open import Relation.Binary.Bundles using (TightApartnessRelation)
open import Algebra.Core using (Op₁; Op₂)
open import Algebra.Bundles using (CommutativeRing)
open import Algebra.Apartness.Structures
Expand All @@ -36,8 +36,11 @@ record HeytingCommutativeRing c ℓ₁ ℓ₂ : Set (suc (c ⊔ ℓ₁ ⊔ ℓ
commutativeRing : CommutativeRing c ℓ₁
commutativeRing = record { isCommutativeRing = isCommutativeRing }

apartnessRelation : ApartnessRelation c ℓ₁ ℓ₂
apartnessRelation = record { isApartnessRelation = isApartnessRelation }
tightApartnessRelation : TightApartnessRelation c ℓ₁ ℓ₂
tightApartnessRelation = record { isTightApartnessRelation = isTightApartnessRelation }

open TightApartnessRelation tightApartnessRelation public
using (apartnessRelation)


record HeytingField c ℓ₁ ℓ₂ : Set (suc (c ⊔ ℓ₁ ⊔ ℓ₂)) where
Expand All @@ -61,5 +64,5 @@ record HeytingField c ℓ₁ ℓ₂ : Set (suc (c ⊔ ℓ₁ ⊔ ℓ₂)) where
heytingCommutativeRing : HeytingCommutativeRing c ℓ₁ ℓ₂
heytingCommutativeRing = record { isHeytingCommutativeRing = isHeytingCommutativeRing }

apartnessRelation : ApartnessRelation c ℓ₁ ℓ₂
apartnessRelation = record { isApartnessRelation = isApartnessRelation }
open HeytingCommutativeRing heytingCommutativeRing public
using (commutativeRing; tightApartnessRelation; apartnessRelation)
91 changes: 23 additions & 68 deletions src/Algebra/Apartness/Properties/HeytingCommutativeRing.agda
Original file line number Diff line number Diff line change
Expand Up @@ -11,74 +11,29 @@ open import Algebra.Apartness.Bundles using (HeytingCommutativeRing)
module Algebra.Apartness.Properties.HeytingCommutativeRing
{c ℓ₁ ℓ₂} (HCR : HeytingCommutativeRing c ℓ₁ ℓ₂) where

open import Function.Base using (_∘_)
open import Data.Product.Base using (_,_; proj₁; proj₂)
open import Algebra using (CommutativeRing; RightIdentity; Invertible; LeftInvertible; RightInvertible)
open import Algebra.Bundles using (CommutativeRing)

open HeytingCommutativeRing HCR
open CommutativeRing commutativeRing using (ring; *-commutativeMonoid)
open HeytingCommutativeRing HCR using (commutativeRing)
open CommutativeRing commutativeRing using (ring)
open import Algebra.Properties.Ring ring using (x-0#≈x)

open import Algebra.Properties.Ring ring
using (-0#≈0#; -‿distribˡ-*; -‿distribʳ-*; -‿anti-homo-+; -‿involutive)
open import Relation.Binary.Definitions using (Symmetric)
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
open import Algebra.Properties.CommutativeMonoid *-commutativeMonoid

private variable
x y z : Carrier

invertibleˡ⇒# : LeftInvertible _≈_ 1# _*_ (x - y) → x # y
invertibleˡ⇒# = invertible⇒# ∘ invertibleˡ⇒invertible

invertibleʳ⇒# : RightInvertible _≈_ 1# _*_ (x - y) → x # y
invertibleʳ⇒# = invertible⇒# ∘ invertibleʳ⇒invertible

x-0≈x : RightIdentity _≈_ 0# _-_
x-0≈x x = trans (+-congˡ -0#≈0#) (+-identityʳ x)

1#0 : 1# # 0#
1#0 = invertibleˡ⇒# (1# , 1*[x-0]≈x)
where
1*[x-0]≈x : 1# * (x - 0#) ≈ x
1*[x-0]≈x {x} = trans (*-identityˡ (x - 0#)) (x-0≈x x)

x#0y#0→xy#0 : x # 0# → y # 0# → x * y # 0#
x#0y#0→xy#0 {x} {y} x#0 y#0 = helper (#⇒invertible x#0) (#⇒invertible y#0)
where

helper : Invertible _≈_ 1# _*_ (x - 0#) → Invertible _≈_ 1# _*_ (y - 0#) → x * y # 0#
helper (x⁻¹ , x⁻¹*x≈1 , x*x⁻¹≈1) (y⁻¹ , y⁻¹*y≈1 , y*y⁻¹≈1)
= invertibleˡ⇒# (y⁻¹ * x⁻¹ , y⁻¹*x⁻¹*x*y≈1)
where
open ≈-Reasoning setoid

y⁻¹*x⁻¹*x*y≈1 : y⁻¹ * x⁻¹ * (x * y - 0#) ≈ 1#
y⁻¹*x⁻¹*x*y≈1 = begin
y⁻¹ * x⁻¹ * (x * y - 0#) ≈⟨ *-congˡ (x-0≈x (x * y)) ⟩
y⁻¹ * x⁻¹ * (x * y) ≈⟨ *-assoc y⁻¹ x⁻¹ (x * y) ⟩
y⁻¹ * (x⁻¹ * (x * y)) ≈⟨ *-congˡ (*-assoc x⁻¹ x y) ⟨
y⁻¹ * ((x⁻¹ * x) * y) ≈⟨ *-congˡ (*-congʳ (*-congˡ (x-0≈x x))) ⟨
y⁻¹ * ((x⁻¹ * (x - 0#)) * y) ≈⟨ *-congˡ (*-congʳ x⁻¹*x≈1) ⟩
y⁻¹ * (1# * y) ≈⟨ *-congˡ (*-identityˡ y) ⟩
y⁻¹ * y ≈⟨ *-congˡ (x-0≈x y) ⟨
y⁻¹ * (y - 0#) ≈⟨ y⁻¹*y≈1 ⟩
1# ∎

#-congʳ : x ≈ y → x # z → y # z
#-congʳ {x} {y} {z} x≈y x#z = helper (#⇒invertible x#z)
where

helper : Invertible _≈_ 1# _*_ (x - z) → y # z
helper (x-z⁻¹ , x-z⁻¹*x-z≈1# , x-z*x-z⁻¹≈1#)
= invertibleˡ⇒# (x-z⁻¹ , x-z⁻¹*y-z≈1)
where
open ≈-Reasoning setoid

x-z⁻¹*y-z≈1 : x-z⁻¹ * (y - z) ≈ 1#
x-z⁻¹*y-z≈1 = begin
x-z⁻¹ * (y - z) ≈⟨ *-congˡ (+-congʳ x≈y) ⟨
x-z⁻¹ * (x - z) ≈⟨ x-z⁻¹*x-z≈1# ⟩
1# ∎

#-congˡ : y ≈ z → x # y → x # z
#-congˡ y≈z x#y = #-sym (#-congʳ y≈z (#-sym x#y))
------------------------------------------------------------------------
-- DEPRECATED NAMES
------------------------------------------------------------------------
-- Please use the new names as continuing support for the old names is
-- not guaranteed.

-- Version 2.3

x-0≈x = x-0#≈x
{-# WARNING_ON_USAGE x-0≈x
"Warning: x-0≈x was deprecated in v2.3.
Please use Algebra.Properties.Ring.x-0#≈x instead."
#-}

open HeytingCommutativeRing HCR public using (#-sym)
{-# WARNING_ON_USAGE #-sym
"Warning: #-sym was deprecated in v2.3.
Please use Algebra.Apartness.Structures.IsHeytingCommutativeRing.#-sym instead."
#-}
85 changes: 85 additions & 0 deletions src/Algebra/Apartness/Properties/HeytingField.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Properties of Heyting Fields
------------------------------------------------------------------------

{-# OPTIONS --cubical-compatible --safe #-}

open import Algebra.Apartness.Bundles using (HeytingField)

module Algebra.Apartness.Properties.HeytingField
{c ℓ₁ ℓ₂} (HF : HeytingField c ℓ₁ ℓ₂) where

open import Function.Base using (_∘_)
open import Data.Product.Base using (_,_; proj₁; proj₂)
open import Algebra.Bundles using (CommutativeRing)

open HeytingField HF
open CommutativeRing commutativeRing using (ring; *-commutativeMonoid)

open import Algebra.Definitions _≈_
using (Invertible; LeftInvertible; RightInvertible)
open import Algebra.Properties.CommutativeMonoid *-commutativeMonoid
using (invertibleˡ⇒invertible; invertibleʳ⇒invertible)
open import Algebra.Properties.Ring ring
using (x-0#≈x; -‿distribˡ-*; -‿distribʳ-*; -‿anti-homo-+; -‿involutive)
open import Relation.Binary.Definitions using (Symmetric)
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning

private
variable
x y z : Carrier


invertibleˡ⇒# : LeftInvertible 1# _*_ (x - y) → x # y
invertibleˡ⇒# = invertible⇒# ∘ invertibleˡ⇒invertible

invertibleʳ⇒# : RightInvertible 1# _*_ (x - y) → x # y
invertibleʳ⇒# = invertible⇒# ∘ invertibleʳ⇒invertible

1#0 : 1# # 0#
1#0 = invertibleˡ⇒# (1# , 1*[x-0]≈x)
where
1*[x-0]≈x : 1# * (x - 0#) ≈ x
1*[x-0]≈x {x} = trans (*-identityˡ (x - 0#)) (x-0#≈x x)

x#0y#0→xy#0 : x # 0# → y # 0# → x * y # 0#
x#0y#0→xy#0 {x} {y} x#0 y#0 = helper (#⇒invertible x#0) (#⇒invertible y#0)
Comment on lines +47 to +48
Copy link
Contributor Author

@jamesmckinna jamesmckinna Feb 19, 2025

Choose a reason for hiding this comment

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

@JacquesCarette 's wanting to refactor the proofs (and FTR, largely inherited from the original PR #1968 ...)
It occurs to me that some (all?) of these arguments could be refactored by going via Algebra.Properties.CancellativeCommutativeSemiring, after establishing the relevant structure/bundle?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Er... no, in fact. Those properties require Decidable _≈_, and maybe could be simplified to consider only Tight _≈_ _≉_, but I'm not going to push my luck...

Copy link
Contributor

Choose a reason for hiding this comment

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

Could be. I was thinking of analogs to the elim and cancel reasoning combinators of agda-categories. While at it, I would have ported push/pull (but renamed them) too. Many of these already work on Monoid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed: please raise a PR addressing #2288 !

where
open ≈-Reasoning setoid

helper : Invertible 1# _*_ (x - 0#) → Invertible 1# _*_ (y - 0#) → x * y # 0#
helper (x⁻¹ , x⁻¹*x≈1 , x*x⁻¹≈1) (y⁻¹ , y⁻¹*y≈1 , y*y⁻¹≈1)
= invertibleˡ⇒# (y⁻¹ * x⁻¹ , y⁻¹*x⁻¹*x*y≈1)
where

y⁻¹*x⁻¹*x*y≈1 : y⁻¹ * x⁻¹ * (x * y - 0#) ≈ 1#
y⁻¹*x⁻¹*x*y≈1 = begin
y⁻¹ * x⁻¹ * (x * y - 0#) ≈⟨ *-congˡ (x-0#≈x (x * y)) ⟩
y⁻¹ * x⁻¹ * (x * y) ≈⟨ *-assoc y⁻¹ x⁻¹ (x * y) ⟩
y⁻¹ * (x⁻¹ * (x * y)) ≈⟨ *-congˡ (*-assoc x⁻¹ x y) ⟨
y⁻¹ * ((x⁻¹ * x) * y) ≈⟨ *-congˡ (*-congʳ (*-congˡ (x-0#≈x x))) ⟨
y⁻¹ * ((x⁻¹ * (x - 0#)) * y) ≈⟨ *-congˡ (*-congʳ x⁻¹*x≈1) ⟩
y⁻¹ * (1# * y) ≈⟨ *-congˡ (*-identityˡ y) ⟩
y⁻¹ * y ≈⟨ *-congˡ (x-0#≈x y) ⟨
y⁻¹ * (y - 0#) ≈⟨ y⁻¹*y≈1 ⟩
1# ∎

#-congʳ : x ≈ y → x # z → y # z
#-congʳ {x} {y} {z} x≈y = helper ∘ #⇒invertible
where
open ≈-Reasoning setoid

helper : Invertible 1# _*_ (x - z) → y # z
helper (x-z⁻¹ , x-z⁻¹*x-z≈1# , x-z*x-z⁻¹≈1#)
= invertibleˡ⇒# (x-z⁻¹ , x-z⁻¹*y-z≈1)
where
x-z⁻¹*y-z≈1 : x-z⁻¹ * (y - z) ≈ 1#
x-z⁻¹*y-z≈1 = begin
x-z⁻¹ * (y - z) ≈⟨ *-congˡ (+-congʳ x≈y) ⟨
x-z⁻¹ * (x - z) ≈⟨ x-z⁻¹*x-z≈1# ⟩
1# ∎

#-congˡ : y ≈ z → x # y → x # z
#-congˡ y≈z = #-sym ∘ #-congʳ y≈z ∘ #-sym
22 changes: 11 additions & 11 deletions src/Algebra/Apartness/Structures.agda
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,29 @@ module Algebra.Apartness.Structures
where

open import Level using (_⊔_; suc)
open import Data.Product.Base using (∃-syntax; _×_; _,_; proj₂)
open import Algebra.Definitions _≈_ using (Invertible)
open import Algebra.Structures _≈_ using (IsCommutativeRing)
open import Relation.Binary.Structures using (IsEquivalence; IsApartnessRelation)
open import Relation.Binary.Definitions using (Tight)
open import Relation.Nullary.Negation using (¬_)
open import Relation.Binary.Structures
using (IsEquivalence; IsApartnessRelation; IsTightApartnessRelation)
import Relation.Binary.Properties.ApartnessRelation as AR


record IsHeytingCommutativeRing : Set (c ⊔ ℓ₁ ⊔ ℓ₂) where

field
isCommutativeRing : IsCommutativeRing _+_ _*_ -_ 0# 1#
isApartnessRelation : IsApartnessRelation _≈_ _#_
isCommutativeRing : IsCommutativeRing _+_ _*_ -_ 0# 1#
isTightApartnessRelation : IsTightApartnessRelation _≈_ _#_

open IsCommutativeRing isCommutativeRing public
open IsTightApartnessRelation isTightApartnessRelation public
using (isApartnessRelation; tight)
open IsApartnessRelation isApartnessRelation public
renaming
( irrefl to #-irrefl
; sym to #-sym
; cotrans to #-cotrans
)

field
#⇒invertible : ∀ {x y} → x # y → Invertible 1# _*_ (x - y)
invertible⇒# : ∀ {x y} → Invertible 1# _*_ (x - y) → x # y

¬#-isEquivalence : IsEquivalence _¬#_
¬#-isEquivalence = AR.¬#-isEquivalence refl isApartnessRelation

Expand All @@ -52,6 +48,10 @@ record IsHeytingField : Set (c ⊔ ℓ₁ ⊔ ℓ₂) where

field
isHeytingCommutativeRing : IsHeytingCommutativeRing
tight : Tight _≈_ _#_

open IsHeytingCommutativeRing isHeytingCommutativeRing public

field
#⇒invertible : ∀ {x y} → x # y → Invertible 1# _*_ (x - y)
invertible⇒# : ∀ {x y} → Invertible 1# _*_ (x - y) → x # y

6 changes: 5 additions & 1 deletion src/Algebra/Properties/AbelianGroup.agda
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

{-# OPTIONS --cubical-compatible --safe #-}

open import Algebra
open import Algebra.Bundles using (AbelianGroup)

module Algebra.Properties.AbelianGroup
{a ℓ} (G : AbelianGroup a ℓ) where

open AbelianGroup G
open import Algebra.Definitions _≈_ using (RightIdentity)
open import Function.Base using (_$_)
open import Relation.Binary.Reasoning.Setoid setoid

Expand Down Expand Up @@ -39,3 +40,6 @@ xyx⁻¹≈y x y = begin
x ⁻¹ ∙ y ⁻¹ ≈⟨ ⁻¹-anti-homo-∙ y x ⟨
(y ∙ x) ⁻¹ ≈⟨ ⁻¹-cong $ comm y x ⟩
(x ∙ y) ⁻¹ ∎

x-ε≈x : RightIdentity ε _-_
x-ε≈x x = trans (∙-congˡ ε⁻¹≈ε) (identityʳ x)
Loading