Debian Codegraph @

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

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