Trait canrun::core::Query

source ·
pub trait Query<'a> {
    fn query<Q: Reify + 'a>(
        query: Q
    ) -> Box<dyn Iterator<Item = Q::Reified> + 'a>; }
Expand description

Derive reified values potential resolved states.

Query is implemented for Goals and States, meaning you can call .query() on them with any type that implements Reify.

This is a convenient wrapper around the common pattern of obtaining a StateIter, calling .reify(query) on each state and returning only the valid, fully resolved results. Query is implemented on a variety of State related types, allowing it to be used in many contexts.

A blanket impl covers anything that implements StateIterator, so many types including Goal and State are queryable.

Required Methods§

Get reified results from things that can produce StateIters.

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

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

Most of the lower level State update methods return an Option<State>. Since StateIterator is implemented for this type, Query is as well!

let state: Option<State> = State::new().unify(&x, &Value::new(1));
let result: Vec<_> = state.query(x).collect();
assert_eq!(result, vec![1])