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