package choice
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=3bbb68622ce9471fbaaa8f647b0fa0f224e1d0edeb0ed99b143fcd84cbbfc7e8
md5=022db6b3d1afc27e4bb76e049c852042
doc/choice/Choice/index.html
Module Choice
Source
Backtracking monad
This is an attempt to implement the Logic monad, as described in this Haskell library or in this paper.
Some design choices are slightly different, since OCaml is strict. Performance may also vary from ghc-compiled code, since it performs some optimizations like deforestation.
Example
We adapt the example from the LogicT paper.
let rec insert e l = match l with
| [] -> return [e]
| x::l' ->
mplus
(return (e :: l))
(insert e l' >>= fun l'' -> return (x :: l''));;
let rec permute = function
| [] -> return []
| x::l ->
permute l >>= fun l' ->
insert x l';;
let rec sorted l = match l with
| [] | [_] -> true
| x::((y::l') as l) ->
x <= y && sorted l;;
let bogosort l =
once (filter sorted (permute l));;
Then, running
run_n 1 (bogosort [2;3;5;1;0]);;
yields
- : int list list = [[0; 1; 2; 3; 5]]
A choice among values of type 'a
Combinators
Multiple returns. Each element of the list is a candidate for success.
Call the function to get alternative choices. Example:
let r = ref 0 in Choice.run_n 10
(Choice.filter
(Choice.from_fun (fun () -> incr r; Some !r)) (fun x -> x mod 3 = 0));;
yields
- : int list = [30; 27; 24; 21; 18; 15; 12; 9; 6; 3]
Delay the computation (the closure will be called in each branch that uses the choice point
Monadic bind. Each solution of the first argument is given to the function, that may in turn return several choices.
Same as mplus
, but fair, ie it enumerates solutions alternatively from its first and second arguments.
ite cond th el
enumerates the choices of cond
. If cond
fails, then it behaves like el
, otherwise each solution of cond
is given to th
.
Special case of bind
, with only zero or one possible output choices for each input choice.
Only keep the solutions that satisfy the given predicate.
Enumerate solutions
Enumerate solutions, until none remains, or the callback returns false
to signal it has had enough solutions
Monadic operators
Infix operators
Infix version of interleave