public override bool Unify(BaseTerm t, VarStack varStack) { if ((t = t.ChainEnd()) is Variable) // t not unified { ((Variable)t).Bind(this); varStack.Push(t); return(true); } if (t is ListPatternTerm && arity == t.Arity) // two ListPatternTerms match if their rangeTerms match { for (int i = 0; i < arity; i++) { if (!args[i].Unify(t.Args[i], varStack)) { return(false); } } return(true); } if (t is ListTerm) { pattern = args; // pattern is searched ... target = ((ListTerm)t).ToList(); // ... in target int ip = 0; int it = 0; return(UnifyTailEx(ip, it, varStack)); } return(false); }
public Variable DeclareGlobalVariable(string name, DObject val) { if (variables.ContainsKey(name)) { throw new InterpreterException(interpreter.CurrentToken, "Global variable already exists: " + name); } var stack = new VarStack(); var v = new Variable(val, -1); stack.Push(v); variables.Add(name, stack); return(v); }
public ReadOnlyVariable DeclareReadOnly(string name, DObject val) { var v = new ReadOnlyVariable(val, interpreter.depth); if (!variables.ContainsKey(name)) { var stack = new VarStack(); stack.Push(v); variables.Add(name, stack); } variables[name].Push(v); return(v); }
public Variable DeclareLocal(string name, DObject val) { val = val == null ? DUndefined.instance : val; var v = new Variable(val, interpreter.depth); if (!variables.ContainsKey(name)) { var stack = new VarStack(); stack.Push(v); variables.Add(name, stack); } variables[name].Push(v); return(v); }
// UNIFICATION // The stack is used to store the variables and choice points that are bound // by the unification. This is required for backtracking. Unify does not do // any unbinding in case of failure. This will be done during backtracking. // refUnifyCount: can be used for calculating the 'cost' of a predicate Call public virtual bool Unify(BaseTerm t, VarStack varStack) { NextUnifyCount(); if (t.IsUnified) { return(this.Unify(t.ChainEnd(), varStack)); } if (t is Variable) // t not unified { ((Variable)t).Bind(this); varStack.Push(t); return(true); } if (t is ListPatternTerm) { return(t.Unify(this, varStack)); } if (termType != t.termType) { return(false); // gives a slight improvement } if (functor.Equals(t.functor) && arity == t.arity) { for (int i = 0; i < arity; i++) { if (!args[i].Unify(t.args[i], varStack)) { return(false); } } return(true); } return(false); }