]> hydra-www.ietfng.org Git - dyna2/commitdiff
Issue #38 "better printing of :- items, and better type checking for :-, |=, &="
authorTim Vieira <tim.f.vieira@gmail.com>
Sat, 6 Jul 2013 16:52:46 +0000 (12:52 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Sat, 6 Jul 2013 16:52:46 +0000 (12:52 -0400)
`:-` and `|=` are now synonymous.

dyna-doctest
src/Dyna/Backend/Python/aggregator.py
src/Dyna/Backend/Python/chart.py
test/repl/boolean-aggregators.dynadoc [new file with mode: 0644]

index 6d8abd858cd9f33c3f7729fe6060b95b968dc7e2..c28b23d3826b71d93d3728838cf1e805995b6758 100755 (executable)
@@ -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 "$@"
index 664a60da51335377f3160ec41f61ce1b6b48ce46..3a5ca30957cba8ca3a3ead5e6fbdcd7a9e3a75ee 100644 (file)
@@ -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,
index cda1156e2375c1ca26dfeb674ba64610eec53cf0..0ef1c9aa125fe42a3ea65a006acd5cb862d60362 100644 (file)
@@ -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 (file)
index 0000000..4c98d5f
--- /dev/null
@@ -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.