]> hydra-www.ietfng.org Git - dyna2/commitdiff
codegen: put docstrings on handlers and initializers.
authortimv <tim.f.vieira@gmail.com>
Tue, 4 Jun 2013 19:42:25 +0000 (15:42 -0400)
committertimv <tim.f.vieira@gmail.com>
Tue, 4 Jun 2013 19:42:25 +0000 (15:42 -0400)
  - update ./debug to parse new format.

Updated TODO list.

parse_sexpr handles double quoted strings (at least crudely).

src/Dyna/Backend/Python/Backend.hs
src/Dyna/Backend/Python/debug.py
src/Dyna/Backend/Python/defn.py
src/Dyna/Backend/Python/interpreter.py
src/Dyna/Backend/Python/utils.py

index d6306c6fc5f587dedc54036121782506effed750..8fe82e29cfe22e2c0a630224243f11f6e9c6ca34 100644 (file)
@@ -235,31 +235,31 @@ pdope _d =         (indent 4 $ "for _ in [None]:")
                  $ go xs
 
 
-printPlanHeader :: Handle -> Rule -> Cost -> Maybe Int -> IO ()
-printPlanHeader h r c mn = do
-  hPutStrLn h $ "# --"
-    -- XXX This "prefixSD" thing is the only real reason we're doing this in
-    -- IO; it'd be great if wl-pprint-extras understood how to prefix each
-    -- line it was laying out.
-  displayIO h $ prefixSD "# " $ renderPretty 1.0 100
-                $ (prettySpanLoc $ r_span r) <> line
-  hPutStrLn h $ "# EvalIx: " ++ (show mn)
-  hPutStrLn h $ "# Cost: " ++ (show c)
-
-printInitializer :: Handle -> Rule -> Actions PyDopeBS -> IO ()
-printInitializer fh rule@(Rule _ h _ r _ _ ucruxes _) dope = do
+printPlanHeader :: Rule -> Cost -> Maybe Int -> Doc e
+printPlanHeader r c mn = do
+  vcat ["'''"
+       , "Span:  " <+> (prettySpanLoc $ r_span r)
+       , "RuleIx:" <+> (pretty $ r_index r)
+       , "EvalIx:" <+> (pretty mn)
+       , "Cost:  " <+> (pretty c)
+       , "'''"]
+
+printInitializer :: Handle -> Rule -> Cost -> Actions PyDopeBS -> IO ()
+printInitializer fh rule@(Rule _ h _ r _ _ ucruxes _) cost dope = do
   displayIO fh $ renderPretty 1.0 100
                  $ "@initializer" <> parens (uncurry pfa $ MA.fromJust $ findHeadFA h ucruxes)
-                   `above` "def" <+> char '_' <> tupled ["emit"] <+> colon
+                   `above` "def" <+> char '_' <> tupled ["emit"] <> colon
+                   `above` (indent 4 $ printPlanHeader rule cost Nothing)
                    `above` pdope dope
                    <> line
 
 -- XXX INDIR EVAL
-printUpdate :: Handle -> Rule -> Maybe DFunctAr -> (DVar, DVar) -> Actions PyDopeBS -> IO ()
-printUpdate fh rule@(Rule _ h _ r _ _ _ _) (Just (f,a)) (hv,v) dope = do
+printUpdate :: Handle -> Rule -> Cost -> Int -> Maybe DFunctAr -> (DVar, DVar) -> Actions PyDopeBS -> IO ()
+printUpdate fh rule@(Rule _ h _ r _ _ _ _) cost evalix (Just (f,a)) (hv,v) dope = do
   displayIO fh $ renderPretty 1.0 100
                  $ "@register" <> parens (pfa f a)
-                   `above` "def" <+> char '_' <> tupled (map pretty [hv,v,"emit"]) <+> colon
+                   `above` "def" <+> char '_' <> tupled (map pretty [hv,v,"emit"]) <> colon
+                   `above` (indent 4 $ printPlanHeader rule cost (Just evalix))
                    `above` pdope dope
                    <> line
 
@@ -282,14 +282,12 @@ driver am um {-qm-} is fh = do
      hPutStrLn fh ""
      hPutStrLn fh $ "# " ++ show fa
      forM_ ps $ \(r,n,c,vi,vo,act) -> do
-       printPlanHeader fh r c (Just n)
-       printUpdate fh r fa (vi,vo) act
+       printUpdate fh r c n fa (vi,vo) act
 
   hPutStrLn fh ""
   hPutStrLn fh $ "# ==Initializers=="
   forM_ is $ \(r,c,a) -> do
-    printPlanHeader  fh r c Nothing
-    printInitializer fh r a
+    printInitializer fh r c a
 
 {-
   hPutStrLn fh $ "# ==Queries=="
index 63a51c3e79c4da66c65eb1261df36aab297cf03b..15130b1d0de6b5a4ed29af89c1efc357c2442da5 100644 (file)
@@ -69,7 +69,6 @@ class Hypergraph(object):
             print >> f, 'digraph rule {'
             print >> f, 'rankdir=LR;'  # left-to-right layout
 
-
             print >> f, 'node [style=filled,fillcolor=white];'
 
             print >> f, 'bgcolor="transparent";'
@@ -366,18 +365,24 @@ Initializer:
 
             print >> html, '<h2>Update code</h2>'
 
-            for block in re.split('# --\n', code)[1:]:  # drop the begining bit.
-                [(f, bline, bcol, eline, ecol, code)] = \
-                    re.findall('# (.*?):(\d+):(\d+)-.*?:(\d+):(\d+)\n#.*\n([\w\W]*)',
+            for block in re.split('\n\s*\n', code):
+
+                x = re.findall('Span:\s*(.*?):(\d+):(\d+)-.*?:(\d+):(\d+)\n',
                                block)
 
-                code = re.compile('^\s*#.*', re.M).sub('', code.strip())
+                print '-------------------'
+                print block
+                print x
 
+                if not x:
+                    continue
+
+                [(f, bline, bcol, eline, ecol)] = x
+                code = block
                 lexer = get_lexer_by_name("python", stripall=True)
                 formatter = HtmlFormatter(linenos=False)
                 pretty_code = highlight(code, lexer, formatter)
 
-#
                 print >> html, """\
 <div class="handler-%s">
 <pre>
@@ -387,11 +392,12 @@ Initializer:
 """ % (bline, pretty_code)
 
         print >> html, '</pre>'
-
         print >> html, '</div>'
 
     if browser:
-        os.system('gnome-open %s 2>/dev/null >/dev/null' % html.name)
+        #os.system('gnome-open %s 2>/dev/null >/dev/null' % html.name)
+        import webbrowser
+        webbrowser.open(html.name)
 
 
 
index 8362854bea6a053c1eaac5727cd544b99a7ba29b..f8137b0ad72e66a0ae68e8b7eec8789c0108520a 100644 (file)
@@ -1,5 +1,5 @@
-import math, operator
-from collections import defaultdict, Counter
+import operator
+from collections import Counter
 from utils import red
 
 
@@ -47,7 +47,7 @@ def user_vars(variables):
     # remove the 'u' prefix on user variables 'uX'
 
     # Note: We also ignore user variables with an underscore prefix
-    
+
     return tuple((name[1:], val) for name, val in variables.items() if name.startswith('u') and not name.startswith('u_'))
 
 
index 47f395bcf8616ff5682fa67c1cfbaed97a6be780..1850e217e6ce81dbde8772c7061df7dd05783bb9 100644 (file)
@@ -1,10 +1,11 @@
 #!/usr/bin/env python
 
 """
-
 MISC
 ====
 
+ - TODO: mode planning failures are slient.
+
  - TODO: create an Interpreter object to hold state.
 
  - TODO: deleting a rule: (1) remove update handlers (2) run initializers in
@@ -38,20 +39,24 @@ MISC
 
  - TODO: transactions for errors.
 
+ - TODO: Numeric precision is an issue with BAggregators.
+
+     a[0.1] += 1
+     a[0.1 + eps] -= 1
+
+   Approaches:
 
+    - arbitrary precision arithmetic
 
-PARSER
-======
+    - approximate deletion ("buckets"), find the nearest neighbor and delete it.
 
-  - TODO: Nested expressions:
+    - hybrid: maintain streaming sum and the bag check periodically for quality
+      and null.
 
-    out(0) dict= _VALUE is (rewrite(X,Y) + rewrite(X,Y,Z)), _VALUE.
+    - numeric approximations, stream folding (fails to get null)
 
-    FATAL: Encountered error in input program:
-     Parser error
-     /tmp/tmp.dyna:1:14: error: expected: "."
-     rewrite(X,Y) + rewrite(X,Y,Z)<EOF>
-                  ^
+    - delete the hyperedge: not sure this is perfect because hyperedges aren't
+      named with numeric values of variables.
 
 
 What is null?
@@ -81,8 +86,8 @@ REPL
 
  - TODO: (Aggregator conflicts)
 
-   The good: we throw and AggregatorConflict exception if newly loaded code
-   tries to overwrite an aggregator in `agg_decl`.
+   We throw and AggregatorConflict exception if newly loaded code tries to
+   overwrite an aggregator in `agg_decl`.
 
    However, we need to make sure that we don't load subsequent code pertaining
    to this rule that we should reject altogether.
@@ -119,7 +124,7 @@ INTERPRETER
 
   The reason we have to support error is because the system might need to go
   through an error state before it can reach it's fixed out. In order to be
-  invariant to execution order (preserve our semantice) we to need to have the
+  invariant to execution order (preserve our semantics) we to need to have the
   ability fo reach pass thru an error state.
 
   for example:
@@ -134,9 +139,6 @@ INTERPRETER
   timv: This isn't sufficiently motivating because we can just leave `a` as
   `null` until we pass the divide by zero error.
 
-
-
-
 """
 
 from __future__ import division
index 396116f87688a69358eb0227d7f874cd3e49dd37..ad9cf09b6991e6081c43b9f9bde52e2f6733aa0e 100644 (file)
@@ -20,7 +20,7 @@ def parse_sexpr(e):
     e = re.compile('^\s*;.*?\n', re.M).sub('', e)  # remove comments
 
     es, stack = [], []
-    for token in re.split(r'([()])|\s+', e):
+    for token in re.split(r'("[^"]*?"|[()])|\s+', e):
         if token == '(':
             new = []
             if stack: