diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
| commit | 98329e0a3dd4f78b5d815ac3896272ec70904901 (patch) | |
| tree | 80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/26/MaybeT/src | |
| parent | 3624c712d72d246f21d4e710cec7c11e052e0326 (diff) | |
| download | book-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz | |
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/26/MaybeT/src')
| -rw-r--r-- | Haskell-book/26/MaybeT/src/Either.hs | 56 | ||||
| -rw-r--r-- | Haskell-book/26/MaybeT/src/Identity.hs | 43 | ||||
| -rw-r--r-- | Haskell-book/26/MaybeT/src/Maybe.hs | 40 | ||||
| -rw-r--r-- | Haskell-book/26/MaybeT/src/MonadIO.hs | 5 | ||||
| -rw-r--r-- | Haskell-book/26/MaybeT/src/MonadTrans.hs | 7 | ||||
| -rw-r--r-- | Haskell-book/26/MaybeT/src/Reader.hs | 35 | ||||
| -rw-r--r-- | Haskell-book/26/MaybeT/src/State.hs | 41 |
7 files changed, 227 insertions, 0 deletions
diff --git a/Haskell-book/26/MaybeT/src/Either.hs b/Haskell-book/26/MaybeT/src/Either.hs new file mode 100644 index 0000000..e09bfe6 --- /dev/null +++ b/Haskell-book/26/MaybeT/src/Either.hs @@ -0,0 +1,56 @@ +module Either where + +import Control.Monad (liftM) +import MonadTrans +import MonadIO + +newtype EitherT e m a = + EitherT { runEitherT :: m (Either e a) } + +-- 1 +instance Functor m => Functor (EitherT e m) where + fmap f (EitherT x) = EitherT $ (fmap . fmap) f x + +-- 2 +instance Applicative m => Applicative (EitherT e m) where + pure x = EitherT $ pure $ pure x + + (EitherT f) <*> (EitherT a) = EitherT $ (<*>) <$> f <*> a + +-- 3 +instance Monad m => Monad (EitherT e m) where + return = pure + + (EitherT em) >>= f = EitherT $ do + v <- em + case v of + Left y -> return $ Left y + Right y -> runEitherT (f y) + + +-- 4 +-- transformer version of swapEither. +-- Hint: write swapEither first, then swapEitherT in terms of the former. +swapEither :: Either e a -> Either a e +swapEither (Left x) = Right x +swapEither (Right y) = Left y + +swapEitherT :: (Functor m) + => EitherT e m a + -> EitherT a m e +swapEitherT (EitherT x) = EitherT $ fmap swapEither x + +-- 5. Write the transformer variant of the either catamorphism. +eitherT :: Monad m + => (a -> m c) + -> (b -> m c) + -> EitherT a m b + -> m c +eitherT f g (EitherT x) = x >>= (either f g) + +instance MonadTrans (EitherT e) where + lift = EitherT . liftM Right + +instance (MonadIO m) + => MonadIO (EitherT e m) where + liftIO = lift . liftIO diff --git a/Haskell-book/26/MaybeT/src/Identity.hs b/Haskell-book/26/MaybeT/src/Identity.hs new file mode 100644 index 0000000..8189c18 --- /dev/null +++ b/Haskell-book/26/MaybeT/src/Identity.hs @@ -0,0 +1,43 @@ +module Identity where + +import MonadIO +import MonadTrans + +newtype Identity a = + Identity { runIdentity :: a } + deriving (Eq, Show) + +instance Functor Identity where + fmap f (Identity a) = Identity (f a) + +instance Applicative Identity where + pure = Identity + (Identity f) <*> (Identity a) = Identity (f a) + +newtype IdentityT f a = + IdentityT { runIdentityT :: f a } + deriving (Eq, Show) + +instance (Functor m) + => Functor (IdentityT m) where + fmap f (IdentityT fa) = IdentityT (fmap f fa) + +instance (Applicative m) + => Applicative (IdentityT m) where + pure x = IdentityT (pure x) + + (IdentityT fab) <*> (IdentityT fa) = + IdentityT (fab <*> fa) + +instance (Monad m) + => Monad (IdentityT m) where + return = pure + + (IdentityT ma) >>= f = IdentityT $ ma >>= runIdentityT . f + +instance (MonadIO m) + => MonadIO (IdentityT m) where + liftIO = IdentityT . liftIO + +instance MonadTrans IdentityT where + lift = IdentityT diff --git a/Haskell-book/26/MaybeT/src/Maybe.hs b/Haskell-book/26/MaybeT/src/Maybe.hs new file mode 100644 index 0000000..4d6c9b7 --- /dev/null +++ b/Haskell-book/26/MaybeT/src/Maybe.hs @@ -0,0 +1,40 @@ +module Maybe where + +import Control.Monad +import MonadIO +import MonadTrans + +newtype MaybeT m a = + MaybeT { runMaybeT :: m (Maybe a) } + +-- compare to the instance for MaybeT +instance (Functor m) + => Functor (MaybeT m) where + fmap f (MaybeT ma) = + MaybeT $ (fmap . fmap) f ma + +instance (Applicative m) + => Applicative (MaybeT m) where + pure x = MaybeT (pure (pure x)) + + (MaybeT fab) <*> (MaybeT mma) = MaybeT $ (<*>) <$> fab <*> mma + +instance (Monad m) + => Monad (MaybeT m) where + return = pure + + -- (>>=) :: MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b + (MaybeT ma) >>= f = MaybeT $ do + -- ma :: m (Maybe a) + -- v :: Maybe a + v <- ma + case v of + Nothing -> return Nothing + Just y -> runMaybeT (f y) + +instance MonadTrans MaybeT where + lift = MaybeT . liftM Just + +instance (MonadIO m) + => MonadIO (MaybeT m) where + liftIO = lift . liftIO diff --git a/Haskell-book/26/MaybeT/src/MonadIO.hs b/Haskell-book/26/MaybeT/src/MonadIO.hs new file mode 100644 index 0000000..93244dd --- /dev/null +++ b/Haskell-book/26/MaybeT/src/MonadIO.hs @@ -0,0 +1,5 @@ +module MonadIO where + +class (Monad m) => MonadIO m where + -- | Lift a computation from the 'IO' monad. + liftIO :: IO a -> m a diff --git a/Haskell-book/26/MaybeT/src/MonadTrans.hs b/Haskell-book/26/MaybeT/src/MonadTrans.hs new file mode 100644 index 0000000..7954164 --- /dev/null +++ b/Haskell-book/26/MaybeT/src/MonadTrans.hs @@ -0,0 +1,7 @@ +module MonadTrans where + +class MonadTrans t where + -- | Lift a computation from + -- the argument monad to + -- the constructed monad. + lift :: (Monad m) => m a -> t m a diff --git a/Haskell-book/26/MaybeT/src/Reader.hs b/Haskell-book/26/MaybeT/src/Reader.hs new file mode 100644 index 0000000..38fded9 --- /dev/null +++ b/Haskell-book/26/MaybeT/src/Reader.hs @@ -0,0 +1,35 @@ +module Reader where + +import MonadIO +import MonadTrans + +newtype ReaderT r m a = + ReaderT { runReaderT :: r -> m a } + +instance (Functor m) + => Functor (ReaderT r m) where + fmap f (ReaderT rma) = + ReaderT $ (fmap . fmap) f rma + +instance (Applicative m) + => Applicative (ReaderT r m) where + pure a = ReaderT (pure (pure a)) + + (ReaderT fmab) <*> (ReaderT rma) = + ReaderT $ (<*>) <$> fmab <*> rma + +instance (Monad m) + => Monad (ReaderT r m) where + return = pure + + (ReaderT rma) >>= f = + ReaderT $ \r -> do + a <- rma r + runReaderT (f a) r + +instance MonadTrans (ReaderT r) where + lift = ReaderT . const + +instance (MonadIO m) + => MonadIO (ReaderT r m) where + liftIO = lift . liftIO diff --git a/Haskell-book/26/MaybeT/src/State.hs b/Haskell-book/26/MaybeT/src/State.hs new file mode 100644 index 0000000..d30fdd5 --- /dev/null +++ b/Haskell-book/26/MaybeT/src/State.hs @@ -0,0 +1,41 @@ +module State where + +import MonadIO +import MonadTrans + +newtype StateT s m a = + StateT { runStateT :: s -> m (a, s) } + +-- 1 +instance (Functor m) + => Functor (StateT s m) where + fmap f (StateT m) = StateT $ \s -> fmap first $ m s + where first = uncurry (\t1 t2 -> ((f t1), t2)) + +-- 2 +-- Links: +-- http://stackoverflow.com/questions/18673525/is-it-possible-to-implement-applicative-m-applicative-statet-s-m +-- https://github.com/NICTA/course/issues/134 +instance (Monad m) + => Applicative (StateT s m) where + pure x = StateT $ (\s -> pure (x, s)) + StateT g <*> StateT h = StateT $ \s -> keepFirst <$> g s <*> h s + where keepFirst (f, s') (x, _) = (f x, s') + + +-- 3 +instance (Monad m) + => Monad (StateT s m) where + return = pure + + (StateT sma) >>= f = + StateT $ \s -> do + a <- sma s + runStateT (f $ fst a) s + +instance MonadTrans (StateT s) where + lift c = StateT $ \s -> c >>= (\x -> return (x, s)) + +instance (MonadIO m) + => MonadIO (StateT s m) where + liftIO = lift . liftIO |
