Function canrun::custom

source ·
pub fn custom<F>(func: F) -> Customwhere
    F: Fn(State) -> Option<State> + 'static,
Expand description

Create a goal that gives access to the underlying State struct.

Similar to lazy, the passed in callback is given access to the state so it can call the lower level State manipulation methods. This should approach should be used sparingly. Ideally most logic should be composable out of lower level primitive goals.

Because the State methods return an Option<[State]> the question mark operator ? can be used to allow chaining operations on the State.

Examples

use canrun::{custom, LVar, Query};

let x = LVar::new();
let goal = custom(move |state| {
    let y = LVar::new();
    state.unify(&y.into(), &1.into())?
         .unify(&x.into(), &y.into())
});
let result: Vec<_> = goal.query(x).collect();
assert_eq!(result, vec![1])