[pypy-svn] r31231 - in pypy/dist/pypy/objspace: . cclp cclp/constraint cclp/constraint/test test

auc at codespeak.net auc at codespeak.net
Thu Aug 10 16:08:20 CEST 2006


Author: auc
Date: Thu Aug 10 16:08:15 2006
New Revision: 31231

Added:
   pypy/dist/pypy/objspace/cclp/constraint/variable.py
   pypy/dist/pypy/objspace/cclp/interp_var.py
Modified:
   pypy/dist/pypy/objspace/cclp/constraint/domain.py
   pypy/dist/pypy/objspace/cclp/constraint/test/test_fd.py
   pypy/dist/pypy/objspace/cclp/thread.py
   pypy/dist/pypy/objspace/cclp/thunk.py
   pypy/dist/pypy/objspace/cclp/types.py
   pypy/dist/pypy/objspace/cclp/variable.py
   pypy/dist/pypy/objspace/logic.py
   pypy/dist/pypy/objspace/test/test_logicobjspace.py
Log:
split files, cleanup


Modified: pypy/dist/pypy/objspace/cclp/constraint/domain.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/domain.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/domain.py	Thu Aug 10 16:08:15 2006
@@ -7,7 +7,8 @@
 
 from pypy.objspace.std.model import StdObjSpaceMultiMethod
 
-from pypy.objspace.cclp.types import W_AbstractDomain
+from pypy.objspace.cclp.types import W_AbstractDomain, W_Var
+from pypy.objspace.cclp.interp_var import interp_bind
 
 all_mms = {}
 
@@ -31,6 +32,27 @@
         self._values = space.newdict([])
         self.set_values(w_values)
 
+
+    def clear_change(self):
+        assert not isinstance(self._changed.w_bound_to, W_Var)
+        self._changed = W_Var(self._space)
+
+    def give_synchronizer(self):
+        return self._changed
+
+    def _value_removed(self):
+        """The implementation of remove_value should call this method"""
+        interp_bind(self._space, self._changed, True)
+        self.clear_change()
+        
+        if self.size() == 0:
+            raise OperationError(self._space.w_RuntimeError,
+                                 self._space.wrap('ConsistencyFailure'))
+
+##     def w__del__(self):
+##         self._space.bind(self._changed, self._space.newbool(False))
+
+
     def set_values(self, w_values):
         """Objects in the value set can't be unwrapped unless we
         specialize on specific types - this might need speccialization
@@ -89,6 +111,8 @@
         return not self == w_other
 
 
+
+
 # function bolted into the space to serve as constructor
 def make_fd(space, w_values):
     assert isinstance(w_values, W_ListObject)

Modified: pypy/dist/pypy/objspace/cclp/constraint/test/test_fd.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/test/test_fd.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/test/test_fd.py	Thu Aug 10 16:08:15 2006
@@ -13,13 +13,9 @@
 
     def test_remove_value(self):
         fd = FiniteDomain([1, 2, 3])
-        Sync = fd.give_synchronizer()
-        def foo(fd):
-            fd.remove_value(2)
-            assert fd.size() == 2
-            assert set(fd.get_values()) == set([1, 3])
-        stacklet(foo, fd)
-        assert Sync == True
+        fd.remove_value(2)
+        assert fd.size() == 2
+        assert set(fd.get_values()) == set([1, 3])
         
     def test_remove_all_values(self):
         fd = FiniteDomain([3])
@@ -28,21 +24,14 @@
         
     def test_remove_values(self):
         fd = FiniteDomain([1, 2, 3])
-        Sync = fd.give_synchronizer()
-        def foo(fd):
-            fd.remove_values([1, 2])
-            assert fd.size() == 1
-            assert set(fd.get_values()) == set([3,])
-        stacklet(foo, fd)
-        assert Sync == True
+        fd.remove_values([1, 2])
+        assert fd.size() == 1
+        assert set(fd.get_values()) == set([3,])
 
     def test_remove_values_empty_list(self):
         fd = FiniteDomain([1, 2, 3])
-        Sync = fd.give_synchronizer()
-        assert is_free(Sync)
         fd.remove_values([])
         assert fd.size() == 3
-        assert is_free(Sync)
 
     def test_intersection(self):
         """not used for now"""

Added: pypy/dist/pypy/objspace/cclp/constraint/variable.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cclp/constraint/variable.py	Thu Aug 10 16:08:15 2006
@@ -0,0 +1,53 @@
+from pypy.interpreter import gateway, baseobjspace
+from pypy.objspace.std.listobject import W_ListObject
+from pypy.objspace.std.stringobject import W_StringObject
+
+from pypy.objspace.cclp.constraint.domain import W_FiniteDomain
+
+from pypy.objspace.cclp.types import deref, W_Var, W_CVar
+from pypy.objspace.cclp.variable import bind_mm, raise_unification_failure, _alias, \
+     _assign_aliases, bind__Var_Root, bind__Var_Var
+from pypy.objspace.cclp.misc import w
+
+W_Root = baseobjspace.W_Root
+
+def domain(space, w_values, w_name):
+    assert isinstance(w_values, W_ListObject)
+    assert isinstance(w_name, W_StringObject)
+    w_dom = W_FiniteDomain(space, w_values)
+    w_var = W_CVar(space, w_dom, w_name)
+    w("CVAR", str(w_var))
+    return w_var
+app_domain = gateway.interp2app(domain)
+
+
+def bind__CVar_Root(space, w_cvar, w_obj):
+    #XXX we should (want to) be able to test membership
+    #    in a wrapped against wrappeds into a non-wrapped dict
+    if [True for elt in w_cvar.w_dom._values.content
+        if space.is_true(space.eq(w_obj, elt))]:
+        return bind__Var_Root(space, w_cvar, w_obj)
+    raise_unification_failure(space, "value not in variable domain")
+
+def bind__CVar_CVar(space, w_cvar1, w_cvar2):
+    w_inter_dom = space.intersection(w_cvar1.w_dom, w_cvar2.w_dom)
+    if w_inter_dom.__len__() > 0:
+        if w_inter_dom.__len__() == 1:
+            w_value = w_inter_dom.get_values()[0]
+            _assign_aliases(space, w_cvar1, w_value)
+            _assign_aliases(space, w_cvar2, w_value)
+        else:
+            w_cvar1.w_dom = w_cvar2.w_dom = w_inter_dom
+            _alias(space, w_cvar1, w_cvar2)
+    else:
+        raise_unification_failure(space, "incompatible domains")
+
+def bind__CVar_Var(space, w_cvar, w_var):
+    if space.is_true(space.is_bound(w_var)):
+        return bind__CVar_Root(space, w_cvar, w_var)
+    return bind__Var_Var(space, w_cvar, w_var)
+
+
+bind_mm.register(bind__CVar_CVar, W_CVar, W_CVar)
+bind_mm.register(bind__CVar_Root, W_CVar, W_Root)
+bind_mm.register(bind__CVar_Var, W_CVar, W_Var)

Added: pypy/dist/pypy/objspace/cclp/interp_var.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cclp/interp_var.py	Thu Aug 10 16:08:15 2006
@@ -0,0 +1,32 @@
+from pypy.objspace.cclp.variable import wait__Var, _assign_aliases, _entail
+from pypy.objspace.cclp.types import W_Var
+
+
+
+def interp_wait(space, obj):
+    return wait__Var(space, obj)
+
+
+class RebindingError(Exception): pass
+
+def interp_bind(space, w_var, obj):
+    if isinstance(w_var.w_bound_to, W_Var):
+        return _assign_aliases(space, w_var, obj)
+    if w_var.w_bound_to == obj:
+        return
+    raise RebindingError
+
+class EntailmentFailure(Exception): pass
+
+def interp_entail(space, w_v1, w_v2):
+    w_v1val = w_v1.w_bound_to
+    w_v2val = w_v2.w_bound_to
+    if not isinstance(w_v1val, W_Var):
+        if not isinstance(w_v2val, W_Var):
+            # let's be simpler than unify
+            if w_v1val != w_v2val:
+                raise EntailmentFailure
+        return _assign_aliases(space, w_v2, w_v1val)
+    else:
+        return _entail(space, w_v1, w_v2)
+

Modified: pypy/dist/pypy/objspace/cclp/thread.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/thread.py	(original)
+++ pypy/dist/pypy/objspace/cclp/thread.py	Thu Aug 10 16:08:15 2006
@@ -53,3 +53,5 @@
 def this_thread(space):
     return ClonableCoroutine.w_getcurrent(space)
 app_this_thread = gateway.interp2app(this_thread)
+
+

Modified: pypy/dist/pypy/objspace/cclp/thunk.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/thunk.py	(original)
+++ pypy/dist/pypy/objspace/cclp/thunk.py	Thu Aug 10 16:08:15 2006
@@ -1,7 +1,10 @@
 from pypy.module._stackless.coroutine import _AppThunk
+from pypy.module._stackless.interp_coroutine import AbstractThunk
+
 from pypy.objspace.cclp.misc import w
 from pypy.objspace.cclp.global_state import scheduler
 from pypy.objspace.cclp.types import W_Var, W_Future, W_FailedValue
+from pypy.objspace.cclp.interp_var import interp_wait, interp_entail
 
 
 def logic_args(args):
@@ -95,9 +98,6 @@
             scheduler[0].schedule()
 
 
-from pypy.interpreter.argument import Arguments
-from pypy.module._stackless.interp_coroutine import AbstractThunk
-
 class PropagatorThunk(AbstractThunk):
     def __init__(self, space, w_constraint, coro, Merged):
         self.space = space
@@ -112,13 +112,14 @@
                 if entailed:
                     break
                 Obs = W_Var(self.space)
-                self.space.entail(self.Merged, Obs)
+                interp_entail(self.space, self.Merged, Obs)
                 for Sync in [var.w_dom.give_synchronizer()
                              for var in self.const._variables]:
-                    self.space.entail(Sync, Obs)
-                self.space.wait(Obs)
+                    interp_entail(self.space, Sync, Obs)
+                interp_wait(self.space, Obs)
         finally:
             self.coro._dead = True
             scheduler[0].remove_thread(self.coro)
             scheduler[0].schedule()
 
+

Modified: pypy/dist/pypy/objspace/cclp/types.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/types.py	(original)
+++ pypy/dist/pypy/objspace/cclp/types.py	Thu Aug 10 16:08:15 2006
@@ -78,30 +78,10 @@
 
     def __init__(self, space):
         self._space = space
-        self.__changed = W_Var(self._space)
+        self._changed = W_Var(self._space)
 
-    def clear_change(self):
-        #XXX called after revise ?
-        assert self._space.is_true(self._space.is_bound(self.__changed))
-        self.__changed = W_Var(self._space)
-
-    def give_synchronizer(self):
-        return self.__changed
-
-    def _value_removed(self):
-        """The implementation of remove_value should call this method"""
-        self._space.bind(self.__changed, self._space.newbool(True))
-        self.clear_change()
-        
-        if self.size() == 0: #        self._space.eq_w(self.w_size(), self._space.newint(0)):
-            raise OperationError(self._space.w_RuntimeError,
-                                 self._space.wrap('ConsistencyFailure'))
 
-    def w__del__(self):
-        self._space.bind(self.__changed, self._space.newbool(False))
-
-W_AbstractDomain.typedef = typedef.TypeDef("W_AbstractDomain",
-     give_synchronizer = gateway.interp2app(W_AbstractDomain.give_synchronizer))
+W_AbstractDomain.typedef = typedef.TypeDef("W_AbstractDomain")
 
 
 

Modified: pypy/dist/pypy/objspace/cclp/variable.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/variable.py	(original)
+++ pypy/dist/pypy/objspace/cclp/variable.py	Thu Aug 10 16:08:15 2006
@@ -9,7 +9,6 @@
 from pypy.objspace.cclp.global_state import scheduler
 from pypy.objspace.cclp.types import deref, W_Var, W_CVar, W_Future, W_FailedValue
 
-from pypy.objspace.cclp.constraint.domain import W_FiniteDomain
 
 W_Root = baseobjspace.W_Root
 all_mms = {}
@@ -20,14 +19,6 @@
     return w_v
 app_newvar = gateway.interp2app(newvar)
 
-def domain(space, w_values, w_name):
-    assert isinstance(w_values, W_ListObject)
-    assert isinstance(w_name, W_StringObject)
-    w_dom = W_FiniteDomain(space, w_values)
-    w_var = W_CVar(space, w_dom, w_name)
-    w("CVAR", str(w_var))
-    return w_var
-app_domain = gateway.interp2app(domain)
     
 #-- Wait -------------------------------------------------
 
@@ -60,6 +51,7 @@
 #-- Wait_needed --------------------------------------------
 
 def wait_needed__Var(space, w_var):
+    w(":wait_needed", str(id(ClonableCoroutine.w_getcurrent(space))))
     if space.is_true(space.is_free(w_var)):
         if w_var.needed:
             return
@@ -74,6 +66,7 @@
     return space.wait_needed(w_var)
 app_wait_needed = gateway.interp2app(wait_needed)            
 
+
 wait_needed_mm = StdObjSpaceMultiMethod('wait_needed', 1)
 wait_needed_mm.register(wait_needed__Var, W_Var)
 all_mms['wait_needed'] = wait_needed_mm
@@ -181,6 +174,7 @@
     assert isinstance(w_v2, W_Var)
     space.entail(w_v1, w_v2)
 app_entail = gateway.interp2app(entail)
+    
 
 def bind__Var_Root(space, w_var, w_obj):
     #w("var val", str(id(w_var)))
@@ -198,14 +192,6 @@
         raise_future_binding(space)
     return bind__Var_Root(space, w_fut, w_obj) # call-next-method ?
 
-def bind__CVar_Root(space, w_cvar, w_obj):
-    #XXX we should (want to) be able to test membership
-    #    in a wrapped against wrappeds into a non-wrapped dict
-    if [True for elt in w_cvar.w_dom._values.content
-        if space.is_true(space.eq(w_obj, elt))]:
-        return bind__Var_Root(space, w_cvar, w_obj)
-    raise_unification_failure(space, "value not in variable domain")
-
 def bind__Var_Var(space, w_v1, w_v2):
     #w("var var")
     if space.is_true(space.is_bound(w_v1)):
@@ -228,23 +214,6 @@
         raise_future_binding(space)
     return bind__Var_Var(space, w_fut, w_var)
 
-def bind__CVar_CVar(space, w_cvar1, w_cvar2):
-    w_inter_dom = space.intersection(w_cvar1.w_dom, w_cvar2.w_dom)
-    if w_inter_dom.__len__() > 0:
-        if w_inter_dom.__len__() == 1:
-            w_value = w_inter_dom.get_values()[0]
-            _assign_aliases(space, w_cvar1, w_value)
-            _assign_aliases(space, w_cvar2, w_value)
-        else:
-            w_cvar1.w_dom = w_cvar2.w_dom = w_inter_dom
-            _alias(space, w_cvar1, w_cvar2)
-    else:
-        raise_unification_failure(space, "incompatible domains")
-
-def bind__CVar_Var(space, w_cvar, w_var):
-    if space.is_true(space.is_bound(w_var)):
-        return bind__CVar_Root(space, w_cvar, w_var)
-    return bind__Var_Var(space, w_cvar, w_var)
 
 def bind__Var_Future(space, w_var, w_fut): 
     if space.is_true(space.is_bound(w_fut)): #XXX write a test for me !
@@ -260,9 +229,6 @@
 bind_mm.register(bind__Future_Root, W_Future, W_Root)
 bind_mm.register(bind__Future_Var, W_Future, W_Var)
 bind_mm.register(bind__Var_Future, W_Var, W_Future)
-bind_mm.register(bind__CVar_CVar, W_CVar, W_CVar)
-bind_mm.register(bind__CVar_Root, W_CVar, W_Root)
-bind_mm.register(bind__CVar_Var, W_CVar, W_Var)
 all_mms['bind'] = bind_mm
 
 
@@ -290,7 +256,7 @@
 def _assign_aliases(space, w_var, w_val):
     w("  :assign")
     assert isinstance(w_var, W_Var)
-    assert isinstance(w_val, W_Root)
+    #assert isinstance(w_val, W_Root)
     w_curr = w_var
     while 1:
         w_next = w_curr.w_bound_to

Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py	(original)
+++ pypy/dist/pypy/objspace/logic.py	Thu Aug 10 16:08:15 2006
@@ -29,7 +29,9 @@
 
 from pypy.objspace.cclp.variable import app_newvar, wait, app_wait, app_wait_needed, \
      app_is_aliased, app_is_free, app_is_bound, app_alias_of, alias_of, app_bind, \
-     app_unify, W_Var, W_CVar, W_Future, app_domain, all_mms as variable_mms, app_entail
+     app_unify, W_Var, W_CVar, W_Future, all_mms as variable_mms, app_entail
+
+from pypy.objspace.cclp.constraint.variable import app_domain
 
 from pypy.objspace.cclp.types import app_domain_of
 

Modified: pypy/dist/pypy/objspace/test/test_logicobjspace.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_logicobjspace.py	(original)
+++ pypy/dist/pypy/objspace/test/test_logicobjspace.py	Thu Aug 10 16:08:15 2006
@@ -789,7 +789,7 @@
                     unify(X, status)
                     break
             spc.merge()
-                
+
         s = newspace(problem)
         Finished = newvar()
         stacklet(solve, s, Finished)



More information about the Pypy-commit mailing list