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):
'*=': 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,
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)
--- /dev/null
+% 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.