| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties related to Pointwise |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Functional.Relation.Binary.Pointwise.Properties where |
| 10 |
|
| 11 |
open import Data.Fin.Base using (zero; suc; _↑ˡ_; _↑ʳ_; splitAt) |
| 12 |
open import Data.Fin.Properties using (all?; splitAt-↑ˡ; splitAt-↑ʳ) |
| 13 |
open import Data.Nat.Base using (ℕ; zero; suc) |
| 14 |
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂) |
| 15 |
open import Data.Product.Relation.Binary.Pointwise.NonDependent |
| 16 |
using () renaming (Pointwise to ×-Pointwise) |
| 17 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]) |
| 18 |
open import Data.Vec.Functional as VF hiding (map) |
| 19 |
open import Data.Vec.Functional.Relation.Binary.Pointwise |
| 20 |
open import Function.Base using (const; _∘_) |
| 21 |
open import Level using (Level) |
| 22 |
open import Relation.Binary.Core using (Rel; REL) |
| 23 |
open import Relation.Binary.Bundles using (Setoid; DecSetoid) |
| 24 |
open import Relation.Binary.Structures |
| 25 |
using (IsEquivalence; IsDecEquivalence) |
| 26 |
open import Relation.Binary.Definitions |
| 27 |
using (Reflexive; Transitive; Symmetric; Decidable) |
| 28 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_) |
| 29 |
|
| 30 |
private |
| 31 |
variable |
| 32 |
a a′ a″ b b′ b″ r s t ℓ : Level |
| 33 |
A : Set a |
| 34 |
B : Set b |
| 35 |
A′ : Set a′ |
| 36 |
B′ : Set b′ |
| 37 |
A″ : Set a″ |
| 38 |
B″ : Set b″ |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Relational properties |
| 42 |
|
| 43 |
module _ {R : Rel A ℓ} where |
| 44 |
|
| 45 |
refl : Reflexive R → ∀ {n} → Reflexive (Pointwise R {n}) |
| 46 |
refl r i = r |
| 47 |
|
| 48 |
sym : Symmetric R → ∀ {n} → Symmetric (Pointwise R {n}) |
| 49 |
sym s xsys i = s (xsys i) |
| 50 |
|
| 51 |
trans : Transitive R → ∀ {n} → Transitive (Pointwise R {n}) |
| 52 |
trans t xsys yszs i = t (xsys i) (yszs i) |
| 53 |
|
| 54 |
decidable : Decidable R → ∀ {n} → Decidable (Pointwise R {n}) |
| 55 |
decidable r? xs ys = all? λ i → r? (xs i) (ys i) |
| 56 |
|
| 57 |
------------------------------------------------------------------------ |
| 58 |
-- Structures |
| 59 |
|
| 60 |
isEquivalence : IsEquivalence R → ∀ n → IsEquivalence (Pointwise R {n}) |
| 61 |
isEquivalence isEq n = record |
| 62 |
{ refl = refl Eq.refl |
| 63 |
; sym = sym Eq.sym |
| 64 |
; trans = trans Eq.trans |
| 65 |
} where module Eq = IsEquivalence isEq |
| 66 |
|
| 67 |
isDecEquivalence : IsDecEquivalence R → |
| 68 |
∀ n → IsDecEquivalence (Pointwise R {n}) |
| 69 |
isDecEquivalence isDecEq n = record |
| 70 |
{ isEquivalence = isEquivalence Eq.isEquivalence n |
| 71 |
; _≟_ = decidable Eq._≟_ |
| 72 |
} where module Eq = IsDecEquivalence isDecEq |
| 73 |
|
| 74 |
------------------------------------------------------------------------ |
| 75 |
-- Bundles |
| 76 |
|
| 77 |
setoid : Setoid a ℓ → ℕ → Setoid a ℓ |
| 78 |
setoid S n = record |
| 79 |
{ isEquivalence = isEquivalence S.isEquivalence n |
| 80 |
} where module S = Setoid S |
| 81 |
|
| 82 |
decSetoid : DecSetoid a ℓ → ℕ → DecSetoid a ℓ |
| 83 |
decSetoid S n = record |
| 84 |
{ isDecEquivalence = isDecEquivalence S.isDecEquivalence n |
| 85 |
} where module S = DecSetoid S |
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- map |
| 89 |
|
| 90 |
module _ {R : REL A B r} {S : REL A′ B′ s} {f : A → A′} {g : B → B′} where |
| 91 |
|
| 92 |
map⁺ : (∀ {x y} → R x y → S (f x) (g y)) → |
| 93 |
∀ {n} {xs : Vector A n} {ys : Vector B n} → |
| 94 |
Pointwise R xs ys → Pointwise S (VF.map f xs) (VF.map g ys) |
| 95 |
map⁺ f rs i = f (rs i) |
| 96 |
|
| 97 |
------------------------------------------------------------------------ |
| 98 |
-- head |
| 99 |
|
| 100 |
module _ (R : REL A B r) {n} {xs : Vector A (suc n)} {ys} where |
| 101 |
|
| 102 |
head⁺ : Pointwise R xs ys → R (head xs) (head ys) |
| 103 |
head⁺ rs = rs zero |
| 104 |
|
| 105 |
------------------------------------------------------------------------ |
| 106 |
-- tail |
| 107 |
|
| 108 |
module _ (R : REL A B r) {n} {xs : Vector A (suc n)} {ys} where |
| 109 |
|
| 110 |
tail⁺ : Pointwise R xs ys → Pointwise R (tail xs) (tail ys) |
| 111 |
tail⁺ rs = rs ∘ suc |
| 112 |
|
| 113 |
------------------------------------------------------------------------ |
| 114 |
-- _++_ |
| 115 |
|
| 116 |
module _ (R : REL A B r) where |
| 117 |
|
| 118 |
++⁺ : ∀ {m n xs ys xs′ ys′} → |
| 119 |
Pointwise R {n = m} xs ys → Pointwise R {n = n} xs′ ys′ → |
| 120 |
Pointwise R (xs ++ xs′) (ys ++ ys′) |
| 121 |
++⁺ {m} rs rs′ i with splitAt m i |
| 122 |
... | inj₁ i′ = rs i′ |
| 123 |
... | inj₂ j′ = rs′ j′ |
| 124 |
|
| 125 |
++⁻ˡ : ∀ {m n} (xs : Vector A m) (ys : Vector B m) {xs′ ys′} → |
| 126 |
Pointwise R (xs ++ xs′) (ys ++ ys′) → Pointwise R xs ys |
| 127 |
++⁻ˡ {m} {n} _ _ rs i with rs (i ↑ˡ n) |
| 128 |
... | r rewrite splitAt-↑ˡ m i n = r |
| 129 |
|
| 130 |
++⁻ʳ : ∀ {m n} (xs : Vector A m) (ys : Vector B m) {xs′ ys′} → |
| 131 |
Pointwise R (xs ++ xs′) (ys ++ ys′) → Pointwise R xs′ ys′ |
| 132 |
++⁻ʳ {m} {n} _ _ rs i with rs (m ↑ʳ i) |
| 133 |
... | r rewrite splitAt-↑ʳ m n i = r |
| 134 |
|
| 135 |
++⁻ : ∀ {m n} xs ys {xs′ ys′} → |
| 136 |
Pointwise R (xs ++ xs′) (ys ++ ys′) → |
| 137 |
Pointwise R {n = m} xs ys × Pointwise R {n = n} xs′ ys′ |
| 138 |
++⁻ _ _ rs = ++⁻ˡ _ _ rs , ++⁻ʳ _ _ rs |
| 139 |
|
| 140 |
------------------------------------------------------------------------ |
| 141 |
-- replicate |
| 142 |
|
| 143 |
module _ {R : REL A B r} {x y n} where |
| 144 |
|
| 145 |
replicate⁺ : R x y → Pointwise R {n = n} (replicate n x) (replicate n y) |
| 146 |
replicate⁺ = const |
| 147 |
|
| 148 |
------------------------------------------------------------------------ |
| 149 |
-- _⊛_ |
| 150 |
|
| 151 |
module _ {R : REL A B r} {S : REL A′ B′ s} {n} where |
| 152 |
|
| 153 |
⊛⁺ : ∀ {fs : Vector (A → A′) n} {gs : Vector (B → B′) n} → |
| 154 |
Pointwise (λ f g → ∀ {x y} → R x y → S (f x) (g y)) fs gs → |
| 155 |
∀ {xs ys} → Pointwise R xs ys → Pointwise S (fs ⊛ xs) (gs ⊛ ys) |
| 156 |
⊛⁺ rss rs i = (rss i) (rs i) |
| 157 |
|
| 158 |
------------------------------------------------------------------------ |
| 159 |
-- zipWith |
| 160 |
|
| 161 |
module _ {R : REL A B r} {S : REL A′ B′ s} {T : REL A″ B″ t} where |
| 162 |
|
| 163 |
zipWith⁺ : ∀ {n xs ys xs′ ys′ f f′} → |
| 164 |
(∀ {x y x′ y′} → R x y → S x′ y′ → T (f x x′) (f′ y y′)) → |
| 165 |
Pointwise R xs ys → Pointwise S xs′ ys′ → |
| 166 |
Pointwise T (zipWith f xs xs′) (zipWith f′ {n = n} ys ys′) |
| 167 |
zipWith⁺ t rs ss i = t (rs i) (ss i) |
| 168 |
|
| 169 |
------------------------------------------------------------------------ |
| 170 |
-- zip |
| 171 |
|
| 172 |
module _ {R : REL A B r} {S : REL A′ B′ s} {n xs ys xs′ ys′} where |
| 173 |
|
| 174 |
zip⁺ : Pointwise R xs ys → Pointwise S xs′ ys′ → |
| 175 |
Pointwise (×-Pointwise R S) (zip xs xs′) (zip {n = n} ys ys′) |
| 176 |
zip⁺ rs ss i = rs i , ss i |
| 177 |
|
| 178 |
zip⁻ : Pointwise (×-Pointwise R S) (zip xs xs′) (zip {n = n} ys ys′) → |
| 179 |
Pointwise R xs ys × Pointwise S xs′ ys′ |
| 180 |
zip⁻ rss = proj₁ ∘ rss , proj₂ ∘ rss |
| 181 |
|
| 182 |
------------------------------------------------------------------------ |
| 183 |
-- foldr |
| 184 |
|
| 185 |
module _ {R : REL A B r} {S : REL A′ B′ s} |
| 186 |
{f : A → A′ → A′} {g : B → B′ → B′} |
| 187 |
where |
| 188 |
|
| 189 |
foldr-cong : (∀ {w x y z} → R w x → S y z → S (f w y) (g x z)) → |
| 190 |
∀ {d : A′} {e : B′} → S d e → |
| 191 |
∀ {n} {xs : Vector A n} {ys : Vector B n} → |
| 192 |
Pointwise R xs ys → S (foldr f d xs) (foldr g e ys) |
| 193 |
foldr-cong fg-cong d~e {zero} rss = d~e |
| 194 |
foldr-cong fg-cong d~e {suc n} rss = |
| 195 |
fg-cong (rss zero) (foldr-cong fg-cong d~e (rss ∘ suc)) |