Fixes #38237 Sort out stack/unstack/enstacken/disenstacken

This commit is contained in:
Simon Forman 2020-04-28 10:42:09 -07:00
parent fac2d2ae5f
commit 750f498512
4 changed files with 1379 additions and 400 deletions

View File

@ -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"
]
}

File diff suppressed because it is too large Load Diff

View File

@ -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",

View File

@ -134,7 +134,7 @@ ALIASES = (
('mul', ['*']),
('floordiv', ['/floor', '//']),
('floor', ['round']),
('truediv', ['/']),
('truediv', ['/', 'div']),
('mod', ['%', 'rem', 'remainder', 'modulus']),
('eq', ['=']),
('ge', ['>=']),