self[val] -= 1
def fromkeys(self, *_):
assert False, "This method should never be called."
+ def empty(self):
+ return not any(m > 0 for m in self.itervalues())
class ColonEquals(BAggregator):
self[val, vs] -= 1
def fold(self):
- from stdlib import todyna
- return todyna([b + (('$val', v),) for (v, b), cnt in self.iteritems() if cnt > 0])
+ if not self.empty():
+ from stdlib import todyna
+ return todyna([b + (('$val', v),) for (v, b), cnt in self.iteritems() if cnt > 0])
class majority_equals(BAggregator):
self.recompute_coarse()
+ self._agenda()
+
# if there are no more rules defining a functor; clear some of the state
if not self.rule_by_head[rule.head_fn]:
if rule.head_fn in self.chart:
- del self.chart[rule.head_fn] # delete the chart.
+ # clear aggregators
+ chart = self.chart[rule.head_fn]
+ chart.agg_name = None
+ for item in chart.intern.values():
+ item.aggregator = None
+ # make sure chart is cleared out
+ for item in chart.intern.values():
+ assert item.value is None
+ assert item not in self.error
if rule.head_fn in self.agg_name:
del self.agg_name[rule.head_fn]
if rule.head_fn in self.pstate[2]:
del self.pstate[2][rule.head_fn] # remove fn aggr def from parser state
- self._agenda()
return self.changed
def recompute_gbc_memo(self, fn, visited=None):
self.aggregator = None
def __eq__(self, other):
- try:
- return (self.fn, self.args) == (other.fn, other.args)
- except AttributeError:
- return False
-
- def __hash__(self):
- return hash((self.fn, self.args))
+ return self is other
def __cmp__(self, other):
try:
return '%s(%s)' % (fn, ','.join(map(_repr, self.args)))
-class Cons(Term):
+class NoIntern(Term):
+ "Mix-in which adds hash and equality method for terms which aren't interned."
+ def __eq__(self, other):
+ try:
+ return (self.fn, self.args) == (other.fn, other.args)
+ except AttributeError:
+ return False
+ def __hash__(self):
+ return hash((self.fn, self.args))
+
+
+class Cons(NoIntern, Term):
def __init__(self, head, tail):
if not (isinstance(tail, Cons) or tail is Nil):
def __repr__(self):
return '[%s]' % (', '.join(map(_repr, self.aslist)))
-# def __contains__(self, x):
-# if x in self.aslist:
-# return true
-# else:
-# return false
-
def like_chart(self):
for a in self.aslist:
if not isinstance(a, Term):
def __iter__(self):
return iter(self.aslist)
-# def __eq__(self, other):
-# try:
-# return self.aslist == other.aslist
-# except AttributeError:
-# return False
-# def __hash__(self):
-# return hash(tuple(self.aslist))
-
-# def __cmp__(self, other):
-# try:
-# return cmp(self.aslist, other.aslist)
-# except AttributeError:
-# return
-
-
-class Error(Term):
+class Error(NoIntern, Term):
def __init__(self):
Term.__init__(self, '$error/0', ())
def __iter__(self):
return iter([])
-# def __eq__(self, other):
-# try:
-# return self.aslist == other.aslist
-# except AttributeError:
-# return False
-
-# def __cmp__(self, other):
-# try:
-# return cmp(self.aslist, other.aslist)
-# except AttributeError:
-# return 1
-
Nil = _Nil()
-class MapsTo(Term):
+class MapsTo(NoIntern, Term):
def __init__(self, k, v):
super(MapsTo, self).__init__('->/2', (k, v))
def __repr__(self):