Skip to content

[Add] Consequences of associativity for Semigroups #2688

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
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0fc076f
Adds reasonig combinator for semigroup
jmougeot Apr 1, 2025
3399c61
Adds reasonig combinator for semigroup
jmougeot Apr 1, 2025
90fe273
Adds reasonig combinator for semigroup
jmougeot Apr 1, 2025
ef3282f
Adds reasonig combinator for semigroup
jmougeot Apr 1, 2025
63e88cc
Add some more missing reasoning combinators
jmougeot Apr 1, 2025
bb7ce15
add module Extends
jmougeot Apr 1, 2025
885d7a0
rename SemiGroup to Semigroup
jmougeot Apr 1, 2025
ad54a5b
fix-whitespace
jmougeot Apr 1, 2025
2b511c2
Update CHANGELOG.md
jmougeot Apr 2, 2025
8151123
New names
jmougeot Apr 3, 2025
2361b40
improve syntx
jmougeot Apr 3, 2025
503c693
fix-whitespace
jmougeot Apr 3, 2025
0d5c9ed
Name changes
jmougeot Apr 7, 2025
1a67eb9
Names change
jmougeot Apr 7, 2025
002cfad
Space
jmougeot Apr 7, 2025
a47bcc5
Proof of assoc with PUshes and Pulles
jmougeot Apr 8, 2025
ca9f576
Proof of assoc with PUshes and Pulles
jmougeot Apr 8, 2025
e07f81b
white space
jmougeot Apr 9, 2025
86b06e0
Update src/Algebra/Properties/Semigroup/Reasoning.agda
jmougeot Apr 10, 2025
7b72ff2
Reasoning to Semigroup and explicit variables
jmougeot Apr 11, 2025
13b5f0e
fix bug
jmougeot Apr 11, 2025
f58aceb
space
jmougeot Apr 11, 2025
74607d5
Update src/Algebra/Properties/Semigroup.agda
jmougeot Apr 14, 2025
e3b2550
Update CHANGELOG.md
jmougeot Apr 14, 2025
e63afbd
Update src/Algebra/Properties/Semigroup.agda
jmougeot Apr 14, 2025
b2bfa03
Update src/Algebra/Properties/Semigroup.agda
jmougeot Apr 14, 2025
14224b9
Update src/Algebra/Properties/Semigroup.agda
jmougeot Apr 14, 2025
76b6637
Update src/Algebra/Properties/Semigroup.agda
jmougeot Apr 14, 2025
06af327
Update src/Algebra/Properties/Semigroup.agda
jmougeot Apr 14, 2025
a22f97d
variables
jmougeot Apr 14, 2025
1283b57
update CHANGELOG
jmougeot Apr 16, 2025
e1304da
Explicit varaibles
jmougeot Apr 29, 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ New modules

* `Data.Sign.Show` to show a sign

* `Algebra.Reasoning.Semigroup` adding reasoning combinators for semigroups

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also not sure whether we should refer to these as reasoning combinators. The Reasoning terminology has quite a specific use in the library for syntax that chains together nicely. I'm not sure these lemmas qualify?

Copy link
Contributor

Choose a reason for hiding this comment

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

We won't need CHANGELOG text if it moves to Additions to..., but the opening comment block should be rephrased...

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

Expand Down
157 changes: 157 additions & 0 deletions src/Algebra/Reasoning/Semigroup.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Equational reasoning for semigroups
-- (Utilities for associativity reasoning, pulling and pushing operations)
------------------------------------------------------------------------

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

open import Algebra using (Semigroup)

module Algebra.Reasoning.Semigroup {o ℓ} (S : Semigroup o ℓ) where

open Semigroup S
using (Carrier; _∙_; _≈_; setoid; trans ; refl; sym; assoc; ∙-cong)
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is by 2 spaces, not 4 as you have here... see style-guide.

Suggested change
using (Carrier; _∙_; _≈_; setoid; trans ; refl; sym; assoc; ∙-cong)
using (Carrier; _∙_; _≈_; setoid; trans ; refl; sym; assoc; ∙-cong; ∙-congˡ; ∙-congʳ)

open import Relation.Binary.Reasoning.Setoid setoid

private
variable
a b c d e x y z : Carrier

module Assoc4 {a b c d : Carrier} where
{-
Explanation of naming scheme:

Each successive association is given a Greek letter, from 'α' associated all
the way to the left, to 'ε' associated all the way to the right. Then,
'assoc²XY' is the proof that X is equal to Y. Explicitly:

α = ((a ∙ b) ∙ c) ∙ d
β = (a ∙ (b ∙ c)) ∙ d
γ = (a ∙ b) ∙ (c ∙ d)
δ = a ∙ ((b ∙ c) ∙ d)
ε = a ∙ (b ∙ (c ∙ d))

Only reassociations that need two assoc steps are defined here.
-}
assoc²αδ : ((a ∙ b) ∙ c) ∙ d ≈ a ∙ ((b ∙ c) ∙ d)
assoc²αδ = trans (∙-cong (assoc a b c) refl) (assoc a (b ∙ c) d)

assoc²αε : ((a ∙ b) ∙ c) ∙ d ≈ a ∙ (b ∙ (c ∙ d))
assoc²αε = trans (assoc (a ∙ b) c d) (assoc a b (c ∙ d))

assoc²βγ : (a ∙ (b ∙ c)) ∙ d ≈ (a ∙ b) ∙ (c ∙ d)
assoc²βγ = trans (sym (∙-cong (assoc a b c) refl)) (assoc (a ∙ b) c d)

assoc²βε : (a ∙ (b ∙ c)) ∙ d ≈ a ∙ (b ∙ (c ∙ d))
assoc²βε = trans (assoc a (b ∙ c) d) (∙-cong refl (assoc b c d))

assoc²γβ : (a ∙ b) ∙ (c ∙ d) ≈ (a ∙ (b ∙ c)) ∙ d
assoc²γβ = trans (sym (assoc (a ∙ b) c d)) (∙-cong (assoc a b c) refl)

assoc²γδ : (a ∙ b) ∙ (c ∙ d) ≈ a ∙ ((b ∙ c) ∙ d)
assoc²γδ = begin
(a ∙ b) ∙ (c ∙ d) ≈⟨ assoc a b (c ∙ d) ⟩
a ∙ (b ∙ (c ∙ d)) ≈⟨ ∙-cong refl (sym (assoc b c d)) ⟩
a ∙ ((b ∙ c) ∙ d) ∎

assoc²δα : a ∙ ((b ∙ c) ∙ d) ≈ ((a ∙ b) ∙ c) ∙ d
assoc²δα = sym (trans (∙-cong (assoc a b c) refl) (assoc a (b ∙ c) d))

assoc²δγ : a ∙ ((b ∙ c) ∙ d) ≈ (a ∙ b) ∙ (c ∙ d)
assoc²δγ = begin
a ∙ ((b ∙ c) ∙ d) ≈⟨ ∙-cong refl (assoc b c d) ⟩
a ∙ (b ∙ (c ∙ d)) ≈⟨ sym (assoc a b (c ∙ d)) ⟩
(a ∙ b) ∙ (c ∙ d) ∎

assoc²εα : a ∙ (b ∙ (c ∙ d)) ≈ ((a ∙ b) ∙ c) ∙ d
assoc²εα = sym (trans (assoc (a ∙ b) c d) (assoc a b (c ∙ d)))

assoc²εβ : a ∙ (b ∙ (c ∙ d)) ≈ (a ∙ (b ∙ c)) ∙ d
assoc²εβ = sym (trans (assoc a (b ∙ c) d) (∙-cong refl (assoc b c d)))

open Assoc4 public

module Pulls (ab≡c : a ∙ b ≈ c) where
pullʳ : ∀ {x} → (x ∙ a) ∙ b ≈ x ∙ c
Copy link
Contributor

@jamesmckinna jamesmckinna Apr 2, 2025

Choose a reason for hiding this comment

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

Elsewhere, the library would use lemma names of the form p∧q⇒r, with square brackets for grouping sub-terms, so here:

module Pulls (x∙y≈z : x ∙ y ≈ z) where
  x∙y≈z⇒[w∙x]∙y≈w∙z : (w ∙ x) ∙ y ≈ w ∙ z

NB. also: you have declared x as a variable, so there's no need to have the quantifier! Although here again, we have the problem that in any deployment in a concrete Semigroup the typechecker might not be able to infer w... so there are questions about how the quantifications should be handled.

If you can find examples in eg Data.Rational.Properties which might be simplified by these lemmas, then you might discover whether Agda can (or not) infer the various implicits?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, the use of in the lemma names would avoid the need for the submodules here to be named; they could (more) simply be anonymous module _ (hyps) where...

Copy link
Contributor

Choose a reason for hiding this comment

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

While x∙y≈z⇒[w∙x]∙y≈w∙z is a logical name, it is also quite long. As these combinators that re-associate are book-keeping things, I'd like to find something shorter / less noisy.

I do hate push/pull. I'm partial to on-right.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, with my suggested notational optimisations, this would/could become...xy≈z⇒wx∙y≈wz, which is about as short as I can make it?!

Copy link
Contributor

Choose a reason for hiding this comment

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

As for on-right I'm really not sure what that is supposed to signify, given that the lemma is not cong like, nor is the action obviously happening on the right? (Never mind our ongoing debates about left/right distinctions giving rise to confusion...)

Copy link
Contributor

Choose a reason for hiding this comment

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

on-right as in apply this equality on the right, after a re-focus. It's very much cong-like to me, it just has a re-association done first, to put "the right" in focus.

Copy link
Contributor

@jamesmckinna jamesmckinna Apr 10, 2025

Choose a reason for hiding this comment

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

I'd tried to reply, but my comment (seems to have) got lost: if you're wedded to an ASCII/prefix-Lisp name for these things, then I'd much prefer refocus-on-right, or even refocus-right (the -on isn't then doing much work), or even refocusʳ... in the style of other lemmas in the library.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going to make some proposal on naming as a full comments instead of inline.

pullʳ {x = x} = begin
(x ∙ a) ∙ b ≈⟨ assoc x a b ⟩
x ∙ (a ∙ b) ≈⟨ ∙-cong refl ab≡c ⟩
x ∙ c ∎

pullˡ : ∀ {x} → a ∙ (b ∙ x) ≈ c ∙ x
pullˡ {x = x} = begin
a ∙ (b ∙ x) ≈⟨ sym (assoc a b x) ⟩
(a ∙ b) ∙ x ≈⟨ ∙-cong ab≡c refl ⟩
c ∙ x ∎

pull-first : ∀ {x y} → a ∙ ((b ∙ x) ∙ y) ≈ c ∙ (x ∙ y)
pull-first {x = x} {y = y} = begin
a ∙ ((b ∙ x) ∙ y) ≈⟨ ∙-cong refl (assoc b x y) ⟩
a ∙ (b ∙ (x ∙ y)) ≈⟨ pullˡ ⟩
c ∙ (x ∙ y) ∎

pull-center : ∀ {x y} → x ∙ (a ∙ (b ∙ y)) ≈ x ∙ (c ∙ y)
pull-center {x = x} {y = y} = ∙-cong refl (pullˡ)

-- could be called pull₃ʳ
pull-last : ∀ {x y} → (x ∙ (y ∙ a)) ∙ b ≈ x ∙ (y ∙ c)
pull-last {x = x} {y = y} = begin
(x ∙ (y ∙ a)) ∙ b ≈⟨ assoc x (y ∙ a) b ⟩
x ∙ ((y ∙ a) ∙ b) ≈⟨ ∙-cong refl (pullʳ {x = y}) ⟩
x ∙ (y ∙ c) ∎

open Pulls public

module Pushes (ab≡c : a ∙ b ≈ c) where
pushʳ : x ∙ c ≈ (x ∙ a) ∙ b
pushʳ {x = x} = begin
x ∙ c ≈⟨ sym (∙-cong refl ab≡c) ⟩
x ∙ (a ∙ b) ≈⟨ sym (assoc x a b) ⟩
(x ∙ a) ∙ b ∎
Copy link
Contributor

Choose a reason for hiding this comment

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

Successive appeals to sym suggest that it should be lifted out (permutation of sym through proofs), but in fact doing so quickly reveals the following:

Suggested change
pushʳ {x = x} = begin
x ∙ c ≈⟨ sym (∙-cong refl ab≡c) ⟩
x ∙ (a ∙ b) ≈⟨ sym (assoc x a b) ⟩
(x ∙ a) ∙ b ∎
pushʳ = sym (pullʳ ab≡c)


pushˡ : c ∙ x ≈ a ∙ (b ∙ x)
pushˡ {x = x} = begin
c ∙ x ≈⟨ sym (∙-cong ab≡c refl) ⟩
(a ∙ b) ∙ x ≈⟨ assoc a b x ⟩
a ∙ (b ∙ x) ∎
open Pushes public

-- operate in the 'center' instead (like pull/push)
center : a ∙ b ≈ c → (d ∙ a) ∙ (b ∙ e) ≈ d ∙ (c ∙ e)
center eq = pullʳ (pullˡ eq)

-- operate on the left part, then the right part
center⁻¹ : a ∙ b ≈ c → x ∙ y ≈ z → a ∙ ((b ∙ x) ∙ y) ≈ c ∙ z
center⁻¹ {a = a} {b = b} {c = c} {x = x} {y = y} {z = z} eq eq′ = begin
a ∙ ((b ∙ x) ∙ y) ≈⟨ ∙-cong refl (pullʳ eq′) ⟩
a ∙ (b ∙ z) ≈⟨ pullˡ eq ⟩
c ∙ z ∎

push-center : a ∙ b ≈ c → x ∙ (c ∙ y) ≈ x ∙ (a ∙ (b ∙ y))
push-center eq = sym (pull-center eq)

module Extends {a b c d : Carrier} (s : a ∙ b ≈ c ∙ d) where
-- rewrite (x ∙ a) ∙ b to (x ∙ c) ∙ d
extendˡ : (x ∙ a) ∙ b ≈ (x ∙ c) ∙ d
extendˡ {x = x} = begin
(x ∙ a) ∙ b ≈⟨ pullʳ s ⟩
x ∙ (c ∙ d) ≈⟨ sym (assoc x c d) ⟩
(x ∙ c) ∙ d ∎

-- rewrite a ∙ (b ∙ x) to c ∙ (d ∙ x)
extendʳ : a ∙ (b ∙ x) ≈ c ∙ (d ∙ x)
extendʳ {x = x} = begin
a ∙ (b ∙ x) ≈⟨ pullˡ s ⟩
(c ∙ d) ∙ x ≈⟨ assoc c d x ⟩
c ∙ (d ∙ x) ∎

-- rewrite (x ∙ a) ∙ (b ∙ y) to (x ∙ c) ∙ (d ∙ y)
extend² : ∀ x y → (x ∙ a) ∙ (b ∙ y) ≈ (x ∙ c) ∙ (d ∙ y)
extend² x y = begin
(x ∙ a) ∙ (b ∙ y) ≈⟨ pullʳ (extendʳ {x = y}) ⟩
x ∙ (c ∙ (d ∙ y)) ≈⟨ sym (assoc x c (d ∙ y)) ⟩
(x ∙ c) ∙ (d ∙ y) ∎

open Extends public