Monads in Minecraft
"A monad is just a monoid in the category of endofunctors""A monad is a burrito"
Metaphors concocted by the deranged minds of those who write in Haskell.
So, let's look at Minecraft:
::
->
![]()
We have a function: a furnace
which is of the type (::) of turning
raw beef
into (->) steak
.If this, and functions with more arguments, are not intuitive to you, read up on Haskell right now.
class Functor f where fmap :: (a -> b) -> f a -> f b (<$) :: a -> f b -> f a
This is the functor, you can make one by implementing fmap and <$ and mentioning it's an instance of the class, the simplest of the hierarchy of functors, applicatives and monads. Functors let you apply a function to a value in a context, but what does that mean? A context "is a burrito" but what is that.
class Functorwhere fmap ::
->
![]()
![]()
->
![]()
![]()
(<$) ::
->
![]()
![]()
->
![]()
![]()
![]()
I see, that's why it's said to be a burrito with filling.. A context
wraps a value
and just provides some abstraction.Let's put our item in a chest then, but, problem, our function
is
not a function that takes a
but one that takes a
. We need to use 'fmap' to take it out
of a chest, apply
and put it back
in one.'fmap' is the autosmelter. Oh also (<$) exists and replaces an item.
class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b
An applicative expands on the functor, it allows lifting a value into a context and applying a function from a context.
class Functor=> Applicative
where pure ::
->
![]()
![]()
(<*>) ::
![]()
![]()
->
![]()
![]()
->
![]()
![]()
![]()
Ah, so what that actually means is that we can finally put items
in
a chest
.We also learned what to do if our furnace
is in a chest
so we don't need to panic anymore but can just place it down for a working autosmelter, as long
as everything is wrapped in a context
in the end.class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a
Time for the monad, it uses everything so far and covers two more cases.
Also lifting is now called 'return' instead of 'pure' because the lazy Haskellers haven't made it an alias yet.
class Applicative=> Monad
where (>>=) ::
![]()
![]()
->
![]()
->
![]()
![]()
(>>) ::
![]()
![]()
->
![]()
![]()
->
![]()
![]()
return ::
->
![]()
![]()
![]()
If we have a function that already puts an item in a chest by itself
::
->

Then we cannot use 'fmap' which also likes putting it in a chest, and so we have >>=.
::
->

With >> we can replace an item like we could with (<$), but now with our
in a chest too.