Trait canrun::Query[][src]

pub trait Query<'a, D: Domain<'a> + 'a> {
    fn query<Q>(self, query: Q) -> Box<dyn Iterator<Item = Q::Reified> + 'a>
        Q: ReifyIn<'a, D> + 'a
; }
Expand description

Derive reified values potential resolved states.

Query is implemented for Goals and States (and other types), meaning you can call .query() on them with any type that implements ReifyIn for a matching Domain.

This is a convenient wrapper around the pattern of iterating over a sequence of ResolvedStates and calling state.reify(query) 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 IterResolved, so many types including Goal and State are queryable.

Required methods

Get reified results from an iterator of ResolvedStates.



use canrun::{Goal, unify, var, State};
use canrun::example::I32;

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


use canrun::{State, IterResolved, Query, val, var};
use canrun::example::I32;
let x = var();

let state: State<I32> = State::new();
let result: Vec<_> = state.query(x).collect();
assert_eq!(result, vec![]) // Nothing has been added to the State


Note that most of the lower level State update methods return an Option<State<D>>. Since IterResolved is implemented for this type, Query is as well!

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