Debian Codegraph @

src/Data/Vec/Relation/Binary/Pointwise/Inductive.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Inductive pointwise lifting of relations to vectors
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Vec.Relation.Binary.Pointwise.Inductive where
10
11 open import Algebra.Definitions
12 using (Associative; Commutative; LeftIdentity; RightIdentity; Congruent₂)
13 open import Data.Fin.Base using (Fin; zero; suc)
14 open import Data.Nat.Base using (ℕ; zero; suc)
15 open import Data.Product.Base using (_×_; _,_; uncurry; <_,_>)
16 open import Data.Vec.Base as Vec hiding ([_]; head; tail; map; lookup; uncons)
17 open import Data.Vec.Relation.Unary.All using (All; []; _∷_)
18 open import Level using (Level; _⊔_)
19 open import Function.Base using (_∘_)
20 open import Function.Bundles using (_⇔_; mk⇔)
21 open import Relation.Binary.Core using (REL; Rel; _⇒_)
22 open import Relation.Binary.Bundles using (Setoid; DecSetoid)
23 open import Relation.Binary.Structures
24 using (IsEquivalence; IsDecEquivalence)
25 open import Relation.Binary.Definitions
26 using (Trans; Decidable; Reflexive; Sym)
27 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
28 open import Relation.Nullary.Decidable using (yes; no; _×-dec_; map′)
29 open import Relation.Unary using (Pred)
30
31 private
32 variable
33 a b c d ℓ ℓ₁ ℓ₂ : Level
34 A : Set a
35 B : Set b
36 C : Set c
37 D : Set d
38 n : ℕ
39
40 ------------------------------------------------------------------------
41 -- Definition
42
43 infixr 5 _∷_
44
45 data Pointwise {a b ℓ} {A : Set a} {B : Set b} (_∼_ : REL A B ℓ) :
46 ∀ {m n} (xs : Vec A m) (ys : Vec B n) → Set (a ⊔ b ⊔ ℓ)
47 where
48 [] : Pointwise _∼_ [] []
49 _∷_ : ∀ {m n x y} {xs : Vec A m} {ys : Vec B n}
50 (x∼y : x ∼ y) (xs∼ys : Pointwise _∼_ xs ys) →
51 Pointwise _∼_ (x ∷ xs) (y ∷ ys)
52
53 ------------------------------------------------------------------------
54 -- Properties
55
56 length-equal : ∀ {m n} {_∼_ : REL A B ℓ} {xs : Vec A m} {ys : Vec B n} →
57 Pointwise _∼_ xs ys → m ≡ n
58 length-equal [] = ≡.refl
59 length-equal (_ ∷ xs∼ys) = ≡.cong suc (length-equal xs∼ys)
60
61 ------------------------------------------------------------------------
62 -- Operations
63
64 module _ {_∼_ : REL A B ℓ} where
65
66 head : ∀ {m n x y} {xs : Vec A m} {ys : Vec B n} →
67 Pointwise _∼_ (x ∷ xs) (y ∷ ys) → x ∼ y
68 head (x∼y ∷ xs∼ys) = x∼y
69
70 tail : ∀ {m n x y} {xs : Vec A m} {ys : Vec B n} →
71 Pointwise _∼_ (x ∷ xs) (y ∷ ys) → Pointwise _∼_ xs ys
72 tail (x∼y ∷ xs∼ys) = xs∼ys
73
74 uncons : ∀ {m n x y} {xs : Vec A m} {ys : Vec B n} →
75 Pointwise _∼_ (x ∷ xs) (y ∷ ys) → x ∼ y × Pointwise _∼_ xs ys
76 uncons = < head , tail >
77
78 lookup : ∀ {n} {xs : Vec A n} {ys : Vec B n} → Pointwise _∼_ xs ys →
79 ∀ i → (Vec.lookup xs i) ∼ (Vec.lookup ys i)
80 lookup (x∼y ∷ _) zero = x∼y
81 lookup (_ ∷ xs∼ys) (suc i) = lookup xs∼ys i
82
83 map : ∀ {ℓ₂} {_≈_ : REL A B ℓ₂} →
84 _≈_ ⇒ _∼_ → ∀ {m n} → Pointwise _≈_ ⇒ Pointwise _∼_ {m} {n}
85 map ∼₁⇒∼₂ [] = []
86 map ∼₁⇒∼₂ (x∼y ∷ xs∼ys) = ∼₁⇒∼₂ x∼y ∷ map ∼₁⇒∼₂ xs∼ys
87
88 ------------------------------------------------------------------------
89 -- Relational properties
90
91 refl : ∀ {_∼_ : Rel A ℓ} {n} →
92 Reflexive _∼_ → Reflexive (Pointwise _∼_ {n})
93 refl ∼-refl {[]} = []
94 refl ∼-refl {x ∷ xs} = ∼-refl ∷ refl ∼-refl
95
96 sym : ∀ {P : REL A B ℓ} {Q : REL B A ℓ} {m n} →
97 Sym P Q → Sym (Pointwise P) (Pointwise Q {m} {n})
98 sym sm [] = []
99 sym sm (x∼y ∷ xs∼ys) = sm x∼y ∷ sym sm xs∼ys
100
101 trans : ∀ {P : REL A B ℓ} {Q : REL B C ℓ} {R : REL A C ℓ} {m n o} →
102 Trans P Q R →
103 Trans (Pointwise P {m}) (Pointwise Q {n} {o}) (Pointwise R)
104 trans trns [] [] = []
105 trans trns (x∼y ∷ xs∼ys) (y∼z ∷ ys∼zs) =
106 trns x∼y y∼z ∷ trans trns xs∼ys ys∼zs
107
108 decidable : ∀ {_∼_ : REL A B ℓ} →
109 Decidable _∼_ → ∀ {m n} → Decidable (Pointwise _∼_ {m} {n})
110 decidable dec [] [] = yes []
111 decidable dec [] (y ∷ ys) = no λ()
112 decidable dec (x ∷ xs) [] = no λ()
113 decidable dec (x ∷ xs) (y ∷ ys) =
114 map′ (uncurry _∷_) uncons (dec x y ×-dec decidable dec xs ys)
115
116 ------------------------------------------------------------------------
117 -- Structures
118
119 module _ {_∼_ : Rel A ℓ} where
120
121 isEquivalence : IsEquivalence _∼_ → ∀ n →
122 IsEquivalence (Pointwise _∼_ {n})
123 isEquivalence equiv n = record
124 { refl = refl Eq.refl
125 ; sym = sym Eq.sym
126 ; trans = trans Eq.trans
127 } where module Eq = IsEquivalence equiv
128
129 isDecEquivalence : IsDecEquivalence _∼_ → ∀ n →
130 IsDecEquivalence (Pointwise _∼_ {n})
131 isDecEquivalence decEquiv n = record
132 { isEquivalence = isEquivalence Eq.isEquivalence n
133 ; _≟_ = decidable Eq._≟_
134 } where module Eq = IsDecEquivalence decEquiv
135
136 ------------------------------------------------------------------------
137 -- Bundles
138
139 setoid : Setoid a ℓ → ℕ → Setoid a (a ⊔ ℓ)
140 setoid S n = record
141 { isEquivalence = isEquivalence Eq.isEquivalence n
142 } where module Eq = Setoid S
143
144 decSetoid : DecSetoid a ℓ → ℕ → DecSetoid a (a ⊔ ℓ)
145 decSetoid S n = record
146 { isDecEquivalence = isDecEquivalence Eq.isDecEquivalence n
147 } where module Eq = DecSetoid S
148
149 ------------------------------------------------------------------------
150 -- map
151
152 module _ {_∼₁_ : REL A B ℓ₁} {_∼₂_ : REL C D ℓ₂}
153 {f : A → C} {g : B → D}
154 where
155
156 map⁺ : (∀ {x y} → x ∼₁ y → f x ∼₂ g y) →
157 ∀ {m n xs ys} → Pointwise _∼₁_ {m} {n} xs ys →
158 Pointwise _∼₂_ (Vec.map f xs) (Vec.map g ys)
159 map⁺ ∼₁⇒∼₂ [] = []
160 map⁺ ∼₁⇒∼₂ (x∼y ∷ xs∼ys) = ∼₁⇒∼₂ x∼y ∷ map⁺ ∼₁⇒∼₂ xs∼ys
161
162 ------------------------------------------------------------------------
163 -- _++_
164
165 module _ {_∼_ : REL A B ℓ} where
166
167 ++⁺ : ∀ {m n p q}
168 {ws : Vec A m} {xs : Vec B p} {ys : Vec A n} {zs : Vec B q} →
169 Pointwise _∼_ ws xs → Pointwise _∼_ ys zs →
170 Pointwise _∼_ (ws ++ ys) (xs ++ zs)
171 ++⁺ [] ys∼zs = ys∼zs
172 ++⁺ (w∼x ∷ ws∼xs) ys∼zs = w∼x ∷ (++⁺ ws∼xs ys∼zs)
173
174 ++ˡ⁻ : ∀ {m n}
175 (ws : Vec A m) (xs : Vec B m) {ys : Vec A n} {zs : Vec B n} →
176 Pointwise _∼_ (ws ++ ys) (xs ++ zs) → Pointwise _∼_ ws xs
177 ++ˡ⁻ [] [] _ = []
178 ++ˡ⁻ (w ∷ ws) (x ∷ xs) (w∼x ∷ ps) = w∼x ∷ ++ˡ⁻ ws xs ps
179
180 ++ʳ⁻ : ∀ {m n}
181 (ws : Vec A m) (xs : Vec B m) {ys : Vec A n} {zs : Vec B n} →
182 Pointwise _∼_ (ws ++ ys) (xs ++ zs) → Pointwise _∼_ ys zs
183 ++ʳ⁻ [] [] ys∼zs = ys∼zs
184 ++ʳ⁻ (w ∷ ws) (x ∷ xs) (_ ∷ ps) = ++ʳ⁻ ws xs ps
185
186 ++⁻ : ∀ {m n}
187 (ws : Vec A m) (xs : Vec B m) {ys : Vec A n} {zs : Vec B n} →
188 Pointwise _∼_ (ws ++ ys) (xs ++ zs) →
189 Pointwise _∼_ ws xs × Pointwise _∼_ ys zs
190 ++⁻ ws xs ps = ++ˡ⁻ ws xs ps , ++ʳ⁻ ws xs ps
191
192 ------------------------------------------------------------------------
193 -- concat
194
195 module _ {_∼_ : REL A B ℓ} where
196
197 concat⁺ : ∀ {m n p q}
198 {xss : Vec (Vec A m) n} {yss : Vec (Vec B p) q} →
199 Pointwise (Pointwise _∼_) xss yss →
200 Pointwise _∼_ (concat xss) (concat yss)
201 concat⁺ [] = []
202 concat⁺ (xs∼ys ∷ ps) = ++⁺ xs∼ys (concat⁺ ps)
203
204 concat⁻ : ∀ {m n} (xss : Vec (Vec A m) n) (yss : Vec (Vec B m) n) →
205 Pointwise _∼_ (concat xss) (concat yss) →
206 Pointwise (Pointwise _∼_) xss yss
207 concat⁻ [] [] [] = []
208 concat⁻ (xs ∷ xss) (ys ∷ yss) ps =
209 ++ˡ⁻ xs ys ps ∷ concat⁻ xss yss (++ʳ⁻ xs ys ps)
210
211 ------------------------------------------------------------------------
212 -- tabulate
213
214 module _ {_∼_ : REL A B ℓ} where
215
216 tabulate⁺ : ∀ {n} {f : Fin n → A} {g : Fin n → B} →
217 (∀ i → f i ∼ g i) →
218 Pointwise _∼_ (tabulate f) (tabulate g)
219 tabulate⁺ {zero} f∼g = []
220 tabulate⁺ {suc n} f∼g = f∼g zero ∷ tabulate⁺ (f∼g ∘ suc)
221
222 tabulate⁻ : ∀ {n} {f : Fin n → A} {g : Fin n → B} →
223 Pointwise _∼_ (tabulate f) (tabulate g) →
224 (∀ i → f i ∼ g i)
225 tabulate⁻ (f₀∼g₀ ∷ _) zero = f₀∼g₀
226 tabulate⁻ (_ ∷ f∼g) (suc i) = tabulate⁻ f∼g i
227
228 ------------------------------------------------------------------------
229 -- cong
230
231 module _ {_∼_ : Rel A ℓ} (refl : Reflexive _∼_) where
232 cong-[_]≔ : ∀ {n} i p {xs} {ys} →
233 Pointwise _∼_ {n} xs ys →
234 Pointwise _∼_ (xs [ i ]≔ p) (ys [ i ]≔ p)
235 cong-[ zero ]≔ p (_ ∷ eqn) = refl ∷ eqn
236 cong-[ suc i ]≔ p (x∼y ∷ eqn) = x∼y ∷ cong-[ i ]≔ p eqn
237
238 ------------------------------------------------------------------------
239 -- zipWith
240
241 module _ {_∼_ : Rel A ℓ} where
242 module _ {f : A → A → A} where
243 zipWith-assoc : Associative _∼_ f →
244 Associative (Pointwise _∼_) (zipWith {n = n} f)
245 zipWith-assoc assoc [] [] [] = []
246 zipWith-assoc assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) = assoc x y z ∷ zipWith-assoc assoc xs ys zs
247
248 module _ {f : A → A → A} {e : A} where
249 zipWith-identityˡ : LeftIdentity _∼_ e f →
250 LeftIdentity (Pointwise _∼_) (replicate n e) (zipWith f)
251 zipWith-identityˡ idˡ [] = []
252 zipWith-identityˡ idˡ (x ∷ xs) = idˡ x ∷ zipWith-identityˡ idˡ xs
253
254 zipWith-identityʳ : RightIdentity _∼_ e f →
255 RightIdentity (Pointwise _∼_) (replicate n e) (zipWith f)
256 zipWith-identityʳ idʳ [] = []
257 zipWith-identityʳ idʳ (x ∷ xs) = idʳ x ∷ zipWith-identityʳ idʳ xs
258
259 module _ {f : A → A → A} where
260 zipWith-comm : Commutative _∼_ f →
261 Commutative (Pointwise _∼_) (zipWith {n = n} f)
262 zipWith-comm comm [] [] = []
263 zipWith-comm comm (x ∷ xs) (y ∷ ys) = comm x y ∷ zipWith-comm comm xs ys
264
265 module _ {f : A → A → A} where
266 zipWith-cong : ∀ {m n}
267 {ws : Vec A m} {xs : Vec A n} {ys : Vec A m} {zs : Vec A n} →
268 Congruent₂ _∼_ f →
269 Pointwise _∼_ ws xs → Pointwise _∼_ ys zs →
270 Pointwise _∼_ (zipWith f ws ys) (zipWith f xs zs)
271 zipWith-cong cong [] [] = []
272 zipWith-cong cong (x∼y ∷ xs) (a∼b ∷ ys) = cong x∼y a∼b ∷ zipWith-cong cong xs ys
273
274 ------------------------------------------------------------------------
275 -- Degenerate pointwise relations
276
277 module _ {P : Pred A ℓ} where
278
279 Pointwiseˡ⇒All : ∀ {m n} {xs : Vec A m} {ys : Vec B n} →
280 Pointwise (λ x y → P x) xs ys → All P xs
281 Pointwiseˡ⇒All [] = []
282 Pointwiseˡ⇒All (p ∷ ps) = p ∷ Pointwiseˡ⇒All ps
283
284 Pointwiseʳ⇒All : ∀ {n} {xs : Vec B n} {ys : Vec A n} →
285 Pointwise (λ x y → P y) xs ys → All P ys
286 Pointwiseʳ⇒All [] = []
287 Pointwiseʳ⇒All (p ∷ ps) = p ∷ Pointwiseʳ⇒All ps
288
289 All⇒Pointwiseˡ : ∀ {n} {xs : Vec A n} {ys : Vec B n} →
290 All P xs → Pointwise (λ x y → P x) xs ys
291 All⇒Pointwiseˡ {ys = []} [] = []
292 All⇒Pointwiseˡ {ys = _ ∷ _} (p ∷ ps) = p ∷ All⇒Pointwiseˡ ps
293
294 All⇒Pointwiseʳ : ∀ {n} {xs : Vec B n} {ys : Vec A n} →
295 All P ys → Pointwise (λ x y → P y) xs ys
296 All⇒Pointwiseʳ {xs = []} [] = []
297 All⇒Pointwiseʳ {xs = _ ∷ _} (p ∷ ps) = p ∷ All⇒Pointwiseʳ ps
298
299 ------------------------------------------------------------------------
300 -- Pointwise _≡_ is equivalent to _≡_
301
302 Pointwise-≡⇒≡ : ∀ {n} {xs ys : Vec A n} → Pointwise _≡_ xs ys → xs ≡ ys
303 Pointwise-≡⇒≡ [] = ≡.refl
304 Pointwise-≡⇒≡ (≡.refl ∷ xs∼ys) = ≡.cong (_ ∷_) (Pointwise-≡⇒≡ xs∼ys)
305
306 ≡⇒Pointwise-≡ : ∀ {n} {xs ys : Vec A n} → xs ≡ ys → Pointwise _≡_ xs ys
307 ≡⇒Pointwise-≡ ≡.refl = refl ≡.refl
308
309 Pointwise-≡↔≡ : ∀ {n} {xs ys : Vec A n} → Pointwise _≡_ xs ys ⇔ xs ≡ ys
310 Pointwise-≡↔≡ = mk⇔ Pointwise-≡⇒≡ ≡⇒Pointwise-≡