From: Tim Vieira Date: Mon, 1 Jul 2013 19:29:15 +0000 (-0400) Subject: Changed output format yet again. Everything is now consistent with "term = X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=0b1afd1e94f3dae6ee733a173fdfbe1e5c1a51af;p=dyna2 Changed output format yet again. Everything is now consistent with "term = value." Robustified retract_rule. Incorporated Jason's tweaks to trace. --- diff --git a/src/Dyna/Backend/Python/chart.py b/src/Dyna/Backend/Python/chart.py index 5d09deb..951f6e3 100644 --- a/src/Dyna/Backend/Python/chart.py +++ b/src/Dyna/Backend/Python/chart.py @@ -22,12 +22,12 @@ class Chart(object): return '' if self.arity == 0: [term] = rows - return '%s => %s.' % (term, _repr(term.value)) + return '%s = %s.' % (term, _repr(term.value)) p = [(_repr(term), _repr(term.value)) for term in sorted(rows)] lines = [self.name, '='*len(self.name)] # heading terms, values = zip(*p) widths = map(len, terms) - fmt = '%%-%ds => %%s.' % min(max(widths), 40) + fmt = '%%-%ds = %%s.' % min(max(widths), 40) for term, value in zip(terms, values): lines.append(fmt % (term, value)) lines.append('') diff --git a/src/Dyna/Backend/Python/interpreter.py b/src/Dyna/Backend/Python/interpreter.py index 9ef289e..d9af1dd 100644 --- a/src/Dyna/Backend/Python/interpreter.py +++ b/src/Dyna/Backend/Python/interpreter.py @@ -272,6 +272,7 @@ class Interpreter(object): def dump_rules(self): if not self.rules: + print 'No rules found.' return print print 'Rules' diff --git a/src/Dyna/Backend/Python/load/matrix.py b/src/Dyna/Backend/Python/load/matrix.py index 6b334b9..e1c9dea 100644 --- a/src/Dyna/Backend/Python/load/matrix.py +++ b/src/Dyna/Backend/Python/load/matrix.py @@ -18,13 +18,13 @@ class matrix(object): ======== m/2 === - m(0,0) => 1.0. - m(0,1) => 2.0. - m(0,2) => 3.0. - m(1,0) => 4.0. - m(1,1) => 5.0. - m(3,0) => 6.0. - m(3,1) => 7.0. + m(0,0) = 1.0. + m(0,1) = 2.0. + m(0,2) = 3.0. + m(1,0) = 4.0. + m(1,1) = 5.0. + m(3,0) = 6.0. + m(3,1) = 7.0. As you can see, by default values are interpreted as floats, to specify a @@ -38,13 +38,13 @@ class matrix(object): ======== m/2 === - m(0,0) => "1". - m(0,1) => "2". - m(0,2) => "3". - m(1,0) => "4". - m(1,1) => "5". - m(3,0) => "6". - m(3,1) => "7". + m(0,0) = "1". + m(0,1) = "2". + m(0,2) = "3". + m(1,0) = "4". + m(1,1) = "5". + m(3,0) = "6". + m(3,1) = "7". """ diff --git a/src/Dyna/Backend/Python/load/sexpr.py b/src/Dyna/Backend/Python/load/sexpr.py index da7dd6f..ea334fc 100644 --- a/src/Dyna/Backend/Python/load/sexpr.py +++ b/src/Dyna/Backend/Python/load/sexpr.py @@ -15,8 +15,8 @@ class sexpr(object): ======== trees/1 ======= - trees(0) => node("a", node("b", "c"), node("d", "e")) - trees(1) => node("a", "b", "c") + trees(0) = node("a", node("b", "c"), node("d", "e")) + trees(1) = node("a", "b", "c") """ diff --git a/src/Dyna/Backend/Python/load/tsv.py b/src/Dyna/Backend/Python/load/tsv.py index de52279..e94d450 100644 --- a/src/Dyna/Backend/Python/load/tsv.py +++ b/src/Dyna/Backend/Python/load/tsv.py @@ -13,13 +13,13 @@ class tsv(object): > sol row/4 ===== - row(0,"0","S","NP VP") => true. - row(1,"1.58","ROOT","S .") => true. - row(2,"1.58","ROOT","S !") => true. - row(3,"1.58","ROOT","VP !") => true. - row(4,"3.81","VP","V") => true. - row(5,"3.81","VP","V NP") => true. - row(6,"1.49","VP","V VP") => true. + row(0,"0","S","NP VP") = true. + row(1,"1.58","ROOT","S .") = true. + row(2,"1.58","ROOT","S !") = true. + row(3,"1.58","ROOT","VP !") = true. + row(4,"3.81","VP","V") = true. + row(5,"3.81","VP","V NP") = true. + row(6,"1.49","VP","V VP") = true. ... """ diff --git a/src/Dyna/Backend/Python/post/trace.py b/src/Dyna/Backend/Python/post/trace.py index 51029af..58c2d48 100644 --- a/src/Dyna/Backend/Python/post/trace.py +++ b/src/Dyna/Backend/Python/post/trace.py @@ -9,8 +9,6 @@ TODO: shared substructure. import re from utils import yellow, green, cyan, red, _repr, drepr import debug, defn -from cStringIO import StringIO -from utils import lexer, subst from draw_circuit import infer_edges from collections import defaultdict @@ -59,10 +57,14 @@ def groupby(key, data): def dig(head, visited, tail, groups, interp): if head in tail: - return [yellow % head + ': ' + red % '*cycle*'] + return ['%s = %s' % (yellow % head, head.value)] \ + + ['|'] \ + + branch([[red % 'continue as before (cyclic structure, will continue forever)']]) if head in visited: - return [yellow % head + ': ' + red % 'shared structure see above'] + return ['%s = %s' % (yellow % head, head.value)] \ + + ['|'] \ + + branch([[red % 'continue as before (shared structure)']]) if head not in groups: return [] @@ -80,9 +82,9 @@ def dig(head, visited, tail, groups, interp): if block: contribs.append(crux.format() + ['|'] + block) else: - contribs.append(crux.format()) + contribs.append(crux.format() + ['']) - return ['%s => %s' % (yellow % head, cyan % _repr(head.value))] \ + return ['%s = %s' % (yellow % head, cyan % _repr(head.value))] \ + ['|'] \ + branch(contribs) + [''] diff --git a/src/Dyna/Backend/Python/repl.py b/src/Dyna/Backend/Python/repl.py index 263dced..c3cd441 100644 --- a/src/Dyna/Backend/Python/repl.py +++ b/src/Dyna/Backend/Python/repl.py @@ -52,31 +52,44 @@ class REPL(cmd.Cmd, object): """ Retract rule from program by rule index. - > a += 1. - > b += 1. - > c += a*b. + > a += 1. + > b += 1. + > c += a*b. - > rules + In order to retract a rule we need to know it's index, for that we use + the command `rules`. - Rules - ===== - 0: a += 1. - 1: b += 1. - 2: c += a * b. + > rules - > retract_rule 0 + Rules + ===== + 0: a += 1. + 1: b += 1. + 2: c += a * b. + + Now let's remove a rule: + + > retract_rule 0 This removes rule 0 from the program. Now, let's inspect the changes to the solution. - > sol + > sol - Solution - ======== - b => 1. + Solution + ======== + b = 1. """ - self.interp.retract_rule(int(idx)) + + try: + idx = int(idx) + except ValueError: + print 'Please specify an integer. Type `help retract_rule` to read more.' + else: + if self.interp.retract_rule(idx) is None: + print 'List available by typing `rules`' + print def do_exit(self, _): """ @@ -154,20 +167,28 @@ class REPL(cmd.Cmd, object): self.interp.new_rules = set() try: - query = "$out(%s) dict= %s." % (self.lineno, q) + query = "$query dict= %s." % q self.default(query, show_changed=False) try: - [(_, _, results)] = self.interp.chart['$out/1'][self.lineno,:] + [(_, _, results)] = self.interp.chart['$query/0'][:,] return results except ValueError: return [] finally: + # cleanup: # retract newly added rules. for r in self.interp.new_rules: self.interp.retract_rule(r) - # drop $out chart - del self.interp.chart['$out/1'] + + try: + # drop $out chart + del self.interp.chart['$query/0'] + except KeyError: + # query must have failed. + pass + + self.interp.new_rules = set() def do_vquery(self, q): """ @@ -199,14 +220,14 @@ class REPL(cmd.Cmd, object): - `vquery` shows variable bindings > vquery f(X) - 1 where {X=1} - 4 where {X=1} + 1 where {X=1} + 4 where {X=1} - `query` shows variable bindings applied to query > query f(X) - 1 =* f(1) - 4 =* f(2) + f(1) = 1. + f(2) = 4. - `trace` is an introspection tool for visualizing the derivation of an item and its value. Type `help trace` for more information. @@ -218,8 +239,9 @@ class REPL(cmd.Cmd, object): if len(results) == 0: print 'No results.' return + print for term, result in sorted((subst(q, dict(result.variables)), result) for result in results): - print ' ', _repr(result.value), '=*', term + print term, '=', _repr(result.value) print def default(self, line, show_changed=True): @@ -238,7 +260,7 @@ class REPL(cmd.Cmd, object): except (DynaInitializerException, DynaCompilerError) as e: print type(e).__name__ + ':' print e - print '> new rule(s) were not added to program.' + print 'new rule(s) were not added to program.' print else: if show_changed: @@ -249,7 +271,7 @@ class REPL(cmd.Cmd, object): return print '=============' for x, v in sorted(changed.items()): - print '%s => %s.' % (x, _repr(v)) + print '%s = %s.' % (x, _repr(v)) # def _changed_subscriptions(self, changed): # @@ -354,12 +376,12 @@ class REPL(cmd.Cmd, object): ======== data/3 ====== - data(2,"cow","boy") => true. + data(2,"cow","boy") = true. data/4 ====== - data(0,"a","b","3.0") => true. - data(1,"c","d","4.0") => true. + data(0,"a","b","3.0") = true. + data(1,"c","d","4.0") = true. """ try: @@ -404,27 +426,27 @@ class REPL(cmd.Cmd, object): In our solution we see that `a` is true. > sol - a => true. - b => true. - c => true. + a = true. + b = true. + c = true. Now we want to find out why > trace a - a => true + a = true | └─ :- true a :- b=true. | - └─ b => true + └─ b = true | └─ :- true b :- c=true. | - └─ c => true + └─ c = true | └─ |= true @@ -461,13 +483,13 @@ class REPL(cmd.Cmd, object): > trace bar(10,10) - bar(10,10) => 220 + bar(10,10) = 220 | ├─ += 110 │ │ bar(A=10, B=10) += (foo(A=10)=11 * B=10)=110. │ | - │ └─ foo(10) => 11 + │ └─ foo(10) = 11 │ | │ └─ = 11 │ @@ -481,7 +503,7 @@ class REPL(cmd.Cmd, object): > trace a - a => 2.0 + a = 2.0 | ├─ += 1 │ @@ -524,8 +546,15 @@ class REPL(cmd.Cmd, object): # retract newly added rules. for r in self.interp.new_rules: self.interp.retract_rule(r) - # drop $out chart - del self.interp.chart['$trace/0'] + + try: + # drop $out chart + del self.interp.chart['$trace/0'] + except KeyError: + # query must have failed. + pass + + self.interp.new_rules = set() do_load.__doc__ = do_load.__doc__.format(load=', '.join(load.available)) diff --git a/src/Dyna/Backend/Python/utils.py b/src/Dyna/Backend/Python/utils.py index 75e07dc..ccf50bd 100644 --- a/src/Dyna/Backend/Python/utils.py +++ b/src/Dyna/Backend/Python/utils.py @@ -74,7 +74,7 @@ def dynac(f, out, anf=None, compiler_args=()): assert not stdout.strip(), [stdout, stderr] # hide our temporary file's ugly sha1 file names from users. ugly_file_name = dotdynadir + '[a-z0-9/.]+\.dyna\S*' - stderr = re.sub(ugly_file_name, '', stderr) + stderr = re.sub(ugly_file_name, '', stderr) raise DynaCompilerError(stderr)