Debian Codegraph @

src/Data/List/Relation/Ternary/Interleaving.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Generalised notion of interleaving two lists into one in an
5 -- order-preserving manner
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --safe #-}
9
10 module Data.List.Relation.Ternary.Interleaving where
11
12 open import Level
13 open import Data.List.Base as List using (List; []; _∷_; _++_)
14 open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise; []; _∷_)
15 open import Data.Product.Base as Product using (∃; ∃₂; _×_; uncurry; _,_; -,_; proj₂)
16 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
17 open import Function.Base
18 open import Relation.Binary.Core using (REL; _⇒_)
19 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
20
21 ------------------------------------------------------------------------
22 -- Definition
23
24 module _ {a b c l r} {A : Set a} {B : Set b} {C : Set c}
25 (L : REL A C l) (R : REL B C r) where
26
27 infixr 5 _∷ˡ_ _∷ʳ_
28
29 data Interleaving : List A → List B → List C → Set (a ⊔ b ⊔ c ⊔ l ⊔ r) where
30 [] : Interleaving [] [] []
31 _∷ˡ_ : ∀ {a c l r cs} → L a c → Interleaving l r cs → Interleaving (a ∷ l) r (c ∷ cs)
32 _∷ʳ_ : ∀ {b c l r cs} → R b c → Interleaving l r cs → Interleaving l (b ∷ r) (c ∷ cs)
33
34 ------------------------------------------------------------------------
35 -- Operations
36
37 module _ {a b c l r} {A : Set a} {B : Set b} {C : Set c}
38 {L : REL A C l} {R : REL B C r} where
39
40 -- injections
41
42 left : ∀ {as cs} → Pointwise L as cs → Interleaving L R as [] cs
43 left [] = []
44 left (l ∷ pw) = l ∷ˡ left pw
45
46 right : ∀ {bs cs} → Pointwise R bs cs → Interleaving L R [] bs cs
47 right [] = []
48 right (r ∷ pw) = r ∷ʳ right pw
49
50 -- swap
51
52 swap : ∀ {cs l r} → Interleaving L R l r cs → Interleaving R L r l cs
53 swap [] = []
54 swap (l ∷ˡ sp) = l ∷ʳ swap sp
55 swap (r ∷ʳ sp) = r ∷ˡ swap sp
56
57 -- extract the "proper" equality split from the pointwise relations
58
59 break : ∀ {cs l r} → Interleaving L R l r cs → ∃ $ uncurry $ λ csl csr →
60 Interleaving _≡_ _≡_ csl csr cs × Pointwise L l csl × Pointwise R r csr
61 break [] = -, [] , [] , []
62 break (l ∷ˡ sp) = let (_ , eq , pwl , pwr) = break sp in
63 -, refl ∷ˡ eq , l ∷ pwl , pwr
64 break (r ∷ʳ sp) = let (_ , eq , pwl , pwr) = break sp in
65 -, refl ∷ʳ eq , pwl , r ∷ pwr
66
67 -- map
68
69 module _ {a b c l r p q} {A : Set a} {B : Set b} {C : Set c}
70 {L : REL A C l} {R : REL B C r} {P : REL A C p} {Q : REL B C q} where
71
72 map : ∀ {cs l r} → L ⇒ P → R ⇒ Q → Interleaving L R l r cs → Interleaving P Q l r cs
73 map L⇒P R⇒Q [] = []
74 map L⇒P R⇒Q (l ∷ˡ sp) = L⇒P l ∷ˡ map L⇒P R⇒Q sp
75 map L⇒P R⇒Q (r ∷ʳ sp) = R⇒Q r ∷ʳ map L⇒P R⇒Q sp
76
77 module _ {a b c l r p} {A : Set a} {B : Set b} {C : Set c}
78 {L : REL A C l} {R : REL B C r} where
79
80 map₁ : ∀ {P : REL A C p} {as l r} → L ⇒ P →
81 Interleaving L R l r as → Interleaving P R l r as
82 map₁ L⇒P = map L⇒P id
83
84 map₂ : ∀ {P : REL B C p} {as l r} → R ⇒ P →
85 Interleaving L R l r as → Interleaving L P l r as
86 map₂ = map id
87
88 ------------------------------------------------------------------------
89 -- Special case: The second and third list have the same type
90
91 module _ {a b l r} {A : Set a} {B : Set b} {L : REL A B l} {R : REL A B r} where
92
93 -- converting back and forth with pointwise
94
95 split : ∀ {as bs} → Pointwise (λ a b → L a b ⊎ R a b) as bs →
96 ∃₂ λ asr asl → Interleaving L R asl asr bs
97 split [] = [] , [] , []
98 split (inj₁ l ∷ pw) = Product.map _ (Product.map _ (l ∷ˡ_)) (split pw)
99 split (inj₂ r ∷ pw) = Product.map _ (Product.map _ (r ∷ʳ_)) (split pw)
100
101 unsplit : ∀ {l r as} → Interleaving L R l r as →
102 ∃ λ bs → Pointwise (λ a b → L a b ⊎ R a b) bs as
103 unsplit [] = -, []
104 unsplit (l ∷ˡ sp) = Product.map _ (inj₁ l ∷_) (unsplit sp)
105 unsplit (r ∷ʳ sp) = Product.map _ (inj₂ r ∷_) (unsplit sp)