From c9f405fbe92f71f192dc3a5c37ab9eb6833b0cec Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 17 Jul 2018 20:09:17 -0700 Subject: [PATCH] Nearly there maybe, maybe not. --- joy/library.py | 31 +++++++++++++++++++++-- joy/utils/polytypes.py | 69 +++++++++++++------------------------------------- joy/utils/types.py | 21 +++++++++++---- 3 files changed, 63 insertions(+), 58 deletions(-) diff --git a/joy/library.py b/joy/library.py index 5e9bba3..5e59dcc 100644 --- a/joy/library.py +++ b/joy/library.py @@ -54,6 +54,7 @@ from .utils.types import ( infer, JoyTypeError, combinator_effect, + poly_combinator_effect, ) @@ -926,7 +927,7 @@ def x(stack, expression, dictionary): @inscribe -#@combinator_effect(_COMB_NUMS(), s7, s6) +@combinator_effect(_COMB_NUMS(), s7, s6) @FunctionWrapper def b(stack, expression, dictionary): ''' @@ -943,6 +944,7 @@ def b(stack, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a1, s1) @FunctionWrapper def dupdip(stack, expression, dictionary): ''' @@ -962,7 +964,7 @@ def dupdip(stack, expression, dictionary): @inscribe -#@combinator_effect(_COMB_NUMS(), s7, s6) +@combinator_effect(_COMB_NUMS(), s7, s6) @FunctionWrapper def infra(stack, expression, dictionary): ''' @@ -980,6 +982,7 @@ def infra(stack, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), s7, s6, s5, s4) @FunctionWrapper def genrec(stack, expression, dictionary): ''' @@ -1041,6 +1044,7 @@ def genrec(stack, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), s7, s6) @FunctionWrapper def map_(S, expression, dictionary): ''' @@ -1078,7 +1082,18 @@ def map_(S, expression, dictionary): # return (q, (p, stack)), expression, dictionary +def branch_true(stack, expression, dictionary): + (then, (else_, (flag, stack))) = stack + return stack, concat(then, expression), dictionary + + +def branch_false(stack, expression, dictionary): + (then, (else_, (flag, stack))) = stack + return stack, concat(else_, expression), dictionary + + @inscribe +@poly_combinator_effect(_COMB_NUMS(), [branch_true, branch_false], b1, s7, s6) @FunctionWrapper def branch(stack, expression, dictionary): ''' @@ -1103,6 +1118,9 @@ def branch(stack, expression, dictionary): return stack, concat(then if flag else else_, expression), dictionary +#FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100) + + ##@inscribe ##@FunctionWrapper ##def ifte(stack, expression, dictionary): @@ -1192,6 +1210,7 @@ def dip(stack, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a2, a1, s1) @FunctionWrapper def dipd(S, expression, dictionary): ''' @@ -1209,6 +1228,7 @@ def dipd(S, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a3, a2, a1, s1) @FunctionWrapper def dipdd(S, expression, dictionary): ''' @@ -1226,6 +1246,7 @@ def dipdd(S, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a1, s1) @FunctionWrapper def app1(S, expression, dictionary): ''' @@ -1245,6 +1266,7 @@ def app1(S, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a2, a1, s1) @FunctionWrapper def app2(S, expression, dictionary): '''Like app1 with two items. @@ -1265,6 +1287,7 @@ def app2(S, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a3, a2, a1, s1) @FunctionWrapper def app3(S, expression, dictionary): '''Like app1 with three items. @@ -1287,6 +1310,7 @@ def app3(S, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), s7, s6) @FunctionWrapper def step(S, expression, dictionary): ''' @@ -1322,6 +1346,7 @@ def step(S, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), i1, s6) @FunctionWrapper def times(stack, expression, dictionary): ''' @@ -1371,6 +1396,7 @@ def times(stack, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), b1, s6) @FunctionWrapper def loop(stack, expression, dictionary): ''' @@ -1393,6 +1419,7 @@ def loop(stack, expression, dictionary): @inscribe +@combinator_effect(_COMB_NUMS(), a1, a2, s6, s7, s8) @FunctionWrapper def cmp_(stack, expression, dictionary): ''' diff --git a/joy/utils/polytypes.py b/joy/utils/polytypes.py index 9ce03b1..cebe0d9 100644 --- a/joy/utils/polytypes.py +++ b/joy/utils/polytypes.py @@ -17,32 +17,12 @@ from joy.utils.stack import ( expression_to_string, list_to_stack, ) -# from joy.utils.types import ( -# AnyJoyType, A, -# BooleanJoyType, B, -# DEFS, -# doc_from_stack_effect, -# FloatJoyType, F, -# JoyTypeError, -# NumberJoyType, N, -# StackJoyType, S, -# _stacky, -# _R, -# relabel, delabel, -# reify, -# ) - -'''Docstring for functions in Sphinx?''' - - -def defs(): - ''' - Return a dict of FunctionJoyType instances to be used with ``infer()``. - ''' - - average = sum_ = product = min_ = max_ = [(((Ns[1], s1), s0), (n0, s0))] - flatten = [(((Ss[1], s1), s0), (s2, s0))] + average = + # sum_ = + # product = + # min_ = max_ = [(((Ns[1], s1), s0), (n0, s0))] + # flatten = [(((Ss[1], s1), s0), (s2, s0))] return { name.rstrip('_'): stack_effect @@ -57,21 +37,7 @@ FUNCTIONS.update({ FUNCTIONS.update({ combo.__name__: CombinatorJoyType(combo.__name__, [combo], i) for i, combo in enumerate(( - joy.library.b, joy.library.concat_, - joy.library.dip, - joy.library.dipd, - joy.library.dipdd, - joy.library.dupdip, - joy.library.i, - joy.library.infra, - joy.library._dictionary['nullary'], - joy.library._dictionary['unary'], - joy.library._dictionary['binary'], - joy.library._dictionary['ternary'], - joy.library._dictionary['quoted'], - joy.library._dictionary['unquoted'], - joy.library._dictionary['enstacken'], joy.library._dictionary['disenstacken'], joy.library.x, )) @@ -121,19 +87,20 @@ def set_expectations_of_definition(cjt): def set_expectations(): - branch.expect = s7, (s6, (b1, s5)) + loop.expect = s6, (b1, s5) - 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) - set_expectations_of_definition(unary) - set_expectations_of_definition(binary) - set_expectations_of_definition(ternary) - set_expectations_of_definition(quoted) - set_expectations_of_definition(unquoted) - set_expectations_of_definition(enstacken) +# 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))) + concat_.expect = s8, (s7, s6) +# b.expect = infra.expect = s8, (s7, s6) + # set_expectations_of_definition(unary) + # set_expectations_of_definition(binary) + # set_expectations_of_definition(ternary) + # set_expectations_of_definition(quoted) + # set_expectations_of_definition(unquoted) + # set_expectations_of_definition(enstacken) disenstacken.expect = (As[1], s1), s0 scope = globals().copy() scope.update(FUNCTIONS) diff --git a/joy/utils/types.py b/joy/utils/types.py index fe4c6a8..7c41005 100644 --- a/joy/utils/types.py +++ b/joy/utils/types.py @@ -665,11 +665,12 @@ def ef(*inputs): def combinator_effect(number, *expect): - def _combinator_effect(c): - C = FUNCTIONS[c.name] = CombinatorJoyType(c.name, [c], number) - if expect: C.expect = __(*expect) - return c - return _combinator_effect + def _combinator_effect(c): + e = __(*expect) if expect else None + C = FUNCTIONS[c.name] = CombinatorJoyType(c.name, [c], number, e) + if expect: C.expect = __(*expect) + return c + return _combinator_effect def show(DEFS): @@ -693,3 +694,13 @@ def generate_library_code(DEFS, f=None): ##if __name__ == '__main__': ## show() + +def poly_combinator_effect(number, effect_funcs, *expect): + def _poly_combinator_effect(c): + e = __(*expect) if expect else None + FUNCTIONS[c.name] = CombinatorJoyType(c.name, effect_funcs, number, e) + return c + return _poly_combinator_effect + +#FUNCTIONS['branch'].expect = s7, (s6, (b1, s5)) + -- 2.11.0