2 ms·
Monads are (abstractions of) container types that allow you to map a function over the elements of the container, but return not just a new element but a entire
by a1369209993 8y ago
Monads are (abstractions of) container types that allow you to map a function over the elements of the container, but return not just a new element but a entire new container.
Just x => f x
[x0,x1,...] => f x1 ++ f x2 ++ ...
returnIO x => f x
What makes "magic monads" like IO useful is that you can feed internal state through them.
thenIO (IO m) k = IO (\ s -> case m s of (# new_s, _ #) -> unIO k new_s)
thenIO a b => c # forces a to be evaluated first to generate new_s
# because b will refuse to preform any IO operations until it recieves new_s
# this sequence is packaged up as c, which can be passed to other thenIO's
# until it reaches a unsafePerformIO or the implicit not-so-unsafePerformIO underneath main
The 'state' (s,new_s,etc) of a IO object is just a placeholder to force in-order evaluation, but you could also store eg a Map of key-value pairs for a State monad.
(Note that this comes from someone who considers it a compilier bug if `* (char *)0 = -1` doesn't produce `mov byte[0], 0xFF`, so take the implementation details with a grain of salt.)