1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::counter::Counter;
use crate::error::RuntimeError;
use crate::events::QueryEvent;
use crate::runnable::Runnable;
use crate::terms::{Operation, Operator, Pattern, Symbol, Term, Value};

use std::collections::HashSet;

fn path(x: &Term) -> Vec<Term> {
    match x.value() {
        Value::Expression(Operation {
            operator: Operator::Dot,
            args,
        }) => [vec![args[0].clone()], path(&args[1])].concat(),
        _ => vec![x.clone()],
    }
}

#[derive(Clone)]
pub struct IsaConstraintCheck {
    existing: Vec<Operation>,
    proposed: Operation,
    result: Option<bool>,
    alternative_check: Option<QueryEvent>,
    last_call_id: u64,
    proposed_names: HashSet<Symbol>,
}

enum Check {
    None,
    One(QueryEvent),
    Two(QueryEvent, QueryEvent),
}

impl IsaConstraintCheck {
    pub fn new(
        existing: Vec<Operation>,
        proposed: Operation,
        proposed_names: HashSet<Symbol>,
    ) -> Self {
        Self {
            existing,
            proposed,
            result: None,
            alternative_check: None,
            last_call_id: 0,
            proposed_names,
        }
    }

    /// Check if existing constraints are compatible with the proposed constraint.
    ///
    /// If either the existing or proposed constraint is not a type constraint or if they are
    /// constraints for the same type, there's no external check required, and we return `None` to
    /// indicate compatibility.
    ///
    /// Otherwise, we return a collection of `QueryEvent`s to check whether the type
    /// constraints are compatible. The constraints are compatible if either of their types is a
    /// subclass of the other's.
    ///
    /// Returns:
    /// Zero, one or two query events.
    fn check_constraint(&mut self, constraint: Operation, counter: &Counter) -> Check {
        // TODO(gj): check non-`Isa` constraints, e.g., `(Unify, partial, 1)` against `(Isa,
        // partial, Integer)`.
        if constraint.operator != Operator::Isa {
            return Check::None;
        }

        let constraint_path = path(&constraint.args[0]);
        let proposed_path = path(&self.proposed.args[0]);

        // Not comparable b/c one of the matches statements has a LHS that isn't a variable or dot
        // op.
        if constraint_path.is_empty() || proposed_path.is_empty() {
            return Check::None;
        }

        let just_vars = constraint_path.len() == 1
            && proposed_path.len() == 1
            && matches!(&constraint.args[0].value().as_symbol(), Ok(Symbol(_)))
            && matches!(&self.proposed.args[0].value().as_symbol(), Ok(Symbol(_)));

        // FIXME(gw): this logic is hard to follow!
        if just_vars {
            let sym = constraint.args[0].value().as_symbol().unwrap();
            if !self.proposed_names.contains(sym) {
                return Check::None;
            }
        } else if constraint_path
            // a.b.c vs. d
            .iter()
            .zip(proposed_path.iter())
            .any(|(a, b)| a != b)
        // FIXME(gw): is this right? what if the first elements are aliases?
        {
            return Check::None;
        }

        let existing = constraint.args.last().unwrap();

        if constraint_path == proposed_path {
            // x matches A{} vs. x matches B{}
            self.subclass_compare(existing, counter)
        } else if constraint_path.len() < proposed_path.len() {
            // Proposed path is a superset of existing path.
            self.path_compare(proposed_path, constraint_path, existing, counter)
        } else if just_vars {
            self.subclass_compare(existing, counter)
        } else {
            // Comparing existing `x.a.b matches B{}` vs. `proposed x.a matches A{}`.
            Check::None
        }
    }

    fn subclass_compare(&mut self, existing: &Term, counter: &Counter) -> Check {
        let proposed = self.proposed.args.last().unwrap();
        match (proposed.value(), existing.value()) {
            (
                Value::Pattern(Pattern::Instance(proposed)),
                Value::Pattern(Pattern::Instance(existing)),
            ) if proposed.tag != existing.tag => {
                let call_id = counter.next();
                self.last_call_id = call_id;

                Check::Two(
                    QueryEvent::ExternalIsSubclass {
                        call_id,
                        left_class_tag: proposed.tag.clone(),
                        right_class_tag: existing.tag.clone(),
                    },
                    QueryEvent::ExternalIsSubclass {
                        call_id,
                        left_class_tag: existing.tag.clone(),
                        right_class_tag: proposed.tag.clone(),
                    },
                )
            }
            _ => Check::None,
        }
    }

    fn path_compare(
        &mut self,
        proposed_path: Vec<Term>,
        constraint_path: Vec<Term>,
        existing: &Term,
        counter: &Counter,
    ) -> Check {
        // given `a.b matches B{}` and `a.b.c.d matches D{}`, we want to assemble an
        // `ExternalIsaWithPath` of `B`, [c, d], and `D`.
        let proposed = self.proposed.args.last().unwrap();
        match (proposed.value(), existing.value()) {
            (
                Value::Pattern(Pattern::Instance(proposed)),
                Value::Pattern(Pattern::Instance(existing)),
            ) => {
                let call_id = counter.next();
                self.last_call_id = call_id;
                Check::One(QueryEvent::ExternalIsaWithPath {
                    call_id,
                    base_tag: existing.tag.clone(),
                    path: proposed_path[constraint_path.len()..].to_vec(),
                    class_tag: proposed.tag.clone(),
                })
            }
            _ => Check::None,
        }
    }
}

impl Runnable for IsaConstraintCheck {
    fn run(&mut self, counter: Option<&mut Counter>) -> Result<QueryEvent, RuntimeError> {
        if let Some(result) = self.result.take() {
            if result {
                // If the primary check succeeds, there's no need to check the alternative.
                self.alternative_check = None;
            } else if self.alternative_check.is_none() {
                // If both checks fail, we fail.
                return Ok(QueryEvent::Done { result: false });
            }
        }

        // If there's an alternative waiting to be checked, check it.
        if let Some(alternative) = self.alternative_check.take() {
            return Ok(alternative);
        }

        let counter = counter.expect("IsaConstraintCheck requires a Counter");
        loop {
            match self.existing.pop() {
                None => return Ok(QueryEvent::Done { result: true }),
                Some(constraint) => match self.check_constraint(constraint, counter) {
                    Check::None => (),
                    Check::One(a) => return Ok(a),
                    Check::Two(a, b) => {
                        self.alternative_check = Some(b);
                        return Ok(a);
                    }
                },
            }
        }
    }

    fn external_question_result(&mut self, call_id: u64, answer: bool) -> Result<(), RuntimeError> {
        if call_id != self.last_call_id {
            return Err(RuntimeError::InvalidState {
                msg: String::from("Unexpected call id"),
            });
        }

        self.result = Some(answer);
        Ok(())
    }

    fn clone_runnable(&self) -> Box<dyn Runnable> {
        Box::new(self.clone())
    }
}