Skip to content

Commit d888195

Browse files
committed
Lists: membership lemmas about nested lists
1 parent 3a64d8e commit d888195

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ Additions to existing modules
148148
∈-concatMap⁺ : Any ((y ∈_) ∘ f) xs → y ∈ concatMap f xs
149149
∈-concatMap⁻ : y ∈ concatMap f xs → Any ((y ∈_) ∘ f) xs
150150
∈-++ : v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys)
151+
[]∉map∷ : [] ∉ map (x ∷_) xss
152+
map∷-decomp∈ : (x ∷ xs) ∈ map (y ∷_) xss → x ≡ y × xs ∈ xss
153+
map∷-decomp : xs ∈ map (y ∷_) xss → ∃[ ys ] ys ∈ xss × y ∷ ys ≡ xs
154+
∈-map∷⁻ : xs ∈ map (x ∷_) xss → x ∈ xs
151155
```
152156

153157
* In `Data.List.Membership.Propositional.Properties.WithK`:

src/Data/List/Membership/Propositional/Properties.agda

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Data.List.Membership.Propositional.Properties where
1010

1111
open import Algebra.Core using (Op₂)
1212
open import Algebra.Definitions using (Selective)
13+
open import Data.Empty using (⊥-elim)
1314
open import Data.Fin.Base using (Fin)
1415
open import Data.List.Base as List
1516
open import Data.List.Effectful using (monad)
@@ -24,13 +25,13 @@ open import Data.List.Relation.Unary.Any.Properties
2425
open import Data.Nat.Base using (ℕ; suc; s≤s; _≤_; _<_; _≰_)
2526
open import Data.Nat.Properties
2627
using (suc-injective; m≤n⇒m≤1+n; _≤?_; <⇒≢; ≰⇒>)
27-
open import Data.Product.Base using (∃; ∃₂; _×_; _,_; ∃-syntax)
28+
open import Data.Product.Base using (∃; ∃₂; _×_; _,_; ∃-syntax; -,_; map₂)
2829
open import Data.Product.Properties using (×-≡,≡↔≡)
2930
open import Data.Product.Function.NonDependent.Propositional using (_×-cong_)
3031
import Data.Product.Function.Dependent.Propositional as Σ
3132
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
3233
open import Effect.Monad using (RawMonad)
33-
open import Function.Base using (_∘_; _∘′_; _$_; id; flip; _⟨_⟩_)
34+
open import Function.Base using (_∘_; _∘′_; _$_; id; flip; _⟨_⟩_; _∋_)
3435
open import Function.Definitions using (Injective)
3536
import Function.Related.Propositional as Related
3637
open import Function.Bundles using (_↔_; _↣_; Injection; _⇔_; mk⇔)
@@ -40,7 +41,7 @@ open import Level using (Level)
4041
open import Relation.Binary.Core using (Rel)
4142
open import Relation.Binary.Definitions as Binary hiding (Decidable)
4243
open import Relation.Binary.PropositionalEquality.Core as ≡
43-
using (_≡_; _≢_; refl; sym; trans; cong; cong₂; resp; _≗_)
44+
using (_≡_; _≢_; refl; sym; trans; cong; cong₂; resp; _≗_; subst)
4445
open import Relation.Binary.PropositionalEquality.Properties as ≡ using (setoid)
4546
import Relation.Binary.Properties.DecTotalOrder as DTOProperties
4647
open import Relation.Nullary.Decidable.Core
@@ -55,6 +56,9 @@ private
5556
variable
5657
: Level
5758
A B C : Set
59+
x y v : A
60+
xs ys : List A
61+
xss : List (List A)
5862

5963
------------------------------------------------------------------------
6064
-- Publicly re-export properties from Core
@@ -64,10 +68,10 @@ open import Data.List.Membership.Propositional.Properties.Core public
6468
------------------------------------------------------------------------
6569
-- Equality
6670

67-
∈-resp-≋ : {x : A} (x ∈_) Respects _≋_
71+
∈-resp-≋ : (x ∈_) Respects _≋_
6872
∈-resp-≋ = Membership.∈-resp-≋ (≡.setoid _)
6973

70-
∉-resp-≋ : {x : A} (x ∉_) Respects _≋_
74+
∉-resp-≋ : (x ∉_) Respects _≋_
7175
∉-resp-≋ = Membership.∉-resp-≋ (≡.setoid _)
7276

7377
------------------------------------------------------------------------
@@ -96,14 +100,14 @@ map-mapWith∈ = Membership.map-mapWith∈ (≡.setoid _)
96100

97101
module _ (f : A B) where
98102

99-
∈-map⁺ : {x xs} x ∈ xs f x ∈ map f xs
103+
∈-map⁺ : x ∈ xs f x ∈ map f xs
100104
∈-map⁺ = Membership.∈-map⁺ (≡.setoid A) (≡.setoid B) (cong f)
101105

102-
∈-map⁻ : {y xs} y ∈ map f xs λ x x ∈ xs × y ≡ f x
106+
∈-map⁻ : y ∈ map f xs λ x x ∈ xs × y ≡ f x
103107
∈-map⁻ = Membership.∈-map⁻ (≡.setoid A) (≡.setoid B)
104108

105-
map-∈↔ : {y xs} (∃ λ x x ∈ xs × y ≡ f x) ↔ y ∈ map f xs
106-
map-∈↔ {y} {xs} =
109+
map-∈↔ : (∃ λ x x ∈ xs × y ≡ f x) ↔ y ∈ map f xs
110+
map-∈↔ {xs}{y} =
107111
(∃ λ x x ∈ xs × y ≡ f x) ↔⟨ Any↔ ⟩
108112
Any (λ x y ≡ f x) xs ↔⟨ map↔ ⟩
109113
y ∈ List.map f xs ∎
@@ -114,7 +118,7 @@ module _ (f : A → B) where
114118

115119
module _ {v : A} where
116120

117-
∈-++⁺ˡ : {xs ys} v ∈ xs v ∈ xs ++ ys
121+
∈-++⁺ˡ : v ∈ xs v ∈ xs ++ ys
118122
∈-++⁺ˡ = Membership.∈-++⁺ˡ (≡.setoid A)
119123

120124
∈-++⁺ʳ : xs {ys} v ∈ ys v ∈ xs ++ ys
@@ -123,13 +127,13 @@ module _ {v : A} where
123127
∈-++⁻ : xs {ys} v ∈ xs ++ ys (v ∈ xs) ⊎ (v ∈ ys)
124128
∈-++⁻ = Membership.∈-++⁻ (≡.setoid A)
125129

126-
∈-++ : {xs ys} v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys)
130+
∈-++ : v ∈ xs ++ ys ⇔ (v ∈ xs ⊎ v ∈ ys)
127131
∈-++ = mk⇔ (∈-++⁻ _) Sum.[ ∈-++⁺ˡ , ∈-++⁺ʳ _ ]
128132

129133
∈-insert : xs {ys} v ∈ xs ++ [ v ] ++ ys
130134
∈-insert xs = Membership.∈-insert (≡.setoid A) xs refl
131135

132-
∈-∃++ : {xs} v ∈ xs ∃₂ λ ys zs xs ≡ ys ++ [ v ] ++ zs
136+
∈-∃++ : v ∈ xs ∃₂ λ ys zs xs ≡ ys ++ [ v ] ++ zs
133137
∈-∃++ v∈xs
134138
with ys , zs , _ , refl , eq Membership.∈-∃++ (≡.setoid A) v∈xs
135139
= ys , zs , ≋⇒≡ eq
@@ -139,7 +143,7 @@ module _ {v : A} where
139143

140144
module _ {v : A} where
141145

142-
∈-concat⁺ : {xss} Any (v ∈_) xss v ∈ concat xss
146+
∈-concat⁺ : Any (v ∈_) xss v ∈ concat xss
143147
∈-concat⁺ = Membership.∈-concat⁺ (≡.setoid A)
144148

145149
∈-concat⁻ : xss v ∈ concat xss Any (v ∈_) xss
@@ -154,8 +158,7 @@ module _ {v : A} where
154158
let xs , v∈xs , xs∈xss = Membership.∈-concat⁻′ (≡.setoid A) xss v∈c
155159
in xs , v∈xs , Any.map ≋⇒≡ xs∈xss
156160

157-
concat-∈↔ : {xss : List (List A)}
158-
(∃ λ xs v ∈ xs × xs ∈ xss) ↔ v ∈ concat xss
161+
concat-∈↔ : (∃ λ xs v ∈ xs × xs ∈ xss) ↔ v ∈ concat xss
159162
concat-∈↔ {xss} =
160163
(∃ λ xs v ∈ xs × xs ∈ xss) ↔⟨ Σ.cong (↔-id _) $ ×-comm _ _ ⟩
161164
(∃ λ xs xs ∈ xss × v ∈ xs) ↔⟨ Any↔ ⟩
@@ -262,10 +265,10 @@ module _ {n} {f : Fin n → A} where
262265

263266
module _ {p} {P : A Set p} (P? : Decidable P) where
264267

265-
∈-filter⁺ : {x xs} x ∈ xs P x x ∈ filter P? xs
268+
∈-filter⁺ : x ∈ xs P x x ∈ filter P? xs
266269
∈-filter⁺ = Membership.∈-filter⁺ (≡.setoid A) P? (≡.resp P)
267270

268-
∈-filter⁻ : {v xs} v ∈ filter P? xs v ∈ xs × P v
271+
∈-filter⁻ : v ∈ filter P? xs v ∈ xs × P v
269272
∈-filter⁻ = Membership.∈-filter⁻ (≡.setoid A) P? (≡.resp P)
270273

271274
------------------------------------------------------------------------
@@ -339,13 +342,13 @@ module _ (_≈?_ : DecidableEquality A) where
339342
------------------------------------------------------------------------
340343
-- length
341344

342-
∈-length : {x : A} {xs} x ∈ xs 0 < length xs
345+
∈-length : x ∈ xs 0 < length xs
343346
∈-length = Membership.∈-length (≡.setoid _)
344347

345348
------------------------------------------------------------------------
346349
-- lookup
347350

348-
∈-lookup : {xs : List A} i lookup xs i ∈ xs
351+
∈-lookup : i lookup xs i ∈ xs
349352
∈-lookup {xs = xs} i = Membership.∈-lookup (≡.setoid _) xs i
350353

351354
------------------------------------------------------------------------
@@ -366,7 +369,7 @@ module _ {_•_ : Op₂ A} where
366369
------------------------------------------------------------------------
367370
-- inits
368371

369-
[]∈inits : {a} {A : Set a} (as : List A) [] ∈ inits as
372+
[]∈inits : (as : List A) [] ∈ inits as
370373
[]∈inits _ = here refl
371374

372375
------------------------------------------------------------------------
@@ -443,3 +446,26 @@ module _ {R : A → A → Set ℓ} where
443446
∈-AllPairs₂ (p ∷ _) (here refl) (there y∈) = inj₂ $ inj₁ $ All.lookup p y∈
444447
∈-AllPairs₂ (p ∷ _) (there x∈) (here refl) = inj₂ $ inj₂ $ All.lookup p x∈
445448
∈-AllPairs₂ (_ ∷ ps) (there x∈) (there y∈) = ∈-AllPairs₂ ps x∈ y∈
449+
450+
------------------------------------------------------------------------
451+
-- nested lists
452+
453+
[]∉map∷ : (List A ∋ []) ∉ map (x ∷_) xss
454+
[]∉map∷ {xss = _ ∷ _} (there p) = []∉map∷ p
455+
456+
map∷-decomp∈ : (List A ∋ x ∷ xs) ∈ map (y ∷_) xss x ≡ y × xs ∈ xss
457+
map∷-decomp∈ {xss = _ ∷ _} = λ where
458+
(here refl) refl , here refl
459+
(there p) map₂ there $ map∷-decomp∈ p
460+
461+
map∷-decomp : xs ∈ map (y ∷_) xss ∃[ ys ] ys ∈ xss × y ∷ ys ≡ xs
462+
map∷-decomp {xss = _ ∷ _} (here refl) = -, here refl , refl
463+
map∷-decomp {xs = []} {xss = _ ∷ _} (there xs∈) = ⊥-elim ([]∉map∷ xs∈)
464+
map∷-decomp {xs = x ∷ xs} {xss = _ ∷ _} (there xs∈) =
465+
let eq , p = map∷-decomp∈ xs∈
466+
in -, there p , subst (λ ◆ ∷ _ ≡ _) eq refl
467+
468+
∈-map∷⁻ : xs ∈ map (x ∷_) xss x ∈ xs
469+
∈-map∷⁻ {xss = _ ∷ _} = λ where
470+
(here refl) here refl
471+
(there p) ∈-map∷⁻ p

0 commit comments

Comments
 (0)