Debian Codegraph @

src/Codata/Musical/Colist/Infinite-merge.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Infinite merge operation for coinductive lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --guardedness #-}
8
9 module Codata.Musical.Colist.Infinite-merge where
10
11 open import Codata.Musical.Notation using (♭; ♯_; ∞)
12 open import Codata.Musical.Colist as Colist hiding (_⋎_)
13 open import Data.Nat.Base using (ℕ; zero; suc; _≤_; _≥′_; _<′_; _≤′_)
14 open import Data.Nat.Induction using (<′-wellFounded)
15 open import Data.Nat.Properties using (s≤′s)
16 open import Data.Product.Base as Product using (_×_; _,_; ∃; ∃₂; proj₁; proj₂)
17 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′)
18 open import Data.Sum.Properties using (inj₁-injective; inj₂-injective)
19 open import Data.Sum.Function.Propositional using (_⊎-cong_)
20 open import Function.Base using (_∘_; _∋_; id; _∘′_; _$_; _on_)
21 open import Function.Bundles using (Inverse; Injection; _↔_; mk↔ₛ′)
22 open import Function.Properties.Inverse using (↔-refl; ↔-sym; ↔⇒↣)
23 import Function.Related.Propositional as Related
24 open import Function.Related.TypeIsomorphisms using (Σ-assoc)
25 open import Level using (Level; _⊔_)
26 open import Relation.Unary using (Pred)
27 import Induction.WellFounded as WF
28 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
29 open import Relation.Binary.PropositionalEquality.Properties
30 using (module ≡-Reasoning)
31 import Relation.Binary.Construct.On as On using (wellFounded)
32
33 private
34 variable
35 a p : Level
36 A : Set a
37 P : Pred A p
38
39 ------------------------------------------------------------------------
40 -- Some code that is used to work around Agda's syntactic guardedness
41 -- checker.
42
43 private
44
45 infixr 5 _∷_ _⋎_
46
47 data ColistP (A : Set a) : Set a where
48 [] : ColistP A
49 _∷_ : A → ∞ (ColistP A) → ColistP A
50 _⋎_ : ColistP A → ColistP A → ColistP A
51
52 data ColistW (A : Set a) : Set a where
53 [] : ColistW A
54 _∷_ : A → ColistP A → ColistW A
55
56 program : Colist A → ColistP A
57 program [] = []
58 program (x ∷ xs) = x ∷ ♯ program (♭ xs)
59
60 mutual
61
62 _⋎W_ : ColistW A → ColistP A → ColistW A
63 [] ⋎W ys = whnf ys
64 (x ∷ xs) ⋎W ys = x ∷ (ys ⋎ xs)
65
66 whnf : ColistP A → ColistW A
67 whnf [] = []
68 whnf (x ∷ xs) = x ∷ ♭ xs
69 whnf (xs ⋎ ys) = whnf xs ⋎W ys
70
71 mutual
72
73 ⟦_⟧P : ColistP A → Colist A
74 ⟦ xs ⟧P = ⟦ whnf xs ⟧W
75
76 ⟦_⟧W : ColistW A → Colist A
77 ⟦ [] ⟧W = []
78 ⟦ x ∷ xs ⟧W = x ∷ ♯ ⟦ xs ⟧P
79
80 mutual
81
82 ⋎-homP : ∀ (xs : ColistP A) {ys} → ⟦ xs ⋎ ys ⟧P ≈ ⟦ xs ⟧P Colist.⋎ ⟦ ys ⟧P
83 ⋎-homP xs = ⋎-homW (whnf xs) _
84
85 ⋎-homW : ∀ (xs : ColistW A) ys → ⟦ xs ⋎W ys ⟧W ≈ ⟦ xs ⟧W Colist.⋎ ⟦ ys ⟧P
86 ⋎-homW (x ∷ xs) ys = x ∷ ♯ ⋎-homP ys
87 ⋎-homW [] ys = begin ⟦ ys ⟧P ∎
88 where open ≈-Reasoning
89
90 ⟦program⟧P : ∀ (xs : Colist A) → ⟦ program xs ⟧P ≈ xs
91 ⟦program⟧P [] = []
92 ⟦program⟧P (x ∷ xs) = x ∷ ♯ ⟦program⟧P (♭ xs)
93
94 Any-⋎P : ∀ xs {ys} →
95 Any P ⟦ program xs ⋎ ys ⟧P ↔ (Any P xs ⊎ Any P ⟦ ys ⟧P)
96 Any-⋎P {P = P} xs {ys} =
97 Any P ⟦ program xs ⋎ ys ⟧P ↔⟨ Any-cong ↔-refl (⋎-homP (program xs)) ⟩
98 Any P (⟦ program xs ⟧P Colist.⋎ ⟦ ys ⟧P) ↔⟨ Any-⋎ _ ⟩
99 (Any P ⟦ program xs ⟧P ⊎ Any P ⟦ ys ⟧P) ↔⟨ Any-cong ↔-refl (⟦program⟧P _) ⊎-cong (_ ∎) ⟩
100 (Any P xs ⊎ Any P ⟦ ys ⟧P) ∎
101 where open Related.EquationalReasoning
102
103 index-Any-⋎P :
104 ∀ xs {ys} (p : Any P ⟦ program xs ⋎ ys ⟧P) →
105 index p ≥′ [ index , index ]′ (Inverse.to (Any-⋎P xs) p)
106 index-Any-⋎P xs p
107 with Any-resp id (⋎-homW (whnf (program xs)) _) p
108 | index-Any-resp {f = id} (⋎-homW (whnf (program xs)) _) p
109 index-Any-⋎P xs p | q | q≡p
110 with Inverse.to (Any-⋎ ⟦ program xs ⟧P) q
111 | index-Any-⋎ ⟦ program xs ⟧P q
112 index-Any-⋎P xs p | q | q≡p | inj₂ r | r≤q rewrite q≡p = r≤q
113 index-Any-⋎P xs p | q | q≡p | inj₁ r | r≤q
114 with Any-resp id (⟦program⟧P xs) r
115 | index-Any-resp {f = id} (⟦program⟧P xs) r
116 index-Any-⋎P xs p | q | q≡p | inj₁ r | r≤q | s | s≡r
117 rewrite s≡r | q≡p = r≤q
118
119 ------------------------------------------------------------------------
120 -- Infinite variant of _⋎_.
121
122 private
123
124 merge′ : Colist (A × Colist A) → ColistP A
125 merge′ [] = []
126 merge′ ((x , xs) ∷ xss) = x ∷ ♯ (program xs ⋎ merge′ (♭ xss))
127
128 merge : Colist (A × Colist A) → Colist A
129 merge xss = ⟦ merge′ xss ⟧P
130
131 ------------------------------------------------------------------------
132 -- Any lemma for merge.
133
134 Any-merge : ∀ xss → Any P (merge xss) ↔ Any (λ { (x , xs) → P x ⊎ Any P xs }) xss
135 Any-merge {P = P} xss = mk↔ₛ′ (proj₁ ∘ to xss) from to∘from (proj₂ ∘ to xss)
136 where
137 open ≡-Reasoning
138
139 -- The from function.
140
141 Q = λ { (x , xs) → P x ⊎ Any P xs }
142
143 from : ∀ {xss} → Any Q xss → Any P (merge xss)
144 from (here (inj₁ p)) = here p
145 from (here (inj₂ p)) = there (Inverse.from (Any-⋎P _) (inj₁ p))
146 from (there {x = _ , xs} p) = there (Inverse.from (Any-⋎P xs) (inj₂ (from p)))
147
148 -- The from function is injective.
149
150 from-injective : ∀ {xss} (p₁ p₂ : Any Q xss) →
151 from p₁ ≡ from p₂ → p₁ ≡ p₂
152 from-injective (here (inj₁ p)) (here (inj₁ .p)) refl = refl
153 from-injective (here (inj₂ p₁)) (here (inj₂ p₂)) eq =
154 cong (here ∘ inj₂) $
155 inj₁-injective $
156 Injection.injective (↔⇒↣ (↔-sym (Any-⋎P _))) $
157 there-injective eq
158 from-injective (here (inj₂ p₁)) (there p₂) eq with
159 Injection.injective (↔⇒↣ (↔-sym (Any-⋎P _)))
160 {x = inj₁ p₁} {y = inj₂ (from p₂)}
161 (there-injective eq)
162 ... | ()
163 from-injective (there p₁) (here (inj₂ p₂)) eq with
164 Injection.injective (↔⇒↣ (↔-sym (Any-⋎P _)))
165 {x = inj₂ (from p₁)} {y = inj₁ p₂}
166 (there-injective eq)
167 ... | ()
168 from-injective (there {x = _ , xs} p₁) (there p₂) eq =
169 cong there $
170 from-injective p₁ p₂ $
171 inj₂-injective $
172 Injection.injective (↔⇒↣ (↔-sym (Any-⋎P xs))) $
173 there-injective eq
174 -- The to function (defined as a right inverse of from).
175
176 Input = ∃ λ xss → Any P (merge xss)
177
178 InputPred : Input → Set _
179 InputPred (xss , p) = ∃ λ (q : Any Q xss) → from q ≡ p
180
181 to : ∀ xss p → InputPred (xss , p)
182 to xss p =
183 WF.All.wfRec (On.wellFounded size <′-wellFounded) _
184 InputPred step (xss , p)
185 where
186 size : Input → ℕ
187 size (_ , p) = index p
188
189 step : ∀ p → WF.WfRec (_<′_ on size) InputPred p → InputPred p
190 step ([] , ()) rec
191 step ((x , xs) ∷ xss , here p) rec = here (inj₁ p) , refl
192 step ((x , xs) ∷ xss , there p) rec
193 with Inverse.to (Any-⋎P xs) p
194 | Inverse.strictlyInverseʳ (Any-⋎P xs) p
195 | index-Any-⋎P xs p
196 ... | inj₁ q | refl | _ = here (inj₂ q) , refl
197 ... | inj₂ q | refl | q≤p =
198 Product.map there
199 (cong (there ∘ (Inverse.from (Any-⋎P xs)) ∘ inj₂))
200 (rec (s≤′s q≤p))
201
202 to∘from = λ p → from-injective _ _ (proj₂ (to xss (from p)))
203
204 -- Every member of xss is a member of merge xss, and vice versa (with
205 -- equal multiplicities).
206
207 ∈-merge : ∀ {y : A} xss → y ∈ merge xss ↔ ∃₂ λ x xs → (x , xs) ∈ xss × (y ≡ x ⊎ y ∈ xs)
208 ∈-merge {y = y} xss =
209 y ∈ merge xss ↔⟨ Any-merge _ ⟩
210 Any (λ { (x , xs) → y ≡ x ⊎ y ∈ xs }) xss ↔⟨ Any-∈ ⟩
211 (∃ λ { (x , xs) → (x , xs) ∈ xss × (y ≡ x ⊎ y ∈ xs) }) ↔⟨ Σ-assoc ⟩
212 (∃₂ λ x xs → (x , xs) ∈ xss × (y ≡ x ⊎ y ∈ xs)) ∎
213 where open Related.EquationalReasoning
214