{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UndecidableInstances #-}
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Safe #-}
#endif
module Control.Monad.Logic (
module Control.Monad.Logic.Class,
Logic,
logic,
runLogic,
observe,
observeMany,
observeAll,
LogicT(..),
runLogicT,
observeT,
observeManyT,
observeAllT,
fromLogicT,
fromLogicTWith,
hoistLogicT,
embedLogicT
) where
import Prelude (error, (-))
import Control.Applicative (Alternative(..), Applicative, liftA2, pure, (<*>))
import Control.Monad (join, MonadPlus(..), liftM, Monad(..), fail)
import qualified Control.Monad.Fail as Fail
import Control.Monad.Identity (Identity(..))
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Trans (MonadTrans(..))
#if MIN_VERSION_base(4,8,0)
import Control.Monad.Zip (MonadZip (..))
#endif
import Control.Monad.Reader.Class (MonadReader(..))
import Control.Monad.State.Class (MonadState(..))
import Control.Monad.Error.Class (MonadError(..))
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup (..))
#endif
import Data.Bool (otherwise)
import Data.Eq ((==))
import qualified Data.Foldable as F
import Data.Function (($), (.), const)
import Data.Functor (Functor(..), (<$>))
import Data.Int
import qualified Data.List as L
import Data.Maybe (Maybe(..))
import Data.Monoid (Monoid (..))
import Data.Ord ((<=))
import qualified Data.Traversable as T
import Control.Monad.Logic.Class
newtype LogicT m a =
LogicT { forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT :: forall r. (a -> m r -> m r) -> m r -> m r }
#if !MIN_VERSION_base(4,13,0)
observeT :: Monad m => LogicT m a -> m a
#else
observeT :: Fail.MonadFail m => LogicT m a -> m a
#endif
observeT :: forall (m :: * -> *) a. MonadFail m => LogicT m a -> m a
observeT LogicT m a
lt = forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
lt (forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return) (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"No answer.")
observeAllT :: Applicative m => LogicT m a -> m [a]
observeAllT :: forall (m :: * -> *) a. Applicative m => LogicT m a -> m [a]
observeAllT LogicT m a
m = forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:)) (forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
observeManyT :: Monad m => Int -> LogicT m a -> m [a]
observeManyT :: forall (m :: * -> *) a. Monad m => Int -> LogicT m a -> m [a]
observeManyT Int
n LogicT m a
m
| Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return []
| Int
n forall a. Eq a => a -> a -> Bool
== Int
1 = forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (\a
a m [a]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return [a
a]) (forall (m :: * -> *) a. Monad m => a -> m a
return [])
| Bool
otherwise = forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT (forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit LogicT m a
m) forall {m :: * -> *} {a} {p}.
Monad m =>
Maybe (a, LogicT m a) -> p -> m [a]
sk (forall (m :: * -> *) a. Monad m => a -> m a
return [])
where
sk :: Maybe (a, LogicT m a) -> p -> m [a]
sk Maybe (a, LogicT m a)
Nothing p
_ = forall (m :: * -> *) a. Monad m => a -> m a
return []
sk (Just (a
a, LogicT m a
m')) p
_ = (a
aforall a. a -> [a] -> [a]
:) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (m :: * -> *) a. Monad m => Int -> LogicT m a -> m [a]
observeManyT (Int
nforall a. Num a => a -> a -> a
-Int
1) LogicT m a
m'
runLogicT :: LogicT m a -> (a -> m r -> m r) -> m r -> m r
runLogicT :: forall (m :: * -> *) a r.
LogicT m a -> (a -> m r -> m r) -> m r -> m r
runLogicT (LogicT forall r. (a -> m r -> m r) -> m r -> m r
r) = forall r. (a -> m r -> m r) -> m r -> m r
r
#if MIN_VERSION_base(4,8,0)
fromLogicT :: (Alternative (t m), MonadTrans t, Monad m, Monad (t m))
=> LogicT m a -> t m a
#else
fromLogicT :: (Alternative (t m), MonadTrans t, Applicative m, Monad m, Monad (t m))
=> LogicT m a -> t m a
#endif
fromLogicT :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(Alternative (t m), MonadTrans t, Monad m, Monad (t m)) =>
LogicT m a -> t m a
fromLogicT = forall (m :: * -> *) (n :: * -> *) a.
(Applicative m, Monad n, Alternative n) =>
(forall x. m x -> n x) -> LogicT m a -> n a
fromLogicTWith forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
fromLogicTWith :: (Applicative m, Monad n, Alternative n)
=> (forall x. m x -> n x) -> LogicT m a -> n a
fromLogicTWith :: forall (m :: * -> *) (n :: * -> *) a.
(Applicative m, Monad n, Alternative n) =>
(forall x. m x -> n x) -> LogicT m a -> n a
fromLogicTWith forall x. m x -> n x
p (LogicT forall r. (a -> m r -> m r) -> m r -> m r
f) = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. m x -> n x
p forall a b. (a -> b) -> a -> b
$
forall r. (a -> m r -> m r) -> m r -> m r
f (\a
a m (n a)
v -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (forall x. m x -> n x
p m (n a)
v))) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. Alternative f => f a
empty)
hoistLogicT :: (Applicative m, Monad n) => (forall x. m x -> n x) -> LogicT m a -> LogicT n a
hoistLogicT :: forall (m :: * -> *) (n :: * -> *) a.
(Applicative m, Monad n) =>
(forall x. m x -> n x) -> LogicT m a -> LogicT n a
hoistLogicT forall x. m x -> n x
f = forall (m :: * -> *) (n :: * -> *) a.
(Applicative m, Monad n, Alternative n) =>
(forall x. m x -> n x) -> LogicT m a -> n a
fromLogicTWith (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. m x -> n x
f)
embedLogicT :: Applicative m => (forall a. m a -> LogicT n a) -> LogicT m b -> LogicT n b
embedLogicT :: forall (m :: * -> *) (n :: * -> *) b.
Applicative m =>
(forall a. m a -> LogicT n a) -> LogicT m b -> LogicT n b
embedLogicT forall a. m a -> LogicT n a
f = forall (m :: * -> *) (n :: * -> *) a.
(Applicative m, Monad n, Alternative n) =>
(forall x. m x -> n x) -> LogicT m a -> n a
fromLogicTWith forall a. m a -> LogicT n a
f
type Logic = LogicT Identity
logic :: (forall r. (a -> r -> r) -> r -> r) -> Logic a
logic :: forall a. (forall r. (a -> r -> r) -> r -> r) -> Logic a
logic forall r. (a -> r -> r) -> r -> r
f = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> Identity r -> Identity r
k -> forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall r. (a -> r -> r) -> r -> r
f (\a
a -> forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Identity r -> Identity r
k a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Identity a
Identity) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. Identity a -> a
runIdentity
observe :: Logic a -> a
observe :: forall a. Logic a -> a
observe Logic a
lt = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT Logic a
lt (forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure) (forall a. HasCallStack => String -> a
error String
"No answer.")
observeAll :: Logic a -> [a]
observeAll :: forall a. Logic a -> [a]
observeAll = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Applicative m => LogicT m a -> m [a]
observeAllT
observeMany :: Int -> Logic a -> [a]
observeMany :: forall a. Int -> Logic a -> [a]
observeMany Int
i = forall a. Int -> [a] -> [a]
L.take Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Logic a -> [a]
observeAll
runLogic :: Logic a -> (a -> r -> r) -> r -> r
runLogic :: forall a r. Logic a -> (a -> r -> r) -> r -> r
runLogic Logic a
l a -> r -> r
s r
f = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT Logic a
l a -> Identity r -> Identity r
si Identity r
fi
where
si :: a -> Identity r -> Identity r
si = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> r -> r
s
fi :: Identity r
fi = forall a. a -> Identity a
Identity r
f
instance Functor (LogicT f) where
fmap :: forall a b. (a -> b) -> LogicT f a -> LogicT f b
fmap a -> b
f LogicT f a
lt = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \b -> f r -> f r
sk f r
fk -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT f a
lt (b -> f r -> f r
sk forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) f r
fk
instance Applicative (LogicT f) where
pure :: forall a. a -> LogicT f a
pure a
a = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> f r -> f r
sk f r
fk -> a -> f r -> f r
sk a
a f r
fk
LogicT f (a -> b)
f <*> :: forall a b. LogicT f (a -> b) -> LogicT f a -> LogicT f b
<*> LogicT f a
a = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \b -> f r -> f r
sk f r
fk -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT f (a -> b)
f (\a -> b
g f r
fk' -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT f a
a (b -> f r -> f r
sk forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
g) f r
fk') f r
fk
instance Alternative (LogicT f) where
empty :: forall a. LogicT f a
empty = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> f r -> f r
_ f r
fk -> f r
fk
LogicT f a
f1 <|> :: forall a. LogicT f a -> LogicT f a -> LogicT f a
<|> LogicT f a
f2 = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> f r -> f r
sk f r
fk -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT f a
f1 a -> f r -> f r
sk (forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT f a
f2 a -> f r -> f r
sk f r
fk)
instance Monad (LogicT m) where
return :: forall a. a -> LogicT m a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
LogicT m a
m >>= :: forall a b. LogicT m a -> (a -> LogicT m b) -> LogicT m b
>>= a -> LogicT m b
f = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \b -> m r -> m r
sk m r
fk -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (\a
a m r
fk' -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT (a -> LogicT m b
f a
a) b -> m r -> m r
sk m r
fk') m r
fk
#if !MIN_VERSION_base(4,13,0)
fail = Fail.fail
#endif
instance Fail.MonadFail (LogicT m) where
fail :: forall a. String -> LogicT m a
fail String
_ = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> m r -> m r
_ m r
fk -> m r
fk
instance MonadPlus (LogicT m) where
mzero :: forall a. LogicT m a
mzero = forall (f :: * -> *) a. Alternative f => f a
empty
mplus :: forall a. LogicT m a -> LogicT m a -> LogicT m a
mplus = forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
#if MIN_VERSION_base(4,9,0)
instance Semigroup (LogicT m a) where
<> :: LogicT m a -> LogicT m a -> LogicT m a
(<>) = forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus
sconcat :: NonEmpty (LogicT m a) -> LogicT m a
sconcat = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
F.foldr1 forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus
#endif
instance Monoid (LogicT m a) where
mempty :: LogicT m a
mempty = forall (f :: * -> *) a. Alternative f => f a
empty
#if MIN_VERSION_base(4,9,0)
mappend :: LogicT m a -> LogicT m a -> LogicT m a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
#else
mappend = (<|>)
#endif
mconcat :: [LogicT m a] -> LogicT m a
mconcat = forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
F.asum
instance MonadTrans LogicT where
lift :: forall (m :: * -> *) a. Monad m => m a -> LogicT m a
lift m a
m = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> m r -> m r
sk m r
fk -> m a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> a -> m r -> m r
sk a
a m r
fk
instance (MonadIO m) => MonadIO (LogicT m) where
liftIO :: forall a. IO a -> LogicT m a
liftIO = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
instance (Monad m) => MonadLogic (LogicT m) where
msplit :: forall a. LogicT m a -> LogicT m (Maybe (a, LogicT m a))
msplit LogicT m a
m = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m forall {t :: (* -> *) -> * -> *} {m :: * -> *} {m :: * -> *} {a}
{b}.
(MonadTrans t, Monad m, Monad m, Monad (t m), Alternative (t m)) =>
a -> m (Maybe (b, t m b)) -> m (Maybe (a, t m b))
ssk (forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing)
where
ssk :: a -> m (Maybe (b, t m b)) -> m (Maybe (a, t m b))
ssk a
a m (Maybe (b, t m b))
fk = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (a
a, forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe (b, t m b))
fk forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. Alternative m => Maybe (a, m a) -> m a
reflect)
once :: forall a. LogicT m a -> LogicT m a
once LogicT m a
m = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> m r -> m r
sk m r
fk -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (\a
a m r
_ -> a -> m r -> m r
sk a
a m r
fk) m r
fk
lnot :: forall a. LogicT m a -> LogicT m ()
lnot LogicT m a
m = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \() -> m r -> m r
sk m r
fk -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (\a
_ m r
_ -> m r
fk) (() -> m r -> m r
sk () m r
fk)
#if MIN_VERSION_base(4,8,0)
instance {-# OVERLAPPABLE #-} (Applicative m, F.Foldable m) => F.Foldable (LogicT m) where
foldMap :: forall m a. Monoid m => (a -> m) -> LogicT m a -> m
foldMap a -> m
f LogicT m a
m = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
F.fold forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => a -> a -> a
mappend forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty)
instance {-# OVERLAPPING #-} F.Foldable (LogicT Identity) where
foldr :: forall a b. (a -> b -> b) -> b -> LogicT Identity a -> b
foldr a -> b -> b
f b
z LogicT Identity a
m = forall a r. Logic a -> (a -> r -> r) -> r -> r
runLogic LogicT Identity a
m a -> b -> b
f b
z
#else
instance (Applicative m, F.Foldable m) => F.Foldable (LogicT m) where
foldMap f m = F.fold $ unLogicT m (fmap . mappend . f) (pure mempty)
#endif
newtype ML m a = ML (m (MLView m a))
deriving (forall a b. a -> ML m b -> ML m a
forall a b. (a -> b) -> ML m a -> ML m b
forall (m :: * -> *) a b. Functor m => a -> ML m b -> ML m a
forall (m :: * -> *) a b. Functor m => (a -> b) -> ML m a -> ML m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ML m b -> ML m a
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> ML m b -> ML m a
fmap :: forall a b. (a -> b) -> ML m a -> ML m b
$cfmap :: forall (m :: * -> *) a b. Functor m => (a -> b) -> ML m a -> ML m b
Functor, forall a. ML m a -> Bool
forall m a. Monoid m => (a -> m) -> ML m a -> m
forall a b. (a -> b -> b) -> b -> ML m a -> b
forall (m :: * -> *) a. (Foldable m, Eq a) => a -> ML m a -> Bool
forall (m :: * -> *) a. (Foldable m, Num a) => ML m a -> a
forall (m :: * -> *) a. (Foldable m, Ord a) => ML m a -> a
forall (m :: * -> *) m. (Foldable m, Monoid m) => ML m m -> m
forall (m :: * -> *) a. Foldable m => ML m a -> Bool
forall (m :: * -> *) a. Foldable m => ML m a -> Int
forall (m :: * -> *) a. Foldable m => ML m a -> [a]
forall (m :: * -> *) a. Foldable m => (a -> a -> a) -> ML m a -> a
forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ML m a -> m
forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ML m a -> b
forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ML m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => ML m a -> a
$cproduct :: forall (m :: * -> *) a. (Foldable m, Num a) => ML m a -> a
sum :: forall a. Num a => ML m a -> a
$csum :: forall (m :: * -> *) a. (Foldable m, Num a) => ML m a -> a
minimum :: forall a. Ord a => ML m a -> a
$cminimum :: forall (m :: * -> *) a. (Foldable m, Ord a) => ML m a -> a
maximum :: forall a. Ord a => ML m a -> a
$cmaximum :: forall (m :: * -> *) a. (Foldable m, Ord a) => ML m a -> a
elem :: forall a. Eq a => a -> ML m a -> Bool
$celem :: forall (m :: * -> *) a. (Foldable m, Eq a) => a -> ML m a -> Bool
length :: forall a. ML m a -> Int
$clength :: forall (m :: * -> *) a. Foldable m => ML m a -> Int
null :: forall a. ML m a -> Bool
$cnull :: forall (m :: * -> *) a. Foldable m => ML m a -> Bool
toList :: forall a. ML m a -> [a]
$ctoList :: forall (m :: * -> *) a. Foldable m => ML m a -> [a]
foldl1 :: forall a. (a -> a -> a) -> ML m a -> a
$cfoldl1 :: forall (m :: * -> *) a. Foldable m => (a -> a -> a) -> ML m a -> a
foldr1 :: forall a. (a -> a -> a) -> ML m a -> a
$cfoldr1 :: forall (m :: * -> *) a. Foldable m => (a -> a -> a) -> ML m a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> ML m a -> b
$cfoldl' :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ML m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ML m a -> b
$cfoldl :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ML m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ML m a -> b
$cfoldr' :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ML m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ML m a -> b
$cfoldr :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ML m a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> ML m a -> m
$cfoldMap' :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ML m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ML m a -> m
$cfoldMap :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ML m a -> m
fold :: forall m. Monoid m => ML m m -> m
$cfold :: forall (m :: * -> *) m. (Foldable m, Monoid m) => ML m m -> m
F.Foldable, forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall {m :: * -> *}. Traversable m => Functor (ML m)
forall {m :: * -> *}. Traversable m => Foldable (ML m)
forall (m :: * -> *) (m :: * -> *) a.
(Traversable m, Monad m) =>
ML m (m a) -> m (ML m a)
forall (m :: * -> *) (f :: * -> *) a.
(Traversable m, Applicative f) =>
ML m (f a) -> f (ML m a)
forall (m :: * -> *) (m :: * -> *) a b.
(Traversable m, Monad m) =>
(a -> m b) -> ML m a -> m (ML m b)
forall (m :: * -> *) (f :: * -> *) a b.
(Traversable m, Applicative f) =>
(a -> f b) -> ML m a -> f (ML m b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ML m a -> f (ML m b)
sequence :: forall (m :: * -> *) a. Monad m => ML m (m a) -> m (ML m a)
$csequence :: forall (m :: * -> *) (m :: * -> *) a.
(Traversable m, Monad m) =>
ML m (m a) -> m (ML m a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ML m a -> m (ML m b)
$cmapM :: forall (m :: * -> *) (m :: * -> *) a b.
(Traversable m, Monad m) =>
(a -> m b) -> ML m a -> m (ML m b)
sequenceA :: forall (f :: * -> *) a. Applicative f => ML m (f a) -> f (ML m a)
$csequenceA :: forall (m :: * -> *) (f :: * -> *) a.
(Traversable m, Applicative f) =>
ML m (f a) -> f (ML m a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ML m a -> f (ML m b)
$ctraverse :: forall (m :: * -> *) (f :: * -> *) a b.
(Traversable m, Applicative f) =>
(a -> f b) -> ML m a -> f (ML m b)
T.Traversable)
data MLView m a = EmptyML | ConsML a (ML m a)
deriving (forall a b. a -> MLView m b -> MLView m a
forall a b. (a -> b) -> MLView m a -> MLView m b
forall (m :: * -> *) a b.
Functor m =>
a -> MLView m b -> MLView m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> MLView m a -> MLView m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> MLView m b -> MLView m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> MLView m b -> MLView m a
fmap :: forall a b. (a -> b) -> MLView m a -> MLView m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> MLView m a -> MLView m b
Functor, forall a. MLView m a -> Bool
forall m a. Monoid m => (a -> m) -> MLView m a -> m
forall a b. (a -> b -> b) -> b -> MLView m a -> b
forall (m :: * -> *) a.
(Foldable m, Eq a) =>
a -> MLView m a -> Bool
forall (m :: * -> *) a. (Foldable m, Num a) => MLView m a -> a
forall (m :: * -> *) a. (Foldable m, Ord a) => MLView m a -> a
forall (m :: * -> *) m. (Foldable m, Monoid m) => MLView m m -> m
forall (m :: * -> *) a. Foldable m => MLView m a -> Bool
forall (m :: * -> *) a. Foldable m => MLView m a -> Int
forall (m :: * -> *) a. Foldable m => MLView m a -> [a]
forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> MLView m a -> a
forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> MLView m a -> m
forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> MLView m a -> b
forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> MLView m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => MLView m a -> a
$cproduct :: forall (m :: * -> *) a. (Foldable m, Num a) => MLView m a -> a
sum :: forall a. Num a => MLView m a -> a
$csum :: forall (m :: * -> *) a. (Foldable m, Num a) => MLView m a -> a
minimum :: forall a. Ord a => MLView m a -> a
$cminimum :: forall (m :: * -> *) a. (Foldable m, Ord a) => MLView m a -> a
maximum :: forall a. Ord a => MLView m a -> a
$cmaximum :: forall (m :: * -> *) a. (Foldable m, Ord a) => MLView m a -> a
elem :: forall a. Eq a => a -> MLView m a -> Bool
$celem :: forall (m :: * -> *) a.
(Foldable m, Eq a) =>
a -> MLView m a -> Bool
length :: forall a. MLView m a -> Int
$clength :: forall (m :: * -> *) a. Foldable m => MLView m a -> Int
null :: forall a. MLView m a -> Bool
$cnull :: forall (m :: * -> *) a. Foldable m => MLView m a -> Bool
toList :: forall a. MLView m a -> [a]
$ctoList :: forall (m :: * -> *) a. Foldable m => MLView m a -> [a]
foldl1 :: forall a. (a -> a -> a) -> MLView m a -> a
$cfoldl1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> MLView m a -> a
foldr1 :: forall a. (a -> a -> a) -> MLView m a -> a
$cfoldr1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> MLView m a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> MLView m a -> b
$cfoldl' :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> MLView m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> MLView m a -> b
$cfoldl :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> MLView m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> MLView m a -> b
$cfoldr' :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> MLView m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> MLView m a -> b
$cfoldr :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> MLView m a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> MLView m a -> m
$cfoldMap' :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> MLView m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> MLView m a -> m
$cfoldMap :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> MLView m a -> m
fold :: forall m. Monoid m => MLView m m -> m
$cfold :: forall (m :: * -> *) m. (Foldable m, Monoid m) => MLView m m -> m
F.Foldable)
instance T.Traversable m => T.Traversable (MLView m) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MLView m a -> f (MLView m b)
traverse a -> f b
_ MLView m a
EmptyML = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) a. MLView m a
EmptyML
traverse a -> f b
f (ConsML a
x (ML m (MLView m a)
m))
= forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\b
y m (MLView m b)
ym -> forall (m :: * -> *) a. a -> ML m a -> MLView m a
ConsML b
y (forall (m :: * -> *) a. m (MLView m a) -> ML m a
ML m (MLView m b)
ym)) (a -> f b
f a
x) (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse a -> f b
f) m (MLView m a)
m)
toML :: Applicative m => LogicT m a -> ML m a
toML :: forall (m :: * -> *) a. Applicative m => LogicT m a -> ML m a
toML (LogicT forall r. (a -> m r -> m r) -> m r -> m r
q) = forall (m :: * -> *) a. m (MLView m a) -> ML m a
ML forall a b. (a -> b) -> a -> b
$ forall r. (a -> m r -> m r) -> m r -> m r
q (\a
a m (MLView m a)
m -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. a -> ML m a -> MLView m a
ConsML a
a (forall (m :: * -> *) a. m (MLView m a) -> ML m a
ML m (MLView m a)
m)) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) a. MLView m a
EmptyML)
fromML :: Monad m => ML m a -> LogicT m a
fromML :: forall (m :: * -> *) a. Monad m => ML m a -> LogicT m a
fromML (ML m (MLView m a)
m) = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (MLView m a)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \MLView m a
r -> case MLView m a
r of
MLView m a
EmptyML -> forall (f :: * -> *) a. Alternative f => f a
empty
ConsML a
a ML m a
xs -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => ML m a -> LogicT m a
fromML ML m a
xs
#if MIN_VERSION_base(4,8,0)
instance {-# OVERLAPPING #-} T.Traversable (LogicT Identity) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> LogicT Identity a -> f (LogicT Identity b)
traverse a -> f b
g LogicT Identity a
l = forall a r. Logic a -> (a -> r -> r) -> r -> r
runLogic LogicT Identity a
l (\a
a f (LogicT Identity b)
ft -> forall {f :: * -> *} {a}. Alternative f => a -> f a -> f a
cons forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
g a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (LogicT Identity b)
ft) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a. Alternative f => f a
empty)
where
cons :: a -> f a -> f a
cons a
a f a
l' = forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f a
l'
instance {-# OVERLAPPABLE #-} (Monad m, T.Traversable m) => T.Traversable (LogicT m) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> LogicT m a -> f (LogicT m b)
traverse a -> f b
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (m :: * -> *) a. Monad m => ML m a -> LogicT m a
fromML forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse a -> f b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Applicative m => LogicT m a -> ML m a
toML
#else
instance (Monad m, Applicative m, T.Traversable m) => T.Traversable (LogicT m) where
traverse f = fmap fromML . T.traverse f . toML
#endif
#if MIN_VERSION_base(4,8,0)
zipWithML :: MonadZip m => (a -> b -> c) -> ML m a -> ML m b -> ML m c
zipWithML :: forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> ML m a -> ML m b -> ML m c
zipWithML a -> b -> c
f = forall {m :: * -> *}. MonadZip m => ML m a -> ML m b -> ML m c
go
where
go :: ML m a -> ML m b -> ML m c
go (ML m (MLView m a)
m1) (ML m (MLView m b)
m2) =
forall (m :: * -> *) a. m (MLView m a) -> ML m a
ML forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> m a -> m b -> m c
mzipWith MLView m a -> MLView m b -> MLView m c
zv m (MLView m a)
m1 m (MLView m b)
m2
zv :: MLView m a -> MLView m b -> MLView m c
zv (a
a `ConsML` ML m a
as) (b
b `ConsML` ML m b
bs) = a -> b -> c
f a
a b
b forall (m :: * -> *) a. a -> ML m a -> MLView m a
`ConsML` ML m a -> ML m b -> ML m c
go ML m a
as ML m b
bs
zv MLView m a
_ MLView m b
_ = forall (m :: * -> *) a. MLView m a
EmptyML
unzipML :: MonadZip m => ML m (a, b) -> (ML m a, ML m b)
unzipML :: forall (m :: * -> *) a b.
MonadZip m =>
ML m (a, b) -> (ML m a, ML m b)
unzipML (ML m (MLView m (a, b))
m)
| (m (MLView m a)
l, m (MLView m b)
r) <- forall (m :: * -> *) a b. MonadZip m => m (a, b) -> (m a, m b)
munzip (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {m :: * -> *} {a} {a}.
MonadZip m =>
MLView m (a, a) -> (MLView m a, MLView m a)
go m (MLView m (a, b))
m)
= (forall (m :: * -> *) a. m (MLView m a) -> ML m a
ML m (MLView m a)
l, forall (m :: * -> *) a. m (MLView m a) -> ML m a
ML m (MLView m b)
r)
where
go :: MLView m (a, a) -> (MLView m a, MLView m a)
go MLView m (a, a)
EmptyML = (forall (m :: * -> *) a. MLView m a
EmptyML, forall (m :: * -> *) a. MLView m a
EmptyML)
go ((a
a, a
b) `ConsML` ML m (a, a)
listab)
= (a
a forall (m :: * -> *) a. a -> ML m a -> MLView m a
`ConsML` ML m a
la, a
b forall (m :: * -> *) a. a -> ML m a -> MLView m a
`ConsML` ML m a
lb)
where
{-# NOINLINE remains #-}
{-# NOINLINE la #-}
{-# NOINLINE lb #-}
remains :: (ML m a, ML m a)
remains = forall (m :: * -> *) a b.
MonadZip m =>
ML m (a, b) -> (ML m a, ML m b)
unzipML ML m (a, a)
listab
(ML m a
la, ML m a
lb) = (ML m a, ML m a)
remains
instance MonadZip m => MonadZip (LogicT m) where
mzipWith :: forall a b c.
(a -> b -> c) -> LogicT m a -> LogicT m b -> LogicT m c
mzipWith a -> b -> c
f LogicT m a
xs LogicT m b
ys = forall (m :: * -> *) a. Monad m => ML m a -> LogicT m a
fromML forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> ML m a -> ML m b -> ML m c
zipWithML a -> b -> c
f (forall (m :: * -> *) a. Applicative m => LogicT m a -> ML m a
toML LogicT m a
xs) (forall (m :: * -> *) a. Applicative m => LogicT m a -> ML m a
toML LogicT m b
ys)
munzip :: forall a b. LogicT m (a, b) -> (LogicT m a, LogicT m b)
munzip LogicT m (a, b)
xys = case forall (m :: * -> *) a b.
MonadZip m =>
ML m (a, b) -> (ML m a, ML m b)
unzipML (forall (m :: * -> *) a. Applicative m => LogicT m a -> ML m a
toML LogicT m (a, b)
xys) of
(ML m a
xs, ML m b
ys) -> (forall (m :: * -> *) a. Monad m => ML m a -> LogicT m a
fromML ML m a
xs, forall (m :: * -> *) a. Monad m => ML m a -> LogicT m a
fromML ML m b
ys)
#endif
instance MonadReader r m => MonadReader r (LogicT m) where
ask :: LogicT m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> LogicT m a -> LogicT m a
local r -> r
f (LogicT forall r. (a -> m r -> m r) -> m r -> m r
m) = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> m r -> m r
sk m r
fk -> do
r
env <- forall r (m :: * -> *). MonadReader r m => m r
ask
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f forall a b. (a -> b) -> a -> b
$ forall r. (a -> m r -> m r) -> m r -> m r
m ((forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (forall a b. a -> b -> a
const r
env) forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m r -> m r
sk) (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (forall a b. a -> b -> a
const r
env) m r
fk)
instance MonadState s m => MonadState s (LogicT m) where
get :: LogicT m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> LogicT m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
instance MonadError e m => MonadError e (LogicT m) where
throwError :: forall a. e -> LogicT m a
throwError = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
catchError :: forall a. LogicT m a -> (e -> LogicT m a) -> LogicT m a
catchError LogicT m a
m e -> LogicT m a
h = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT forall a b. (a -> b) -> a -> b
$ \a -> m r -> m r
sk m r
fk -> let
handle :: m r -> m r
handle m r
r = m r
r forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` \e
e -> forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT (e -> LogicT m a
h e
e) a -> m r -> m r
sk m r
fk
in m r -> m r
handle forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
LogicT m a -> forall r. (a -> m r -> m r) -> m r -> m r
unLogicT LogicT m a
m (\a
a -> a -> m r -> m r
sk a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. m r -> m r
handle) m r
fk