[pypy-svn] r43364 - pypy/dist/pypy/lang/prolog/interpreter
cfbolz at codespeak.net
cfbolz at codespeak.net
Mon May 14 15:20:16 CEST 2007
Author: cfbolz
Date: Mon May 14 15:20:16 2007
New Revision: 43364
Modified:
pypy/dist/pypy/lang/prolog/interpreter/engine.py
pypy/dist/pypy/lang/prolog/interpreter/term.py
Log:
simplify the unify hash thing a bit
Modified: pypy/dist/pypy/lang/prolog/interpreter/engine.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/interpreter/engine.py (original)
+++ pypy/dist/pypy/lang/prolog/interpreter/engine.py Mon May 14 15:20:16 2007
@@ -104,13 +104,14 @@
curr = curr.next
return first, copy
- def find_applicable_rule(self, query, uh1):
+ def find_applicable_rule(self, query):
#import pdb;pdb.set_trace()
while self:
- uh2 = self.rule.unify_hash
- assert len(uh1) == len(uh2)
- for j in range(len(uh1)):
- if uh1[j] != 0 and uh2[j] != 0 and uh1[j] != uh2[j]:
+ uh = self.rule.unify_hash
+ for j in range(len(uh)):
+ hash1 = uh[j]
+ hash2 = query.unify_hash_of_child(j)
+ if hash1 != 0 and hash2 != 0 and hash1 != hash2:
break
else:
return self
@@ -215,8 +216,7 @@
if function is None:
error.throw_existence_error(
"procedure", query.get_prolog_signature())
- unify_hash = query.get_deeper_unify_hash(self.heap)
- rulechain = function.rulechain.find_applicable_rule(query, unify_hash)
+ rulechain = function.rulechain.find_applicable_rule(query)
if rulechain is None:
# none of the rules apply
raise UnificationFailed()
@@ -224,7 +224,7 @@
rulechain = rulechain.next
oldstate = self.heap.branch()
while rulechain:
- rulechain = rulechain.find_applicable_rule(query, unify_hash)
+ rulechain = rulechain.find_applicable_rule(query)
if rulechain is None:
self.heap.discard(oldstate)
break
Modified: pypy/dist/pypy/lang/prolog/interpreter/term.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/interpreter/term.py (original)
+++ pypy/dist/pypy/lang/prolog/interpreter/term.py Mon May 14 15:20:16 2007
@@ -38,13 +38,13 @@
def clone_compress_vars(self, vars_new_indexes, offset):
return self
- def get_unify_hash(self, heap=None):
+ def get_unify_hash(self):
# if two non-var objects return two different numbers
# they must not be unifiable
raise NotImplementedError("abstract base class")
- def get_deeper_unify_hash(self, heap=None):
- return [self.get_unify_hash(heap)]
+ def unify_hash_of_child(self, i, heap=None):
+ raise KeyError
@specialize.arg(3)
def unify(self, other, heap, occurs_check=False):
@@ -136,13 +136,8 @@
vars_new_indexes[self.index] = index
return Var(index)
- def get_unify_hash(self, heap=None):
- if heap is None:
- return 0
- self = self.dereference(heap)
- if isinstance(self, Var):
- return 0
- return self.get_unify_hash(heap)
+ def get_unify_hash(self):
+ return 0
def contains_var(self, var, heap):
self = self.dereference(heap)
@@ -238,7 +233,7 @@
else:
raise UnificationFailed
- def get_unify_hash(self, heap=None):
+ def get_unify_hash(self):
return intmask(hash(self.name) << TAGBITS | self.TAG)
def get_prolog_signature(self):
@@ -281,7 +276,7 @@
def __repr__(self):
return "Number(%r)" % (self.num, )
- def get_unify_hash(self, heap=None):
+ def get_unify_hash(self):
return intmask(self.num << TAGBITS | self.TAG)
@@ -307,7 +302,7 @@
else:
raise UnificationFailed
- def get_unify_hash(self, heap=None):
+ def get_unify_hash(self):
#XXX no clue whether this is a good idea...
m, e = math.frexp(self.num)
m = intmask(int(m / 2 * 2 ** (32 - TAGBITS)))
@@ -339,7 +334,7 @@
else:
raise UnificationFailed
- def get_unify_hash(self, heap=None):
+ def get_unify_hash(self):
return intmask(id(self) << TAGBITS | self.TAG)
@@ -436,14 +431,11 @@
else:
return self
- def get_unify_hash(self, heap=None):
+ def get_unify_hash(self):
return intmask(hash(self.signature) << TAGBITS | self.TAG)
- def get_deeper_unify_hash(self, heap=None):
- result = [0] * len(self.args)
- for i in range(len(self.args)):
- result[i] = self.args[i].get_unify_hash(heap)
- return result
+ def unify_hash_of_child(self, i):
+ return self.args[i].get_unify_hash()
def get_prolog_signature(self):
return Term("/", [Atom.newatom(self.name), Number(len(self.args))])
@@ -456,6 +448,7 @@
class Rule(object):
+ unify_hash = []
def __init__(self, head, body):
from pypy.lang.prolog.interpreter import helper
d = {}
@@ -469,7 +462,8 @@
self.body = None
self.numvars = len(d)
self.signature = self.head.signature
- self.unify_hash = self.head.get_deeper_unify_hash()
+ if isinstance(head, Term):
+ self.unify_hash = [arg.get_unify_hash() for arg in head.args]
self._does_contain_cut()
def _does_contain_cut(self):
More information about the Pypy-commit
mailing list