Debian Codegraph @

doc/README/Data/Container/FreeMonad.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Example showing how the free monad construction on containers can be
5 -- used
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --sized-types #-}
9
10 module README.Data.Container.FreeMonad where
11
12 open import Level using (0ℓ)
13 open import Effect.Monad
14 open import Data.Empty
15 open import Data.Unit
16 open import Data.Bool.Base using (Bool; true)
17 open import Data.Nat
18 open import Data.Sum.Base using (inj₁; inj₂)
19 open import Data.Product.Base
20 open import Data.Container using (Container; _▷_)
21 open import Data.Container.Combinator hiding (_×_)
22 open import Data.Container.FreeMonad
23 open import Data.W
24 open import Relation.Binary.PropositionalEquality as ≡
25
26 ------------------------------------------------------------------------
27 -- Defining the signature of an effect and building trees describing
28 -- computations leveraging that effect.
29
30 -- The signature of state and its (generic) operations.
31
32 State : Set → Container _ _
33 State S = ⊤ ⟶ S ⊎ S ⟶ ⊤
34 where
35 _⟶_ : Set → Set → Container _ _
36 I ⟶ O = I ▷ λ _ → O
37
38 get : ∀ {S} → State S ⋆ S
39 get = impure (inj₁ _ , pure)
40
41 put : ∀ {S} → S → State S ⋆ ⊤
42 put s = impure (inj₂ s , pure)
43
44 -- Using the above we can, for example, write a stateful program that
45 -- delivers a boolean.
46 prog : State ℕ ⋆ Bool
47 prog =
48 get >>= λ n →
49 put (suc n) >>
50 pure true
51 where
52 open RawMonad monad using (_>>_)
53
54 runState : {S X : Set} → State S ⋆ X → (S → X × S)
55 runState (pure x) = λ s → x , s
56 runState (impure ((inj₁ _) , k)) = λ s → runState (k s) s
57 runState (impure ((inj₂ s) , k)) = λ _ → runState (k _) s
58
59 test : runState prog 0 ≡ (true , 1)
60 test = ≡.refl
61
62 -- It should be noted that @State S ⋆ X@ is not the state monad. If we
63 -- could quotient @State S ⋆ X@ by the seven axioms of state (see
64 -- Plotkin and Power's "Notions of Computation Determine Monads", 2002)
65 -- then we would get the state monad.
66
67 ------------------------------------------------------------------------
68 -- Defining effectful inductive data structures
69
70 -- The definition of `C ⋆ A` is strictly positive in `A`, meaning that we
71 -- can use `C ⋆_` when defining (co)inductive datatypes.
72
73
74 open import Size
75 open import Codata.Sized.Thunk
76 open import Data.Vec.Base using (Vec; []; _∷_)
77
78
79 -- A `Tap C A` is a infinite source of `A`s provided that we can perform
80 -- the effectful computations described by `C`.
81 -- The first one can be accessed readily but the rest of them is hidden
82 -- under layers of `C` computations.
83
84 module _ (C : Container 0ℓ 0ℓ) (A : Set 0ℓ) where
85
86 data Tap (i : Size) : Set 0ℓ where
87 _∷_ : A → Thunk (λ i → C ⋆ Tap i) i → Tap i
88
89 -- We can run a given tap for a set number of steps and collect the elements
90 -- thus generated along the way. This gives us a `C ⋆_` computation of a vector.
91
92 module _ {C : Container 0ℓ 0ℓ} {A : Set 0ℓ} where
93
94 take : Tap C A _ → (n : ℕ) → C ⋆ Vec A n
95 take _ 0 = pure []
96 take (x ∷ _) 1 = pure (x ∷ [])
97 take (x ∷ mxs) (suc n) = do
98 xs ← mxs .force
99 rest ← take xs n
100 pure (x ∷ rest)
101
102 -- A stream of all the natural numbers starting from a given value is an
103 -- example of a tap.
104
105 natsFrom : ∀ {i} → State ℕ ⋆ Tap (State ℕ) ℕ i
106 natsFrom = let open RawMonad monad using (_>>_) in do
107 n ← get
108 put (suc n)
109 pure (n ∷ λ where .force → natsFrom)
110
111 -- We can use `take` to capture an initial segment of the `natsFrom` tap
112 -- and, after running the state operations, observe that it does generate
113 -- successive numbers.
114
115 _ : ∀ k →
116 runState (natsFrom >>= λ ns → take ns 5) k
117 ≡ (k ∷ 1 + k ∷ 2 + k ∷ 3 + k ∷ 4 + k ∷ [] , 5 + k)
118 _ = λ k → refl