| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties related to propositional list membership |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Membership.Propositional.Properties where |
| 10 |
|
| 11 |
open import Algebra.Core using (Op₂) |
| 12 |
open import Algebra.Definitions using (Selective) |
| 13 |
open import Data.Fin.Base using (Fin) |
| 14 |
open import Data.List.Base as List |
| 15 |
open import Data.List.Effectful using (monad) |
| 16 |
open import Data.List.Membership.Propositional |
| 17 |
using (_∈_; _∉_; mapWith∈; _≢∈_) |
| 18 |
import Data.List.Membership.Setoid.Properties as Membership |
| 19 |
open import Data.List.Relation.Binary.Equality.Propositional |
| 20 |
using (_≋_; ≡⇒≋; ≋⇒≡) |
| 21 |
open import Data.List.Relation.Unary.Any as Any using (Any; here; there) |
| 22 |
open import Data.List.Relation.Unary.Any.Properties |
| 23 |
using (map↔; concat↔; >>=↔; ⊛↔; Any-cong; ⊗↔′; ¬Any[]) |
| 24 |
open import Data.Nat.Base using (ℕ; suc; s≤s; _≤_; _<_; _≰_) |
| 25 |
open import Data.Nat.Properties |
| 26 |
using (suc-injective; m≤n⇒m≤1+n; _≤?_; <⇒≢; ≰⇒>) |
| 27 |
open import Data.Product.Base using (∃; ∃₂; _×_; _,_; ∃-syntax; -,_; map₂) |
| 28 |
open import Data.Product.Properties using (×-≡,≡↔≡) |
| 29 |
open import Data.Product.Function.NonDependent.Propositional using (_×-cong_) |
| 30 |
import Data.Product.Function.Dependent.Propositional as Σ |
| 31 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 32 |
open import Effect.Monad using (RawMonad) |
| 33 |
open import Function.Base using (_∘_; _∘′_; _$_; id; flip; _⟨_⟩_; _∋_) |
| 34 |
open import Function.Definitions using (Injective) |
| 35 |
import Function.Related.Propositional as Related |
| 36 |
open import Function.Bundles using (_↔_; _↣_; Injection; _⇔_; mk⇔) |
| 37 |
open import Function.Related.TypeIsomorphisms using (×-comm; ∃∃↔∃∃) |
| 38 |
open import Function.Construct.Identity using (↔-id) |
| 39 |
open import Level using (Level) |
| 40 |
open import Relation.Binary.Core using (Rel) |
| 41 |
open import Relation.Binary.Definitions as Binary hiding (Decidable) |
| 42 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 43 |
using (_≡_; _≢_; refl; sym; trans; cong; cong₂; resp; _≗_) |
| 44 |
open import Relation.Binary.PropositionalEquality.Properties as ≡ using (setoid) |
| 45 |
import Relation.Binary.Properties.DecTotalOrder as DTOProperties |
| 46 |
open import Relation.Nullary.Decidable.Core |
| 47 |
using (Dec; yes; no; ¬¬-excluded-middle) |
| 48 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 49 |
open import Relation.Nullary.Reflects using (invert) |
| 50 |
open import Relation.Unary using (_⟨×⟩_; Decidable) |
| 51 |
|
| 52 |
private |
| 53 |
open module ListMonad {ℓ} = RawMonad (monad {ℓ = ℓ}) |
| 54 |
|
| 55 |
variable |
| 56 |
ℓ : Level |
| 57 |
A B C : Set ℓ |
| 58 |
x y v : A |
| 59 |
xs ys : List A |
| 60 |
xss : List (List A) |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Publicly re-export properties from Core |
| 64 |
|
| 65 |
open import Data.List.Membership.Propositional.Properties.Core public |
| 66 |
|
| 67 |
------------------------------------------------------------------------ |
| 68 |
-- Equality |
| 69 |
|
| 70 |
∈-resp-≋ : (x ∈_) Respects _≋_ |
| 71 |
∈-resp-≋ = Membership.∈-resp-≋ (≡.setoid _) |
| 72 |
|
| 73 |
∉-resp-≋ : (x ∉_) Respects _≋_ |
| 74 |
∉-resp-≋ = Membership.∉-resp-≋ (≡.setoid _) |
| 75 |
|
| 76 |
------------------------------------------------------------------------ |
| 77 |
-- mapWith∈ |
| 78 |
|
| 79 |
mapWith∈-cong : ∀ (xs : List A) → (f g : ∀ {x} → x ∈ xs → B) → |
| 80 |
(∀ {x} → (x∈xs : x ∈ xs) → f x∈xs ≡ g x∈xs) → |
| 81 |
mapWith∈ xs f ≡ mapWith∈ xs g |
| 82 |
mapWith∈-cong [] f g cong = refl |
| 83 |
mapWith∈-cong (x ∷ xs) f g cong = cong₂ _∷_ (cong (here refl)) |
| 84 |
(mapWith∈-cong xs (f ∘ there) (g ∘ there) (cong ∘ there)) |
| 85 |
|
| 86 |
mapWith∈≗map : ∀ (f : A → B) xs → mapWith∈ xs (λ {x} _ → f x) ≡ map f xs |
| 87 |
mapWith∈≗map f xs = |
| 88 |
≋⇒≡ (Membership.mapWith∈≗map (≡.setoid _) (≡.setoid _) f xs) |
| 89 |
|
| 90 |
mapWith∈-id : (xs : List A) → mapWith∈ xs (λ {x} _ → x) ≡ xs |
| 91 |
mapWith∈-id = Membership.mapWith∈-id (≡.setoid _) |
| 92 |
|
| 93 |
map-mapWith∈ : (xs : List A) (f : ∀ {x} → x ∈ xs → B) (g : B → C) → |
| 94 |
map g (mapWith∈ xs f) ≡ mapWith∈ xs (g ∘′ f) |
| 95 |
map-mapWith∈ = Membership.map-mapWith∈ (≡.setoid _) |
| 96 |
|
| 97 |
------------------------------------------------------------------------ |
| 98 |
-- map |
| 99 |
|
| 100 |
module _ (f : A → B) where |
| 101 |
|
| 102 |
∈-map⁺ : x ∈ xs → f x ∈ map f xs |
| 103 |
∈-map⁺ = Membership.∈-map⁺ (≡.setoid A) (≡.setoid B) (cong f) |
| 104 |
|
| 105 |
∈-map⁻ : y ∈ map f xs → ∃ λ x → x ∈ xs × y ≡ f x |
| 106 |
∈-map⁻ = Membership.∈-map⁻ (≡.setoid A) (≡.setoid B) |
| 107 |
|
| 108 |
map-∈↔ : (∃ λ x → x ∈ xs × y ≡ f x) ↔ y ∈ map f xs |
| 109 |
map-∈↔ {xs} {y} = |
| 110 |
(∃ λ x → x ∈ xs × y ≡ f x) ↔⟨ Any↔ ⟩ |
| 111 |
Any (λ x → y ≡ f x) xs ↔⟨ map↔ ⟩ |
| 112 |
y ∈ List.map f xs ∎ |
| 113 |
where open Related.EquationalReasoning |
| 114 |
|
| 115 |
------------------------------------------------------------------------ |
| 116 |
-- _++_ |
| 117 |
|
| 118 |
module _ {v : A} where |
| 119 |
|
| 120 |
∈-++⁺ˡ : v ∈ xs → v ∈ xs ++ ys |
| 121 |
∈-++⁺ˡ = Membership.∈-++⁺ˡ (≡.setoid A) |
| 122 |
|
| 123 |
∈-++⁺ʳ : ∀ xs {ys} → v ∈ ys → v ∈ xs ++ ys |
| 124 |
∈-++⁺ʳ = Membership.∈-++⁺ʳ (≡.setoid A) |
| 125 |
|
| 126 |
∈-++⁻ : ∀ xs {ys} → v ∈ xs ++ ys → (v ∈ xs) ⊎ (v ∈ ys) |
| 127 |
∈-++⁻ = Membership.∈-++⁻ (≡.setoid A) |
| 128 |
|
| 129 |
++-∈⇔ : v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys) |
| 130 |
++-∈⇔ = mk⇔ (∈-++⁻ _) Sum.[ ∈-++⁺ˡ , ∈-++⁺ʳ _ ] |
| 131 |
|
| 132 |
∈-insert : ∀ xs {ys} → v ∈ xs ++ [ v ] ++ ys |
| 133 |
∈-insert xs = Membership.∈-insert (≡.setoid A) xs refl |
| 134 |
|
| 135 |
∈-∃++ : v ∈ xs → ∃₂ λ ys zs → xs ≡ ys ++ [ v ] ++ zs |
| 136 |
∈-∃++ v∈xs |
| 137 |
with ys , zs , _ , refl , eq ← Membership.∈-∃++ (≡.setoid A) v∈xs |
| 138 |
= ys , zs , ≋⇒≡ eq |
| 139 |
|
| 140 |
------------------------------------------------------------------------ |
| 141 |
-- concat |
| 142 |
|
| 143 |
module _ {v : A} where |
| 144 |
|
| 145 |
∈-concat⁺ : Any (v ∈_) xss → v ∈ concat xss |
| 146 |
∈-concat⁺ = Membership.∈-concat⁺ (≡.setoid A) |
| 147 |
|
| 148 |
∈-concat⁻ : ∀ xss → v ∈ concat xss → Any (v ∈_) xss |
| 149 |
∈-concat⁻ = Membership.∈-concat⁻ (≡.setoid A) |
| 150 |
|
| 151 |
∈-concat⁺′ : ∀ {vs xss} → v ∈ vs → vs ∈ xss → v ∈ concat xss |
| 152 |
∈-concat⁺′ v∈vs vs∈xss = |
| 153 |
Membership.∈-concat⁺′ (≡.setoid A) v∈vs (Any.map ≡⇒≋ vs∈xss) |
| 154 |
|
| 155 |
∈-concat⁻′ : ∀ xss → v ∈ concat xss → ∃ λ xs → v ∈ xs × xs ∈ xss |
| 156 |
∈-concat⁻′ xss v∈c = |
| 157 |
let xs , v∈xs , xs∈xss = Membership.∈-concat⁻′ (≡.setoid A) xss v∈c |
| 158 |
in xs , v∈xs , Any.map ≋⇒≡ xs∈xss |
| 159 |
|
| 160 |
concat-∈↔ : (∃ λ xs → v ∈ xs × xs ∈ xss) ↔ v ∈ concat xss |
| 161 |
concat-∈↔ {xss} = |
| 162 |
(∃ λ xs → v ∈ xs × xs ∈ xss) ↔⟨ Σ.cong (↔-id _) $ ×-comm _ _ ⟩ |
| 163 |
(∃ λ xs → xs ∈ xss × v ∈ xs) ↔⟨ Any↔ ⟩ |
| 164 |
Any (Any (v ≡_)) xss ↔⟨ concat↔ ⟩ |
| 165 |
v ∈ concat xss ∎ |
| 166 |
where open Related.EquationalReasoning |
| 167 |
|
| 168 |
------------------------------------------------------------------------ |
| 169 |
-- concatMap |
| 170 |
|
| 171 |
module _ (f : A → List B) {xs y} where |
| 172 |
|
| 173 |
private Sᴬ = ≡.setoid A; Sᴮ = ≡.setoid B |
| 174 |
|
| 175 |
∈-concatMap⁺ : Any ((y ∈_) ∘ f) xs → y ∈ concatMap f xs |
| 176 |
∈-concatMap⁺ = Membership.∈-concatMap⁺ Sᴬ Sᴮ |
| 177 |
|
| 178 |
∈-concatMap⁻ : y ∈ concatMap f xs → Any ((y ∈_) ∘ f) xs |
| 179 |
∈-concatMap⁻ = Membership.∈-concatMap⁻ Sᴬ Sᴮ |
| 180 |
|
| 181 |
------------------------------------------------------------------------ |
| 182 |
-- cartesianProductWith |
| 183 |
|
| 184 |
module _ (f : A → B → C) where |
| 185 |
|
| 186 |
∈-cartesianProductWith⁺ : ∀ {xs ys a b} → a ∈ xs → b ∈ ys → |
| 187 |
f a b ∈ cartesianProductWith f xs ys |
| 188 |
∈-cartesianProductWith⁺ = Membership.∈-cartesianProductWith⁺ |
| 189 |
(≡.setoid A) (≡.setoid B) (≡.setoid C) (cong₂ f) |
| 190 |
|
| 191 |
∈-cartesianProductWith⁻ : ∀ xs ys {v} → v ∈ cartesianProductWith f xs ys → |
| 192 |
∃₂ λ a b → a ∈ xs × b ∈ ys × v ≡ f a b |
| 193 |
∈-cartesianProductWith⁻ = Membership.∈-cartesianProductWith⁻ |
| 194 |
(≡.setoid A) (≡.setoid B) (≡.setoid C) f |
| 195 |
|
| 196 |
------------------------------------------------------------------------ |
| 197 |
-- cartesianProduct |
| 198 |
|
| 199 |
∈-cartesianProduct⁺ : ∀ {x : A} {y : B} {xs ys} → x ∈ xs → y ∈ ys → |
| 200 |
(x , y) ∈ cartesianProduct xs ys |
| 201 |
∈-cartesianProduct⁺ = ∈-cartesianProductWith⁺ _,_ |
| 202 |
|
| 203 |
∈-cartesianProduct⁻ : ∀ xs ys {xy@(x , y) : A × B} → |
| 204 |
xy ∈ cartesianProduct xs ys → x ∈ xs × y ∈ ys |
| 205 |
∈-cartesianProduct⁻ xs ys xy∈p[xs,ys] |
| 206 |
with _ , _ , x∈xs , y∈ys , refl ← ∈-cartesianProductWith⁻ _,_ xs ys xy∈p[xs,ys] |
| 207 |
= x∈xs , y∈ys |
| 208 |
|
| 209 |
------------------------------------------------------------------------ |
| 210 |
-- applyUpTo |
| 211 |
|
| 212 |
module _ (f : ℕ → A) where |
| 213 |
|
| 214 |
∈-applyUpTo⁺ : ∀ {i n} → i < n → f i ∈ applyUpTo f n |
| 215 |
∈-applyUpTo⁺ = Membership.∈-applyUpTo⁺ (≡.setoid _) f |
| 216 |
|
| 217 |
∈-applyUpTo⁻ : ∀ {v n} → v ∈ applyUpTo f n → |
| 218 |
∃ λ i → i < n × v ≡ f i |
| 219 |
∈-applyUpTo⁻ = Membership.∈-applyUpTo⁻ (≡.setoid _) f |
| 220 |
|
| 221 |
------------------------------------------------------------------------ |
| 222 |
-- upTo |
| 223 |
|
| 224 |
∈-upTo⁺ : ∀ {n i} → i < n → i ∈ upTo n |
| 225 |
∈-upTo⁺ = ∈-applyUpTo⁺ id |
| 226 |
|
| 227 |
∈-upTo⁻ : ∀ {n i} → i ∈ upTo n → i < n |
| 228 |
∈-upTo⁻ p with _ , i<n , refl ← ∈-applyUpTo⁻ id p = i<n |
| 229 |
|
| 230 |
------------------------------------------------------------------------ |
| 231 |
-- applyDownFrom |
| 232 |
|
| 233 |
module _ (f : ℕ → A) where |
| 234 |
|
| 235 |
∈-applyDownFrom⁺ : ∀ {i n} → i < n → f i ∈ applyDownFrom f n |
| 236 |
∈-applyDownFrom⁺ = Membership.∈-applyDownFrom⁺ (≡.setoid _) f |
| 237 |
|
| 238 |
∈-applyDownFrom⁻ : ∀ {v n} → v ∈ applyDownFrom f n → |
| 239 |
∃ λ i → i < n × v ≡ f i |
| 240 |
∈-applyDownFrom⁻ = Membership.∈-applyDownFrom⁻ (≡.setoid _) f |
| 241 |
|
| 242 |
------------------------------------------------------------------------ |
| 243 |
-- downFrom |
| 244 |
|
| 245 |
∈-downFrom⁺ : ∀ {n i} → i < n → i ∈ downFrom n |
| 246 |
∈-downFrom⁺ i<n = ∈-applyDownFrom⁺ id i<n |
| 247 |
|
| 248 |
∈-downFrom⁻ : ∀ {n i} → i ∈ downFrom n → i < n |
| 249 |
∈-downFrom⁻ p with _ , i<n , refl ← ∈-applyDownFrom⁻ id p = i<n |
| 250 |
|
| 251 |
------------------------------------------------------------------------ |
| 252 |
-- tabulate |
| 253 |
|
| 254 |
module _ {n} {f : Fin n → A} where |
| 255 |
|
| 256 |
∈-tabulate⁺ : ∀ i → f i ∈ tabulate f |
| 257 |
∈-tabulate⁺ = Membership.∈-tabulate⁺ (≡.setoid _) |
| 258 |
|
| 259 |
∈-tabulate⁻ : ∀ {v} → v ∈ tabulate f → ∃ λ i → v ≡ f i |
| 260 |
∈-tabulate⁻ = Membership.∈-tabulate⁻ (≡.setoid _) |
| 261 |
|
| 262 |
------------------------------------------------------------------------ |
| 263 |
-- filter |
| 264 |
|
| 265 |
module _ {p} {P : A → Set p} (P? : Decidable P) where |
| 266 |
|
| 267 |
∈-filter⁺ : x ∈ xs → P x → x ∈ filter P? xs |
| 268 |
∈-filter⁺ = Membership.∈-filter⁺ (≡.setoid A) P? (≡.resp P) |
| 269 |
|
| 270 |
∈-filter⁻ : v ∈ filter P? xs → v ∈ xs × P v |
| 271 |
∈-filter⁻ = Membership.∈-filter⁻ (≡.setoid A) P? (≡.resp P) |
| 272 |
|
| 273 |
------------------------------------------------------------------------ |
| 274 |
-- map∘filter |
| 275 |
|
| 276 |
module _ (f : A → B) {p} {P : A → Set p} (P? : Decidable P) {f xs y} where |
| 277 |
|
| 278 |
private Sᴬ = ≡.setoid A; Sᴮ = ≡.setoid B; respP = ≡.resp P |
| 279 |
|
| 280 |
∈-map∘filter⁻ : y ∈ map f (filter P? xs) → (∃[ x ] x ∈ xs × y ≡ f x × P x) |
| 281 |
∈-map∘filter⁻ = Membership.∈-map∘filter⁻ Sᴬ Sᴮ P? respP |
| 282 |
|
| 283 |
∈-map∘filter⁺ : (∃[ x ] x ∈ xs × y ≡ f x × P x) → y ∈ map f (filter P? xs) |
| 284 |
∈-map∘filter⁺ = Membership.∈-map∘filter⁺ Sᴬ Sᴮ P? respP (cong f) |
| 285 |
|
| 286 |
------------------------------------------------------------------------ |
| 287 |
-- derun and deduplicate |
| 288 |
|
| 289 |
module _ {r} {R : Rel A r} (R? : Binary.Decidable R) where |
| 290 |
|
| 291 |
∈-derun⁻ : ∀ xs {z} → z ∈ derun R? xs → z ∈ xs |
| 292 |
∈-derun⁻ xs z∈derun[R,xs] = Membership.∈-derun⁻ (≡.setoid A) R? xs z∈derun[R,xs] |
| 293 |
|
| 294 |
∈-deduplicate⁻ : ∀ xs {z} → z ∈ deduplicate R? xs → z ∈ xs |
| 295 |
∈-deduplicate⁻ xs z∈dedup[R,xs] = Membership.∈-deduplicate⁻ (≡.setoid A) R? xs z∈dedup[R,xs] |
| 296 |
|
| 297 |
module _ (_≈?_ : DecidableEquality A) where |
| 298 |
|
| 299 |
∈-derun⁺ : ∀ {xs z} → z ∈ xs → z ∈ derun _≈?_ xs |
| 300 |
∈-derun⁺ z∈xs = Membership.∈-derun⁺ (≡.setoid A) _≈?_ (flip trans) z∈xs |
| 301 |
|
| 302 |
private resp≈ = λ {c b a : A} (c≡b : c ≡ b) (a≡b : a ≡ b) → trans a≡b (sym c≡b) |
| 303 |
|
| 304 |
∈-deduplicate⁺ : ∀ {xs z} → z ∈ xs → z ∈ deduplicate _≈?_ xs |
| 305 |
∈-deduplicate⁺ z∈xs = Membership.∈-deduplicate⁺ (≡.setoid A) _≈?_ resp≈ z∈xs |
| 306 |
|
| 307 |
deduplicate-∈⇔ : ∀ {xs z} → z ∈ xs ⇔ z ∈ deduplicate _≈?_ xs |
| 308 |
deduplicate-∈⇔ = Membership.deduplicate-∈⇔ (≡.setoid A) _≈?_ resp≈ |
| 309 |
|
| 310 |
------------------------------------------------------------------------ |
| 311 |
-- _>>=_ |
| 312 |
|
| 313 |
>>=-∈↔ : ∀ {xs} {f : A → List B} {y} → |
| 314 |
(∃ λ x → x ∈ xs × y ∈ f x) ↔ y ∈ (xs >>= f) |
| 315 |
>>=-∈↔ {xs = xs} {f} {y} = |
| 316 |
(∃ λ x → x ∈ xs × y ∈ f x) ↔⟨ Any↔ ⟩ |
| 317 |
Any (Any (y ≡_) ∘ f) xs ↔⟨ >>=↔ ⟩ |
| 318 |
y ∈ (xs >>= f) ∎ |
| 319 |
where open Related.EquationalReasoning |
| 320 |
|
| 321 |
------------------------------------------------------------------------ |
| 322 |
-- _⊛_ |
| 323 |
|
| 324 |
⊛-∈↔ : ∀ (fs : List (A → B)) {xs y} → |
| 325 |
(∃₂ λ f x → f ∈ fs × x ∈ xs × y ≡ f x) ↔ y ∈ (fs ⊛ xs) |
| 326 |
⊛-∈↔ fs {xs} {y} = |
| 327 |
(∃₂ λ f x → f ∈ fs × x ∈ xs × y ≡ f x) ↔⟨ Σ.cong (↔-id _) (∃∃↔∃∃ _) ⟩ |
| 328 |
(∃ λ f → f ∈ fs × ∃ λ x → x ∈ xs × y ≡ f x) ↔⟨ Σ.cong (↔-id _) (↔-id _ ⟨ _×-cong_ ⟩ Any↔) ⟩ |
| 329 |
(∃ λ f → f ∈ fs × Any (_≡_ y ∘ f) xs) ↔⟨ Any↔ ⟩ |
| 330 |
Any (λ f → Any (_≡_ y ∘ f) xs) fs ↔⟨ ⊛↔ ⟩ |
| 331 |
y ∈ (fs ⊛ xs) ∎ |
| 332 |
where open Related.EquationalReasoning |
| 333 |
|
| 334 |
------------------------------------------------------------------------ |
| 335 |
-- _⊗_ |
| 336 |
|
| 337 |
⊗-∈↔ : ∀ {xs ys} {x : A} {y : B} → |
| 338 |
(x ∈ xs × y ∈ ys) ↔ (x , y) ∈ (xs ⊗ ys) |
| 339 |
⊗-∈↔ {xs = xs} {ys} {x} {y} = |
| 340 |
(x ∈ xs × y ∈ ys) ↔⟨ ⊗↔′ ⟩ |
| 341 |
Any (x ≡_ ⟨×⟩ y ≡_) (xs ⊗ ys) ↔⟨ Any-cong (λ _ → ×-≡,≡↔≡) (↔-id _) ⟩ |
| 342 |
(x , y) ∈ (xs ⊗ ys) ∎ |
| 343 |
where |
| 344 |
open Related.EquationalReasoning |
| 345 |
|
| 346 |
------------------------------------------------------------------------ |
| 347 |
-- length |
| 348 |
|
| 349 |
∈-length : x ∈ xs → 0 < length xs |
| 350 |
∈-length = Membership.∈-length (≡.setoid _) |
| 351 |
|
| 352 |
------------------------------------------------------------------------ |
| 353 |
-- lookup |
| 354 |
|
| 355 |
∈-lookup : ∀ i → lookup xs i ∈ xs |
| 356 |
∈-lookup {xs = xs} i = Membership.∈-lookup (≡.setoid _) xs i |
| 357 |
|
| 358 |
------------------------------------------------------------------------ |
| 359 |
-- foldr |
| 360 |
|
| 361 |
module _ {_•_ : Op₂ A} where |
| 362 |
|
| 363 |
foldr-selective : Selective _≡_ _•_ → ∀ e xs → |
| 364 |
(foldr _•_ e xs ≡ e) ⊎ (foldr _•_ e xs ∈ xs) |
| 365 |
foldr-selective = Membership.foldr-selective (≡.setoid A) |
| 366 |
|
| 367 |
------------------------------------------------------------------------ |
| 368 |
-- allFin |
| 369 |
|
| 370 |
∈-allFin : ∀ {n} (k : Fin n) → k ∈ allFin n |
| 371 |
∈-allFin = ∈-tabulate⁺ |
| 372 |
|
| 373 |
------------------------------------------------------------------------ |
| 374 |
-- inits |
| 375 |
|
| 376 |
[]∈inits : (as : List A) → [] ∈ inits as |
| 377 |
[]∈inits _ = here refl |
| 378 |
|
| 379 |
------------------------------------------------------------------------ |
| 380 |
-- Other properties |
| 381 |
|
| 382 |
-- Only a finite number of distinct elements can be members of a |
| 383 |
-- given list. |
| 384 |
|
| 385 |
finite : (inj : ℕ ↣ A) → ∀ xs → ¬ (∀ i → Injection.to inj i ∈ xs) |
| 386 |
finite inj [] fᵢ∈[] = ¬Any[] (fᵢ∈[] 0) |
| 387 |
finite inj (x ∷ xs) fᵢ∈x∷xs = ¬¬-excluded-middle helper |
| 388 |
where |
| 389 |
open Injection inj renaming (injective to f-inj) |
| 390 |
|
| 391 |
f : ℕ → _ |
| 392 |
f = to |
| 393 |
|
| 394 |
not-x : ∀ {i} → f i ≢ x → f i ∈ xs |
| 395 |
not-x {i} fᵢ≢x with fᵢ∈x∷xs i |
| 396 |
... | here fᵢ≡x = contradiction fᵢ≡x fᵢ≢x |
| 397 |
... | there fᵢ∈xs = fᵢ∈xs |
| 398 |
|
| 399 |
helper : ¬ Dec (∃ λ i → f i ≡ x) |
| 400 |
helper (no fᵢ≢x) = finite inj xs (λ i → not-x (fᵢ≢x ∘ _,_ i)) |
| 401 |
helper (yes (i , fᵢ≡x)) = finite f′-inj xs f′ⱼ∈xs |
| 402 |
where |
| 403 |
f′ : ℕ → _ |
| 404 |
f′ j with i ≤? j |
| 405 |
... | yes _ = f (suc j) |
| 406 |
... | no _ = f j |
| 407 |
|
| 408 |
∈-if-not-i : ∀ {j} → i ≢ j → f j ∈ xs |
| 409 |
∈-if-not-i i≢j = not-x (i≢j ∘ f-inj ∘ trans fᵢ≡x ∘ sym) |
| 410 |
|
| 411 |
lemma : ∀ {k j} → i ≤ j → i ≰ k → suc j ≢ k |
| 412 |
lemma i≤j i≰1+j refl = i≰1+j (m≤n⇒m≤1+n i≤j) |
| 413 |
|
| 414 |
f′ⱼ∈xs : ∀ j → f′ j ∈ xs |
| 415 |
f′ⱼ∈xs j with i ≤? j |
| 416 |
... | yes i≤j = ∈-if-not-i (<⇒≢ (s≤s i≤j)) |
| 417 |
... | no i≰j = ∈-if-not-i (<⇒≢ (≰⇒> i≰j) ∘ sym) |
| 418 |
|
| 419 |
f′-injective′ : Injective _≡_ _≡_ f′ |
| 420 |
f′-injective′ {j} {k} eq with i ≤? j | i ≤? k |
| 421 |
... | yes i≤j | yes i≤k = suc-injective (f-inj eq) |
| 422 |
... | yes i≤j | no i≰k = contradiction (f-inj eq) (lemma i≤j i≰k) |
| 423 |
... | no i≰j | yes i≤k = contradiction (f-inj eq) (lemma i≤k i≰j ∘ sym) |
| 424 |
... | no i≰j | no i≰k = f-inj eq |
| 425 |
|
| 426 |
f′-inj : ℕ ↣ _ |
| 427 |
f′-inj = record |
| 428 |
{ to = f′ |
| 429 |
; cong = ≡.cong f′ |
| 430 |
; injective = f′-injective′ |
| 431 |
} |
| 432 |
|
| 433 |
------------------------------------------------------------------------ |
| 434 |
-- Different members |
| 435 |
|
| 436 |
there-injective-≢∈ : ∀ {xs} {x y z : A} {x∈xs : x ∈ xs} {y∈xs : y ∈ xs} → |
| 437 |
there {x = z} x∈xs ≢∈ there y∈xs → |
| 438 |
x∈xs ≢∈ y∈xs |
| 439 |
there-injective-≢∈ neq refl eq = neq refl (≡.cong there eq) |
| 440 |
|
| 441 |
------------------------------------------------------------------------ |
| 442 |
-- AllPairs |
| 443 |
|
| 444 |
open import Data.List.Relation.Unary.AllPairs using (AllPairs; []; _∷_) |
| 445 |
import Data.List.Relation.Unary.All as All |
| 446 |
|
| 447 |
module _ {R : A → A → Set ℓ} where |
| 448 |
∈-AllPairs₂ : ∀ {xs x y} → AllPairs R xs → x ∈ xs → y ∈ xs → x ≡ y ⊎ R x y ⊎ R y x |
| 449 |
∈-AllPairs₂ (_ ∷ _) (here refl) (here refl) = inj₁ refl |
| 450 |
∈-AllPairs₂ (p ∷ _) (here refl) (there y∈) = inj₂ $ inj₁ $ All.lookup p y∈ |
| 451 |
∈-AllPairs₂ (p ∷ _) (there x∈) (here refl) = inj₂ $ inj₂ $ All.lookup p x∈ |
| 452 |
∈-AllPairs₂ (_ ∷ ps) (there x∈) (there y∈) = ∈-AllPairs₂ ps x∈ y∈ |
| 453 |
|
| 454 |
------------------------------------------------------------------------ |
| 455 |
-- nested lists |
| 456 |
|
| 457 |
map∷⁻ : xs ∈ map (y ∷_) xss → ∃[ ys ] ys ∈ xss × xs ≡ y ∷ ys |
| 458 |
map∷⁻ = ∈-map⁻ (_ ∷_) |
| 459 |
|
| 460 |
[]∉map∷ : (List A ∋ []) ∉ map (x ∷_) xss |
| 461 |
[]∉map∷ p with () ← map∷⁻ p |
| 462 |
|
| 463 |
map∷-decomp∈ : (List A ∋ x ∷ xs) ∈ map (y ∷_) xss → x ≡ y × xs ∈ xss |
| 464 |
map∷-decomp∈ p with _ , xs∈xss , refl ← map∷⁻ p = refl , xs∈xss |
| 465 |
|
| 466 |
∈-map∷⁻ : xs ∈ map (x ∷_) xss → x ∈ xs |
| 467 |
∈-map∷⁻ p with _ , _ , refl ← map∷⁻ p = here refl |