ITree.Interp.Interp
Monadic interpretations of interaction trees
- The semantics of itrees are given as monad morphisms
itree E ~> M, also called "interpreters".
- "ITree interpreters" (or "itree morphisms") are monad morphisms where the codomain is made of ITrees: itree E ~> itree F.
- In general, "event handlers" are functions E ~> M where
M is a monad.
- "ITree event handlers" are functions E ~> itree F.
Translate
translate h t ≈ interp (trigger ∘ h) t
Definition translateF {E F R} (h : E ~> F) (rec: itree E R → itree F R) (t : itreeF E R _) : itree F R :=
match t with
| RetF x ⇒ Ret x
| TauF t ⇒ Tau (rec t)
| VisF e k ⇒ Vis (h _ e) (fun x ⇒ rec (k x))
end.
Definition translate {E F} (h : E ~> F)
: itree E ~> itree F
:= fun R ⇒ cofix translate_ t := translateF h translate_ (observe t).
Arguments translate {E F} & h [T].
match t with
| RetF x ⇒ Ret x
| TauF t ⇒ Tau (rec t)
| VisF e k ⇒ Vis (h _ e) (fun x ⇒ rec (k x))
end.
Definition translate {E F} (h : E ~> F)
: itree E ~> itree F
:= fun R ⇒ cofix translate_ t := translateF h translate_ (observe t).
Arguments translate {E F} & h [T].
Interpret
Definition interp {E M : Type → Type}
{FM : Functor M} {MM : Monad M} {IM : MonadIter M}
(h : E ~> M) :
itree E ~> M := fun R ⇒
iter (fun t ⇒
match observe t with
| RetF r ⇒ ret (inr r)
| TauF t ⇒ ret (inl t)
| VisF e k ⇒ fmap (fun x ⇒ inl (k x)) (h _ e)
end).
(* TODO: this does a map, and aloop does a bind. We could fuse those
by giving aloop a continuation to compose its bind with.
(coyoneda...) *)
Arguments interp {E M FM MM IM} & h [T].