From: timv Date: Thu, 13 Dec 2012 02:52:08 +0000 (-0500) Subject: simpl, flat, replacement updates in python script. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=a7ec76a2499c409d8129c98870f8bc9cb0eb5982;p=dyna2 simpl, flat, replacement updates in python script. --- diff --git a/bin/defn.py b/bin/defn.py index a165201..89eed96 100644 --- a/bin/defn.py +++ b/bin/defn.py @@ -32,9 +32,9 @@ call = {'*/2': operator.mul, '-/1': operator.neg, -# '~/1': operator.inv, # differs -# '|/1': operator.or_, -# '&/2': operator.and_, + '~/1': lambda x: not x, + '|/1': lambda x,y: x or y, + '&/2': lambda x,y: x and y, # comparisons '>> emit(('rewrite/3', 5), -1000) -# >>> run_agenda() - - [dyna] = sys.argv[1:] cmd = """ghc -isrc Dyna.Backend.Python -e 'processFile "%s"' """ % dyna @@ -298,4 +301,62 @@ for init in initializer.handlers: init() # start running the agenda -run() +go() + + + +class UserChart(object): + + def __init__(self, _chart): + self.ncols = _chart.ncols + self.data = _chart.data + self.name = _chart.name + self._chart = _chart + + def __getitem__(self, item): + assert isinstance(item, tuple) and len(item) == self.ncols - 1 + nonslice = [(i, val) for i, val in enumerate(item) if not isinstance(val, slice)] + for idx, row in self.data.iteritems(): + if row[-1] is None: + continue + if all(row[i] == val for i, val in nonslice): + yield (self.name, idx) + + def __setitem__(self, item, now): + # XXX: can't add new things only change old things.. + + assert isinstance(item, tuple) and len(item) == self.ncols - 1 + nonslice = [(i, v) for i, v in enumerate(item) if not isinstance(v, slice)] + + foundone = False + for idx, row in self.data.iteritems(): + if all(row[i] == val for i, val in nonslice): + item = (self.name, idx) + + # timv: technically, should restrict to ":=" and extensional + aggregator[item].clear() + aggregator[item][now] += 1 + agenda.add(item) + + foundone = True + + if not foundone and len(nonslice) == len(item): + idx = self._chart.insert(item + (None,)) + item = (self.name, idx) + aggregator[item].clear() + aggregator[item][now] += 1 + agenda.add(item) + + go() + + +#constructor_names = {''.join(functor.split('/')[:-1]) for functor in chart} +for _fn in chart: + exec '%s = UserChart(chart[%r])' % (_fn.replace('/', ''), _fn) + +#def phrase(A,I,K): +# return chart['phrase/3'] + +rewrite3["VP","VP",rewrite3["VP","VP","PP"]] = -100 + +from debug import ip; ip() diff --git a/examples/papa2.dyna b/examples/papa2.dyna index 1dbe959..42a4bfb 100644 --- a/examples/papa2.dyna +++ b/examples/papa2.dyna @@ -38,4 +38,4 @@ word( ".", 7) += 1. % for fun, try making the above words facts instead of +=1's. -phrase(W, I, I+1, W) += 1 whenever word(W, I). +phrase(W, I, I+1, W) += (word(W, I) > 0) & 1.