Add MonadLogic instence for ReaderT
This commit is contained in:
parent
c2088a6967
commit
e08de1f8f7
|
@ -3,8 +3,8 @@ module Control.Monad.Logic.Class (class MonadLogic, msplit, interleave, fbind, r
|
|||
import Prelude
|
||||
|
||||
import Control.MonadPlus (class MonadPlus, (<|>), empty)
|
||||
import Control.Monad.State (runStateT)
|
||||
import Control.Monad.State.Trans (StateT(..))
|
||||
import Control.Monad.State.Trans (StateT(..), runStateT)
|
||||
import Control.Monad.Reader.Trans (ReaderT(..), runReaderT)
|
||||
import Data.Tuple.Nested (type (/\), (/\))
|
||||
import Data.Maybe (Maybe(..))
|
||||
|
||||
|
@ -32,3 +32,10 @@ instance MonadLogic m => MonadLogic (StateT s m) where
|
|||
Nothing -> pure (Nothing /\ s)
|
||||
Just ((a /\ s') /\ m) -> pure (Just (a /\ (StateT $ const m)) /\ s')
|
||||
interleave m1 m2 = StateT $ \s -> runStateT m1 s <|> runStateT m2 s
|
||||
|
||||
instance MonadLogic m => MonadLogic (ReaderT r m) where
|
||||
msplit sm = ReaderT $ \r -> do
|
||||
msplit (runReaderT sm r) >>= case _ of
|
||||
Nothing -> pure Nothing
|
||||
Just (a /\ m) -> pure $ Just $ a /\ (ReaderT $ const m)
|
||||
interleave m1 m2 = ReaderT $ \r -> runReaderT m1 r <|> runReaderT m2 r
|
||||
|
|
Loading…
Reference in New Issue
Block a user