This repo is not current. Development has moved from Hg to Git. For the latest code use the "Source Code" tab above to go to the "Thun" git repo or navigate to:
https://osdn.net/projects/joypy/scm/git/Thun
Revision | 0d6ddc806c69ff4021a09bbbb5d4206b8a6fdf68 (tree) |
---|---|
Time | 2018-07-18 12:52:57 |
Author | Simon Forman <sforman@hush...> |
Commiter | Simon Forman |
Definitions infer stack effects.
At start-up defs that DON'T type check are permitted (so you can use e.g.
loop, map, etc.) in the definitions "source", but then the user-facing
inscribe command only allows you to define new commands that DO
type-check. The ideal solution here is to get inference working for the
loopy words. (In the meantime you can select and execute their
definition text directly. That's not (yet!) type-checked.)
@@ -23,7 +23,7 @@ | ||
23 | 23 | from joy.joy import run |
24 | 24 | from joy.parser import Symbol |
25 | 25 | from joy.utils.stack import stack_to_string |
26 | -from joy.utils.polytypes import type_check | |
26 | +from joy.utils.types import type_check | |
27 | 27 | |
28 | 28 | |
29 | 29 | def is_numerical(s): |
@@ -236,6 +236,7 @@ | ||
236 | 236 | make_generator == [codireco] ccons |
237 | 237 | ifte == [nullary not] dipd branch |
238 | 238 | ''' |
239 | +# | |
239 | 240 | # |
240 | 241 | # ifte == [nullary] dipd swap branch |
241 | 242 | # genrec == [[genrec] cons cons cons cons] nullary swons concat ifte |
@@ -365,17 +366,20 @@ | ||
365 | 366 | class_.add_def(definition, dictionary) |
366 | 367 | |
367 | 368 | @classmethod |
368 | - def add_def(class_, definition, dictionary): | |
369 | + def add_def(class_, definition, dictionary, fail_fails=False): | |
369 | 370 | ''' |
370 | 371 | Add the definition to the dictionary. |
371 | 372 | ''' |
372 | 373 | F = class_.parse_definition(definition) |
373 | 374 | try: |
374 | - #print F._body | |
375 | + # print F.name, F._body | |
375 | 376 | secs = infer(*F._body) |
376 | 377 | except JoyTypeError: |
377 | 378 | pass |
378 | 379 | print F.name, '==', expression_to_string(F.body), ' --failed to infer stack effect.' |
380 | + if fail_fails: | |
381 | + print 'Function not inscribed.' | |
382 | + return | |
379 | 383 | else: |
380 | 384 | FUNCTIONS[F.name] = SymbolJoyType(F.name, secs, _SYM_NUMS()) |
381 | 385 | dictionary[F.name] = F |
@@ -405,7 +409,7 @@ | ||
405 | 409 | the definitions.txt resource. |
406 | 410 | ''' |
407 | 411 | definition, stack = stack |
408 | - DefinitionWrapper.add_def(definition, dictionary) | |
412 | + DefinitionWrapper.add_def(definition, dictionary, fail_fails=True) | |
409 | 413 | return stack, expression, dictionary |
410 | 414 | |
411 | 415 |
@@ -652,6 +656,7 @@ | ||
652 | 656 | |
653 | 657 | |
654 | 658 | @inscribe |
659 | +@combinator_effect(_COMB_NUMS(), s7, s6) | |
655 | 660 | @SimpleFunctionWrapper |
656 | 661 | def concat_(S): |
657 | 662 | '''Concatinate the two lists on the top of the stack. |
@@ -982,7 +987,7 @@ | ||
982 | 987 | |
983 | 988 | |
984 | 989 | @inscribe |
985 | -@combinator_effect(_COMB_NUMS(), s7, s6, s5, s4) | |
990 | +#@combinator_effect(_COMB_NUMS(), s7, s6, s5, s4) | |
986 | 991 | @FunctionWrapper |
987 | 992 | def genrec(stack, expression, dictionary): |
988 | 993 | ''' |
@@ -1396,7 +1401,7 @@ | ||
1396 | 1401 | |
1397 | 1402 | |
1398 | 1403 | @inscribe |
1399 | -@combinator_effect(_COMB_NUMS(), b1, s6) | |
1404 | +#@combinator_effect(_COMB_NUMS(), b1, s6) | |
1400 | 1405 | @FunctionWrapper |
1401 | 1406 | def loop(stack, expression, dictionary): |
1402 | 1407 | ''' |
@@ -1,58 +1,15 @@ | ||
1 | 1 | # -*- coding: utf_8 |
2 | -''' | |
3 | - | |
4 | -Multiple Stack Effects | |
5 | - | |
6 | -By adjusting the machinery in types.py to handles lists of stack effect comments | |
7 | -we can capture more information about the type signatures of some functions, | |
8 | -and we can introduce a kind of Kleene Star or sequence type that can stand for | |
9 | -an unbounded sequence of other types. | |
10 | - | |
11 | -''' | |
12 | -import sys | |
13 | -import joy.library | |
14 | -from joy.parser import Symbol, text_to_expression | |
15 | -from joy.utils.stack import ( | |
16 | - concat as CONCAT, | |
17 | - expression_to_string, | |
18 | - list_to_stack, | |
19 | - ) | |
20 | - | |
21 | - average = | |
22 | - # sum_ = | |
23 | - # product = | |
24 | - # min_ = max_ = [(((Ns[1], s1), s0), (n0, s0))] | |
25 | - # flatten = [(((Ss[1], s1), s0), (s2, s0))] | |
26 | - | |
27 | - return { | |
28 | - name.rstrip('_'): stack_effect | |
29 | - for name, stack_effect in locals().iteritems() | |
30 | - } | |
31 | 2 | |
32 | 3 | |
33 | -FUNCTIONS.update({ | |
34 | - name: SymbolJoyType(name, stack_effect, i) | |
35 | - for i, (name, stack_effect) in enumerate(defs().iteritems()) | |
36 | - }) | |
37 | -FUNCTIONS.update({ | |
38 | - combo.__name__: CombinatorJoyType(combo.__name__, [combo], i) | |
39 | - for i, combo in enumerate(( | |
40 | - joy.library.concat_, | |
41 | - joy.library._dictionary['disenstacken'], | |
42 | - joy.library.x, | |
43 | - )) | |
44 | - }) | |
45 | 4 | |
46 | 5 | |
47 | -def branch_true(stack, expression, dictionary): | |
48 | - (then, (else_, (flag, stack))) = stack | |
49 | - return stack, CONCAT(then, expression), dictionary | |
50 | 6 | |
51 | -def branch_false(stack, expression, dictionary): | |
52 | - (then, (else_, (flag, stack))) = stack | |
53 | - return stack, CONCAT(else_, expression), dictionary | |
54 | 7 | |
55 | -FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100) | |
8 | + | |
9 | + | |
10 | + | |
11 | + | |
12 | + | |
56 | 13 | pop = FUNCTIONS['pop'] |
57 | 14 | |
58 | 15 | def loop_true(stack, expression, dictionary): |
@@ -70,41 +27,42 @@ | ||
70 | 27 | FUNCTIONS['loop'] = CombinatorJoyType('loop', [loop_two_true, loop_true, loop_false], 101) |
71 | 28 | |
72 | 29 | |
73 | -joy.library.add_aliases(FUNCTIONS, joy.library.ALIASES) | |
74 | - | |
75 | - | |
76 | -def set_expectations_of_definition(cjt): | |
77 | - if len(cjt.stack_effects) != 1: | |
78 | - raise ValueError | |
79 | - defi = cjt.stack_effects[0] | |
80 | - if not isinstance(defi, joy.library.DefinitionWrapper): | |
81 | - raise ValueError | |
82 | - F = infer_expression(defi.body) | |
83 | - assert len(F) == 1, repr(F) | |
84 | - fi, fo = F[0] | |
85 | - cjt.expect = fi | |
86 | 30 | |
87 | 31 | |
88 | 32 | |
89 | -def set_expectations(): | |
90 | - | |
91 | - loop.expect = s6, (b1, s5) | |
92 | -# i.expect = nullary.expect = x.expect = s7, s6 | |
93 | -# dip.expect = dupdip.expect = s8, (a8, s7) | |
94 | -# dipd.expect = s8, (a8, (a7, s7)) | |
95 | -# dipdd.expect = s8, (a8, (a7, (a6, s7))) | |
96 | - concat_.expect = s8, (s7, s6) | |
97 | -# b.expect = infra.expect = s8, (s7, s6) | |
98 | - # set_expectations_of_definition(unary) | |
99 | - # set_expectations_of_definition(binary) | |
100 | - # set_expectations_of_definition(ternary) | |
101 | - # set_expectations_of_definition(quoted) | |
102 | - # set_expectations_of_definition(unquoted) | |
103 | - # set_expectations_of_definition(enstacken) | |
104 | - disenstacken.expect = (As[1], s1), s0 | |
105 | -scope = globals().copy() | |
106 | -scope.update(FUNCTIONS) | |
107 | -eval(set_expectations.func_code, scope) | |
108 | -del scope | |
109 | 33 | |
110 | 34 | |
35 | + | |
36 | + | |
37 | + | |
38 | + | |
39 | + | |
40 | +# joy.library.add_aliases(FUNCTIONS, joy.library.ALIASES) | |
41 | + | |
42 | + | |
43 | +# def set_expectations_of_definition(cjt): | |
44 | +# if len(cjt.stack_effects) != 1: | |
45 | +# raise ValueError | |
46 | +# defi = cjt.stack_effects[0] | |
47 | +# if not isinstance(defi, joy.library.DefinitionWrapper): | |
48 | +# raise ValueError | |
49 | +# F = infer_expression(defi.body) | |
50 | +# assert len(F) == 1, repr(F) | |
51 | +# fi, fo = F[0] | |
52 | +# cjt.expect = fi | |
53 | + | |
54 | + | |
55 | + | |
56 | + | |
57 | + average = | |
58 | + loop.expect = s6, (b1, s5) | |
59 | + disenstacken.expect = (As[1], s1), s0 | |
60 | + joy.library._dictionary['disenstacken'], | |
61 | + | |
62 | + | |
63 | +# scope = globals().copy() | |
64 | +# scope.update(FUNCTIONS) | |
65 | +# eval(set_expectations.func_code, scope) | |
66 | +# del scope | |
67 | + | |
68 | + |