Debian Codegraph @

src/Data/List/Relation/Binary/Subset/Setoid/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the extensional sublist relation over setoid equality.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Binary.Subset.Setoid.Properties where
10
11 open import Data.Bool.Base using (Bool; true; false)
12 open import Data.List.Base hiding (_∷ʳ_; find)
13 import Data.List.Properties as List
14 open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
15 open import Data.List.Relation.Unary.All as All using (All)
16 import Data.List.Membership.Setoid as Membership
17 import Data.List.Membership.Setoid.Properties as Membershipₚ
18 open import Data.Nat.Base using (ℕ; s≤s; _≤_)
19 import Data.List.Relation.Binary.Subset.Setoid as Subset
20 import Data.List.Relation.Binary.Sublist.Setoid as Sublist
21 import Data.List.Relation.Binary.Equality.Setoid as Equality
22 import Data.List.Relation.Binary.Permutation.Setoid as Permutation
23 import Data.List.Relation.Binary.Permutation.Setoid.Properties as Permutationₚ
24 open import Data.Product.Base using (_,_)
25 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
26 open import Function.Base using (_∘_; _∘₂_; _$_; case_of_)
27 open import Level using (Level)
28 open import Relation.Nullary using (¬_; does; yes; no)
29 open import Relation.Nullary.Negation using (contradiction)
30 open import Relation.Unary using (Pred; Decidable) renaming (_⊆_ to _⋐_)
31 open import Relation.Binary.Core using (_⇒_; _Preserves_⟶_)
32 open import Relation.Binary.Definitions
33 using (Reflexive; Transitive; _Respectsʳ_; _Respectsˡ_; _Respects_)
34 open import Relation.Binary.Bundles using (Setoid; Preorder)
35 open import Relation.Binary.Structures using (IsPreorder)
36 import Relation.Binary.Reasoning.Preorder as ≲-Reasoning
37 open import Relation.Binary.Reasoning.Syntax
38 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
39
40 open Setoid using (Carrier)
41
42 private
43 variable
44 a b p q r ℓ : Level
45
46 ------------------------------------------------------------------------
47 -- Basics
48 ------------------------------------------------------------------------
49
50 module _ (S : Setoid a ℓ) where
51
52 open Setoid S using (refl)
53 open Subset S
54
55 ∷⊈[] : ∀ {x xs} → x ∷ xs ⊈ []
56 ∷⊈[] p = Membershipₚ.∉[] S $ p (here refl)
57
58 ⊆[]⇒≡[] : (_⊆ []) ⋐ (_≡ [])
59 ⊆[]⇒≡[] {x = []} _ = ≡.refl
60 ⊆[]⇒≡[] {x = _ ∷ _} p with () ← ∷⊈[] p
61
62 ------------------------------------------------------------------------
63 -- Relational properties with _≋_ (pointwise equality)
64 ------------------------------------------------------------------------
65
66 module _ (S : Setoid a ℓ) where
67
68 open Subset S
69 open Equality S
70 open Membership S
71 open Membershipₚ
72
73 ⊆-reflexive : _≋_ ⇒ _⊆_
74 ⊆-reflexive xs≋ys = ∈-resp-≋ S xs≋ys
75
76 ⊆-refl : Reflexive _⊆_
77 ⊆-refl x∈xs = x∈xs
78
79 ⊆-trans : Transitive _⊆_
80 ⊆-trans xs⊆ys ys⊆zs x∈xs = ys⊆zs (xs⊆ys x∈xs)
81
82 ⊆-respʳ-≋ : _⊆_ Respectsʳ _≋_
83 ⊆-respʳ-≋ xs≋ys = ∈-resp-≋ S xs≋ys ∘_
84
85 ⊆-respˡ-≋ : _⊆_ Respectsˡ _≋_
86 ⊆-respˡ-≋ xs≋ys = _∘ ∈-resp-≋ S (≋-sym xs≋ys)
87
88 ⊆-isPreorder : IsPreorder _≋_ _⊆_
89 ⊆-isPreorder = record
90 { isEquivalence = ≋-isEquivalence
91 ; reflexive = ⊆-reflexive
92 ; trans = ⊆-trans
93 }
94
95 ⊆-preorder : Preorder _ _ _
96 ⊆-preorder = record
97 { isPreorder = ⊆-isPreorder
98 }
99
100 ------------------------------------------------------------------------
101 -- Relational properties with _↭_ (permutations)
102 ------------------------------------------------------------------------
103
104 module _ (S : Setoid a ℓ) where
105
106 open Subset S
107 open Permutation S
108 open Membership S
109
110 ⊆-reflexive-↭ : _↭_ ⇒ _⊆_
111 ⊆-reflexive-↭ xs↭ys = Permutationₚ.∈-resp-↭ S xs↭ys
112
113 ⊆-respʳ-↭ : _⊆_ Respectsʳ _↭_
114 ⊆-respʳ-↭ xs↭ys = Permutationₚ.∈-resp-↭ S xs↭ys ∘_
115
116 ⊆-respˡ-↭ : _⊆_ Respectsˡ _↭_
117 ⊆-respˡ-↭ xs↭ys = _∘ Permutationₚ.∈-resp-↭ S (↭-sym xs↭ys)
118
119 ⊆-↭-isPreorder : IsPreorder _↭_ _⊆_
120 ⊆-↭-isPreorder = record
121 { isEquivalence = ↭-isEquivalence
122 ; reflexive = ⊆-reflexive-↭
123 ; trans = ⊆-trans S
124 }
125
126 ⊆-↭-preorder : Preorder _ _ _
127 ⊆-↭-preorder = record
128 { isPreorder = ⊆-↭-isPreorder
129 }
130
131 ------------------------------------------------------------------------
132 -- Reasoning over subsets
133 ------------------------------------------------------------------------
134
135 module ⊆-Reasoning (S : Setoid a ℓ) where
136 open Membership S using (_∈_)
137
138 private module Base = ≲-Reasoning (⊆-preorder S)
139
140 open Base public
141 hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨; step-≲; step-∼)
142 renaming (≲-go to ⊆-go; ≈-go to ≋-go)
143
144 open begin-membership-syntax _IsRelatedTo_ _∈_ (λ x → Base.begin x) public
145 open ⊆-syntax _IsRelatedTo_ _IsRelatedTo_ ⊆-go public
146 open ≋-syntax _IsRelatedTo_ _IsRelatedTo_ ≋-go public
147
148 ------------------------------------------------------------------------
149 -- Relationship with other binary relations
150 ------------------------------------------------------------------------
151
152 module _ (S : Setoid a ℓ) where
153
154 open Setoid S
155 open Subset S
156 open Sublist S renaming (_⊆_ to _⊑_)
157
158 Sublist⇒Subset : ∀ {xs ys} → xs ⊑ ys → xs ⊆ ys
159 Sublist⇒Subset (x≈y ∷ xs⊑ys) (here v≈x) = here (trans v≈x x≈y)
160 Sublist⇒Subset (x≈y ∷ xs⊑ys) (there v∈xs) = there (Sublist⇒Subset xs⊑ys v∈xs)
161 Sublist⇒Subset (y ∷ʳ xs⊑ys) v∈xs = there (Sublist⇒Subset xs⊑ys v∈xs)
162
163 ------------------------------------------------------------------------
164 -- Relationship with predicates
165 ------------------------------------------------------------------------
166
167 module _ (S : Setoid a ℓ) where
168
169 open Setoid S renaming (Carrier to A)
170 open Subset S
171 open Membership S
172
173 Any-resp-⊆ : ∀ {P : Pred A p} → P Respects _≈_ → (Any P) Respects _⊆_
174 Any-resp-⊆ resp ⊆ pxs with find pxs
175 ... | (x , x∈xs , px) = lose resp (⊆ x∈xs) px
176
177 All-resp-⊇ : ∀ {P : Pred A p} → P Respects _≈_ → (All P) Respects _⊇_
178 All-resp-⊇ resp ⊇ pxs = All.tabulateₛ S (All.lookupₛ S resp pxs ∘ ⊇)
179
180 ------------------------------------------------------------------------
181 -- Properties of list functions
182 ------------------------------------------------------------------------
183
184 ------------------------------------------------------------------------
185 -- ∷
186
187 module _ (S : Setoid a ℓ) where
188
189 open Setoid S renaming (Carrier to A)
190 open Subset S
191 open Membership S
192 open Membershipₚ
193
194 xs⊆x∷xs : ∀ xs x → xs ⊆ x ∷ xs
195 xs⊆x∷xs xs x = there
196
197 private variable
198 x y : A
199 xs ys : List A
200
201 ∷⁺ʳ : ∀ x → xs ⊆ ys → x ∷ xs ⊆ x ∷ ys
202 ∷⁺ʳ x xs⊆ys (here p) = here p
203 ∷⁺ʳ x xs⊆ys (there p) = there (xs⊆ys p)
204
205 ∈-∷⁺ʳ : x ∈ ys → xs ⊆ ys → x ∷ xs ⊆ ys
206 ∈-∷⁺ʳ x∈ys _ (here v≈x) = ∈-resp-≈ S (sym v≈x) x∈ys
207 ∈-∷⁺ʳ _ xs⊆ys (there x∈xs) = xs⊆ys x∈xs
208
209 ⊆∷⇒∈∨⊆ : xs ⊆ y ∷ ys → y ∈ xs ⊎ xs ⊆ ys
210 ⊆∷⇒∈∨⊆ {xs = []} []⊆y∷ys = inj₂ λ ()
211 ⊆∷⇒∈∨⊆ {xs = _ ∷ _} x∷xs⊆y∷ys with ⊆∷⇒∈∨⊆ $ x∷xs⊆y∷ys ∘ there
212 ... | inj₁ y∈xs = inj₁ $ there y∈xs
213 ... | inj₂ xs⊆ys with x∷xs⊆y∷ys (here refl)
214 ... | here x≈y = inj₁ $ here (sym x≈y)
215 ... | there x∈ys = inj₂ (∈-∷⁺ʳ x∈ys xs⊆ys)
216
217 ⊆∷∧∉⇒⊆ : xs ⊆ y ∷ ys → y ∉ xs → xs ⊆ ys
218 ⊆∷∧∉⇒⊆ xs⊆y∷ys y∉xs with ⊆∷⇒∈∨⊆ xs⊆y∷ys
219 ... | inj₁ y∈xs = contradiction y∈xs y∉xs
220 ... | inj₂ xs⊆ys = xs⊆ys
221
222 ------------------------------------------------------------------------
223 -- ++
224
225 module _ (S : Setoid a ℓ) where
226
227 open Subset S
228 open Membership S
229 open Membershipₚ
230
231 xs⊆xs++ys : ∀ xs ys → xs ⊆ xs ++ ys
232 xs⊆xs++ys xs ys = ∈-++⁺ˡ S
233
234 xs⊆ys++xs : ∀ xs ys → xs ⊆ ys ++ xs
235 xs⊆ys++xs xs ys = ∈-++⁺ʳ S _
236
237 ++⁺ʳ : ∀ {xs ys} zs → xs ⊆ ys → zs ++ xs ⊆ zs ++ ys
238 ++⁺ʳ [] xs⊆ys = xs⊆ys
239 ++⁺ʳ (x ∷ zs) xs⊆ys (here p) = here p
240 ++⁺ʳ (x ∷ zs) xs⊆ys (there p) = there (++⁺ʳ zs xs⊆ys p)
241
242 ++⁺ˡ : ∀ {xs ys} zs → xs ⊆ ys → xs ++ zs ⊆ ys ++ zs
243 ++⁺ˡ {[]} {ys} zs xs⊆ys = xs⊆ys++xs zs ys
244 ++⁺ˡ {x ∷ xs} {ys} zs xs⊆ys (here p) = xs⊆xs++ys ys zs (xs⊆ys (here p))
245 ++⁺ˡ {x ∷ xs} {ys} zs xs⊆ys (there p) = ++⁺ˡ zs (xs⊆ys ∘ there) p
246
247 ++⁺ : ∀ {ws xs ys zs} → ws ⊆ xs → ys ⊆ zs → ws ++ ys ⊆ xs ++ zs
248 ++⁺ ws⊆xs ys⊆zs = ⊆-trans S (++⁺ˡ _ ws⊆xs) (++⁺ʳ _ ys⊆zs)
249
250 ------------------------------------------------------------------------
251 -- map
252
253 module _ (S : Setoid a ℓ) (R : Setoid b r) where
254
255 private
256 module S = Setoid S
257 module R = Setoid R
258
259 module S⊆ = Subset S
260 module R⊆ = Subset R
261
262 open Membershipₚ
263
264 map⁺ : ∀ {as bs} {f : S.Carrier → R.Carrier} →
265 f Preserves S._≈_ ⟶ R._≈_ →
266 as S⊆.⊆ bs → map f as R⊆.⊆ map f bs
267 map⁺ {f = f} f-pres as⊆bs v∈f[as] =
268 let x , x∈as , v≈f[x] = ∈-map⁻ S R v∈f[as] in
269 ∈-resp-≈ R (R.sym v≈f[x]) (∈-map⁺ S R f-pres (as⊆bs x∈as))
270
271 ------------------------------------------------------------------------
272 -- reverse
273
274 module _ (S : Setoid a ℓ) where
275
276 open Setoid S renaming (Carrier to A)
277 open Subset S
278
279 reverse-selfAdjoint : ∀ {as bs} → as ⊆ reverse bs → reverse as ⊆ bs
280 reverse-selfAdjoint rs = reverse⁻ ∘ rs ∘ reverse⁻
281 where reverse⁻ = Membershipₚ.reverse⁻ S
282
283 -- NB. the unit and counit of this adjunction are given by:
284 -- reverse-η : ∀ {xs} → xs ⊆ reverse xs
285 -- reverse-η = Membershipₚ.reverse⁺ S
286 -- reverse-ε : ∀ {xs} → reverse xs ⊆ xs
287 -- reverse-ε = Membershipₚ.reverse⁻ S
288
289 reverse⁺ : ∀ {as bs} → as ⊆ bs → reverse as ⊆ reverse bs
290 reverse⁺ {as} {bs} rs = reverse-selfAdjoint $ begin
291 as ⊆⟨ rs ⟩
292 bs ≡⟨ List.reverse-involutive bs ⟨
293 reverse (reverse bs) ∎
294 where open ⊆-Reasoning S
295
296 reverse⁻ : ∀ {as bs} → reverse as ⊆ reverse bs → as ⊆ bs
297 reverse⁻ {as} {bs} rs = begin
298 as ≡⟨ List.reverse-involutive as ⟨
299 reverse (reverse as) ⊆⟨ reverse-selfAdjoint rs ⟩
300 bs ∎
301 where open ⊆-Reasoning S
302
303 ------------------------------------------------------------------------
304 -- filter
305
306 module _ (S : Setoid a ℓ) where
307
308 open Setoid S renaming (Carrier to A)
309 open Subset S
310 open Membershipₚ
311
312 filter-⊆ : ∀ {P : Pred A p} (P? : Decidable P) →
313 ∀ xs → filter P? xs ⊆ xs
314 filter-⊆ P? (x ∷ xs) y∈f[x∷xs] with does (P? x)
315 ... | false = there (filter-⊆ P? xs y∈f[x∷xs])
316 ... | true with y∈f[x∷xs]
317 ... | here y≈x = here y≈x
318 ... | there y∈f[xs] = there (filter-⊆ P? xs y∈f[xs])
319
320 -- Should be known as `filter⁺` (no prime) but `filter-⊆` used
321 -- to be called this so for backwards compatability reasons, the
322 -- correct name will have to wait until the deprecated name is
323 -- removed.
324 filter⁺′ : ∀ {P : Pred A p} (P? : Decidable P) → P Respects _≈_ →
325 ∀ {Q : Pred A q} (Q? : Decidable Q) → Q Respects _≈_ →
326 P ⋐ Q → ∀ {xs ys} → xs ⊆ ys → filter P? xs ⊆ filter Q? ys
327 filter⁺′ P? P-resp Q? Q-resp P⋐Q xs⊆ys v∈fxs with ∈-filter⁻ S P? P-resp v∈fxs
328 ... | v∈xs , Pv = ∈-filter⁺ S Q? Q-resp (xs⊆ys v∈xs) (P⋐Q Pv)
329
330 ------------------------------------------------------------------------
331 -- applyUpTo
332
333 module _ (S : Setoid a ℓ) where
334
335 open Setoid S renaming (Carrier to A)
336 open Subset S
337
338 applyUpTo⁺ : ∀ (f : ℕ → A) {m n} → m ≤ n → applyUpTo f m ⊆ applyUpTo f n
339 applyUpTo⁺ _ (s≤s m≤n) (here f≡f[0]) = here f≡f[0]
340 applyUpTo⁺ _ (s≤s m≤n) (there v∈xs) = there (applyUpTo⁺ _ m≤n v∈xs)
341
342
343 ------------------------------------------------------------------------
344 -- DEPRECATED
345 ------------------------------------------------------------------------
346
347 -- Version 1.5
348
349 filter⁺ = filter-⊆
350 {-# WARNING_ON_USAGE filter⁺
351 "Warning: filter⁺ was deprecated in v1.5.
352 Please use filter-⊆ instead."
353 #-}