Debian Codegraph @

src/Codata/Musical/Colist/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Coinductive lists: base type and functions
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --guardedness #-}
8
9 module Codata.Musical.Colist.Base where
10
11 open import Level using (Level)
12 open import Codata.Musical.Notation using (♭; ∞; ♯_)
13 open import Codata.Musical.Conat.Base using (Coℕ; zero; suc)
14 open import Data.Bool.Base using (Bool; true; false)
15 open import Data.List.Base using (List; []; _∷_)
16 open import Data.List.NonEmpty.Base using (List⁺; _∷_)
17 open import Data.Maybe.Base using (Maybe; nothing; just)
18 open import Data.Nat.Base using (ℕ; zero; suc)
19
20 private
21 variable
22 a b : Level
23 A : Set a
24 B : Set b
25
26 ------------------------------------------------------------------------
27 -- The type
28
29 infixr 5 _∷_
30
31 data Colist (A : Set a) : Set a where
32 [] : Colist A
33 _∷_ : (x : A) (xs : ∞ (Colist A)) → Colist A
34
35 {-# FOREIGN GHC
36 data AgdaColist a = Nil | Cons a (MAlonzo.RTE.Inf (AgdaColist a))
37 type AgdaColist' l a = AgdaColist a
38 #-}
39 {-# COMPILE GHC Colist = data AgdaColist' (Nil | Cons) #-}
40 {-# COMPILE UHC Colist = data __LIST__ (__NIL__ | __CONS__) #-}
41
42 ------------------------------------------------------------------------
43 -- Some operations
44
45 null : Colist A → Bool
46 null [] = true
47 null (_ ∷ _) = false
48
49 length : Colist A → Coℕ
50 length [] = zero
51 length (x ∷ xs) = suc (♯ length (♭ xs))
52
53 map : (A → B) → Colist A → Colist B
54 map f [] = []
55 map f (x ∷ xs) = f x ∷ ♯ map f (♭ xs)
56
57 fromList : List A → Colist A
58 fromList [] = []
59 fromList (x ∷ xs) = x ∷ ♯ fromList xs
60
61 replicate : Coℕ → A → Colist A
62 replicate zero x = []
63 replicate (suc n) x = x ∷ ♯ replicate (♭ n) x
64
65 lookup : Colist A → ℕ → Maybe A
66 lookup [] _ = nothing
67 lookup (x ∷ _) zero = just x
68 lookup (_ ∷ xs) (suc n) = lookup (♭ xs) n
69
70 infixr 5 _++_
71
72 _++_ : Colist A → Colist A → Colist A
73 [] ++ ys = ys
74 (x ∷ xs) ++ ys = x ∷ ♯ (♭ xs ++ ys)
75
76 -- Interleaves the two colists (until the shorter one, if any, has
77 -- been exhausted).
78
79 infixr 5 _⋎_
80
81 _⋎_ : Colist A → Colist A → Colist A
82 [] ⋎ ys = ys
83 (x ∷ xs) ⋎ ys = x ∷ ♯ (ys ⋎ ♭ xs)
84
85 concat : Colist (List⁺ A) → Colist A
86 concat [] = []
87 concat ((x ∷ []) ∷ xss) = x ∷ ♯ concat (♭ xss)
88 concat ((x ∷ (y ∷ xs)) ∷ xss) = x ∷ ♯ concat ((y ∷ xs) ∷ xss)
89
90 [_] : A → Colist A
91 [ x ] = x ∷ ♯ []