]> hydra-www.ietfng.org Git - dyna2/commitdiff
simpl, flat, replacement updates in python script.
authortimv <tim.f.vieira@gmail.com>
Thu, 13 Dec 2012 02:52:08 +0000 (21:52 -0500)
committertimv <tim.f.vieira@gmail.com>
Thu, 13 Dec 2012 02:52:08 +0000 (21:52 -0500)
bin/defn.py
bin/stdlib.py
examples/papa2.dyna

index a1652012f2a263ba150d54f4a1e6ca189bf2e7c4..89eed96027610066a98b38ffabd01db2a683c81b 100644 (file)
@@ -32,9 +32,9 @@ call = {'*/2': operator.mul,
 
         '-/1': operator.neg,
 
-#        '~/1': operator.inv,   # differs
-#        '|/1': operator.or_,
-#        '&/2': operator.and_,
+        '~/1': lambda x: not x,
+        '|/1': lambda x,y: x or y,
+        '&/2': lambda x,y: x and y,
 
         # comparisons
         '</2': operator.lt,
index e8f0e19333ba6a3028fcebc24edc81e5b2bd903a..a0741b7bf0ce2a5d21aac45120d00994288016af 100644 (file)
@@ -55,16 +55,25 @@ class Chart(object):
     def __getitem__(self, item):
         assert isinstance(item, tuple) and len(item) == self.ncols, \
             'item width mismatch: ncols %s, item %s' % (self.ncols, len(item))
-
         nonslice = [(i, val) for i, val in enumerate(item) if not isinstance(val, slice)]
         slices = [i for i, val in enumerate(item) if isinstance(val, slice)]
-
         for row in self.data.values():                    # XXX: very inefficient
             if row[-1] is None:
                 continue
             if all(row[i] == val for i, val in nonslice):
                 yield tuple(row[i] for i in slices)
 
+    def __setitem__(self, item, now):
+        assert isinstance(item, tuple) and len(item) == self.ncols - 1
+        nonslice = [(i, v) for i, v in enumerate(item) if not isinstance(v, slice)]
+        for idx, row in self.data.iteritems():
+            if all(row[i] == val for i, val in nonslice):
+                item = (self.name, idx)
+                was = self.data[idx][-1]
+                delete(item, was)
+                emit(item, now)
+        go()
+
     def lookup(self, *args):
         "find index for these args"
         assert len(args) == self.ncols - 1                    # XXX: lookup doesn't want val?
@@ -235,7 +244,7 @@ def aggregate(item):
 agenda = set()
 
 
-def _run():
+def _go():
     "the main loop"
     while agenda:
         (fn, idx) = item = agenda.pop()
@@ -261,9 +270,9 @@ def _run():
         update_dispatcher(item, now)
 
 
-def run():
+def go():
     try:
-        _run()
+        _go()
     except KeyboardInterrupt:
         pass
     finally:
@@ -273,12 +282,6 @@ def run():
             dump_charts(f)
 
 
-
-# Example of reactivity
-#  >>> emit(('rewrite/3', 5), -1000)
-#  >>> run_agenda()
-
-
 [dyna] = sys.argv[1:]
 
 cmd = """ghc -isrc Dyna.Backend.Python -e 'processFile "%s"' """ % dyna
@@ -298,4 +301,62 @@ for init in initializer.handlers:
     init()
 
 # start running the agenda
-run()
+go()
+
+
+
+class UserChart(object):
+
+    def __init__(self, _chart):
+        self.ncols = _chart.ncols
+        self.data = _chart.data
+        self.name = _chart.name
+        self._chart = _chart
+
+    def __getitem__(self, item):
+        assert isinstance(item, tuple) and len(item) == self.ncols - 1
+        nonslice = [(i, val) for i, val in enumerate(item) if not isinstance(val, slice)]
+        for idx, row in self.data.iteritems():
+            if row[-1] is None:
+                continue
+            if all(row[i] == val for i, val in nonslice):
+                yield (self.name, idx)
+
+    def __setitem__(self, item, now):
+        # XXX: can't add new things only change old things..
+
+        assert isinstance(item, tuple) and len(item) == self.ncols - 1
+        nonslice = [(i, v) for i, v in enumerate(item) if not isinstance(v, slice)]
+
+        foundone = False
+        for idx, row in self.data.iteritems():
+            if all(row[i] == val for i, val in nonslice):
+                item = (self.name, idx)
+
+                # timv: technically, should restrict to ":=" and extensional
+                aggregator[item].clear()
+                aggregator[item][now] += 1
+                agenda.add(item)
+
+                foundone = True
+
+        if not foundone and len(nonslice) == len(item):
+            idx = self._chart.insert(item + (None,))
+            item = (self.name, idx)
+            aggregator[item].clear()
+            aggregator[item][now] += 1
+            agenda.add(item)
+
+        go()
+
+
+#constructor_names = {''.join(functor.split('/')[:-1]) for functor in chart}
+for _fn in chart:
+    exec '%s = UserChart(chart[%r])' % (_fn.replace('/', ''), _fn)
+
+#def phrase(A,I,K):
+#    return chart['phrase/3']
+
+rewrite3["VP","VP",rewrite3["VP","VP","PP"]] = -100
+
+from debug import ip; ip()
index 1dbe959a883526733b8e88e1e6b9015cf9652bfa..42a4bfbf0f7bb3bbd892965e4b508d80afa2eab4 100644 (file)
@@ -38,4 +38,4 @@ word(     ".", 7) += 1.
 
 % for fun, try making the above words facts instead of +=1's.
 
-phrase(W, I, I+1, W) += 1 whenever word(W, I).
+phrase(W, I, I+1, W) += (word(W, I) > 0) & 1.