[pypy-svn] r23589 - pypy/dist/pypy/lib/logic/computation_space

auc at codespeak.net auc at codespeak.net
Wed Feb 22 10:44:14 CET 2006


Author: auc
Date: Wed Feb 22 10:44:12 2006
New Revision: 23589

Modified:
   pypy/dist/pypy/lib/logic/computation_space/test_variable.py
   pypy/dist/pypy/lib/logic/computation_space/variable.py
Log:
removal of useless stream stuff


Modified: pypy/dist/pypy/lib/logic/computation_space/test_variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/test_variable.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/test_variable.py	Wed Feb 22 10:44:12 2006
@@ -107,7 +107,7 @@
     if n<limit:
         sp = newspace()
         Xr = sp.var('Xr')
-        Xs.bind(v.CList(n, Xr))
+        Xs.bind((n, Xr))
         generate(thread, Xr, n+1, limit)
     else:
         Xs.bind(EOL)    
@@ -125,10 +125,10 @@
     print "GENERATOR waits on Xs"
     X_Xr = Xs.get()      # destructure Xs
     if X_Xr == None: return
-    X = X_Xr.first()     # ... into X
+    X = X_Xr[0]          # ... into X
     X.bind(n)            # bind X to n
     print "GENERATOR binds X to", n
-    Xr = X_Xr.rest()     # ... and Xr
+    Xr = X_Xr[1]         # ... and Xr
     dgenerate(thread, n+1, Xr)
 
 def dsum(thread, Xs, a, limit):
@@ -146,7 +146,7 @@
         X = sp.var('X')
         Xr = sp.var('Xr')
         print "CLIENT binds Xs to X|Xr"
-        Xs.bind(v.CList(X, Xr))
+        Xs.bind((X, Xr))
         x = X.get() # wait on the value of X
         print "CLIENT got", x
         dsum(thread, Xr, a+x, limit-1)
@@ -168,91 +168,12 @@
     if X_Xr == EOL:
         thread.result = a
         return
-    Xr = X_Xr.rest()
-    reduc(thread, Xr, fun(a, X_Xr.first()), fun)
+    Xr = X_Xr[1]
+    reduc(thread, Xr, fun(a, X_Xr[0]), fun)
 
 #-- meat ----------------------------------------
 
 class TestStream:
-
-    def test_basic_list(self):
-        s = v.make_list([1, 2, 3])
-        assert s.__str__() == '1|2|3'
-        assert s.length() == 3
-        s.rest().rest().set_rest(s)
-        assert s.length() == 4
-        assert s.__str__() == '1|2|3|...'
-        s. set_rest(s)
-        assert s.__str__() == '1|...'
-        assert s.length() == 2
-
-    def test_producer_consummer_stream(self):
-        """test FIFO stream behaviour"""
-        sp = newspace()
-        import time
-
-        def generate(thread, var, n, limit):
-            s = var.get()
-            while n<limit:
-                s.put(limit-n)
-                n += 1
-            s.put(v.NoValue)
-        
-        def reduc(thread, var, fun):
-            stream = var.get()
-            val = stream.get()
-            while (val != v.NoValue):
-                thread.result = fun(thread.result, val)
-                val = stream.get()
-
-        Xs = sp.var('s')
-        Xs.bind(v.Stream())
-        
-        generator = FunThread(generate, Xs, 1, 10)
-        reductor = FunThread(reduc, Xs, operator.mul)
-        reductor.result = 2
-
-        generator.start()
-        reductor.start()
-        generator.join()
-        reductor.join()
-        
-        assert reductor.result == 725760
-
-    def test_daisychain_stream(self):
-        """walk a list whose last element
-           is a var bound to another list
-        """
-        sp = newspace()
-
-        def woman_in_chains(thread, S):
-            stream = S.get()
-            assert isinstance(stream, v.Stream)
-            val = stream.get()
-            while val != v.NoValue:
-                print val
-                thread.result = val
-                val = stream.get()
-                if isinstance(val, v.Var):
-                    stream = val.get()
-                    val = stream.get()
-
-        s1 = sp.var('s1')
-        s2 = sp.var('s2')
-        stream1 = v.Stream(v.make_list([1, 2, 3, s2]))
-        stream2 = v.Stream(v.make_list([4, 5, 6, v.NoValue]))
-        assert str(stream1) == '1|2|3|s2'
-        assert str(stream2) == '4|5|6|variable.NoValue'
-        
-        woman = FunThread(woman_in_chains, s1)
-        woman.start()
-
-        s1.bind(stream1)
-        s2.bind(stream2)
-
-        woman.join()
-
-        assert woman.result == 6
                 
     def test_multiple_readers_eager_list(self):
         """the generator controls the flow"""
@@ -321,7 +242,7 @@
                 sp = newspace()
                 X_ = sp.var('X_')
                 Xr = sp.var('Xr')
-                Xs.bind(v.CList(X_, Xr))
+                Xs.bind((X_, Xr))
                 return startup(n-1, Xr)
 
             def ask_loop(Ys, Xs, End):
@@ -337,12 +258,12 @@
                 sp = newspace()
                 Y_Yr = Ys.get()   # destructure Ys
                 if Y_Yr != None: 
-                    Y, Yr = Y_Yr.as_tuple()
-                    X, Xr = Xs.get().as_tuple()
+                    Y, Yr = Y_Yr
+                    X, Xr = Xs.get()
                     Y.bind(X.get())
                     End2 = sp.var('End2')
                     X_ = sp.var('X_')
-                    End.bind(v.CList(X_, End2))
+                    End.bind((X_, End2))
                     ask_loop(Yr, Xr, End2)
                 else:
                     End.bind(None)

Modified: pypy/dist/pypy/lib/logic/computation_space/variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/variable.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/variable.py	Wed Feb 22 10:44:12 2006
@@ -40,8 +40,6 @@
         self._cs = cs
         # top-level 'commited' binding
         self._val = NoValue
-        # domains in multiple spaces
-        # self._doms = {cs : FiniteDomain([])}
         # when updated while unification happens, keep track
         # of our initial value (for failure cases)
         self._previous = None
@@ -100,13 +98,6 @@
 
     is_bound = _is_bound
 
-    #-- domain setter/getter is per space
-##     def cs_set_dom(self, cs, dom):
-##         self._doms[cs] = dom
-
-##     def cs_get_dom(self, cs):
-##         self._doms.setdefault(cs, FiniteDomain([]))
-##         return self._doms[cs]
 
     #-- Dataflow ops with concurrent semantics ------
     # should be used by threads that want to block on
@@ -125,178 +116,4 @@
             self._value_condition.release()
 
 
-#-- stream stuff -----------------------------
-
-class Pair(object):
-    """similar to CONS in Lisp"""
-
-    def __init__(self, car, cdr):
-        self._car = car
-        self._cdr = cdr
-
-    def first(self):
-        return self._car
-
-    def set_first(self, first):
-        self._cdr = first
-
-    def rest(self):
-        return self._cdr
-
-    def set_rest(self, stuff):
-        self._cdr = stuff
-
-    def as_tuple(self):
-        return (self._car, self._cdr)
-
-    def is_empty(self):
-        return self._car is None and self._cdr is None
-
-    def length(self):
-        ln = 0
-        curr = self
-        if curr.first() != None:
-            ln += 1
-        while curr.rest() != None:
-            curr = curr.rest()
-            if curr.first() != None:
-                ln += 1
-            # check for circularity
-            if curr == self: return ln
-        return ln
-
-    def __str__(self):
-        # This will show bogus stuff for trees ...
-        seen = set()
-        strs = []
-
-        def build_elt_str(elt):
-            if elt in seen:
-                strs.pop() ; strs.pop()
-                # show ellipsis when recursing
-                strs.append('...')
-            elif isinstance(elt, Pair):
-                seen.add(elt)
-                build_pair_str(elt)
-            else:
-                if elt is None:
-                    strs.pop()
-                elif isinstance(elt, Var):
-                    if elt.is_bound():
-                        strs.append(str(elt.val))
-                    else:
-                        strs.append(elt.name)
-                else:
-                    strs.append(str(elt))
-
-        def build_pair_str(pair):
-            build_elt_str(pair._car)
-            strs.append('|')
-            build_elt_str(pair._cdr)
-
-        if self._car is None:
-            return 'nil'
-        build_pair_str(self)
-        return ''.join(strs)
-
-def make_list(data=None):
-    """Builds a list with pairs"""
-    assert (data is None) \
-           or type(data) in (list, tuple, set)
-    if data is None:
-        return Pair(None, None)
-    curr = Pair(data[0], None)
-    head = curr
-    for datum in data[1:]:
-        curr.set_rest(Pair(datum, None))
-        curr = curr.rest()
-    return head
-
-class CList(Pair):
-    """A List supporting concurrent access"""
-
-    def __init__(self, car, cdr):
-        Pair.__init__(self, car, cdr)
-        self.last_condition = threading.Condition()
-
-    def set_rest(self, rest):
-        self.last_condition.acquire()
-        try:
-            self._cdr = rest
-            self.last_condition.notifyAll()
-        finally:
-            self.last_condition.release()
-
-    def rest(self):
-        self.last_condition.acquire()
-        try:
-            while self._cdr == None:
-                self.condition.wait()
-            return self._cdr
-        finally:
-            self.last_condition.release()
-        
-
-class Stream(object):
-    """A FIFO stream"""
-
-    def __init__(self, elts=Pair(None, None)):
-        self.head = elts
-        if elts.first() == None:
-            self.tail = elts
-        else:
-            curr = elts.rest()
-            prev = elts
-            while isinstance(curr, Pair):
-                prev = curr
-                curr = curr.rest()
-            # last pair of the chain
-            self.tail = prev
-        # head hurts tail sometimes ...
-        self.empty_condition = threading.Condition()
-
-    def get(self):
-        print self.head
-        # first thing to check is whether
-        # there is stuff to feed
-        self.empty_condition.acquire()
-        try:
-            if self.head == self.tail:
-                # there might remain one element there
-                while self.head.is_empty():
-                    self.empty_condition.wait()
-            # sky is clear : there is something to get
-            elt = self.head.first()
-            # we might want to advance to the next pair
-            if self.head != self.tail:
-                self.head = self.head.rest()
-            else:
-                # or just nullify what we read
-                # to avoid reading it again ...
-                self.head._car = None
-        finally:
-            self.empty_condition.release()
-        return elt
-
-    def put(self, val):
-        # first, check for emptyness special case
-        self.empty_condition.acquire()
-        try:
-            if self.head.is_empty():
-                # then we put stuff into head
-                # without consing and just return
-                self.head._car = val
-                self.empty_condition.notifyAll()
-                return
-        finally:
-            self.empty_condition.release()
-        # either we did put and return
-        # or nothing done yet
-        new_tail = Pair(val, None)
-        self.tail.set_rest(new_tail)
-        self.tail = new_tail
-
-    def __str__(self):
-        return str(self.head)
-
 



More information about the Pypy-commit mailing list