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 -219
View File
@@ -221,8 +221,11 @@ class DefinitionWrapper(object):
self.body = text_to_expression(body_text)
self._body = tuple(iter_stack(self.body))
self.__doc__ = doc or body_text
self._compiled = None
def __call__(self, stack, expression, dictionary):
if self._compiled:
return self._compiled(stack, expression, dictionary)
expression = list_to_stack(self._body, expression)
return stack, expression, dictionary
@@ -278,32 +281,6 @@ def parse(stack):
return expression, stack
##@inscribe
##@SimpleFunctionWrapper
##def first(stack):
## '''
## ::
##
## first == uncons pop
##
## '''
## ((head, tail), stack) = stack
## return head, stack
##@inscribe
##@SimpleFunctionWrapper
##def rest(stack):
## '''
## ::
##
## rest == uncons popd
##
## '''
## ((head, tail), stack) = stack
## return tail, stack
@inscribe
@SimpleFunctionWrapper
def getitem(stack):
@@ -490,30 +467,6 @@ def sort_(S):
return list_to_stack(sorted(iter_stack(tos))), stack
##@inscribe
##@SimpleFunctionWrapper
##def cons(S):
## '''
## The cons operator expects a list on top of the stack and the potential
## member below. The effect is to add the potential member into the
## aggregate.
## '''
## (tos, (second, stack)) = S
## return (second, tos), stack
##@inscribe
##@SimpleFunctionWrapper
##def uncons(S):
## '''
## Inverse of cons, removes an item from the top of the list on the stack
## and places it under the remaining list.
## '''
## (tos, stack) = S
## item, tos = tos
## return tos, (item, stack)
@inscribe
@SimpleFunctionWrapper
def clear(stack):
@@ -527,72 +480,6 @@ def clear(stack):
return ()
##@inscribe
##@SimpleFunctionWrapper
##def dup(S):
## '''Duplicate the top item on the stack.'''
## (tos, stack) = S
## return tos, (tos, stack)
##@inscribe
##@SimpleFunctionWrapper
##def over(S):
## '''
## Copy the second item down on the stack to the top of the stack.
## ::
##
## a b over
## --------------
## a b a
##
## '''
## second = S[1][0]
## return second, S
##@inscribe
##@SimpleFunctionWrapper
##def tuck(S):
## '''
## Copy the item at TOS under the second item of the stack.
## ::
##
## a b tuck
## --------------
## b a b
##
## '''
## (tos, (second, stack)) = S
## return tos, (second, (tos, stack))
##@inscribe
##@SimpleFunctionWrapper
##def swap(S):
## '''Swap the top two items on stack.'''
## (tos, (second, stack)) = S
## return second, (tos, stack)
##@inscribe
##@SimpleFunctionWrapper
##def swaack(stack):
## '''swap stack'''
## old_stack, stack = stack
## return stack, old_stack
##@inscribe
##@SimpleFunctionWrapper
##def stack_(stack):
## '''
## The stack operator pushes onto the stack a list containing all the
## elements of the stack.
## '''
## return stack, stack
@inscribe
@SimpleFunctionWrapper
def unstack(stack):
@@ -603,44 +490,6 @@ def unstack(stack):
return stack[0]
##@inscribe
##@SimpleFunctionWrapper
##def pop(stack):
## '''Pop and discard the top item from the stack.'''
## return stack[1]
##@inscribe
##@SimpleFunctionWrapper
##def popd(stack):
## '''Pop and discard the second item from the stack.'''
## (tos, (_, stack)) = stack
## return tos, stack
##@inscribe
##@SimpleFunctionWrapper
##def popdd(stack):
## '''Pop and discard the third item from the stack.'''
## (tos, (second, (_, stack))) = stack
## return tos, (second, stack)
##@inscribe
##@SimpleFunctionWrapper
##def popop(stack):
## '''Pop and discard the first and second items from the stack.'''
## return stack[1][1]
##@inscribe
##@SimpleFunctionWrapper
##def dupd(S):
## '''Duplicate the second item on the stack.'''
## (tos, (second, stack)) = S
## return tos, (second, (second, stack))
@inscribe
@SimpleFunctionWrapper
def reverse(S):
@@ -771,36 +620,6 @@ def sqrt(a):
return r
##@inscribe
##@SimpleFunctionWrapper
##def rollup(S):
## '''
## ::
##
## a b c
## -----------
## b c a
##
## '''
## (a, (b, (c, stack))) = S
## return b, (c, (a, stack))
##@inscribe
##@SimpleFunctionWrapper
##def rolldown(S):
## '''
## ::
##
## a b c
## -----------
## c a b
##
## '''
## (a, (b, (c, stack))) = S
## return c, (a, (b, stack))
#def execute(S):
# (text, stack) = S
# if isinstance(text, str):
@@ -1448,42 +1267,7 @@ def cmp_(stack, expression, dictionary):
return stack, expression, dictionary
#def nullary(S, expression, dictionary):
# '''
# Run the program on TOS and return its first result without consuming
# any of the stack (except the program on TOS.)
# '''
# (quote, stack) = S
# result = joy(stack, quote, dictionary)
# return (result[0][0], stack), expression, dictionary
#
#
#def unary(S, expression, dictionary):
# (quote, stack) = S
# _, return_stack = stack
# result = joy(stack, quote, dictionary)[0]
# return (result[0], return_stack), expression, dictionary
#
#
#def binary(S, expression, dictionary):
# (quote, stack) = S
# _, (_, return_stack) = stack
# result = joy(stack, quote, dictionary)[0]
# return (result[0], return_stack), expression, dictionary
#
#
#def ternary(S, expression, dictionary):
# (quote, stack) = S
# _, (_, (_, return_stack)) = stack
# result = joy(stack, quote, dictionary)[0]
# return (result[0], return_stack), expression, dictionary
# FunctionWrapper(binary),
# FunctionWrapper(cleave),
# FunctionWrapper(nullary),
# FunctionWrapper(ternary),
# FunctionWrapper(unary),
# FunctionWrapper(while_),
+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):