From 750f49851228c9b6874a0a1ae35a821a42e9bb43 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 28 Apr 2020 10:42:09 -0700 Subject: [PATCH] Fixes #38237 Sort out stack/unstack/enstacken/disenstacken --- ...This_Implementation_of_Joy_in_Python.ipynb | 200 +-- docs/2._Library_Examples.ipynb | 1561 ++++++++++++++--- docs/Trees.ipynb | 16 +- joy/library.py | 2 +- 4 files changed, 1379 insertions(+), 400 deletions(-) diff --git a/docs/0._This_Implementation_of_Joy_in_Python.ipynb b/docs/0._This_Implementation_of_Joy_in_Python.ipynb index 6f203aa..ffe2838 100644 --- a/docs/0._This_Implementation_of_Joy_in_Python.ipynb +++ b/docs/0._This_Implementation_of_Joy_in_Python.ipynb @@ -62,64 +62,7 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "When talking about Joy we use the terms \"stack\", \"quote\", \"sequence\",\n", - "\"list\", and others to mean the same thing: a simple linear datatype that\n", - "permits certain operations such as iterating and pushing and popping\n", - "values from (at least) one end.\n", - "\n", - "There is no \"Stack\" Python class, instead we use the `cons list`_, a \n", - "venerable two-tuple recursive sequence datastructure, where the\n", - "empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the recursive\n", - "form of a stack with one or more items on it::\n", - "\n", - " stack := () | (item, stack)\n", - "\n", - "Putting some numbers onto a stack::\n", - "\n", - " ()\n", - " (1, ())\n", - " (2, (1, ()))\n", - " (3, (2, (1, ())))\n", - " ...\n", - "\n", - "Python has very nice \"tuple packing and unpacking\" in its syntax which\n", - "means we can directly \"unpack\" the expected arguments to a Joy function.\n", - "\n", - "For example::\n", - "\n", - " def dup((head, tail)):\n", - " return head, (head, tail)\n", - "\n", - "We replace the argument \"stack\" by the expected structure of the stack,\n", - "in this case \"(head, tail)\", and Python takes care of unpacking the\n", - "incoming tuple and assigning values to the names. (Note that Python\n", - "syntax doesn't require parentheses around tuples used in expressions\n", - "where they would be redundant.)\n", - "\n", - "Unfortunately, the Sphinx documentation generator, which is used to generate this\n", - "web page, doesn't handle tuples in the function parameters. And in Python 3, this\n", - "syntax was removed entirely. Instead you would have to write::\n", - "\n", - " def dup(stack):\n", - " head, tail = stack\n", - " return head, (head, tail)\n", - "\n", - "\n", - "We have two very simple functions, one to build up a stack from a Python\n", - "iterable and another to iterate through a stack and yield its items\n", - "one-by-one in order. There are also two functions to generate string representations\n", - "of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions *internal stacks* are\n", - "printed left-to-right. These functions are written to support :doc:`../pretty`.\n", - "\n", - ".. _cons list: https://en.wikipedia.org/wiki/Cons#Lists\n" - ] - } - ], + "outputs": [], "source": [ "import inspect\n", "import joy.utils.stack\n", @@ -140,18 +83,7 @@ "cell_type": "code", "execution_count": 2, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(1, (2, (3, ())))" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "joy.utils.stack.list_to_stack([1, 2, 3])" ] @@ -160,18 +92,7 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "list(joy.utils.stack.iter_stack((1, (2, (3, ())))))" ] @@ -187,16 +108,7 @@ "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3, (2, (1, ())))\n", - "[3, 2, 1]\n" - ] - } - ], + "outputs": [], "source": [ "stack = ()\n", "\n", @@ -230,46 +142,7 @@ "cell_type": "code", "execution_count": 5, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "def joy(stack, expression, dictionary, viewer=None):\n", - "\t'''Evaluate a Joy expression on a stack.\n", - "\n", - "\tThis function iterates through a sequence of terms which are either\n", - "\tliterals (strings, numbers, sequences of terms) or function symbols.\n", - "\tLiterals are put onto the stack and functions are looked up in the\n", - "\tdisctionary and executed.\n", - "\n", - "\tThe viewer is a function that is called with the stack and expression\n", - "\ton every iteration, its return value is ignored.\n", - "\n", - "\t:param stack stack: The stack.\n", - "\t:param stack expression: The expression to evaluate.\n", - "\t:param dict dictionary: A ``dict`` mapping names to Joy functions.\n", - "\t:param function viewer: Optional viewer function.\n", - "\t:rtype: (stack, (), dictionary)\n", - "\n", - "\t'''\n", - "\twhile expression:\n", - "\n", - "\t\tif viewer: viewer(stack, expression)\n", - "\n", - "\t\tterm, expression = expression\n", - "\t\tif isinstance(term, Symbol):\n", - "\t\t\tterm = dictionary[term]\n", - "\t\t\tstack, expression, dictionary = term(stack, expression, dictionary)\n", - "\t\telse:\n", - "\t\t\tstack = term, stack\n", - "\n", - "\tif viewer: viewer(stack, expression)\n", - "\treturn stack, expression, dictionary\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "import joy.joy\n", "\n", @@ -514,7 +387,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swons take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •\n" + "!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •\n" ] } ], @@ -582,42 +455,43 @@ "name": "stdout", "output_type": "stream", "text": [ - "ii == [dip] dupdip i\n", - "of == swap at\n", - "product == 1 swap [*] step\n", - "flatten == [] swap [concat] step\n", - "quoted == [unit] dip\n", - "unquoted == [i] dip\n", - "enstacken == stack [clear] dip\n", "? == dup truthy\n", - "disenstacken == ? [uncons ?] loop pop\n", - "dinfrirst == dip infra first\n", - "nullary == [stack] dinfrirst\n", - "unary == nullary popd\n", - "binary == nullary [popop] dip\n", - "ternary == unary [popop] dip\n", - "pam == [i] map\n", - "run == [] swap infra\n", - "sqr == dup mul\n", - "size == 0 swap [pop ++] step\n", - "fork == [i] app2\n", - "cleave == fork [popd] dip\n", - "average == [sum 1.0 *] [size] cleave /\n", - "gcd == 1 [tuck modulus dup 0 >] loop pop\n", - "least_fraction == dup [gcd] infra [div] concat map\n", "*fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons\n", "*fraction0 == concat [[swap] dip * [*] dip] infra\n", - "down_to_zero == [0 >] [dup --] while\n", - "range_to_zero == unit [down_to_zero] infra\n", "anamorphism == [pop []] swap [dip swons] genrec\n", - "range == [0 <=] [1 - dup] anamorphism\n", - "while == swap [nullary] cons dup dipd concat loop\n", - "dupdipd == dup dipd\n", - "primrec == [i] genrec\n", - "step_zero == 0 roll> step\n", + "average == [sum 1.0 *] [size] cleave /\n", + "binary == nullary [popop] dip\n", + "cleave == fork [popd] dip\n", "codireco == cons dip rest cons\n", - "make_generator == [codireco] ccons\n", + "dinfrirst == dip infra first\n", + "unstack == ? [uncons ?] loop pop\n", + "down_to_zero == [0 >] [dup --] while\n", + "dupdipd == dup dipd\n", + "enstacken == stack [clear] dip\n", + "flatten == [] swap [concat] step\n", + "fork == [i] app2\n", + "gcd == 1 [tuck modulus dup 0 >] loop pop\n", "ifte == [nullary not] dipd branch\n", + "ii == [dip] dupdip i\n", + "least_fraction == dup [gcd] infra [div] concat map\n", + "make_generator == [codireco] ccons\n", + "nullary == [stack] dinfrirst\n", + "of == swap at\n", + "pam == [i] map\n", + "primrec == [i] genrec\n", + "product == 1 swap [*] step\n", + "quoted == [unit] dip\n", + "range == [0 <=] [1 - dup] anamorphism\n", + "range_to_zero == unit [down_to_zero] infra\n", + "run == [] swap infra\n", + "size == 0 swap [pop ++] step\n", + "sqr == dup mul\n", + "step_zero == 0 roll> step\n", + "swoncat == swap concat\n", + "ternary == unary [popop] dip\n", + "unary == nullary popd\n", + "unquoted == [i] dip\n", + "while == swap [nullary] cons dup dipd concat loop\n", "\n" ] } diff --git a/docs/2._Library_Examples.ipynb b/docs/2._Library_Examples.ipynb index 3d4d739..6ed7f05 100644 --- a/docs/2._Library_Examples.ipynb +++ b/docs/2._Library_Examples.ipynb @@ -94,7 +94,8 @@ "metadata": {}, "source": [ "### `enstacken` `disenstacken` `stack` `unstack`\n", - "(I may have these paired up wrong. I.e. `disenstacken` should be `unstack` and vice versa.)" + "\n", + "Replace the stack with a quote of itself." ] }, { @@ -111,7 +112,14 @@ } ], "source": [ - "J('1 2 3 enstacken') # Replace the stack with a quote of itself." + "J('1 2 3 enstacken')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Unpack a list onto the stack." ] }, { @@ -128,7 +136,14 @@ } ], "source": [ - "J('4 5 6 [3 2 1] disenstacken') # Unpack a list onto the stack." + "J('4 5 6 [3 2 1] unstack')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Get the stack on the stack." ] }, { @@ -145,7 +160,16 @@ } ], "source": [ - "J('1 2 3 stack') # Get the stack on the stack." + "J('1 2 3 stack')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Replace the stack with the list on top.\n", + "The items appear reversed but they are not,\n", + "is on the top of both the list and the stack." ] }, { @@ -162,9 +186,7 @@ } ], "source": [ - "J('1 2 3 [4 5 6] unstack') # Replace the stack with the list on top.\n", - " # The items appear reversed but they are not,\n", - " # 4 is on the top of both the list and the stack." + "J('1 2 3 [4 5 6] disenstacken')" ] }, { @@ -461,17 +483,10 @@ }, "outputs": [ { - "ename": "KeyError", - "evalue": "swoncat", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mJ\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'[1 2 3] [4 5 6] swoncat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m/home/sforman/Desktop/ArchLayer/System/source/Thun/docs/notebook_preamble.py\u001b[0m in \u001b[0;36mJ\u001b[0;34m(text, stack, dictionary)\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mJ\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mS\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdictionary\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mD\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mstack_to_string\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstack\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdictionary\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/home/sforman/Desktop/ArchLayer/System/source/Thun/venv/local/lib/python2.7/site-packages/joy/joy.pyc\u001b[0m in \u001b[0;36mrun\u001b[0;34m(text, stack, dictionary, viewer)\u001b[0m\n\u001b[1;32m 77\u001b[0m \t'''\n\u001b[1;32m 78\u001b[0m \u001b[0mexpression\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtext_to_expression\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 79\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mjoy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpression\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdictionary\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mviewer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 80\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/home/sforman/Desktop/ArchLayer/System/source/Thun/venv/local/lib/python2.7/site-packages/joy/joy.pyc\u001b[0m in \u001b[0;36mjoy\u001b[0;34m(stack, expression, dictionary, viewer)\u001b[0m\n\u001b[1;32m 56\u001b[0m \u001b[0mterm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpression\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mexpression\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mterm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 58\u001b[0;31m \u001b[0mterm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdictionary\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mterm\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 59\u001b[0m \u001b[0mstack\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpression\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdictionary\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mterm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpression\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdictionary\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 60\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mKeyError\u001b[0m: swoncat" + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 5 6 1 2 3]\n" ] } ], @@ -481,11 +496,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6 5 4 1 2 3]\n" + ] + } + ], "source": [ "J('[1 2 3] [4 5 6] shunt')" ] @@ -499,27 +522,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ] + } + ], "source": [ "J('1 [2 3] cons')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ] + } + ], "source": [ "J('[2 3] 1 swons')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 [2 3]\n" + ] + } + ], "source": [ "J('[1 2 3] uncons')" ] @@ -533,38 +580,70 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "J('[1 2 3 4] first')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "J('[1 2 3 4] second')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "J('[1 2 3 4] third')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 3 4]\n" + ] + } + ], "source": [ "J('[1 2 3 4] rest')" ] @@ -578,9 +657,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 [3] 4 5 6]\n" + ] + } + ], "source": [ "J('[[1] [2 [3] 4] [5 6]] flatten')" ] @@ -596,47 +683,87 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], "source": [ "J('[10 11 12 13 14] 2 getitem')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "J('[1 2 3 4] 0 at')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "J('2 [1 2 3 4] of')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3 4]\n" + ] + } + ], "source": [ "J('[1 2 3 4] 2 drop')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 1]\n" + ] + } + ], "source": [ "J('[1 2 3 4] 2 take') # reverses the order" ] @@ -657,9 +784,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 3 1 4]\n" + ] + } + ], "source": [ "J('[1 2 3 1 4] 1 remove')" ] @@ -673,11 +808,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 3 2 1]\n" + ] + } + ], "source": [ "J('[1 2 3 4] reverse')" ] @@ -691,11 +834,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ "J('[1 1 1 1] size')" ] @@ -710,11 +861,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6 5 4 [3 2 1]\n" + ] + } + ], "source": [ "J('1 2 3 [4 5 6] swaack')" ] @@ -728,40 +887,72 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], "source": [ "J('23 9 1 choice')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ "J('23 9 0 choice')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], "source": [ "J('[23 9 7] 1 select') # select is basically getitem, should retire it?" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ "J('[23 9 7] 0 select')" ] @@ -775,18 +966,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[6 1] [5 2] [4 3]]\n" + ] + } + ], "source": [ "J('[1 2 3] [6 5 4] zip')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[7 7 7]\n" + ] + } + ], "source": [ "J('[1 2 3] [6 5 4] zip [sum] map')" ] @@ -807,9 +1014,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "32\n" + ] + } + ], "source": [ "J('23 9 +')" ] @@ -823,9 +1038,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], "source": [ "J('23 9 -')" ] @@ -839,9 +1062,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "207\n" + ] + } + ], "source": [ "J('23 9 *')" ] @@ -855,66 +1086,114 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.5555555555555554\n" + ] + } + ], "source": [ "J('23 9 /')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-2.5555555555555554\n" + ] + } + ], "source": [ "J('23 -9 truediv')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.5555555555555554\n" + ] + } + ], "source": [ "J('23 9 div')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "J('23 9 floordiv')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-2.5555555555555554\n" + ] + } + ], "source": [ "J('23 -9 div')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-3\n" + ] + } + ], "source": [ "J('23 -9 floordiv')" ] @@ -928,9 +1207,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], "source": [ "J('23 9 %')" ] @@ -944,9 +1231,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-23 5\n" + ] + } + ], "source": [ "J('23 neg -5 neg')" ] @@ -960,9 +1255,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1024\n" + ] + } + ], "source": [ "J('2 10 pow')" ] @@ -976,18 +1279,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "529\n" + ] + } + ], "source": [ "J('23 sqr')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4.795831523312719\n" + ] + } + ], "source": [ "J('23 sqrt')" ] @@ -1001,20 +1320,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "J('1 ++')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ "J('1 --')" ] @@ -1028,18 +1363,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n" + ] + } + ], "source": [ "J('8 1 <<')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ "J('8 1 >>')" ] @@ -1053,9 +1404,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.75\n" + ] + } + ], "source": [ "J('[1 2 3 5] average')" ] @@ -1069,27 +1428,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 3 2 1 0]\n" + ] + } + ], "source": [ "J('5 range')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5]\n" + ] + } + ], "source": [ "J('5 range_to_zero')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 4 3 2 1 0\n" + ] + } + ], "source": [ "J('5 down_to_zero')" ] @@ -1103,9 +1486,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n" + ] + } + ], "source": [ "J('[1 2 3 5] product')" ] @@ -1119,9 +1510,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11\n" + ] + } + ], "source": [ "J('[1 2 3 5] sum')" ] @@ -1135,9 +1534,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "J('[1 2 3 5] min')" ] @@ -1151,9 +1558,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n" + ] + } + ], "source": [ "J('45 30 gcd')" ] @@ -1168,18 +1583,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3.0 2.0]\n" + ] + } + ], "source": [ "J('[45 30] least_fraction')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[23.0 12.0]\n" + ] + } + ], "source": [ "J('[23 12] least_fraction')" ] @@ -1201,27 +1632,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "J('23 truthy')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "J('[] truthy') # Python semantics." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "J('0 truthy')" ] @@ -1235,27 +1690,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 23 ?\n", + " 23 . ?\n", + " 23 . dup truthy\n", + " 23 23 . truthy\n", + "23 True . \n" + ] + } + ], "source": [ "V('23 ?')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[] False\n" + ] + } + ], "source": [ "J('[] ?')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 False\n" + ] + } + ], "source": [ "J('0 ?')" ] @@ -1269,9 +1752,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "J('23 9 &')" ] @@ -1285,9 +1776,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "J('23 9 !=')" ] @@ -1315,18 +1814,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 83, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ "J('1 1 ^')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "J('1 0 ^')" ] @@ -1347,11 +1862,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on help ====\n", + "\n", + "Accepts a quoted symbol on the top of the stack and prints its docs.\n", + "\n", + "---- end (help)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[help] help')" ] @@ -1365,20 +1895,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on parse ====\n", + "\n", + "Parse the string on the stack to a Joy expression.\n", + "\n", + "---- end (parse)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[parse] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 [2 [3] dup]\n" + ] + } + ], "source": [ "J('1 \"2 [3] dup\" parse')" ] @@ -1393,9 +1946,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5]\n" + ] + } + ], "source": [ "J('[1 2 dup + +] run')" ] @@ -1416,45 +1977,112 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on app1 ====\n", + "\n", + "Given a quoted program on TOS and anything as the second stack item run\n", + "the program and replace the two args with the first result of the\n", + "program.\n", + "::\n", + "\n", + " ... x [Q] . app1\n", + " -----------------------------------\n", + " ... [x ...] [Q] . infra first\n", + "\n", + "---- end (app1)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[app1] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 160\n" + ] + } + ], "source": [ "J('10 4 [sqr *] app1')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 90 160\n" + ] + } + ], "source": [ "J('10 3 4 [sqr *] app2')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on app2 ====\n", + "\n", + "Like app1 with two items.\n", + "::\n", + "\n", + " ... y x [Q] . app2\n", + " -----------------------------------\n", + " ... [y ...] [Q] . infra first\n", + " [x ...] [Q] infra first\n", + "\n", + "---- end (app2)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[app2] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 93, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 40 90 160\n" + ] + } + ], "source": [ "J('10 2 3 4 [sqr *] app3')" ] @@ -1477,9 +2105,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 1 0]\n" + ] + } + ], "source": [ "J('3 [0 <=] [1 - dup] anamorphism')" ] @@ -1493,18 +2129,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 95, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], "source": [ "J('3 4 1 [+] [*] branch')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], "source": [ "J('3 4 0 [+] [*] branch')" ] @@ -1529,9 +2181,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 12 8\n" + ] + } + ], "source": [ "J('10 2 [+] [-] cleave')" ] @@ -1545,27 +2205,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 7 5\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] dip')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 5 4 5\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] dipd')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 100, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 3 4 5\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] dipdd')" ] @@ -1582,9 +2266,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 23 [++] dupdip *\n", + " 23 . [++] dupdip *\n", + "23 [++] . dupdip *\n", + " 23 . ++ 23 *\n", + " 24 . 23 *\n", + " 24 23 . *\n", + " 552 . \n" + ] + } + ], "source": [ "V('23 [++] dupdip *') # N(N + 1)" ] @@ -1598,18 +2296,88 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on genrec ====\n", + "\n", + "General Recursion Combinator.\n", + "::\n", + "\n", + " [if] [then] [rec1] [rec2] genrec\n", + " ---------------------------------------------------------------------\n", + " [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte\n", + "\n", + "From \"Recursion Theory and Joy\" (j05cmp.html) by Manfred von Thun:\n", + "\"The genrec combinator takes four program parameters in addition to\n", + "whatever data parameters it needs. Fourth from the top is an if-part,\n", + "followed by a then-part. If the if-part yields true, then the then-part\n", + "is executed and the combinator terminates. The other two parameters are\n", + "the rec1-part and the rec2-part. If the if-part yields false, the\n", + "rec1-part is executed. Following that the four program parameters and\n", + "the combinator are again pushed onto the stack bundled up in a quoted\n", + "form. Then the rec2-part is executed, where it will find the bundled\n", + "form. Typically it will then execute the bundled form, either with i or\n", + "with app2, or some other combinator.\"\n", + "\n", + "The way to design one of these is to fix your base case [then] and the\n", + "test [if], and then treat rec1 and rec2 as an else-part \"sandwiching\"\n", + "a quotation of the whole function.\n", + "\n", + "For example, given a (general recursive) function 'F':\n", + "::\n", + "\n", + " F == [I] [T] [R1] [R2] genrec\n", + "\n", + "If the [I] if-part fails you must derive R1 and R2 from:\n", + "::\n", + "\n", + " ... R1 [F] R2\n", + "\n", + "Just set the stack arguments in front, and figure out what R1 and R2\n", + "have to do to apply the quoted [F] in the proper way. In effect, the\n", + "genrec combinator turns into an ifte combinator with a quoted copy of\n", + "the original definition in the else-part:\n", + "::\n", + "\n", + " F == [I] [T] [R1] [R2] genrec\n", + " == [I] [T] [R1 [F] R2] ifte\n", + "\n", + "Primitive recursive functions are those where R2 == i.\n", + "::\n", + "\n", + " P == [I] [T] [R] primrec\n", + " == [I] [T] [R [P] i] ifte\n", + " == [I] [T] [R P] ifte\n", + "\n", + "---- end (genrec)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[genrec] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], "source": [ "J('3 [1 <=] [] [dup --] [i *] genrec')" ] @@ -1623,11 +2391,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 1 2 3 [+ +] i\n", + " 1 . 2 3 [+ +] i\n", + " 1 2 . 3 [+ +] i\n", + " 1 2 3 . [+ +] i\n", + "1 2 3 [+ +] . i\n", + " 1 2 3 . + +\n", + " 1 5 . +\n", + " 6 . \n" + ] + } + ], "source": [ "V('1 2 3 [+ +] i')" ] @@ -1642,18 +2425,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 105, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "J('1 2 [1] [+] [*] ifte')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "J('1 2 [0] [+] [*] ifte')" ] @@ -1667,9 +2466,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 107, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 1 2 3 [4 5 6] [* +] infra\n", + " 1 . 2 3 [4 5 6] [* +] infra\n", + " 1 2 . 3 [4 5 6] [* +] infra\n", + " 1 2 3 . [4 5 6] [* +] infra\n", + " 1 2 3 [4 5 6] . [* +] infra\n", + "1 2 3 [4 5 6] [* +] . infra\n", + " 6 5 4 . * + [3 2 1] swaack\n", + " 6 20 . + [3 2 1] swaack\n", + " 26 . [3 2 1] swaack\n", + " 26 [3 2 1] . swaack\n", + " 1 2 3 [26] . \n" + ] + } + ], "source": [ "V('1 2 3 [4 5 6] [* +] infra')" ] @@ -1683,18 +2500,69 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 108, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on loop ====\n", + "\n", + "Basic loop combinator.\n", + "::\n", + "\n", + " ... True [Q] loop\n", + " -----------------------\n", + " ... Q [Q] loop\n", + "\n", + " ... False [Q] loop\n", + " ------------------------\n", + " ...\n", + "\n", + "---- end (loop)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[loop] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 109, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 3 dup [1 - dup] loop\n", + " 3 . dup [1 - dup] loop\n", + " 3 3 . [1 - dup] loop\n", + "3 3 [1 - dup] . loop\n", + " 3 . 1 - dup [1 - dup] loop\n", + " 3 1 . - dup [1 - dup] loop\n", + " 2 . dup [1 - dup] loop\n", + " 2 2 . [1 - dup] loop\n", + "2 2 [1 - dup] . loop\n", + " 2 . 1 - dup [1 - dup] loop\n", + " 2 1 . - dup [1 - dup] loop\n", + " 1 . dup [1 - dup] loop\n", + " 1 1 . [1 - dup] loop\n", + "1 1 [1 - dup] . loop\n", + " 1 . 1 - dup [1 - dup] loop\n", + " 1 1 . - dup [1 - dup] loop\n", + " 0 . dup [1 - dup] loop\n", + " 0 0 . [1 - dup] loop\n", + "0 0 [1 - dup] . loop\n", + " 0 . \n" + ] + } + ], "source": [ "V('3 dup [1 - dup] loop')" ] @@ -1708,18 +2576,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 110, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 [10 20 30]\n" + ] + } + ], "source": [ "J('10 [1 2 3] [*] map')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 111, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 5 [50 2.0 15 5]\n" + ] + } + ], "source": [ "J('10 5 [[*][/][+][-]] pam')" ] @@ -1734,36 +2618,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5 9\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] nullary')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 113, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 9\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] unary')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 9\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] binary') # + has arity 2 so this is technically pointless..." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 115, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 9\n" + ] + } + ], "source": [ "J('1 2 3 4 5 [+] ternary')" ] @@ -1777,20 +2693,77 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 116, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on step ====\n", + "\n", + "Run a quoted program on each item in a sequence.\n", + "::\n", + "\n", + " ... [] [Q] . step\n", + " -----------------------\n", + " ... .\n", + "\n", + "\n", + " ... [a] [Q] . step\n", + " ------------------------\n", + " ... a . Q\n", + "\n", + "\n", + " ... [a b c] [Q] . step\n", + " ----------------------------------------\n", + " ... a . Q [b c] [Q] step\n", + "\n", + "The step combinator executes the quotation on each member of the list\n", + "on top of the stack.\n", + "\n", + "---- end (step)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[step] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 117, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 0 [1 2 3] [+] step\n", + " 0 . [1 2 3] [+] step\n", + " 0 [1 2 3] . [+] step\n", + "0 [1 2 3] [+] . step\n", + " 0 1 [+] . i [2 3] [+] step\n", + " 0 1 . + [2 3] [+] step\n", + " 1 . [2 3] [+] step\n", + " 1 [2 3] . [+] step\n", + " 1 [2 3] [+] . step\n", + " 1 2 [+] . i [3] [+] step\n", + " 1 2 . + [3] [+] step\n", + " 3 . [3] [+] step\n", + " 3 [3] . [+] step\n", + " 3 [3] [+] . step\n", + " 3 3 [+] . i\n", + " 3 3 . +\n", + " 6 . \n" + ] + } + ], "source": [ "V('0 [1 2 3] [+] step')" ] @@ -1804,9 +2777,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 118, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 3 2 1 2 [+] times\n", + " 3 . 2 1 2 [+] times\n", + " 3 2 . 1 2 [+] times\n", + " 3 2 1 . 2 [+] times\n", + " 3 2 1 2 . [+] times\n", + "3 2 1 2 [+] . times\n", + " 3 2 1 . + 1 [+] times\n", + " 3 3 . 1 [+] times\n", + " 3 3 1 . [+] times\n", + " 3 3 1 [+] . times\n", + " 3 3 . +\n", + " 6 . \n" + ] + } + ], "source": [ "V('3 2 1 2 [+] times')" ] @@ -1820,18 +2812,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on b ====\n", + "\n", + "::\n", + "\n", + " b == [i] dip i\n", + "\n", + " ... [P] [Q] b == ... [P] i [Q] i\n", + " ... [P] [Q] b == ... P Q\n", + "\n", + "---- end (b)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[b] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 120, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 1 2 [3] [4] b\n", + " 1 . 2 [3] [4] b\n", + " 1 2 . [3] [4] b\n", + " 1 2 [3] . [4] b\n", + "1 2 [3] [4] . b\n", + " 1 2 . 3 4\n", + " 1 2 3 . 4\n", + " 1 2 3 4 . \n" + ] + } + ], "source": [ "V('1 2 [3] [4] b')" ] @@ -1846,9 +2873,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 121, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 0\n" + ] + } + ], "source": [ "J('3 [0 >] [dup --] while')" ] @@ -1862,18 +2897,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 122, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "==== Help on x ====\n", + "\n", + "::\n", + "\n", + " x == dup i\n", + "\n", + " ... [Q] x = ... [Q] dup i\n", + " ... [Q] x = ... [Q] [Q] i\n", + " ... [Q] x = ... [Q] Q\n", + "\n", + "---- end (x)\n", + "\n", + "\n" + ] + } + ], "source": [ "J('[x] help')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 123, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 1 [2] [i 3] x\n", + " 1 . [2] [i 3] x\n", + " 1 [2] . [i 3] x\n", + "1 [2] [i 3] . x\n", + "1 [2] [i 3] . i 3\n", + " 1 [2] . i 3 3\n", + " 1 . 2 3 3\n", + " 1 2 . 3 3\n", + " 1 2 3 . 3\n", + " 1 2 3 3 . \n" + ] + } + ], "source": [ "V('1 [2] [i 3] x') # Kind of a pointless example." ] @@ -1888,36 +2961,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 124, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "J('[] void')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 125, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "J('[[]] void')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 126, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "J('[[][[]]] void')" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 127, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "J('[[[]][[][]]] void')" ] diff --git a/docs/Trees.ipynb b/docs/Trees.ipynb index 8dd1113..8baf723 100644 --- a/docs/Trees.ipynb +++ b/docs/Trees.ipynb @@ -2417,7 +2417,7 @@ " left BTree-iter-order key value F right BTree-iter-order\n", "\n", "\n", - " [key value left right] disenstacken swap\n", + " [key value left right] unstack swap\n", " key value left right swap\n", " key value right left\n", "\n", @@ -2432,11 +2432,11 @@ "\n", "So:\n", "\n", - " R0 == disenstacken swap\n", + " R0 == unstack swap\n", " R1 == [cons dipdd [F] dip] dupdip i\n", "\n", " [key value left right] R0 [BTree-iter-order] R1\n", - " [key value left right] disenstacken swap [BTree-iter-order] [cons dipdd [F] dip] dupdip i\n", + " [key value left right] unstack swap [BTree-iter-order] [cons dipdd [F] dip] dupdip i\n", " key value right left [BTree-iter-order] [cons dipdd [F] dip] dupdip i\n", "\n", " key value right left [BTree-iter-order] cons dipdd [F] dip [BTree-iter-order] i\n", @@ -2446,7 +2446,7 @@ " left BTree-iter-order key value F right BTree-iter-order\n", "\n", "\n", - " BTree-iter-order == [not] [pop] [disenstacken swap] [[cons dipdd [F] dip] dupdip i] genrec" + " BTree-iter-order == [not] [pop] [unstack swap] [[cons dipdd [F] dip] dupdip i] genrec" ] }, { @@ -2531,8 +2531,8 @@ "\n", "We worked out one scheme for ?in-order? traversal above, but maybe we can do better?\n", "\n", - " [key value left right] [F] [BTree-iter] [disenstacken] dipd\n", - " [key value left right] disenstacken [F] [BTree-iter]\n", + " [key value left right] [F] [BTree-iter] [unstack] dipd\n", + " [key value left right] unstack [F] [BTree-iter]\n", " key value left right [F] [BTree-iter]\n", "\n", " key value left right [F] [BTree-iter] R1.1\n", @@ -2543,8 +2543,8 @@ " key value left right [BTree-iter] [F] [BTree-iter] \n", "\n", "\n", - " [key value left right] [F] [BTree-iter] [disenstacken [roll>] dip] dipd\n", - " [key value left right] disenstacken [roll>] dip [F] [BTree-iter]\n", + " [key value left right] [F] [BTree-iter] [unstack [roll>] dip] dipd\n", + " [key value left right] unstack [roll>] dip [F] [BTree-iter]\n", " key value left right [roll>] dip [F] [BTree-iter]\n", " key value left roll> right [F] [BTree-iter]\n", " left key value right [F] [BTree-iter]\n", diff --git a/joy/library.py b/joy/library.py index 3292826..1e83a7d 100644 --- a/joy/library.py +++ b/joy/library.py @@ -134,7 +134,7 @@ ALIASES = ( ('mul', ['*']), ('floordiv', ['/floor', '//']), ('floor', ['round']), - ('truediv', ['/']), + ('truediv', ['/', 'div']), ('mod', ['%', 'rem', 'remainder', 'modulus']), ('eq', ['=']), ('ge', ['>=']),