SLIDE 129 fromMonad m = BindK (m >>=)
newtype BindK m a = BindK { }
suspended bind!
toMonad (BindK k) = k return
_only_ return!
runBindK :: forall b. (a -> m b) -> m b
fromMonad :: Monad m => m a -> BindK m a
toMonad :: Monad m => BindK m a -> m a
instance Monad (BindK m) where (BindK m) >>= k = BindK (\c -> m (\a -> runBindK (k a) c))
(just plumbing)
Just like before, we’re going to suspend bind in a function by partially applying it. We can lower back into the target monad by directing that bind pipeline into a return. Note that this is the only return in our resulting expression. The expression will be in monadic normal form. I’ll flesh out the types now, but don’t stress. It’s just the right-hand side of a bind. Just like we had a Monoid for AppendK, and a Functor for MapK, we can build a Monad instance for BindK. Wrapping an interface in functions produces a thing that’s slightly more general than that interface. Don’t worry about the monad instance too much either. If you work it out in GHCi it’s quite intuitive, but less so on a slide. Basically, we have three bind chains, and we just plumb them together using function application. It looks a lot like inlined compose.