1 # -*- coding: utf-8 -*-
3 # Copyright © 2014, 2015, 2017, 2018 Simon Forman
5 # This file is part of Thun
7 # Thun is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # Thun is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Thun. If not see <http://www.gnu.org/licenses/>.
21 This module contains the Joy function infrastructure and a library of
22 functions. Its main export is a Python function initialize() that
23 returns a dictionary of Joy functions suitable for use with the joy()
26 from __future__ import print_function
27 from builtins import map
28 from builtins import zip
29 from builtins import range
30 from builtins import object
31 from logging import getLogger
33 _log = getLogger(__name__)
34 _log.info('Loading library.')
36 from inspect import getdoc
37 from functools import wraps
38 from itertools import count
39 from inspect import getmembers, isfunction
42 from .parser import text_to_expression, Symbol
43 from .utils.stack import expression_to_string, list_to_stack, iter_stack, pick, concat
44 from .utils.brutal_hackery import rename_code_object
46 from .utils import generated_library as genlib
47 from .utils.types import (
69 poly_combinator_effect,
70 doc_from_stack_effect,
74 _SYM_NUMS = count().__next__
75 _COMB_NUMS = count().__next__
79 A = a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 = list(map(AnyJoyType, _R))
80 B = b0, b1, b2, b3, b4, b5, b6, b7, b8, b9 = list(map(BooleanJoyType, _R))
81 N = n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = list(map(NumberJoyType, _R))
82 S = s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = list(map(StackJoyType, _R))
83 F = f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = list(map(FloatJoyType, _R))
84 I = i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = list(map(IntJoyType, _R))
85 T = t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 = list(map(TextJoyType, _R))
88 _R = list(range(1, 11))
89 As = list(map(AnyStarJoyType, _R))
90 Ns = list(map(NumberStarJoyType, _R))
91 Ss = list(map(StackStarJoyType, _R))
94 sec0 = stack_effect(t1)()
95 sec1 = stack_effect(s0, i1)(s1)
96 sec2 = stack_effect(s0, i1)(a1)
97 sec_binary_cmp = stack_effect(n1, n2)(b1)
98 sec_binary_ints = stack_effect(i1, i2)(i3)
99 sec_binary_logic = stack_effect(b1, b2)(b3)
100 sec_binary_math = stack_effect(n1, n2)(n3)
101 sec_unary_logic = stack_effect(a1)(b1)
102 sec_unary_math = stack_effect(n1)(n2)
103 sec_Ns_math = stack_effect((Ns[1], s1),)(n0)
108 def inscribe(function):
109 '''A decorator to inscribe functions into the default dictionary.'''
110 _dictionary[function.name] = function
115 '''Return a dictionary of Joy functions for use with joy().'''
116 return _dictionary.copy()
122 ('bool', ['truthy']),
124 ('floordiv', ['/floor', '//']),
125 ('floor', ['round']),
127 ('mod', ['%', 'rem', 'remainder', 'modulus']),
130 ('getitem', ['pick', 'at']),
135 ('ne', ['<>', '!=']),
141 ('rolldown', ['roll<']),
142 ('rollup', ['roll>']),
148 def add_aliases(D, A):
150 Given a dict and a iterable of (name, [alias, ...]) pairs, create
151 additional entries in the dict mapping each alias to the named function
152 if it's in the dict. Aliases for functions not in the dict are ignored.
154 for name, aliases in A:
159 for alias in aliases:
165 Return a dict of named stack effects.
167 "Yin" functions are those that only rearrange items in stacks and
168 can be defined completely by their stack effects. This means they
169 can be auto-compiled.
171 # pylint: disable=unused-variable
172 cons = ef(a1, s0)((a1, s0))
173 ccons = compose(cons, cons)
175 dupd = ef(a2, a1)(a2, a2, a1)
176 dupdd = ef(a3, a2, a1)(a3, a3, a2, a1)
177 first = ef((a1, s1),)(a1,)
178 over = ef(a2, a1)(a2, a1, a2)
180 popd = ef(a2, a1,)(a1)
181 popdd = ef(a3, a2, a1,)(a2, a1,)
182 popop = ef(a2, a1,)()
183 popopd = ef(a3, a2, a1,)(a1)
184 popopdd = ef(a4, a3, a2, a1,)(a2, a1)
185 rest = ef((a1, s0),)(s0,)
186 rolldown = ef(a1, a2, a3)(a2, a3, a1)
187 rollup = ef(a1, a2, a3)(a3, a1, a2)
188 rrest = compose(rest, rest)
189 second = compose(rest, first)
191 swaack = (s1, s0), (s0, s1)
192 swap = ef(a1, a2)(a2, a1)
193 swons = compose(swap, cons)
194 third = compose(rest, second)
195 tuck = ef(a2, a1)(a1, a2, a1)
196 uncons = ef((a1, s0),)(a1, s0)
197 unswons = compose(uncons, swap)
198 stuncons = compose(stack, uncons)
199 stununcons = compose(stack, uncons, uncons)
200 unit = ef(a1)((a1, ()))
202 first_two = compose(uncons, uncons, pop)
203 fourth = compose(rest, third)
205 _Tree_add_Ee = compose(pop, swap, rolldown, rrest, ccons)
206 _Tree_get_E = compose(popop, second)
207 _Tree_delete_clear_stuff = compose(rollup, popop, rest)
208 _Tree_delete_R0 = compose(over, first, swap, dup)
216 product == 1 swap [*] step
217 flatten == [] swap [concat] step
220 enstacken == stack [clear] dip
222 disenstacken == ? [uncons ?] loop pop
223 dinfrirst == dip infra first
224 nullary == [stack] dinfrirst
225 unary == nullary popd
226 binary == nullary [popop] dip
227 ternary == unary [popop] dip
231 size == 0 swap [pop ++] step
233 cleave == fork [popd] dip
234 average == [sum 1.0 *] [size] cleave /
235 gcd == 1 [tuck modulus dup 0 >] loop pop
236 least_fraction == dup [gcd] infra [div] concat map
237 *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
238 *fraction0 == concat [[swap] dip * [*] dip] infra
239 down_to_zero == [0 >] [dup --] while
240 range_to_zero == unit [down_to_zero] infra
241 anamorphism == [pop []] swap [dip swons] genrec
242 range == [0 <=] [1 - dup] anamorphism
243 while == swap [nullary] cons dup dipd concat loop
245 primrec == [i] genrec
246 step_zero == 0 roll> step
247 codireco == cons dip rest cons
248 make_generator == [codireco] ccons
249 ifte == [nullary not] dipd branch
253 # ifte == [nullary] dipd swap branch
254 # genrec == [[genrec] cons cons cons cons] nullary swons concat ifte
256 # Another definition for while. FWIW
257 # while == over [[i] dip nullary] ccons [nullary] dip loop
261 ##second == rest first
262 ##third == rest rest first
264 ##swoncat == swap concat
267 ##z-down == [] swap uncons swap
268 ##z-up == swons swap shunt
269 ##z-right == [swons] cons dip uncons swap
270 ##z-left == swons [uncons swap] dip swap
273 ##divisor == popop 2 *
275 ##radical == swap dup * rollup * 4 * - sqrt
278 ##q0 == [[divisor] [minusb] [radical]] pam
279 ##q1 == [[root1] [root2]] pam
280 ##quadratic == [q0] ternary i [q1] ternary
284 ##PE1.1 == + dup [+] dip
285 ##PE1.2 == dup [3 & PE1.1] dip 2 >>
286 ##PE1.3 == 14811 swap [PE1.2] times pop
287 ##PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop
289 #PE1.2 == [PE1.1] step
290 #PE1 == 0 0 66 [[3 2 1 3 1 2 3] PE1.2] times [3 2 1 3] PE1.2 pop
294 def FunctionWrapper(f):
295 '''Set name attribute.'''
297 raise ValueError('Function %s must have doc string.' % f.__name__)
298 f.name = f.__name__.rstrip('_') # Don't shadow builtins.
302 def SimpleFunctionWrapper(f):
304 Wrap functions that take and return just a stack.
308 @rename_code_object(f.__name__)
309 def inner(stack, expression, dictionary):
310 return f(stack), expression, dictionary
314 def BinaryBuiltinWrapper(f):
316 Wrap functions that take two arguments and return a single result.
320 @rename_code_object(f.__name__)
321 def inner(stack, expression, dictionary):
322 (a, (b, stack)) = stack
324 return (result, stack), expression, dictionary
328 def UnaryBuiltinWrapper(f):
330 Wrap functions that take one argument and return a single result.
334 @rename_code_object(f.__name__)
335 def inner(stack, expression, dictionary):
338 return (result, stack), expression, dictionary
342 class DefinitionWrapper(object):
344 Provide implementation of defined functions, and some helper methods.
347 def __init__(self, name, body_text, doc=None):
348 self.name = self.__name__ = name
349 self.body = text_to_expression(body_text)
350 self._body = tuple(iter_stack(self.body))
351 self.__doc__ = doc or body_text
352 self._compiled = None
354 def __call__(self, stack, expression, dictionary):
356 return self._compiled(stack, expression, dictionary) # pylint: disable=E1102
357 expression = list_to_stack(self._body, expression)
358 return stack, expression, dictionary
361 def parse_definition(class_, defi):
363 Given some text describing a Joy function definition parse it and
364 return a DefinitionWrapper.
366 name, proper, body_text = (n.strip() for n in defi.partition('=='))
368 raise ValueError('Definition %r failed' % (defi,))
369 return class_(name, body_text)
372 def add_definitions(class_, defs, dictionary):
374 Scan multi-line string defs for definitions and add them to the
377 for definition in _text_to_defs(defs):
378 class_.add_def(definition, dictionary)
381 def add_def(class_, definition, dictionary, fail_fails=False):
383 Add the definition to the dictionary.
385 F = class_.parse_definition(definition)
386 _log.info('Adding definition %s := %s', F.name, expression_to_string(F.body))
387 dictionary[F.name] = F
390 def load_definitions(class_, filename, dictionary):
391 with open(filename) as f:
392 lines = [line for line in f if '==' in line]
394 class_.add_def(line, dictionary)
397 def _text_to_defs(text):
398 return (line.strip() for line in text.splitlines() if '==' in line)
409 def inscribe_(stack, expression, dictionary):
411 Create a new Joy function definition in the Joy dictionary. A
412 definition is given as a string with a name followed by a double
413 equal sign then one or more Joy functions, the body. for example:
417 If you want the definition to persist over restarts, enter it into
418 the definitions.txt resource.
420 definition, stack = stack
421 DefinitionWrapper.add_def(definition, dictionary, fail_fails=True)
422 return stack, expression, dictionary
426 @SimpleFunctionWrapper
428 '''Parse the string on the stack to a Joy expression.'''
430 expression = text_to_expression(text)
431 return expression, stack
435 @SimpleFunctionWrapper
437 '''Attempt to infer the stack effect of a Joy expression.'''
439 effects = infer_expression(E)
440 e = list_to_stack([(fi, (fo, ())) for fi, fo in effects])
446 @SimpleFunctionWrapper
451 getitem == drop first
453 Expects an integer and a quote on the stack and returns the item at the
454 nth position in the quote counting from 0.
458 -------------------------
462 n, (Q, stack) = stack
463 return pick(Q, n), stack
468 @SimpleFunctionWrapper
475 Expects an integer and a quote on the stack and returns the quote with
476 n items removed off the top.
480 ----------------------
484 n, (Q, stack) = stack
496 @SimpleFunctionWrapper
499 Expects an integer and a quote on the stack and returns the quote with
500 just the top n items in reverse order (because that's easier and you can
501 use reverse if needed.)
505 ----------------------
509 n, (Q, stack) = stack
522 @SimpleFunctionWrapper
525 Use a Boolean value to select one of two items.
529 ----------------------
534 ---------------------
537 Currently Python semantics are used to evaluate the "truthiness" of the
538 Boolean value (so empty string, zero, etc. are counted as false, etc.)
540 (if_, (then, (else_, stack))) = stack
541 return then if if_ else else_, stack
545 @SimpleFunctionWrapper
548 Use a Boolean value to select one of two items from a sequence.
552 ------------------------
557 -----------------------
560 The sequence can contain more than two items but not fewer.
561 Currently Python semantics are used to evaluate the "truthiness" of the
562 Boolean value (so empty string, zero, etc. are counted as false, etc.)
564 (flag, (choices, stack)) = stack
565 (else_, (then, _)) = choices
566 return then if flag else else_, stack
571 @SimpleFunctionWrapper
573 '''Given a list find the maximum.'''
575 return max(iter_stack(tos)), stack
580 @SimpleFunctionWrapper
582 '''Given a list find the minimum.'''
584 return min(iter_stack(tos)), stack
589 @SimpleFunctionWrapper
591 '''Given a quoted sequence of numbers return the sum.
593 sum == 0 swap [+] step
596 return sum(iter_stack(tos)), stack
600 @SimpleFunctionWrapper
603 Expects an item on the stack and a quote under it and removes that item
604 from the the quote. The item is only removed once.
608 ------------------------
612 (tos, (second, stack)) = S
613 l = list(iter_stack(second))
615 return list_to_stack(l), stack
619 @SimpleFunctionWrapper
621 '''Given a list remove duplicate items.'''
623 I = list(iter_stack(tos))
624 return list_to_stack(sorted(set(I), key=I.index)), stack
628 @SimpleFunctionWrapper
630 '''Given a list return it sorted.'''
632 return list_to_stack(sorted(iter_stack(tos))), stack
635 _functions['clear'] = s0, s1
637 @SimpleFunctionWrapper
639 '''Clear everything from the stack.
642 clear == stack [pop stack] loop
652 @SimpleFunctionWrapper
655 The unstack operator expects a list on top of the stack and makes that
656 the stack discarding the rest of the stack.
662 @SimpleFunctionWrapper
664 '''Reverse the list on the top of the stack.
667 reverse == [] swap shunt
671 for term in iter_stack(tos):
677 @combinator_effect(_COMB_NUMS(), s7, s6)
678 @SimpleFunctionWrapper
680 '''Concatinate the two lists on the top of the stack.
683 [a b c] [d e f] concat
684 ----------------------------
688 (tos, (second, stack)) = S
689 return concat(second, tos), stack
693 @SimpleFunctionWrapper
695 '''Like concat but reverses the top list into the second.
698 shunt == [swons] step == reverse swap concat
700 [a b c] [d e f] shunt
701 ---------------------------
705 (tos, (second, stack)) = stack
708 second = term, second
713 @SimpleFunctionWrapper
716 Replace the two lists on the top of the stack with a list of the pairs
717 from each list. The smallest list sets the length of the result list.
719 (tos, (second, stack)) = S
722 for a, b in zip(iter_stack(tos), iter_stack(second))
724 return list_to_stack(accumulator), stack
729 @SimpleFunctionWrapper
733 return tos + 1, stack
738 @SimpleFunctionWrapper
742 return tos - 1, stack
746 @SimpleFunctionWrapper
757 a, (b, stack) = stack
763 return int(math.floor(n))
765 floor.__doc__ = math.floor.__doc__
769 @SimpleFunctionWrapper
772 divmod(x, y) -> (quotient, remainder)
774 Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
783 Return the square root of the number a.
784 Negative numbers return complex roots.
789 assert a < 0, repr(a)
790 r = math.sqrt(-a) * 1j
796 # if isinstance(text, str):
797 # return run(text, stack)
802 @SimpleFunctionWrapper
804 '''The identity function.'''
809 @SimpleFunctionWrapper
811 '''True if the form on TOS is void otherwise False.'''
813 return _void(form), stack
817 return any(not _void(i) for i in iter_stack(form))
828 def words(stack, expression, dictionary):
829 '''Print all the words in alphabetical order.'''
830 print(' '.join(sorted(dictionary)))
831 return stack, expression, dictionary
836 def sharing(stack, expression, dictionary):
837 '''Print redistribution information.'''
838 print("You may convey verbatim copies of the Program's source code as"
839 ' you receive it, in any medium, provided that you conspicuously'
840 ' and appropriately publish on each copy an appropriate copyright'
841 ' notice; keep intact all notices stating that this License and'
842 ' any non-permissive terms added in accord with section 7 apply'
843 ' to the code; keep intact all notices of the absence of any'
844 ' warranty; and give all recipients a copy of this License along'
846 ' You should have received a copy of the GNU General Public License'
847 ' along with Thun. If not see <http://www.gnu.org/licenses/>.')
848 return stack, expression, dictionary
853 def warranty(stack, expression, dictionary):
854 '''Print warranty information.'''
855 print('THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY'
856 ' APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE'
857 ' COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM'
858 ' "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR'
859 ' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES'
860 ' OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE'
861 ' ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS'
862 ' WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE'
863 ' COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.')
864 return stack, expression, dictionary
867 # def simple_manual(stack):
869 # Print words and help for each word.
871 # for name, f in sorted(FUNCTIONS.items()):
873 # boxline = '+%s+' % ('-' * (len(name) + 2))
876 # '| %s |' % (name,),
878 # d if d else ' ...',
888 def help_(S, expression, dictionary):
889 '''Accepts a quoted symbol on the top of the stack and prints its docs.'''
890 ((symbol, _), stack) = S
891 word = dictionary[symbol]
893 return stack, expression, dictionary
901 # Several combinators depend on other words in their definitions,
902 # we use symbols to prevent hard-coding these, so in theory, you
903 # could change the word in the dictionary to use different semantics.
904 S_choice = Symbol('choice')
905 S_first = Symbol('first')
906 S_getitem = Symbol('getitem')
907 S_genrec = Symbol('genrec')
908 S_loop = Symbol('loop')
910 S_ifte = Symbol('ifte')
911 S_infra = Symbol('infra')
912 S_pop = Symbol('pop')
913 S_step = Symbol('step')
914 S_times = Symbol('times')
915 S_swaack = Symbol('swaack')
919 @combinator_effect(_COMB_NUMS(), s1)
921 def i(stack, expression, dictionary):
923 The i combinator expects a quoted program on the stack and unpacks it
924 onto the pending expression for evaluation.
933 return stack, concat(quote, expression), dictionary
937 @combinator_effect(_COMB_NUMS(), s1)
939 def x(stack, expression, dictionary):
945 ... [Q] x = ... [Q] dup i
946 ... [Q] x = ... [Q] [Q] i
947 ... [Q] x = ... [Q] Q
951 return stack, concat(quote, expression), dictionary
955 @combinator_effect(_COMB_NUMS(), s7, s6)
957 def b(stack, expression, dictionary):
963 ... [P] [Q] b == ... [P] i [Q] i
964 ... [P] [Q] b == ... P Q
967 q, (p, (stack)) = stack
968 return stack, concat(p, concat(q, expression)), dictionary
972 @combinator_effect(_COMB_NUMS(), a1, s1)
974 def dupdip(stack, expression, dictionary):
978 [F] dupdip == dup [F] dip
988 return stack, concat(F, (a, expression)), dictionary
992 @combinator_effect(_COMB_NUMS(), s7, s6)
994 def infra(stack, expression, dictionary):
996 Accept a quoted program and a list on the stack and run the program
997 with the list as its stack. Does not affect the rest of the stack.
1000 ... [a b c] [Q] . infra
1001 -----------------------------
1002 c b a . Q [...] swaack
1005 (quote, (aggregate, stack)) = stack
1006 return aggregate, concat(quote, (stack, (S_swaack, expression))), dictionary
1010 #@combinator_effect(_COMB_NUMS(), s7, s6, s5, s4)
1012 def genrec(stack, expression, dictionary):
1014 General Recursion Combinator.
1017 [if] [then] [rec1] [rec2] genrec
1018 ---------------------------------------------------------------------
1019 [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
1021 From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun:
1022 "The genrec combinator takes four program parameters in addition to
1023 whatever data parameters it needs. Fourth from the top is an if-part,
1024 followed by a then-part. If the if-part yields true, then the then-part
1025 is executed and the combinator terminates. The other two parameters are
1026 the rec1-part and the rec2-part. If the if-part yields false, the
1027 rec1-part is executed. Following that the four program parameters and
1028 the combinator are again pushed onto the stack bundled up in a quoted
1029 form. Then the rec2-part is executed, where it will find the bundled
1030 form. Typically it will then execute the bundled form, either with i or
1031 with app2, or some other combinator."
1033 The way to design one of these is to fix your base case [then] and the
1034 test [if], and then treat rec1 and rec2 as an else-part "sandwiching"
1035 a quotation of the whole function.
1037 For example, given a (general recursive) function 'F':
1040 F == [I] [T] [R1] [R2] genrec
1042 If the [I] if-part fails you must derive R1 and R2 from:
1047 Just set the stack arguments in front, and figure out what R1 and R2
1048 have to do to apply the quoted [F] in the proper way. In effect, the
1049 genrec combinator turns into an ifte combinator with a quoted copy of
1050 the original definition in the else-part:
1053 F == [I] [T] [R1] [R2] genrec
1054 == [I] [T] [R1 [F] R2] ifte
1056 Primitive recursive functions are those where R2 == i.
1059 P == [I] [T] [R] primrec
1060 == [I] [T] [R [P] i] ifte
1061 == [I] [T] [R P] ifte
1064 (rec2, (rec1, stack)) = stack
1065 (then, (if_, _)) = stack
1066 F = (if_, (then, (rec1, (rec2, (S_genrec, ())))))
1067 else_ = concat(rec1, (F, rec2))
1068 return (else_, stack), (S_ifte, expression), dictionary
1072 @combinator_effect(_COMB_NUMS(), s7, s6)
1074 def map_(S, expression, dictionary):
1076 Run the quoted program on TOS on the items in the list under it, push a
1077 new list with the results in place of the program and original list.
1079 # (quote, (aggregate, stack)) = S
1080 # results = list_to_stack([
1081 # joy((term, stack), quote, dictionary)[0][0]
1082 # for term in iter_stack(aggregate)
1084 # return (results, stack), expression, dictionary
1085 (quote, (aggregate, stack)) = S
1087 return (aggregate, stack), expression, dictionary
1089 for term in iter_stack(aggregate):
1091 batch = (s, (quote, (S_infra, (S_first, batch))))
1092 stack = (batch, ((), stack))
1093 return stack, (S_infra, expression), dictionary
1096 #def cleave(S, expression, dictionary):
1098 # The cleave combinator expects two quotations, and below that an item X.
1099 # It first executes [P], with X on top, and saves the top result element.
1100 # Then it executes [Q], again with X, and saves the top result.
1101 # Finally it restores the stack to what it was below X and pushes the two
1102 # results P(X) and Q(X).
1104 # (Q, (P, (x, stack))) = S
1105 # p = joy((x, stack), P, dictionary)[0][0]
1106 # q = joy((x, stack), Q, dictionary)[0][0]
1107 # return (q, (p, stack)), expression, dictionary
1110 def branch_true(stack, expression, dictionary):
1111 # pylint: disable=unused-variable
1112 (then, (else_, (flag, stack))) = stack
1113 return stack, concat(then, expression), dictionary
1116 def branch_false(stack, expression, dictionary):
1117 # pylint: disable=unused-variable
1118 (then, (else_, (flag, stack))) = stack
1119 return stack, concat(else_, expression), dictionary
1123 @poly_combinator_effect(_COMB_NUMS(), [branch_true, branch_false], b1, s7, s6)
1125 def branch(stack, expression, dictionary):
1127 Use a Boolean value to select one of two quoted programs to run.
1131 branch == roll< choice i
1135 False [F] [T] branch
1136 --------------------------
1140 -------------------------
1144 (then, (else_, (flag, stack))) = stack
1145 return stack, concat(then if flag else else_, expression), dictionary
1148 #FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100)
1153 ##def ifte(stack, expression, dictionary):
1155 ## If-Then-Else Combinator
1158 ## ... [if] [then] [else] ifte
1159 ## ---------------------------------------------------
1160 ## ... [[else] [then]] [...] [if] infra select i
1165 ## ... [if] [then] [else] ifte
1166 ## -------------------------------------------------------
1167 ## ... [else] [then] [...] [if] infra first choice i
1170 ## Has the effect of grabbing a copy of the stack on which to run the
1171 ## if-part using infra.
1173 ## (else_, (then, (if_, stack))) = stack
1174 ## expression = (S_infra, (S_first, (S_choice, (S_i, expression))))
1175 ## stack = (if_, (stack, (then, (else_, stack))))
1176 ## return stack, expression, dictionary
1181 def cond(stack, expression, dictionary):
1183 This combinator works like a case statement. It expects a single quote
1184 on the stack that must contain zero or more condition quotes and a
1185 default quote. Each condition clause should contain a quoted predicate
1186 followed by the function expression to run if that predicate returns
1187 true. If no predicates return true the default function runs.
1189 It works by rewriting into a chain of nested `ifte` expressions, e.g.::
1191 [[[B0] T0] [[B1] T1] [D]] cond
1192 -----------------------------------------
1193 [B0] [T0] [[B1] [T1] [D] ifte] ifte
1196 conditions, stack = stack
1198 expression = _cond(conditions, expression)
1200 # Attempt to preload the args to first ifte.
1201 (P, (T, (E, expression))) = expression
1203 # If, for any reason, the argument to cond should happen to contain
1204 # only the default clause then this optimization will fail.
1207 stack = (E, (T, (P, stack)))
1208 return stack, expression, dictionary
1211 def _cond(conditions, expression):
1212 (clause, rest) = conditions
1213 if not rest: # clause is [D]
1216 return (P, (T, (_cond(rest, ()), (S_ifte, expression))))
1220 @combinator_effect(_COMB_NUMS(), a1, s1)
1222 def dip(stack, expression, dictionary):
1224 The dip combinator expects a quoted program on the stack and below it
1225 some item, it hoists the item into the expression and runs the program
1226 on the rest of the stack.
1234 (quote, (x, stack)) = stack
1235 expression = (x, expression)
1236 return stack, concat(quote, expression), dictionary
1240 @combinator_effect(_COMB_NUMS(), a2, a1, s1)
1242 def dipd(S, expression, dictionary):
1244 Like dip but expects two items.
1248 ---------------------
1252 (quote, (x, (y, stack))) = S
1253 expression = (y, (x, expression))
1254 return stack, concat(quote, expression), dictionary
1258 @combinator_effect(_COMB_NUMS(), a3, a2, a1, s1)
1260 def dipdd(S, expression, dictionary):
1262 Like dip but expects three items.
1266 -----------------------
1270 (quote, (x, (y, (z, stack)))) = S
1271 expression = (z, (y, (x, expression)))
1272 return stack, concat(quote, expression), dictionary
1276 @combinator_effect(_COMB_NUMS(), a1, s1)
1278 def app1(S, expression, dictionary):
1280 Given a quoted program on TOS and anything as the second stack item run
1281 the program and replace the two args with the first result of the
1286 -----------------------------------
1287 ... [x ...] [Q] . infra first
1289 (quote, (x, stack)) = S
1290 stack = (quote, ((x, stack), stack))
1291 expression = (S_infra, (S_first, expression))
1292 return stack, expression, dictionary
1296 @combinator_effect(_COMB_NUMS(), a2, a1, s1)
1298 def app2(S, expression, dictionary):
1299 '''Like app1 with two items.
1303 -----------------------------------
1304 ... [y ...] [Q] . infra first
1305 [x ...] [Q] infra first
1308 (quote, (x, (y, stack))) = S
1309 expression = (S_infra, (S_first,
1310 ((x, stack), (quote, (S_infra, (S_first,
1312 stack = (quote, ((y, stack), stack))
1313 return stack, expression, dictionary
1317 @combinator_effect(_COMB_NUMS(), a3, a2, a1, s1)
1319 def app3(S, expression, dictionary):
1320 '''Like app1 with three items.
1323 ... z y x [Q] . app3
1324 -----------------------------------
1325 ... [z ...] [Q] . infra first
1326 [y ...] [Q] infra first
1327 [x ...] [Q] infra first
1330 (quote, (x, (y, (z, stack)))) = S
1331 expression = (S_infra, (S_first,
1332 ((y, stack), (quote, (S_infra, (S_first,
1333 ((x, stack), (quote, (S_infra, (S_first,
1334 expression))))))))))
1335 stack = (quote, ((z, stack), stack))
1336 return stack, expression, dictionary
1340 @combinator_effect(_COMB_NUMS(), s7, s6)
1342 def step(S, expression, dictionary):
1344 Run a quoted program on each item in a sequence.
1348 -----------------------
1353 ------------------------
1357 ... [a b c] [Q] . step
1358 ----------------------------------------
1359 ... a . Q [b c] [Q] step
1361 The step combinator executes the quotation on each member of the list
1362 on top of the stack.
1364 (quote, (aggregate, stack)) = S
1366 return stack, expression, dictionary
1367 head, tail = aggregate
1368 stack = quote, (head, stack)
1370 expression = tail, (quote, (S_step, expression))
1371 expression = S_i, expression
1372 return stack, expression, dictionary
1376 @combinator_effect(_COMB_NUMS(), i1, s6)
1378 def times(stack, expression, dictionary):
1380 times == [-- dip] cons [swap] infra [0 >] swap while pop
1384 --------------------- w/ n <= 0
1389 ---------------------------------
1394 --------------------------------- w/ n > 1
1395 ... . Q (n - 1) [Q] times
1398 # times == [-- dip] cons [swap] infra [0 >] swap while pop
1399 (quote, (n, stack)) = stack
1401 return stack, expression, dictionary
1404 expression = n, (quote, (S_times, expression))
1405 expression = concat(quote, expression)
1406 return stack, expression, dictionary
1409 # The current definition above works like this:
1412 # --------------------------------------
1413 # [P] nullary [Q [P] nullary] loop
1415 # while == [pop i not] [popop] [dudipd] primrec
1417 #def while_(S, expression, dictionary):
1418 # '''[if] [body] while'''
1419 # (body, (if_, stack)) = S
1420 # while joy(stack, if_, dictionary)[0][0]:
1421 # stack = joy(stack, body, dictionary)[0]
1422 # return stack, expression, dictionary
1425 def loop_true(stack, expression, dictionary):
1426 quote, (flag, stack) = stack # pylint: disable=unused-variable
1427 return stack, concat(quote, (S_pop, expression)), dictionary
1429 def loop_two_true(stack, expression, dictionary):
1430 quote, (flag, stack) = stack # pylint: disable=unused-variable
1431 return stack, concat(quote, (S_pop, concat(quote, (S_pop, expression)))), dictionary
1433 def loop_false(stack, expression, dictionary):
1434 quote, (flag, stack) = stack # pylint: disable=unused-variable
1435 return stack, expression, dictionary
1439 @poly_combinator_effect(_COMB_NUMS(), [loop_two_true, loop_true, loop_false], b1, s6)
1441 def loop(stack, expression, dictionary):
1443 Basic loop combinator.
1447 -----------------------
1451 ------------------------
1455 quote, (flag, stack) = stack
1457 expression = concat(quote, (quote, (S_loop, expression)))
1458 return stack, expression, dictionary
1462 @combinator_effect(_COMB_NUMS(), a1, a2, s6, s7, s8)
1464 def cmp_(stack, expression, dictionary):
1466 cmp takes two values and three quoted programs on the stack and runs
1467 one of the three depending on the results of comparing the two values:
1471 ------------------------- a > b
1475 ------------------------- a = b
1479 ------------------------- a < b
1482 L, (E, (G, (b, (a, stack)))) = stack
1483 expression = concat(G if a > b else L if a < b else E, expression)
1484 return stack, expression, dictionary
1487 # FunctionWrapper(cleave),
1488 # FunctionWrapper(while_),
1493 #divmod_ = pm = __(n2, n1), __(n4, n3)
1495 sec_binary_cmp(BinaryBuiltinWrapper(operator.eq)),
1496 sec_binary_cmp(BinaryBuiltinWrapper(operator.ge)),
1497 sec_binary_cmp(BinaryBuiltinWrapper(operator.gt)),
1498 sec_binary_cmp(BinaryBuiltinWrapper(operator.le)),
1499 sec_binary_cmp(BinaryBuiltinWrapper(operator.lt)),
1500 sec_binary_cmp(BinaryBuiltinWrapper(operator.ne)),
1502 sec_binary_ints(BinaryBuiltinWrapper(operator.xor)),
1503 sec_binary_ints(BinaryBuiltinWrapper(operator.lshift)),
1504 sec_binary_ints(BinaryBuiltinWrapper(operator.rshift)),
1506 sec_binary_logic(BinaryBuiltinWrapper(operator.and_)),
1507 sec_binary_logic(BinaryBuiltinWrapper(operator.or_)),
1509 sec_binary_math(BinaryBuiltinWrapper(operator.add)),
1510 sec_binary_math(BinaryBuiltinWrapper(operator.floordiv)),
1511 sec_binary_math(BinaryBuiltinWrapper(operator.mod)),
1512 sec_binary_math(BinaryBuiltinWrapper(operator.mul)),
1513 sec_binary_math(BinaryBuiltinWrapper(operator.pow)),
1514 sec_binary_math(BinaryBuiltinWrapper(operator.sub)),
1515 sec_binary_math(BinaryBuiltinWrapper(operator.truediv)),
1517 sec_unary_logic(UnaryBuiltinWrapper(bool)),
1518 sec_unary_logic(UnaryBuiltinWrapper(operator.not_)),
1520 sec_unary_math(UnaryBuiltinWrapper(abs)),
1521 sec_unary_math(UnaryBuiltinWrapper(operator.neg)),
1522 sec_unary_math(UnaryBuiltinWrapper(sqrt)),
1524 stack_effect(n1)(i1)(UnaryBuiltinWrapper(floor)),
1527 del F # Otherwise Sphinx autodoc will pick it up.
1530 YIN_STACK_EFFECTS = yin_functions()
1531 add_aliases(YIN_STACK_EFFECTS, ALIASES)
1533 # Load the auto-generated primitives into the dictionary.
1534 _functions.update(YIN_STACK_EFFECTS)
1537 # eh = compose(dup, bool)
1538 # sqr = compose(dup, mul)
1539 # of = compose(swap, at)
1541 # ''' in dict(compose=compose), _functions
1542 for name in sorted(_functions):
1543 sec = _functions[name]
1544 F = FUNCTIONS[name] = SymbolJoyType(name, [sec], _SYM_NUMS())
1545 if name in YIN_STACK_EFFECTS:
1546 _log.info('Setting stack effect for Yin function %s := %s', F.name, doc_from_stack_effect(*sec))
1548 for name, primitive in getmembers(genlib, isfunction):
1549 inscribe(SimpleFunctionWrapper(primitive))
1552 add_aliases(_dictionary, ALIASES)
1553 add_aliases(_functions, ALIASES)
1554 add_aliases(FUNCTIONS, ALIASES)
1557 DefinitionWrapper.add_definitions(definitions, _dictionary)
1560 EXPECTATIONS = dict(
1561 ifte=(s7, (s6, (s5, s4))),
1565 EXPECTATIONS['while'] = (s7, (s6, s5))
1576 C = _dictionary[name]
1577 expect = EXPECTATIONS.get(name)
1579 sec = doc_from_stack_effect(expect)
1580 _log.info('Setting stack EXPECT for combinator %s := %s', C.name, sec)
1582 _log.info('combinator %s', C.name)
1583 FUNCTIONS[name] = CombinatorJoyType(name, [C], _COMB_NUMS(), expect)
1587 of quoted enstacken ?
1588 unary binary ternary
1591 of_ = _dictionary[name]
1592 secs = infer_expression(of_.body)
1593 assert len(secs) == 1, repr(secs)
1595 'Setting stack effect for definition %s := %s',
1597 doc_from_stack_effect(*secs[0]),
1599 FUNCTIONS[name] = SymbolJoyType(name, infer_expression(of_.body), _SYM_NUMS())
1602 #sec_Ns_math(_dictionary['product'])
1604 ## product == 1 swap [*] step
1605 ## flatten == [] swap [concat] step
1606 ## disenstacken == ? [uncons ?] loop pop
1608 ## size == 0 swap [pop ++] step
1610 ## cleave == fork [popd] dip
1611 ## average == [sum 1.0 *] [size] cleave /
1612 ## gcd == 1 [tuck modulus dup 0 >] loop pop
1613 ## least_fraction == dup [gcd] infra [div] concat map
1614 ## *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
1615 ## *fraction0 == concat [[swap] dip * [*] dip] infra
1616 ## down_to_zero == [0 >] [dup --] while
1617 ## range_to_zero == unit [down_to_zero] infra
1618 ## anamorphism == [pop []] swap [dip swons] genrec
1619 ## range == [0 <=] [1 - dup] anamorphism
1620 ## while == swap [nullary] cons dup dipd concat loop
1621 ## dupdipd == dup dipd
1622 ## primrec == [i] genrec
1623 ## step_zero == 0 roll> step
1624 ## codireco == cons dip rest cons
1625 ## make_generator == [codireco] ccons
1626 ## ifte == [nullary not] dipd branch