Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions daml/Daml/Control/Monad/Trans/Except.daml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import Daml.Control.Monad.Signatures (Listen, Pass)
import Daml.Control.Monad.Trans.Class
-- import Data.Functor.Classes
-- #if MIN_VERSION_base(4,12,0)
-- import Data.Functor.Contravariant
import Daml.Data.Functor.Contravariant (Contravariant, contramap)
-- #endif
import Daml.Data.Functor.Identity

Expand Down Expand Up @@ -262,8 +262,7 @@ instance (Action m) => Action (ExceptT e m) where
-- {-# INLINE mfix #-}

instance MonadTrans (ExceptT e) where
-- lift = ExceptT . liftM Right
lift mx = ExceptT $ Right <$> mx
lift = ExceptT . fmap Right
{-# INLINE lift #-}

-- instance (MonadIO m) => MonadIO (ExceptT e m) where
Expand All @@ -277,9 +276,9 @@ instance MonadTrans (ExceptT e) where
-- #endif

-- #if MIN_VERSION_base(4,12,0)
-- instance Contravariant m => Contravariant (ExceptT e m) where
-- contramap f = ExceptT . contramap (fmap f) . runExceptT
-- {-# INLINE contramap #-}
instance Contravariant m => Contravariant (ExceptT e m) where
contramap f = ExceptT . contramap (fmap f) . runExceptT
{-# INLINE contramap #-}
-- #endif

-- | Signal an exception value @e@.
Expand Down
20 changes: 9 additions & 11 deletions daml/Daml/Control/Monad/Trans/Free.daml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ import DA.Bifunctor
import DA.Foldable
import Prelude hiding (mapA, mapM)

liftM : Monad m => (a -> r) -> m a -> m r
liftM = fmap

-- | The base functor for a free monad.
data FreeF f a b = Pure a | Free (f b)
Expand Down Expand Up @@ -315,7 +313,7 @@ instance (Functor f, Read1 f, Functor m, Read1 m, Read a) => Read (FreeT f m a)
-}

instance (Functor f, Monad m) => Functor (FreeT f m) where
fmap f (FreeT m) = FreeT (liftM f' m) where
fmap f (FreeT m) = FreeT (fmap f' m) where
f' (Pure a) = Pure (f a)
f' (Free as) = Free (fmap (fmap f) as)

Expand Down Expand Up @@ -348,7 +346,7 @@ instance (Functor f, ActionFail m) => ActionFail (FreeT f m) where
fail e = FreeT (fail e)

instance MonadTrans (FreeT f) where
lift = FreeT . liftM Pure
lift = FreeT . fmap Pure
{-# INLINE lift #-}

{-
Expand All @@ -370,21 +368,21 @@ instance (Functor f, MonadReader r m) => MonadReader r (FreeT f m) where
instance (Functor f, MonadWriter w m) => MonadWriter w (FreeT f m) where
tell = lift . tell
{-# INLINE tell #-}
listen (FreeT m) = FreeT $ liftM concat' $ listen (fmap listen `liftM` m)
listen (FreeT m) = FreeT $ fmap concat' $ listen (fmap listen `fmap` m)
where
concat' (Pure x, w) = Pure (x, w)
concat' (Free y, w) = Free $ fmap (second (w <>)) <$> y
pass m = FreeT . pass' . runFreeT . hoistFreeT clean $ listen m
where
clean = pass . liftM (\x -> (x, const mempty))
clean = pass . fmap (\x -> (x, const mempty))
-- #if MIN_VERSION_mtl(2,1,1)
writer w = lift (writer w)
{-# INLINE writer #-}
-- #endif

-- Recursive helper fns for MonadWriter Instance
pass' : (Functor f, MonadWriter w m) => m (FreeF f ((a, w -> w), w) (FreeT f m ((a, w -> w), w))) -> m (FreeF f a (FreeT f m a))
pass' = join . liftM g
pass' = join . fmap g
g (Pure ((x, f), w)) = tell (f w) >> return (Pure x)
g (Free f) = return . Free . fmap (FreeT . pass' . runFreeT) $ f

Expand Down Expand Up @@ -464,7 +462,7 @@ instance (Monad m, Traversable m, Traversable f) => Traversable (FreeT f m) wher
--
-- @'hoistFreeT' : ('Monad' m, 'Functor' f) => (m ~> n) -> 'FreeT' f m ~> 'FreeT' f n@
hoistFreeT : (Monad m, Functor f) => (forall a. m a -> n a) -> FreeT f m b -> FreeT f n b
hoistFreeT mh = FreeT . mh . liftM (fmap (hoistFreeT mh)) . runFreeT
hoistFreeT mh = FreeT . mh . fmap (fmap (hoistFreeT mh)) . runFreeT

-- | The very definition of a free monad transformer is that given a natural
-- transformation you get a monad transformer homomorphism.
Expand All @@ -477,14 +475,14 @@ foldFreeT f (FreeT m) = lift m >>= foldFreeF

-- | Lift a natural transformation from @f@ to @g@ into a monad homomorphism from @'FreeT' f m@ to @'FreeT' g m@
transFreeT : (Monad m, Functor g) => (forall a. f a -> g a) -> FreeT f m b -> FreeT g m b
transFreeT nt = FreeT . liftM (fmap (transFreeT nt) . transFreeF nt) . runFreeT
transFreeT nt = FreeT . fmap (fmap (transFreeT nt) . transFreeF nt) . runFreeT

-- | Pull out and join @m@ layers of @'FreeT' f m a@.
joinFreeT : (Monad m, Traversable f) => FreeT f m a -> m (Free f a)
joinFreeT (FreeT m) = m >>= joinFreeF
where
joinFreeF (Pure x) = return (return x)
joinFreeF (Free f) = wrap `liftM` mapA joinFreeT f
joinFreeF (Free f) = wrap `fmap` mapA joinFreeT f

-- |
-- 'retract' is the left inverse of 'liftF'
Expand Down Expand Up @@ -523,7 +521,7 @@ iterM phi = iterT phi . hoistFreeT (return . runIdentity)
-- steps in the iteration is terminating.
cutoff : (Functor f, Monad m) => Integer -> FreeT f m a -> FreeT f m (Maybe a)
cutoff n _ | n <= 0 = return Nothing
cutoff n (FreeT m) = FreeT $ bimap Just (cutoff (n - 1)) `liftM` m
cutoff n (FreeT m) = FreeT $ bimap Just (cutoff (n - 1)) `fmap` m

-- | @partialIterT n phi m@ interprets first @n@ layers of @m@ using @phi@.
-- This is sort of the opposite for @'cutoff'@.
Expand Down
7 changes: 4 additions & 3 deletions daml/Daml/Control/Monad/Trans/Reader.daml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Daml.Control.Monad.Trans.Reader (

import Daml.Control.Monad.Trans.Class (MonadTrans(..))
import Daml.Data.Functor.Identity
import Daml.Data.Functor.Contravariant (Contravariant, contramap)

-- | The parameterizable reader monad.
--
Expand Down Expand Up @@ -171,13 +172,13 @@ instance (MonadZip m) => MonadZip (ReaderT r m) where
mzipWith f (m a) (n a)
{-# INLINE mzipWith #-}
#endif
-}

#if MIN_VERSION_base(4,12,0)
-- #if MIN_VERSION_base(4,12,0)
instance Contravariant m => Contravariant (ReaderT r m) where
contramap f = ReaderT . fmap (contramap f) . runReaderT
{-# INLINE contramap #-}
#endif
-}
-- #endif

liftReaderT : m a -> ReaderT r m a
liftReaderT m = ReaderT (const m)
Expand Down
10 changes: 5 additions & 5 deletions daml/Daml/Control/Monad/Trans/State.daml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module Daml.Control.Monad.Trans.State (
-- mport Control.Monad.Signatures
import Daml.Control.Monad.Trans.Class
-- #if MIN_VERSION_base(4,12,0)
-- import Data.Functor.Contravariant
import Daml.Data.Functor.Contravariant (Contravariant, contramap)
-- #endif
import Daml.Data.Functor.Identity

Expand Down Expand Up @@ -253,10 +253,10 @@ instance MonadTrans (StateT s) where
return (a, s)
{-# INLINE lift #-}
-- if MIN_VERSION_base(4,12,0)
-- nstance Contravariant m => Contravariant (StateT s m) where
-- contramap f m = StateT $ \s ->
-- contramap (\ (a, s') -> (f a, s')) $ runStateT m s
-- {-# INLINE contramap #-}
instance Contravariant m => Contravariant (StateT s m) where
contramap f m = StateT $ \s ->
contramap (\ (a, s') -> (f a, s')) $ runStateT m s
{-# INLINE contramap #-}
-- endif

instance ActionFail m => ActionFail (StateT s m) where
Expand Down
8 changes: 4 additions & 4 deletions daml/Daml/Control/Monad/Trans/Writer.daml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import Prelude hiding (mapA)
import Daml.Control.Monad.Trans.Class
-- mport Data.Functor.Classes
-- if MIN_VERSION_base(4,12,0)
-- mport Data.Functor.Contravariant
import Daml.Data.Functor.Contravariant (Contravariant, contramap)
-- endif
import Daml.Data.Functor.Identity
--
Expand Down Expand Up @@ -260,9 +260,9 @@ instance (Monoid w, CanAbort m) => CanAbort (WriterT w m) where
-- endif

-- if MIN_VERSION_base(4,12,0)
-- nstance Contravariant m => Contravariant (WriterT w m) where
-- contramap f = mapWriterT $ contramap $ \ (a, w) -> (f a, w)
-- {-# INLINE contramap #-}
instance Contravariant m => Contravariant (WriterT w m) where
contramap f = mapWriterT $ contramap $ \ (a, w) -> (f a, w)
{-# INLINE contramap #-}
-- endif

-- | @'tell' w@ is an action that produces the output @w@.
Expand Down
26 changes: 12 additions & 14 deletions daml/Daml/Data/Profunctor.daml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Daml.Data.Profunctor
{-import Control.Arrow -}
import Daml.Control.Category
import Daml.Control.Arrow (Kleisli(..))
import Daml.Control.Arrow.CoKleisli (CoKleisli(..))
{-import Control.Comonad (Cokleisli(..))
import Control.Monad (liftM)
import Data.Bifunctor.Biff (Biff(..))
Expand All @@ -42,11 +43,11 @@ import Data.Bifunctor.Tannen (Tannen(..))
import Data.Coerce (Coercible, coerce)
#if __GLASGOW_HASKELL__ < 710
import Data.Functor
#endif
import Data.Functor.Contravariant (Contravariant(..))
#endif
import Data.Tagged
-}
import Prelude hiding (id,(.))
import Prelude hiding (id, (.))

{-
infixr 9 #.
Expand Down Expand Up @@ -196,34 +197,31 @@ instance Profunctor Tagged where
{-# INLINE (.#) #-}
-}

liftM : Monad m => (a -> b) -> m a -> m b
liftM f ma = ma >>= pure . f

instance Monad m => Profunctor (Kleisli m) where
dimap f g (Kleisli h) = Kleisli (liftM g . h . f)
dimap f g (Kleisli h) = Kleisli (fmap g . h . f)
{-# INLINE dimap #-}
lmap k (Kleisli f) = Kleisli (f . k)
{-# INLINE lmap #-}
rmap k (Kleisli f) = Kleisli (liftM k . f)
rmap k (Kleisli f) = Kleisli (fmap k . f)
{-# INLINE rmap #-}
-- We cannot safely overload (#.) because we didn't provide the 'Monad'.
{-
(.#) pbc _ = coerce pbc
{-# INLINE (.#) #-}
-}

{-
instance Functor w => Profunctor (Cokleisli w) where
dimap f g (Cokleisli h) = Cokleisli (g . h . fmap f)
instance Functor w => Profunctor (CoKleisli w) where
dimap f g (CoKleisli h) = CoKleisli (g . h . fmap f)
{-# INLINE dimap #-}
lmap k (Cokleisli f) = Cokleisli (f . fmap k)
lmap k (CoKleisli f) = CoKleisli (f . fmap k)
{-# INLINE lmap #-}
rmap k (Cokleisli f) = Cokleisli (k . f)
rmap k (CoKleisli f) = CoKleisli (k . f)
{-# INLINE rmap #-}
-- We cannot safely overload (.#) because we didn't provide the 'Functor'.
(#.) _ = coerce (\x -> x : b) : forall a b. Coercible b a => a -> b
{-# INLINE (#.) #-}
-- (#.) _ = coerce (\x -> x : b) : forall a b. Coercible b a => a -> b
-- {-# INLINE (#.) #-}

{-
instance Contravariant f => Profunctor (Clown f) where
lmap f (Clown fa) = Clown (contramap f fa)
{-# INLINE lmap #-}
Expand Down