Commit 5b06ce2e authored by Michael Hanus's avatar Michael Hanus
Browse files

transformers package restricted to work with PAKCS2

parent 065ac203
Copyright (c) 2017, <AUTHOR NAME>
Copyright (c) 2020, Kai-Oliver Prott
All rights reserved.
Redistribution and use in source and binary forms, with or without
......
{
"name": "transformers",
"version": "3.0.0",
"version": "2.0.0",
"author": "Kai-Oliver Prott <stu126209@mail.uni-kiel.de>",
"synopsis": "A portable library of functor and monad transformers",
"category": [ "Control" ],
......@@ -8,17 +8,18 @@
"license": "BSD-3-Clause",
"licenseFile": "LICENSE",
"dependencies": {
"base" : ">= 3.0.0, < 4.0.0"
"base" : ">= 1.0.0, < 2.0.0"
},
"compilerCompatibility": {
"pakcs": ">= 3.0.0, < 4.0.0",
"kics2": ">= 3.0.0, < 4.0.0"
"pakcs": ">= 2.0.0, < 3.0.0",
"kics2": ">= 2.0.0, < 3.0.0"
},
"exportedModules": [
"Data.Functor.Identity",
"Control.Monad.IO.Class",
"Control.Monad.Trans.Class",
"Control.Monad.Trans.Cont",
"Control.Monad.Trans.Exept",
"Control.Monad.Trans.Except",
"Control.Monad.Trans.Maybe",
"Control.Monad.Trans.State" ],
"source": {
......
......@@ -18,24 +18,24 @@ withContT f m = ContT $ runContT m . f
instance Functor (ContT r m) where
fmap f m = ContT $ \ c -> runContT m (c . f)
instance Applicative (ContT r m) where
pure x = ContT ($ x)
f <*> v = ContT $ \ c -> runContT f $ \ g -> runContT v (c . g)
-- instance Applicative (ContT r m) where
-- pure x = ContT ($ x)
--
-- f <*> v = ContT $ \ c -> runContT f $ \ g -> runContT v (c . g)
instance Monad (ContT r m) where
return x = ContT ($ x)
m >>= k = ContT $ \ c -> runContT m (\ x -> runContT (k x) c)
instance MonadFail m => MonadFail (ContT r m) where
fail msg = ContT $ \ _ -> fail msg
-- instance MonadFail m => MonadFail (ContT r m) where
-- fail msg = ContT $ \ _ -> fail msg
instance MonadTrans (ContT r) where
lift m = ContT (m >>=)
instance (MonadIO m) => MonadIO (ContT r m) where
liftIO = lift . liftIO
liftIO = lift . Control.Monad.IO.Class.liftIO
resetT :: Monad m => ContT r m r -> ContT r' m r
resetT = lift . evalContT
......
......@@ -8,28 +8,28 @@ newtype ExceptT e m a = ExceptT {
runExceptT :: m (Either e a)
}
instance Monad m => Functor (ExceptT e m) where
instance Functor m => Functor (ExceptT e m) where
fmap f = ExceptT . fmap (either Left (Right . f)) . runExceptT
instance Monad m => Applicative (ExceptT e m) where
pure a = ExceptT $ return (Right a)
ExceptT f <*> ExceptT v = ExceptT $ do
mf <- f
case mf of
Left e -> return (Left e)
Right k -> do
mv <- v
case mv of
Left e -> return (Left e)
Right x -> return (Right (k x))
instance (Monad m, Monoid e) => Alternative (ExceptT e m) where
empty = ExceptT $ return (Left mempty)
ExceptT mx <|> ExceptT my = ExceptT $ do
ex <- mx
case ex of
Left e -> fmap (either (Left . mappend e) Right) my
Right x -> return (Right x)
-- instance Monad m => Applicative (ExceptT e m) where
-- pure a = ExceptT $ return (Right a)
-- ExceptT f <*> ExceptT v = ExceptT $ do
-- mf <- f
-- case mf of
-- Left e -> return (Left e)
-- Right k -> do
-- mv <- v
-- case mv of
-- Left e -> return (Left e)
-- Right x -> return (Right (k x))
-- instance (Monad m, Monoid e) => Alternative (ExceptT e m) where
-- empty = ExceptT $ return (Left mempty)
-- ExceptT mx <|> ExceptT my = ExceptT $ do
-- ex <- mx
-- case ex of
-- Left e -> fmap (either (Left . mappend e) Right) my
-- Right x -> return (Right x)
instance (Monad m) => Monad (ExceptT e m) where
return a = ExceptT $ return (Right a)
......@@ -40,21 +40,21 @@ instance (Monad m) => Monad (ExceptT e m) where
Left e -> return (Left e)
Right x -> runExceptT (k x)
instance MonadFail m => MonadFail (ExceptT e m) where
fail = ExceptT . fail
-- instance MonadFail m => MonadFail (ExceptT e m) where
-- fail = ExceptT . fail
instance MonadTrans (ExceptT e) where
lift m = ExceptT (fmap Right m)
-- instance MonadTrans (ExceptT e) where
-- lift m = ExceptT (fmap Right m)
instance MonadIO m => MonadIO (ExceptT e m) where
liftIO = lift . liftIO
-- instance MonadIO m => MonadIO (ExceptT e m) where
-- liftIO = lift . liftIO
mapExceptT :: (m (Either e a) -> n (Either e' b))
-> ExceptT e m a
-> ExceptT e' n b
mapExceptT f m = ExceptT $ f (runExceptT m)
withExceptT :: (Monad m) => (e -> e') -> ExceptT e m a -> ExceptT e' m a
withExceptT :: Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a
withExceptT f = mapExceptT $ fmap $ either (Left . f) Right
type Except e = ExceptT e Identity
......
......@@ -7,38 +7,42 @@ newtype MaybeT m a = MaybeT {
runMaybeT :: m (Maybe a)
}
instance Monad m => Functor (MaybeT m) where
fmap f m = MaybeT (fmap (fmap f) (runMaybeT m))
instance Monad m => Applicative (MaybeT m) where
pure x = MaybeT (return (Just x))
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just x) = Just (f x)
mf <*> m = MaybeT $ do f <- runMaybeT mf
v <- runMaybeT m
return (f <*> v)
instance Monad m => Alternative (MaybeT m) where
empty = MaybeT (return Nothing)
instance Functor m => Functor (MaybeT m) where
fmap f m = MaybeT (fmap (fmap f) (runMaybeT m))
x <|> y = MaybeT $ do v <- runMaybeT x
case v of
Nothing -> runMaybeT y
Just _ -> return v
-- instance Monad m => Applicative (MaybeT m) where
-- pure x = MaybeT (return (Just x))
--
-- mf <*> m = MaybeT $ do f <- runMaybeT mf
-- v <- runMaybeT m
-- return (f <*> v)
-- instance Monad m => Alternative (MaybeT m) where
-- empty = MaybeT (return Nothing)
--
-- x <|> y = MaybeT $ do v <- runMaybeT x
-- case v of
-- Nothing -> runMaybeT y
-- Just _ -> return v
instance Monad m => Monad (MaybeT m) where
return = pure
return x = MaybeT (return (Just x)) -- pure
m >>= f = MaybeT $ do v <- runMaybeT m
maybe (return Nothing) (runMaybeT . f) v
instance MonadFail m => MonadFail (MaybeT m) where
fail _ = MaybeT (return Nothing)
-- instance MonadFail m => MonadFail (MaybeT m) where
-- fail _ = MaybeT (return Nothing)
instance MonadTrans MaybeT where
lift m = MaybeT (fmap Just m)
-- instance MonadTrans MaybeT where
-- lift m = MaybeT (fmap Just m)
instance MonadIO m => MonadIO (MaybeT m) where
liftIO = lift . liftIO
-- instance MonadIO m => MonadIO (MaybeT m) where
-- liftIO = lift . Control.Monad.IO.Class.liftIO
mapMaybeT :: (m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT f = MaybeT . f . runMaybeT
......@@ -12,23 +12,23 @@ instance Monad m => Functor (StateT s m) where
fmap f m = StateT $ \ s -> do (v, s') <- runStateT m s
return (f v, s')
instance Monad m => Applicative (StateT s m) where
pure x = StateT $ \ s -> return (x, s)
-- instance Monad m => Applicative (StateT s m) where
-- pure x = StateT $ \ s -> return (x, s)
instance Monad m => Monad (StateT s m) where
return = pure
m >>= fm = StateT $ \ s -> do (v ,s') <- runStateT m s
return a = StateT $ \ s -> return (a, s) -- pure
m >>= fm = StateT $ \ s -> do (v, s') <- runStateT m s
runStateT (fm v) s'
instance MonadFail m => MonadFail (StateT s m) where
fail msg = StateT $ \ _ -> fail msg
-- instance MonadFail m => MonadFail (StateT s m) where
-- fail msg = StateT $ \ _ -> fail msg
instance MonadTrans (StateT s) where
lift m = StateT $ \ s -> do v <- m
return (v, s)
instance MonadIO m => MonadIO (StateT s m) where
liftIO = lift . liftIO
liftIO = lift . Control.Monad.IO.Class.liftIO
get :: Monad m => StateT s m s
get = StateT $ \s -> return (s, s)
......@@ -45,10 +45,10 @@ modify f = StateT $ \s -> return ((), f s)
gets :: Monad m => (s -> a) -> StateT s m a
gets f = StateT $ \ s -> return (f s, s)
evalStateT :: Monad m => StateT s m a -> s -> m a
evalStateT :: Functor m => StateT s m a -> s -> m a
evalStateT m = fmap fst . runStateT m
execStateT :: Monad m => StateT s m a -> s -> m s
execStateT :: Functor m => StateT s m a -> s -> m s
execStateT m = fmap snd . runStateT m
mapStateT :: (Monad m, Monad n)
......
module Data.Functor.Identity where
newtype Identity a = Identity { runIdentity :: a }
deriving (Eq, Ord, Read, 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)
instance Monad Identity where
m >>= k = k (runIdentity m)
return a = Identity a
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment