Last active
July 5, 2016 06:33
-
-
Save jooyunghan/f51eb9267da8af990b013188c839dbad to your computer and use it in GitHub Desktop.
Fetch monad with runFetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ApplicativeDo #-} | |
import Control.Monad | |
data Fetch a = Done a | Blocked (Fetch a) | |
instance Functor Fetch where | |
fmap = liftM | |
instance Applicative Fetch where | |
pure = Done | |
-- (<*>) = ap | |
Done a <*> Done b = Done (a b) | |
Done a <*> Blocked b = Blocked (a <$> b) | |
Blocked a <*> Done b = Blocked (a <*> Done b) | |
Blocked a <*> Blocked b = Blocked (a <*> b) | |
instance Monad Fetch where | |
return = pure | |
Done a >>= f = f a | |
Blocked c >>= f = Blocked (c >>= f) | |
runFetch :: Fetch a -> IO a | |
runFetch f = case f of | |
Done a -> return a | |
Blocked c -> putStrLn "Blocked" >> runFetch c | |
prog = do --Blocked (Done (+1)) <*> Blocked (Done 1) | |
f <- Blocked (Done (+1)) | |
x <- Blocked (Done 1) | |
return $ f x | |
main = do | |
a <- runFetch prog | |
print a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment