| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An inductive definition of the heterogeneous suffix relation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Suffix.Heterogeneous where |
| 10 |
|
| 11 |
open import Level |
| 12 |
open import Relation.Binary.Core using (REL; _⇒_) |
| 13 |
open import Data.List.Base as List using (List; []; _∷_) |
| 14 |
open import Data.List.Relation.Binary.Pointwise.Base as Pointwise |
| 15 |
using (Pointwise; []; _∷_) |
| 16 |
|
| 17 |
module _ {a b r} {A : Set a} {B : Set b} (R : REL A B r) where |
| 18 |
|
| 19 |
infixr 5 _++_ |
| 20 |
|
| 21 |
data Suffix : REL (List A) (List B) (a ⊔ b ⊔ r) where |
| 22 |
here : ∀ {as bs} → Pointwise R as bs → Suffix as bs |
| 23 |
there : ∀ {b as bs} → Suffix as bs → Suffix as (b ∷ bs) |
| 24 |
|
| 25 |
data SuffixView (as : List A) : List B → Set (a ⊔ b ⊔ r) where |
| 26 |
_++_ : ∀ cs {ds} → Pointwise R as ds → SuffixView as (cs List.++ ds) |
| 27 |
|
| 28 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 29 |
|
| 30 |
tail : ∀ {a as bs} → Suffix R (a ∷ as) bs → Suffix R as bs |
| 31 |
tail (here (_ ∷ rs)) = there (here rs) |
| 32 |
tail (there x) = there (tail x) |
| 33 |
|
| 34 |
infixr 5 _++ˢ_ |
| 35 |
|
| 36 |
_++ˢ_ : ∀ pre {as bs} → Suffix R as bs → Suffix R as (pre List.++ bs) |
| 37 |
[] ++ˢ rs = rs |
| 38 |
(x ∷ xs) ++ˢ rs = there (xs ++ˢ 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 → Suffix R ⇒ Suffix S |
| 43 |
map R⇒S (here rs) = here (Pointwise.map R⇒S rs) |
| 44 |
map R⇒S (there suf) = there (map R⇒S suf) |
| 45 |
|
| 46 |
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where |
| 47 |
|
| 48 |
toView : ∀ {as bs} → Suffix R as bs → SuffixView R as bs |
| 49 |
toView (here rs) = [] ++ rs |
| 50 |
toView (there {c} suf) with cs ++ rs ← toView suf = (c ∷ cs) ++ rs |
| 51 |
|
| 52 |
fromView : ∀ {as bs} → SuffixView R as bs → Suffix R as bs |
| 53 |
fromView ([] ++ rs) = here rs |
| 54 |
fromView ((c ∷ cs) ++ rs) = there (fromView (cs ++ rs)) |