Debian Codegraph @

src/Data/List/Relation/Binary/Pointwise.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Pointwise lifting of relations to lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Binary.Pointwise where
10
11 open import Algebra.Core using (Op₂)
12 open import Function.Base
13 open import Function.Bundles using (Inverse)
14 open import Data.Bool.Base using (true; false)
15 open import Data.Product.Base hiding (map)
16 open import Data.List.Base as List hiding (map; head; tail; uncons)
17 open import Data.List.Properties using (≡-dec; length-++)
18 open import Data.List.Relation.Unary.All as All using (All; []; _∷_)
19 open import Data.List.Relation.Unary.AllPairs using (AllPairs; []; _∷_)
20 open import Data.List.Relation.Unary.Any using (Any; here; there)
21 open import Data.Fin.Base
22 using (Fin; toℕ; cast)
23 renaming (zero to fzero; suc to fsuc)
24 open import Data.Nat.Base using (ℕ; zero; suc)
25 open import Data.Nat.Properties
26 open import Level using (Level; _⊔_)
27 open import Relation.Binary.Core renaming (Rel to Rel₂)
28 open import Relation.Binary.Definitions
29 using (Reflexive; _Respects_; _Respects₂_)
30 open import Relation.Binary.Bundles using (Setoid; DecSetoid; Preorder; Poset)
31 open import Relation.Binary.Structures
32 using (IsEquivalence; IsDecEquivalence; IsPartialOrder; IsPreorder)
33 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
34 import Relation.Binary.PropositionalEquality.Properties as ≡
35 open import Relation.Nullary.Decidable as Dec
36 using (map′; yes; no; Dec; _because_)
37 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
38 open import Relation.Unary as U using (Pred)
39
40 private
41 variable
42 a b c d p q ℓ ℓ₁ ℓ₂ : Level
43 A B C D : Set d
44 x y z : A
45 ws xs ys zs : List A
46 R S T : REL A B ℓ
47
48 ------------------------------------------------------------------------
49 -- Re-exporting the definition and basic operations
50 ------------------------------------------------------------------------
51
52 open import Data.List.Relation.Binary.Pointwise.Base public
53 open import Data.List.Relation.Binary.Pointwise.Properties public
54
55 ------------------------------------------------------------------------
56 -- Structures
57
58 isEquivalence : IsEquivalence R → IsEquivalence (Pointwise R)
59 isEquivalence eq = record
60 { refl = refl Eq.refl
61 ; sym = symmetric Eq.sym
62 ; trans = transitive Eq.trans
63 } where module Eq = IsEquivalence eq
64
65 isDecEquivalence : IsDecEquivalence R → IsDecEquivalence (Pointwise R)
66 isDecEquivalence eq = record
67 { isEquivalence = isEquivalence DE.isEquivalence
68 ; _≟_ = decidable DE._≟_
69 } where module DE = IsDecEquivalence eq
70
71 isPreorder : IsPreorder R S → IsPreorder (Pointwise R) (Pointwise S)
72 isPreorder pre = record
73 { isEquivalence = isEquivalence Pre.isEquivalence
74 ; reflexive = reflexive Pre.reflexive
75 ; trans = transitive Pre.trans
76 } where module Pre = IsPreorder pre
77
78 isPartialOrder : IsPartialOrder R S →
79 IsPartialOrder (Pointwise R) (Pointwise S)
80 isPartialOrder po = record
81 { isPreorder = isPreorder PO.isPreorder
82 ; antisym = antisymmetric PO.antisym
83 } where module PO = IsPartialOrder po
84
85 ------------------------------------------------------------------------
86 -- Bundles
87
88 setoid : Setoid a ℓ → Setoid a (a ⊔ ℓ)
89 setoid s = record
90 { isEquivalence = isEquivalence (Setoid.isEquivalence s)
91 }
92
93 decSetoid : DecSetoid a ℓ → DecSetoid a (a ⊔ ℓ)
94 decSetoid d = record
95 { isDecEquivalence = isDecEquivalence (DecSetoid.isDecEquivalence d)
96 }
97
98 preorder : Preorder a ℓ₁ ℓ₂ → Preorder _ _ _
99 preorder p = record
100 { isPreorder = isPreorder (Preorder.isPreorder p)
101 }
102
103 poset : Poset a ℓ₁ ℓ₂ → Poset _ _ _
104 poset p = record
105 { isPartialOrder = isPartialOrder (Poset.isPartialOrder p)
106 }
107
108 ------------------------------------------------------------------------
109 -- Relationships to other list predicates
110 ------------------------------------------------------------------------
111
112 All-resp-Pointwise : ∀ {P : Pred A p} → P Respects R →
113 (All P) Respects (Pointwise R)
114 All-resp-Pointwise resp [] [] = []
115 All-resp-Pointwise resp (x∼y ∷ xs) (px ∷ pxs) =
116 resp x∼y px ∷ All-resp-Pointwise resp xs pxs
117
118 Any-resp-Pointwise : ∀ {P : Pred A p} → P Respects R →
119 (Any P) Respects (Pointwise R)
120 Any-resp-Pointwise resp (x∼y ∷ xs) (here px) = here (resp x∼y px)
121 Any-resp-Pointwise resp (x∼y ∷ xs) (there pxs) =
122 there (Any-resp-Pointwise resp xs pxs)
123
124 AllPairs-resp-Pointwise : R Respects₂ S →
125 (AllPairs R) Respects (Pointwise S)
126 AllPairs-resp-Pointwise _ [] [] = []
127 AllPairs-resp-Pointwise resp@(respₗ , respᵣ) (x∼y ∷ xs) (px ∷ pxs) =
128 All-resp-Pointwise respₗ xs (All.map (respᵣ x∼y) px) ∷
129 (AllPairs-resp-Pointwise resp xs pxs)
130
131 ------------------------------------------------------------------------
132 -- Relationship to functions over lists
133 ------------------------------------------------------------------------
134 -- tabulate
135
136 tabulate⁺ : ∀ {n} {f : Fin n → A} {g : Fin n → B} →
137 (∀ i → R (f i) (g i)) → Pointwise R (tabulate f) (tabulate g)
138 tabulate⁺ {n = zero} f∼g = []
139 tabulate⁺ {n = suc n} f∼g = f∼g fzero ∷ tabulate⁺ (f∼g ∘ fsuc)
140
141 tabulate⁻ : ∀ {n} {f : Fin n → A} {g : Fin n → B} →
142 Pointwise R (tabulate f) (tabulate g) → (∀ i → R (f i) (g i))
143 tabulate⁻ {n = suc n} (x∼y ∷ xs∼ys) fzero = x∼y
144 tabulate⁻ {n = suc n} (x∼y ∷ xs∼ys) (fsuc i) = tabulate⁻ xs∼ys i
145
146 ------------------------------------------------------------------------
147 -- _++_
148
149 ++⁺ : Pointwise R ws xs → Pointwise R ys zs →
150 Pointwise R (ws ++ ys) (xs ++ zs)
151 ++⁺ [] ys∼zs = ys∼zs
152 ++⁺ (w∼x ∷ ws∼xs) ys∼zs = w∼x ∷ ++⁺ ws∼xs ys∼zs
153
154 ++-cancelˡ : ∀ xs → Pointwise R (xs ++ ys) (xs ++ zs) → Pointwise R ys zs
155 ++-cancelˡ [] ys∼zs = ys∼zs
156 ++-cancelˡ (x ∷ xs) (_ ∷ xs++ys∼xs++zs) = ++-cancelˡ xs xs++ys∼xs++zs
157
158 ++-cancelʳ : ∀ ys zs → Pointwise R (ys ++ xs) (zs ++ xs) → Pointwise R ys zs
159 ++-cancelʳ [] [] _ = []
160 ++-cancelʳ (y ∷ ys) (z ∷ zs) (y∼z ∷ ys∼zs) = y∼z ∷ (++-cancelʳ ys zs ys∼zs)
161 -- Impossible cases
162 ++-cancelʳ {xs = xs} [] (z ∷ zs) eq =
163 contradiction (≡.trans (Pointwise-length eq) (length-++ (z ∷ zs))) (m≢1+n+m (length xs))
164 ++-cancelʳ {xs = xs} (y ∷ ys) [] eq =
165 contradiction (≡.trans (≡.sym (length-++ (y ∷ ys))) (Pointwise-length eq)) (m≢1+n+m (length xs) ∘ ≡.sym)
166
167 module _ (rfl : Reflexive R) where
168
169 ++⁺ˡ : ∀ xs → (xs ++_) Preserves (Pointwise R) ⟶ (Pointwise R)
170 ++⁺ˡ xs = ++⁺ (refl rfl)
171
172 ++⁺ʳ : ∀ zs → (_++ zs) Preserves (Pointwise R) ⟶ (Pointwise R)
173 ++⁺ʳ zs rs = ++⁺ rs (refl rfl)
174
175
176 ------------------------------------------------------------------------
177 -- concat
178
179 concat⁺ : ∀ {xss yss} → Pointwise (Pointwise R) xss yss →
180 Pointwise R (concat xss) (concat yss)
181 concat⁺ [] = []
182 concat⁺ (xs∼ys ∷ xss∼yss) = ++⁺ xs∼ys (concat⁺ xss∼yss)
183
184 ------------------------------------------------------------------------
185 -- reverse
186
187 reverseAcc⁺ : Pointwise R ws xs → Pointwise R ys zs →
188 Pointwise R (reverseAcc ws ys) (reverseAcc xs zs)
189 reverseAcc⁺ rs′ [] = rs′
190 reverseAcc⁺ rs′ (r ∷ rs) = reverseAcc⁺ (r ∷ rs′) rs
191
192 ʳ++⁺ : Pointwise R ws xs → Pointwise R ys zs →
193 Pointwise R (ws ʳ++ ys) (xs ʳ++ zs)
194 ʳ++⁺ rs rs′ = reverseAcc⁺ rs′ rs
195
196 reverse⁺ : Pointwise R xs ys → Pointwise R (reverse xs) (reverse ys)
197 reverse⁺ = reverseAcc⁺ []
198
199 ------------------------------------------------------------------------
200 -- map
201
202 map⁺ : ∀ (f : A → C) (g : B → D) →
203 Pointwise (λ a b → R (f a) (g b)) xs ys →
204 Pointwise R (List.map f xs) (List.map g ys)
205 map⁺ f g [] = []
206 map⁺ f g (r ∷ rs) = r ∷ map⁺ f g rs
207
208 map⁻ : ∀ (f : A → C) (g : B → D) →
209 Pointwise R (List.map f xs) (List.map g ys) →
210 Pointwise (λ a b → R (f a) (g b)) xs ys
211 map⁻ {xs = []} {[]} f g [] = []
212 map⁻ {xs = _ ∷ _} {_ ∷ _} f g (r ∷ rs) = r ∷ map⁻ f g rs
213
214 ------------------------------------------------------------------------
215 -- foldr
216
217 foldr⁺ : ∀ {_•_ : Op₂ A} {_◦_ : Op₂ B} →
218 (∀ {w x y z} → R w x → R y z → R (w • y) (x ◦ z)) →
219 ∀ {e f} → R e f → Pointwise R xs ys →
220 R (foldr _•_ e xs) (foldr _◦_ f ys)
221 foldr⁺ _ e~f [] = e~f
222 foldr⁺ pres e~f (x~y ∷ xs~ys) = pres x~y (foldr⁺ pres e~f xs~ys)
223
224 ------------------------------------------------------------------------
225 -- filter
226
227 module _ {P : Pred A p} {Q : Pred B q}
228 (P? : U.Decidable P) (Q? : U.Decidable Q)
229 (P⇒Q : ∀ {a b} → R a b → P a → Q b)
230 (Q⇒P : ∀ {a b} → R a b → Q b → P a)
231 where
232
233 filter⁺ : Pointwise R xs ys →
234 Pointwise R (filter P? xs) (filter Q? ys)
235 filter⁺ [] = []
236 filter⁺ {x ∷ _} {y ∷ _} (r ∷ rs) with P? x | Q? y
237 ... | true because _ | true because _ = r ∷ filter⁺ rs
238 ... | false because _ | false because _ = filter⁺ rs
239 ... | yes p | no ¬q = contradiction (P⇒Q r p) ¬q
240 ... | no ¬p | yes q = contradiction (Q⇒P r q) ¬p
241
242 ------------------------------------------------------------------------
243 -- replicate
244
245 replicate⁺ : R x y → ∀ n → Pointwise R (replicate n x) (replicate n y)
246 replicate⁺ r 0 = []
247 replicate⁺ r (suc n) = r ∷ replicate⁺ r n
248
249 ------------------------------------------------------------------------
250 -- lookup
251
252 lookup⁻ : length xs ≡ length ys →
253 (∀ {i j} → toℕ i ≡ toℕ j → R (lookup xs i) (lookup ys j)) →
254 Pointwise R xs ys
255 lookup⁻ {xs = []} {ys = []} _ _ = []
256 lookup⁻ {xs = _ ∷ _} {ys = _ ∷ _} |xs|≡|ys| eq = eq {fzero} ≡.refl ∷
257 lookup⁻ (suc-injective |xs|≡|ys|) (eq ∘ ≡.cong ℕ.suc)
258
259 lookup⁺ : ∀ (Rxys : Pointwise R xs ys) →
260 ∀ i → (let j = cast (Pointwise-length Rxys) i) →
261 R (lookup xs i) (lookup ys j)
262 lookup⁺ (Rxy ∷ _) fzero = Rxy
263 lookup⁺ (_ ∷ Rxys) (fsuc i) = lookup⁺ Rxys i
264
265 ------------------------------------------------------------------------
266 -- Properties of propositional pointwise
267 ------------------------------------------------------------------------
268
269 Pointwise-≡⇒≡ : Pointwise {A = A} _≡_ ⇒ _≡_
270 Pointwise-≡⇒≡ [] = ≡.refl
271 Pointwise-≡⇒≡ (≡.refl ∷ xs∼ys) = ≡.cong (_ ∷_) (Pointwise-≡⇒≡ xs∼ys)
272
273 ≡⇒Pointwise-≡ : _≡_ ⇒ Pointwise {A = A} _≡_
274 ≡⇒Pointwise-≡ ≡.refl = refl ≡.refl
275
276 Pointwise-≡↔≡ : Inverse (setoid (≡.setoid A)) (≡.setoid (List A))
277 Pointwise-≡↔≡ = record
278 { to = id
279 ; from = id
280 ; to-cong = Pointwise-≡⇒≡
281 ; from-cong = ≡⇒Pointwise-≡
282 ; inverse = Pointwise-≡⇒≡ , ≡⇒Pointwise-≡
283 }