From: timv Date: Tue, 4 Jun 2013 19:42:25 +0000 (-0400) Subject: codegen: put docstrings on handlers and initializers. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=a5d1ef3b47b901c258bd159b9d4082f58a8d3d63;p=dyna2 codegen: put docstrings on handlers and initializers. - update ./debug to parse new format. Updated TODO list. parse_sexpr handles double quoted strings (at least crudely). --- diff --git a/src/Dyna/Backend/Python/Backend.hs b/src/Dyna/Backend/Python/Backend.hs index d6306c6..8fe82e2 100644 --- a/src/Dyna/Backend/Python/Backend.hs +++ b/src/Dyna/Backend/Python/Backend.hs @@ -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==" diff --git a/src/Dyna/Backend/Python/debug.py b/src/Dyna/Backend/Python/debug.py index 63a51c3..15130b1 100644 --- a/src/Dyna/Backend/Python/debug.py +++ b/src/Dyna/Backend/Python/debug.py @@ -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, '

Update code

' - 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, """\
@@ -387,11 +392,12 @@ Initializer:
 """ % (bline, pretty_code)
 
         print >> html, '
' - print >> html, '
' 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) diff --git a/src/Dyna/Backend/Python/defn.py b/src/Dyna/Backend/Python/defn.py index 8362854..f8137b0 100644 --- a/src/Dyna/Backend/Python/defn.py +++ b/src/Dyna/Backend/Python/defn.py @@ -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_')) diff --git a/src/Dyna/Backend/Python/interpreter.py b/src/Dyna/Backend/Python/interpreter.py index 47f395b..1850e21 100644 --- a/src/Dyna/Backend/Python/interpreter.py +++ b/src/Dyna/Backend/Python/interpreter.py @@ -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) - ^ + - 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 diff --git a/src/Dyna/Backend/Python/utils.py b/src/Dyna/Backend/Python/utils.py index 396116f..ad9cf09 100644 --- a/src/Dyna/Backend/Python/utils.py +++ b/src/Dyna/Backend/Python/utils.py @@ -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: