Debian Codegraph @

src/Codata/Sized/Cowriter.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The Cowriter type and some operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --sized-types #-}
8
9 module Codata.Sized.Cowriter where
10
11 open import Size using (Size; ∞)
12 open import Level as L using (Level)
13 open import Codata.Sized.Thunk using (Thunk; force)
14 open import Codata.Sized.Conat using (Conat; zero; suc)
15 open import Codata.Sized.Delay using (Delay; later; now)
16 open import Codata.Sized.Stream as Stream using (Stream; _∷_)
17 open import Data.Unit.Base using (⊤)
18 open import Data.List.Base using (List; []; _∷_)
19 open import Data.List.NonEmpty.Base using (List⁺; _∷_)
20 open import Data.Nat.Base as ℕ using (ℕ; zero; suc)
21 open import Data.Product.Base as Product using (_×_; _,_)
22 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
23 open import Data.Vec.Base using (Vec; []; _∷_)
24 open import Data.Vec.Bounded.Base as Vec≤ using (Vec≤; _,_)
25 open import Function.Base using (_$_; _∘′_; id)
26
27 private
28 variable
29 a b w x : Level
30 A : Set a
31 B : Set b
32 W : Set w
33 X : Set x
34
35 ------------------------------------------------------------------------
36 -- Definition
37
38 data Cowriter (W : Set w) (A : Set a) (i : Size) : Set (a L.⊔ w) where
39 [_] : A → Cowriter W A i
40 _∷_ : W → Thunk (Cowriter W A) i → Cowriter W A i
41
42 infixr 5 _∷_
43
44 ------------------------------------------------------------------------
45 -- Relationship to Delay.
46
47 fromDelay : ∀ {i} → Delay A i → Cowriter ⊤ A i
48 fromDelay (now a) = [ a ]
49 fromDelay (later da) = _ ∷ λ where .force → fromDelay (da .force)
50
51 toDelay : ∀ {i} → Cowriter W A i → Delay A i
52 toDelay [ a ] = now a
53 toDelay (_ ∷ ca) = later λ where .force → toDelay (ca .force)
54
55 ------------------------------------------------------------------------
56 -- Basic functions.
57
58 fromStream : ∀ {i} → Stream W i → Cowriter W A i
59 fromStream (w ∷ ws) = w ∷ λ where .force → fromStream (ws .force)
60
61 repeat : W → Cowriter W A ∞
62 repeat = fromStream ∘′ Stream.repeat
63
64 length : ∀ {i} → Cowriter W A i → Conat i
65 length [ _ ] = zero
66 length (w ∷ cw) = suc λ where .force → length (cw .force)
67
68 splitAt : ∀ (n : ℕ) → Cowriter W A ∞ → (Vec W n × Cowriter W A ∞) ⊎ (Vec≤ W n × A)
69 splitAt zero cw = inj₁ ([] , cw)
70 splitAt (suc n) [ a ] = inj₂ (Vec≤.[] , a)
71 splitAt (suc n) (w ∷ cw) = Sum.map (Product.map₁ (w ∷_)) (Product.map₁ (w Vec≤.∷_))
72 $ splitAt n (cw .force)
73
74 take : ∀ (n : ℕ) → Cowriter W A ∞ → Vec W n ⊎ (Vec≤ W n × A)
75 take n = Sum.map₁ Product.proj₁ ∘′ splitAt n
76
77 infixr 5 _++_ _⁺++_
78 _++_ : ∀ {i} → List W → Cowriter W A i → Cowriter W A i
79 [] ++ ca = ca
80 (w ∷ ws) ++ ca = w ∷ λ where .force → ws ++ ca
81
82 _⁺++_ : ∀ {i} → List⁺ W → Thunk (Cowriter W A) i → Cowriter W A i
83 (w ∷ ws) ⁺++ ca = w ∷ λ where .force → ws ++ ca .force
84
85 concat : ∀ {i} → Cowriter (List⁺ W) A i → Cowriter W A i
86 concat [ a ] = [ a ]
87 concat (w ∷ ca) = w ⁺++ λ where .force → concat (ca .force)
88
89 ------------------------------------------------------------------------
90 -- Functor, Applicative and Monad
91
92 map : ∀ {i} → (W → X) → (A → B) → Cowriter W A i → Cowriter X B i
93 map f g [ a ] = [ g a ]
94 map f g (w ∷ cw) = f w ∷ λ where .force → map f g (cw .force)
95
96 map₁ : ∀ {i} → (W → X) → Cowriter W A i → Cowriter X A i
97 map₁ f = map f id
98
99 map₂ : ∀ {i} → (A → X) → Cowriter W A i → Cowriter W X i
100 map₂ = map id
101
102 ap : ∀ {i} → Cowriter W (A → X) i → Cowriter W A i → Cowriter W X i
103 ap [ f ] ca = map₂ f ca
104 ap (w ∷ cf) ca = w ∷ λ where .force → ap (cf .force) ca
105
106 infixl 1 _>>=_
107
108 _>>=_ : ∀ {i} → Cowriter W A i → (A → Cowriter W X i) → Cowriter W X i
109 [ a ] >>= f = f a
110 (w ∷ ca) >>= f = w ∷ λ where .force → ca .force >>= f
111
112 ------------------------------------------------------------------------
113 -- Construction.
114
115 unfold : ∀ {i} → (X → (W × X) ⊎ A) → X → Cowriter W A i
116 unfold next seed =
117 Sum.[ (λ where (w , seed′) → w ∷ λ where .force → unfold next seed′)
118 , [_]
119 ] (next seed)