-
Notifications
You must be signed in to change notification settings - Fork 247
[Add] Consequences of associativity for Semigroup
s
#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
base: master
Are you sure you want to change the base?
Changes from 9 commits
0fc076f
3399c61
90fe273
ef3282f
63e88cc
bb7ce15
885d7a0
ad54a5b
2b511c2
8151123
2361b40
503c693
0d5c9ed
1a67eb9
002cfad
a47bcc5
ca9f576
e07f81b
86b06e0
7b72ff2
13b5f0e
f58aceb
74607d5
e3b2550
e63afbd
b2bfa03
14224b9
76b6637
06af327
a22f97d
1283b57
e1304da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
module Algebra.Reasoning.Semigroup {o ℓ} (S : Semigroup o ℓ) where | ||||||||||||
|
||||||||||||
open Semigroup S | ||||||||||||
using (Carrier; _∙_; _≈_; setoid; trans ; refl; sym; assoc; ∙-cong) | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is by 2 spaces, not 4 as you have here... see
Suggested change
|
||||||||||||
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) | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
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 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elsewhere, the library would use lemma names of the form 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 If you can find examples in eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I do hate push/pull. I'm partial to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, with my suggested notational optimisations, this would/could become... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ⟩ | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
x ∙ c ∎ | ||||||||||||
|
||||||||||||
pullˡ : ∀ {x} → a ∙ (b ∙ x) ≈ c ∙ x | ||||||||||||
pullˡ {x = x} = begin | ||||||||||||
a ∙ (b ∙ x) ≈⟨ sym (assoc a b x) ⟩ | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
(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₃ʳ | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
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 | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
pushʳ {x = x} = begin | ||||||||||||
x ∙ c ≈⟨ sym (∙-cong refl ab≡c) ⟩ | ||||||||||||
x ∙ (a ∙ b) ≈⟨ sym (assoc x a b) ⟩ | ||||||||||||
(x ∙ a) ∙ b ∎ | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Successive appeals to
Suggested change
|
||||||||||||
|
||||||||||||
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) | ||||||||||||
jmougeot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
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 |
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 also not sure whether we should refer to these as
reasoning combinators
. TheReasoning
terminology has quite a specific use in the library for syntax that chains together nicely. I'm not sure these lemmas qualify?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.
We won't need
CHANGELOG
text if it moves toAdditions to...
, but the opening comment block should be rephrased...