Debian Codegraph @

src/Data/List/Relation/Binary/BagAndSetEquality.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Bag and set equality
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Binary.BagAndSetEquality where
10
11 open import Algebra.Bundles using (CommutativeMonoid)
12 open import Algebra.Definitions using (Idempotent)
13 open import Algebra.Structures.Biased using (isCommutativeMonoidˡ)
14 open import Effect.Monad using (RawMonad)
15 open import Data.Empty using (⊥)
16 open import Data.Fin.Base using (Fin; zero; suc)
17 open import Data.List.Base
18 using (List; []; _∷_; map; _++_; concat; [_]; lookup; length)
19 open import Data.List.Effectful using (monad; module Applicative; module MonadProperties)
20 import Data.List.Properties as List
21 open import Data.List.Relation.Unary.Any using (Any; here; there)
22 open import Data.List.Relation.Unary.Any.Properties
23 using (∷↔; map↔; Any-cong; ++↔; concat↔; >>=↔; ++↔++; ⊎↔; ⊥↔Any[])
24 open import Data.List.Membership.Propositional using (_∈_)
25 open import Data.List.Membership.Propositional.Properties
26 using (∈-∃++)
27 open import Data.List.Relation.Binary.Subset.Propositional.Properties
28 using (⊆-preorder)
29 open import Data.List.Relation.Binary.Permutation.Propositional
30 using (_↭_; ↭-refl; ↭-sym; ↭-prep; module PermutationReasoning)
31 open import Data.List.Relation.Binary.Permutation.Propositional.Properties
32 using (∈-resp-↭; ∈-resp-[σ⁻¹∘σ]; ∈-resp-[σ∘σ⁻¹]; shift; ++-comm)
33 open import Data.Product.Base as Product using (∃; _,_; proj₁; proj₂; _×_)
34 import Data.Product.Function.Dependent.Propositional as Σ
35 open import Data.Sum.Base as Sum using (_⊎_; [_,_]′; inj₁; inj₂)
36 open import Data.Sum.Properties using (inj₂-injective; inj₁-injective)
37 open import Data.Sum.Function.Propositional using (_⊎-cong_)
38 open import Data.Unit.Polymorphic.Base using (⊤)
39 open import Function.Base using (_∘_; _$_; id; _⟨_⟩_; case_of_)
40 open import Function.Bundles using (_↔_; Inverse; Equivalence; mk↔ₛ′; mk⇔)
41 open import Function.Related.Propositional as Related
42 using (↔⇒; ⌊_⌋; ⌊_⌋→; ⇒→; K-refl; SK-sym)
43 open import Function.Related.TypeIsomorphisms using (×-identityʳ; ∃∃↔∃∃)
44 open import Function.Properties.Inverse using (↔-sym; ↔-trans; to-from)
45 open import Level using (Level)
46 open import Relation.Binary.Core using (_⇒_)
47 open import Relation.Binary.Definitions using (Trans)
48 open import Relation.Binary.Bundles using (Preorder; Setoid)
49 import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
50 import Relation.Binary.Reasoning.Preorder as ≲-Reasoning
51 open import Relation.Binary.PropositionalEquality.Core as ≡
52 using (_≡_; _≢_; _≗_; refl)
53 open import Relation.Binary.PropositionalEquality.Properties as ≡
54 using (module ≡-Reasoning)
55 open import Relation.Binary.Reasoning.Syntax
56 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
57
58 private
59 variable
60 a b : Level
61 A B : Set a
62 x y : A
63 ws xs ys zs : List A
64
65 ------------------------------------------------------------------------
66 -- Definitions
67
68 open Related public using (Kind; SymmetricKind) renaming
69 ( implication to subset
70 ; reverseImplication to superset
71 ; equivalence to set
72 ; injection to subbag
73 ; reverseInjection to superbag
74 ; bijection to bag
75 )
76
77 [_]-Order : Kind → Set a → Preorder _ _ _
78 [ k ]-Order A = Related.InducedPreorder₂ {A = A} k _∈_
79
80 [_]-Equality : SymmetricKind → Set a → Setoid _ _
81 [ k ]-Equality A = Related.InducedEquivalence₂ {A = A} k _∈_
82
83 infix 4 _∼[_]_
84
85 _∼[_]_ : ∀ {a} {A : Set a} → List A → Kind → List A → Set _
86 _∼[_]_ {A = A} xs k ys = Preorder._≲_ ([ k ]-Order A) xs ys
87
88 private
89 module Eq {k a} {A : Set a} = Setoid ([ k ]-Equality A)
90 module Ord {k a} {A : Set a} = Preorder ([ k ]-Order A)
91 open module ListMonad {ℓ} = RawMonad (monad {ℓ = ℓ})
92 module MP = MonadProperties
93
94 ------------------------------------------------------------------------
95 -- Bag equality implies the other relations.
96
97 bag-=⇒ : ∀ {k} → xs ∼[ bag ] ys → xs ∼[ k ] ys
98 bag-=⇒ xs≈ys = ↔⇒ xs≈ys
99
100 ------------------------------------------------------------------------
101 -- "Equational" reasoning for _⊆_ along with an additional relatedness
102
103 module ⊆-Reasoning {A : Set a} where
104 private module Base = ≲-Reasoning (⊆-preorder A)
105
106 open Base public
107 hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨; step-∼; step-≲)
108 renaming (≲-go to ⊆-go)
109
110 open begin-membership-syntax _IsRelatedTo_ _∈_ (λ x → begin x) public
111 open ⊆-syntax _IsRelatedTo_ _IsRelatedTo_ ⊆-go public
112
113 module _ {k : Related.ForwardKind} where
114 ∼-go : Trans _∼[ ⌊ k ⌋→ ]_ _IsRelatedTo_ _IsRelatedTo_
115 ∼-go eq = ⊆-go (⇒→ eq)
116
117 open ∼-syntax _IsRelatedTo_ _IsRelatedTo_ ∼-go public
118
119
120 ------------------------------------------------------------------------
121 -- Congruence lemmas
122 ------------------------------------------------------------------------
123 -- _∷_
124
125 module _ {k} {x y : A} {xs ys} where
126
127 ∷-cong : x ≡ y → xs ∼[ k ] ys → x ∷ xs ∼[ k ] y ∷ ys
128 ∷-cong refl xs≈ys {y} = begin
129 y ∈ x ∷ xs ↔⟨ SK-sym $ ∷↔ (y ≡_) ⟩
130 (y ≡ x ⊎ y ∈ xs) ∼⟨ K-refl ⊎-cong xs≈ys ⟩
131 (y ≡ x ⊎ y ∈ ys) ↔⟨ ∷↔ (y ≡_) ⟩
132 y ∈ x ∷ ys ∎
133 where open Related.EquationalReasoning
134
135 ------------------------------------------------------------------------
136 -- map
137
138 module _ {k} {f g : A → B} {xs ys} where
139
140 map-cong : f ≗ g → xs ∼[ k ] ys → map f xs ∼[ k ] map g ys
141 map-cong f≗g xs≈ys {x} = begin
142 x ∈ map f xs ↔⟨ SK-sym $ map↔ ⟩
143 Any (λ y → x ≡ f y) xs ∼⟨ Any-cong (↔⇒ ∘ helper) xs≈ys ⟩
144 Any (λ y → x ≡ g y) ys ↔⟨ map↔ ⟩
145 x ∈ map g ys ∎
146 where
147 open Related.EquationalReasoning
148
149 helper : ∀ y → x ≡ f y ↔ x ≡ g y
150 helper y = mk↔ₛ′
151 (λ x≡fy → ≡.trans x≡fy ( f≗g y))
152 (λ x≡gy → ≡.trans x≡gy (≡.sym $ f≗g y))
153 (λ { ≡.refl → ≡.trans-symˡ (f≗g y) })
154 λ { ≡.refl → ≡.trans-symʳ (f≗g y) }
155
156 ------------------------------------------------------------------------
157 -- _++_
158
159 module _ {k} {xs₁ xs₂ ys₁ ys₂ : List A} where
160
161 ++-cong : xs₁ ∼[ k ] xs₂ → ys₁ ∼[ k ] ys₂ →
162 xs₁ ++ ys₁ ∼[ k ] xs₂ ++ ys₂
163 ++-cong xs₁≈xs₂ ys₁≈ys₂ {x} = begin
164 x ∈ xs₁ ++ ys₁ ↔⟨ SK-sym $ ++↔ ⟩
165 (x ∈ xs₁ ⊎ x ∈ ys₁) ∼⟨ xs₁≈xs₂ ⊎-cong ys₁≈ys₂ ⟩
166 (x ∈ xs₂ ⊎ x ∈ ys₂) ↔⟨ ++↔ ⟩
167 x ∈ xs₂ ++ ys₂ ∎
168 where open Related.EquationalReasoning
169
170 ------------------------------------------------------------------------
171 -- concat
172
173 module _ {k} {xss yss : List (List A)} where
174
175 concat-cong : xss ∼[ k ] yss → concat xss ∼[ k ] concat yss
176 concat-cong xss≈yss {x} = begin
177 x ∈ concat xss ↔⟨ SK-sym concat↔ ⟩
178 Any (Any (x ≡_)) xss ∼⟨ Any-cong (λ _ → _ ∎) xss≈yss ⟩
179 Any (Any (x ≡_)) yss ↔⟨ concat↔ ⟩
180 x ∈ concat yss ∎
181 where open Related.EquationalReasoning
182
183 ------------------------------------------------------------------------
184 -- _>>=_
185
186 module _ {k} {A B : Set a} {xs ys} {f g : A → List B} where
187
188 >>=-cong : xs ∼[ k ] ys → (∀ x → f x ∼[ k ] g x) →
189 (xs >>= f) ∼[ k ] (ys >>= g)
190 >>=-cong xs≈ys f≈g {x} = begin
191 x ∈ (xs >>= f) ↔⟨ SK-sym >>=↔ ⟩
192 Any (λ y → x ∈ f y) xs ∼⟨ Any-cong (λ x → f≈g x) xs≈ys ⟩
193 Any (λ y → x ∈ g y) ys ↔⟨ >>=↔ ⟩
194 x ∈ (ys >>= g) ∎
195 where open Related.EquationalReasoning
196
197
198 ------------------------------------------------------------------------
199 -- _⊛_
200
201 module _ {k} {A B : Set a} {fs gs : List (A → B)} {xs ys} where
202
203 ⊛-cong : fs ∼[ k ] gs → xs ∼[ k ] ys → (fs ⊛ xs) ∼[ k ] (gs ⊛ ys)
204 ⊛-cong fs≈gs xs≈ys {x} = begin
205 x ∈ (fs ⊛ xs)
206 ≡⟨ ≡.cong (x ∈_) (Applicative.unfold-⊛ fs xs) ⟩
207 x ∈ (fs >>= λ f → xs >>= λ x → pure (f x))
208 ∼⟨ >>=-cong fs≈gs (λ f → >>=-cong xs≈ys λ x → K-refl) ⟩
209 x ∈ (gs >>= λ g → ys >>= λ y → pure (g y))
210 ≡⟨ ≡.cong (x ∈_) (Applicative.unfold-⊛ gs ys) ⟨
211 x ∈ (gs ⊛ ys) ∎
212 where open Related.EquationalReasoning
213
214 ------------------------------------------------------------------------
215 -- _⊗_
216
217 module _ {ℓ k} {A B : Set ℓ} {xs₁ xs₂ : List A} {ys₁ ys₂ : List B} where
218
219 ⊗-cong : xs₁ ∼[ k ] xs₂ → ys₁ ∼[ k ] ys₂ →
220 (xs₁ ⊗ ys₁) ∼[ k ] (xs₂ ⊗ ys₂)
221 ⊗-cong xs₁≈xs₂ ys₁≈ys₂ =
222 ⊛-cong (map-cong (λ _ → refl) xs₁≈xs₂) ys₁≈ys₂
223
224 ------------------------------------------------------------------------
225 -- Other properties
226
227 -- _++_ and [] form a commutative monoid, with either bag or set
228 -- equality as the underlying equality.
229
230 commutativeMonoid : SymmetricKind → Set a → CommutativeMonoid _ _
231 commutativeMonoid {a} k A = record
232 { Carrier = List A
233 ; _≈_ = _∼[ ⌊ k ⌋ ]_
234 ; _∙_ = _++_
235 ; ε = []
236 ; isCommutativeMonoid = isCommutativeMonoidˡ record
237 { isSemigroup = record
238 { isMagma = record
239 { isEquivalence = Eq.isEquivalence
240 ; ∙-cong = ++-cong
241 }
242 ; assoc = λ xs ys zs →
243 Eq.reflexive (List.++-assoc xs ys zs)
244 }
245 ; identityˡ = λ xs → K-refl
246 ; comm = λ xs ys → ↔⇒ (++↔++ xs ys)
247 }
248 }
249 where open Related.EquationalReasoning
250
251 -- The only list which is bag or set equal to the empty list (or a
252 -- subset or subbag of the list) is the empty list itself.
253
254 empty-unique : ∀ {k} → xs ∼[ ⌊ k ⌋→ ] [] → xs ≡ []
255 empty-unique {xs = []} _ = refl
256 empty-unique {xs = _ ∷ _} ∷∼[] with () ← ⇒→ ∷∼[] (here refl)
257
258 -- _++_ is idempotent (under set equality).
259
260 ++-idempotent : Idempotent {A = List A} _∼[ set ]_ _++_
261 ++-idempotent xs {x} =
262 x ∈ xs ++ xs
263 ∼⟨ mk⇔ ([ id , id ]′ ∘ (Inverse.from $ ++↔)) (Inverse.to ++↔ ∘ inj₁) ⟩
264 x ∈ xs ∎
265 where open Related.EquationalReasoning
266
267 -- The list monad's bind distributes from the left over _++_.
268
269 >>=-left-distributive :
270 ∀ (xs : List A) {f g : A → List B} →
271 (xs >>= λ x → f x ++ g x) ∼[ bag ] (xs >>= f) ++ (xs >>= g)
272 >>=-left-distributive {ℓ} xs {f} {g} {y} =
273 y ∈ (xs >>= λ x → f x ++ g x) ↔⟨ SK-sym $ >>=↔ ⟩
274 Any (λ x → y ∈ f x ++ g x) xs ↔⟨ SK-sym (Any-cong (λ _ → ++↔) (_ ∎)) ⟩
275 Any (λ x → y ∈ f x ⊎ y ∈ g x) xs ↔⟨ SK-sym $ ⊎↔ ⟩
276 (Any (λ x → y ∈ f x) xs ⊎ Any (λ x → y ∈ g x) xs) ↔⟨ >>=↔ ⟨ _⊎-cong_ ⟩ >>=↔ ⟩
277 (y ∈ (xs >>= f) ⊎ y ∈ (xs >>= g)) ↔⟨ ++↔ ⟩
278 y ∈ (xs >>= f) ++ (xs >>= g) ∎
279 where open Related.EquationalReasoning
280
281 -- The same applies to _⊛_.
282
283 ⊛-left-distributive :
284 ∀ (fs : List (A → B)) xs₁ xs₂ →
285 (fs ⊛ (xs₁ ++ xs₂)) ∼[ bag ] (fs ⊛ xs₁) ++ (fs ⊛ xs₂)
286 ⊛-left-distributive {B = B} fs xs₁ xs₂ = begin
287 fs ⊛ (xs₁ ++ xs₂) ≡⟨ Applicative.unfold-⊛ fs (xs₁ ++ xs₂) ⟩
288 (fs >>= λ f → xs₁ ++ xs₂ >>= pure ∘ f) ≡⟨ (MP.cong (refl {x = fs}) λ f →
289 MP.right-distributive xs₁ xs₂ (pure ∘ f)) ⟩
290 (fs >>= λ f → (xs₁ >>= pure ∘ f) ++
291 (xs₂ >>= pure ∘ f)) ≈⟨ >>=-left-distributive fs ⟩
292
293 (fs >>= λ f → xs₁ >>= pure ∘ f) ++
294 (fs >>= λ f → xs₂ >>= pure ∘ f) ≡⟨ ≡.cong₂ _++_ (Applicative.unfold-⊛ fs xs₁) (Applicative.unfold-⊛ fs xs₂) ⟨
295
296 (fs ⊛ xs₁) ++ (fs ⊛ xs₂) ∎
297 where open ≈-Reasoning ([ bag ]-Equality B)
298
299 private
300
301 -- If x ∷ xs is set equal to x ∷ ys, then xs and ys are not
302 -- necessarily set equal.
303
304 ¬-drop-cons : ∀ {x : A} →
305 ¬ (∀ {xs ys} → x ∷ xs ∼[ set ] x ∷ ys → xs ∼[ set ] ys)
306 ¬-drop-cons {x = x} drop-cons with Equivalence.to x∼[] (here refl)
307 where
308 x,x≈x : (x ∷ x ∷ []) ∼[ set ] [ x ]
309 x,x≈x = ++-idempotent [ x ]
310
311 x∼[] : [ x ] ∼[ set ] []
312 x∼[] = drop-cons x,x≈x
313 ... | ()
314
315 -- However, the corresponding property does hold for bag equality.
316
317 drop-cons : ∀ {x : A} {xs ys} → x ∷ xs ∼[ bag ] x ∷ ys → xs ∼[ bag ] ys
318 drop-cons {x = x} {xs} {ys} x∷xs≈x∷ys =
319 ⊎-left-cancellative
320 (∼→⊎↔⊎ x∷xs≈x∷ys)
321 (lemma x∷xs≈x∷ys)
322 (lemma (SK-sym x∷xs≈x∷ys))
323
324 where
325
326 -- TODO: Some of the code below could perhaps be exposed to users.
327
328 -- List membership can be expressed as "there is an index which
329 -- points to the element".
330
331 ∈-index : ∀ {z} (xs : List A) → z ∈ xs ↔ ∃ λ i → z ≡ lookup xs i
332 ∈-index {z = z} [] =
333 z ∈ [] ↔⟨ SK-sym ⊥↔Any[] ⟩
334 ⊥ ↔⟨ mk↔ₛ′ (λ ()) (λ { (() , _) }) (λ { (() , _) }) (λ ()) ⟩
335 (∃ λ (i : Fin 0) → z ≡ lookup [] i) ∎
336 where
337 open Related.EquationalReasoning
338 ∈-index {z = z} (x ∷ xs) =
339 z ∈ x ∷ xs ↔⟨ SK-sym (∷↔ _) ⟩
340 (z ≡ x ⊎ z ∈ xs) ↔⟨ K-refl ⊎-cong ∈-index xs ⟩
341 (z ≡ x ⊎ ∃ λ i → z ≡ lookup xs i) ↔⟨ mk↔ₛ′ (λ { (inj₁ p) → zero , p; (inj₂ (i , p)) → suc i , p })
342 (λ { (zero , p) → inj₁ p; (suc i , p) → inj₂ (i , p) })
343 (λ { (zero , _) → refl; (suc _ , _) → refl })
344 (λ { (inj₁ _) → refl; (inj₂ _) → refl }) ⟩
345 (∃ λ i → z ≡ lookup (x ∷ xs) i) ∎
346 where
347 open Related.EquationalReasoning
348
349 -- The index which points to the element.
350
351 index-of : ∀ {a} {A : Set a} {z} {xs : List A} →
352 z ∈ xs → Fin (length xs)
353 index-of = proj₁ ∘ (Inverse.to (∈-index _))
354
355 -- The type ∃ λ z → z ∈ xs is isomorphic to Fin n, where n is the
356 -- length of xs.
357 --
358 -- Thierry Coquand pointed out that (a variant of) this statement is
359 -- a generalisation of the fact that singletons are contractible.
360
361 Fin-length : ∀ {a} {A : Set a}
362 (xs : List A) → (∃ λ z → z ∈ xs) ↔ Fin (length xs)
363 Fin-length xs =
364 (∃ λ z → z ∈ xs) ↔⟨ Σ.cong K-refl (∈-index xs) ⟩
365 (∃ λ z → ∃ λ i → z ≡ lookup xs i) ↔⟨ ∃∃↔∃∃ _ ⟩
366 (∃ λ i → ∃ λ z → z ≡ lookup xs i) ↔⟨ Σ.cong K-refl (mk↔ₛ′ _ (λ _ → _ , refl) (λ _ → refl) (λ { (_ , refl) → refl })) ⟩
367 (Fin (length xs) × ⊤) ↔⟨ ×-identityʳ _ _ ⟩
368 Fin (length xs) ∎
369 where
370 open Related.EquationalReasoning
371
372 -- From this lemma we get that lists which are bag equivalent have
373 -- related lengths.
374
375 Fin-length-cong : ∀ {a} {A : Set a} {xs ys : List A} →
376 xs ∼[ bag ] ys → Fin (length xs) ↔ Fin (length ys)
377 Fin-length-cong {xs = xs} {ys} xs≈ys =
378 Fin (length xs) ↔⟨ SK-sym $ Fin-length xs ⟩
379 ∃ (λ z → z ∈ xs) ↔⟨ Σ.cong K-refl xs≈ys ⟩
380 ∃ (λ z → z ∈ ys) ↔⟨ Fin-length ys ⟩
381 Fin (length ys) ∎
382 where
383 open Related.EquationalReasoning
384
385 -- The index-of function commutes with applications of certain
386 -- inverses.
387
388 index-of-commutes :
389 ∀ {a} {A : Set a} {z : A} {xs ys} →
390 (xs≈ys : xs ∼[ bag ] ys) (p : z ∈ xs) →
391 index-of (Inverse.to xs≈ys p) ≡
392 Inverse.to (Fin-length-cong xs≈ys) (index-of p)
393 index-of-commutes {z = z} {xs} {ys} xs≈ys p =
394 index-of (to xs≈ys p) ≡⟨ lemma z p ⟩
395
396 index-of (to xs≈ys (proj₂
397 (from (Fin-length xs) (to (Fin-length xs) (z , p))))) ≡⟨⟩
398
399 index-of (proj₂ (Product.map₂ (to xs≈ys)
400 (from (Fin-length xs) (to (Fin-length xs) (z , p))))) ≡⟨⟩
401
402 to (Fin-length ys) (Product.map₂ (to xs≈ys)
403 (from (Fin-length xs) (index-of p))) ≡⟨⟩
404
405 to (Fin-length-cong xs≈ys) (index-of p) ∎
406 where
407 open ≡-Reasoning
408 open Inverse
409
410 lemma :
411 ∀ z p →
412 index-of (to xs≈ys p) ≡
413 index-of (to xs≈ys (proj₂
414 (from (Fin-length xs) (to (Fin-length xs) (z , p)))))
415 lemma z p with to (Fin-length xs) (z , p)
416 | strictlyInverseʳ (Fin-length xs) (z , p)
417 lemma .(lookup xs i) .(from (∈-index xs) (i , refl)) | i | refl =
418 refl
419
420 -- Bag equivalence isomorphisms preserve index equality. Note that
421 -- this means that, even if the underlying equality is proof
422 -- relevant, a bag equivalence isomorphism cannot map two distinct
423 -- proofs, that point to the same position, to different positions.
424
425 index-equality-preserved :
426 ∀ {a} {A : Set a} {z : A} {xs ys} {p q : z ∈ xs}
427 (xs≈ys : xs ∼[ bag ] ys) →
428 index-of p ≡ index-of q →
429 index-of (Inverse.to xs≈ys p) ≡
430 index-of (Inverse.to xs≈ys q)
431 index-equality-preserved {p = p} {q} xs≈ys eq =
432 index-of (Inverse.to xs≈ys p) ≡⟨ index-of-commutes xs≈ys p ⟩
433 Inverse.to (Fin-length-cong xs≈ys) (index-of p) ≡⟨ ≡.cong (Inverse.to (Fin-length-cong xs≈ys)) eq ⟩
434 Inverse.to (Fin-length-cong xs≈ys) (index-of q) ≡⟨ ≡.sym $ index-of-commutes xs≈ys q ⟩
435 index-of (Inverse.to xs≈ys q) ∎
436 where
437 open ≡-Reasoning
438
439 -- The old inspect idiom.
440
441 inspect : ∀ {a} {A : Set a} (x : A) → ∃ (x ≡_)
442 inspect x = x , refl
443
444 -- A function is "well-behaved" if any "left" element which is the
445 -- image of a "right" element is in turn not mapped to another
446 -- "left" element.
447
448 Well-behaved : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} →
449 (A ⊎ B → A ⊎ C) → Set _
450 Well-behaved f =
451 ∀ {b a a′} → f (inj₂ b) ≡ inj₁ a → f (inj₁ a) ≢ inj₁ a′
452
453 -- The type constructor _⊎_ is left cancellative for certain
454 -- well-behaved inverses.
455
456 ⊎-left-cancellative :
457 ∀ {a b c} {A : Set a} {B : Set b} {C : Set c}
458 (f : (A ⊎ B) ↔ (A ⊎ C)) →
459 Well-behaved (Inverse.to f) →
460 Well-behaved (Inverse.from f) →
461 B ↔ C
462 ⊎-left-cancellative {A = A} = λ inv to-hyp from-hyp → mk↔ₛ′
463 (g (to inv) to-hyp)
464 (g (from inv) from-hyp)
465 (g∘g (SK-sym inv) from-hyp to-hyp)
466 (g∘g inv to-hyp from-hyp)
467 where
468 open Inverse
469
470 module _
471 {a b c} {A : Set a} {B : Set b} {C : Set c}
472 (f : A ⊎ B → A ⊎ C)
473 (hyp : Well-behaved f)
474 where
475
476 mutual
477
478 g : B → C
479 g b = g′ (inspect (f (inj₂ b)))
480
481 g′ : ∀ {b} → ∃ (f (inj₂ b) ≡_) → C
482 g′ (inj₂ c , _) = c
483 g′ (inj₁ a , eq) = g″ eq (inspect (f (inj₁ a)))
484
485 g″ : ∀ {a b} →
486 f (inj₂ b) ≡ inj₁ a → ∃ (f (inj₁ a) ≡_) → C
487 g″ _ (inj₂ c , _) = c
488 g″ eq₁ (inj₁ _ , eq₂) = contradiction eq₂ (hyp eq₁)
489
490 g∘g : ∀ {b c} {B : Set b} {C : Set c}
491 (f : (A ⊎ B) ↔ (A ⊎ C)) →
492 (to-hyp : Well-behaved (to f)) →
493 (from-hyp : Well-behaved (from f)) →
494 ∀ b → g (from f) from-hyp (g (to f) to-hyp b) ≡ b
495 g∘g f to-hyp from-hyp b = g∘g′
496 where
497 open ≡-Reasoning
498
499 g∘g′ : g (from f) from-hyp (g (to f) to-hyp b) ≡ b
500 g∘g′ with inspect (to f (inj₂ b))
501 g∘g′ | inj₂ c , eq₁ with inspect (from f (inj₂ c))
502 ... | inj₂ b′ , eq₂ = inj₂-injective (
503 inj₂ b′ ≡⟨ ≡.sym eq₂ ⟩
504 from f (inj₂ c) ≡⟨ to-from f eq₁ ⟩
505 inj₂ b ∎)
506 ... | inj₁ a , eq₂ with
507 inj₁ a ≡⟨ ≡.sym eq₂ ⟩
508 from f (inj₂ c) ≡⟨ to-from f eq₁ ⟩
509 inj₂ b ∎
510 ... | ()
511 g∘g′ | inj₁ a , eq₁ with inspect (to f (inj₁ a))
512 g∘g′ | inj₁ a , eq₁ | inj₁ a′ , eq₂ = contradiction eq₂ (to-hyp eq₁)
513 g∘g′ | inj₁ a , eq₁ | inj₂ c , eq₂ with inspect (from f (inj₂ c))
514 g∘g′ | inj₁ a , eq₁ | inj₂ c , eq₂ | inj₂ b′ , eq₃ with
515 inj₁ a ≡⟨ ≡.sym (to-from f eq₂) ⟩
516 from f (inj₂ c) ≡⟨ eq₃ ⟩
517 inj₂ b′ ∎
518 ... | ()
519 g∘g′ | inj₁ a , eq₁ | inj₂ c , eq₂ | inj₁ a′ , eq₃ with inspect (from f $ inj₁ a′)
520 g∘g′ | inj₁ a , eq₁ | inj₂ c , eq₂ | inj₁ a′ , eq₃ | inj₁ a″ , eq₄ = contradiction eq₄ (from-hyp eq₃)
521 g∘g′ | inj₁ a , eq₁ | inj₂ c , eq₂ | inj₁ a′ , eq₃ | inj₂ b′ , eq₄ = inj₂-injective (
522 let lemma =
523 inj₁ a′ ≡⟨ ≡.sym eq₃ ⟩
524 from f (inj₂ c) ≡⟨ to-from f eq₂ ⟩
525 inj₁ a ∎
526 in
527 inj₂ b′ ≡⟨ ≡.sym eq₄ ⟩
528 from f (inj₁ a′) ≡⟨ ≡.cong (from f ∘ inj₁) $ inj₁-injective lemma ⟩
529 from f (inj₁ a) ≡⟨ to-from f eq₁ ⟩
530 inj₂ b ∎)
531
532 -- Some final lemmas.
533
534 ∼→⊎↔⊎ :
535 ∀ {x : A} {xs ys} →
536 x ∷ xs ∼[ bag ] x ∷ ys →
537 ∀ {z} → (z ≡ x ⊎ z ∈ xs) ↔ (z ≡ x ⊎ z ∈ ys)
538 ∼→⊎↔⊎ {x = x} {xs} {ys} x∷xs≈x∷ys {z} =
539 (z ≡ x ⊎ z ∈ xs) ↔⟨ ∷↔ _ ⟩
540 z ∈ x ∷ xs ↔⟨ x∷xs≈x∷ys ⟩
541 z ∈ x ∷ ys ↔⟨ SK-sym (∷↔ _) ⟩
542 (z ≡ x ⊎ z ∈ ys) ∎
543 where
544 open Related.EquationalReasoning
545
546 lemma : ∀ {xs ys} (inv : x ∷ xs ∼[ bag ] x ∷ ys) {z} →
547 Well-behaved (Inverse.to (∼→⊎↔⊎ inv {z}))
548 lemma {xs} inv {b = z∈xs} {a = p} {a′ = q} hyp₁ hyp₂ = case contra of λ ()
549 where
550 open Inverse
551 open ≡-Reasoning
552 contra : zero ≡ suc (index-of {xs = xs} z∈xs)
553 contra = begin
554 zero
555 ≡⟨⟩
556 index-of {xs = x ∷ xs} (here p)
557 ≡⟨⟩
558 index-of {xs = x ∷ xs} (to (∷↔ _) $ inj₁ p)
559 ≡⟨ ≡.cong (index-of ∘ (to (∷↔ (_ ≡_)))) $ to-from (∼→⊎↔⊎ inv) {x = inj₁ p} hyp₂ ⟨
560 index-of {xs = x ∷ xs} (to (∷↔ _) $ (from (∼→⊎↔⊎ inv) $ inj₁ q))
561 ≡⟨ ≡.cong index-of $ strictlyInverseˡ (∷↔ _) (from inv (here q)) ⟩
562 index-of {xs = x ∷ xs} (to (SK-sym inv) $ here q)
563 ≡⟨ index-equality-preserved (SK-sym inv) refl ⟩
564 index-of {xs = x ∷ xs} (to (SK-sym inv) $ here p)
565 ≡⟨ ≡.cong index-of $ strictlyInverseˡ (∷↔ _) (from inv (here p)) ⟨
566 index-of {xs = x ∷ xs} (to (∷↔ _) (from (∼→⊎↔⊎ inv) $ inj₁ p))
567 ≡⟨ ≡.cong (index-of ∘ (to (∷↔ (_ ≡_)))) $ to-from (∼→⊎↔⊎ inv) {x = inj₂ z∈xs} hyp₁ ⟩
568 index-of {xs = x ∷ xs} (to (∷↔ _) $ inj₂ z∈xs)
569 ≡⟨⟩
570 index-of {xs = x ∷ xs} (there z∈xs)
571 ≡⟨⟩
572 suc (index-of {xs = xs} z∈xs)
573 ∎
574
575
576 ------------------------------------------------------------------------
577 -- Relationships to propositional permutation
578
579 ↭⇒∼bag : _↭_ {A = A} ⇒ _∼[ bag ]_
580 ↭⇒∼bag xs↭ys {v} =
581 mk↔ₛ′ (∈-resp-↭ xs↭ys) (∈-resp-↭ (↭-sym xs↭ys)) (∈-resp-[σ∘σ⁻¹] xs↭ys) (∈-resp-[σ⁻¹∘σ] xs↭ys)
582
583 ∼bag⇒↭ : _∼[ bag ]_ ⇒ _↭_ {A = A}
584 ∼bag⇒↭ {A = A} {[]} eq with refl ← empty-unique (↔-sym eq) = ↭-refl
585 ∼bag⇒↭ {A = A} {x ∷ xs} eq
586 with zs₁ , zs₂ , refl ← ∈-∃++ (Inverse.to (eq {x}) (here refl)) = begin
587 x ∷ xs ↭⟨ ↭-prep x (∼bag⇒↭ (drop-cons (↔-trans eq (comm zs₁ (x ∷ zs₂))))) ⟩
588 x ∷ (zs₂ ++ zs₁) ↭⟨ ↭-prep x (++-comm zs₂ zs₁) ⟩
589 x ∷ (zs₁ ++ zs₂) ↭⟨ shift x zs₁ zs₂ ⟨
590 zs₁ ++ x ∷ zs₂ ∎
591 where
592 open CommutativeMonoid (commutativeMonoid bag A)
593 open PermutationReasoning