Skip to content

Commit

Permalink
Work more with applicative
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerdvisscher committed Dec 3, 2011
1 parent 8ca9f45 commit f1ae0ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/Control/Effects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,33 @@ instance Monad m => Monad (Base m) where
data Effect e (m :: * -> *) = Effect


class (Monad m, Monad n) => AutoLift e m n where
class (Applicative m, Applicative n, Monad m, Monad n) => AutoLift e m n where
operation' :: Effect e m -> ((a -> m e) -> m e) -> n a

instance (Monad m, Monad n, AutoLiftInternal (Layer e m) (Base n) (Layer e m) (Base n)) => AutoLift e m (Base n) where
instance (Applicative m, Applicative n, Monad m, Monad n, AutoLiftInternal (Layer e m) (Base n) (Layer e m) (Base n)) => AutoLift e m (Base n) where
operation' _ f = autolift (Proxy :: Proxy (Layer e m)) (Proxy :: Proxy (Base n)) (Layer f)
instance (Monad m, Monad n, AutoLiftInternal (Layer e m) (Layer d n) (Layer e m) (Layer d n)) => AutoLift e m (Layer d n) where
instance (Applicative m, Applicative n, Monad m, Monad n, AutoLiftInternal (Layer e m) (Layer d n) (Layer e m) (Layer d n)) => AutoLift e m (Layer d n) where
operation' _ f = autolift (Proxy :: Proxy (Layer e m)) (Proxy :: Proxy (Layer d n)) (Layer f)


class (Monad m, Monad n) => AutoLiftBase m n where
class (Applicative m, Applicative n, Monad m, Monad n) => AutoLiftBase m n where
base' :: m a -> n a

instance (Monad m, Monad n, AutoLiftInternal (Base m) (Base n) (Base m) (Base n)) => AutoLiftBase m (Base n) where
instance (Applicative m, Applicative n, Monad m, Monad n, AutoLiftInternal (Base m) (Base n) (Base m) (Base n)) => AutoLiftBase m (Base n) where
base' m = autolift (Proxy :: Proxy (Base m)) (Proxy :: Proxy (Base n)) (Base m)
instance (Monad m, Monad n, AutoLiftInternal (Base m) (Layer e n) (Base m) (Layer e n)) => AutoLiftBase m (Layer e n) where
instance (Applicative m, Applicative n, Monad m, Monad n, AutoLiftInternal (Base m) (Layer e n) (Base m) (Layer e n)) => AutoLiftBase m (Layer e n) where
base' m = autolift (Proxy :: Proxy (Base m)) (Proxy :: Proxy (Layer e n)) (Base m)


data Proxy (m :: * -> *) = Proxy

class (Monad m1, Monad m2) => AutoLiftInternal m1 m2 n1 n2 where
class (Applicative m1, Applicative m2, Monad m1, Monad m2) => AutoLiftInternal m1 m2 n1 n2 where
autolift :: Proxy n1 -> Proxy n2 -> m1 a -> m2 a

pre :: Proxy (Layer r m) -> Proxy m
pre Proxy = Proxy

instance (Monad m) => AutoLiftInternal m m (Base n) (Base n) where
instance (Applicative m, Monad m) => AutoLiftInternal m m (Base n) (Base n) where
autolift Proxy Proxy = id
instance (AutoLiftInternal m1 m2 (Base n1) n2) => AutoLiftInternal m1 (Layer r m2) (Base n1) (Layer s n2) where
autolift p1 p2 = Layer . (>>=) . autolift p1 (pre p2)
Expand Down
9 changes: 4 additions & 5 deletions src/Control/Effects/NonDet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import Prelude hiding (foldr)
import Data.Foldable
import Data.Monoid
import Control.Applicative
import Control.Monad
import Control.Newtype

instance (Monad m, Monoid r) => Monoid (WrappedMonad m r) where
mempty = WrapMonad $ return mempty
mappend (WrapMonad a) (WrapMonad b) = WrapMonad $ liftM2 mappend a b
instance (Applicative m, Monoid r) => Monoid (WrappedMonad m r) where
mempty = WrapMonad $ pure mempty
mappend (WrapMonad a) (WrapMonad b) = WrapMonad $ liftA2 mappend a b

newtype WrappedAlt f a = WrapAlt (f a)
instance Newtype (WrappedAlt m a) (m a) where
Expand Down Expand Up @@ -47,7 +46,7 @@ accumulate f = Handler
, fin = return . unpack
}

newtype BFS r = BFS { unBFS :: Int -> Maybe r }
newtype BFS r = BFS (Int -> Maybe r)
instance Monoid r => Monoid (BFS r) where
mempty = BFS $ \d -> if d == 0 then Just mempty else Nothing
BFS f `mappend` BFS g = BFS $ \d -> if d == 0 then f d else f d `mappend` g (d - 1)
Expand Down

0 comments on commit f1ae0ec

Please sign in to comment.