]> hydra-www.ietfng.org Git - dyna2/commitdiff
elaborate papa2 example works!
authortimv <tim.f.vieira@gmail.com>
Wed, 12 Dec 2012 06:34:28 +0000 (01:34 -0500)
committertimv <tim.f.vieira@gmail.com>
Wed, 12 Dec 2012 06:34:28 +0000 (01:34 -0500)
use correct aggregators for each functor.

bin/stdlib.py
examples/papa2.dyna
src/Dyna/Backend/Python.hs

index adb1943795afc512247297da54337f112783d019..b2b311bbc03b2a5f3cde6c80eb2f3a4872687dad 100644 (file)
@@ -83,7 +83,7 @@ def dump_charts():
     for x in chart:
         print x
         for idx, row in chart[x].data.items():
-            print '%-30s := %s' % (pretty((x,idx)), row[-1])
+            print '%s: %-30s := %s' % (idx, pretty((x,idx)), row[-1])
         print
 
 
@@ -285,28 +285,34 @@ def run_agenda():
         update_dispatcher(item, now)
 
 
+agg = {}
+
 def aggregate(item):
-    print '    aggregate:', pretty(item), aggregator[item],
-    val = 0.0  # need identity element or do a "fold1"
-    for k, multiplicity in aggregator[item].iteritems():
-        assert multiplicity >= 0, "negative multiplicity: this should never happen."
-        val += k*multiplicity         # val*multiplicity; TODO: use correct aggregator
-    print 'result:', val
-    return val
+    (fn, _) = item
+    return aggr[agg[fn]](item)
 
 
 def max_equals(item):
-    raise NotImplementedError
+    return max(k for k, m in aggregator[item].iteritems() if m > 0)
+
 def min_equals(item):
-    raise NotImplementedError
+    return min(k for k, m in aggregator[item].iteritems() if m > 0)
+
 def plus_equals(item):
-    raise NotImplementedError
+    return reduce(operator.add,
+                  [k*m for k, m in aggregator[item].iteritems()])
+
 def times_equals(item):
-    raise NotImplementedError
+    return reduce(operator.mul,
+                  [k**m for k, m in aggregator[item].iteritems()])
+
 def and_equals(item):
-    raise NotImplementedError
+    return reduce(operator.and_,
+                  [k for k, m in aggregator[item].iteritems() if m > 0])
+
 def or_equals(item):
-    raise NotImplementedError
+    return reduce(operator.or_,
+                  [k for k, m in aggregator[item].iteritems() if m > 0])
 
 
 aggr = {
@@ -327,6 +333,10 @@ def delete(item, val):
     _delete = False
 
 
+# Example of reactivity
+#  >>> emit(('rewrite/3', 5), -1000)
+#  >>> run_agenda()
+
 [dyna] = sys.argv[1:]
 
 cmd = """ghc -isrc Dyna.Backend.Python -e 'processFile "%s"' """ % dyna
index 8e14d76574ca2cd6f7f1c296394383766101f0a2..1dbe959a883526733b8e88e1e6b9015cf9652bfa 100644 (file)
@@ -4,9 +4,9 @@
 phrase(X,I,K,t(X,TY)) += phrase(Y,I,K,TY) * rewrite(X,Y).
 phrase(X,I,K,t(X,TY,TZ)) += phrase(Y,I,J,TY) * phrase(Z,J,K,TZ) * rewrite(X,Y,Z).
 
-goal(P) = phrase("S",0,*length,P).
+goal(P) += phrase("S", 0, *length, P).
 
-length max= word(_, I), I.
+length max= word(_, I), I+1.
 
 % grammar rules
 rewrite( "S",   "S",  ".") += 1.
index e6a91d9b3d56ac4d3a099be1c80c0a1963ad480a..f43109a0f47292b3c79486b6ce08dec60cb7ee1d 100644 (file)
@@ -206,8 +206,10 @@ processFile_ fileName fh = do
                    Left e -> throw $ TLEAggPlan e
                    Right x -> return x
 
-         forM (M.toList aggm) $ \(k,v) -> do {
-             hPutStrLn fh $ "# " ++ (show k) ++ "->" ++ show v
+         hPutStrLn fh $ "agg = {}"
+         forM (M.toList aggm) $ \((f,a),v) -> do {
+             hPutStrLn fh $ show $ "agg" <> brackets (dquotes $ pretty f <> "/" <> pretty a)
+                <+> equals <+> (dquotes $ pretty v)
            }
 
          cPlans <- return $! combinePlans                  -- crux plans