def unify(x, y, s={}): """Unify expressions x,y with substitution s; return a substitution that would make x,y equal, or None if x,y can not unify. x and y can be variables (e.g. Expr('x')), constants, lists, or Exprs. [Figure 9.1] >>> unify(x, 3, {}) {x: 3} """ if s is None: return None elif x == y: return s elif is_variable(x): return unify_var(x, y, s) elif is_variable(y): return unify_var(y, x, s) elif isinstance(x, Expr) and isinstance(y, Expr): return unify(x.args, y.args, unify(x.op, y.op, s)) elif isinstance(x, str) or isinstance(y, str): return None elif issequence(x) and issequence(y) and len(x) == len(y): if not x: return s return unify(x[1:], y[1:], unify(x[0], y[0], s)) else: return None
def unify(x, y, s={}): """Unify expressions x,y with substitution s; return a substitution that would make x,y equal, or None if x,y can not unify. x and y can be variables (e.g. Expr('x')), constants, lists, or Exprs. [Fig. 9.1] >>> unify(P(x,y), P(a,b), {}) {y: b, x: a} """ if trace: print "exp1:", x, " exp2:", y, " mu = ", s, "\n" if s == None: return None elif x == y: return s # if any of therm is variable unify it with it's correspondent elif is_variable(x): return unify_var(x, y, s) elif is_variable(y): return unify_var(y, x, s) # if they are expression elif isinstance(x, Expr) and isinstance(y, Expr): return unify(x.args, y.args, unify(x.op, y.op, s)) # if atom elif isinstance(x, str) or isinstance(y, str) or not x or not y: return s if x == y else None # continue unification elif issequence(x) and issequence(y) and len(x) == len(y): return unify(x[1:], y[1:], unify(x[0], y[0], s)) else: return None
def subset(self, indices): '''Return a subset of the NumPy array. @param indices: Row indices (ints). @note: A I{subset} is different from a I{slice} in 2 ways: (a) the I{subset} is typically specified as a list of (un-)ordered indices and (b) the I{subset} allocates a new, separate NumPy array while a I{slice} is just an other I{view} of the original NumPy array. @return: Sub-array (numpy.array). @raise IndexError: Out of range indices value. @raise TypeError: If indices is not a I{range} or a I{list} of ints. ''' if not issequence(indices, tuple): # NO tuple, only list # and range work properly to get Numpy array sub-sets raise TypeError('%s invalid: %s' % ('indices', type(indices))) n = len(self) for i, v in enumerate(indices): if not isint(v): raise TypeError('%s[%s] invalid: %r' % ('indices', i, v)) elif not 0 <= v < n: raise IndexError('%s[%s] invalid: %r' % ('indices', i, v)) return self._subset(indices)
def unify(x, y, s): if s is None: return None elif x == y: return s elif is_variable(x): return unify_var(x, y, s) elif is_variable(y): return unify_var(y, x, s) elif isinstance(x, Expr) and isinstance(y, Expr): return unify(x.args, y.args, unify(x.op, y.op, s)) elif isinstance(x, str) or isinstance(y, str): return None elif issequence(x) and issequence(y) and len(x) == len(y): if not x: return s return unify(x[1:], y[1:], unify(x[0], y[0], s)) else: return None
def unify(x, y, s): """Unify expressions x,y with substitution s; return a substitution that would make x,y equal, or None if x,y can not unify. x and y can be variables (e.g. Expr('x')), constants, lists, or Exprs. [Figure 9.1]""" if s is None: return None elif x == y: return s elif is_variable(x): return unify_var(x, y, s) elif is_variable(y): return unify_var(y, x, s) elif isinstance(x, Expr) and isinstance(y, Expr): return unify(x.args, y.args, unify(x.op, y.op, s)) elif isinstance(x, str) or isinstance(y, str): return None elif issequence(x) and issequence(y) and len(x) == len(y): if not x: return s return unify(x[1:], y[1:], unify(x[0], y[0], s)) else: return None
def occur_check(var, x): """ Return true if var occurs anywhere in x. """ # if both are equal if var == x: return True # if they are not and one the operands are equal or it's in occures in its variables elif isinstance(x, Expr): return var.op == x.op or occur_check(var, x.args) # if it's list of arguments check every one elif not isinstance(x, str) and issequence(x): for xi in x: if occur_check(var, xi): return True return False