diff --git a/exampleCode/lecture3/MaybeIO.hs b/exampleCode/lecture3/MaybeIO.hs new file mode 100644 index 0000000..136f090 --- /dev/null +++ b/exampleCode/lecture3/MaybeIO.hs @@ -0,0 +1,24 @@ +module MaybeIO where + +import Control.Applicative + +data MaybeIO a = MaybeIO { runMaybeIO :: IO (Maybe a) } + +instance Functor MaybeIO where + fmap f = MaybeIO . fmap (fmap f) . runMaybeIO + +instance Applicative MaybeIO where + pure = MaybeIO . pure . pure + f <*> x = MaybeIO $ (<*>) <$> f' <*> x' + where + f' = runMaybeIO f + x' = runMaybeIO x + +instance Monad MaybeIO where + return = pure + x >>= f = MaybeIO $ x' >>= runMaybeIO . mb . fmap f + where + x' = runMaybeIO x + mb :: Maybe (MaybeIO a) -> MaybeIO a + mb (Just a) = a + mb Nothing = MaybeIO $ return Nothing diff --git a/lecture3.pdf b/lecture3.pdf new file mode 100644 index 0000000..84abe68 Binary files /dev/null and b/lecture3.pdf differ