Function canrun::either

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

Create a goal that succeeds if either sub-goal succeed.

This is essentially an “OR” operation, and will eventually lead to zero, one or two resolved states, depending on the success or failure of the sub-goals.

Examples

Two successful goals will yield up two different results:

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

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

One failing goal will not cause the other to fail:

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

Both goals can fail, leading to no results:

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