Copyright | (c) 2007-2014 Dan Doel (c) 2011-2013 Edward Kmett (c) 2014 Roman Cheplyaka (c) 2020-2021 Andrew Lelechenko (c) 2020-2021 Kevin Quick |
---|---|
License | BSD3 |
Maintainer | Andrew Lelechenko <andrew.lelechenko@gmail.com> |
Safe Haskell | Safe |
Language | Haskell2010 |
Adapted from the paper
Backtracking, Interleaving, and Terminating Monad Transformers
by Oleg Kiselyov, Chung-chieh Shan, Daniel P. Friedman, Amr Sabry.
Note that the paper uses MonadPlus
vocabulary
(mzero
and mplus
),
while examples below prefer empty
and <|>
from Alternative
.
Synopsis
- module Control.Monad.Logic.Class
- type Logic = LogicT Identity
- logic :: (forall r. (a -> r -> r) -> r -> r) -> Logic a
- runLogic :: Logic a -> (a -> r -> r) -> r -> r
- observe :: Logic a -> a
- observeMany :: Int -> Logic a -> [a]
- observeAll :: Logic a -> [a]
- newtype LogicT m a = LogicT {
- unLogicT :: forall r. (a -> m r -> m r) -> m r -> m r
- runLogicT :: LogicT m a -> (a -> m r -> m r) -> m r -> m r
- observeT :: MonadFail m => LogicT m a -> m a
- observeManyT :: Monad m => Int -> LogicT m a -> m [a]
- observeAllT :: Applicative m => LogicT m a -> m [a]
- module Control.Monad
- module Control.Monad.Trans
Documentation
module Control.Monad.Logic.Class
The Logic monad
logic :: (forall r. (a -> r -> r) -> r -> r) -> Logic a Source #
A smart constructor for Logic
computations.
runLogic :: Logic a -> (a -> r -> r) -> r -> r Source #
Runs a Logic
computation with the specified initial success and
failure continuations.
>>>
runLogic empty (+) 0
0
>>>
runLogic (pure 5 <|> pure 3 <|> empty) (+) 0
8
observe :: Logic a -> a Source #
Extracts the first result from a Logic
computation, failing if
there are no results.
>>>
observe (pure 5 <|> pure 3 <|> empty)
5
>>>
observe empty
*** Exception: No answer.
observeMany :: Int -> Logic a -> [a] Source #
Extracts up to a given number of results from a Logic
computation.
>>>
let nats = pure 0 <|> fmap (+ 1) nats
>>>
observeMany 5 nats
[0,1,2,3,4]
observeAll :: Logic a -> [a] Source #
Extracts all results from a Logic
computation.
>>>
observe (pure 5 <|> empty <|> empty <|> pure 3 <|> empty)
[5,3]
The LogicT monad transformer
A monad transformer for performing backtracking computations
layered over another monad m
.
Instances
runLogicT :: LogicT m a -> (a -> m r -> m r) -> m r -> m r Source #
Runs a LogicT
computation with the specified initial success and
failure continuations.
The second argument ("success continuation") takes one result of
the LogicT
computation and the monad to run for any subsequent
matches.
The third argument ("failure continuation") is called when the
LogicT
cannot produce any more results.
For example:
>>>
yieldWords = foldr ((<|>) . pure) empty
>>>
showEach wrd nxt = putStrLn wrd >> nxt
>>>
runLogicT (yieldWords ["foo", "bar"]) showEach (putStrLn "none!")
foo bar none!>>>
runLogicT (yieldWords []) showEach (putStrLn "none!")
none!>>>
showFirst wrd _ = putStrLn wrd
>>>
runLogicT (yieldWords ["foo", "bar"]) showFirst (putStrLn "none!")
foo
observeT :: MonadFail m => LogicT m a -> m a Source #
Extracts the first result from a LogicT
computation,
failing if there are no results at all.
observeManyT :: Monad m => Int -> LogicT m a -> m [a] Source #
Extracts up to a given number of results from a LogicT
computation.
observeAllT :: Applicative m => LogicT m a -> m [a] Source #
Extracts all results from a LogicT
computation, unless blocked by the
underlying monad.
For example, given
>>>
let nats = pure 0 <|> fmap (+ 1) nats
some monads (like Identity
, Reader
,
Writer
, and State
)
will be productive:
>>>
take 5 $ runIdentity (observeAllT nats)
[0,1,2,3,4]
but others (like ExceptT
,
and ContT
) will not:
>>>
take 20 <$> runExcept (observeAllT nats)
In general, if the underlying monad manages control flow then
observeAllT
may be unproductive under infinite branching,
and observeManyT
should be used instead.
module Control.Monad
module Control.Monad.Trans