A nondeterminism monad. #
We represent nondeterministic values in a type α
as a single field structure containing an
MLList m (σ × α)
, i.e. as a monadic lazy list of possible values,
each equipped with the backtrackable state
required to run further computations in the ambient monad.
We provide an Alternative
Monad
instance, as well as functions bind
, mapM
, and filterMapM
,
and functions singletonM
, ofListM
, ofOptionM
, and firstM
for entering and leaving the nondeterministic world.
Operations on the nondeterministic value via bind
, mapM
, and filterMapM
run with the appropriate backtrackable state, and are responsible for updating the state themselves
(typically this doesn't need to be done explicitly,
but just happens as a side effect in the monad m
).
Convert a non-deterministic value into a lazy list, keeping the backtrackable state. Be careful that monadic operations on the
MLList
will not respect this state!
Nondet m α
is variation on MLList m α
suitable for use with backtrackable monads m
.
We think of Nondet m α
as a nondeterministic value in α
,
with the possible alternatives stored in a monadic lazy list.
Along with each a : α
we store the backtrackable state, and ensure that monadic operations
on alternatives run with the appropriate state.
Operations on the nondeterministic value via bind
, mapM
, and filterMapM
run with the appropriate backtrackable state, and are responsible for updating the state themselves
(typically this doesn't need to be done explicitly,
but just happens as a side effect in the monad m
).
Instances For
The empty nondeterministic value.
Instances For
Bind a nondeterministic function over a nondeterministic value, ensuring the function is run with the relevant backtrackable state at each value.
Convert any value in the monad to the singleton nondeterministic value.
Instances For
Convert a value to the singleton nondeterministic value.
Instances For
Nondet m
is a monad.
Nondet m
is an alternative monad.
Lift a list of monadic values to a nondeterministic value. We ensure that each monadic value is evaluated with the same backtrackable state.
Instances For
Lift a list of values to a nondeterministic value. (The backtrackable state in each will be identical: whatever the state was when we first read from the result.)
Instances For
Convert a monadic optional value to a nondeterministic value.
Instances For
Convert an optional value to a nondeterministic value.
Instances For
All iterations of a non-deterministic function on an initial value.
(That is, depth first search.)
Find the first alternative in a nondeterministic value, as a monadic value.
Instances For
Find the value of a monadic function on the first alternative in a nondeterministic value where the function succeeds.
Instances For
Convert a non-deterministic value into a lazy list, by discarding the backtrackable state.
Instances For
Convert a non-deterministic value into a list in the monad, by discarding the backtrackable state.