[pypy-svn] r22560 - pypy/dist/pypy/lib/logic
auc at codespeak.net
auc at codespeak.net
Tue Jan 24 10:25:34 CET 2006
Author: auc
Date: Tue Jan 24 10:25:32 2006
New Revision: 22560
Modified:
pypy/dist/pypy/lib/logic/test_unification.py
pypy/dist/pypy/lib/logic/test_variable.py
pypy/dist/pypy/lib/logic/unification.py
Log:
* remove bogus test
* more comments in code
* bugs fixed (double successful unification must be ok)
Modified: pypy/dist/pypy/lib/logic/test_unification.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_unification.py (original)
+++ pypy/dist/pypy/lib/logic/test_unification.py Tue Jan 24 10:25:32 2006
@@ -63,7 +63,9 @@
u.bind(y, z)
u.unify(x, y)
assert z.val == 42
- #raises(u.UnificationFailure, u.unify, x, y)
+ u.unify(x, y)
+ assert (z.val == x.val) and (x.val == y.val)
+
def test_unify_values(self):
x, y = u.var('x'), u.var('y')
@@ -187,37 +189,6 @@
(t1.raised and not t2.raised)
- def test_threads_unifying_vars(self):
- x, y, z = u.var('x'), u.var('y'), u.var('z')
- l1 = range(999)
- l2 = range(999)
- l1[-1] = z
- l2[0] = z
- l2[-1] = 0
- u.bind(x, l1)
- u.bind(y, l2)
-
- def do_unify(thread, v1, v2):
- thread.raised = False
- print thread
- try:
- u.unify(v1, v2)
- except u.UnificationFailure:
- thread.raised = True
- print thread
-
- t1, t2 = (FunThread(do_unify, x, y),
- FunThread(do_unify, x, y))
- t1.start()
- t2.start()
- t1.join()
- t2.join()
- print "Z", z
- assert (t2.raised and not t1.raised) or \
- (t1.raised and not t2.raised)
- assert z.val == 0
-
-
def test_set_var_domain(self):
x = u.var('x')
u.set_domain(x, [1, 3, 5])
Modified: pypy/dist/pypy/lib/logic/test_variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_variable.py (original)
+++ pypy/dist/pypy/lib/logic/test_variable.py Tue Jan 24 10:25:32 2006
@@ -25,9 +25,9 @@
u._store = u.Store()
-## def test_no_same_name(self):
-## x = u.var('x')
-## raises(v.AlreadyExists, u.var, 'x')
+ def test_no_same_name(self):
+ x = u.var('x')
+ raises(v.AlreadyExists, u.var, 'x')
def test_one_thread_reading_one_var(self):
cons = Consumer()
Modified: pypy/dist/pypy/lib/logic/unification.py
==============================================================================
--- pypy/dist/pypy/lib/logic/unification.py (original)
+++ pypy/dist/pypy/lib/logic/unification.py Tue Jan 24 10:25:32 2006
@@ -178,7 +178,7 @@
self.contraints = {}
# consistency-preserving stuff
self.in_transaction = False
- self.lock = threading.Lock()
+ self.lock = threading.RLock()
def add_unbound(self, var):
# register globally
@@ -221,6 +221,7 @@
else: # 1. both are unbound
self._merge(var, val)
else: # 3. val is really a value
+ print "%s, is that you ?" % var
if var._is_bound():
raise AlreadyBound(var.name)
self._bind(var.val, val)
@@ -278,13 +279,15 @@
def _really_unify(self, x, y):
# print "unify %s with %s" % (x,y)
if not _unifiable(x, y): raise UnificationFailure(x, y)
- # dispatch to the apropriate unifier
if not x in self.vars:
if not y in self.vars:
+ # duh ! x & y not vars
if x != y: raise UnificationFailure(x, y)
else: return
+ # same call, reverse args. order
self._unify_var_val(y, x)
elif not y in self.vars:
+ # x is Var, y a value
self._unify_var_val(x, y)
elif _both_are_bound(x, y):
self._unify_bound(x,y)
@@ -294,7 +297,6 @@
self.bind(y,x)
def _unify_var_val(self, x, y):
- #if x._is_bound(): raise UnificationFailure(x, y)
if x.val != y:
try:
self.bind(x, y)
@@ -309,10 +311,11 @@
elif type(vx) is dict and isinstance(vy, type(vx)):
self._unify_mapping(x, y)
else:
- raise UnificationFailure(x, y)
+ if vx != vy:
+ raise UnificationFailure(x, y)
def _unify_iterable(self, x, y):
- # print "unify sequences %s %s" % (x, y)
+ print "unify sequences %s %s" % (x, y)
vx, vy = (x.val, y.val)
idx, top = (0, len(vx))
while (idx < top):
More information about the Pypy-commit
mailing list