Free Monads #
This file defines the free monad on an arbitrary mapping f : Type u → Type v
.
This has the undesirable property of raising the universe level by 1, which means we can't pass
a FreeMonad f
object as a continuation into an interactive protocol.
See PFunctor.FreeM
for the free monad of a polynomial functor, which does not raise the universe level.
The free monad on a functor f
is the type freely generated by values of type f α
and the operations pure
and bind
. Implemented directly rather than as a quotient.
Slightly different than constructions of free monads in Haskell because of universe issues.
- pure {f : Type u → Type v} {α : Type w} (x : α) : FreeMonad f α
- roll {f : Type u → Type v} {α : Type w} {β : Type u} (x : f β) (r : β → FreeMonad f α) : FreeMonad f α
Instances For
Lift an element of the base functor to the free monad by directly returning the result.
Equations
Instances For
Equations
- FreeMonad.instMonadLift = { monadLift := fun {α : Type ?u.11} (x : f α) => FreeMonad.lift x }
Equations
- FreeMonad.instInhabited = { default := FreeMonad.lift default }
Bind operator on OracleComp spec
operation used in the monad definition.
Equations
- (FreeMonad.pure x_2).bind x✝ = x✝ x_2
- (FreeMonad.roll x_2 r).bind x✝ = FreeMonad.roll x_2 fun (u : β_1) => (r u).bind x✝
Instances For
Equations
- One or more equations did not get rendered due to their size.
Proving something about FreeMonad f α
only requires two cases:
pure x
for somex : α
Note that we can't useSort v
instead ofProp
due to universe levels.
Equations
- ⋯ = ⋯
Instances For
Shoulde be possible to unify with the above
Equations
- FreeMonad.construct pure roll (FreeMonad.pure x_1) = pure x_1
- FreeMonad.construct pure roll (FreeMonad.roll x_1 r) = roll x_1 r fun (u : β) => FreeMonad.construct pure (fun {β : Type ?u.236} => roll) (r u)
Instances For
Equations
- FreeMonad.mapM_aux s (FreeMonad.pure x_1) = pure x_1
- FreeMonad.mapM_aux s (FreeMonad.roll x_1 r) = do let u ← s x_1 FreeMonad.mapM_aux (fun {α : Type ?u.57} => s) (r u)
Instances For
Equations
- FreeMonad.mapM' m s = { toFun := fun {α : Type ?u.14} => FreeMonad.mapM_aux fun {α : Type ?u.14} => s, toFun_pure' := ⋯, toFun_bind' := ⋯ }
Instances For
Canonical mapping of a free monad into any other monad, given a map on the base functor,
Equations
- (FreeMonad.pure x_2).mapM x✝ = pure x_2
- (FreeMonad.roll x_2 r).mapM x✝ = do let u ← x✝ x_2 (r u).mapM fun {α : Type ?u.263} => x✝
Instances For
The depth of a free monad as a potentially infinite natural number, defined recursively as the maximum depth of its children.
Equations
- (FreeMonad.pure x_1).depth = 0
- (FreeMonad.roll x_1 r).depth = 1 + ⨆ (u : β), (r u).depth
Instances For
Equations
- (FreeMonad.pure x_2).depthBindAux x✝ = (x✝ x_2).depth
- (FreeMonad.roll x_2 r).depthBindAux x✝ = 1 + ⨆ (u : β_1), (r u).depthBindAux x✝