Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of membership of vectors based on propositional equality.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Vec.Membership.Propositional.Properties where
10
11 open import Data.Fin.Base using (Fin; zero; suc)
12 open import Data.Product.Base using (_,_; ∃; _×_; -,_; map₁; map₂)
13 open import Data.Vec.Base
14 open import Data.Vec.Relation.Unary.Any using (here; there)
15 open import Data.List.Base using ([]; _∷_)
16 open import Data.List.Relation.Unary.Any as ListAny using (here; there)
17 open import Data.Vec.Relation.Unary.Any as Any using (Any; here; there)
18 open import Data.Vec.Membership.Propositional
19 open import Data.List.Membership.Propositional
20 using () renaming (_∈_ to _∈ₗ_)
21 open import Level using (Level)
22 open import Function.Base using (_∘_; id)
23 open import Relation.Unary using (Pred)
24 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
25
26 private
27 variable
28 a b p : Level
29 A : Set a
30 B : Set b
31
32 ------------------------------------------------------------------------
33 -- lookup
34
35 ∈-lookup : ∀ {n} i (xs : Vec A n) → lookup xs i ∈ xs
36 ∈-lookup zero (x ∷ xs) = here refl
37 ∈-lookup (suc i) (x ∷ xs) = there (∈-lookup i xs)
38
39 index-∈-lookup : ∀ {n} (i : Fin n) (xs : Vec A n) → Any.index (∈-lookup i xs) ≡ i
40 index-∈-lookup zero (x ∷ xs) = refl
41 index-∈-lookup (suc i) (x ∷ xs) = cong suc (index-∈-lookup i xs)
42
43 ------------------------------------------------------------------------
44 -- map
45
46 ∈-map⁺ : (f : A → B) → ∀ {m v} {xs : Vec A m} → v ∈ xs → f v ∈ map f xs
47 ∈-map⁺ f (here refl) = here refl
48 ∈-map⁺ f (there x∈xs) = there (∈-map⁺ f x∈xs)
49
50 ------------------------------------------------------------------------
51 -- _++_
52
53 ∈-++⁺ˡ : ∀ {m n v} {xs : Vec A m} {ys : Vec A n} → v ∈ xs → v ∈ xs ++ ys
54 ∈-++⁺ˡ (here refl) = here refl
55 ∈-++⁺ˡ (there x∈xs) = there (∈-++⁺ˡ x∈xs)
56
57 ∈-++⁺ʳ : ∀ {m n v} (xs : Vec A m) {ys : Vec A n} → v ∈ ys → v ∈ xs ++ ys
58 ∈-++⁺ʳ [] x∈ys = x∈ys
59 ∈-++⁺ʳ (x ∷ xs) x∈ys = there (∈-++⁺ʳ xs x∈ys)
60
61 ------------------------------------------------------------------------
62 -- tabulate
63
64 ∈-tabulate⁺ : ∀ {n} (f : Fin n → A) i → f i ∈ tabulate f
65 ∈-tabulate⁺ f zero = here refl
66 ∈-tabulate⁺ f (suc i) = there (∈-tabulate⁺ (f ∘ suc) i)
67
68 ------------------------------------------------------------------------
69 -- allFin
70
71 ∈-allFin⁺ : ∀ {n} (i : Fin n) → i ∈ allFin n
72 ∈-allFin⁺ = ∈-tabulate⁺ id
73
74 ------------------------------------------------------------------------
75 -- allPairs
76
77 ∈-allPairs⁺ : ∀ {m n x y} {xs : Vec A m} {ys : Vec B n} →
78 x ∈ xs → y ∈ ys → (x , y) ∈ allPairs xs ys
79 ∈-allPairs⁺ {xs = x ∷ xs} (here refl) = ∈-++⁺ˡ ∘ ∈-map⁺ (x ,_)
80 ∈-allPairs⁺ {xs = x ∷ _} (there x∈xs) =
81 ∈-++⁺ʳ (map (x ,_) _) ∘ ∈-allPairs⁺ x∈xs
82
83 ------------------------------------------------------------------------
84 -- toList
85
86 ∈-toList⁺ : ∀ {n} {v : A} {xs : Vec A n} → v ∈ xs → v ∈ₗ toList xs
87 ∈-toList⁺ (here refl) = here refl
88 ∈-toList⁺ (there x∈) = there (∈-toList⁺ x∈)
89
90 ∈-toList⁻ : ∀ {n} {v : A} {xs : Vec A n} → v ∈ₗ toList xs → v ∈ xs
91 ∈-toList⁻ {xs = x ∷ xs} (here refl) = here refl
92 ∈-toList⁻ {xs = x ∷ xs} (there v∈xs) = there (∈-toList⁻ v∈xs)
93
94 ------------------------------------------------------------------------
95 -- fromList
96
97 ∈-fromList⁺ : ∀ {v : A} {xs} → v ∈ₗ xs → v ∈ fromList xs
98 ∈-fromList⁺ (here refl) = here refl
99 ∈-fromList⁺ (there x∈) = there (∈-fromList⁺ x∈)
100
101 ∈-fromList⁻ : ∀ {v : A} {xs} → v ∈ fromList xs → v ∈ₗ xs
102 ∈-fromList⁻ {xs = _ ∷ _} (here refl) = here refl
103 ∈-fromList⁻ {xs = _ ∷ _} (there v∈xs) = there (∈-fromList⁻ v∈xs)
104
105 index-∈-fromList⁺ : ∀ {v : A} {xs} → (v∈xs : v ∈ₗ xs) →
106 Any.index (∈-fromList⁺ v∈xs) ≡ ListAny.index v∈xs
107 index-∈-fromList⁺ (here refl) = refl
108 index-∈-fromList⁺ (there v∈xs) = cong suc (index-∈-fromList⁺ v∈xs)
109
110 ------------------------------------------------------------------------
111 -- Relationship to Any
112
113 module _ {P : Pred A p} where
114
115 fromAny : ∀ {n} {xs : Vec A n} → Any P xs → ∃ λ x → x ∈ xs × P x
116 fromAny (here px) = -, here refl , px
117 fromAny (there v) = map₂ (map₁ there) (fromAny v)
118
119 toAny : ∀ {n x} {xs : Vec A n} → x ∈ xs → P x → Any P xs
120 toAny (here refl) px = here px
121 toAny (there v) px = there (toAny v px)