{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Safe #-}
#endif
module Control.Monad.Logic.Class (MonadLogic(..), reflect) where
import Control.Applicative
import Control.Monad
import Control.Monad.Reader (ReaderT(..))
import Control.Monad.Trans (MonadTrans(..))
import qualified Control.Monad.State.Lazy as LazyST
import qualified Control.Monad.State.Strict as StrictST
class (Monad m, Alternative m) => MonadLogic m where
msplit :: m a -> m (Maybe (a, m a))
interleave :: m a -> m a -> m a
(>>-) :: m a -> (a -> m b) -> m b
infixl 1 >>-
once :: m a -> m a
lnot :: m a -> m ()
ifte :: m a -> (a -> m b) -> m b -> m b
interleave m a
m1 m a
m2 = m a -> m (Maybe (a, m a))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit m a
m1 m (Maybe (a, m a)) -> (Maybe (a, m a) -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
m a -> ((a, m a) -> m a) -> Maybe (a, m a) -> m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m a
m2 (\(a
a, m a
m1') -> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m a -> m a -> m a
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
interleave m a
m2 m a
m1')
m a
m >>- a -> m b
f = do (a
a, m a
m') <- m (a, m a)
-> ((a, m a) -> m (a, m a)) -> Maybe (a, m a) -> m (a, m a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m (a, m a)
forall (f :: * -> *) a. Alternative f => f a
empty (a, m a) -> m (a, m a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, m a) -> m (a, m a)) -> m (Maybe (a, m a)) -> m (a, m a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m a -> m (Maybe (a, m a))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit m a
m
m b -> m b -> m b
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
interleave (a -> m b
f a
a) (m a
m' m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- a -> m b
f)
ifte m a
t a -> m b
th m b
el = m a -> m (Maybe (a, m a))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit m a
t m (Maybe (a, m a)) -> (Maybe (a, m a) -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m b -> ((a, m a) -> m b) -> Maybe (a, m a) -> m b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m b
el (\(a
a,m a
m) -> a -> m b
th a
a m b -> m b -> m b
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (m a
m m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
th))
once m a
m = do (a
a, m a
_) <- m (a, m a)
-> ((a, m a) -> m (a, m a)) -> Maybe (a, m a) -> m (a, m a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m (a, m a)
forall (f :: * -> *) a. Alternative f => f a
empty (a, m a) -> m (a, m a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, m a) -> m (a, m a)) -> m (Maybe (a, m a)) -> m (a, m a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m a -> m (Maybe (a, m a))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit m a
m
a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
lnot m a
m = m a -> (a -> m ()) -> m () -> m ()
forall (m :: * -> *) a b.
MonadLogic m =>
m a -> (a -> m b) -> m b -> m b
ifte (m a -> m a
forall (m :: * -> *) a. MonadLogic m => m a -> m a
once m a
m) (m () -> a -> m ()
forall a b. a -> b -> a
const m ()
forall (f :: * -> *) a. Alternative f => f a
empty) (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
reflect :: Alternative m => Maybe (a, m a) -> m a
reflect :: forall (m :: * -> *) a. Alternative m => Maybe (a, m a) -> m a
reflect Maybe (a, m a)
Nothing = m a
forall (f :: * -> *) a. Alternative f => f a
empty
reflect (Just (a
a, m a
m)) = a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m a
m
instance MonadLogic [] where
msplit :: forall a. [a] -> [Maybe (a, [a])]
msplit [] = Maybe (a, [a]) -> [Maybe (a, [a])]
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (a, [a])
forall a. Maybe a
Nothing
msplit (a
x:[a]
xs) = Maybe (a, [a]) -> [Maybe (a, [a])]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, [a]) -> [Maybe (a, [a])])
-> Maybe (a, [a]) -> [Maybe (a, [a])]
forall a b. (a -> b) -> a -> b
$ (a, [a]) -> Maybe (a, [a])
forall a. a -> Maybe a
Just (a
x, [a]
xs)
instance MonadLogic m => MonadLogic (ReaderT e m) where
msplit :: forall a. ReaderT e m a -> ReaderT e m (Maybe (a, ReaderT e m a))
msplit ReaderT e m a
rm = (e -> m (Maybe (a, ReaderT e m a)))
-> ReaderT e m (Maybe (a, ReaderT e m a))
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((e -> m (Maybe (a, ReaderT e m a)))
-> ReaderT e m (Maybe (a, ReaderT e m a)))
-> (e -> m (Maybe (a, ReaderT e m a)))
-> ReaderT e m (Maybe (a, ReaderT e m a))
forall a b. (a -> b) -> a -> b
$ \e
e -> do Maybe (a, m a)
r <- m a -> m (Maybe (a, m a))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit (m a -> m (Maybe (a, m a))) -> m a -> m (Maybe (a, m a))
forall a b. (a -> b) -> a -> b
$ ReaderT e m a -> e -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT e m a
rm e
e
case Maybe (a, m a)
r of
Maybe (a, m a)
Nothing -> Maybe (a, ReaderT e m a) -> m (Maybe (a, ReaderT e m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (a, ReaderT e m a)
forall a. Maybe a
Nothing
Just (a
a, m a
m) -> Maybe (a, ReaderT e m a) -> m (Maybe (a, ReaderT e m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, ReaderT e m a) -> Maybe (a, ReaderT e m a)
forall a. a -> Maybe a
Just (a
a, m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m))
instance (MonadLogic m, MonadPlus m) => MonadLogic (StrictST.StateT s m) where
msplit :: forall a. StateT s m a -> StateT s m (Maybe (a, StateT s m a))
msplit StateT s m a
sm = (s -> m (Maybe (a, StateT s m a), s))
-> StateT s m (Maybe (a, StateT s m a))
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StrictST.StateT ((s -> m (Maybe (a, StateT s m a), s))
-> StateT s m (Maybe (a, StateT s m a)))
-> (s -> m (Maybe (a, StateT s m a), s))
-> StateT s m (Maybe (a, StateT s m a))
forall a b. (a -> b) -> a -> b
$ \s
s ->
do Maybe ((a, s), m (a, s))
r <- m (a, s) -> m (Maybe ((a, s), m (a, s)))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m a
sm s
s)
case Maybe ((a, s), m (a, s))
r of
Maybe ((a, s), m (a, s))
Nothing -> (Maybe (a, StateT s m a), s) -> m (Maybe (a, StateT s m a), s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, StateT s m a)
forall a. Maybe a
Nothing, s
s)
Just ((a
a,s
s'), m (a, s)
m) ->
(Maybe (a, StateT s m a), s) -> m (Maybe (a, StateT s m a), s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, StateT s m a) -> Maybe (a, StateT s m a)
forall a. a -> Maybe a
Just (a
a, (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StrictST.StateT (m (a, s) -> s -> m (a, s)
forall a b. a -> b -> a
const m (a, s)
m)), s
s')
interleave :: forall a. StateT s m a -> StateT s m a -> StateT s m a
interleave StateT s m a
ma StateT s m a
mb = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StrictST.StateT ((s -> m (a, s)) -> StateT s m a)
-> (s -> m (a, s)) -> StateT s m a
forall a b. (a -> b) -> a -> b
$ \s
s ->
StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m a
ma s
s m (a, s) -> m (a, s) -> m (a, s)
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
`interleave` StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m a
mb s
s
StateT s m a
ma >>- :: forall a b. StateT s m a -> (a -> StateT s m b) -> StateT s m b
>>- a -> StateT s m b
f = (s -> m (b, s)) -> StateT s m b
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StrictST.StateT ((s -> m (b, s)) -> StateT s m b)
-> (s -> m (b, s)) -> StateT s m b
forall a b. (a -> b) -> a -> b
$ \s
s ->
StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m a
ma s
s m (a, s) -> ((a, s) -> m (b, s)) -> m (b, s)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \(a
a,s
s') -> StateT s m b -> s -> m (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT (a -> StateT s m b
f a
a) s
s'
ifte :: forall a b.
StateT s m a -> (a -> StateT s m b) -> StateT s m b -> StateT s m b
ifte StateT s m a
t a -> StateT s m b
th StateT s m b
el = (s -> m (b, s)) -> StateT s m b
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StrictST.StateT ((s -> m (b, s)) -> StateT s m b)
-> (s -> m (b, s)) -> StateT s m b
forall a b. (a -> b) -> a -> b
$ \s
s -> m (a, s) -> ((a, s) -> m (b, s)) -> m (b, s) -> m (b, s)
forall (m :: * -> *) a b.
MonadLogic m =>
m a -> (a -> m b) -> m b -> m b
ifte (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m a
t s
s)
(\(a
a,s
s') -> StateT s m b -> s -> m (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT (a -> StateT s m b
th a
a) s
s')
(StateT s m b -> s -> m (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m b
el s
s)
once :: forall a. StateT s m a -> StateT s m a
once StateT s m a
ma = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StrictST.StateT ((s -> m (a, s)) -> StateT s m a)
-> (s -> m (a, s)) -> StateT s m a
forall a b. (a -> b) -> a -> b
$ \s
s -> m (a, s) -> m (a, s)
forall (m :: * -> *) a. MonadLogic m => m a -> m a
once (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
StrictST.runStateT StateT s m a
ma s
s)
instance (MonadLogic m, MonadPlus m) => MonadLogic (LazyST.StateT s m) where
msplit :: forall a. StateT s m a -> StateT s m (Maybe (a, StateT s m a))
msplit StateT s m a
sm = (s -> m (Maybe (a, StateT s m a), s))
-> StateT s m (Maybe (a, StateT s m a))
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
LazyST.StateT ((s -> m (Maybe (a, StateT s m a), s))
-> StateT s m (Maybe (a, StateT s m a)))
-> (s -> m (Maybe (a, StateT s m a), s))
-> StateT s m (Maybe (a, StateT s m a))
forall a b. (a -> b) -> a -> b
$ \s
s ->
do Maybe ((a, s), m (a, s))
r <- m (a, s) -> m (Maybe ((a, s), m (a, s)))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m a
sm s
s)
case Maybe ((a, s), m (a, s))
r of
Maybe ((a, s), m (a, s))
Nothing -> (Maybe (a, StateT s m a), s) -> m (Maybe (a, StateT s m a), s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, StateT s m a)
forall a. Maybe a
Nothing, s
s)
Just ((a
a,s
s'), m (a, s)
m) ->
(Maybe (a, StateT s m a), s) -> m (Maybe (a, StateT s m a), s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, StateT s m a) -> Maybe (a, StateT s m a)
forall a. a -> Maybe a
Just (a
a, (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
LazyST.StateT (m (a, s) -> s -> m (a, s)
forall a b. a -> b -> a
const m (a, s)
m)), s
s')
interleave :: forall a. StateT s m a -> StateT s m a -> StateT s m a
interleave StateT s m a
ma StateT s m a
mb = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
LazyST.StateT ((s -> m (a, s)) -> StateT s m a)
-> (s -> m (a, s)) -> StateT s m a
forall a b. (a -> b) -> a -> b
$ \s
s ->
StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m a
ma s
s m (a, s) -> m (a, s) -> m (a, s)
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
`interleave` StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m a
mb s
s
StateT s m a
ma >>- :: forall a b. StateT s m a -> (a -> StateT s m b) -> StateT s m b
>>- a -> StateT s m b
f = (s -> m (b, s)) -> StateT s m b
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
LazyST.StateT ((s -> m (b, s)) -> StateT s m b)
-> (s -> m (b, s)) -> StateT s m b
forall a b. (a -> b) -> a -> b
$ \s
s ->
StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m a
ma s
s m (a, s) -> ((a, s) -> m (b, s)) -> m (b, s)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \(a
a,s
s') -> StateT s m b -> s -> m (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT (a -> StateT s m b
f a
a) s
s'
ifte :: forall a b.
StateT s m a -> (a -> StateT s m b) -> StateT s m b -> StateT s m b
ifte StateT s m a
t a -> StateT s m b
th StateT s m b
el = (s -> m (b, s)) -> StateT s m b
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
LazyST.StateT ((s -> m (b, s)) -> StateT s m b)
-> (s -> m (b, s)) -> StateT s m b
forall a b. (a -> b) -> a -> b
$ \s
s -> m (a, s) -> ((a, s) -> m (b, s)) -> m (b, s) -> m (b, s)
forall (m :: * -> *) a b.
MonadLogic m =>
m a -> (a -> m b) -> m b -> m b
ifte (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m a
t s
s)
(\(a
a,s
s') -> StateT s m b -> s -> m (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT (a -> StateT s m b
th a
a) s
s')
(StateT s m b -> s -> m (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m b
el s
s)
once :: forall a. StateT s m a -> StateT s m a
once StateT s m a
ma = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
LazyST.StateT ((s -> m (a, s)) -> StateT s m a)
-> (s -> m (a, s)) -> StateT s m a
forall a b. (a -> b) -> a -> b
$ \s
s -> m (a, s) -> m (a, s)
forall (m :: * -> *) a. MonadLogic m => m a -> m a
once (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LazyST.runStateT StateT s m a
ma s
s)