patternMinor
Intuition for “run” function of monads
Viewed 0 times
monadsfunctionforintuitionrun
Problem
I'm learning about monads, I understood why they are useful, I understood in general what bind, join, return do.
I also looked at basic usage examples for the basic reader / writer / state / list / maybe monads.
Still, being a beginner, I still don't feel I have a grip on what the "run" function means in general. It does not seem to have a generic signature like the above functions, and I don't get it if it's definable / makes sense for all monads.
I also looked at basic usage examples for the basic reader / writer / state / list / maybe monads.
Still, being a beginner, I still don't feel I have a grip on what the "run" function means in general. It does not seem to have a generic signature like the above functions, and I don't get it if it's definable / makes sense for all monads.
Solution
Short answer:
There isn't a run-function for all monads, and its use is evaluating data structures.
Medium answer:
Certain monads build data structures that will be evaluated afterwards. Let's say we want to build our own IO monad (I'll need to use GADT notation, basically I just talk about inhabitants and their specific types):
Like this, we can build a few IO actions, like one that just reads a string and prints it:
However, our monad doesn't do anything: We don't know how to execute those actions, so we need some kind of conversion function:
This is some kind of run function, it uses the monad-made structure to make use of it. It definitely isn't unique though: You might as well write a run function
There isn't a run-function for all monads, and its use is evaluating data structures.
Medium answer:
Certain monads build data structures that will be evaluated afterwards. Let's say we want to build our own IO monad (I'll need to use GADT notation, basically I just talk about inhabitants and their specific types):
data SimpleIO a where
PutStr :: String -> SimpleIO ()
GetLine :: SimpleIO String
Bind :: SimpleIO a -> (a -> SimpleIO b) -> SimpleIO b
Return :: a -> SimpleIO aLike this, we can build a few IO actions, like one that just reads a string and prints it:
Bind GetLine PutStrdoes exactly that.However, our monad doesn't do anything: We don't know how to execute those actions, so we need some kind of conversion function:
runSimpleIO :: SimpleIO a -> IO a
runSimpleIO (PutStr x) = putStrLn x
runSimpleIO GetLine = getLine
runSimpleIO (Bind x f) = x >>= f
runSimpleIO (Return x) = return xThis is some kind of run function, it uses the monad-made structure to make use of it. It definitely isn't unique though: You might as well write a run function
SimpleIO a -> Maybe a. That's nothing defined within the monad, but more of an interpretation.Code Snippets
data SimpleIO a where
PutStr :: String -> SimpleIO ()
GetLine :: SimpleIO String
Bind :: SimpleIO a -> (a -> SimpleIO b) -> SimpleIO b
Return :: a -> SimpleIO arunSimpleIO :: SimpleIO a -> IO a
runSimpleIO (PutStr x) = putStrLn x
runSimpleIO GetLine = getLine
runSimpleIO (Bind x f) = x >>= f
runSimpleIO (Return x) = return xContext
StackExchange Computer Science Q#103885, answer score: 2
Revisions (0)
No revisions yet.