| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Extensional pointwise lifting of relations to vectors |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Relation.Binary.Pointwise.Extensional where |
| 10 |
|
| 11 |
open import Data.Fin.Base using (zero; suc) |
| 12 |
open import Data.Vec.Base as Vec hiding ([_]; head; tail; map) |
| 13 |
open import Data.Vec.Relation.Binary.Pointwise.Inductive as Inductive |
| 14 |
using ([]; _∷_) |
| 15 |
renaming (Pointwise to IPointwise) |
| 16 |
open import Level using (_⊔_) |
| 17 |
open import Function.Base using (_∘_) |
| 18 |
open import Function.Bundles using (module Equivalence; _⇔_; mk⇔) |
| 19 |
open import Function.Properties.Equivalence using (⇔-setoid) |
| 20 |
open import Level using (Level; _⊔_; 0ℓ) |
| 21 |
open import Relation.Binary.Core using (Rel; REL; _⇒_; _=[_]⇒_) |
| 22 |
open import Relation.Binary.Bundles using (Setoid) |
| 23 |
open import Relation.Binary.Structures using (IsEquivalence; IsDecEquivalence) |
| 24 |
open import Relation.Binary.Definitions using (Reflexive; Sym; Trans; Decidable) |
| 25 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 26 |
open import Relation.Binary.Construct.Closure.Transitive as Plus |
| 27 |
hiding (equivalent; map) |
| 28 |
open import Relation.Nullary.Negation.Core using (¬_) |
| 29 |
import Relation.Nullary.Decidable as Dec |
| 30 |
|
| 31 |
private |
| 32 |
variable |
| 33 |
a b c ℓ : Level |
| 34 |
A : Set a |
| 35 |
B : Set b |
| 36 |
C : Set c |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- Definition |
| 40 |
|
| 41 |
record Pointwise {a b ℓ} {A : Set a} {B : Set b} (_∼_ : REL A B ℓ) |
| 42 |
{n} (xs : Vec A n) (ys : Vec B n) : Set (a ⊔ b ⊔ ℓ) |
| 43 |
where |
| 44 |
constructor ext |
| 45 |
field app : ∀ i → lookup xs i ∼ lookup ys i |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Operations |
| 49 |
|
| 50 |
head : ∀ {_∼_ : REL A B ℓ} {n x y xs} {ys : Vec B n} → |
| 51 |
Pointwise _∼_ (x ∷ xs) (y ∷ ys) → x ∼ y |
| 52 |
head (ext app) = app zero |
| 53 |
|
| 54 |
tail : ∀ {_∼_ : REL A B ℓ} {n x y xs} {ys : Vec B n} → |
| 55 |
Pointwise _∼_ (x ∷ xs) (y ∷ ys) → Pointwise _∼_ xs ys |
| 56 |
tail (ext app) = ext (app ∘ suc) |
| 57 |
|
| 58 |
map : ∀ {_∼_ _∼′_ : REL A B ℓ} {n} → |
| 59 |
_∼_ ⇒ _∼′_ → Pointwise _∼_ ⇒ Pointwise _∼′_ {n} |
| 60 |
map ∼⇒∼′ xs∼ys = ext (∼⇒∼′ ∘ Pointwise.app xs∼ys) |
| 61 |
|
| 62 |
gmap : ∀ {_∼_ : Rel A ℓ} {_∼′_ : Rel B ℓ} {f : A → B} {n} → |
| 63 |
_∼_ =[ f ]⇒ _∼′_ → |
| 64 |
Pointwise _∼_ =[ Vec.map {n = n} f ]⇒ Pointwise _∼′_ |
| 65 |
gmap {_} ∼⇒∼′ {[]} {[]} xs∼ys = ext λ() |
| 66 |
gmap {_∼′_ = _∼′_} ∼⇒∼′ {x ∷ xs} {y ∷ ys} xs∼ys = ext λ |
| 67 |
{ zero → ∼⇒∼′ (head xs∼ys) |
| 68 |
; (suc i) → Pointwise.app (gmap {_∼′_ = _∼′_} ∼⇒∼′ (tail xs∼ys)) i |
| 69 |
} |
| 70 |
|
| 71 |
------------------------------------------------------------------------ |
| 72 |
-- The inductive and extensional definitions are equivalent. |
| 73 |
|
| 74 |
module _ {_∼_ : REL A B ℓ} where |
| 75 |
|
| 76 |
extensional⇒inductive : ∀ {n} {xs : Vec A n} {ys : Vec B n} → |
| 77 |
Pointwise _∼_ xs ys → IPointwise _∼_ xs ys |
| 78 |
extensional⇒inductive {xs = []} {[]} xs∼ys = [] |
| 79 |
extensional⇒inductive {xs = x ∷ xs} {y ∷ ys} xs∼ys = |
| 80 |
(head xs∼ys) ∷ extensional⇒inductive (tail xs∼ys) |
| 81 |
|
| 82 |
inductive⇒extensional : ∀ {n} {xs : Vec A n} {ys : Vec B n} → |
| 83 |
IPointwise _∼_ xs ys → Pointwise _∼_ xs ys |
| 84 |
inductive⇒extensional [] = ext λ() |
| 85 |
inductive⇒extensional (x∼y ∷ xs∼ys) = ext λ |
| 86 |
{ zero → x∼y |
| 87 |
; (suc i) → Pointwise.app (inductive⇒extensional xs∼ys) i |
| 88 |
} |
| 89 |
|
| 90 |
equivalent : ∀ {n} {xs : Vec A n} {ys : Vec B n} → |
| 91 |
Pointwise _∼_ xs ys ⇔ IPointwise _∼_ xs ys |
| 92 |
equivalent = mk⇔ extensional⇒inductive inductive⇒extensional |
| 93 |
|
| 94 |
------------------------------------------------------------------------ |
| 95 |
-- Relational properties |
| 96 |
|
| 97 |
refl : ∀ {_∼_ : Rel A ℓ} {n} → |
| 98 |
Reflexive _∼_ → Reflexive (Pointwise _∼_ {n = n}) |
| 99 |
refl ∼-rfl = ext (λ _ → ∼-rfl) |
| 100 |
|
| 101 |
sym : ∀ {P : REL A B ℓ} {Q : REL B A ℓ} {n} → |
| 102 |
Sym P Q → Sym (Pointwise P) (Pointwise Q {n = n}) |
| 103 |
sym sm xs∼ys = ext λ i → sm (Pointwise.app xs∼ys i) |
| 104 |
|
| 105 |
trans : ∀ {P : REL A B ℓ} {Q : REL B C ℓ} {R : REL A C ℓ} {n} → |
| 106 |
Trans P Q R → |
| 107 |
Trans (Pointwise P) (Pointwise Q) (Pointwise R {n = n}) |
| 108 |
trans trns xs∼ys ys∼zs = ext λ i → |
| 109 |
trns (Pointwise.app xs∼ys i) (Pointwise.app ys∼zs i) |
| 110 |
|
| 111 |
decidable : ∀ {_∼_ : REL A B ℓ} → |
| 112 |
Decidable _∼_ → ∀ {n} → Decidable (Pointwise _∼_ {n = n}) |
| 113 |
decidable dec xs ys = Dec.map |
| 114 |
(Setoid.sym (⇔-setoid _) equivalent) |
| 115 |
(Inductive.decidable dec xs ys) |
| 116 |
|
| 117 |
isEquivalence : ∀ {_∼_ : Rel A ℓ} {n} → |
| 118 |
IsEquivalence _∼_ → IsEquivalence (Pointwise _∼_ {n = n}) |
| 119 |
isEquivalence equiv = record |
| 120 |
{ refl = refl Eq.refl |
| 121 |
; sym = sym Eq.sym |
| 122 |
; trans = trans Eq.trans |
| 123 |
} where module Eq = IsEquivalence equiv |
| 124 |
|
| 125 |
isDecEquivalence : ∀ {_∼_ : Rel A ℓ} {n} → |
| 126 |
IsDecEquivalence _∼_ → |
| 127 |
IsDecEquivalence (Pointwise _∼_ {n = n}) |
| 128 |
isDecEquivalence decEquiv = record |
| 129 |
{ isEquivalence = isEquivalence DecEq.isEquivalence |
| 130 |
; _≟_ = decidable DecEq._≟_ |
| 131 |
} where module DecEq = IsDecEquivalence decEquiv |
| 132 |
|
| 133 |
------------------------------------------------------------------------ |
| 134 |
-- Pointwise _≡_ is equivalent to _≡_. |
| 135 |
|
| 136 |
Pointwise-≡⇒≡ : ∀ {n} {xs ys : Vec A n} → Pointwise _≡_ xs ys → xs ≡ ys |
| 137 |
Pointwise-≡⇒≡ {xs = []} {[]} (ext app) = ≡.refl |
| 138 |
Pointwise-≡⇒≡ {xs = x ∷ xs} {y ∷ ys} xs∼ys = |
| 139 |
≡.cong₂ _∷_ (head xs∼ys) (Pointwise-≡⇒≡ (tail xs∼ys)) |
| 140 |
|
| 141 |
≡⇒Pointwise-≡ : ∀ {n} {xs ys : Vec A n} → xs ≡ ys → Pointwise _≡_ xs ys |
| 142 |
≡⇒Pointwise-≡ ≡.refl = refl ≡.refl |
| 143 |
|
| 144 |
Pointwise-≡↔≡ : ∀ {n} {xs ys : Vec A n} → Pointwise _≡_ xs ys ⇔ xs ≡ ys |
| 145 |
Pointwise-≡↔≡ {ℓ} {A} = mk⇔ Pointwise-≡⇒≡ ≡⇒Pointwise-≡ |
| 146 |
|
| 147 |
------------------------------------------------------------------------ |
| 148 |
-- Pointwise and Plus commute when the underlying relation is |
| 149 |
-- reflexive. |
| 150 |
|
| 151 |
module _ {_∼_ : Rel A ℓ} where |
| 152 |
|
| 153 |
⁺∙⇒∙⁺ : ∀ {n} {xs ys : Vec A n} → |
| 154 |
Plus (Pointwise _∼_) xs ys → Pointwise (Plus _∼_) xs ys |
| 155 |
⁺∙⇒∙⁺ [ ρ≈ρ′ ] = ext (λ x → [ Pointwise.app ρ≈ρ′ x ]) |
| 156 |
⁺∙⇒∙⁺ (ρ ∼⁺⟨ ρ≈ρ′ ⟩ ρ′≈ρ″) = ext (λ x → |
| 157 |
_ ∼⁺⟨ Pointwise.app (⁺∙⇒∙⁺ ρ≈ρ′ ) x ⟩ |
| 158 |
Pointwise.app (⁺∙⇒∙⁺ ρ′≈ρ″) x) |
| 159 |
|
| 160 |
∙⁺⇒⁺∙ : ∀ {n} {xs ys : Vec A n} → Reflexive _∼_ → |
| 161 |
Pointwise (Plus _∼_) xs ys → Plus (Pointwise _∼_) xs ys |
| 162 |
∙⁺⇒⁺∙ rfl = |
| 163 |
Plus.map (Equivalence.from equivalent) ∘ |
| 164 |
helper ∘ |
| 165 |
Equivalence.to equivalent |
| 166 |
where |
| 167 |
helper : ∀ {n} {xs ys : Vec A n} → |
| 168 |
IPointwise (Plus _∼_) xs ys → Plus (IPointwise _∼_) xs ys |
| 169 |
helper [] = [ [] ] |
| 170 |
helper (_∷_ {x = x} {y = y} {xs = xs} {ys = ys} x∼y xs∼ys) = |
| 171 |
x ∷ xs ∼⁺⟨ Plus.map (_∷ Inductive.refl rfl) x∼y ⟩ |
| 172 |
y ∷ xs ∼⁺⟨ Plus.map (rfl ∷_) (helper xs∼ys) ⟩∎ |
| 173 |
y ∷ ys ∎ |
| 174 |
|
| 175 |
-- ∙⁺⇒⁺∙ cannot be defined if the requirement of reflexivity |
| 176 |
-- is dropped. |
| 177 |
private |
| 178 |
|
| 179 |
module Counterexample where |
| 180 |
|
| 181 |
data D : Set where |
| 182 |
i j x y z : D |
| 183 |
|
| 184 |
data _R_ : Rel D 0ℓ where |
| 185 |
iRj : i R j |
| 186 |
xRy : x R y |
| 187 |
yRz : y R z |
| 188 |
|
| 189 |
xR⁺z : x [ _R_ ]⁺ z |
| 190 |
xR⁺z = |
| 191 |
x ∼⁺⟨ [ xRy ] ⟩ |
| 192 |
y ∼⁺⟨ [ yRz ] ⟩∎ |
| 193 |
z ∎ |
| 194 |
|
| 195 |
ix : Vec D 2 |
| 196 |
ix = i ∷ x ∷ [] |
| 197 |
|
| 198 |
jz : Vec D 2 |
| 199 |
jz = j ∷ z ∷ [] |
| 200 |
|
| 201 |
ix∙⁺jz : IPointwise (Plus _R_) ix jz |
| 202 |
ix∙⁺jz = [ iRj ] ∷ xR⁺z ∷ [] |
| 203 |
|
| 204 |
¬ix⁺∙jz : ¬ TransClosure (IPointwise _R_) ix jz |
| 205 |
¬ix⁺∙jz [ iRj ∷ () ∷ [] ] |
| 206 |
¬ix⁺∙jz ((iRj ∷ xRy ∷ []) ∷ [ () ∷ yRz ∷ [] ]) |
| 207 |
¬ix⁺∙jz ((iRj ∷ xRy ∷ []) ∷ (() ∷ yRz ∷ []) ∷ _) |
| 208 |
|
| 209 |
counterexample : |
| 210 |
¬ (∀ {n} {xs ys : Vec D n} → |
| 211 |
Pointwise (Plus _R_) xs ys → |
| 212 |
Plus (Pointwise _R_) xs ys) |
| 213 |
counterexample ∙⁺⇒⁺∙ = |
| 214 |
¬ix⁺∙jz (Equivalence.to Plus.equivalent |
| 215 |
(Plus.map (Equivalence.to equivalent) |
| 216 |
(∙⁺⇒⁺∙ (Equivalence.from equivalent ix∙⁺jz)))) |