Function canrun::goals::both

source ·
pub fn both(a: impl Goal, b: impl Goal) -> Both
Expand description

Create a goal that only succeeds if both sub-goals succeed.

This is essentially an “AND” operation. The resulting state will be the result of the combining the two sub-goals.

If the first goal fails, the second goal will not be attempted.

Examples

Two successful goals allow values to flow between vars:

use canrun::{both, unify, LVar, Query};

let x = LVar::new();
let y = LVar::new();
let goal = both(unify(y, x), unify(1, x));
let result: Vec<_> = goal.query(x).collect();
assert_eq!(result, vec![1])

A failing goal will cause the entire goal to fail:

let goal = both(unify(2, x), unify(1, x));
let result: Vec<_> = goal.query(x).collect();
assert_eq!(result, vec![]) // Empty result