From: Tim Vieira Date: Sat, 6 Jul 2013 16:52:46 +0000 (-0400) Subject: Issue #38 "better printing of :- items, and better type checking for :-, |=, &=" X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=47f84af325cf768e67e43fc7eecb4d779dd60056;p=dyna2 Issue #38 "better printing of :- items, and better type checking for :-, |=, &=" `:-` and `|=` are now synonymous. --- diff --git a/dyna-doctest b/dyna-doctest index 6d8abd8..c28b23d 100755 --- a/dyna-doctest +++ b/dyna-doctest @@ -1,3 +1,3 @@ #!/usr/bin/env bash -exec python ${DYNAHOME:-.}/src/Dyna/Backend/Python/dyna-doctest.py "$@" +exec python ${DYNAHOME:-.}/src/Dyna/Backend/Python/dyna_doctest.py "$@" diff --git a/src/Dyna/Backend/Python/aggregator.py b/src/Dyna/Backend/Python/aggregator.py index 664a60d..3a5ca30 100644 --- a/src/Dyna/Backend/Python/aggregator.py +++ b/src/Dyna/Backend/Python/aggregator.py @@ -193,17 +193,25 @@ class or_equals(BAggregator): if len(s): return reduce(lambda x,y: x or y, s) -class b_and_equals(BAggregator): + +class boolean_or_equals(BAggregator): def fold(self): - s = [k for k, m in self.iteritems() if m > 0] + s = [x for x, m in self.iteritems() if m > 0] if len(s): - return reduce(operator.and_, s) + for val in s: + if val is not True and val is not False: + raise TypeError('%s is not Boolean.' % _repr(val)) + + return reduce(lambda x,y: x or y, s) -class b_or_equals(BAggregator): +class boolean_and_equals(BAggregator): def fold(self): - s = [k for k, m in self.iteritems() if m > 0] + s = [x for x, m in self.iteritems() if m > 0] if len(s): - return reduce(operator.or_, s) + for val in s: + if val is not True and val is not False: + raise TypeError('%s is not Boolean.' % _repr(val)) + return reduce(lambda x,y: x and y, s) class set_equals(BAggregator): def fold(self): @@ -226,9 +234,9 @@ defs = { '*=': times_equals, 'and=': and_equals, 'or=': or_equals, - '&=': b_and_equals, - '|=': b_or_equals, - ':-': or_equals, + '&=': boolean_and_equals, + '|=': boolean_or_equals, + ':-': boolean_or_equals, 'majority=': majority_equals, 'set=': set_equals, 'bag=': bag_equals, diff --git a/src/Dyna/Backend/Python/chart.py b/src/Dyna/Backend/Python/chart.py index cda1156..0ef1c9a 100644 --- a/src/Dyna/Backend/Python/chart.py +++ b/src/Dyna/Backend/Python/chart.py @@ -20,9 +20,23 @@ class Chart(object): rows = [term for term in self.intern.values() if term.value is not None] if not rows: return '' + + # special handing or-equals aggregators -- only list true facts (and errors) + if self.agg_name == ':-' or self.agg_name == '|=': + lines = [] + for term in sorted(rows): + if term.value is True: + lines.append('%s.' % _repr(term)) + elif term.value: # e.g. $error + lines.append('%s = %s.' % (_repr(term), _repr(term.value))) + if self.arity != 0: + lines.append('') + return '\n'.join(lines) + if self.arity == 0: [term] = rows 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) diff --git a/test/repl/boolean-aggregators.dynadoc b/test/repl/boolean-aggregators.dynadoc new file mode 100644 index 0000000..4c98d5f --- /dev/null +++ b/test/repl/boolean-aggregators.dynadoc @@ -0,0 +1,81 @@ +% Dynamic type checking for Boolean aggregators. + +> e :- 10. + +Changes +======= +e = $error. + +> sol + +Solution +======== +e = $error. + +Errors +====== +Error(s) aggregating e/0: + TypeError: + `e`: 10 is not Boolean. + +> retract_rule 0 + +Changes +======= +e = null. + + + + +> a :- false. + +Changes +======= +a = false. + +> a :- true. + +Changes +======= +a = true. + + +> c := "horse". +| b &= c. +| b &= true. + +Changes +======= +b = $error. +c = "horse". + + +> sol + +Solution +======== +a. +b = $error. +c = "horse". + +Errors +====== +Error(s) aggregating b/0: + TypeError: + `b`: "horse" is not Boolean. + + +> c := true. + +Changes +======= +b = true. +c = true. + +> sol + +Solution +======== +a. +b = true. +c = true.