Debian Codegraph @

src/Codata/Musical/Colist.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Coinductive lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --guardedness #-}
8
9 module Codata.Musical.Colist where
10
11 open import Level using (Level)
12 open import Effect.Monad using (RawMonad)
13 open import Codata.Musical.Notation using (♭; ∞; ♯_)
14 open import Codata.Musical.Conat using (Coℕ; zero; suc)
15 import Codata.Musical.Colist.Properties
16 import Codata.Musical.Colist.Relation.Unary.All.Properties
17 open import Data.Bool.Base using (Bool; true; false)
18 open import Data.Empty using (⊥)
19 open import Data.Maybe using (Maybe; nothing; just; Is-just)
20 open import Data.Maybe.Relation.Unary.Any using (just)
21 open import Data.Nat.Base using (ℕ; zero; suc)
22 open import Data.List.Base using (List; []; _∷_)
23 open import Data.List.NonEmpty using (List⁺; _∷_)
24 open import Data.Product.Base as Product using (∃; _×_; _,_)
25 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′)
26 open import Data.Vec.Bounded as Vec≤ using (Vec≤)
27 open import Function.Base using (_∘_; const; id; _∋_; _$_)
28 open import Function.Bundles using (_↔_; mk↔ₛ′)
29 open import Level using (_⊔_)
30 open import Relation.Binary.Core using (Rel; _⇒_)
31 open import Relation.Binary.Bundles using (Poset; Setoid; Preorder)
32 open import Relation.Binary.Definitions using (Transitive; Antisymmetric)
33 import Relation.Binary.Construct.FromRel as Ind using (preorder)
34 import Relation.Binary.Reasoning.Preorder as ≲-Reasoning
35 import Relation.Binary.Reasoning.PartialOrder as ≤-Reasoning
36 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
37 open import Relation.Binary.Reasoning.Syntax
38 open import Relation.Nullary.Reflects using (invert)
39 open import Relation.Nullary
40 open import Relation.Nullary.Negation using (¬_; contradiction; ¬¬-Monad)
41 open import Relation.Nullary.Decidable using (¬¬-excluded-middle)
42 open import Relation.Unary using (Pred)
43
44 private
45 variable
46 a b p : Level
47 A : Set a
48 B : Set b
49 P : Pred A p
50
51 ------------------------------------------------------------------------
52 -- Re-export type and basic definitions
53
54 open import Codata.Musical.Colist.Base public
55 module Colist-injective = Codata.Musical.Colist.Properties
56 open import Codata.Musical.Colist.Bisimilarity public
57 open import Codata.Musical.Colist.Relation.Unary.All public
58 module All-injective = Codata.Musical.Colist.Relation.Unary.All.Properties
59 open import Codata.Musical.Colist.Relation.Unary.Any public
60 open import Codata.Musical.Colist.Relation.Unary.Any.Properties public
61
62 ------------------------------------------------------------------------
63 -- More operations
64
65 take : ∀ {a} {A : Set a} (n : ℕ) → Colist A → Vec≤ A n
66 take zero xs = Vec≤.[]
67 take (suc n) [] = Vec≤.[]
68 take (suc n) (x ∷ xs) = x Vec≤.∷ take n (♭ xs)
69
70
71 module ¬¬Monad {a} where
72 open RawMonad (¬¬-Monad {a}) public
73 open ¬¬Monad -- we don't want the RawMonad content to be opened publicly
74
75 ------------------------------------------------------------------------
76 -- Memberships, subsets, prefixes
77
78 -- x ∈ xs means that x is a member of xs.
79
80 infix 4 _∈_
81
82 _∈_ : {A : Set a} → A → Colist A → Set a
83 x ∈ xs = Any (_≡_ x) xs
84
85 -- xs ⊆ ys means that xs is a subset of ys.
86
87 infix 4 _⊆_
88
89 _⊆_ : {A : Set a} → Rel (Colist A) a
90 xs ⊆ ys = ∀ {x} → x ∈ xs → x ∈ ys
91
92 -- xs ⊑ ys means that xs is a prefix of ys.
93
94 infix 4 _⊑_
95
96 data _⊑_ {A : Set a} : Rel (Colist A) a where
97 [] : ∀ {ys} → [] ⊑ ys
98 _∷_ : ∀ x {xs ys} (p : ∞ (♭ xs ⊑ ♭ ys)) → x ∷ xs ⊑ x ∷ ys
99
100 -- Any can be expressed using _∈_ (and vice versa).
101
102 Any-∈ : ∀ {xs} → Any P xs ↔ ∃ λ x → x ∈ xs × P x
103 Any-∈ {P = P} = mk↔ₛ′
104 to
105 (λ { (x , x∈xs , p) → from x∈xs p })
106 (λ { (x , x∈xs , p) → to∘from x∈xs p })
107 from∘to
108 where
109 to : ∀ {xs} → Any P xs → ∃ λ x → x ∈ xs × P x
110 to (here p) = _ , here refl , p
111 to (there p) = Product.map id (Product.map there id) (to p)
112
113 from : ∀ {x xs} → x ∈ xs → P x → Any P xs
114 from (here refl) p = here p
115 from (there x∈xs) p = there (from x∈xs p)
116
117 to∘from : ∀ {x xs} (x∈xs : x ∈ xs) (p : P x) →
118 to (from x∈xs p) ≡ (x , x∈xs , p)
119 to∘from (here refl) p = refl
120 to∘from (there x∈xs) p =
121 cong (Product.map id (Product.map there id)) (to∘from x∈xs p)
122
123 from∘to : ∀ {xs} (p : Any P xs) →
124 let (x , x∈xs , px) = to p in from x∈xs px ≡ p
125 from∘to (here _) = refl
126 from∘to (there p) = cong there (from∘to p)
127
128 -- Prefixes are subsets.
129
130 ⊑⇒⊆ : _⊑_ {A = A} ⇒ _⊆_
131 ⊑⇒⊆ (x ∷ xs⊑ys) (here ≡x) = here ≡x
132 ⊑⇒⊆ (_ ∷ xs⊑ys) (there x∈xs) = there (⊑⇒⊆ (♭ xs⊑ys) x∈xs)
133
134 -- The prefix relation forms a poset.
135
136 ⊑-Poset : ∀ {ℓ} → Set ℓ → Poset _ _ _
137 ⊑-Poset A = record
138 { Carrier = Colist A
139 ; _≈_ = _≈_
140 ; _≤_ = _⊑_
141 ; isPartialOrder = record
142 { isPreorder = record
143 { isEquivalence = Setoid.isEquivalence (setoid A)
144 ; reflexive = reflexive
145 ; trans = trans
146 }
147 ; antisym = antisym
148 }
149 }
150 where
151 reflexive : _≈_ ⇒ _⊑_
152 reflexive [] = []
153 reflexive (x ∷ xs≈) = x ∷ ♯ reflexive (♭ xs≈)
154
155 trans : Transitive _⊑_
156 trans [] _ = []
157 trans (x ∷ xs≈) (.x ∷ ys≈) = x ∷ ♯ trans (♭ xs≈) (♭ ys≈)
158
159 tail : ∀ {x xs y ys} → x ∷ xs ⊑ y ∷ ys → ♭ xs ⊑ ♭ ys
160 tail (_ ∷ p) = ♭ p
161
162 antisym : Antisymmetric _≈_ _⊑_
163 antisym [] [] = []
164 antisym (x ∷ p₁) p₂ = x ∷ ♯ antisym (♭ p₁) (tail p₂)
165
166 module ⊑-Reasoning {a} {A : Set a} where
167 private module Base = ≤-Reasoning (⊑-Poset A)
168
169 open Base public hiding (step-<; step-≤)
170
171 open ⊑-syntax _IsRelatedTo_ _IsRelatedTo_ Base.≤-go public
172 open ⊏-syntax _IsRelatedTo_ _IsRelatedTo_ Base.<-go public
173
174
175 -- The subset relation forms a preorder.
176
177 ⊆-Preorder : ∀ {ℓ} → Set ℓ → Preorder _ _ _
178 ⊆-Preorder A = Ind.preorder (setoid A) _∈_
179 (λ xs≈ys → ⊑⇒⊆ (⊑A.reflexive xs≈ys))
180 where module ⊑A = Poset (⊑-Poset A)
181
182 -- Example uses:
183 --
184 -- x∈zs : x ∈ zs
185 -- x∈zs =
186 -- x ∈⟨ x∈xs ⟩
187 -- xs ⊆⟨ xs⊆ys ⟩
188 -- ys ≡⟨ ys≡zs ⟩
189 -- zs ∎
190 module ⊆-Reasoning {A : Set a} where
191 private module Base = ≲-Reasoning (⊆-Preorder A)
192
193 open Base public
194 hiding (step-≲; step-∼)
195 renaming (≲-go to ⊆-go)
196
197 open begin-membership-syntax _IsRelatedTo_ _∈_ (λ x → begin x)
198 open ⊆-syntax _IsRelatedTo_ _IsRelatedTo_ ⊆-go public
199
200
201 -- take returns a prefix.
202 take-⊑ : ∀ n (xs : Colist A) →
203 let toColist = fromList {a} ∘ Vec≤.toList in
204 toColist (take n xs) ⊑ xs
205 take-⊑ zero xs = []
206 take-⊑ (suc n) [] = []
207 take-⊑ (suc n) (x ∷ xs) = x ∷ ♯ take-⊑ n (♭ xs)
208
209 ------------------------------------------------------------------------
210 -- Finiteness and infiniteness
211
212 -- Finite xs means that xs has finite length.
213
214 data Finite {A : Set a} : Colist A → Set a where
215 [] : Finite []
216 _∷_ : ∀ x {xs} (fin : Finite (♭ xs)) → Finite (x ∷ xs)
217
218 infixr 5 _∷_
219
220 module Finite-injective where
221
222 ∷-injective : ∀ {x : A} {xs p q} → (Finite (x ∷ xs) ∋ x ∷ p) ≡ x ∷ q → p ≡ q
223 ∷-injective refl = refl
224
225 -- Infinite xs means that xs has infinite length.
226
227 data Infinite {A : Set a} : Colist A → Set a where
228 _∷_ : ∀ x {xs} (inf : ∞ (Infinite (♭ xs))) → Infinite (x ∷ xs)
229
230 module Infinite-injective where
231
232 ∷-injective : ∀ {x : A} {xs p q} → (Infinite (x ∷ xs) ∋ x ∷ p) ≡ x ∷ q → p ≡ q
233 ∷-injective refl = refl
234
235 -- Colists which are not finite are infinite.
236
237 not-finite-is-infinite :
238 (xs : Colist A) → ¬ Finite xs → Infinite xs
239 not-finite-is-infinite [] hyp = contradiction [] hyp
240 not-finite-is-infinite (x ∷ xs) hyp =
241 x ∷ ♯ not-finite-is-infinite (♭ xs) (hyp ∘ _∷_ x)
242
243 -- Colists are either finite or infinite (classically).
244
245 finite-or-infinite :
246 (xs : Colist A) → ¬ ¬ (Finite xs ⊎ Infinite xs)
247 finite-or-infinite xs = helper <$> ¬¬-excluded-middle
248 where
249 helper : Dec (Finite xs) → Finite xs ⊎ Infinite xs
250 helper ( true because [fin]) = inj₁ (invert [fin])
251 helper (false because [¬fin]) =
252 inj₂ $ not-finite-is-infinite xs (invert [¬fin])
253
254 -- Colists are not both finite and infinite.
255
256 not-finite-and-infinite :
257 ∀ {xs : Colist A} → Finite xs → Infinite xs → ⊥
258 not-finite-and-infinite (x ∷ fin) (.x ∷ inf) =
259 not-finite-and-infinite fin (♭ inf)