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

auc at codespeak.net auc at codespeak.net
Wed Aug 9 12:35:47 CEST 2006


Author: auc
Date: Wed Aug  9 12:35:42 2006
New Revision: 31193

Modified:
   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:
entailment constraint on vars.


Modified: pypy/dist/pypy/objspace/cclp/types.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/types.py	(original)
+++ pypy/dist/pypy/objspace/cclp/types.py	Wed Aug  9 12:35:42 2006
@@ -1,7 +1,7 @@
 from pypy.interpreter import baseobjspace, gateway, typedef
 
 from pypy.objspace.cclp.misc import w, ClonableCoroutine
-from pypy.objspace.constraint.domain import W_FiniteDomain
+#from pypy.objspace.constraint.domain import W_FiniteDomain
 
 W_Root = baseobjspace.W_Root
 
@@ -11,6 +11,7 @@
     def __init__(w_self, space):
         # ring of aliases or bound value
         w_self.w_bound_to = w_self
+        w_self.entails = {}
         # byneed flag
         w_self.needed = False
 
@@ -31,10 +32,14 @@
 
 
 class W_CVar(W_Var):
-    def __init__(w_self, space, w_dom):
+    def __init__(w_self, space, w_dom): #, w_name):
         assert isinstance(w_dom, W_FiniteDomain)
         W_Var.__init__(w_self, space)
         w_self.w_dom = w_dom
+        #w_self.name = space.str_w(w_name)
+
+    def name_w(w_self):
+        return w_self.name
 
 def domain_of(space, w_v):
     assert isinstance(w_v, W_CVar)
@@ -50,6 +55,16 @@
     def __init__(w_self, exc):
         w_self.exc = exc
 
+#-- Constraint ---------------------------------------------
+
+## class W_Constraint(baseobjspace.Wrappable):
+##     def __init__(self, object_space):
+##         self._space = object_space
+
+## W_Constraint.typedef = typedef.TypeDef(
+##     "W_Constraint")
+
+
 #-- Misc ---------------------------------------------------
 
 def deref(space, w_var):

Modified: pypy/dist/pypy/objspace/cclp/variable.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/variable.py	(original)
+++ pypy/dist/pypy/objspace/cclp/variable.py	Wed Aug  9 12:35:42 2006
@@ -3,6 +3,7 @@
 from pypy.objspace.std.model import StdObjSpaceMultiMethod
 from pypy.objspace.std.listobject import W_ListObject, W_TupleObject
 from pypy.objspace.std.dictobject import W_DictObject
+from pypy.objspace.std.stringobject import W_StringObject
 
 from pypy.objspace.cclp.misc import w, v, ClonableCoroutine
 from pypy.objspace.cclp.global_state import scheduler
@@ -19,10 +20,11 @@
     return w_v
 app_newvar = gateway.interp2app(newvar)
 
-def domain(space, w_values):
+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_var = W_CVar(space, w_dom)#, w_name)
     w("CVAR", str(w_var))
     return w_var
 app_domain = gateway.interp2app(domain)
@@ -161,8 +163,7 @@
                          space.wrap("This future is read-only for you, pal"))
 
 
-#-- BIND -----------------------------
-
+#-- BIND, ENTAIL----------------------------
 
 def bind(space, w_var, w_obj):
     """1. aliasing of unbound variables
@@ -175,6 +176,13 @@
     space.bind(w_var, w_obj)
 app_bind = gateway.interp2app(bind)
 
+def entail(space, w_v1, w_v2):
+    "X -> Y"
+    assert isinstance(w_v1, W_Var)
+    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)))
     # 3. var and value
@@ -245,7 +253,8 @@
     if w_fut._client == ClonableCoroutine.w_getcurrent(space):
         raise_future_binding(space)
     return bind__Var_Var(space, w_var, w_fut) #and for me ...
-    
+
+
 bind_mm = StdObjSpaceMultiMethod('bind', 2)
 bind_mm.register(bind__Var_Root, W_Var, W_Root)
 bind_mm.register(bind__Var_Var, W_Var, W_Var)
@@ -257,6 +266,28 @@
 bind_mm.register(bind__CVar_Var, W_CVar, W_Var)
 all_mms['bind'] = bind_mm
 
+
+def entail__Var_Var(space, w_v1, w_v2):
+    w("  :entail Var Var")
+    if space.is_true(space.is_bound(w_v1)):
+        if space.is_true(space.is_bound(w_v2)):
+            return unify(space,
+                         deref(space, w_v1),
+                         deref(space, w_v2))
+        return _assign_aliases(space, w_v2, deref(space, w_v1))
+    else:
+        return _entail(space, w_v1, w_v2)
+
+entail_mm = StdObjSpaceMultiMethod('entail', 2)
+entail_mm.register(entail__Var_Var, W_Var, W_Var)
+all_mms['entail'] = entail_mm
+
+def _entail(space, w_v1, w_v2):
+    assert isinstance(w_v1, W_Var)
+    assert isinstance(w_v2, W_Var)
+    w_v1.entails[w_v2] = True
+    return space.w_None
+        
 def _assign_aliases(space, w_var, w_val):
     w("  :assign")
     assert isinstance(w_var, W_Var)
@@ -271,9 +302,18 @@
             break
         # switch to next
         w_curr = w_next
+    _assign_entailed(space, w_var, w_val)
     w("  :assigned")
     return space.w_None
 
+def _assign_entailed(space, w_var, w_val):
+    w("   :assign entailed")
+    for var in w_var.entails:
+        if space.is_true(space.is_free(var)):
+            _assign_aliases(space, var, w_val)
+        else:
+            unify(space, deref(space, var), w_val)
+
 def _assign(space, w_var, w_val):
     assert isinstance(w_var, W_Var)
     if isinstance(w_var, W_CVar):

Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py	(original)
+++ pypy/dist/pypy/objspace/logic.py	Wed Aug  9 12:35:42 2006
@@ -21,13 +21,15 @@
 
 from pypy.objspace.cclp.global_state import scheduler
 
-from pypy.objspace.cclp.space import app_newspace, app_choose, W_CSpace
+#-- COMP. SPACE --------------------------------------------
+
+from pypy.objspace.cclp.space import app_newspace, app_choose, W_CSpace #app_tell
 
 #-- VARIABLE ------------------------------------------------
 
 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_unify, W_Var, W_CVar, W_Future, app_domain, all_mms as variable_mms, app_entail
 
 from pypy.objspace.cclp.types import app_domain_of
 
@@ -214,6 +216,7 @@
         setattr(space, name, boundmethod)  # store into 'space' instance
     # /multimethods hack
 
+    #-- variable -------
     space.setitem(space.builtin.w_dict, space.wrap('newvar'),
                   space.wrap(app_newvar))
     space.setitem(space.builtin.w_dict, space.wrap('domain'),
@@ -230,6 +233,8 @@
                   space.wrap(app_is_aliased))
     space.setitem(space.builtin.w_dict, space.wrap('bind'),
                  space.wrap(app_bind))
+    space.setitem(space.builtin.w_dict, space.wrap('entail'),
+                 space.wrap(app_entail))
     space.setitem(space.builtin.w_dict, space.wrap('unify'),
                  space.wrap(app_unify))
 ##     #-- comp space ---
@@ -240,7 +245,7 @@
                  space.wrap(domain.app_make_fd))
     space.setitem(space.builtin.w_dict, space.wrap('intersection'),
                  space.wrap(domain.app_intersection))
-##     #-- constraint ----
+##     #-- constraints ----
 ##     space.setitem(space.builtin.w_dict, space.wrap('make_expression'),
 ##                  space.wrap(constraint.app_make_expression))
 ##     space.setitem(space.builtin.w_dict, space.wrap('AllDistinct'),
@@ -276,6 +281,8 @@
                   space.wrap(app_newspace))
     space.setitem(space.builtin.w_dict, space.wrap('choose'),
                   space.wrap(app_choose))
+##     space.setitem(space.builtin.w_dict, space.wrap('tell'),
+##                   space.wrap(app_tell))
 
     #-- misc -----
     space.setitem(space.builtin.w_dict, space.wrap('interp_id'),

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	Wed Aug  9 12:35:42 2006
@@ -193,6 +193,43 @@
         raises(UnificationError, unify, f1.b, 24)
 
 
+    def test_entail(self):
+        X, Y = newvar(), newvar()
+        entail(X, Y)
+        unify(42, X)
+        assert X == Y == 42
+
+        X, Y = newvar(), newvar()
+        entail(X, Y)
+        unify(42, Y)
+        assert is_free(X)
+        assert Y == 42
+        unify(X, 42)
+        assert X == Y == 42
+
+        X, Y = newvar(), newvar()
+        entail(X, Y)
+        unify(42, Y)
+        assert is_free(X)
+        assert Y == 42
+        raises(UnificationError, unify, X, True)
+
+        X, Y = newvar(), newvar()
+        entail(X, Y)
+        unify(X, Y)
+        assert is_free(X)
+        assert is_free(Y)
+        unify(Y, 42)
+        assert X == Y == 42
+
+        X, Y, O = newvar(), newvar(), newvar()
+        entail(X, O)
+        entail(Y, O)
+        unify(Y, 42)
+        assert is_free(X)
+        assert Y == O == 42
+        
+
 class AppTest_LogicFutures(object):
 
     def setup_class(cls):
@@ -739,3 +776,28 @@
         schedule()
         assert len(sched_all()['threads']) == 1
 
+    def test_tell(self):
+
+        skip("not finished ...")
+
+        def problem():
+            X, Y = domain([1, 2], 'X'), domain([1, 2, 3], 'Y')
+            tell(make_expression([X, Y], 'X + Y > 4'))
+
+
+        def solve(spc, X):
+            while 1:
+                status = spc.ask()
+                if status == 1:
+                    unify(X, status)
+                    break
+                
+        switch_debug_info()
+        s = newspace(problem)
+        Finished = newvar()
+        stacklet(solve, s, Finished)
+
+        wait(Finished)
+
+        assert domain_of(X) == FiniteDomain([2])
+        assert domain_of(Y) == FiniteDomain([3])



More information about the Pypy-commit mailing list