Debian Codegraph @

src/Algebra/Construct/LexProduct/Inner.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the inner lexicographic product of two operators.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Algebra
10 open import Data.Bool.Base using (false; true)
11 open import Data.Product.Base using (_×_; _,_; swap; map; uncurry′)
12 open import Function.Base using (_∘_)
13 open import Level using (Level; _⊔_)
14 open import Relation.Binary.Definitions using (Decidable)
15 open import Relation.Nullary.Decidable using (does; yes; no)
16 open import Relation.Nullary.Negation
17 using (contradiction; contradiction₂)
18 import Relation.Binary.Reasoning.Setoid as SetoidReasoning
19
20 import Algebra.Construct.LexProduct.Base as Base
21
22 module Algebra.Construct.LexProduct.Inner
23 {ℓ₁ ℓ₂ ℓ₃ ℓ₄} (M : Magma ℓ₁ ℓ₂) (N : Magma ℓ₃ ℓ₄)
24 (_≟₁_ : Decidable (Magma._≈_ M))
25 where
26
27 open module M = Magma M
28 renaming
29 ( Carrier to A
30 ; _≈_ to _≈₁_
31 ; _≉_ to _≉₁_
32 )
33
34 open module N = Magma N
35 using ()
36 renaming
37 ( Carrier to B
38 ; _∙_ to _◦_
39 ; _≈_ to _≈₂_
40 ; ∙-cong to ◦-cong
41 )
42
43 private
44 variable
45 a b c d : A
46 w x y z : B
47
48 ------------------------------------------------------------------------
49 -- Base definition
50
51 open Base _∙_ _◦_ _≟₁_ public
52 using (innerLex)
53
54 -- Save ourselves some typing in this file
55 private
56 lex = innerLex
57
58 ------------------------------------------------------------------------
59 -- Properties
60
61 module NaturalOrder where
62
63 -- It would be really nice if we could use
64 -- `Relation.Binary.Construct.NaturalOrder.Left/Right` to prove these
65 -- properties but the equalities are defined the wrong way around
66
67 open SetoidReasoning M.setoid
68
69 ≤∙ˡ-resp-≈ : a ∙ b ≈₁ b → a ≈₁ c → b ≈₁ d → c ∙ d ≈₁ d
70 ≤∙ˡ-resp-≈ {a} {b} {c} {d} ab≈b a≈c b≈d = begin
71 c ∙ d ≈⟨ ∙-cong (M.sym a≈c) (M.sym b≈d) ⟩
72 a ∙ b ≈⟨ ab≈b ⟩
73 b ≈⟨ b≈d ⟩
74 d ∎
75
76 ≤∙ʳ-resp-≈ : a ∙ b ≈₁ a → a ≈₁ c → b ≈₁ d → c ∙ d ≈₁ c
77 ≤∙ʳ-resp-≈ {a} {b} {c} {d} ab≈b a≈c b≈d = begin
78 c ∙ d ≈⟨ ∙-cong (M.sym a≈c) (M.sym b≈d) ⟩
79 a ∙ b ≈⟨ ab≈b ⟩
80 a ≈⟨ a≈c ⟩
81 c ∎
82
83 ≤∙ˡ-trans : Associative _≈₁_ _∙_ → (a ∙ b) ≈₁ b → (b ∙ c) ≈₁ c → (a ∙ c) ≈₁ c
84 ≤∙ˡ-trans {a} {b} {c} ∙-assoc ab≈b bc≈c = begin
85 a ∙ c ≈⟨ ∙-congˡ bc≈c ⟨
86 a ∙ (b ∙ c) ≈⟨ ∙-assoc a b c ⟨
87 (a ∙ b) ∙ c ≈⟨ ∙-congʳ ab≈b ⟩
88 b ∙ c ≈⟨ bc≈c ⟩
89 c ∎
90
91 ≰∙ˡ-trans : Commutative _≈₁_ _∙_ → (a ∙ b) ≉₁ a → (a ∙ c) ≈₁ c → (b ∙ c) ≈₁ c → (a ∙ c) ≉₁ a
92 ≰∙ˡ-trans {a} {b} {c} ∙-comm ab≉a ac≈c bc≈c ac≈a = ab≉a (begin
93 a ∙ b ≈⟨ ∙-congʳ (M.trans (M.sym ac≈a) ac≈c) ⟩
94 c ∙ b ≈⟨ ∙-comm c b ⟩
95 b ∙ c ≈⟨ bc≈c ⟩
96 c ≈⟨ M.trans (M.sym ac≈c) ac≈a ⟩
97 a ∎)
98
99 <∙ˡ-trans : Associative _≈₁_ _∙_ → Commutative _≈₁_ _∙_ →
100 (a ∙ b) ≈₁ b → (a ∙ b) ≉₁ a → (b ∙ c) ≈₁ c →
101 (a ∙ c) ≉₁ a × (a ∙ c) ≈₁ c
102 <∙ˡ-trans {a} {b} {c} ∙-assoc ∙-comm ab≈b ab≉a bc≈c = ac≉a , ac≈c
103 where
104 ac≈c = ≤∙ˡ-trans ∙-assoc ab≈b bc≈c
105 ac≉a = ≰∙ˡ-trans ∙-comm ab≉a ac≈c bc≈c
106
107 <∙ʳ-trans : Associative _≈₁_ _∙_ → Commutative _≈₁_ _∙_ →
108 (a ∙ b) ≈₁ a → (b ∙ c) ≈₁ b → (b ∙ c) ≉₁ c →
109 (a ∙ c) ≈₁ a × (a ∙ c) ≉₁ c
110 <∙ʳ-trans {a} {b} {c} assoc comm ab≈a bc≈b bc≉c = map
111 (M.trans (comm a c))
112 (_∘ M.trans (comm c a))
113 (swap (<∙ˡ-trans assoc comm
114 (M.trans (comm c b) bc≈b)
115 (bc≉c ∘ M.trans (comm b c))
116 (M.trans (comm b a) ab≈a)))
117
118 ------------------------------------------------------------------------
119 -- Basic properties
120
121 open SetoidReasoning N.setoid
122 open NaturalOrder
123
124 case₁ : a ∙ b ≈₁ a → a ∙ b ≉₁ b → lex a b x y ≈₂ x
125 case₁ {a} {b} ab≈a ab≉b with (a ∙ b) ≟₁ a | (a ∙ b) ≟₁ b
126 ... | no ab≉a | _ = contradiction ab≈a ab≉a
127 ... | yes _ | yes ab≈b = contradiction ab≈b ab≉b
128 ... | yes _ | no _ = N.refl
129
130 case₂ : a ∙ b ≉₁ a → a ∙ b ≈₁ b → lex a b x y ≈₂ y
131 case₂ {a} {b} ab≉a ab≈b with (a ∙ b) ≟₁ a | (a ∙ b) ≟₁ b
132 ... | yes ab≈a | _ = contradiction ab≈a ab≉a
133 ... | no _ | no ab≉b = contradiction ab≈b ab≉b
134 ... | no _ | yes _ = N.refl
135
136 case₃ : a ∙ b ≈₁ a → a ∙ b ≈₁ b → lex a b x y ≈₂ (x ◦ y)
137 case₃ {a} {b} ab≈a ab≈b with (a ∙ b) ≟₁ a | (a ∙ b) ≟₁ b
138 ... | no ab≉a | _ = contradiction ab≈a ab≉a
139 ... | yes _ | no ab≉b = contradiction ab≈b ab≉b
140 ... | yes _ | yes _ = N.refl
141
142 ------------------------------------------------------------------------
143 -- Algebraic properties
144
145 cong : a ≈₁ b → c ≈₁ d → w ≈₂ x → y ≈₂ z → lex a c w y ≈₂ lex b d x z
146 cong {a} {b} {c} {d} a≈b c≈d w≈x y≈z
147 with (a ∙ c) ≟₁ a | (a ∙ c) ≟₁ c | (b ∙ d) ≟₁ b | (b ∙ d) ≟₁ d
148 ... | yes _ | yes _ | yes _ | yes _ = ◦-cong w≈x y≈z
149 ... | yes _ | yes _ | no _ | no _ = ◦-cong w≈x y≈z
150 ... | no _ | no _ | yes _ | yes _ = ◦-cong w≈x y≈z
151 ... | no _ | no _ | no _ | no _ = ◦-cong w≈x y≈z
152 ... | yes _ | no _ | yes _ | no _ = w≈x
153 ... | no _ | yes _ | no _ | yes _ = y≈z
154 ... | _ | yes ac≈c | _ | no bd≉d = contradiction (≤∙ˡ-resp-≈ ac≈c a≈b c≈d) bd≉d
155 ... | yes ac≈a | _ | no bd≉b | _ = contradiction (≤∙ʳ-resp-≈ ac≈a a≈b c≈d) bd≉b
156 ... | _ | no ac≉c | _ | yes bd≈d = contradiction (≤∙ˡ-resp-≈ bd≈d (M.sym a≈b) (M.sym c≈d)) ac≉c
157 ... | no ac≉a | _ | yes bd≈b | _ = contradiction (≤∙ʳ-resp-≈ bd≈b (M.sym a≈b) (M.sym c≈d)) ac≉a
158
159 cong₁₂ : a ≈₁ b → c ≈₁ d → lex a c x y ≈₂ lex b d x y
160 cong₁₂ a≈b c≈d = cong a≈b c≈d N.refl N.refl
161
162 cong₁ : a ≈₁ b → lex a c x y ≈₂ lex b c x y
163 cong₁ a≈b = cong₁₂ a≈b M.refl
164
165 cong₂ : b ≈₁ c → lex a b x y ≈₂ lex a c x y
166 cong₂ = cong₁₂ M.refl
167
168 -- It is possible to relax this. Instead of ∙ being selective and ◦
169 -- being associative it's also possible for _◦_ to return a single
170 -- idempotent element.
171 assoc : Associative _≈₁_ _∙_ → Commutative _≈₁_ _∙_ →
172 Selective _≈₁_ _∙_ → Associative _≈₂_ _◦_ →
173 ∀ a b c x y z → lex (a ∙ b) c (lex a b x y) z ≈₂ lex a (b ∙ c) x (lex b c y z)
174 assoc ∙-assoc ∙-comm ∙-sel ◦-assoc a b c x y z
175 with (a ∙ b) ≟₁ a | (a ∙ b) ≟₁ b | (b ∙ c) ≟₁ b | (b ∙ c) ≟₁ c
176 ... | _ | _ | no bc≉b | no bc≉c = contradiction₂ (∙-sel b c) bc≉b bc≉c
177 ... | no ab≉a | no ab≉b | _ | _ = contradiction₂ (∙-sel a b) ab≉a ab≉b
178 ... | yes ab≈a | no ab≉b | no bc≉b | yes bc≈c = cong₁₂ ab≈a (M.sym bc≈c)
179 ... | no ab≉a | yes ab≈b | yes bc≈b | yes bc≈c = begin
180 lex (a ∙ b) c y z ≈⟨ cong₁ ab≈b ⟩
181 lex b c y z ≈⟨ case₃ bc≈b bc≈c ⟩
182 y ◦ z ≈⟨ case₂ ab≉a ab≈b ⟨
183 lex a b x (y ◦ z) ≈⟨ cong₂ bc≈b ⟨
184 lex a (b ∙ c) x (y ◦ z) ∎
185 ... | no ab≉a | yes ab≈b | yes bc≈b | no bc≉c = begin
186 lex (a ∙ b) c y z ≈⟨ cong₁ ab≈b ⟩
187 lex b c y z ≈⟨ case₁ bc≈b bc≉c ⟩
188 y ≈⟨ case₂ ab≉a ab≈b ⟨
189 lex a b x y ≈⟨ cong₂ bc≈b ⟨
190 lex a (b ∙ c) x y ∎
191 ... | yes ab≈a | yes ab≈b | yes bc≈b | no bc≉c = begin
192 lex (a ∙ b) c (x ◦ y) z ≈⟨ cong₁ ab≈b ⟩
193 lex b c (x ◦ y) z ≈⟨ case₁ bc≈b bc≉c ⟩
194 x ◦ y ≈⟨ case₃ ab≈a ab≈b ⟨
195 lex a b x y ≈⟨ cong₂ bc≈b ⟨
196 lex a (b ∙ c) x y ∎
197 ... | yes ab≈a | yes ab≈b | yes bc≈b | yes bc≈c = begin
198 lex (a ∙ b) c (x ◦ y) z ≈⟨ cong₁ ab≈b ⟩
199 lex b c (x ◦ y) z ≈⟨ case₃ bc≈b bc≈c ⟩
200 (x ◦ y) ◦ z ≈⟨ ◦-assoc x y z ⟩
201 x ◦ (y ◦ z) ≈⟨ case₃ ab≈a ab≈b ⟨
202 lex a b x (y ◦ z) ≈⟨ cong₂ bc≈b ⟨
203 lex a (b ∙ c) x (y ◦ z) ∎
204 ... | yes ab≈a | yes ab≈b | no bc≉b | yes bc≈c = begin
205 lex (a ∙ b) c (x ◦ y) z ≈⟨ cong₁ ab≈b ⟩
206 lex b c (x ◦ y) z ≈⟨ case₂ bc≉b bc≈c ⟩
207 z ≈⟨ case₂ bc≉b bc≈c ⟨
208 lex b c x z ≈⟨ cong₁₂ (M.trans (M.sym ab≈a) ab≈b) bc≈c ⟨
209 lex a (b ∙ c) x z ∎
210 ... | yes ab≈a | no ab≉b | yes bc≈b | yes bc≈c = begin
211 lex (a ∙ b) c x z ≈⟨ cong₁₂ ab≈a (M.trans (M.sym bc≈c) bc≈b) ⟩
212 lex a b x z ≈⟨ case₁ ab≈a ab≉b ⟩
213 x ≈⟨ case₁ ab≈a ab≉b ⟨
214 lex a b x (y ◦ z) ≈⟨ cong₂ bc≈b ⟨
215 lex a (b ∙ c) x (y ◦ z) ∎
216 ... | no ab≉a | yes ab≈b | no bc≉b | yes bc≈c = begin
217 lex (a ∙ b) c y z ≈⟨ cong₁ ab≈b ⟩
218 lex b c y z ≈⟨ case₂ bc≉b bc≈c ⟩
219 z ≈⟨ uncurry′ case₂ (<∙ˡ-trans ∙-assoc ∙-comm ab≈b ab≉a bc≈c) ⟨
220 lex a c x z ≈⟨ cong₂ bc≈c ⟨
221 lex a (b ∙ c) x z ∎
222 ... | yes ab≈a | no ab≉b | yes bc≈b | no bc≉c = begin
223 lex (a ∙ b) c x z ≈⟨ cong₁ ab≈a ⟩
224 lex a c x z ≈⟨ uncurry′ case₁ (<∙ʳ-trans ∙-assoc ∙-comm ab≈a bc≈b bc≉c) ⟩
225 x ≈⟨ case₁ ab≈a ab≉b ⟨
226 lex a b x y ≈⟨ cong₂ bc≈b ⟨
227 lex a (b ∙ c) x y ∎
228
229 comm : Commutative _≈₁_ _∙_ → Commutative _≈₂_ _◦_ →
230 ∀ a b x y → lex a b x y ≈₂ lex b a y x
231 comm ∙-comm ◦-comm a b x y
232 with (a ∙ b) ≟₁ a | (a ∙ b) ≟₁ b | (b ∙ a) ≟₁ b | (b ∙ a) ≟₁ a
233 ... | yes ab≈a | _ | _ | no ba≉a = contradiction (M.trans (∙-comm b a) ab≈a) ba≉a
234 ... | no ab≉a | _ | _ | yes ba≈a = contradiction (M.trans (∙-comm a b) ba≈a) ab≉a
235 ... | _ | yes ab≈b | no ba≉b | _ = contradiction (M.trans (∙-comm b a) ab≈b) ba≉b
236 ... | _ | no ab≉b | yes ba≈b | _ = contradiction (M.trans (∙-comm a b) ba≈b) ab≉b
237 ... | yes _ | yes _ | yes _ | yes _ = ◦-comm x y
238 ... | yes _ | no _ | no _ | yes _ = N.refl
239 ... | no _ | yes _ | yes _ | no _ = N.refl
240 ... | no _ | no _ | no _ | no _ = ◦-comm x y
241
242 idem : Idempotent _≈₂_ _◦_ → ∀ a b x → lex a b x x ≈₂ x
243 idem ◦-idem a b x with does ((a ∙ b) ≟₁ a) | does ((a ∙ b) ≟₁ b)
244 ... | false | false = ◦-idem x
245 ... | false | true = N.refl
246 ... | true | false = N.refl
247 ... | true | true = ◦-idem x
248
249 zeroʳ : ∀ {e f} → RightZero _≈₁_ e _∙_ → RightZero _≈₂_ f _◦_ →
250 lex a e x f ≈₂ f
251 zeroʳ {a} {x} {e} {f} ze₁ ze₂ with (a ∙ e) ≟₁ a | (a ∙ e) ≟₁ e
252 ... | _ | no a∙e≉e = contradiction (ze₁ a) a∙e≉e
253 ... | no _ | yes _ = N.refl
254 ... | yes _ | yes _ = ze₂ x
255
256 identityʳ : ∀ {e f} → RightIdentity _≈₁_ e _∙_ → RightIdentity _≈₂_ f _◦_ →
257 lex a e x f ≈₂ x
258 identityʳ {a} {x} {e} {f} id₁ id₂ with (a ∙ e) ≟₁ a | (a ∙ e) ≟₁ e
259 ... | no a∙e≉a | _ = contradiction (id₁ a) a∙e≉a
260 ... | yes _ | no _ = N.refl
261 ... | yes _ | yes _ = id₂ x