| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An inductive definition of the heterogeneous prefix relation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Prefix.Heterogeneous where |
| 10 |
|
| 11 |
open import Level |
| 12 |
open import Data.List.Base as List using (List; []; _∷_) |
| 13 |
open import Data.List.Relation.Binary.Pointwise |
| 14 |
using (Pointwise; []; _∷_) |
| 15 |
open import Data.Product.Base using (∃; _×_; _,_; uncurry) |
| 16 |
open import Relation.Binary.Core using (REL; _⇒_) |
| 17 |
|
| 18 |
module _ {a b r} {A : Set a} {B : Set b} (R : REL A B r) where |
| 19 |
|
| 20 |
infixr 5 _∷_ _++_ |
| 21 |
|
| 22 |
data Prefix : REL (List A) (List B) (a ⊔ b ⊔ r) where |
| 23 |
[] : ∀ {bs} → Prefix [] bs |
| 24 |
_∷_ : ∀ {a b as bs} → R a b → Prefix as bs → Prefix (a ∷ as) (b ∷ bs) |
| 25 |
|
| 26 |
data PrefixView (as : List A) : List B → Set (a ⊔ b ⊔ r) where |
| 27 |
_++_ : ∀ {cs} → Pointwise R as cs → ∀ ds → PrefixView as (cs List.++ ds) |
| 28 |
|
| 29 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} {a b as bs} where |
| 30 |
|
| 31 |
head : Prefix R (a ∷ as) (b ∷ bs) → R a b |
| 32 |
head (r ∷ rs) = r |
| 33 |
|
| 34 |
tail : Prefix R (a ∷ as) (b ∷ bs) → Prefix R as bs |
| 35 |
tail (r ∷ rs) = rs |
| 36 |
|
| 37 |
uncons : Prefix R (a ∷ as) (b ∷ bs) → R a b × Prefix R as bs |
| 38 |
uncons (r ∷ rs) = r , rs |
| 39 |
|
| 40 |
module _ {a b r s} {A : Set a} {B : Set b} {R : REL A B r} {S : REL A B s} where |
| 41 |
|
| 42 |
map : R ⇒ S → Prefix R ⇒ Prefix S |
| 43 |
map R⇒S [] = [] |
| 44 |
map R⇒S (r ∷ rs) = R⇒S r ∷ map R⇒S rs |
| 45 |
|
| 46 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 47 |
|
| 48 |
infixr 5 _++ᵖ_ |
| 49 |
|
| 50 |
_++ᵖ_ : ∀ {as bs} → Prefix R as bs → ∀ suf → Prefix R as (bs List.++ suf) |
| 51 |
[] ++ᵖ suf = [] |
| 52 |
(r ∷ rs) ++ᵖ suf = r ∷ (rs ++ᵖ suf) |
| 53 |
|
| 54 |
toView : ∀ {as bs} → Prefix R as bs → PrefixView R as bs |
| 55 |
toView [] = [] ++ _ |
| 56 |
toView (r ∷ rs) with rs′ ++ ds ← toView rs = (r ∷ rs′) ++ ds |
| 57 |
|
| 58 |
fromView : ∀ {as bs} → PrefixView R as bs → Prefix R as bs |
| 59 |
fromView ([] ++ ds) = [] |
| 60 |
fromView ((r ∷ rs) ++ ds) = r ∷ fromView (rs ++ ds) |