Debian Codegraph @

src/Data/Container/Relation/Unary/Any/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Propertiers of any for containers
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Container.Relation.Unary.Any.Properties where
10
11 open import Algebra.Bundles using (CommutativeSemiring)
12 open import Data.Product.Base using (∃; _×_; ∃₂; _,_; proj₂)
13 open import Data.Product.Properties using (Σ-≡,≡→≡)
14 open import Data.Product.Function.NonDependent.Propositional using (_×-cong_)
15 import Data.Product.Function.Dependent.Propositional as Σ using (cong)
16 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_])
17 open import Function.Base using (_∘_; _∘′_; id; flip; _$_)
18 open import Function.Bundles using (_↔_; mk↔ₛ′; Inverse)
19 open import Function.Properties.Inverse using (↔-refl)
20 open import Function.Properties.Inverse.HalfAdjointEquivalence using (_≃_; ↔⇒≃)
21 open import Function.Related.Propositional as Related using (Related; SK-sym)
22 open import Function.Related.TypeIsomorphisms
23 using (×-⊎-commutativeSemiring; ∃∃↔∃∃; Σ-assoc; ×-comm)
24 open import Level using (Level; _⊔_)
25 open import Relation.Unary using (Pred ; _∪_ ; _∩_)
26 open import Relation.Binary.Core using (REL)
27 open import Relation.Binary.PropositionalEquality.Core as ≡
28 using (_≡_; _≗_; refl)
29 open import Relation.Binary.PropositionalEquality.Properties as ≡
30
31 private
32 module ×⊎ {k ℓ} = CommutativeSemiring (×-⊎-commutativeSemiring k ℓ)
33
34
35 open import Data.Container.Core
36 import Data.Container.Combinator as C
37 open import Data.Container.Combinator.Properties
38 open import Data.Container.Related
39 open import Data.Container.Relation.Unary.Any as Any using (◇; any)
40 open import Data.Container.Membership
41
42 module _ {s p} (C : Container s p) {x} {X : Set x} {ℓ} {P : Pred X ℓ} where
43
44 -- ◇ can be unwrapped to reveal the Σ type
45
46 ↔Σ : ∀ {xs : ⟦ C ⟧ X} → ◇ C P xs ↔ ∃ λ p → P (proj₂ xs p)
47 ↔Σ {xs} = mk↔ₛ′ ◇.proof any (λ _ → refl) (λ _ → refl)
48
49 -- ◇ can be expressed using _∈_.
50
51 ↔∈ : ∀ {xs : ⟦ C ⟧ X} → ◇ C P xs ↔ (∃ λ x → x ∈ xs × P x)
52 ↔∈ {xs} = mk↔ₛ′ to from to∘from (λ _ → refl) where
53
54 to : ◇ C P xs → ∃ λ x → x ∈ xs × P x
55 to (any (p , Px)) = (proj₂ xs p , (any (p , refl)) , Px)
56
57 from : (∃ λ x → x ∈ xs × P x) → ◇ C P xs
58 from (.(proj₂ xs p) , (any (p , refl)) , Px) = any (p , Px)
59
60 to∘from : to ∘ from ≗ id
61 to∘from (.(proj₂ xs p) , any (p , refl) , Px) = refl
62
63 module _ {s p} {C : Container s p} {x} {X : Set x}
64 {ℓ₁ ℓ₂} {P₁ : Pred X ℓ₁} {P₂ : Pred X ℓ₂} where
65 -- ◇ is a congruence for bag and set equality and related preorders.
66
67 cong : ∀ {k} {xs₁ xs₂ : ⟦ C ⟧ X} →
68 (∀ x → Related k (P₁ x) (P₂ x)) → xs₁ ≲[ k ] xs₂ →
69 Related k (◇ C P₁ xs₁) (◇ C P₂ xs₂)
70 cong {k} {xs₁} {xs₂} P₁↔P₂ xs₁≈xs₂ =
71 ◇ C P₁ xs₁ ↔⟨ ↔∈ C ⟩
72 (∃ λ x → x ∈ xs₁ × P₁ x) ∼⟨ Σ.cong ↔-refl (xs₁≈xs₂ ×-cong P₁↔P₂ _) ⟩
73 (∃ λ x → x ∈ xs₂ × P₂ x) ↔⟨ SK-sym (↔∈ C) ⟩
74 ◇ C P₂ xs₂ ∎
75 where open Related.EquationalReasoning
76
77 -- Nested occurrences of ◇ can sometimes be swapped.
78
79 module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s₂ p₂}
80 {x y} {X : Set x} {Y : Set y} {r} {P : REL X Y r} where
81
82 swap : {xs : ⟦ C₁ ⟧ X} {ys : ⟦ C₂ ⟧ Y} →
83 let ◈ : ∀ {s p} {C : Container s p} {x} {X : Set x} {ℓ} → ⟦ C ⟧ X → Pred X ℓ → Set (p ⊔ ℓ)
84 ◈ = λ {_} {_} → flip (◇ _) in
85 ◈ xs (◈ ys ∘ P) ↔ ◈ ys (◈ xs ∘ flip P)
86 swap {xs} {ys} =
87 ◇ _ (λ x → ◇ _ (P x) ys) xs ↔⟨ ↔∈ C₁ ⟩
88 (∃ λ x → x ∈ xs × ◇ _ (P x) ys) ↔⟨ Σ.cong ↔-refl $ Σ.cong ↔-refl $ ↔∈ C₂ ⟩
89 (∃ λ x → x ∈ xs × ∃ λ y → y ∈ ys × P x y) ↔⟨ Σ.cong ↔-refl (λ {x} → ∃∃↔∃∃ (λ _ y → y ∈ ys × P x y)) ⟩
90 (∃₂ λ x y → x ∈ xs × y ∈ ys × P x y) ↔⟨ ∃∃↔∃∃ (λ x y → x ∈ xs × y ∈ ys × P x y) ⟩
91 (∃₂ λ y x → x ∈ xs × y ∈ ys × P x y) ↔⟨ Σ.cong ↔-refl (λ {y} → Σ.cong ↔-refl (λ {x} →
92 (x ∈ xs × y ∈ ys × P x y) ↔⟨ SK-sym Σ-assoc ⟩
93 ((x ∈ xs × y ∈ ys) × P x y) ↔⟨ Σ.cong (×-comm _ _) ↔-refl ⟩
94 ((y ∈ ys × x ∈ xs) × P x y) ↔⟨ Σ-assoc ⟩
95 (y ∈ ys × x ∈ xs × P x y) ∎)) ⟩
96 (∃₂ λ y x → y ∈ ys × x ∈ xs × P x y) ↔⟨ Σ.cong ↔-refl (λ {y} → ∃∃↔∃∃ {B = y ∈ ys} (λ x _ → x ∈ xs × P x y)) ⟩
97 (∃ λ y → y ∈ ys × ∃ λ x → x ∈ xs × P x y) ↔⟨ Σ.cong ↔-refl (Σ.cong ↔-refl (SK-sym (↔∈ C₁))) ⟩
98 (∃ λ y → y ∈ ys × ◇ _ (flip P y) xs) ↔⟨ SK-sym (↔∈ C₂) ⟩
99 ◇ _ (λ y → ◇ _ (flip P y) xs) ys ∎
100 where open Related.EquationalReasoning
101
102 -- Nested occurrences of ◇ can sometimes be flattened.
103
104 module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s₂ p₂}
105 {x} {X : Set x} {ℓ} (P : Pred X ℓ) where
106
107 flatten : ∀ (xss : ⟦ C₁ ⟧ (⟦ C₂ ⟧ X)) →
108 ◇ C₁ (◇ C₂ P) xss ↔
109 ◇ (C₁ C.∘ C₂) P (Inverse.from (Composition.correct C₁ C₂) xss)
110 flatten xss = mk↔ₛ′ t f (λ _ → refl) (λ _ → refl) where
111
112 ◇₁ = ◇ C₁; ◇₂ = ◇ C₂; ◇₁₂ = ◇ (C₁ C.∘ C₂)
113 open Inverse
114
115 t : ◇₁ (◇₂ P) xss → ◇₁₂ P (from (Composition.correct C₁ C₂) xss)
116 t (any (p₁ , (any (p₂ , p)))) = any (any (p₁ , p₂) , p)
117
118 f : ◇₁₂ P (from (Composition.correct C₁ C₂) xss) → ◇₁ (◇₂ P) xss
119 f (any (any (p₁ , p₂) , p)) = any (p₁ , any (p₂ , p))
120
121 -- Sums commute with ◇ (for a fixed instance of a given container).
122
123 module _ {s p} {C : Container s p} {x} {X : Set x}
124 {ℓ ℓ′} {P : Pred X ℓ} {Q : Pred X ℓ′} where
125
126 ◇⊎↔⊎◇ : ∀ {xs : ⟦ C ⟧ X} → ◇ C (P ∪ Q) xs ↔ (◇ C P xs ⊎ ◇ C Q xs)
127 ◇⊎↔⊎◇ {xs} = mk↔ₛ′ to from to∘from from∘to
128 where
129 to : ◇ C (λ x → P x ⊎ Q x) xs → ◇ C P xs ⊎ ◇ C Q xs
130 to (any (pos , inj₁ p)) = inj₁ (any (pos , p))
131 to (any (pos , inj₂ q)) = inj₂ (any (pos , q))
132
133 from : ◇ C P xs ⊎ ◇ C Q xs → ◇ C (λ x → P x ⊎ Q x) xs
134 from = [ Any.map₂ inj₁ , Any.map₂ inj₂ ]
135
136 from∘to : from ∘ to ≗ id
137 from∘to (any (pos , inj₁ p)) = refl
138 from∘to (any (pos , inj₂ q)) = refl
139
140 to∘from : to ∘ from ≗ id
141 to∘from = [ (λ _ → refl) , (λ _ → refl) ]
142
143 -- Products "commute" with ◇.
144
145 module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s₂ p₂}
146 {x y} {X : Set x} {Y : Set y} {ℓ ℓ′} {P : Pred X ℓ} {Q : Pred Y ℓ′} where
147
148 ×◇↔◇◇× : ∀ {xs : ⟦ C₁ ⟧ X} {ys : ⟦ C₂ ⟧ Y} →
149 ◇ C₁ (λ x → ◇ C₂ (λ y → P x × Q y) ys) xs ↔ (◇ C₁ P xs × ◇ C₂ Q ys)
150 ×◇↔◇◇× {xs} {ys} = mk↔ₛ′ to from (λ _ → refl) (λ _ → refl)
151 where
152 ◇₁ = ◇ C₁; ◇₂ = ◇ C₂
153
154 to : ◇₁ (λ x → ◇₂ (λ y → P x × Q y) ys) xs → ◇₁ P xs × ◇₂ Q ys
155 to (any (p₁ , any (p₂ , p , q))) = (any (p₁ , p) , any (p₂ , q))
156
157 from : ◇₁ P xs × ◇₂ Q ys → ◇₁ (λ x → ◇₂ (λ y → P x × Q y) ys) xs
158 from (any (p₁ , p) , any (p₂ , q)) = any (p₁ , any (p₂ , p , q))
159
160 -- map can be absorbed by the predicate.
161
162 module _ {s p} (C : Container s p) {x y} {X : Set x} {Y : Set y}
163 {ℓ} (P : Pred Y ℓ) where
164
165 map↔∘ : ∀ {xs : ⟦ C ⟧ X} (f : X → Y) → ◇ C P (map f xs) ↔ ◇ C (P ∘′ f) xs
166 map↔∘ {xs} f =
167 ◇ C P (map f xs) ↔⟨ ↔Σ C ⟩
168 ∃ (P ∘′ proj₂ (map f xs)) ≡⟨⟩
169 ∃ (P ∘′ f ∘′ proj₂ xs) ↔⟨ SK-sym (↔Σ C) ⟩
170 ◇ C (P ∘′ f) xs ∎
171 where open Related.EquationalReasoning
172
173 -- Membership in a mapped container can be expressed without reference
174 -- to map.
175
176 module _ {s p} (C : Container s p) {x y} {X : Set x} {Y : Set y}
177 {ℓ} (P : Pred Y ℓ) where
178
179 ∈map↔∈×≡ : ∀ {f : X → Y} {xs : ⟦ C ⟧ X} {y} →
180 y ∈ map f xs ↔ (∃ λ x → x ∈ xs × y ≡ f x)
181 ∈map↔∈×≡ {f = f} {xs} {y} =
182 y ∈ map f xs ↔⟨ map↔∘ C (y ≡_) f ⟩
183 ◇ C (λ x → y ≡ f x) xs ↔⟨ ↔∈ C ⟩
184 ∃ (λ x → x ∈ xs × y ≡ f x) ∎
185 where open Related.EquationalReasoning
186
187 -- map is a congruence for bag and set equality and related preorders.
188
189 module _ {s p} (C : Container s p) {x y} {X : Set x} {Y : Set y}
190 {ℓ} (P : Pred Y ℓ) where
191
192 map-cong : ∀ {k} {f₁ f₂ : X → Y} {xs₁ xs₂ : ⟦ C ⟧ X} →
193 f₁ ≗ f₂ → xs₁ ≲[ k ] xs₂ →
194 map f₁ xs₁ ≲[ k ] map f₂ xs₂
195 map-cong {f₁ = f₁} {f₂} {xs₁} {xs₂} f₁≗f₂ xs₁≈xs₂ {x} =
196 x ∈ map f₁ xs₁ ↔⟨ map↔∘ C (_≡_ x) f₁ ⟩
197 ◇ C (λ y → x ≡ f₁ y) xs₁ ∼⟨ cong (Related.↔⇒ ∘ helper) xs₁≈xs₂ ⟩
198 ◇ C (λ y → x ≡ f₂ y) xs₂ ↔⟨ SK-sym (map↔∘ C (_≡_ x) f₂) ⟩
199 x ∈ map f₂ xs₂ ∎
200 where
201 open Related.EquationalReasoning
202 helper : ∀ y → (x ≡ f₁ y) ↔ (x ≡ f₂ y)
203 helper y rewrite f₁≗f₂ y = ↔-refl
204
205 -- Uses of linear morphisms can be removed.
206
207 module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s₂ p₂}
208 {x} {X : Set x} {ℓ} (P : Pred X ℓ) where
209
210 remove-linear : ∀ {xs : ⟦ C₁ ⟧ X} (m : C₁ ⊸ C₂) → ◇ C₂ P (⟪ m ⟫⊸ xs) ↔ ◇ C₁ P xs
211 remove-linear {xs} m = mk↔ₛ′ t f t∘f f∘t
212 where
213 open _≃_
214 open ≡.≡-Reasoning
215
216 position⊸m : ∀ {s} → Position C₂ (shape⊸ m s) ≃ Position C₁ s
217 position⊸m = ↔⇒≃ (position⊸ m)
218
219 ◇₁ = ◇ C₁; ◇₂ = ◇ C₂
220
221 t : ◇₂ P (⟪ m ⟫⊸ xs) → ◇₁ P xs
222 t = Any.map₁ (_⊸_.morphism m)
223
224 f : ◇₁ P xs → ◇₂ P (⟪ m ⟫⊸ xs)
225 f (any (x , p)) =
226 any $ from position⊸m x
227 , ≡.subst (P ∘′ proj₂ xs) (≡.sym (right-inverse-of position⊸m _)) p
228
229 f∘t : f ∘ t ≗ id
230 f∘t (any (p₂ , p)) = ≡.cong any $ Σ-≡,≡→≡
231 ( left-inverse-of position⊸m p₂
232 , (≡.subst (P ∘ proj₂ xs ∘ to position⊸m)
233 (left-inverse-of position⊸m p₂)
234 (≡.subst (P ∘ proj₂ xs)
235 (≡.sym (right-inverse-of position⊸m
236 (to position⊸m p₂)))
237 p) ≡⟨ ≡.subst-∘ (left-inverse-of position⊸m _) ⟩
238
239 ≡.subst (P ∘ proj₂ xs)
240 (≡.cong (to position⊸m)
241 (left-inverse-of position⊸m p₂))
242 (≡.subst (P ∘ proj₂ xs)
243 (≡.sym (right-inverse-of position⊸m
244 (to position⊸m p₂)))
245 p) ≡⟨ ≡.cong (λ eq → ≡.subst (P ∘ proj₂ xs) eq
246 (≡.subst (P ∘ proj₂ xs)
247 (≡.sym (right-inverse-of position⊸m _)) _))
248 (_≃_.left-right position⊸m _) ⟩
249 ≡.subst (P ∘ proj₂ xs)
250 (right-inverse-of position⊸m
251 (to position⊸m p₂))
252 (≡.subst (P ∘ proj₂ xs)
253 (≡.sym (right-inverse-of position⊸m
254 (to position⊸m p₂)))
255 p) ≡⟨ ≡.subst-subst (≡.sym (right-inverse-of position⊸m _)) ⟩
256
257 ≡.subst (P ∘ proj₂ xs)
258 (≡.trans
259 (≡.sym (right-inverse-of position⊸m
260 (to position⊸m p₂)))
261 (right-inverse-of position⊸m
262 (to position⊸m p₂)))
263 p ≡⟨ ≡.cong (λ eq → ≡.subst (P ∘ proj₂ xs) eq p)
264 (≡.trans-symˡ (right-inverse-of position⊸m _)) ⟩
265
266 ≡.subst (P ∘ proj₂ xs) ≡.refl p ≡⟨⟩
267
268 p ∎)
269 )
270
271 t∘f : t ∘ f ≗ id
272 t∘f (any (p₁ , p)) = ≡.cong any $ Σ-≡,≡→≡
273 ( right-inverse-of position⊸m p₁
274 , (≡.subst (P ∘ proj₂ xs)
275 (right-inverse-of position⊸m p₁)
276 (≡.subst (P ∘ proj₂ xs)
277 (≡.sym (right-inverse-of position⊸m p₁))
278 p) ≡⟨ ≡.subst-subst (≡.sym (right-inverse-of position⊸m _)) ⟩
279
280 ≡.subst (P ∘ proj₂ xs)
281 (≡.trans
282 (≡.sym (right-inverse-of position⊸m p₁))
283 (right-inverse-of position⊸m p₁))
284 p ≡⟨ ≡.cong (λ eq → ≡.subst (P ∘ proj₂ xs) eq p)
285 (≡.trans-symˡ (right-inverse-of position⊸m _)) ⟩
286 ≡.subst (P ∘ proj₂ xs) ≡.refl p ≡⟨⟩
287
288 p ∎)
289 )
290
291 -- Linear endomorphisms are identity functions if bag equality is used.
292
293 module _ {s p} {C : Container s p} {x} {X : Set x} where
294
295 linear-identity : ∀ {xs : ⟦ C ⟧ X} (m : C ⊸ C) → ⟪ m ⟫⊸ xs ≲[ bag ] xs
296 linear-identity {xs} m {x} =
297 x ∈ ⟪ m ⟫⊸ xs ↔⟨ remove-linear (_≡_ x) m ⟩
298 x ∈ xs ∎
299 where open Related.EquationalReasoning
300
301 -- If join can be expressed using a linear morphism (in a certain
302 -- way), then it can be absorbed by the predicate.
303
304 module _ {s₁ s₂ s₃ p₁ p₂ p₃}
305 {C₁ : Container s₁ p₁} {C₂ : Container s₂ p₂} {C₃ : Container s₃ p₃}
306 {x} {X : Set x} {ℓ} (P : Pred X ℓ) where
307
308 join↔◇ : (join′ : (C₁ C.∘ C₂) ⊸ C₃) (xss : ⟦ C₁ ⟧ (⟦ C₂ ⟧ X)) →
309 let join : ∀ {X} → ⟦ C₁ ⟧ (⟦ C₂ ⟧ X) → ⟦ C₃ ⟧ X
310 join = λ {_} → ⟪ join′ ⟫⊸ ∘
311 (Inverse.from (Composition.correct C₁ C₂)) in
312 ◇ C₃ P (join xss) ↔ ◇ C₁ (◇ C₂ P) xss
313 join↔◇ join xss =
314 ◇ C₃ P (⟪ join ⟫⊸ xss′) ↔⟨ remove-linear P join ⟩
315 ◇ (C₁ C.∘ C₂) P xss′ ↔⟨ SK-sym $ flatten P xss ⟩
316 ◇ C₁ (◇ C₂ P) xss ∎
317 where
318 open Related.EquationalReasoning
319 xss′ = Inverse.from (Composition.correct C₁ C₂) xss