From 72eeedec746fb8eea40404b11bce0a05a58117d4 Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Tue, 28 Mar 2017 17:38:01 -0700 Subject: [PATCH] Part 4: add OrJoin::is_fully_unified. This allows us to tell if all the variables in a valid `or` join are to be unified, which is necessary for simple joins. --- query/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/query/src/lib.rs b/query/src/lib.rs index 6fbff8f0..40ddb06b 100644 --- a/query/src/lib.rs +++ b/query/src/lib.rs @@ -580,6 +580,24 @@ pub struct FindQuery { // TODO: in_rules; } +impl OrJoin { + /// Return true if either the `OrJoin` is `UnifyVars::Implicit`, or if + /// every variable mentioned inside the join is also mentioned in the `UnifyVars` list. + pub fn is_fully_unified(&self) -> bool { + match &self.unify_vars { + &UnifyVars::Implicit => true, + &UnifyVars::Explicit(ref vars) => { + // We know that the join list must be a subset of the vars in the pattern, or + // it would have failed validation. That allows us to simply compare counts here. + // TODO: in debug mode, do a full intersection, and verify that our count check + // returns the same results. + let mentioned = self.collect_mentioned_variables(); + vars.len() == mentioned.len() + } + } + } +} + pub trait ContainsVariables { fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet); fn collect_mentioned_variables(&self) -> BTreeSet {