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
).
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
).
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!
The empty nondeterministic value.
Equations
- Nondet.nil = { toMLList := MLList.nil }
Equations
- Nondet.instInhabited = { default := Nondet.nil }
Squash a monadic nondeterministic value to a nondeterministic value.
Equations
- Nondet.squash L = { toMLList := MLList.squash fun (x : Unit) => do let __do_lift ← L () pure __do_lift.toMLList }
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.
Equations
- Nondet.singletonM x = { toMLList := MLList.singletonM do let a ← x let __do_lift ← Lean.saveState pure (a, __do_lift) }
Convert a value to the singleton nondeterministic value.
Equations
Nondet m
is a monad.
Equations
- One or more equations did not get rendered due to their size.
Nondet m
is an alternative monad.
Equations
- One or more equations did not get rendered due to their size.
Equations
- Nondet.instMonadLift = { monadLift := fun {α : Type} => Nondet.singletonM }
Lift a list of monadic values to a nondeterministic value. We ensure that each monadic value is evaluated with the same backtrackable state.
Equations
- One or more equations did not get rendered due to their size.
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.)
Equations
- Nondet.ofList L = Nondet.ofListM (List.map pure L)
Apply a function which returns values in the monad to every alternative of a Nondet m α
.
Equations
- Nondet.mapM f L = L.bind fun (a : α) => Nondet.singletonM (f a)
Apply a function to each alternative in a Nondet m α
.
Equations
- Nondet.map f L = Nondet.mapM (fun (a : α) => pure (f a)) L
Convert a monadic optional value to a nondeterministic value.
Equations
- Nondet.ofOptionM x = Nondet.squash fun (x_1 : Unit) => do let __do_lift ← x match __do_lift with | none => pure Nondet.nil | some a => pure (Nondet.singleton a)
Convert an optional value to a nondeterministic value.
Equations
- Nondet.ofOption x = Nondet.ofOptionM (pure x)
Filter and map a nondeterministic value using a monadic function which may return none
.
Equations
- Nondet.filterMapM f L = L.bind fun (a : α) => Nondet.ofOptionM (f a)
Filter and map a nondeterministic value.
Equations
- Nondet.filterMap f L = Nondet.filterMapM (fun (a : α) => pure (f a)) L
Filter a nondeterministic value.
Equations
- Nondet.filter p L = Nondet.filterM (fun (a : α) => pure { down := p a }) L
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.
Find the value of a monadic function on the first alternative in a nondeterministic value where the function succeeds.
Equations
- L.firstM f = (Nondet.filterMapM f L).head
Convert a non-deterministic value into a list in the monad, by discarding the backtrackable state.