{-# OPTIONS --cubical-compatible --sized-types #-}
module README.Data.Container.FreeMonad where
open import Level using (0ℓ)
open import Effect.Monad
open import Data.Empty
open import Data.Unit
open import Data.Bool.Base using (Bool; true)
open import Data.Nat
open import Data.Sum.Base using (inj₁; inj₂)
open import Data.Product.Base
open import Data.Container using (Container; _▷_)
open import Data.Container.Combinator hiding (_×_)
open import Data.Container.FreeMonad
open import Data.W
open import Relation.Binary.PropositionalEquality as ≡
State : Set → Container _ _
State S = ⊤ ⟶ S ⊎ S ⟶ ⊤
where
_⟶_ : Set → Set → Container _ _
I ⟶ O = I ▷ λ _ → O
get : ∀ {S} → State S ⋆ S
get = impure (inj₁ _ , pure)
put : ∀ {S} → S → State S ⋆ ⊤
put s = impure (inj₂ s , pure)
prog : State ℕ ⋆ Bool
prog =
get >>= λ n →
put (suc n) >>
pure true
where
open RawMonad monad using (_>>_)
runState : {S X : Set} → State S ⋆ X → (S → X × S)
runState (pure x) = λ s → x , s
runState (impure ((inj₁ _) , k)) = λ s → runState (k s) s
runState (impure ((inj₂ s) , k)) = λ _ → runState (k _) s
test : runState prog 0 ≡ (true , 1)
test = ≡.refl
open import Size
open import Codata.Sized.Thunk
open import Data.Vec.Base using (Vec; []; _∷_)
module _ (C : Container 0ℓ 0ℓ) (A : Set 0ℓ) where
data Tap (i : Size) : Set 0ℓ where
_∷_ : A → Thunk (λ i → C ⋆ Tap i) i → Tap i
module _ {C : Container 0ℓ 0ℓ} {A : Set 0ℓ} where
take : Tap C A _ → (n : ℕ) → C ⋆ Vec A n
take _ 0 = pure []
take (x ∷ _) 1 = pure (x ∷ [])
take (x ∷ mxs) (suc n) = do
xs ← mxs .force
rest ← take xs n
pure (x ∷ rest)
natsFrom : ∀ {i} → State ℕ ⋆ Tap (State ℕ) ℕ i
natsFrom = let open RawMonad monad using (_>>_) in do
n ← get
put (suc n)
pure (n ∷ λ where .force → natsFrom)
_ : ∀ k →
runState (natsFrom >>= λ ns → take ns 5) k
≡ (k ∷ 1 + k ∷ 2 + k ∷ 3 + k ∷ 4 + k ∷ [] , 5 + k)
_ = λ k → refl