| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Vectors where every consecutative pair of elements is related. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Relation.Unary.Linked {a} {A : Set a} where |
| 10 |
|
| 11 |
open import Data.Nat.Base using (ℕ; zero; suc; _+_) |
| 12 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_) |
| 13 |
open import Data.Vec.Relation.Unary.All as All using (All; []; _∷_) |
| 14 |
open import Data.Product.Base as Product using (_,_; _×_; uncurry; <_,_>) |
| 15 |
open import Function.Base using (id; _∘_) |
| 16 |
open import Level using (Level; _⊔_) |
| 17 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 18 |
import Relation.Binary.Definitions as B |
| 19 |
open import Relation.Binary.Construct.Intersection renaming (_∩_ to _∩ᵇ_) |
| 20 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong₂) |
| 21 |
open import Relation.Unary as U renaming (_∩_ to _∩ᵘ_) hiding (_⇒_) |
| 22 |
open import Relation.Nullary.Decidable as Dec using (yes; no; _×-dec_; map′) |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
p q r s ℓ : Level |
| 27 |
P : Rel A p |
| 28 |
Q : Rel A q |
| 29 |
R : Rel A r |
| 30 |
S : Rel A s |
| 31 |
n : ℕ |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Definition |
| 35 |
|
| 36 |
-- Linked R xs means that every consecutative pair of elements in xs is |
| 37 |
-- a member of relation R. |
| 38 |
|
| 39 |
infixr 5 _∷_ |
| 40 |
|
| 41 |
data Linked (R : Rel A ℓ) : Vec A n → Set (a ⊔ ℓ) where |
| 42 |
[] : Linked R [] |
| 43 |
[-] : ∀ {x} → Linked R (x ∷ []) |
| 44 |
_∷_ : ∀ {x} {xs : Vec A (suc n)} → R x (Vec.head xs) → Linked R xs → Linked R (x ∷ xs) |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Operations |
| 48 |
|
| 49 |
head : ∀ {x} {xs : Vec A (suc n)} → Linked R (x ∷ xs) → R x (Vec.head xs) |
| 50 |
head (Rxy ∷ Rxs) = Rxy |
| 51 |
|
| 52 |
tail : ∀ {xs : Vec A (suc n)} → Linked R xs → Linked R (Vec.tail xs) |
| 53 |
tail [-] = [] |
| 54 |
tail (_ ∷ Rxs) = Rxs |
| 55 |
|
| 56 |
map : R ⇒ S → Linked R {n} ⊆ Linked S |
| 57 |
map R⇒S [] = [] |
| 58 |
map R⇒S [-] = [-] |
| 59 |
map R⇒S (x~xs ∷ pxs) = R⇒S x~xs ∷ map R⇒S pxs |
| 60 |
|
| 61 |
zipWith : P ∩ᵇ Q ⇒ R → Linked P {n} ∩ᵘ Linked Q ⊆ Linked R |
| 62 |
zipWith f ([] , []) = [] |
| 63 |
zipWith f ([-] , [-]) = [-] |
| 64 |
zipWith f (px ∷ pxs , qx ∷ qxs) = f (px , qx) ∷ zipWith f (pxs , qxs) |
| 65 |
|
| 66 |
unzipWith : R ⇒ P ∩ᵇ Q → Linked R {n} ⊆ Linked P ∩ᵘ Linked Q |
| 67 |
unzipWith f [] = [] , [] |
| 68 |
unzipWith f [-] = [-] , [-] |
| 69 |
unzipWith f (rx ∷ rxs) = Product.zip _∷_ _∷_ (f rx) (unzipWith f rxs) |
| 70 |
|
| 71 |
zip : Linked P {n} ∩ᵘ Linked Q ⊆ Linked (P ∩ᵇ Q) |
| 72 |
zip = zipWith id |
| 73 |
|
| 74 |
unzip : Linked (P ∩ᵇ Q) {n} ⊆ Linked P ∩ᵘ Linked Q |
| 75 |
unzip = unzipWith id |
| 76 |
|
| 77 |
------------------------------------------------------------------------ |
| 78 |
-- Properties of predicates preserved by Linked |
| 79 |
|
| 80 |
linked? : B.Decidable R → U.Decidable (Linked R {n}) |
| 81 |
linked? R? [] = yes [] |
| 82 |
linked? R? (x ∷ []) = yes [-] |
| 83 |
linked? R? (x ∷ y ∷ xs) = |
| 84 |
map′ (uncurry _∷_) < head , tail > (R? x y ×-dec linked? R? (y ∷ xs)) |
| 85 |
|
| 86 |
irrelevant : B.Irrelevant R → U.Irrelevant (Linked R {n}) |
| 87 |
irrelevant irr [] [] = refl |
| 88 |
irrelevant irr [-] [-] = refl |
| 89 |
irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) = |
| 90 |
cong₂ _∷_ (irr px₁ px₂) (irrelevant irr pxs₁ pxs₂) |
| 91 |
|
| 92 |
satisfiable : U.Satisfiable (Linked R) |
| 93 |
satisfiable = [] , [] |