1
1
------------------------------------------------------------------------
2
2
-- Lists where at least one element satisfies a given property
3
3
------------------------------------------------------------------------
4
+
4
5
{-# OPTIONS --universe-polymorphism #-}
5
6
6
7
module Data.List.Any where
@@ -26,7 +27,8 @@ open import Relation.Binary.PropositionalEquality as PropEq
26
27
27
28
-- Any P xs means that at least one element in xs satisfies P.
28
29
29
- data Any {a p : Level} {A : Set a} (P : A → Set p) : List A → Set (a ⊔ p) where
30
+ data Any {a p} {A : Set a}
31
+ (P : A → Set p) : List A → Set (a ⊔ p) where
30
32
here : ∀ {x xs} (px : P x) → Any P (x ∷ xs)
31
33
there : ∀ {x xs} (pxs : Any P xs) → Any P (x ∷ xs)
32
34
@@ -39,14 +41,14 @@ map g (there pxs) = there (map g pxs)
39
41
40
42
-- If the head does not satisfy the predicate, then the tail will.
41
43
42
- tail : ∀ {a} {A : Set a} {x : A} {xs : List A} {P : A → Set } →
44
+ tail : ∀ {a p } {A : Set a} {x : A} {xs : List A} {P : A → Set p } →
43
45
¬ P x → Any P (x ∷ xs) → Any P xs
44
46
tail ¬px (here px) = ⊥-elim (¬px px)
45
47
tail ¬px (there pxs) = pxs
46
48
47
49
-- Decides Any.
48
50
49
- any : {a : Level } {A : Set a} {P : A → Set } →
51
+ any : ∀ {a p } {A : Set a} {P : A → Set p } →
50
52
(∀ x → Dec (P x)) → (xs : List A) → Dec (Any P xs)
51
53
any p [] = no λ ()
52
54
any p (x ∷ xs) with p x
@@ -55,7 +57,8 @@ any p (x ∷ xs) | no ¬px = Dec.map′ there (tail ¬px) (any p xs)
55
57
56
58
-- index x∈xs is the list position (zero-based) which x∈xs points to.
57
59
58
- index : ∀ {a} {A : Set a} {P : A → Set } {xs} → Any P xs → Fin (List.length xs)
60
+ index : ∀ {a p} {A : Set a} {P : A → Set p} {xs} →
61
+ Any P xs → Fin (List.length xs)
59
62
index (here px) = zero
60
63
index (there pxs) = suc (index pxs)
61
64
@@ -72,7 +75,8 @@ module Membership {c ℓ : Level} (S : Setoid c ℓ) where
72
75
-- If a predicate P respects the underlying equality then Any P
73
76
-- respects the list equality.
74
77
75
- lift-resp : ∀ {P} → P Respects _≈_ → Any P Respects _≋_
78
+ lift-resp : ∀ {p} {P : A → Set p} →
79
+ P Respects _≈_ → Any P Respects _≋_
76
80
lift-resp resp [] ()
77
81
lift-resp resp (x≈y ∷ xs≈ys) (here px) = here (resp x≈y px)
78
82
lift-resp resp (x≈y ∷ xs≈ys) (there pxs) =
@@ -82,7 +86,7 @@ module Membership {c ℓ : Level} (S : Setoid c ℓ) where
82
86
83
87
infix 4 _∈_ _∉_
84
88
85
- _∈_ : A → List A → Set (c ⊔ ℓ)
89
+ _∈_ : A → List A → Set _
86
90
x ∈ xs = Any (_≈_ x) xs
87
91
88
92
_∉_ : A → List A → Set _
@@ -126,18 +130,20 @@ module Membership {c ℓ : Level} (S : Setoid c ℓ) where
126
130
127
131
-- A variant of List.map.
128
132
129
- map-with-∈ : ∀ {b} {B : Set b} (xs : List A) → (∀ {x} → x ∈ xs → B) → List B
133
+ map-with-∈ : ∀ {b} {B : Set b}
134
+ (xs : List A) → (∀ {x} → x ∈ xs → B) → List B
130
135
map-with-∈ [] f = []
131
136
map-with-∈ (x ∷ xs) f = f (here S.refl) ∷ map-with-∈ xs (f ∘ there)
132
137
133
138
-- Finds an element satisfying the predicate.
134
139
135
- find : ∀ {p} {P : A → Set p} {xs} → Any P xs → ∃ λ x → x ∈ xs × P x
140
+ find : ∀ {p} {P : A → Set p} {xs} →
141
+ Any P xs → ∃ λ x → x ∈ xs × P x
136
142
find (here px) = (_ , here S.refl , px)
137
143
find (there pxs) = Prod.map id (Prod.map there id) (find pxs)
138
144
139
- lose : ∀ {p} {P : A → Set p} {x xs} → P Respects _≈_ →
140
- x ∈ xs → P x → Any P xs
145
+ lose : ∀ {p} {P : A → Set p} {x xs} →
146
+ P Respects _≈_ → x ∈ xs → P x → Any P xs
141
147
lose resp x∈xs px = map (flip resp px) x∈xs
142
148
143
149
-- The code above instantiated (and slightly changed) for
@@ -149,12 +155,13 @@ module Membership-≡ where
149
155
open module M {a} {A : Set a} = Membership (PropEq.setoid A) public
150
156
hiding (lift-resp; lose; ⊆-preorder; module ⊆-Reasoning )
151
157
152
- lose : ∀ {a p} {A : Set a} {P : A → Set p} {x xs} → x ∈ xs → P x → Any P xs
158
+ lose : ∀ {a p} {A : Set a} {P : A → Set p} {x xs} →
159
+ x ∈ xs → P x → Any P xs
153
160
lose {P = P} = M.lose (PropEq.subst P)
154
161
155
162
-- _⊆_ is a preorder.
156
163
157
- ⊆-preorder : {a : Level } → Set a → Preorder _ _ _
164
+ ⊆-preorder : ∀ {a } → Set a → Preorder _ _ _
158
165
⊆-preorder A = Ind.InducedPreorder₂ (PropEq.setoid (List A)) _∈_
159
166
(PropEq.subst (_∈_ _))
160
167
@@ -163,17 +170,17 @@ module Membership-≡ where
163
170
open Inv public
164
171
using (Kind) renaming (equivalent to set; inverse to bag)
165
172
166
- [_]-Equality : Kind → {a : Level } → Set a → Setoid _ _
173
+ [_]-Equality : Kind → ∀ {a } → Set a → Setoid _ _
167
174
[ k ]-Equality A = Inv.InducedEquivalence₂ k (_∈_ {A = A})
168
175
169
176
infix 4 _≈[_]_
170
177
171
- _≈[_]_ : {a : Level } {A : Set a} → List A → Kind → List A → Set _
172
- _≈[_]_ {a} { A} xs k ys = Setoid._≈_ ([ k ]-Equality A) xs ys
178
+ _≈[_]_ : ∀ {a } {A : Set a} → List A → Kind → List A → Set _
179
+ _≈[_]_ {A = A} xs k ys = Setoid._≈_ ([ k ]-Equality A) xs ys
173
180
174
181
-- Bag equality implies set equality.
175
182
176
- bag-=⇒set-= : {a : Level } {A : Set a} {xs ys : List A} →
183
+ bag-=⇒set-= : ∀ {a } {A : Set a} {xs ys : List A} →
177
184
xs ≈[ bag ] ys → xs ≈[ set ] ys
178
185
bag-=⇒set-= xs≈ys = Inv.⇿⇒ xs≈ys
179
186
@@ -182,17 +189,17 @@ module Membership-≡ where
182
189
module ⊆-Reasoning where
183
190
import Relation.Binary.PreorderReasoning as PreR
184
191
private
185
- open module PR {A : Set } = PreR (⊆-preorder A) public
192
+ open module PR {a} { A : Set a } = PreR (⊆-preorder A) public
186
193
renaming (_∼⟨_⟩_ to _⊆⟨_⟩_; _≈⟨_⟩_ to _≡⟨_⟩_)
187
194
188
195
infixr 2 _≈⟨_⟩_
189
196
infix 1 _∈⟨_⟩_
190
197
191
- _∈⟨_⟩_ : ∀ {A : Set } x {xs ys : List A} →
198
+ _∈⟨_⟩_ : ∀ {a} { A : Set a } x {xs ys : List A} →
192
199
x ∈ xs → xs IsRelatedTo ys → x ∈ ys
193
200
x ∈⟨ x∈xs ⟩ xs⊆ys = (begin xs⊆ys) x∈xs
194
201
195
- _≈⟨_⟩_ : ∀ {k} {A : Set } xs {ys zs : List A} →
202
+ _≈⟨_⟩_ : ∀ {k a } {A : Set a } xs {ys zs : List A} →
196
203
xs ≈[ k ] ys → ys IsRelatedTo zs → xs IsRelatedTo zs
197
204
xs ≈⟨ xs≈ys ⟩ ys≈zs =
198
205
xs ⊆⟨ _⟨$⟩_ (Equivalent.to (Inv.⇒⇔ xs≈ys)) ⟩ ys≈zs
@@ -202,5 +209,6 @@ module Membership-≡ where
202
209
203
210
-- If any element satisfies P, then P is satisfied.
204
211
205
- satisfied : ∀ {a p} {A : Set a} {P : A → Set p} {xs} → Any P xs → ∃ P
212
+ satisfied : ∀ {a p} {A : Set a} {P : A → Set p} {xs} →
213
+ Any P xs → ∃ P
206
214
satisfied = Prod.map id Prod.proj₂ ∘ Membership-≡.find
0 commit comments