2 In order to debug the problem I'm having with definitions breaking a
3 zipper expression I need to load a system, load the defs into a dictionary,
4 and then run the expression and see if it raises an exception, substituting
5 definitions one-at-a-time until I find the one that breaks it.
8 from joy.library import default_defs, initialize, inscribe, Def
9 from joy.joy import joy
10 from joy.parser import text_to_expression
11 from joy.utils.pretty_print import trace
12 from joy.utils.stack import stack_to_string
17 dictionary = initialize()
23 expression = text_to_expression(
24 '[1 [2 [3 4 25 6] 7] 8]'
26 '[dip dip infra dip infra dip infra]'
27 ## '[[] ccons] step i'
29 # to trace replace last line above with:
34 expected_result = '[1 [2 [3 4 625 6] 7] 8]'
35 expected_result_as_stack = text_to_expression(expected_result)
40 Run the test expression with the defs in ds.
41 Return the resulting stack as a string or the
42 exception raised if any.
47 stack, _, _ = joy((), expression, D)
48 except Exception as err:
50 return stack_to_string(stack)
53 # The problem is that it works with the built-ins:
55 ##print(test_expr({}))
58 # [1 [2 [3 4 625 6] 7] 8]
60 # But not with the definitions:
62 ##print(test_expr(defs))
65 # not enough values to unpack (expected 2, got 0)
67 # This obviously sucks and is bad. :(
69 # First, because it's easy, let's try adding single defs
70 # one-at-a-time to the dictionary and see if any one of
73 # Only the defs that shadow the built-ins could be the problem:
74 candidates = set(dictionary) & set(defs)
76 ##for def_name in candidates:
77 ## stack_str = test_expr({def_name: defs[def_name]})
78 ## if stack_str != expected_result:
79 ## print(def_name, 'failed!')
86 # Ah yes, step's definition has parts (and dependencies).
92 for name in ('?', 'dupdipd', 'popopop'):
93 step_defs[name] = defs[name]
94 ##print(sorted(step_defs))
95 ##print(test_expr(step_defs))
98 # [1 [2 [3 4 625 6] 7] 8]
100 # So it's not step by itself, it's some combination of defintions
101 # that is causing the bug.
103 its_is_probably_not = set('''
108 def_name: defs[def_name]
110 if (def_name in candidates
111 or def_name in step_defs)
112 and def_name not in its_is_probably_not
115 ##print(test_expr(sus_defs))
118 d['uncons'] = defs['uncons']
119 d['cleave'] = defs['cleave']
120 d['fork'] = defs['fork']
122 ##print(test_expr(d))
126 for name in candidates
129 CD['codi'] = defs['codi']
130 CD['swapd'] = defs['swapd']
131 CD['cleave'] = defs['cleave']
132 CD['fork'] = defs['fork']
133 CD['grba'] = defs['grba']
134 CD['infrst'] = defs['infrst']
136 ##print(test_expr(CD))
139 # [++, --, '?', _step0, _step1, _stept, abs, app1, app2, app3, at, b, ccons, clear, 'cleave', 'codi', dipd, disenstacken, drop, dupd, dupdd, dupdip, 'dupdipd', 'fork', fourth, genrec, 'grba', ii, infra, 'infrst', map, mod, neg, not, pm, popd, popdd, popop, popopd, popopdd, 'popopop', rest, reverse, roll<, roll>, rolldown, rollup, rrest, second, shunt, step, step_zero, sum, 'swapd', swons, take, third, times, tuck, uncons, unit, unswons, x]
155 ##del CD['cleave'] # <-- dep
158 del CD['disenstacken']
164 ##del CD['fork'] # <-- dep
167 ##del CD['grba'] # <-- dep
170 ##del CD['infrst'] # <-- dep
177 ##del CD['popdd'] # <-- !!!!!
191 ##del CD['step'] # <-- !!!!!
200 ##del CD['uncons'] # <-- popopop !?
225 ##print(set(dictionary) & set(defs))