Add MonadLogic instence for ReaderT

This commit is contained in:
Danila Fedorin 2023-03-04 19:01:23 -08:00
parent c2088a6967
commit e08de1f8f7
1 changed files with 9 additions and 2 deletions

View File

@ -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