{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples (and some documentation) for the Words in the Library" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from notebook_preamble import J, V" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Stack Chatter\n", "This is what I like to call the functions that just rearrange things on the stack. (One thing I want to mention is that during a hypothetical compilation phase these \"stack chatter\" words effectively disappear, because we can map the logical stack locations to registers that remain static for the duration of the computation. This remains to be done but it's \"off the shelf\" technology.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `clear`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "J('1 2 3 clear')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `dup` `dupd`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 3\n" ] } ], "source": [ "J('1 2 3 dup')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 2 3\n" ] } ], "source": [ "J('1 2 3 dupd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `enstacken` `disenstacken` `stack` `unstack`\n", "(I may have these paired up wrong. I.e. `disenstacken` should be `unstack` and vice versa.)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 2 1]\n" ] } ], "source": [ "J('1 2 3 enstacken') # Replace the stack with a quote of itself." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 5 6 3 2 1\n" ] } ], "source": [ "J('4 5 6 [3 2 1] disenstacken') # Unpack a list onto the stack." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 [3 2 1]\n" ] } ], "source": [ "J('1 2 3 stack') # Get the stack on the stack." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 5 4\n" ] } ], "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `pop` `popd` `popop`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2\n" ] } ], "source": [ "J('1 2 3 pop')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3\n" ] } ], "source": [ "J('1 2 3 popd')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "J('1 2 3 popop')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `roll<` `rolldown` `roll>` `rollup`\n", "The \"down\" and \"up\" refer to the movement of two of the top three items (displacing the third.)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 3 1\n" ] } ], "source": [ "J('1 2 3 roll<')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 1 2\n" ] } ], "source": [ "J('1 2 3 roll>')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `swap`" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3 2\n" ] } ], "source": [ "J('1 2 3 swap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `tuck` `over`" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3 2 3\n" ] } ], "source": [ "J('1 2 3 tuck')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 2\n" ] } ], "source": [ "J('1 2 3 over')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `unit` `quoted` `unquoted`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 [3]\n" ] } ], "source": [ "J('1 2 3 unit')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 [2] 3\n" ] } ], "source": [ "J('1 2 3 quoted')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3\n" ] } ], "source": [ "J('1 [2] 3 unquoted')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " . 1 [dup] 3 unquoted\n", " 1 . [dup] 3 unquoted\n", " 1 [dup] . 3 unquoted\n", " 1 [dup] 3 . unquoted\n", " 1 [dup] 3 . [i] dip\n", "1 [dup] 3 [i] . dip\n", " 1 [dup] . i 3\n", " 1 . dup 3\n", " 1 1 . 3\n", " 1 1 3 . \n" ] } ], "source": [ "V('1 [dup] 3 unquoted') # Unquoting evaluates. Be aware." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `concat` `swoncat` `shunt`" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "J('[1 2 3] [4 5 6] concat')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "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" ] } ], "source": [ "J('[1 2 3] [4 5 6] swoncat')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[1 2 3] [4 5 6] shunt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `cons` `swons` `uncons`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 [2 3] cons')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[2 3] 1 swons')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3] uncons')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `first` `second` `third` `rest`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 4] first')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 4] second')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 4] third')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[1 2 3 4] rest')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `flatten`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[1] [2 [3] 4] [5 6]] flatten')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `getitem` `at` `of` `drop` `take`\n", "\n", "`at` and `getitem` are the same function. `of == swap at`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[10 11 12 13 14] 2 getitem')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 4] 0 at')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('2 [1 2 3 4] of')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 4] 2 drop')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 4] 2 take') # reverses the order" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`reverse` could be defines as `reverse == dup size take`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `remove`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 1 4] 1 remove')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `reverse`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[1 2 3 4] reverse')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `size`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[1 1 1 1] size')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `swaack`\n", "\"Swap stack\" swap the list on the top of the stack for the stack, and put the old stack on top of the new one. Think of it as a context switch. Niether of the lists/stacks change their order." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('1 2 3 [4 5 6] swaack')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `choice` `select`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('23 9 1 choice')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 0 choice')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[23 9 7] 1 select') # select is basically getitem, should retire it?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[23 9 7] 0 select')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `zip`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3] [6 5 4] zip')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3] [6 5 4] zip [sum] map')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Math words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `+` `add`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 +')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `-` `sub`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 -')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `*` `mul`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 *')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `/` `div` `floordiv` `truediv`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('23 9 /')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "J('23 -9 truediv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "J('23 9 div')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "J('23 9 floordiv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "J('23 -9 div')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "J('23 -9 floordiv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `%` `mod` `modulus` `rem` `remainder`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 %')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `neg`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 neg -5 neg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pow" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('2 10 pow')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `sqr` `sqrt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 sqr')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 sqrt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `++` `succ` `--` `pred`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 ++')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('1 --')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `<<` `lshift` `>>` `rshift`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('8 1 <<')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('8 1 >>')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `average`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 5] average')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `range` `range_to_zero` `down_to_zero`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('5 range')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('5 range_to_zero')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('5 down_to_zero')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `product`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 5] product')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `sum`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 5] sum')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `min`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 3 5] min')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `gcd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('45 30 gcd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `least_fraction`\n", "If we represent fractions as a quoted pair of integers [q d] this word reduces them to their ... least common factors or whatever." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[45 30] least_fraction')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[23 12] least_fraction')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Logic and Comparison" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `?` `truthy`\n", "Get the Boolean value of the item on the top of the stack." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 truthy')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[] truthy') # Python semantics." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('0 truthy')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ? == dup truthy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('23 ?')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[] ?')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('0 ?')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `&` `and` " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 &')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `!=` `<>` `ne`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('23 9 !=')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The usual suspects:\n", "- `<` `lt`\n", "- `<=` `le` \n", "- `=` `eq`\n", "- `>` `gt`\n", "- `>=` `ge`\n", "- `not`\n", "- `or`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `^` `xor`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 1 ^')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 0 ^')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Miscellaneous" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `help`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[help] help')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `parse`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "J('[parse] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 \"2 [3] dup\" parse')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `run`\n", "Evaluate a quoted Joy sequence." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[1 2 dup + +] run')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Combinators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `app1` `app2` `app3`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[app1] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 4 [sqr *] app1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 3 4 [sqr *] app2')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[app2] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 2 3 4 [sqr *] app3')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `anamorphism`\n", "Given an initial value, a predicate function `[P]`, and a generator function `[G]`, the `anamorphism` combinator creates a sequence.\n", "\n", " n [P] [G] anamorphism\n", " ---------------------------\n", " [...]\n", "\n", "Example, `range`:\n", "\n", " range == [0 <=] [1 - dup] anamorphism" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 [0 <=] [1 - dup] anamorphism')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `branch`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 4 1 [+] [*] branch')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 4 0 [+] [*] branch')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `cleave`\n", " ... x [P] [Q] cleave\n", "\n", "From the original Joy docs: \"The cleave combinator expects two quotations, and below that an item `x`\n", "It first executes `[P]`, with `x` on top, and saves the top result element.\n", "Then it executes `[Q]`, again with `x`, and saves the top result.\n", "Finally it restores the stack to what it was below `x` and pushes the two\n", "results P(X) and Q(X).\"\n", "\n", "Note that `P` and `Q` can use items from the stack freely, since the stack (below `x`) is restored. `cleave` is a kind of *parallel* primitive, and it would make sense to create a version that uses, e.g. Python threads or something, to actually run `P` and `Q` concurrently. The current implementation of `cleave` is a definition in terms of `app2`:\n", "\n", " cleave == [i] app2 [popd] dip" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 2 [+] [-] cleave')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `dip` `dipd` `dipdd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] dip')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] dipd')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] dipdd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `dupdip`\n", "Expects a quoted program `[Q]` on the stack and some item under it, `dup` the item and `dip` the quoted program under it.\n", "\n", " n [Q] dupdip == n Q n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('23 [++] dupdip *') # N(N + 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `genrec` `primrec`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[genrec] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 [1 <=] [] [dup --] [i *] genrec')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `i`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "V('1 2 3 [+ +] i')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `ifte`\n", " [predicate] [then] [else] ifte" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 [1] [+] [*] ifte')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 [0] [+] [*] ifte')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `infra`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('1 2 3 [4 5 6] [* +] infra')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `loop`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[loop] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('3 dup [1 - dup] loop')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `map` `pam`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 [1 2 3] [*] map')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 5 [[*][/][+][-]] pam')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `nullary` `unary` `binary` `ternary`\n", "Run a quoted program enforcing [arity](https://en.wikipedia.org/wiki/Arity)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] nullary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] unary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] binary') # + has arity 2 so this is technically pointless..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] ternary')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `step`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[step] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "V('0 [1 2 3] [+] step')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `times`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('3 2 1 2 [+] times')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `b`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[b] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('1 2 [3] [4] b')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `while`\n", " [predicate] [body] while" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 [0 >] [dup --] while')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `x`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[x] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('1 [2] [i 3] x') # Kind of a pointless example." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `void`\n", "Implements [**Laws of Form** *arithmetic*](https://en.wikipedia.org/wiki/Laws_of_Form#The_primary_arithmetic_.28Chapter_4.29) over quote-only datastructures (that is, datastructures that consist soley of containers, without strings or numbers or anything else.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[] void')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[]] void')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[][[]]] void')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[[]][[][]]] void')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 2 }