I think update() should be done better...
This commit is contained in:
Simon Forman
2018-06-30 12:59:14 -07:00
parent 4406a6620b
commit ab1f5227ba
5 changed files with 478 additions and 271 deletions
+3 -47
View File
@@ -168,7 +168,7 @@ def unify(u, v, s=None):
elif isinstance(u, tuple) and isinstance(v, tuple):
if len(u) != 2 or len(v) != 2:
raise JoyTypeError('Cannot unify %r and %r.' % (u, v))
raise ValueError(repr((u, v))) # Bad input.
(a, b), (c, d) = v, u
if isinstance(a, KleeneStar):
@@ -442,21 +442,16 @@ FUNCTIONS['loop'] = CombinatorJoyType('loop', [loop_two_true, loop_true, loop_fa
def set_expectations():
branch.expect = s7, (s6, (b1, s5))
loop.expect = s6, (b1, s5)
i.expect = x.expect = s7, s6
i.expect = nullary.expect = x.expect = s7, s6
dip.expect = dupdip.expect = s8, (a8, s7)
dipd.expect = s8, (a8, (a7, s7))
dipdd.expect = s8, (a8, (a7, (a6, s7)))
b.expect = concat_.expect = infra.expect = s8, (s7, s6)
nullary.expect = s7, s6
scope = globals().copy()
scope.update(FUNCTIONS)
eval(set_expectations.func_code, scope)
#NULLARY = infer(((stack, s3), (dip, (infra, (first, ())))))
##print NULLARY
# Type Checking...
def _ge(self, other):
@@ -467,43 +462,4 @@ def _ge(self, other):
AnyJoyType.__ge__ = _ge
AnyJoyType.accept = tuple, int, float, long, str, unicode, bool, Symbol
StackJoyType.accept = tuple
##if __name__ == '__main__':
##
## from joy.parser import text_to_expression
## from joy.utils.stack import list_to_stack as l2s
##
##
## F = infer(l2s((pop, pop, pop)))
## for f in F:
## print doc_from_stack_effect(*f)
## s = text_to_expression('0 1 2')
## L = unify(s, F[0][0])
## print L
##
## print
##
## F = infer(l2s((pop, swap, rolldown, rest, rest, cons, cons)))
## for f in F:
## print doc_from_stack_effect(*f)
## s = text_to_expression('0 1 2 [3 4]')
## L = unify(s, F[0][0])
## print L
## print
##
## g = update(L[0], F[0])
## print doc_from_stack_effect(*g)
## print g
##
##
## print '- - - - - -'
## s = text_to_expression('[3 4]')
## L = unify(s, F[0][1])
## print L
## print
##
## g = update(L[0], F[0])
## print doc_from_stack_effect(*g)
## print g
##
FloatJoyType.accept = float
+19 -1
View File
@@ -26,6 +26,10 @@ class AnyJoyType(object):
def __ge__(self, other):
return issubclass(other.__class__, self.__class__)
def __le__(self, other):
# 'a string' >= AnyJoyType() should be False.
return issubclass(self.__class__, other.__class__)
def __add__(self, other):
return self.__class__(self.number + other)
__radd__ = __add__
@@ -53,9 +57,23 @@ def update(s, term):
'''
Apply substitution dict to term, returning new term.
'''
t = _update(s, term)
n = 10
while t != term:
n -= 1
if not n:
print t
print term
print 'lalala'
1/0
term = t
t = _update(s, term)
return term
def _update(s, term):
if not isinstance(term, tuple):
return s.get(term, term)
return tuple(update(s, inner) for inner in term)
return tuple(_update(s, inner) for inner in term)
def relabel(left, right):