disposition for `$key/1`.
Removed previous version of `argmin=/argmax=`.
% single source shortest path with optimal path extraction.
-path(start) argmin= [0, start].
-path(B) argmin= [path(A) + edge(A,B), A].
-goal argmin= [path(end), end].
+path(start) min= 0 with_key start.
+path(B) min= path(A) + edge(A,B) with_key A.
+goal min= path(end) with_key end.
% expensive path
edge("a","b") := 1.
% extract cheapest path by following backpointers from each vertex.
bestpath(start) := [start].
-bestpath(V) := U is $key(&path(V)), [V | bestpath(U)] for V != start.
+bestpath(V) := U is $key(path(V)), [V | bestpath(U)] for V != start.
% the optimal path is the one from the `end`.
optimalpath = reverse(bestpath(end)).
% A and B are names of matrices
-times(A, B, I, J) += m(A, I, K) * m(B, K, J) whenever _ is product(A,B).
+times(A, B, I, J) += m(A, I, K) * m(B, K, J) for _ is product(A,B).
m(P, I, J) += shape(A,R,C), % pair(R, C) is shape(A),
shape(B,C,_), % pair(C, D) is shape(B),
b([X,Y,Z]) := [X,b(Y),b(Z)].
% CKY parser
-phrase(X,I,K) argmax= [phrase(Y,I,J) * phrase(Z,J,K) * p(X,Y,Z), [[Y,I,J], [Z,J,K]]].
-phrase(X,I,K) argmax= [phrase(Y,I,K) * p(X,Y), [[Y,I,K]]].
-phrase(X,I,I+1) argmax= [1, X] for [I,X] in enumerate(sentence).
+phrase(X,I,K) max= phrase(Y,I,J) * phrase(Z,J,K) * p(X,Y,Z) with_key [[Y,I,J], [Z,J,K]].
+phrase(X,I,K) max= phrase(Y,I,K) * p(X,Y) with_key [[Y,I,K]].
+phrase(X,I,I+1) max= 1 with_key X
+ for [I,X] in enumerate(sentence).
% backpointers
-bk(X,I,K) = $key(&phrase(X,I,K)).
+bk(X,I,K) = $key(phrase(X,I,K)).
% extract path from backpointers
path(X,I,K) := W is bk(X,I,K), W.
aggrs :: S.Set String
aggrs = S.fromList
- [ "max=" , "min=", "argmax=", "argmin="
+ [ "max=" , "min="
, "+=" , "*="
, "and=" , "or=" , "&=" , "|="
, ":-"
'set=': set_equals,
'bag=': bag_equals,
'mean=': mean_equals,
- 'argmax=': maxwithkey_equals,
- 'argmin=': minwithkey_equals,
+# 'argmax=': maxwithkey_equals,
+# 'argmin=': minwithkey_equals,
}
def aggregator(name, term):
self.do(self.dynac_code(code), initialize=False)
def new_fn(self, fn, agg):
-
# check for aggregator conflict.
if self.agg_name[fn] is None:
self.agg_name[fn] = agg
print >> out, ' %s' % (e)
print >> out
-# for item, (val, es) in self.error.items():
-# print >> out, 'because %r is %s:' % (item, _repr(val))
-# for e, h in es:
-# if h is not None:
-# r = h.rule
-# print >> out, ' %s\n in rule %s\n %s' % (e, r.span, r.src)
print >> out
def dump_rules(self):
return Cons(*args)
if fn == 'nil/0':
return Nil
+
+ if fn == '$key/1':
+ self.new_fn(fn, '=')
+
if fn not in self.agg_name:
- # item has no aggregator (e.g purely structural stuff) -- what
- # happens if we add one later?
+ # item has no aggregator and this is the first time we're seeing it.
self.new_fn(fn, None)
+
return self.chart[fn].insert(args)
# def retract_item(self, item):
item.value = now
continue
+ if hasattr(now, 'fn') and now.fn == 'with_key/2':
+ val, key = now.args
+ now = val
+ dkey = self.build('$key/1', item)
+ self.delete_emit(dkey, dkey.value, None, None)
+ self.emit(dkey, key, None, None, delete=False)
+
if was == now:
continue
- # aggregator with special key
- if hasattr(item.aggregator, 'key'):
- key = self.build('$key/1', item)
- if key.aggregator is None:
- from aggregator import aggregator
- key.aggregator = aggregator('=', key)
- self.delete_emit(key, key.value, None, None)
- self.emit(key, item.aggregator.key, None, None, delete=False)
-
-
was_error = False
if item in error: # clear error
was_error = True
def __setstate__(self, state):
(self.fn, self.args, self.value, self.aggregator) = state
- __add__ = __sub__ = __mul__ = notimplemented
+ def __add__(self, _):
+ raise TypeError("Can't subtract terms.")
+
+ def __sub__(self, _):
+ raise TypeError("Can't add terms.")
+
+ def __mul__(self, _):
+ raise TypeError("Can't multiply terms.")
+
+ def __div__(self, _):
+ raise TypeError("Can't divide terms.")
class Cons(Term):
, ("in" ,[(4,PFIn AssocNone ) ])
+ , ("with_key" ,[(4,PFIn AssocNone ) ])
+
, ("<=" ,[(4,PFIn AssocNone ) ])
, ("<" ,[(4,PFIn AssocNone ) ])
, ("=" ,[(4,PFIn AssocNone ) ])
, (("pair" ,2),(SDQuote,[ADEval,ADEval]))
, (("true" ,0),(SDQuote,[]))
, (("false",0),(SDQuote,[]))
+ -- key
+ , (("$key" ,1),(SDEval,[ADQuote]))
+ , (("with_key",2),(SDQuote,[ADEval, ADQuote]))
-- lists
, (("nil", 0),(SDQuote,[]))
, (("cons", 2),(SDQuote,[ADEval,ADEval]))
-- lists
, (("nil", 0),(SDQuote,[]))
, (("cons", 2),(SDQuote,[ADEval,ADEval]))
+ -- key
+ , (("$key" ,1),(SDEval,[ADQuote]))
+ , (("with_key",2),(SDQuote,[ADEval, ADQuote]))
]
------------------------------------------------------------------------}}}