~ubuntu-branches/ubuntu/wily/agda-stdlib/wily-proposed

« back to all changes in this revision

Viewing changes to src/Data/Colist.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2011-05-24 10:26:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110524102615-9tontf43bq9ym690
Tags: 0.5-1
* [9251e0b] Imported Upstream version 0.5
* [ba20206] Make package architecture independent (Closes: #573807, #598708,
  #599958)
* [2c82171] Add watch file
* [0518fa6] No longer need procps | hurd BD as we no longer have the ticker
* [daf2445] Don't use upstream's make install — handled ourselves by
  dh_install
* [1b86533] Update watchfile to point at new location
* [637f47d] Remove debian/gbp.conf as we are no longer building for exp
* [cc88671] Require Agda 2.2.10
* [e99dab5] Set maximum stack size to 1G to prevent overflows in the build
* [251cd1d] Run the test suite manually
* [a7db697] Set the variable in the emacs loading script properly
* [540ca3f] Improve short description
* [6b3e794] Add dependencies on compatible versions of the Agda library
* [7127678] Standards-Version bump to 3.9.2, no changes required
* [9d0ae30] Update to use ghc instead of ghc6
* [3f6879a] Set Maintainer to my d.o email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
-- Coinductive lists
3
3
------------------------------------------------------------------------
4
4
 
 
5
{-# OPTIONS --universe-polymorphism #-}
 
6
 
5
7
module Data.Colist where
6
8
 
7
9
open import Category.Monad
18
20
  using (BoundedVec; []; _∷_)
19
21
open import Data.Product using (_,_)
20
22
open import Data.Sum     using (_⊎_; inj₁; inj₂)
21
 
open import Data.Function
 
23
open import Function
 
24
open import Level
22
25
open import Relation.Binary
23
26
import Relation.Binary.InducedPreorders as Ind
 
27
open import Relation.Binary.PropositionalEquality using (_≡_)
24
28
open import Relation.Nullary
25
29
open import Relation.Nullary.Negation
26
30
open RawMonad ¬¬-Monad
30
34
 
31
35
infixr 5 _∷_
32
36
 
33
 
data Colist (A : Set) : Set where
 
37
data Colist {a} (A : Set a) : Set a where
34
38
  []  : Colist A
35
39
  _∷_ : (x : A) (xs : ∞ (Colist A)) → Colist A
36
40
 
37
 
data Any {A} (P : A → Set) : Colist A → Set where
 
41
data Any {a p} {A : Set a} (P : A → Set p) :
 
42
         Colist A → Set (a ⊔ p) where
38
43
  here  : ∀ {x xs} (px  : P x)          → Any P (x ∷ xs)
39
44
  there : ∀ {x xs} (pxs : Any P (♭ xs)) → Any P (x ∷ xs)
40
45
 
41
 
data All {A} (P : A → Set) : Colist A → Set where
 
46
data All {a p} {A : Set a} (P : A → Set p) :
 
47
         Colist A → Set (a ⊔ p) where
42
48
  []  : All P []
43
49
  _∷_ : ∀ {x xs} (px : P x) (pxs : ∞ (All P (♭ xs))) → All P (x ∷ xs)
44
50
 
45
51
------------------------------------------------------------------------
46
52
-- Some operations
47
53
 
48
 
null : ∀ {A} → Colist A → Bool
 
54
null : ∀ {a} {A : Set a} → Colist A → Bool
49
55
null []      = true
50
56
null (_ ∷ _) = false
51
57
 
52
 
length : ∀ {A} → Colist A → Coℕ
 
58
length : ∀ {a} {A : Set a} → Colist A → Coℕ
53
59
length []       = zero
54
60
length (x ∷ xs) = suc (♯ length (♭ xs))
55
61
 
56
 
map : ∀ {A B} → (A → B) → Colist A → Colist B
 
62
map : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → Colist A → Colist B
57
63
map f []       = []
58
64
map f (x ∷ xs) = f x ∷ ♯ map f (♭ xs)
59
65
 
60
 
fromList : ∀ {A} → List A → Colist A
 
66
fromList : ∀ {a} {A : Set a} → List A → Colist A
61
67
fromList []       = []
62
68
fromList (x ∷ xs) = x ∷ ♯ fromList xs
63
69
 
64
 
take : ∀ {A} (n : ℕ) → Colist A → BoundedVec A n
 
70
take : ∀ {a} {A : Set a} (n : ℕ) → Colist A → BoundedVec A n
65
71
take zero    xs       = []
66
72
take (suc n) []       = []
67
73
take (suc n) (x ∷ xs) = x ∷ take n (♭ xs)
68
74
 
69
 
replicate : ∀ {A} → Coℕ → A → Colist A
 
75
replicate : ∀ {a} {A : Set a} → Coℕ → A → Colist A
70
76
replicate zero    x = []
71
77
replicate (suc n) x = x ∷ ♯ replicate (♭ n) x
72
78
 
73
 
lookup : ∀ {A} → ℕ → Colist A → Maybe A
 
79
lookup : ∀ {a} {A : Set a} → ℕ → Colist A → Maybe A
74
80
lookup n       []       = nothing
75
81
lookup zero    (x ∷ xs) = just x
76
82
lookup (suc n) (x ∷ xs) = lookup n (♭ xs)
77
83
 
78
84
infixr 5 _++_
79
85
 
80
 
_++_ : ∀ {A} → Colist A → Colist A → Colist A
 
86
_++_ : ∀ {a} {A : Set a} → Colist A → Colist A → Colist A
81
87
[]       ++ ys = ys
82
88
(x ∷ xs) ++ ys = x ∷ ♯ (♭ xs ++ ys)
83
89
 
84
 
concat : ∀ {A} → Colist (List⁺ A) → Colist A
 
90
concat : ∀ {a} {A : Set a} → Colist (List⁺ A) → Colist A
85
91
concat []               = []
86
92
concat ([ x ]⁺   ∷ xss) = x ∷ ♯ concat (♭ xss)
87
93
concat ((x ∷ xs) ∷ xss) = x ∷ ♯ concat (xs ∷ xss)
88
94
 
89
 
[_] : ∀ {A} → A → Colist A
 
95
[_] : ∀ {a} {A : Set a} → A → Colist A
90
96
[ x ] = x ∷ ♯ []
91
97
 
92
98
------------------------------------------------------------------------
96
102
 
97
103
infix 4 _≈_
98
104
 
99
 
data _≈_ {A} : (xs ys : Colist A) → Set where
 
105
data _≈_ {a} {A : Set a} : (xs ys : Colist A) → Set a where
100
106
  []  :                                       []     ≈ []
101
107
  _∷_ : ∀ x {xs ys} (xs≈ : ∞ (♭ xs ≈ ♭ ys)) → x ∷ xs ≈ x ∷ ys
102
108
 
103
109
-- The equality relation forms a setoid.
104
110
 
105
 
setoid : Set → Setoid _ _
 
111
setoid : ∀ {ℓ} → Set ℓ → Setoid _ _
106
112
setoid A = record
107
113
  { Carrier       = Colist A
108
114
  ; _≈_           = _≈_
128
134
module ≈-Reasoning where
129
135
  import Relation.Binary.EqReasoning as EqR
130
136
  private
131
 
    open module R {A : Set} = EqR (setoid A) public
 
137
    open module R {a} {A : Set a} = EqR (setoid A) public
132
138
 
133
139
-- map preserves equality.
134
140
 
135
 
map-cong : ∀ {A B} (f : A → B) → _≈_ =[ map f ]⇒ _≈_
 
141
map-cong : ∀ {a b} {A : Set a} {B : Set b}
 
142
           (f : A → B) → _≈_ =[ map f ]⇒ _≈_
136
143
map-cong f []        = []
137
144
map-cong f (x ∷ xs≈) = f x ∷ ♯ map-cong f (♭ xs≈)
138
145
 
143
150
 
144
151
infix 4 _∈_
145
152
 
146
 
data _∈_ {A : Set} : A → Colist A → Set where
147
 
  here  : ∀ {x   xs}                   → x ∈ x ∷ xs
148
 
  there : ∀ {x y xs} (x∈xs : x ∈ ♭ xs) → x ∈ y ∷ xs
 
153
_∈_ : ∀ {a} → {A : Set a} → A → Colist A → Set a
 
154
x ∈ xs = Any (_≡_ x) xs
149
155
 
150
156
-- xs ⊆ ys means that xs is a subset of ys.
151
157
 
152
158
infix 4 _⊆_
153
159
 
154
 
_⊆_ : {A : Set} → Colist A → Colist A → Set
 
160
_⊆_ : ∀ {a} → {A : Set a} → Colist A → Colist A → Set a
155
161
xs ⊆ ys = ∀ {x} → x ∈ xs → x ∈ ys
156
162
 
157
163
-- xs ⊑ ys means that xs is a prefix of ys.
158
164
 
159
165
infix 4 _⊑_
160
166
 
161
 
data _⊑_ {A : Set} : Colist A → Colist A → Set where
 
167
data _⊑_ {a} {A : Set a} : Colist A → Colist A → Set a where
162
168
  []  : ∀ {ys}                            → []     ⊑ ys
163
169
  _∷_ : ∀ x {xs ys} (p : ∞ (♭ xs ⊑ ♭ ys)) → x ∷ xs ⊑ x ∷ ys
164
170
 
165
171
-- Prefixes are subsets.
166
172
 
167
 
⊑⇒⊆ : {A : Set} → _⊑_ {A = A} ⇒ _⊆_
 
173
⊑⇒⊆ : ∀ {a} → {A : Set a} → _⊑_ {A = A} ⇒ _⊆_
168
174
⊑⇒⊆ []          ()
169
 
⊑⇒⊆ (x ∷ xs⊑ys) here         = here
 
175
⊑⇒⊆ (x ∷ xs⊑ys) (here ≡x)    = here ≡x
170
176
⊑⇒⊆ (_ ∷ xs⊑ys) (there x∈xs) = there (⊑⇒⊆ (♭ xs⊑ys) x∈xs)
171
177
 
172
178
-- The prefix relation forms a poset.
173
179
 
174
 
⊑-Poset : Set → Poset _ _ _
 
180
⊑-Poset : ∀ {ℓ} → Set ℓ → Poset _ _ _
175
181
⊑-Poset A = record
176
182
  { Carrier        = Colist A
177
183
  ; _≈_            = _≈_
181
187
      { isEquivalence = Setoid.isEquivalence (setoid A)
182
188
      ; reflexive     = reflexive
183
189
      ; trans         = trans
184
 
      ; ∼-resp-≈      = ((λ {_} → ⊑-resp-≈ˡ) , λ {_} → ⊑-resp-≈ʳ)
185
190
      }
186
191
    ; antisym  = antisym
187
192
    }
195
200
  trans []        _          = []
196
201
  trans (x ∷ xs≈) (.x ∷ ys≈) = x ∷ ♯ trans (♭ xs≈) (♭ ys≈)
197
202
 
198
 
  ⊑-resp-≈ˡ : {xs : Colist A} →  (λ ys → xs ⊑ ys) Respects _≈_
199
 
  ⊑-resp-≈ˡ _         []       = []
200
 
  ⊑-resp-≈ˡ (x ∷ xs≈) (.x ∷ p) = x ∷ ♯ ⊑-resp-≈ˡ (♭ xs≈) (♭ p)
201
 
 
202
 
  ⊑-resp-≈ʳ : {ys : Colist A} →  (λ xs → xs ⊑ ys) Respects _≈_
203
 
  ⊑-resp-≈ʳ []        _        = []
204
 
  ⊑-resp-≈ʳ (x ∷ xs≈) (.x ∷ p) = x ∷ ♯ ⊑-resp-≈ʳ (♭ xs≈) (♭ p)
205
 
 
206
203
  antisym : Antisymmetric _≈_ _⊑_
207
204
  antisym []       []        = []
208
205
  antisym (x ∷ p₁) (.x ∷ p₂) = x ∷ ♯ antisym (♭ p₁) (♭ p₂)
210
207
module ⊑-Reasoning where
211
208
  import Relation.Binary.PartialOrderReasoning as POR
212
209
  private
213
 
    open module R {A : Set} = POR (⊑-Poset A)
 
210
    open module R {a} {A : Set a} = POR (⊑-Poset A)
214
211
      public renaming (_≤⟨_⟩_ to _⊑⟨_⟩_)
215
212
 
216
213
-- The subset relation forms a preorder.
217
214
 
218
 
⊆-Preorder : Set → Preorder _ _ _
 
215
⊆-Preorder : ∀ {ℓ} → Set ℓ → Preorder _ _ _
219
216
⊆-Preorder A =
220
217
  Ind.InducedPreorder₂ (setoid A) _∈_
221
 
                       (λ xs≈ys → ⊑⇒⊆ $ ⊑P.reflexive xs≈ys)
 
218
                       (λ xs≈ys → ⊑⇒⊆ (⊑P.reflexive xs≈ys))
222
219
  where module ⊑P = Poset (⊑-Poset A)
223
220
 
224
221
module ⊆-Reasoning where
225
222
  import Relation.Binary.PreorderReasoning as PreR
226
223
  private
227
 
    open module R {A : Set} = PreR (⊆-Preorder A)
 
224
    open module R {a} {A : Set a} = PreR (⊆-Preorder A)
228
225
      public renaming (_∼⟨_⟩_ to _⊆⟨_⟩_)
229
226
 
230
227
  infix 1 _∈⟨_⟩_
231
228
 
232
 
  _∈⟨_⟩_ : ∀ {A : Set} (x : A) {xs ys} →
 
229
  _∈⟨_⟩_ : ∀ {a} {A : Set a} (x : A) {xs ys} →
233
230
           x ∈ xs → xs IsRelatedTo ys → x ∈ ys
234
231
  x ∈⟨ x∈xs ⟩ xs⊆ys = (begin xs⊆ys) x∈xs
235
232
 
236
233
-- take returns a prefix.
237
234
 
238
 
take-⊑ : ∀ {A} n (xs : Colist A) →
239
 
         let toColist = fromList ∘ BVec.toList in
 
235
take-⊑ : ∀ {a} {A : Set a} n (xs : Colist A) →
 
236
         let toColist = fromList {a} ∘ BVec.toList in
240
237
         toColist (take n xs) ⊑ xs
241
238
take-⊑ zero    xs       = []
242
239
take-⊑ (suc n) []       = []
247
244
 
248
245
-- Finite xs means that xs has finite length.
249
246
 
250
 
data Finite {A : Set} : Colist A → Set where
 
247
data Finite {a} {A : Set a} : Colist A → Set a where
251
248
  []  : Finite []
252
249
  _∷_ : ∀ x {xs} (fin : Finite (♭ xs)) → Finite (x ∷ xs)
253
250
 
254
251
-- Infinite xs means that xs has infinite length.
255
252
 
256
 
data Infinite {A : Set} : Colist A → Set where
 
253
data Infinite {a} {A : Set a} : Colist A → Set a where
257
254
  _∷_ : ∀ x {xs} (inf : ∞ (Infinite (♭ xs))) → Infinite (x ∷ xs)
258
255
 
259
256
-- Colists which are not finite are infinite.
260
257
 
261
258
not-finite-is-infinite :
262
 
  {A : Set} (xs : Colist A) → ¬ Finite xs → Infinite xs
 
259
  ∀ {a} {A : Set a} (xs : Colist A) → ¬ Finite xs → Infinite xs
263
260
not-finite-is-infinite []       hyp with hyp []
264
261
... | ()
265
262
not-finite-is-infinite (x ∷ xs) hyp =
266
263
  x ∷ ♯ not-finite-is-infinite (♭ xs) (hyp ∘ _∷_ x)
267
264
 
268
265
-- Colists are either finite or infinite (classically).
 
266
--
 
267
-- TODO: Make this definition universe polymorphic.
269
268
 
270
269
finite-or-infinite :
271
270
  {A : Set} (xs : Colist A) → ¬ ¬ (Finite xs ⊎ Infinite xs)
278
277
-- Colists are not both finite and infinite.
279
278
 
280
279
not-finite-and-infinite :
281
 
  {A : Set} {xs : Colist A} → Finite xs → Infinite xs → ⊥
 
280
  ∀ {a} {A : Set a} {xs : Colist A} → Finite xs → Infinite xs → ⊥
282
281
not-finite-and-infinite []        ()
283
282
not-finite-and-infinite (x ∷ fin) (.x ∷ inf) =
284
283
  not-finite-and-infinite fin (♭ inf)