Most of the monad explanations use examples where the monad wraps a value. E.g. Maybe a
, where the a
type variable is what's wrapped. But I'm wondering about monads that never wrap anything.
For a contrived example, suppose I have a real-world robot that can be controlled, but has no sensors. Maybe I'd like to control it like this:
robotMovementScript :: RobotMonad ()
robotMovementScript = do
moveLeft 10
moveForward 25
rotate 180
main :: IO ()
main =
liftIO $ runRobot robotMovementScript connectToRobot
In our imaginary API, connectToRobot
returns some kind of handle to the physical device. This connection becomes the "context" of the RobotMonad
. Because our connection to the robot can never send a value back to us, the monad's concrete type is always RobotMonad ()
.
Some questions:
- Does my contrived example seem right?
- Am I understanding the idea of a monad's "context" correctly? Am I correct to describe the robot's connection as the context?
- Does it make sense to have a monad--such as
RobotMonad
--that never wraps a value? Or is this contrary to the basic concept of monads? - Are monoids a better fit for this kind of application? I can imagine concatenating robot control actions with
<>
. Thoughdo
notation seems more readable. - In the monad's definition, would/could there be something that ensures the type is always
RobotMonad ()
?
I've looked at Data.Binary.Put
as an example. It appears to be similar (or maybe identical?) to what I'm thinking of. But it also involves the Writer monad and the Builder monoid. Considering those added wrinkles and my current skill level, I think the Put
monad might not be the most instructive example.
Edit
I don't actually need to build a robot or an API like this. The example is completely contrived. I just needed an example where there would never be a reason to pull a value out of the monad. So I'm not asking for the easiest way to solve the robot problem. Rather, this thought experiment about monads without inner values is an attempt to better understand monads generally.