Debian Codegraph @

src/Data/List/Relation/Unary/All/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties related to All
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Unary.All.Properties where
10
11 open import Axiom.Extensionality.Propositional using (Extensionality)
12 open import Data.Bool.Base using (Bool; T; true; false)
13 open import Data.Bool.ListAction using (all)
14 open import Data.Bool.Properties using (T-∧)
15 open import Data.Fin.Base using (Fin; zero; suc)
16 open import Data.List.Base as List hiding (lookup; updateAt; and; or; all; any)
17 open import Data.List.Membership.Propositional using (_∈_; _≢∈_)
18 open import Data.List.Membership.Propositional.Properties
19 using (there-injective-≢∈; ∈-filter⁻)
20 import Data.List.Membership.Setoid as SetoidMembership
21 import Data.List.Properties as List
22 import Data.List.Relation.Binary.Equality.Setoid as ≋
23 open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise; []; _∷_)
24 import Data.List.Relation.Binary.Subset.Propositional as Subset
25 open import Data.List.Relation.Unary.All as All using
26 ( All; []; _∷_; lookup; updateAt
27 ; _[_]=_; here; there
28 ; Null
29 )
30 open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
31 open import Data.Maybe.Base as Maybe using (Maybe; just; nothing)
32 open import Data.Maybe.Relation.Unary.All as Maybe using (just; nothing; fromAny)
33 open import Data.Maybe.Relation.Unary.Any as Maybe using (just)
34 open import Data.Nat.Base using (zero; suc; s≤s; _<_; z<s; s<s)
35 open import Data.Nat.Properties using (≤-refl; m≤n⇒m≤1+n)
36 open import Data.Product.Base as Product using (_×_; _,_; uncurry; uncurry′)
37 open import Function.Base using (_∘_; _$_; id; case_of_; flip)
38 open import Function.Bundles using (_↠_; mk↠ₛ; _⇔_; mk⇔; _↔_; mk↔ₛ′; Equivalence)
39 open import Level using (Level)
40 open import Relation.Binary.Core using (REL)
41 open import Relation.Binary.Bundles using (Setoid)
42 import Relation.Binary.Definitions as B
43 open import Relation.Binary.PropositionalEquality.Core
44 using (_≡_; refl; sym; cong; cong₂; _≗_)
45 open import Relation.Nullary.Reflects using (invert)
46 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
47 open import Relation.Nullary.Decidable
48 using (Dec; does; yes; no; _because_; ¬?; decidable-stable; dec-true)
49 open import Relation.Unary
50 using (Decidable; Pred; ∁; _⟨×⟩_) renaming (_⊆_ to _⋐_)
51 open import Relation.Unary.Properties using (∁?)
52
53 private
54 variable
55 a b c p q r ℓ ℓ₁ ℓ₂ : Level
56 A : Set a
57 B : Set b
58 C : Set c
59 P : Pred A p
60 Q : Pred B q
61 R : Pred C r
62 x y : A
63 xs ys : List A
64
65 ------------------------------------------------------------------------
66 -- Re-export Core Properties
67
68 open import Data.List.Relation.Unary.All.Properties.Core public
69
70 ------------------------------------------------------------------------
71 -- Properties regarding Null
72
73 Null⇒null : Null xs → T (null xs)
74 Null⇒null [] = _
75
76 null⇒Null : T (null xs) → Null xs
77 null⇒Null {xs = [] } _ = []
78 null⇒Null {xs = _ ∷ _} ()
79
80 ------------------------------------------------------------------------
81 -- Properties of the "points-to" relation _[_]=_
82
83 -- Relation _[_]=_ is deterministic: each index points to a single value.
84
85 []=-injective : ∀ {px qx : P x} {pxs : All P xs} {i : x ∈ xs} →
86 pxs [ i ]= px →
87 pxs [ i ]= qx →
88 px ≡ qx
89 []=-injective here here = refl
90 []=-injective (there x↦px) (there x↦qx) = []=-injective x↦px x↦qx
91
92 -- See also Data.List.Relation.Unary.All.Properties.WithK.[]=-irrelevant.
93
94 ------------------------------------------------------------------------
95 -- Defining properties of lookup and _[_]=_
96 --
97 -- pxs [ i ]= px if and only if lookup pxs i = px.
98
99 -- `i` points to `lookup pxs i` in `pxs`.
100
101 []=lookup : (pxs : All P xs) (i : x ∈ xs) →
102 pxs [ i ]= lookup pxs i
103 []=lookup (px ∷ pxs) (here refl) = here
104 []=lookup (px ∷ pxs) (there i) = there ([]=lookup pxs i)
105
106 -- If `i` points to `px` in `pxs`, then `lookup pxs i ≡ px`.
107
108 []=⇒lookup : ∀ {px : P x} {pxs : All P xs} {i : x ∈ xs} →
109 pxs [ i ]= px →
110 lookup pxs i ≡ px
111 []=⇒lookup x↦px = []=-injective ([]=lookup _ _) x↦px
112
113 -- If `lookup pxs i ≡ px`, then `i` points to `px` in `pxs`.
114
115 lookup⇒[]= : ∀ {px : P x} (pxs : All P xs) (i : x ∈ xs) →
116 lookup pxs i ≡ px →
117 pxs [ i ]= px
118 lookup⇒[]= pxs i refl = []=lookup pxs i
119
120 ------------------------------------------------------------------------
121 -- Properties of operations over `All`
122 ------------------------------------------------------------------------
123 -- map
124
125 map-cong : ∀ {f : P ⋐ Q} {g : P ⋐ Q} (pxs : All P xs) →
126 (∀ {x} → f {x} ≗ g) → All.map f pxs ≡ All.map g pxs
127 map-cong [] _ = refl
128 map-cong (px ∷ pxs) feq = cong₂ _∷_ (feq px) (map-cong pxs feq)
129
130 map-id : ∀ (pxs : All P xs) → All.map id pxs ≡ pxs
131 map-id [] = refl
132 map-id (px ∷ pxs) = cong (px ∷_) (map-id pxs)
133
134 map-∘ : ∀ {f : P ⋐ Q} {g : Q ⋐ R} (pxs : All P xs) →
135 All.map g (All.map f pxs) ≡ All.map (g ∘ f) pxs
136 map-∘ [] = refl
137 map-∘ (px ∷ pxs) = cong (_ ∷_) (map-∘ pxs)
138
139 lookup-map : ∀ {f : P ⋐ Q} (pxs : All P xs) (i : x ∈ xs) →
140 lookup (All.map f pxs) i ≡ f (lookup pxs i)
141 lookup-map (px ∷ pxs) (here refl) = refl
142 lookup-map (px ∷ pxs) (there i) = lookup-map pxs i
143
144 ------------------------------------------------------------------------
145 -- _[_]%=_ / updateAt
146
147 -- Defining properties of updateAt:
148
149 -- (+) updateAt actually updates the element at the given index.
150
151 updateAt-updates : ∀ (i : x ∈ xs) {f : P x → P x} {px : P x} (pxs : All P xs) →
152 pxs [ i ]= px →
153 updateAt i f pxs [ i ]= f px
154 updateAt-updates (here refl) (px ∷ pxs) here = here
155 updateAt-updates (there i) (px ∷ pxs) (there x↦px) =
156 there (updateAt-updates i pxs x↦px)
157
158 -- (-) updateAt i does not touch the elements at other indices.
159
160 updateAt-minimal : ∀ (i : x ∈ xs) (j : y ∈ xs) →
161 ∀ {f : P y → P y} {px : P x} (pxs : All P xs) →
162 i ≢∈ j →
163 pxs [ i ]= px →
164 updateAt j f pxs [ i ]= px
165 updateAt-minimal (here .refl) (here refl) (px ∷ pxs) i≢j here =
166 contradiction refl (i≢j refl)
167 updateAt-minimal (here .refl) (there j) (px ∷ pxs) i≢j here = here
168 updateAt-minimal (there i) (here refl) (px ∷ pxs) i≢j (there val) = there val
169 updateAt-minimal (there i) (there j) (px ∷ pxs) i≢j (there val) =
170 there (updateAt-minimal i j pxs (there-injective-≢∈ i≢j) val)
171
172 -- lookup after updateAt reduces.
173
174 -- For same index this is an easy consequence of updateAt-updates
175 -- using []=↔lookup.
176
177 lookup∘updateAt : ∀ (pxs : All P xs) (i : x ∈ xs) {f : P x → P x} →
178 lookup (updateAt i f pxs) i ≡ f (lookup pxs i)
179 lookup∘updateAt pxs i =
180 []=⇒lookup (updateAt-updates i pxs (lookup⇒[]= pxs i refl))
181
182 -- For different indices it easily follows from updateAt-minimal.
183
184 lookup∘updateAt′ : ∀ (i : x ∈ xs) (j : y ∈ xs) →
185 ∀ {f : P y → P y} {px : P x} (pxs : All P xs) →
186 i ≢∈ j →
187 lookup (updateAt j f pxs) i ≡ lookup pxs i
188 lookup∘updateAt′ i j pxs i≢j =
189 []=⇒lookup (updateAt-minimal i j pxs i≢j (lookup⇒[]= pxs i refl))
190
191 -- The other properties are consequences of (+) and (-).
192 -- We spell the most natural properties out.
193 -- Direct inductive proofs are in most cases easier than just using
194 -- the defining properties.
195
196 -- In the explanations, we make use of shorthand f = g ↾ x
197 -- meaning that f and g agree locally at point x, i.e. f x ≡ g x.
198
199 -- updateAt (i : x ∈ xs) is a morphism
200 -- from the monoid of endofunctions P x → P x
201 -- to the monoid of endofunctions All P xs → All P xs.
202
203 -- 1a. local identity: f = id ↾ (lookup pxs i)
204 -- implies updateAt i f = id ↾ pxs
205
206 updateAt-id-local : ∀ (i : x ∈ xs) {f : P x → P x} (pxs : All P xs) →
207 f (lookup pxs i) ≡ lookup pxs i →
208 updateAt i f pxs ≡ pxs
209 updateAt-id-local (here refl)(px ∷ pxs) eq = cong (_∷ pxs) eq
210 updateAt-id-local (there i) (px ∷ pxs) eq = cong (px ∷_) (updateAt-id-local i pxs eq)
211
212 -- 1b. identity: updateAt i id ≗ id
213
214 updateAt-id : ∀ (i : x ∈ xs) (pxs : All P xs) → updateAt i id pxs ≡ pxs
215 updateAt-id i pxs = updateAt-id-local i pxs refl
216
217 -- 2a. relative composition: f ∘ g = h ↾ (lookup i pxs)
218 -- implies updateAt i f ∘ updateAt i g = updateAt i h ↾ pxs
219
220 updateAt-∘-local : ∀ (i : x ∈ xs) {f g h : P x → P x} (pxs : All P xs) →
221 f (g (lookup pxs i)) ≡ h (lookup pxs i) →
222 updateAt i f (updateAt i g pxs) ≡ updateAt i h pxs
223 updateAt-∘-local (here refl) (px ∷ pxs) fg=h = cong (_∷ pxs) fg=h
224 updateAt-∘-local (there i) (px ∷ pxs) fg=h = cong (px ∷_) (updateAt-∘-local i pxs fg=h)
225
226 -- 2b. composition: updateAt i f ∘ updateAt i g ≗ updateAt i (f ∘ g)
227
228 updateAt-∘ : ∀ (i : x ∈ xs) {f g : P x → P x} →
229 updateAt {P = P} i f ∘ updateAt i g ≗ updateAt i (f ∘ g)
230 updateAt-∘ i pxs = updateAt-∘-local i pxs refl
231
232 -- 3. congruence: updateAt i is a congruence wrt. extensional equality.
233
234 -- 3a. If f = g ↾ (lookup pxs i)
235 -- then updateAt i f = updateAt i g ↾ pxs
236
237 updateAt-cong-local : ∀ (i : x ∈ xs) {f g : P x → P x} (pxs : All P xs) →
238 f (lookup pxs i) ≡ g (lookup pxs i) →
239 updateAt i f pxs ≡ updateAt i g pxs
240 updateAt-cong-local (here refl) (px ∷ pxs) f=g = cong (_∷ pxs) f=g
241 updateAt-cong-local (there i) (px ∷ pxs) f=g = cong (px ∷_) (updateAt-cong-local i pxs f=g)
242
243 -- 3b. congruence: f ≗ g → updateAt i f ≗ updateAt i g
244
245 updateAt-cong : ∀ (i : x ∈ xs) {f g : P x → P x} →
246 f ≗ g → updateAt {P = P} i f ≗ updateAt i g
247 updateAt-cong i f≗g pxs = updateAt-cong-local i pxs (f≗g (lookup pxs i))
248
249 -- The order of updates at different indices i ≢ j does not matter.
250
251 -- This a consequence of updateAt-updates and updateAt-minimal
252 -- but easier to prove inductively.
253
254 updateAt-commutes : ∀ (i : x ∈ xs) (j : y ∈ xs) →
255 ∀ {f : P x → P x} {g : P y → P y} →
256 i ≢∈ j →
257 updateAt {P = P} i f ∘ updateAt j g ≗ updateAt j g ∘ updateAt i f
258 updateAt-commutes (here refl) (here refl) i≢j (px ∷ pxs) =
259 contradiction refl (i≢j refl)
260 updateAt-commutes (here refl) (there j) i≢j (px ∷ pxs) = refl
261 updateAt-commutes (there i) (here refl) i≢j (px ∷ pxs) = refl
262 updateAt-commutes (there i) (there j) i≢j (px ∷ pxs) =
263 cong (px ∷_) (updateAt-commutes i j (there-injective-≢∈ i≢j) pxs)
264
265 map-updateAt : ∀ {f : P ⋐ Q} {g : P x → P x} {h : Q x → Q x}
266 (pxs : All P xs) (i : x ∈ xs) →
267 f (g (lookup pxs i)) ≡ h (f (lookup pxs i)) →
268 All.map f (pxs All.[ i ]%= g) ≡ (All.map f pxs) All.[ i ]%= h
269 map-updateAt (px ∷ pxs) (here refl) = cong (_∷ _)
270 map-updateAt (px ∷ pxs) (there i) feq = cong (_ ∷_) (map-updateAt pxs i feq)
271
272 ------------------------------------------------------------------------
273 -- Introduction (⁺) and elimination (⁻) rules for list operations
274 ------------------------------------------------------------------------
275 -- singleton
276
277 singleton⁻ : All P [ x ] → P x
278 singleton⁻ (px ∷ []) = px
279
280 -- head
281
282 head⁺ : All P xs → Maybe.All P (head xs)
283 head⁺ [] = nothing
284 head⁺ (px ∷ _) = just px
285
286 -- tail
287
288 tail⁺ : All P xs → Maybe.All (All P) (tail xs)
289 tail⁺ [] = nothing
290 tail⁺ (_ ∷ pxs) = just pxs
291
292 -- last
293
294 last⁺ : All P xs → Maybe.All P (last xs)
295 last⁺ [] = nothing
296 last⁺ (px ∷ []) = just px
297 last⁺ (px ∷ pxs@(_ ∷ _)) = last⁺ pxs
298
299 -- uncons
300
301 uncons⁺ : All P xs → Maybe.All (P ⟨×⟩ All P) (uncons xs)
302 uncons⁺ [] = nothing
303 uncons⁺ (px ∷ pxs) = just (px , pxs)
304
305 uncons⁻ : Maybe.All (P ⟨×⟩ All P) (uncons xs) → All P xs
306 uncons⁻ {xs = []} nothing = []
307 uncons⁻ {xs = x ∷ xs} (just (px , pxs)) = px ∷ pxs
308
309 -- map
310
311 map⁺ : ∀ {f : A → B} → All (P ∘ f) xs → All P (map f xs)
312 map⁺ [] = []
313 map⁺ (p ∷ ps) = p ∷ map⁺ ps
314
315 map⁻ : ∀ {f : A → B} → All P (map f xs) → All (P ∘ f) xs
316 map⁻ {xs = []} [] = []
317 map⁻ {xs = _ ∷ _} (p ∷ ps) = p ∷ map⁻ ps
318
319 -- A variant of All.map.
320
321 gmap⁺ : ∀ {f : A → B} → P ⋐ Q ∘ f → All P ⋐ All Q ∘ map f
322 gmap⁺ g = map⁺ ∘ All.map g
323
324 gmap⁻ : ∀ {f : A → B} → Q ∘ f ⋐ P → All Q ∘ map f ⋐ All P
325 gmap⁻ g = All.map g ∘ map⁻
326
327 ------------------------------------------------------------------------
328 -- mapMaybe
329
330 mapMaybe⁺ : ∀ {f : A → Maybe B} →
331 All (Maybe.All P) (map f xs) → All P (mapMaybe f xs)
332 mapMaybe⁺ {xs = []} {f = f} [] = []
333 mapMaybe⁺ {xs = x ∷ xs} {f = f} (px ∷ pxs) with f x
334 ... | nothing = mapMaybe⁺ pxs
335 ... | just v with just pv ← px = pv ∷ mapMaybe⁺ pxs
336
337 ------------------------------------------------------------------------
338 -- catMaybes
339
340 All-catMaybes⁺ : All (Maybe.All P) xs → All P (catMaybes xs)
341 All-catMaybes⁺ [] = []
342 All-catMaybes⁺ (just px ∷ pxs) = px ∷ All-catMaybes⁺ pxs
343 All-catMaybes⁺ (nothing ∷ pxs) = All-catMaybes⁺ pxs
344
345 Any-catMaybes⁺ : All (Maybe.Any P) xs → All P (catMaybes xs)
346 Any-catMaybes⁺ = All-catMaybes⁺ ∘ All.map fromAny
347
348 ------------------------------------------------------------------------
349 -- _++_
350
351 ++⁺ : All P xs → All P ys → All P (xs ++ ys)
352 ++⁺ [] pys = pys
353 ++⁺ (px ∷ pxs) pys = px ∷ ++⁺ pxs pys
354
355 ++⁻ˡ : ∀ xs {ys} → All P (xs ++ ys) → All P xs
356 ++⁻ˡ [] p = []
357 ++⁻ˡ (x ∷ xs) (px ∷ pxs) = px ∷ (++⁻ˡ _ pxs)
358
359 ++⁻ʳ : ∀ xs {ys} → All P (xs ++ ys) → All P ys
360 ++⁻ʳ [] p = p
361 ++⁻ʳ (x ∷ xs) (px ∷ pxs) = ++⁻ʳ xs pxs
362
363 ++⁻ : ∀ xs {ys} → All P (xs ++ ys) → All P xs × All P ys
364 ++⁻ [] p = [] , p
365 ++⁻ (x ∷ xs) (px ∷ pxs) = Product.map (px ∷_) id (++⁻ _ pxs)
366
367 ++↔ : (All P xs × All P ys) ↔ All P (xs ++ ys)
368 ++↔ {xs = zs} = mk↔ₛ′ (uncurry ++⁺) (++⁻ zs) (++⁺∘++⁻ zs) ++⁻∘++⁺
369 where
370 ++⁺∘++⁻ : ∀ xs (p : All P (xs ++ ys)) → uncurry′ ++⁺ (++⁻ xs p) ≡ p
371 ++⁺∘++⁻ [] p = refl
372 ++⁺∘++⁻ (x ∷ xs) (px ∷ pxs) = cong (_∷_ px) $ ++⁺∘++⁻ xs pxs
373
374 ++⁻∘++⁺ : ∀ (p : All P xs × All P ys) → ++⁻ xs (uncurry ++⁺ p) ≡ p
375 ++⁻∘++⁺ ([] , pys) = refl
376 ++⁻∘++⁺ (px ∷ pxs , pys) rewrite ++⁻∘++⁺ (pxs , pys) = refl
377
378 ------------------------------------------------------------------------
379 -- concat
380
381 concat⁺ : ∀ {xss} → All (All P) xss → All P (concat xss)
382 concat⁺ [] = []
383 concat⁺ (pxs ∷ pxss) = ++⁺ pxs (concat⁺ pxss)
384
385 concat⁻ : ∀ {xss} → All P (concat xss) → All (All P) xss
386 concat⁻ {xss = []} [] = []
387 concat⁻ {xss = xs ∷ xss} pxs = ++⁻ˡ xs pxs ∷ concat⁻ (++⁻ʳ xs pxs)
388
389 ------------------------------------------------------------------------
390 -- snoc
391
392 ∷ʳ⁺ : All P xs → P x → All P (xs ∷ʳ x)
393 ∷ʳ⁺ pxs px = ++⁺ pxs (px ∷ [])
394
395 ∷ʳ⁻ : All P (xs ∷ʳ x) → All P xs × P x
396 ∷ʳ⁻ pxs = Product.map₂ singleton⁻ $ ++⁻ _ pxs
397
398 -- unsnoc
399
400 unsnoc⁺ : All P xs → Maybe.All (All P ⟨×⟩ P) (unsnoc xs)
401 unsnoc⁺ {xs = xs} pxs with initLast xs
402 unsnoc⁺ {xs = .[]} pxs | [] = nothing
403 unsnoc⁺ {xs = .(xs ∷ʳ x)} pxs | xs ∷ʳ′ x = just (∷ʳ⁻ pxs)
404
405 unsnoc⁻ : Maybe.All (All P ⟨×⟩ P) (unsnoc xs) → All P xs
406 unsnoc⁻ {xs = xs} pxs with initLast xs
407 unsnoc⁻ {xs = .[]} nothing | [] = []
408 unsnoc⁻ {xs = .(xs ∷ʳ x)} (just (pxs , px)) | xs ∷ʳ′ x = ∷ʳ⁺ pxs px
409
410 ------------------------------------------------------------------------
411 -- cartesianProductWith and cartesianProduct
412
413 module _ (S₁ : Setoid a ℓ₁) (S₂ : Setoid b ℓ₂) where
414 open SetoidMembership S₁ using () renaming (_∈_ to _∈₁_)
415 open SetoidMembership S₂ using () renaming (_∈_ to _∈₂_)
416
417 cartesianProductWith⁺ : ∀ f xs ys →
418 (∀ {x y} → x ∈₁ xs → y ∈₂ ys → P (f x y)) →
419 All P (cartesianProductWith f xs ys)
420 cartesianProductWith⁺ f [] ys pres = []
421 cartesianProductWith⁺ f (x ∷ xs) ys pres = ++⁺
422 (map⁺ (All.tabulateₛ S₂ (pres (here (Setoid.refl S₁)))))
423 (cartesianProductWith⁺ f xs ys (pres ∘ there))
424
425 cartesianProduct⁺ : ∀ xs ys →
426 (∀ {x y} → x ∈₁ xs → y ∈₂ ys → P (x , y)) →
427 All P (cartesianProduct xs ys)
428 cartesianProduct⁺ = cartesianProductWith⁺ _,_
429
430 ------------------------------------------------------------------------
431 -- take and drop
432
433 drop⁺ : ∀ n → All P xs → All P (drop n xs)
434 drop⁺ zero pxs = pxs
435 drop⁺ (suc n) [] = []
436 drop⁺ (suc n) (px ∷ pxs) = drop⁺ n pxs
437
438 dropWhile⁺ : (Q? : Decidable Q) → All P xs → All P (dropWhile Q? xs)
439 dropWhile⁺ Q? [] = []
440 dropWhile⁺ {xs = x ∷ xs} Q? px∷pxs@(_ ∷ pxs) with does (Q? x)
441 ... | true = dropWhile⁺ Q? pxs
442 ... | false = px∷pxs
443
444 dropWhile⁻ : (P? : Decidable P) → dropWhile P? xs ≡ [] → All P xs
445 dropWhile⁻ {xs = []} P? eq = []
446 dropWhile⁻ {xs = x ∷ xs} P? eq with P? x
447 ... | yes px = px ∷ (dropWhile⁻ P? eq)
448 ... | no ¬px = case eq of λ ()
449
450 all-head-dropWhile : (P? : Decidable P) →
451 ∀ xs → Maybe.All (∁ P) (head (dropWhile P? xs))
452 all-head-dropWhile P? [] = nothing
453 all-head-dropWhile P? (x ∷ xs) with P? x
454 ... | yes px = all-head-dropWhile P? xs
455 ... | no ¬px = just ¬px
456
457 all⇒dropWhile≡[] : (P? : Decidable P) → All P xs → dropWhile P? xs ≡ []
458 all⇒dropWhile≡[] P? [] = refl
459 all⇒dropWhile≡[] P? (px ∷ pxs) rewrite dec-true (P? _) px
460 = all⇒dropWhile≡[] P? pxs
461
462 take⁺ : ∀ n → All P xs → All P (take n xs)
463 take⁺ zero pxs = []
464 take⁺ (suc n) [] = []
465 take⁺ (suc n) (px ∷ pxs) = px ∷ take⁺ n pxs
466
467 takeWhile⁺ : (Q? : Decidable Q) → All P xs → All P (takeWhile Q? xs)
468 takeWhile⁺ Q? [] = []
469 takeWhile⁺ {xs = x ∷ xs} Q? (px ∷ pxs) with does (Q? x)
470 ... | true = px ∷ takeWhile⁺ Q? pxs
471 ... | false = []
472
473 all-takeWhile : (P? : Decidable P) → ∀ xs → All P (takeWhile P? xs)
474 all-takeWhile P? [] = []
475 all-takeWhile P? (x ∷ xs) with P? x
476 ... | yes px = px ∷ all-takeWhile P? xs
477 ... | no ¬px = []
478
479 all⇒takeWhile≗id : (P? : Decidable P) → All P xs → takeWhile P? xs ≡ xs
480 all⇒takeWhile≗id P? [] = refl
481 all⇒takeWhile≗id P? (px ∷ pxs) rewrite dec-true (P? _) px
482 = cong (_ ∷_) (all⇒takeWhile≗id P? pxs)
483
484 ------------------------------------------------------------------------
485 -- applyUpTo
486
487 applyUpTo⁺₁ : ∀ f n → (∀ {i} → i < n → P (f i)) → All P (applyUpTo f n)
488 applyUpTo⁺₁ f zero Pf = []
489 applyUpTo⁺₁ f (suc n) Pf = Pf z<s ∷ applyUpTo⁺₁ (f ∘ suc) n (Pf ∘ s<s)
490
491 applyUpTo⁺₂ : ∀ f n → (∀ i → P (f i)) → All P (applyUpTo f n)
492 applyUpTo⁺₂ f n Pf = applyUpTo⁺₁ f n (λ _ → Pf _)
493
494 applyUpTo⁻ : ∀ f n → All P (applyUpTo f n) → ∀ {i} → i < n → P (f i)
495 applyUpTo⁻ f (suc n) (px ∷ _) z<s = px
496 applyUpTo⁻ f (suc n) (_ ∷ pxs) (s<s i<n@(s≤s _)) =
497 applyUpTo⁻ (f ∘ suc) n pxs i<n
498
499 ------------------------------------------------------------------------
500 -- upTo
501
502 all-upTo : ∀ n → All (_< n) (upTo n)
503 all-upTo n = applyUpTo⁺₁ id n id
504
505 ------------------------------------------------------------------------
506 -- applyDownFrom
507
508 applyDownFrom⁺₁ : ∀ f n → (∀ {i} → i < n → P (f i)) → All P (applyDownFrom f n)
509 applyDownFrom⁺₁ f zero Pf = []
510 applyDownFrom⁺₁ f (suc n) Pf = Pf ≤-refl ∷ applyDownFrom⁺₁ f n (Pf ∘ m≤n⇒m≤1+n)
511
512 applyDownFrom⁺₂ : ∀ f n → (∀ i → P (f i)) → All P (applyDownFrom f n)
513 applyDownFrom⁺₂ f n Pf = applyDownFrom⁺₁ f n (λ _ → Pf _)
514
515 ------------------------------------------------------------------------
516 -- tabulate
517
518 tabulate⁺ : ∀ {n} {f : Fin n → A} →
519 (∀ i → P (f i)) → All P (tabulate f)
520 tabulate⁺ {n = zero} Pf = []
521 tabulate⁺ {n = suc _} Pf = Pf zero ∷ tabulate⁺ (Pf ∘ suc)
522
523 tabulate⁻ : ∀ {n} {f : Fin n → A} →
524 All P (tabulate f) → (∀ i → P (f i))
525 tabulate⁻ (px ∷ _) zero = px
526 tabulate⁻ (_ ∷ pf) (suc i) = tabulate⁻ pf i
527
528 ------------------------------------------------------------------------
529 -- remove
530
531 ─⁺ : ∀ (p : Any P xs) → All Q xs → All Q (xs Any.─ p)
532 ─⁺ (here px) (_ ∷ qs) = qs
533 ─⁺ (there p) (q ∷ qs) = q ∷ ─⁺ p qs
534
535 ─⁻ : ∀ (p : Any P xs) → Q (Any.lookup p) → All Q (xs Any.─ p) → All Q xs
536 ─⁻ (here px) q qs = q ∷ qs
537 ─⁻ (there p) q (q′ ∷ qs) = q′ ∷ ─⁻ p q qs
538
539 ------------------------------------------------------------------------
540 -- filter
541
542 module _ (P? : Decidable P) where
543
544 all-filter : ∀ xs → All P (filter P? xs)
545 all-filter [] = []
546 all-filter (x ∷ xs) with P? x
547 ... | true because [Px] = invert [Px] ∷ all-filter xs
548 ... | false because _ = all-filter xs
549
550 filter⁺ : All Q xs → All Q (filter P? xs)
551 filter⁺ {xs = _} [] = []
552 filter⁺ {xs = x ∷ _} (Qx ∷ Qxs) with does (P? x)
553 ... | false = filter⁺ Qxs
554 ... | true = Qx ∷ filter⁺ Qxs
555
556 filter⁻ : All Q (filter P? xs) → All Q (filter (¬? ∘ P?) xs) → All Q xs
557 filter⁻ {xs = []} [] [] = []
558 filter⁻ {xs = x ∷ _} all⁺ all⁻ with P? x | ¬? (P? x)
559 filter⁻ {xs = x ∷ _} all⁺ all⁻ | yes Px | yes ¬Px = contradiction Px ¬Px
560 filter⁻ {xs = x ∷ _} (qx ∷ all⁺) all⁻ | yes Px | no ¬¬Px = qx ∷ filter⁻ all⁺ all⁻
561 filter⁻ {xs = x ∷ _} all⁺ (qx ∷ all⁻) | no _ | yes ¬Px = qx ∷ filter⁻ all⁺ all⁻
562 filter⁻ {xs = x ∷ _} all⁺ all⁻ | no ¬Px | no ¬¬Px = contradiction ¬Px ¬¬Px
563
564 ------------------------------------------------------------------------
565 -- partition
566
567 module _ {P : A → Set p} (P? : Decidable P) where
568
569 partition-All : ∀ xs → (let ys , zs = partition P? xs) →
570 All P ys × All (∁ P) zs
571 partition-All xs rewrite List.partition-defn P? xs =
572 all-filter P? xs , all-filter (∁? P?) xs
573
574 ------------------------------------------------------------------------
575 -- derun and deduplicate
576
577 module _ {R : A → A → Set q} (R? : B.Decidable R) where
578
579 derun⁺ : All P xs → All P (derun R? xs)
580 derun⁺ {xs = []} [] = []
581 derun⁺ {xs = x ∷ []} (px ∷ []) = px ∷ []
582 derun⁺ {xs = x ∷ y ∷ xs} (px ∷ all[P,y∷xs]) with does (R? x y)
583 ... | false = px ∷ derun⁺ all[P,y∷xs]
584 ... | true = derun⁺ all[P,y∷xs]
585
586 deduplicate⁺ : All P xs → All P (deduplicate R? xs)
587 deduplicate⁺ [] = []
588 deduplicate⁺ (px ∷ pxs) = px ∷ filter⁺ (¬? ∘ R? _) (deduplicate⁺ pxs)
589
590 derun⁻ : P B.Respects (flip R) → ∀ xs → All P (derun R? xs) → All P xs
591 derun⁻ {P = P} P-resp-R [] [] = []
592 derun⁻ {P = P} P-resp-R (x ∷ xs) all[P,x∷xs] = aux x xs all[P,x∷xs]
593 where
594 aux : ∀ x xs → All P (derun R? (x ∷ xs)) → All P (x ∷ xs)
595 aux x [] (px ∷ []) = px ∷ []
596 aux x (y ∷ xs) all[P,x∷y∷xs] with R? x y
597 aux x (y ∷ xs) all[P,y∷xs] | yes Rxy
598 with r@(py ∷ _) ← aux y xs all[P,y∷xs] = P-resp-R Rxy py ∷ r
599 aux x (y ∷ xs) (px ∷ all[P,y∷xs]) | no _ = px ∷ aux y xs all[P,y∷xs]
600
601 deduplicate⁻ : P B.Respects R → ∀ xs → All P (deduplicate R? xs) → All P xs
602 deduplicate⁻ {P = P} resp [] [] = []
603 deduplicate⁻ {P = P} resp (x ∷ xs) (px ∷ pxs!) =
604 px ∷ deduplicate⁻ resp xs (filter⁻ (¬? ∘ R? x) pxs! (All.tabulate aux))
605 where
606 aux : ∀ {z} → z ∈ filter (¬? ∘ ¬? ∘ R? x) (deduplicate R? xs) → P z
607 aux {z = z} z∈filter = resp (decidable-stable (R? x z)
608 (Product.proj₂ (∈-filter⁻ (¬? ∘ ¬? ∘ R? x) {z} {deduplicate R? xs} z∈filter))) px
609
610 ------------------------------------------------------------------------
611 -- zipWith
612
613 zipWith⁺ : ∀ (f : A → B → C) → Pointwise (λ x y → P (f x y)) xs ys →
614 All P (zipWith f xs ys)
615 zipWith⁺ f [] = []
616 zipWith⁺ f (Pfxy ∷ Pfxsys) = Pfxy ∷ zipWith⁺ f Pfxsys
617
618 ------------------------------------------------------------------------
619 -- Operations for constructing lists
620 ------------------------------------------------------------------------
621 -- fromMaybe
622
623 fromMaybe⁺ : ∀ {mx} → Maybe.All P mx → All P (fromMaybe mx)
624 fromMaybe⁺ (just px) = px ∷ []
625 fromMaybe⁺ nothing = []
626
627 fromMaybe⁻ : ∀ mx → All P (fromMaybe mx) → Maybe.All P mx
628 fromMaybe⁻ (just x) (px ∷ []) = just px
629 fromMaybe⁻ nothing p = nothing
630
631 ------------------------------------------------------------------------
632 -- replicate
633
634 replicate⁺ : ∀ n → P x → All P (replicate n x)
635 replicate⁺ zero px = []
636 replicate⁺ (suc n) px = px ∷ replicate⁺ n px
637
638 replicate⁻ : ∀ {n} → All P (replicate (suc n) x) → P x
639 replicate⁻ (px ∷ _) = px
640
641 ------------------------------------------------------------------------
642 -- inits
643
644 inits⁺ : All P xs → All (All P) (inits xs)
645 inits⁺ [] = [] ∷ []
646 inits⁺ (px ∷ pxs) = [] ∷ gmap⁺ (px ∷_) (inits⁺ pxs)
647
648 inits⁻ : ∀ xs → All (All P) (inits xs) → All P xs
649 inits⁻ [] pxs = []
650 inits⁻ (x ∷ []) ([] ∷ p[x] ∷ []) = p[x]
651 inits⁻ (x ∷ xs@(_ ∷ _)) ([] ∷ pxs@(p[x] ∷ _)) =
652 singleton⁻ p[x] ∷ inits⁻ xs (All.map (drop⁺ 1) (map⁻ pxs))
653
654 ------------------------------------------------------------------------
655 -- tails
656
657 tails⁺ : All P xs → All (All P) (tails xs)
658 tails⁺ [] = [] ∷ []
659 tails⁺ pxxs@(_ ∷ pxs) = pxxs ∷ tails⁺ pxs
660
661 tails⁻ : ∀ xs → All (All P) (tails xs) → All P xs
662 tails⁻ [] pxs = []
663 tails⁻ (x ∷ xs) (pxxs ∷ _) = pxxs
664
665 ------------------------------------------------------------------------
666 -- all
667
668 module _ (p : A → Bool) where
669
670 all⁺ : ∀ xs → T (all p xs) → All (T ∘ p) xs
671 all⁺ [] _ = []
672 all⁺ (x ∷ xs) px∷pxs =
673 let px , pxs = Equivalence.to (T-∧ {p x}) px∷pxs
674 in px ∷ all⁺ xs pxs
675
676 all⁻ : All (T ∘ p) xs → T (all p xs)
677 all⁻ [] = _
678 all⁻ (px ∷ pxs) = Equivalence.from T-∧ (px , all⁻ pxs)
679
680 ------------------------------------------------------------------------
681 -- All is anti-monotone.
682
683 anti-mono : xs Subset.⊆ ys → All P ys → All P xs
684 anti-mono xs⊆ys pys = All.tabulate (lookup pys ∘ xs⊆ys)
685
686 all-anti-mono : ∀ (p : A → Bool) → xs Subset.⊆ ys → T (all p ys) → T (all p xs)
687 all-anti-mono p xs⊆ys = all⁻ p ∘ anti-mono xs⊆ys ∘ all⁺ p _
688
689 ------------------------------------------------------------------------
690 -- Interactions with pointwise equality
691 ------------------------------------------------------------------------
692
693 module _ (S : Setoid c ℓ) where
694
695 open Setoid S
696 open ≋ S
697
698 respects : P B.Respects _≈_ → (All P) B.Respects _≋_
699 respects p≈ [] [] = []
700 respects p≈ (x≈y ∷ xs≈ys) (px ∷ pxs) = p≈ x≈y px ∷ respects p≈ xs≈ys pxs
701
702 ------------------------------------------------------------------------
703 -- DEPRECATED NAMES
704 ------------------------------------------------------------------------
705 -- Please use the new names as continuing support for the old names is
706 -- not guaranteed.
707
708 -- Version 1.3
709
710 Any¬→¬All = Any¬⇒¬All
711 {-# WARNING_ON_USAGE Any¬→¬All
712 "Warning: Any¬→¬All was deprecated in v1.3.
713 Please use Any¬⇒¬All instead."
714 #-}
715
716 -- Version 2.0
717
718 updateAt-id-relative = updateAt-id-local
719 {-# WARNING_ON_USAGE updateAt-id-relative
720 "Warning: updateAt-id-relative was deprecated in v2.0.
721 Please use updateAt-id-local instead."
722 #-}
723
724 updateAt-compose-relative = updateAt-∘-local
725 {-# WARNING_ON_USAGE updateAt-compose-relative
726 "Warning: updateAt-compose-relative was deprecated in v2.0.
727 Please use updateAt-∘-local instead."
728 #-}
729
730 updateAt-compose = updateAt-∘
731 {-# WARNING_ON_USAGE updateAt-compose
732 "Warning: updateAt-compose was deprecated in v2.0.
733 Please use updateAt-∘ instead."
734 #-}
735
736 updateAt-cong-relative = updateAt-cong-local
737 {-# WARNING_ON_USAGE updateAt-cong-relative
738 "Warning: updateAt-cong-relative was deprecated in v2.0.
739 Please use updateAt-cong-local instead."
740 #-}
741
742 gmap = gmap⁺
743 {-# WARNING_ON_USAGE gmap
744 "Warning: gmap was deprecated in v2.0.
745 Please use gmap⁺ instead."
746 #-}
747
748 -- Version 2.1
749
750 map-compose = map-∘
751 {-# WARNING_ON_USAGE map-compose
752 "Warning: map-compose was deprecated in v2.1.
753 Please use map-∘ instead."
754 #-}
755
756 -- Version 2.2
757
758 takeWhile⁻ : (P? : Decidable P) → takeWhile P? xs ≡ xs → All P xs
759 takeWhile⁻ {xs = xs} P? eq rewrite sym eq = all-takeWhile P? xs
760 {-# WARNING_ON_USAGE takeWhile⁻
761 "Warning: takeWhile⁻ was deprecated in v2.2.
762 Please use all-takeWhile instead."
763 #-}