Suppose I want to create a wrapping Monad that takes an IO Action and wraps it as follows. The sequence operator (>>) works just fine, but I'm having a hard time implementing return
and >>=
.
I tried return x = DelayedAction (return x)
but that doesn't result in the correct type.
newtype DelayedAction a = DelayedAction {action :: IO a}
instance Functor DelayedAction where
fmap = liftM
instance Applicative DelayedAction where
pure = return
(<*>) = ap
instance Monad DelayedAction where
return x = undefined
(DelayedAction firstIO) >>= f = undefined
(DelayedAction firstIO) >> (DelayedAction secondIO) =
DelayedAction
( do
firstIO
threadDelay 1000000
secondIO
)