forked from agda/agda-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperties.agda
315 lines (228 loc) · 12 KB
/
Properties.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
------------------------------------------------------------------------
-- The Agda standard library
--
-- Properties of the setoid sublist relation
------------------------------------------------------------------------
{-# OPTIONS --cubical-compatible --safe #-}
open import Relation.Binary.Core using (Rel; _⇒_; _Preserves_⟶_)
open import Relation.Binary.Bundles using (Setoid)
module Data.List.Relation.Binary.Sublist.Setoid.Properties
{c ℓ} (S : Setoid c ℓ) where
open import Data.List.Base hiding (_∷ʳ_)
open import Data.List.Relation.Unary.Any using (Any)
import Data.Maybe.Relation.Unary.All as Maybe
open import Data.Nat.Base using (_≤_; _≥_)
import Data.Nat.Properties as ℕₚ
open import Data.Product using (∃; _,_; proj₂)
open import Function.Base
open import Function.Bundles using (_⇔_; _⤖_)
open import Level
open import Relation.Binary.Definitions using () renaming (Decidable to Decidable₂)
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
open import Relation.Binary.Structures using (IsDecTotalOrder)
open import Relation.Unary using (Pred; Decidable; Irrelevant)
open import Relation.Nullary.Negation using (¬_)
open import Relation.Nullary.Decidable using (¬?; yes; no)
import Data.List.Relation.Binary.Equality.Setoid as SetoidEquality
import Data.List.Relation.Binary.Sublist.Setoid as SetoidSublist
import Data.List.Relation.Binary.Sublist.Heterogeneous.Properties
as HeteroProperties
import Data.List.Membership.Setoid as SetoidMembership
open Setoid S using (_≈_; trans) renaming (Carrier to A; refl to ≈-refl)
open SetoidEquality S using (_≋_; ≋-refl)
open SetoidSublist S hiding (map)
open SetoidMembership S using (_∈_)
------------------------------------------------------------------------
-- Injectivity of constructors
------------------------------------------------------------------------
module _ {xs ys : List A} where
∷-injectiveˡ : ∀ {x y} {px qx : x ≈ y} {pxs qxs : xs ⊆ ys} →
((x ∷ xs) ⊆ (y ∷ ys) ∋ px ∷ pxs) ≡ (qx ∷ qxs) → px ≡ qx
∷-injectiveˡ refl = refl
∷-injectiveʳ : ∀ {x y} {px qx : x ≈ y} {pxs qxs : xs ⊆ ys} →
((x ∷ xs) ⊆ (y ∷ ys) ∋ px ∷ pxs) ≡ (qx ∷ qxs) → pxs ≡ qxs
∷-injectiveʳ refl = refl
∷ʳ-injective : ∀ {y} {pxs qxs : xs ⊆ ys} → y ∷ʳ pxs ≡ y ∷ʳ qxs → pxs ≡ qxs
∷ʳ-injective refl = refl
------------------------------------------------------------------------
-- Various functions' outputs are sublists
------------------------------------------------------------------------
tail-⊆ : ∀ xs → Maybe.All (_⊆ xs) (tail xs)
tail-⊆ xs = HeteroProperties.tail-Sublist ⊆-refl
take-⊆ : ∀ n xs → take n xs ⊆ xs
take-⊆ n xs = HeteroProperties.take-Sublist n ⊆-refl
drop-⊆ : ∀ n xs → drop n xs ⊆ xs
drop-⊆ n xs = HeteroProperties.drop-Sublist n ⊆-refl
module _ {p} {P : Pred A p} (P? : Decidable P) where
takeWhile-⊆ : ∀ xs → takeWhile P? xs ⊆ xs
takeWhile-⊆ xs = HeteroProperties.takeWhile-Sublist P? ⊆-refl
dropWhile-⊆ : ∀ xs → dropWhile P? xs ⊆ xs
dropWhile-⊆ xs = HeteroProperties.dropWhile-Sublist P? ⊆-refl
filter-⊆ : ∀ xs → filter P? xs ⊆ xs
filter-⊆ xs = HeteroProperties.filter-Sublist P? ⊆-refl
module _ {p} {P : Pred A p} (P? : Decidable P) where
takeWhile⊆filter : ∀ xs → takeWhile P? xs ⊆ filter P? xs
takeWhile⊆filter xs = HeteroProperties.takeWhile-filter P? {xs} ≋-refl
filter⊆dropWhile : ∀ xs → filter P? xs ⊆ dropWhile (¬? ∘ P?) xs
filter⊆dropWhile xs = HeteroProperties.filter-dropWhile P? {xs} ≋-refl
------------------------------------------------------------------------
-- Various list functions are increasing wrt _⊆_
------------------------------------------------------------------------
-- We write f⁺ for the proof that `xs ⊆ ys → f xs ⊆ f ys`
-- and f⁻ for the one that `f xs ⊆ f ys → xs ⊆ ys`.
module _ {as bs : List A} where
∷ˡ⁻ : ∀ {a} → a ∷ as ⊆ bs → as ⊆ bs
∷ˡ⁻ = HeteroProperties.∷ˡ⁻
∷ʳ⁻ : ∀ {a b} → ¬ (a ≈ b) → a ∷ as ⊆ b ∷ bs → a ∷ as ⊆ bs
∷ʳ⁻ = HeteroProperties.∷ʳ⁻
∷⁻ : ∀ {a b} → a ∷ as ⊆ b ∷ bs → as ⊆ bs
∷⁻ = HeteroProperties.∷⁻
------------------------------------------------------------------------
-- map
module _ {b ℓ} (R : Setoid b ℓ) where
open Setoid R using () renaming (Carrier to B; _≈_ to _≈′_)
open SetoidSublist R using () renaming (_⊆_ to _⊆′_)
map⁺ : ∀ {as bs} {f : A → B} → f Preserves _≈_ ⟶ _≈′_ →
as ⊆ bs → map f as ⊆′ map f bs
map⁺ {f = f} f-resp as⊆bs =
HeteroProperties.map⁺ f f (SetoidSublist.map S f-resp as⊆bs)
------------------------------------------------------------------------
-- _++_
module _ {as bs : List A} where
++⁺ˡ : ∀ cs → as ⊆ bs → as ⊆ cs ++ bs
++⁺ˡ = HeteroProperties.++ˡ
++⁺ʳ : ∀ cs → as ⊆ bs → as ⊆ bs ++ cs
++⁺ʳ = HeteroProperties.++ʳ
++⁺ : ∀ {cs ds} → as ⊆ bs → cs ⊆ ds → as ++ cs ⊆ bs ++ ds
++⁺ = HeteroProperties.++⁺
++⁻ : ∀ {cs ds} → length as ≡ length bs → as ++ cs ⊆ bs ++ ds → cs ⊆ ds
++⁻ = HeteroProperties.++⁻
------------------------------------------------------------------------
-- take
module _ {m n} {xs} where
take⁺ : m ≤ n → take m xs ⊆ take n xs
take⁺ m≤n = HeteroProperties.take⁺ m≤n ≋-refl
------------------------------------------------------------------------
-- drop
module _ {m n} {xs ys : List A} where
drop⁺ : m ≥ n → xs ⊆ ys → drop m xs ⊆ drop n ys
drop⁺ = HeteroProperties.drop⁺
module _ {m n} {xs : List A} where
drop⁺-≥ : m ≥ n → drop m xs ⊆ drop n xs
drop⁺-≥ m≥n = drop⁺ m≥n ⊆-refl
module _ {xs ys : List A} where
drop⁺-⊆ : ∀ n → xs ⊆ ys → drop n xs ⊆ drop n ys
drop⁺-⊆ n xs⊆ys = drop⁺ {n} ℕₚ.≤-refl xs⊆ys
------------------------------------------------------------------------
-- takeWhile / dropWhile
module _ {p q} {P : Pred A p} {Q : Pred A q}
(P? : Decidable P) (Q? : Decidable Q) where
takeWhile⁺ : ∀ {xs} → (∀ {a b} → a ≈ b → P a → Q b) →
takeWhile P? xs ⊆ takeWhile Q? xs
takeWhile⁺ {xs} P⇒Q = HeteroProperties.⊆-takeWhile-Sublist P? Q? {xs} P⇒Q ≋-refl
dropWhile⁺ : ∀ {xs} → (∀ {a b} → a ≈ b → Q b → P a) →
dropWhile P? xs ⊆ dropWhile Q? xs
dropWhile⁺ {xs} P⇒Q = HeteroProperties.⊇-dropWhile-Sublist P? Q? {xs} P⇒Q ≋-refl
------------------------------------------------------------------------
-- filter
module _ {p q} {P : Pred A p} {Q : Pred A q}
(P? : Decidable P) (Q? : Decidable Q) where
filter⁺ : ∀ {as bs} → (∀ {a b} → a ≈ b → P a → Q b) →
as ⊆ bs → filter P? as ⊆ filter Q? bs
filter⁺ = HeteroProperties.⊆-filter-Sublist P? Q?
------------------------------------------------------------------------
-- reverse
module _ {as bs : List A} where
reverseAcc⁺ : ∀ {cs ds} → as ⊆ bs → cs ⊆ ds →
reverseAcc cs as ⊆ reverseAcc ds bs
reverseAcc⁺ = HeteroProperties.reverseAcc⁺
ʳ++⁺ : ∀ {cs ds} →
as ⊆ bs →
cs ⊆ ds →
as ʳ++ cs ⊆ bs ʳ++ ds
ʳ++⁺ = reverseAcc⁺
reverse⁺ : as ⊆ bs → reverse as ⊆ reverse bs
reverse⁺ = HeteroProperties.reverse⁺
reverse⁻ : reverse as ⊆ reverse bs → as ⊆ bs
reverse⁻ = HeteroProperties.reverse⁻
------------------------------------------------------------------------
-- merge
module _ {ℓ′} {_≤_ : Rel A ℓ′} (_≤?_ : Decidable₂ _≤_) where
⊆-mergeˡ : ∀ xs ys → xs ⊆ merge _≤?_ xs ys
⊆-mergeˡ [] ys = minimum ys
⊆-mergeˡ (x ∷ xs) [] = ⊆-refl
⊆-mergeˡ (x ∷ xs) (y ∷ ys)
with x ≤? y | ⊆-mergeˡ xs (y ∷ ys)
| ⊆-mergeˡ (x ∷ xs) ys
... | yes x≤y | rec | _ = ≈-refl ∷ rec
... | no x≰y | _ | rec = y ∷ʳ rec
⊆-mergeʳ : ∀ xs ys → ys ⊆ merge _≤?_ xs ys
⊆-mergeʳ [] ys = ⊆-refl
⊆-mergeʳ (x ∷ xs) [] = minimum (merge _≤?_ (x ∷ xs) [])
⊆-mergeʳ (x ∷ xs) (y ∷ ys)
with x ≤? y | ⊆-mergeʳ xs (y ∷ ys)
| ⊆-mergeʳ (x ∷ xs) ys
... | yes x≤y | rec | _ = x ∷ʳ rec
... | no x≰y | _ | rec = ≈-refl ∷ rec
------------------------------------------------------------------------
-- Inversion lemmas
------------------------------------------------------------------------
module _ {a as b bs} where
∷⁻¹ : a ≈ b → as ⊆ bs ⇔ a ∷ as ⊆ b ∷ bs
∷⁻¹ = HeteroProperties.∷⁻¹
∷ʳ⁻¹ : ¬ (a ≈ b) → a ∷ as ⊆ bs ⇔ a ∷ as ⊆ b ∷ bs
∷ʳ⁻¹ = HeteroProperties.∷ʳ⁻¹
------------------------------------------------------------------------
-- Other
------------------------------------------------------------------------
module _ where
length-mono-≤ : ∀ {as bs} → as ⊆ bs → length as ≤ length bs
length-mono-≤ = HeteroProperties.length-mono-≤
------------------------------------------------------------------------
-- Conversion to and from list equality
to-≋ : ∀ {as bs} → length as ≡ length bs → as ⊆ bs → as ≋ bs
to-≋ = HeteroProperties.toPointwise
------------------------------------------------------------------------
-- Irrelevant special case
[]⊆-irrelevant : Irrelevant ([] ⊆_)
[]⊆-irrelevant = HeteroProperties.Sublist-[]-irrelevant
------------------------------------------------------------------------
-- (to/from)∈ is a bijection
module _ {x xs} where
to∈-injective : ∀ {p q : [ x ] ⊆ xs} → to∈ p ≡ to∈ q → p ≡ q
to∈-injective = HeteroProperties.toAny-injective
from∈-injective : ∀ {p q : x ∈ xs} → from∈ p ≡ from∈ q → p ≡ q
from∈-injective = HeteroProperties.fromAny-injective
to∈∘from∈≗id : ∀ (p : x ∈ xs) → to∈ (from∈ p) ≡ p
to∈∘from∈≗id = HeteroProperties.toAny∘fromAny≗id
[x]⊆xs⤖x∈xs : ([ x ] ⊆ xs) ⤖ (x ∈ xs)
[x]⊆xs⤖x∈xs = HeteroProperties.Sublist-[x]-bijection
------------------------------------------------------------------------
-- Properties of Disjoint(ness) and DisjointUnion
open HeteroProperties.Disjointness {R = _≈_} public
open HeteroProperties.DisjointnessMonotonicity {R = _≈_} {S = _≈_} {T = _≈_} trans public
-- Shrinking one of two disjoint lists preserves disjointness.
-- We would have liked to define shrinkDisjointˡ σ = shrinkDisjoint σ ⊆-refl
-- but alas, this is only possible for groupoids, not setoids in general.
shrinkDisjointˡ : ∀ {xs ys zs us} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} (σ : us ⊆ xs) →
Disjoint τ₁ τ₂ →
Disjoint (⊆-trans σ τ₁) τ₂
-- Not affected by σ:
shrinkDisjointˡ σ (y ∷ₙ d) = y ∷ₙ shrinkDisjointˡ σ d
shrinkDisjointˡ σ (y≈z ∷ᵣ d) = y≈z ∷ᵣ shrinkDisjointˡ σ d
-- In σ: keep x.
shrinkDisjointˡ (u≈x ∷ σ) (x≈z ∷ₗ d) = trans u≈x x≈z ∷ₗ shrinkDisjointˡ σ d
-- Not in σ: drop x.
shrinkDisjointˡ (x ∷ʳ σ) (x≈z ∷ₗ d) = _ ∷ₙ shrinkDisjointˡ σ d
shrinkDisjointˡ [] [] = []
shrinkDisjointʳ : ∀ {xs ys zs vs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} (σ : vs ⊆ ys) →
Disjoint τ₁ τ₂ →
Disjoint τ₁ (⊆-trans σ τ₂)
-- Not affected by σ:
shrinkDisjointʳ σ (y ∷ₙ d) = y ∷ₙ shrinkDisjointʳ σ d
shrinkDisjointʳ σ (x≈z ∷ₗ d) = x≈z ∷ₗ shrinkDisjointʳ σ d
-- In σ: keep y.
shrinkDisjointʳ (v≈y ∷ σ) (y≈z ∷ᵣ d) = trans v≈y y≈z ∷ᵣ shrinkDisjointʳ σ d
-- Not in σ: drop y.
shrinkDisjointʳ (y ∷ʳ σ) (y≈z ∷ᵣ d) = _ ∷ₙ shrinkDisjointʳ σ d
shrinkDisjointʳ [] [] = []