diff --git a/docs/0._This_Implementation_of_Joy_in_Python.html b/docs/0._This_Implementation_of_Joy_in_Python.html new file mode 100644 index 0000000..6bb8ac8 --- /dev/null +++ b/docs/0._This_Implementation_of_Joy_in_Python.html @@ -0,0 +1,12620 @@ + + + +0._This_Implementation_of_Joy_in_Python + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Joypy

Joy in Python

This implementation is meant as a tool for exploring the programming model and method of Joy. Python seems like a great implementation language for Joy for several reasons.

+

We can lean on the Python immutable types for our basic semantics and types: ints, floats, strings, and tuples, which enforces functional purity. We get garbage collection for free. Compilation via Cython. Glue language with loads of libraries.

+ +
+
+
+
+
+
+
+

Read-Eval-Print Loop (REPL)

The main way to interact with the Joy interpreter is through a simple REPL that you start by running the package:

+ +
$ python -m joy
+Joypy - Copyright © 2017 Simon Forman
+This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty".
+This is free software, and you are welcome to redistribute it
+under certain conditions; type "sharing" for details.
+Type "words" to see a list of all words, and "[<name>] help" to print the
+docs for a word.
+
+
+ <-top
+
+joy? _
+
+
+

The <-top marker points to the top of the (initially empty) stack. You can enter Joy notation at the prompt and a trace of evaluation will be printed followed by the stack and prompt again:

+ +
joy? 23 sqr 18 +
+       . 23 sqr 18 +
+    23 . sqr 18 +
+    23 . dup mul 18 +
+ 23 23 . mul 18 +
+   529 . 18 +
+529 18 . +
+   547 . 
+
+547 <-top
+
+joy? 
+ +
+
+
+
+
+
+
+

Stacks (aka list, quote, sequence, etc.)

In Joy, in addition to the types Boolean, integer, float, and string, there is a single sequence type represented by enclosing a sequence of terms in brackets [...]. This sequence type is used to represent both the stack and the expression. It is a cons list made from Python tuples.

+ +
+
+
+
+
+
In [1]:
+
+
+
import inspect
+import joy.utils.stack
+
+
+print inspect.getdoc(joy.utils.stack)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
§ Stack
+
+
+When talking about Joy we use the terms "stack", "list", "sequence" and
+"aggregate" to mean the same thing: a simple datatype that permits
+certain operations such as iterating and pushing and popping values from
+(at least) one end.
+
+We use the venerable two-tuple recursive form of sequences where the
+empty tuple () is the empty stack and (head, rest) gives the recursive
+form of a stack with one or more items on it.
+
+  ()
+  (1, ())
+  (2, (1, ()))
+  (3, (2, (1, ())))
+  ...
+
+And so on.
+
+
+We have two very simple functions to build up a stack from a Python
+iterable and also to iterate through a stack and yield its items
+one-by-one in order, and two functions to generate string representations
+of stacks:
+
+  list_to_stack()
+
+  iter_stack()
+
+  expression_to_string()  (prints left-to-right)
+
+  stack_to_string()  (prints right-to-left)
+
+
+A word about the stack data structure.
+
+Python has very nice "tuple packing and unpacking" in its syntax which
+means we can directly "unpack" the expected arguments to a Joy function.
+
+For example:
+
+  def dup(stack):
+    head, tail = stack
+    return head, (head, tail)
+
+We replace the argument "stack" by the expected structure of the stack,
+in this case "(head, tail)", and Python takes care of de-structuring the
+incoming argument and assigning values to the names.  Note that Python
+syntax doesn't require parentheses around tuples used in expressions
+where they would be redundant.
+
+
+
+ +
+
+ +
+
+
+
+
+

The utility functions maintain order.

The 0th item in the list will be on the top of the stack and vise versa.

+ +
+
+
+
+
+
In [2]:
+
+
+
joy.utils.stack.list_to_stack([1, 2, 3])
+
+ +
+
+
+ +
+
+ + +
+ +
Out[2]:
+ + + + +
+
(1, (2, (3, ())))
+
+ +
+ +
+
+ +
+
+
+
In [3]:
+
+
+
list(joy.utils.stack.iter_stack((1, (2, (3, ())))))
+
+ +
+
+
+ +
+
+ + +
+ +
Out[3]:
+ + + + +
+
[1, 2, 3]
+
+ +
+ +
+
+ +
+
+
+
+
+

This requires reversing the sequence (or iterating backwards) otherwise:

+ +
+
+
+
+
+
In [4]:
+
+
+
stack = ()
+
+for n in [1, 2, 3]:
+    stack = n, stack
+
+print stack
+print list(joy.utils.stack.iter_stack(stack))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
(3, (2, (1, ())))
+[3, 2, 1]
+
+
+
+ +
+
+ +
+
+
+
+
+

Purely Functional Datastructures.

Because Joy lists are made out of Python tuples they are immutable, so all Joy datastructures are purely functional.

+ +
+
+
+
+
+
+
+

The joy() function.

An Interpreter

The joy() function is extrememly simple. It accepts a stack, an expression, and a dictionary, and it iterates through the expression putting values onto the stack and delegating execution to functions it looks up in the dictionary.

+

Each function is passed the stack, expression, and dictionary and returns them. Whatever the function returns becomes the new stack, expression, and dictionary. (The dictionary is passed to enable e.g. writing words that let you enter new words into the dictionary at runtime, which nothing does yet and may be a bad idea, and the help command.)

+ +
+
+
+
+
+
In [5]:
+
+
+
import joy.joy
+
+print inspect.getsource(joy.joy.joy)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
def joy(stack, expression, dictionary, viewer=None):
+  '''
+  Evaluate the Joy expression on the stack.
+  '''
+  while expression:
+
+    if viewer: viewer(stack, expression)
+
+    term, expression = expression
+    if isinstance(term, Symbol):
+      term = dictionary[term]
+      stack, expression, dictionary = term(stack, expression, dictionary)
+    else:
+      stack = term, stack
+
+  if viewer: viewer(stack, expression)
+  return stack, expression, dictionary
+
+
+
+
+ +
+
+ +
+
+
+
+
+

View function

The joy() function accepts a "viewer" function which it calls on each iteration passing the current stack and expression just before evaluation. This can be used for tracing, breakpoints, retrying after exceptions, or interrupting an evaluation and saving to disk or sending over the network to resume later. The stack and expression together contain all the state of the computation at each step.

+ +
+
+
+
+
+
+
+

The TracePrinter.

A viewer records each step of the evaluation of a Joy program. The TracePrinter has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression ("continuation") on the right.

+ +
+
+
+
+
+
+
+

Continuation-Passing Style

One day I thought, What happens if you rewrite Joy to use CSP? I made all the functions accept and return the expression as well as the stack and found that all the combinators could be rewritten to work by modifying the expression rather than making recursive calls to the joy() function.

+ +
+
+
+
+
+
+
+

Parser

+
+
+
+
+
+
In [6]:
+
+
+
import joy.parser
+
+print inspect.getdoc(joy.parser)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
§ Converting text to a joy expression.
+
+This module exports a single function:
+
+  text_to_expression(text)
+
+As well as a single Symbol class and a single Exception type:
+
+  ParseError
+
+When supplied with a string this function returns a Python datastructure
+that represents the Joy datastructure described by the text expression.
+Any unbalanced square brackets will raise a ParseError.
+
+
+
+ +
+
+ +
+
+
+
+
+

The parser is extremely simple, the undocumented re.Scanner class does most of the tokenizing work and then you just build the tuple structure out of the tokens. There's no Abstract Syntax Tree or anything like that.

+ +
+
+
+
+
+
In [7]:
+
+
+
print inspect.getsource(joy.parser._parse)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
def _parse(tokens):
+  '''
+  Return a stack/list expression of the tokens.
+  '''
+  frame = []
+  stack = []
+  for tok in tokens:
+    if tok == '[':
+      stack.append(frame)
+      frame = []
+      stack[-1].append(frame)
+    elif tok == ']':
+      try:
+        frame = stack.pop()
+      except IndexError:
+        raise ParseError('One or more extra closing brackets.')
+      frame[-1] = list_to_stack(frame[-1])
+    else:
+      frame.append(tok)
+  if stack:
+    raise ParseError('One or more unclosed brackets.')
+  return list_to_stack(frame)
+
+
+
+
+ +
+
+ +
+
+
+
+
+

That's pretty much all there is to it.

+ +
+
+
+
+
+
In [8]:
+
+
+
joy.parser.text_to_expression('1 2 3 4 5')  # A simple sequence.
+
+ +
+
+
+ +
+
+ + +
+ +
Out[8]:
+ + + + +
+
(1, (2, (3, (4, (5, ())))))
+
+ +
+ +
+
+ +
+
+
+
In [9]:
+
+
+
joy.parser.text_to_expression('[1 2 3] 4 5')  # Three items, the first is a list with three items
+
+ +
+
+
+ +
+
+ + +
+ +
Out[9]:
+ + + + +
+
((1, (2, (3, ()))), (4, (5, ())))
+
+ +
+ +
+
+ +
+
+
+
In [10]:
+
+
+
joy.parser.text_to_expression('1 23 ["four" [-5.0] cons] 8888')  # A mixed bag. cons is
+                                                                 # a Symbol, no lookup at
+                                                                 # parse-time.  Haiku docs.
+
+ +
+
+
+ +
+
+ + +
+ +
Out[10]:
+ + + + +
+
(1, (23, (('four', ((-5.0, ()), (cons, ()))), (8888, ()))))
+
+ +
+ +
+
+ +
+
+
+
In [11]:
+
+
+
joy.parser.text_to_expression('[][][][][]')  # Five empty lists.
+
+ +
+
+
+ +
+
+ + +
+ +
Out[11]:
+ + + + +
+
((), ((), ((), ((), ((), ())))))
+
+ +
+ +
+
+ +
+
+
+
In [12]:
+
+
+
joy.parser.text_to_expression('[[[[[]]]]]')  # Five nested lists.
+
+ +
+
+
+ +
+
+ + +
+ +
Out[12]:
+ + + + +
+
((((((), ()), ()), ()), ()), ())
+
+ +
+ +
+
+ +
+
+
+
+
+

Library

The Joy library of functions (aka commands, or "words" after Forth usage) encapsulates all the actual functionality (no pun intended) of the Joy system. There are simple functions such as addition add (or +, the library module supports aliases), and combinators which provide control-flow and higher-order operations.

+ +
+
+
+
+
+
In [13]:
+
+
+
import joy.library
+
+print ' '.join(sorted(joy.library.initialize()))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
!= % & * *fraction *fraction0 + ++ - -- / < << <= <> = > >= >> ? ^ add anamorphism and app1 app2 app3 average b binary branch choice clear cleave concat cons dinfrirst dip dipd dipdd disenstacken div down_to_zero dudipd dup dupd dupdip enstacken eq first flatten floordiv gcd ge genrec getitem gt help i id ifte infra le least_fraction loop lshift lt map min mod modulus mul ne neg not nullary or over pam parse pm pop popd popdd popop pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup rshift run second select sharing shunt size sqr sqrt stack step sub succ sum swaack swap swoncat swons ternary third times truediv truthy tuck unary uncons unit unquoted unstack void warranty while words x xor zip •
+
+
+
+ +
+
+ +
+
+
+
+
+

Many of the functions are defined in Python, like dip:

+ +
+
+
+
+
+
In [14]:
+
+
+
print inspect.getsource(joy.library.dip)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
def dip(stack, expression, dictionary):
+  (quote, (x, stack)) = stack
+  expression = x, expression
+  return stack, pushback(quote, expression), dictionary
+
+
+
+
+ +
+
+ +
+
+
+
+
+

Some functions are defined in equations in terms of other functions. When the interpreter executes a definition function that function just pushes its body expression onto the pending expression (the continuation) and returns control to the interpreter.

+ +
+
+
+
+
+
In [15]:
+
+
+
print joy.library.definitions
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
second == rest first
+third == rest rest first
+product == 1 swap [*] step
+swons == swap cons
+swoncat == swap concat
+flatten == [] swap [concat] step
+unit == [] cons
+quoted == [unit] dip
+unquoted == [i] dip
+enstacken == stack [clear] dip
+disenstacken == ? [uncons ?] loop pop
+? == dup truthy
+dinfrirst == dip infra first
+nullary == [stack] dinfrirst
+unary == [stack [pop] dip] dinfrirst
+binary == [stack [popop] dip] dinfrirst
+ternary == [stack [popop pop] dip] dinfrirst
+pam == [i] map
+run == [] swap infra
+sqr == dup mul
+size == 0 swap [pop ++] step
+cleave == [i] app2 [popd] dip
+average == [sum 1.0 *] [size] cleave /
+gcd == 1 [tuck modulus dup 0 >] loop pop
+least_fraction == dup [gcd] infra [div] concat map
+*fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
+*fraction0 == concat [[swap] dip * [*] dip] infra
+down_to_zero == [0 >] [dup --] while
+range_to_zero == unit [down_to_zero] infra
+anamorphism == [pop []] swap [dip swons] genrec
+range == [0 <=] [1 - dup] anamorphism
+while == swap [nullary] cons dup dipd concat loop
+dudipd == dup dipd
+primrec == [i] genrec
+
+
+
+
+ +
+
+ +
+
+
+
+
+

Currently, there's no function to add new definitions to the dictionary from "within" Joy code itself. Adding new definitions remains a meta-interpreter action. You have to do it yourself, in Python, and wash your hands afterward.

+

It would be simple enough to define one, but it would open the door to name binding and break the idea that all state is captured in the stack and expression. There's an implicit standard dictionary that defines the actual semantics of the syntactic stack and expression datastructures (which only contain symbols, not the actual functions. Pickle some and see for yourself.)

+

"There should be only one."

Which brings me to talking about one of my hopes and dreams for this notation: "There should be only one." What I mean is that there should be one universal standard dictionary of commands, and all bespoke work done in a UI for purposes takes place by direct interaction and macros. There would be a Grand Refactoring biannually (two years, not six months, that's semi-annually) where any new definitions factored out of the usage and macros of the previous time, along with new algorithms and such, were entered into the dictionary and posted to e.g. IPFS.

+

Code should not burgeon wildly, as it does today. The variety of code should map more-or-less to the well-factored variety of human computably-solvable problems. There shouldn't be dozens of chat apps, JS frameworks, programming languages. It's a waste of time, a fractal "thundering herd" attack on human mentality.

+

Literary Code Library

If you read over the other notebooks you'll see that developing code in Joy is a lot like doing simple mathematics, and the descriptions of the code resemble math papers. The code also works the first time, no bugs. If you have any experience programming at all, you are probably skeptical, as I was, but it seems to work: deriving code mathematically seems to lead to fewer errors.

+

But my point now is that this great ratio of textual explanation to wind up with code that consists of a few equations and could fit on an index card is highly desirable. Less code has fewer errors. The structure of Joy engenders a kind of thinking that seems to be very effective for developing structured processes.

+

There seems to be an elegance and power to the notation.

+ +
+
+
+
+
+
In [ ]:
+
+
+
  
+
+ +
+
+
+ +
+
+
+ + + + + + diff --git a/docs/0._This_Implementation_of_Joy_in_Python.ipynb b/docs/0._This_Implementation_of_Joy_in_Python.ipynb new file mode 100644 index 0000000..8b3ce7e --- /dev/null +++ b/docs/0._This_Implementation_of_Joy_in_Python.ipynb @@ -0,0 +1,651 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Joypy\n", + "\n", + "## Joy in Python\n", + "\n", + "This implementation is meant as a tool for exploring the programming model and method of Joy. Python seems like a great implementation language for Joy for several reasons.\n", + "\n", + "We can lean on the Python immutable types for our basic semantics and types: ints, floats, strings, and tuples, which enforces functional purity. We get garbage collection for free. Compilation via Cython. Glue language with loads of libraries." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### [Read-Eval-Print Loop (REPL)](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)\n", + "The main way to interact with the Joy interpreter is through a simple [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) that you start by running the package:\n", + "\n", + " $ python -m joy\n", + " Joypy - Copyright © 2017 Simon Forman\n", + " This program comes with ABSOLUTELY NO WARRANTY; for details type \"warranty\".\n", + " This is free software, and you are welcome to redistribute it\n", + " under certain conditions; type \"sharing\" for details.\n", + " Type \"words\" to see a list of all words, and \"[] help\" to print the\n", + " docs for a word.\n", + "\n", + "\n", + " <-top\n", + "\n", + " joy? _\n", + "\n", + "The `<-top` marker points to the top of the (initially empty) stack. You can enter Joy notation at the prompt and a [trace of evaluation](#The-TracePrinter.) will be printed followed by the stack and prompt again:\n", + "\n", + " joy? 23 sqr 18 +\n", + " . 23 sqr 18 +\n", + " 23 . sqr 18 +\n", + " 23 . dup mul 18 +\n", + " 23 23 . mul 18 +\n", + " 529 . 18 +\n", + " 529 18 . +\n", + " 547 . \n", + "\n", + " 547 <-top\n", + "\n", + " joy? \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stacks (aka list, quote, sequence, etc.)\n", + "\n", + "In Joy, in addition to the types Boolean, integer, float, and string, there is a single sequence type represented by enclosing a sequence of terms in brackets `[...]`. This sequence type is used to represent both the stack and the expression. It is a [cons list](https://en.wikipedia.org/wiki/Cons#Lists) made from Python tuples." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "§ Stack\n", + "\n", + "\n", + "When talking about Joy we use the terms \"stack\", \"list\", \"sequence\" and\n", + "\"aggregate\" to mean the same thing: a simple datatype that permits\n", + "certain operations such as iterating and pushing and popping values from\n", + "(at least) one end.\n", + "\n", + "We use the venerable two-tuple recursive form of sequences 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", + " ()\n", + " (1, ())\n", + " (2, (1, ()))\n", + " (3, (2, (1, ())))\n", + " ...\n", + "\n", + "And so on.\n", + "\n", + "\n", + "We have two very simple functions to build up a stack from a Python\n", + "iterable and also to iterate through a stack and yield its items\n", + "one-by-one in order, and two functions to generate string representations\n", + "of stacks:\n", + "\n", + " list_to_stack()\n", + "\n", + " iter_stack()\n", + "\n", + " expression_to_string() (prints left-to-right)\n", + "\n", + " stack_to_string() (prints right-to-left)\n", + "\n", + "\n", + "A word about the stack data structure.\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(stack):\n", + " head, tail = stack\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 de-structuring the\n", + "incoming argument 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" + ] + } + ], + "source": [ + "import inspect\n", + "import joy.utils.stack\n", + "\n", + "\n", + "print inspect.getdoc(joy.utils.stack)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The utility functions maintain order.\n", + "The 0th item in the list will be on the top of the stack and *vise versa*." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, (2, (3, ())))" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joy.utils.stack.list_to_stack([1, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(joy.utils.stack.iter_stack((1, (2, (3, ())))))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This requires reversing the sequence (or iterating backwards) otherwise:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, (2, (1, ())))\n", + "[3, 2, 1]\n" + ] + } + ], + "source": [ + "stack = ()\n", + "\n", + "for n in [1, 2, 3]:\n", + " stack = n, stack\n", + "\n", + "print stack\n", + "print list(joy.utils.stack.iter_stack(stack))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Purely Functional Datastructures.\n", + "Because Joy lists are made out of Python tuples they are immutable, so all Joy datastructures are *[purely functional](https://en.wikipedia.org/wiki/Purely_functional_data_structure)*." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The `joy()` function.\n", + "## An Interpreter\n", + "The `joy()` function is extrememly simple. It accepts a stack, an expression, and a dictionary, and it iterates through the expression putting values onto the stack and delegating execution to functions it looks up in the dictionary.\n", + "\n", + "Each function is passed the stack, expression, and dictionary and returns them. Whatever the function returns becomes the new stack, expression, and dictionary. (The dictionary is passed to enable e.g. writing words that let you enter new words into the dictionary at runtime, which nothing does yet and may be a bad idea, and the `help` command.)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "def joy(stack, expression, dictionary, viewer=None):\n", + " '''\n", + " Evaluate the Joy expression on the stack.\n", + " '''\n", + " while expression:\n", + "\n", + " if viewer: viewer(stack, expression)\n", + "\n", + " term, expression = expression\n", + " if isinstance(term, Symbol):\n", + " term = dictionary[term]\n", + " stack, expression, dictionary = term(stack, expression, dictionary)\n", + " else:\n", + " stack = term, stack\n", + "\n", + " if viewer: viewer(stack, expression)\n", + " return stack, expression, dictionary\n", + "\n" + ] + } + ], + "source": [ + "import joy.joy\n", + "\n", + "print inspect.getsource(joy.joy.joy)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### View function\n", + "The `joy()` function accepts a \"viewer\" function which it calls on each iteration passing the current stack and expression just before evaluation. This can be used for tracing, breakpoints, retrying after exceptions, or interrupting an evaluation and saving to disk or sending over the network to resume later. The stack and expression together contain all the state of the computation at each step." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The `TracePrinter`.\n", + "\n", + "A `viewer` records each step of the evaluation of a Joy program. The `TracePrinter` has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression (\"continuation\") on the right." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### [Continuation-Passing Style](https://en.wikipedia.org/wiki/Continuation-passing_style)\n", + "One day I thought, What happens if you rewrite Joy to use [CSP](https://en.wikipedia.org/wiki/Continuation-passing_style)? I made all the functions accept and return the expression as well as the stack and found that all the combinators could be rewritten to work by modifying the expression rather than making recursive calls to the `joy()` function." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Parser" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "§ Converting text to a joy expression.\n", + "\n", + "This module exports a single function:\n", + "\n", + " text_to_expression(text)\n", + "\n", + "As well as a single Symbol class and a single Exception type:\n", + "\n", + " ParseError\n", + "\n", + "When supplied with a string this function returns a Python datastructure\n", + "that represents the Joy datastructure described by the text expression.\n", + "Any unbalanced square brackets will raise a ParseError.\n" + ] + } + ], + "source": [ + "import joy.parser\n", + "\n", + "print inspect.getdoc(joy.parser)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The parser is extremely simple, the undocumented `re.Scanner` class does most of the tokenizing work and then you just build the tuple structure out of the tokens. There's no Abstract Syntax Tree or anything like that." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "def _parse(tokens):\n", + " '''\n", + " Return a stack/list expression of the tokens.\n", + " '''\n", + " frame = []\n", + " stack = []\n", + " for tok in tokens:\n", + " if tok == '[':\n", + " stack.append(frame)\n", + " frame = []\n", + " stack[-1].append(frame)\n", + " elif tok == ']':\n", + " try:\n", + " frame = stack.pop()\n", + " except IndexError:\n", + " raise ParseError('One or more extra closing brackets.')\n", + " frame[-1] = list_to_stack(frame[-1])\n", + " else:\n", + " frame.append(tok)\n", + " if stack:\n", + " raise ParseError('One or more unclosed brackets.')\n", + " return list_to_stack(frame)\n", + "\n" + ] + } + ], + "source": [ + "print inspect.getsource(joy.parser._parse)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "That's pretty much all there is to it." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, (2, (3, (4, (5, ())))))" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joy.parser.text_to_expression('1 2 3 4 5') # A simple sequence." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((1, (2, (3, ()))), (4, (5, ())))" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joy.parser.text_to_expression('[1 2 3] 4 5') # Three items, the first is a list with three items" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, (23, (('four', ((-5.0, ()), (cons, ()))), (8888, ()))))" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joy.parser.text_to_expression('1 23 [\"four\" [-5.0] cons] 8888') # A mixed bag. cons is\n", + " # a Symbol, no lookup at\n", + " # parse-time. Haiku docs." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((), ((), ((), ((), ((), ())))))" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joy.parser.text_to_expression('[][][][][]') # Five empty lists." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((((((), ()), ()), ()), ()), ())" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "joy.parser.text_to_expression('[[[[[]]]]]') # Five nested lists." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Library\n", + "The Joy library of functions (aka commands, or \"words\" after Forth usage) encapsulates all the actual functionality (no pun intended) of the Joy system. There are simple functions such as addition `add` (or `+`, the library module supports aliases), and combinators which provide control-flow and higher-order operations." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "!= % & * *fraction *fraction0 + ++ - -- / < << <= <> = > >= >> ? ^ add anamorphism and app1 app2 app3 average b binary branch choice clear cleave concat cons dinfrirst dip dipd dipdd disenstacken div down_to_zero dudipd dup dupd dupdip enstacken eq first flatten floordiv gcd ge genrec getitem gt help i id ifte infra le least_fraction loop lshift lt map min mod modulus mul ne neg not nullary or over pam parse pm pop popd popdd popop pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup rshift run second select sharing shunt size sqr sqrt stack step sub succ sum swaack swap swoncat swons ternary third times truediv truthy tuck unary uncons unit unquoted unstack void warranty while words x xor zip •\n" + ] + } + ], + "source": [ + "import joy.library\n", + "\n", + "print ' '.join(sorted(joy.library.initialize()))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Many of the functions are defined in Python, like `dip`:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "def dip(stack, expression, dictionary):\n", + " (quote, (x, stack)) = stack\n", + " expression = x, expression\n", + " return stack, pushback(quote, expression), dictionary\n", + "\n" + ] + } + ], + "source": [ + "print inspect.getsource(joy.library.dip)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Some functions are defined in equations in terms of other functions. When the interpreter executes a definition function that function just pushes its body expression onto the pending expression (the continuation) and returns control to the interpreter." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "second == rest first\n", + "third == rest rest first\n", + "product == 1 swap [*] step\n", + "swons == swap cons\n", + "swoncat == swap concat\n", + "flatten == [] swap [concat] step\n", + "unit == [] cons\n", + "quoted == [unit] dip\n", + "unquoted == [i] dip\n", + "enstacken == stack [clear] dip\n", + "disenstacken == ? [uncons ?] loop pop\n", + "? == dup truthy\n", + "dinfrirst == dip infra first\n", + "nullary == [stack] dinfrirst\n", + "unary == [stack [pop] dip] dinfrirst\n", + "binary == [stack [popop] dip] dinfrirst\n", + "ternary == [stack [popop pop] dip] dinfrirst\n", + "pam == [i] map\n", + "run == [] swap infra\n", + "sqr == dup mul\n", + "size == 0 swap [pop ++] step\n", + "cleave == [i] app2 [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", + "dudipd == dup dipd\n", + "primrec == [i] genrec\n", + "\n" + ] + } + ], + "source": [ + "print joy.library.definitions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Currently, there's no function to add new definitions to the dictionary from \"within\" Joy code itself. Adding new definitions remains a meta-interpreter action. You have to do it yourself, in Python, and wash your hands afterward.\n", + "\n", + "It would be simple enough to define one, but it would open the door to *name binding* and break the idea that all state is captured in the stack and expression. There's an implicit *standard dictionary* that defines the actual semantics of the syntactic stack and expression datastructures (which only contain symbols, not the actual functions. Pickle some and see for yourself.)\n", + "\n", + "#### \"There should be only one.\"\n", + "\n", + "Which brings me to talking about one of my hopes and dreams for this notation: \"There should be only one.\" What I mean is that there should be one universal standard dictionary of commands, and all bespoke work done in a UI for purposes takes place by direct interaction and macros. There would be a *Grand Refactoring* biannually (two years, not six months, that's semi-annually) where any new definitions factored out of the usage and macros of the previous time, along with new algorithms and such, were entered into the dictionary and posted to e.g. IPFS.\n", + "\n", + "Code should not burgeon wildly, as it does today. The variety of code should map more-or-less to the well-factored variety of human computably-solvable problems. There shouldn't be dozens of chat apps, JS frameworks, programming languages. It's a waste of time, a [fractal \"thundering herd\" attack](https://en.wikipedia.org/wiki/Thundering_herd_problem) on human mentality.\n", + "\n", + "#### Literary Code Library\n", + "\n", + "If you read over the other notebooks you'll see that developing code in Joy is a lot like doing simple mathematics, and the descriptions of the code resemble math papers. The code also works the first time, no bugs. If you have any experience programming at all, you are probably skeptical, as I was, but it seems to work: deriving code mathematically seems to lead to fewer errors.\n", + "\n", + "But my point now is that this great ratio of textual explanation to wind up with code that consists of a few equations and could fit on an index card is highly desirable. Less code has fewer errors. The structure of Joy engenders a kind of thinking that seems to be very effective for developing structured processes.\n", + "\n", + "There seems to be an elegance and power to the notation.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + " " + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/0._This_Implementation_of_Joy_in_Python.md b/docs/0._This_Implementation_of_Joy_in_Python.md new file mode 100644 index 0000000..1e0b8b9 --- /dev/null +++ b/docs/0._This_Implementation_of_Joy_in_Python.md @@ -0,0 +1,411 @@ + +# Joypy + +## Joy in Python + +This implementation is meant as a tool for exploring the programming model and method of Joy. Python seems like a great implementation language for Joy for several reasons. + +We can lean on the Python immutable types for our basic semantics and types: ints, floats, strings, and tuples, which enforces functional purity. We get garbage collection for free. Compilation via Cython. Glue language with loads of libraries. + +### [Read-Eval-Print Loop (REPL)](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) +The main way to interact with the Joy interpreter is through a simple [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) that you start by running the package: + + $ python -m joy + Joypy - Copyright © 2017 Simon Forman + This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty". + This is free software, and you are welcome to redistribute it + under certain conditions; type "sharing" for details. + Type "words" to see a list of all words, and "[] help" to print the + docs for a word. + + + <-top + + joy? _ + +The `<-top` marker points to the top of the (initially empty) stack. You can enter Joy notation at the prompt and a [trace of evaluation](#The-TracePrinter.) will be printed followed by the stack and prompt again: + + joy? 23 sqr 18 + + . 23 sqr 18 + + 23 . sqr 18 + + 23 . dup mul 18 + + 23 23 . mul 18 + + 529 . 18 + + 529 18 . + + 547 . + + 547 <-top + + joy? + + +# Stacks (aka list, quote, sequence, etc.) + +In Joy, in addition to the types Boolean, integer, float, and string, there is a single sequence type represented by enclosing a sequence of terms in brackets `[...]`. This sequence type is used to represent both the stack and the expression. It is a [cons list](https://en.wikipedia.org/wiki/Cons#Lists) made from Python tuples. + + +```python +import inspect +import joy.utils.stack + + +print inspect.getdoc(joy.utils.stack) +``` + + § Stack + + + When talking about Joy we use the terms "stack", "list", "sequence" and + "aggregate" to mean the same thing: a simple datatype that permits + certain operations such as iterating and pushing and popping values from + (at least) one end. + + We use the venerable two-tuple recursive form of sequences where the + empty tuple () is the empty stack and (head, rest) gives the recursive + form of a stack with one or more items on it. + + () + (1, ()) + (2, (1, ())) + (3, (2, (1, ()))) + ... + + And so on. + + + We have two very simple functions to build up a stack from a Python + iterable and also to iterate through a stack and yield its items + one-by-one in order, and two functions to generate string representations + of stacks: + + list_to_stack() + + iter_stack() + + expression_to_string() (prints left-to-right) + + stack_to_string() (prints right-to-left) + + + A word about the stack data structure. + + Python has very nice "tuple packing and unpacking" in its syntax which + means we can directly "unpack" the expected arguments to a Joy function. + + For example: + + def dup(stack): + head, tail = stack + return head, (head, tail) + + We replace the argument "stack" by the expected structure of the stack, + in this case "(head, tail)", and Python takes care of de-structuring the + incoming argument and assigning values to the names. Note that Python + syntax doesn't require parentheses around tuples used in expressions + where they would be redundant. + + +### The utility functions maintain order. +The 0th item in the list will be on the top of the stack and *vise versa*. + + +```python +joy.utils.stack.list_to_stack([1, 2, 3]) +``` + + + + + (1, (2, (3, ()))) + + + + +```python +list(joy.utils.stack.iter_stack((1, (2, (3, ()))))) +``` + + + + + [1, 2, 3] + + + +This requires reversing the sequence (or iterating backwards) otherwise: + + +```python +stack = () + +for n in [1, 2, 3]: + stack = n, stack + +print stack +print list(joy.utils.stack.iter_stack(stack)) +``` + + (3, (2, (1, ()))) + [3, 2, 1] + + +### Purely Functional Datastructures. +Because Joy lists are made out of Python tuples they are immutable, so all Joy datastructures are *[purely functional](https://en.wikipedia.org/wiki/Purely_functional_data_structure)*. + +# The `joy()` function. +## An Interpreter +The `joy()` function is extrememly simple. It accepts a stack, an expression, and a dictionary, and it iterates through the expression putting values onto the stack and delegating execution to functions it looks up in the dictionary. + +Each function is passed the stack, expression, and dictionary and returns them. Whatever the function returns becomes the new stack, expression, and dictionary. (The dictionary is passed to enable e.g. writing words that let you enter new words into the dictionary at runtime, which nothing does yet and may be a bad idea, and the `help` command.) + + +```python +import joy.joy + +print inspect.getsource(joy.joy.joy) +``` + + def joy(stack, expression, dictionary, viewer=None): + ''' + Evaluate the Joy expression on the stack. + ''' + while expression: + + if viewer: viewer(stack, expression) + + term, expression = expression + if isinstance(term, Symbol): + term = dictionary[term] + stack, expression, dictionary = term(stack, expression, dictionary) + else: + stack = term, stack + + if viewer: viewer(stack, expression) + return stack, expression, dictionary + + + +### View function +The `joy()` function accepts a "viewer" function which it calls on each iteration passing the current stack and expression just before evaluation. This can be used for tracing, breakpoints, retrying after exceptions, or interrupting an evaluation and saving to disk or sending over the network to resume later. The stack and expression together contain all the state of the computation at each step. + +### The `TracePrinter`. + +A `viewer` records each step of the evaluation of a Joy program. The `TracePrinter` has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression ("continuation") on the right. + +### [Continuation-Passing Style](https://en.wikipedia.org/wiki/Continuation-passing_style) +One day I thought, What happens if you rewrite Joy to use [CSP](https://en.wikipedia.org/wiki/Continuation-passing_style)? I made all the functions accept and return the expression as well as the stack and found that all the combinators could be rewritten to work by modifying the expression rather than making recursive calls to the `joy()` function. + +# Parser + + +```python +import joy.parser + +print inspect.getdoc(joy.parser) +``` + + § Converting text to a joy expression. + + This module exports a single function: + + text_to_expression(text) + + As well as a single Symbol class and a single Exception type: + + ParseError + + When supplied with a string this function returns a Python datastructure + that represents the Joy datastructure described by the text expression. + Any unbalanced square brackets will raise a ParseError. + + +The parser is extremely simple, the undocumented `re.Scanner` class does most of the tokenizing work and then you just build the tuple structure out of the tokens. There's no Abstract Syntax Tree or anything like that. + + +```python +print inspect.getsource(joy.parser._parse) +``` + + def _parse(tokens): + ''' + Return a stack/list expression of the tokens. + ''' + frame = [] + stack = [] + for tok in tokens: + if tok == '[': + stack.append(frame) + frame = [] + stack[-1].append(frame) + elif tok == ']': + try: + frame = stack.pop() + except IndexError: + raise ParseError('One or more extra closing brackets.') + frame[-1] = list_to_stack(frame[-1]) + else: + frame.append(tok) + if stack: + raise ParseError('One or more unclosed brackets.') + return list_to_stack(frame) + + + +That's pretty much all there is to it. + + +```python +joy.parser.text_to_expression('1 2 3 4 5') # A simple sequence. +``` + + + + + (1, (2, (3, (4, (5, ()))))) + + + + +```python +joy.parser.text_to_expression('[1 2 3] 4 5') # Three items, the first is a list with three items +``` + + + + + ((1, (2, (3, ()))), (4, (5, ()))) + + + + +```python +joy.parser.text_to_expression('1 23 ["four" [-5.0] cons] 8888') # A mixed bag. cons is + # a Symbol, no lookup at + # parse-time. Haiku docs. +``` + + + + + (1, (23, (('four', ((-5.0, ()), (cons, ()))), (8888, ())))) + + + + +```python +joy.parser.text_to_expression('[][][][][]') # Five empty lists. +``` + + + + + ((), ((), ((), ((), ((), ()))))) + + + + +```python +joy.parser.text_to_expression('[[[[[]]]]]') # Five nested lists. +``` + + + + + ((((((), ()), ()), ()), ()), ()) + + + +# Library +The Joy library of functions (aka commands, or "words" after Forth usage) encapsulates all the actual functionality (no pun intended) of the Joy system. There are simple functions such as addition `add` (or `+`, the library module supports aliases), and combinators which provide control-flow and higher-order operations. + + +```python +import joy.library + +print ' '.join(sorted(joy.library.initialize())) +``` + + != % & * *fraction *fraction0 + ++ - -- / < << <= <> = > >= >> ? ^ add anamorphism and app1 app2 app3 average b binary branch choice clear cleave concat cons dinfrirst dip dipd dipdd disenstacken div down_to_zero dudipd dup dupd dupdip enstacken eq first flatten floordiv gcd ge genrec getitem gt help i id ifte infra le least_fraction loop lshift lt map min mod modulus mul ne neg not nullary or over pam parse pm pop popd popdd popop pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup rshift run second select sharing shunt size sqr sqrt stack step sub succ sum swaack swap swoncat swons ternary third times truediv truthy tuck unary uncons unit unquoted unstack void warranty while words x xor zip • + + +Many of the functions are defined in Python, like `dip`: + + +```python +print inspect.getsource(joy.library.dip) +``` + + def dip(stack, expression, dictionary): + (quote, (x, stack)) = stack + expression = x, expression + return stack, pushback(quote, expression), dictionary + + + +Some functions are defined in equations in terms of other functions. When the interpreter executes a definition function that function just pushes its body expression onto the pending expression (the continuation) and returns control to the interpreter. + + +```python +print joy.library.definitions +``` + + second == rest first + third == rest rest first + product == 1 swap [*] step + swons == swap cons + swoncat == swap concat + flatten == [] swap [concat] step + unit == [] cons + quoted == [unit] dip + unquoted == [i] dip + enstacken == stack [clear] dip + disenstacken == ? [uncons ?] loop pop + ? == dup truthy + dinfrirst == dip infra first + nullary == [stack] dinfrirst + unary == [stack [pop] dip] dinfrirst + binary == [stack [popop] dip] dinfrirst + ternary == [stack [popop pop] dip] dinfrirst + pam == [i] map + run == [] swap infra + sqr == dup mul + size == 0 swap [pop ++] step + cleave == [i] app2 [popd] dip + average == [sum 1.0 *] [size] cleave / + gcd == 1 [tuck modulus dup 0 >] loop pop + least_fraction == dup [gcd] infra [div] concat map + *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons + *fraction0 == concat [[swap] dip * [*] dip] infra + down_to_zero == [0 >] [dup --] while + range_to_zero == unit [down_to_zero] infra + anamorphism == [pop []] swap [dip swons] genrec + range == [0 <=] [1 - dup] anamorphism + while == swap [nullary] cons dup dipd concat loop + dudipd == dup dipd + primrec == [i] genrec + + + +Currently, there's no function to add new definitions to the dictionary from "within" Joy code itself. Adding new definitions remains a meta-interpreter action. You have to do it yourself, in Python, and wash your hands afterward. + +It would be simple enough to define one, but it would open the door to *name binding* and break the idea that all state is captured in the stack and expression. There's an implicit *standard dictionary* that defines the actual semantics of the syntactic stack and expression datastructures (which only contain symbols, not the actual functions. Pickle some and see for yourself.) + +#### "There should be only one." + +Which brings me to talking about one of my hopes and dreams for this notation: "There should be only one." What I mean is that there should be one universal standard dictionary of commands, and all bespoke work done in a UI for purposes takes place by direct interaction and macros. There would be a *Grand Refactoring* biannually (two years, not six months, that's semi-annually) where any new definitions factored out of the usage and macros of the previous time, along with new algorithms and such, were entered into the dictionary and posted to e.g. IPFS. + +Code should not burgeon wildly, as it does today. The variety of code should map more-or-less to the well-factored variety of human computably-solvable problems. There shouldn't be dozens of chat apps, JS frameworks, programming languages. It's a waste of time, a [fractal "thundering herd" attack](https://en.wikipedia.org/wiki/Thundering_herd_problem) on human mentality. + +#### Literary Code Library + +If you read over the other notebooks you'll see that developing code in Joy is a lot like doing simple mathematics, and the descriptions of the code resemble math papers. The code also works the first time, no bugs. If you have any experience programming at all, you are probably skeptical, as I was, but it seems to work: deriving code mathematically seems to lead to fewer errors. + +But my point now is that this great ratio of textual explanation to wind up with code that consists of a few equations and could fit on an index card is highly desirable. Less code has fewer errors. The structure of Joy engenders a kind of thinking that seems to be very effective for developing structured processes. + +There seems to be an elegance and power to the notation. + + + +```python + +``` diff --git a/docs/0._This_Implementation_of_Joy_in_Python.rst b/docs/0._This_Implementation_of_Joy_in_Python.rst new file mode 100644 index 0000000..7515dcc --- /dev/null +++ b/docs/0._This_Implementation_of_Joy_in_Python.rst @@ -0,0 +1,535 @@ + +Joypy +===== + +Joy in Python +------------- + +This implementation is meant as a tool for exploring the programming +model and method of Joy. Python seems like a great implementation +language for Joy for several reasons. + +We can lean on the Python immutable types for our basic semantics and +types: ints, floats, strings, and tuples, which enforces functional +purity. We get garbage collection for free. Compilation via Cython. Glue +language with loads of libraries. + +`Read-Eval-Print Loop (REPL) `__ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The main way to interact with the Joy interpreter is through a simple +`REPL `__ +that you start by running the package: + +:: + + $ python -m joy + Joypy - Copyright © 2017 Simon Forman + This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty". + This is free software, and you are welcome to redistribute it + under certain conditions; type "sharing" for details. + Type "words" to see a list of all words, and "[] help" to print the + docs for a word. + + + <-top + + joy? _ + +The ``<-top`` marker points to the top of the (initially empty) stack. +You can enter Joy notation at the prompt and a `trace of +evaluation <#The-TracePrinter.>`__ will be printed followed by the stack +and prompt again: + +:: + + joy? 23 sqr 18 + + . 23 sqr 18 + + 23 . sqr 18 + + 23 . dup mul 18 + + 23 23 . mul 18 + + 529 . 18 + + 529 18 . + + 547 . + + 547 <-top + + joy? + +Stacks (aka list, quote, sequence, etc.) +======================================== + +In Joy, in addition to the types Boolean, integer, float, and string, +there is a single sequence type represented by enclosing a sequence of +terms in brackets ``[...]``. This sequence type is used to represent +both the stack and the expression. It is a `cons +list `__ made from Python +tuples. + +.. code:: ipython2 + + import inspect + import joy.utils.stack + + + print inspect.getdoc(joy.utils.stack) + + +.. parsed-literal:: + + § Stack + + + When talking about Joy we use the terms "stack", "list", "sequence" and + "aggregate" to mean the same thing: a simple datatype that permits + certain operations such as iterating and pushing and popping values from + (at least) one end. + + We use the venerable two-tuple recursive form of sequences where the + empty tuple () is the empty stack and (head, rest) gives the recursive + form of a stack with one or more items on it. + + () + (1, ()) + (2, (1, ())) + (3, (2, (1, ()))) + ... + + And so on. + + + We have two very simple functions to build up a stack from a Python + iterable and also to iterate through a stack and yield its items + one-by-one in order, and two functions to generate string representations + of stacks: + + list_to_stack() + + iter_stack() + + expression_to_string() (prints left-to-right) + + stack_to_string() (prints right-to-left) + + + A word about the stack data structure. + + Python has very nice "tuple packing and unpacking" in its syntax which + means we can directly "unpack" the expected arguments to a Joy function. + + For example: + + def dup(stack): + head, tail = stack + return head, (head, tail) + + We replace the argument "stack" by the expected structure of the stack, + in this case "(head, tail)", and Python takes care of de-structuring the + incoming argument and assigning values to the names. Note that Python + syntax doesn't require parentheses around tuples used in expressions + where they would be redundant. + + +The utility functions maintain order. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The 0th item in the list will be on the top of the stack and *vise +versa*. + +.. code:: ipython2 + + joy.utils.stack.list_to_stack([1, 2, 3]) + + + + +.. parsed-literal:: + + (1, (2, (3, ()))) + + + +.. code:: ipython2 + + list(joy.utils.stack.iter_stack((1, (2, (3, ()))))) + + + + +.. parsed-literal:: + + [1, 2, 3] + + + +This requires reversing the sequence (or iterating backwards) otherwise: + +.. code:: ipython2 + + stack = () + + for n in [1, 2, 3]: + stack = n, stack + + print stack + print list(joy.utils.stack.iter_stack(stack)) + + +.. parsed-literal:: + + (3, (2, (1, ()))) + [3, 2, 1] + + +Purely Functional Datastructures. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Because Joy lists are made out of Python tuples they are immutable, so +all Joy datastructures are *`purely +functional `__*. + +The ``joy()`` function. +======================= + +An Interpreter +-------------- + +The ``joy()`` function is extrememly simple. It accepts a stack, an +expression, and a dictionary, and it iterates through the expression +putting values onto the stack and delegating execution to functions it +looks up in the dictionary. + +Each function is passed the stack, expression, and dictionary and +returns them. Whatever the function returns becomes the new stack, +expression, and dictionary. (The dictionary is passed to enable e.g. +writing words that let you enter new words into the dictionary at +runtime, which nothing does yet and may be a bad idea, and the ``help`` +command.) + +.. code:: ipython2 + + import joy.joy + + print inspect.getsource(joy.joy.joy) + + +.. parsed-literal:: + + def joy(stack, expression, dictionary, viewer=None): + ''' + Evaluate the Joy expression on the stack. + ''' + while expression: + + if viewer: viewer(stack, expression) + + term, expression = expression + if isinstance(term, Symbol): + term = dictionary[term] + stack, expression, dictionary = term(stack, expression, dictionary) + else: + stack = term, stack + + if viewer: viewer(stack, expression) + return stack, expression, dictionary + + + +View function +~~~~~~~~~~~~~ + +The ``joy()`` function accepts a "viewer" function which it calls on +each iteration passing the current stack and expression just before +evaluation. This can be used for tracing, breakpoints, retrying after +exceptions, or interrupting an evaluation and saving to disk or sending +over the network to resume later. The stack and expression together +contain all the state of the computation at each step. + +The ``TracePrinter``. +~~~~~~~~~~~~~~~~~~~~~ + +A ``viewer`` records each step of the evaluation of a Joy program. The +``TracePrinter`` has a facility for printing out a trace of the +evaluation, one line per step. Each step is aligned to the current +interpreter position, signified by a period separating the stack on the +left from the pending expression ("continuation") on the right. + +`Continuation-Passing Style `__ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +One day I thought, What happens if you rewrite Joy to use +`CSP `__? I +made all the functions accept and return the expression as well as the +stack and found that all the combinators could be rewritten to work by +modifying the expression rather than making recursive calls to the +``joy()`` function. + +Parser +====== + +.. code:: ipython2 + + import joy.parser + + print inspect.getdoc(joy.parser) + + +.. parsed-literal:: + + § Converting text to a joy expression. + + This module exports a single function: + + text_to_expression(text) + + As well as a single Symbol class and a single Exception type: + + ParseError + + When supplied with a string this function returns a Python datastructure + that represents the Joy datastructure described by the text expression. + Any unbalanced square brackets will raise a ParseError. + + +The parser is extremely simple, the undocumented ``re.Scanner`` class +does most of the tokenizing work and then you just build the tuple +structure out of the tokens. There's no Abstract Syntax Tree or anything +like that. + +.. code:: ipython2 + + print inspect.getsource(joy.parser._parse) + + +.. parsed-literal:: + + def _parse(tokens): + ''' + Return a stack/list expression of the tokens. + ''' + frame = [] + stack = [] + for tok in tokens: + if tok == '[': + stack.append(frame) + frame = [] + stack[-1].append(frame) + elif tok == ']': + try: + frame = stack.pop() + except IndexError: + raise ParseError('One or more extra closing brackets.') + frame[-1] = list_to_stack(frame[-1]) + else: + frame.append(tok) + if stack: + raise ParseError('One or more unclosed brackets.') + return list_to_stack(frame) + + + +That's pretty much all there is to it. + +.. code:: ipython2 + + joy.parser.text_to_expression('1 2 3 4 5') # A simple sequence. + + + + +.. parsed-literal:: + + (1, (2, (3, (4, (5, ()))))) + + + +.. code:: ipython2 + + joy.parser.text_to_expression('[1 2 3] 4 5') # Three items, the first is a list with three items + + + + +.. parsed-literal:: + + ((1, (2, (3, ()))), (4, (5, ()))) + + + +.. code:: ipython2 + + joy.parser.text_to_expression('1 23 ["four" [-5.0] cons] 8888') # A mixed bag. cons is + # a Symbol, no lookup at + # parse-time. Haiku docs. + + + + +.. parsed-literal:: + + (1, (23, (('four', ((-5.0, ()), (cons, ()))), (8888, ())))) + + + +.. code:: ipython2 + + joy.parser.text_to_expression('[][][][][]') # Five empty lists. + + + + +.. parsed-literal:: + + ((), ((), ((), ((), ((), ()))))) + + + +.. code:: ipython2 + + joy.parser.text_to_expression('[[[[[]]]]]') # Five nested lists. + + + + +.. parsed-literal:: + + ((((((), ()), ()), ()), ()), ()) + + + +Library +======= + +The Joy library of functions (aka commands, or "words" after Forth +usage) encapsulates all the actual functionality (no pun intended) of +the Joy system. There are simple functions such as addition ``add`` (or +``+``, the library module supports aliases), and combinators which +provide control-flow and higher-order operations. + +.. code:: ipython2 + + import joy.library + + print ' '.join(sorted(joy.library.initialize())) + + +.. parsed-literal:: + + != % & * *fraction *fraction0 + ++ - -- / < << <= <> = > >= >> ? ^ add anamorphism and app1 app2 app3 average b binary branch choice clear cleave concat cons dinfrirst dip dipd dipdd disenstacken div down_to_zero dudipd dup dupd dupdip enstacken eq first flatten floordiv gcd ge genrec getitem gt help i id ifte infra le least_fraction loop lshift lt map min mod modulus mul ne neg not nullary or over pam parse pm pop popd popdd popop pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup rshift run second select sharing shunt size sqr sqrt stack step sub succ sum swaack swap swoncat swons ternary third times truediv truthy tuck unary uncons unit unquoted unstack void warranty while words x xor zip • + + +Many of the functions are defined in Python, like ``dip``: + +.. code:: ipython2 + + print inspect.getsource(joy.library.dip) + + +.. parsed-literal:: + + def dip(stack, expression, dictionary): + (quote, (x, stack)) = stack + expression = x, expression + return stack, pushback(quote, expression), dictionary + + + +Some functions are defined in equations in terms of other functions. +When the interpreter executes a definition function that function just +pushes its body expression onto the pending expression (the +continuation) and returns control to the interpreter. + +.. code:: ipython2 + + print joy.library.definitions + + +.. parsed-literal:: + + second == rest first + third == rest rest first + product == 1 swap [*] step + swons == swap cons + swoncat == swap concat + flatten == [] swap [concat] step + unit == [] cons + quoted == [unit] dip + unquoted == [i] dip + enstacken == stack [clear] dip + disenstacken == ? [uncons ?] loop pop + ? == dup truthy + dinfrirst == dip infra first + nullary == [stack] dinfrirst + unary == [stack [pop] dip] dinfrirst + binary == [stack [popop] dip] dinfrirst + ternary == [stack [popop pop] dip] dinfrirst + pam == [i] map + run == [] swap infra + sqr == dup mul + size == 0 swap [pop ++] step + cleave == [i] app2 [popd] dip + average == [sum 1.0 *] [size] cleave / + gcd == 1 [tuck modulus dup 0 >] loop pop + least_fraction == dup [gcd] infra [div] concat map + *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons + *fraction0 == concat [[swap] dip * [*] dip] infra + down_to_zero == [0 >] [dup --] while + range_to_zero == unit [down_to_zero] infra + anamorphism == [pop []] swap [dip swons] genrec + range == [0 <=] [1 - dup] anamorphism + while == swap [nullary] cons dup dipd concat loop + dudipd == dup dipd + primrec == [i] genrec + + + +Currently, there's no function to add new definitions to the dictionary +from "within" Joy code itself. Adding new definitions remains a +meta-interpreter action. You have to do it yourself, in Python, and wash +your hands afterward. + +It would be simple enough to define one, but it would open the door to +*name binding* and break the idea that all state is captured in the +stack and expression. There's an implicit *standard dictionary* that +defines the actual semantics of the syntactic stack and expression +datastructures (which only contain symbols, not the actual functions. +Pickle some and see for yourself.) + +"There should be only one." +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Which brings me to talking about one of my hopes and dreams for this +notation: "There should be only one." What I mean is that there should +be one universal standard dictionary of commands, and all bespoke work +done in a UI for purposes takes place by direct interaction and macros. +There would be a *Grand Refactoring* biannually (two years, not six +months, that's semi-annually) where any new definitions factored out of +the usage and macros of the previous time, along with new algorithms and +such, were entered into the dictionary and posted to e.g. IPFS. + +Code should not burgeon wildly, as it does today. The variety of code +should map more-or-less to the well-factored variety of human +computably-solvable problems. There shouldn't be dozens of chat apps, JS +frameworks, programming languages. It's a waste of time, a `fractal +"thundering herd" +attack `__ on +human mentality. + +Literary Code Library +^^^^^^^^^^^^^^^^^^^^^ + +If you read over the other notebooks you'll see that developing code in +Joy is a lot like doing simple mathematics, and the descriptions of the +code resemble math papers. The code also works the first time, no bugs. +If you have any experience programming at all, you are probably +skeptical, as I was, but it seems to work: deriving code mathematically +seems to lead to fewer errors. + +But my point now is that this great ratio of textual explanation to wind +up with code that consists of a few equations and could fit on an index +card is highly desirable. Less code has fewer errors. The structure of +Joy engenders a kind of thinking that seems to be very effective for +developing structured processes. + +There seems to be an elegance and power to the notation. + diff --git a/docs/1._Basic_Use_of_Joy_in_a_Notebook.html b/docs/1._Basic_Use_of_Joy_in_a_Notebook.html new file mode 100644 index 0000000..d974edc --- /dev/null +++ b/docs/1._Basic_Use_of_Joy_in_a_Notebook.html @@ -0,0 +1,12079 @@ + + + +1._Basic_Use_of_Joy_in_a_Notebook + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Preamble

First, import what we need.

+ +
+
+
+
+
+
In [1]:
+
+
+
from joy.joy import run
+from joy.library import initialize
+from joy.utils.stack import stack_to_string
+from joy.utils.pretty_print import TracePrinter
+
+ +
+
+
+ +
+
+
+
+
+

Define a dictionary, an initial stack, and two helper functions to run Joy code and print results for us.

+ +
+
+
+
+
+
In [2]:
+
+
+
D = initialize()
+S = ()
+
+
+def J(text):
+    print stack_to_string(run(text, S, D)[0])
+
+
+def V(text):
+    tp = TracePrinter()
+    run(text, S, D, tp.viewer)
+    tp.print_()
+
+ +
+
+
+ +
+
+
+
+
+

Run some simple programs

+
+
+
+
+
+
In [3]:
+
+
+
J('23 18 +')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
41
+
+
+
+ +
+
+ +
+
+
+
In [4]:
+
+
+
J('45 30 gcd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
15
+
+
+
+ +
+
+ +
+
+
+
+
+

With Viewer

A viewer records each step of the evaluation of a Joy program. The TracePrinter has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression ("continuation") on the right. I find these traces beautiful, like a kind of art.

+ +
+
+
+
+
+
In [5]:
+
+
+
V('23 18 +')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
      . 23 18 +
+   23 . 18 +
+23 18 . +
+   41 . 
+
+
+
+ +
+
+ +
+
+
+
In [6]:
+
+
+
V('45 30 gcd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                  . 45 30 gcd
+                               45 . 30 gcd
+                            45 30 . gcd
+                            45 30 . 1 [tuck modulus dup 0 >] loop pop
+                          45 30 1 . [tuck modulus dup 0 >] loop pop
+   45 30 1 [tuck modulus dup 0 >] . loop pop
+                            45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                         30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                            30 15 . dup 0 > [tuck modulus dup 0 >] loop pop
+                         30 15 15 . 0 > [tuck modulus dup 0 >] loop pop
+                       30 15 15 0 . > [tuck modulus dup 0 >] loop pop
+                       30 15 True . [tuck modulus dup 0 >] loop pop
+30 15 True [tuck modulus dup 0 >] . loop pop
+                            30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                         15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                             15 0 . dup 0 > [tuck modulus dup 0 >] loop pop
+                           15 0 0 . 0 > [tuck modulus dup 0 >] loop pop
+                         15 0 0 0 . > [tuck modulus dup 0 >] loop pop
+                       15 0 False . [tuck modulus dup 0 >] loop pop
+15 0 False [tuck modulus dup 0 >] . loop pop
+                             15 0 . pop
+                               15 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Here's a longer trace.

+ +
+
+
+
+
+
In [7]:
+
+
+
V('96 27 gcd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                  . 96 27 gcd
+                               96 . 27 gcd
+                            96 27 . gcd
+                            96 27 . 1 [tuck modulus dup 0 >] loop pop
+                          96 27 1 . [tuck modulus dup 0 >] loop pop
+   96 27 1 [tuck modulus dup 0 >] . loop pop
+                            96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                         27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                            27 15 . dup 0 > [tuck modulus dup 0 >] loop pop
+                         27 15 15 . 0 > [tuck modulus dup 0 >] loop pop
+                       27 15 15 0 . > [tuck modulus dup 0 >] loop pop
+                       27 15 True . [tuck modulus dup 0 >] loop pop
+27 15 True [tuck modulus dup 0 >] . loop pop
+                            27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                         15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                            15 12 . dup 0 > [tuck modulus dup 0 >] loop pop
+                         15 12 12 . 0 > [tuck modulus dup 0 >] loop pop
+                       15 12 12 0 . > [tuck modulus dup 0 >] loop pop
+                       15 12 True . [tuck modulus dup 0 >] loop pop
+15 12 True [tuck modulus dup 0 >] . loop pop
+                            15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                         12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                             12 3 . dup 0 > [tuck modulus dup 0 >] loop pop
+                           12 3 3 . 0 > [tuck modulus dup 0 >] loop pop
+                         12 3 3 0 . > [tuck modulus dup 0 >] loop pop
+                        12 3 True . [tuck modulus dup 0 >] loop pop
+ 12 3 True [tuck modulus dup 0 >] . loop pop
+                             12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                           3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
+                              3 0 . dup 0 > [tuck modulus dup 0 >] loop pop
+                            3 0 0 . 0 > [tuck modulus dup 0 >] loop pop
+                          3 0 0 0 . > [tuck modulus dup 0 >] loop pop
+                        3 0 False . [tuck modulus dup 0 >] loop pop
+ 3 0 False [tuck modulus dup 0 >] . loop pop
+                              3 0 . pop
+                                3 . 
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/1._Basic_Use_of_Joy_in_a_Notebook.ipynb b/docs/1._Basic_Use_of_Joy_in_a_Notebook.ipynb new file mode 100644 index 0000000..7c8f2a3 --- /dev/null +++ b/docs/1._Basic_Use_of_Joy_in_a_Notebook.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Preamble\n", + "\n", + "First, import what we need." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from joy.joy import run\n", + "from joy.library import initialize\n", + "from joy.utils.stack import stack_to_string\n", + "from joy.utils.pretty_print import TracePrinter" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Define a dictionary, an initial stack, and two helper functions to run Joy code and print results for us." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "D = initialize()\n", + "S = ()\n", + "\n", + "\n", + "def J(text):\n", + " print stack_to_string(run(text, S, D)[0])\n", + "\n", + "\n", + "def V(text):\n", + " tp = TracePrinter()\n", + " run(text, S, D, tp.viewer)\n", + " tp.print_()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run some simple programs" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "41\n" + ] + } + ], + "source": [ + "J('23 18 +')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n" + ] + } + ], + "source": [ + "J('45 30 gcd')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### With Viewer\n", + "\n", + "A `viewer` records each step of the evaluation of a Joy program. The `TracePrinter` has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression (\"continuation\") on the right. I find these traces beautiful, like a kind of art." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 23 18 +\n", + " 23 . 18 +\n", + "23 18 . +\n", + " 41 . \n" + ] + } + ], + "source": [ + "V('23 18 +')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 45 30 gcd\n", + " 45 . 30 gcd\n", + " 45 30 . gcd\n", + " 45 30 . 1 [tuck modulus dup 0 >] loop pop\n", + " 45 30 1 . [tuck modulus dup 0 >] loop pop\n", + " 45 30 1 [tuck modulus dup 0 >] . loop pop\n", + " 45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 30 15 . dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 30 15 15 . 0 > [tuck modulus dup 0 >] loop pop\n", + " 30 15 15 0 . > [tuck modulus dup 0 >] loop pop\n", + " 30 15 True . [tuck modulus dup 0 >] loop pop\n", + "30 15 True [tuck modulus dup 0 >] . loop pop\n", + " 30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 0 . dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 0 0 . 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 0 0 0 . > [tuck modulus dup 0 >] loop pop\n", + " 15 0 False . [tuck modulus dup 0 >] loop pop\n", + "15 0 False [tuck modulus dup 0 >] . loop pop\n", + " 15 0 . pop\n", + " 15 . \n" + ] + } + ], + "source": [ + "V('45 30 gcd')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here's a longer trace." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 96 27 gcd\n", + " 96 . 27 gcd\n", + " 96 27 . gcd\n", + " 96 27 . 1 [tuck modulus dup 0 >] loop pop\n", + " 96 27 1 . [tuck modulus dup 0 >] loop pop\n", + " 96 27 1 [tuck modulus dup 0 >] . loop pop\n", + " 96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 27 15 . dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 27 15 15 . 0 > [tuck modulus dup 0 >] loop pop\n", + " 27 15 15 0 . > [tuck modulus dup 0 >] loop pop\n", + " 27 15 True . [tuck modulus dup 0 >] loop pop\n", + "27 15 True [tuck modulus dup 0 >] . loop pop\n", + " 27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 12 . dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 12 12 . 0 > [tuck modulus dup 0 >] loop pop\n", + " 15 12 12 0 . > [tuck modulus dup 0 >] loop pop\n", + " 15 12 True . [tuck modulus dup 0 >] loop pop\n", + "15 12 True [tuck modulus dup 0 >] . loop pop\n", + " 15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 12 3 . dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 12 3 3 . 0 > [tuck modulus dup 0 >] loop pop\n", + " 12 3 3 0 . > [tuck modulus dup 0 >] loop pop\n", + " 12 3 True . [tuck modulus dup 0 >] loop pop\n", + " 12 3 True [tuck modulus dup 0 >] . loop pop\n", + " 12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 3 0 . dup 0 > [tuck modulus dup 0 >] loop pop\n", + " 3 0 0 . 0 > [tuck modulus dup 0 >] loop pop\n", + " 3 0 0 0 . > [tuck modulus dup 0 >] loop pop\n", + " 3 0 False . [tuck modulus dup 0 >] loop pop\n", + " 3 0 False [tuck modulus dup 0 >] . loop pop\n", + " 3 0 . pop\n", + " 3 . \n" + ] + } + ], + "source": [ + "V('96 27 gcd')" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/1._Basic_Use_of_Joy_in_a_Notebook.md b/docs/1._Basic_Use_of_Joy_in_a_Notebook.md new file mode 100644 index 0000000..a46ae6a --- /dev/null +++ b/docs/1._Basic_Use_of_Joy_in_a_Notebook.md @@ -0,0 +1,137 @@ + +### Preamble + +First, import what we need. + + +```python +from joy.joy import run +from joy.library import initialize +from joy.utils.stack import stack_to_string +from joy.utils.pretty_print import TracePrinter +``` + +Define a dictionary, an initial stack, and two helper functions to run Joy code and print results for us. + + +```python +D = initialize() +S = () + + +def J(text): + print stack_to_string(run(text, S, D)[0]) + + +def V(text): + tp = TracePrinter() + run(text, S, D, tp.viewer) + tp.print_() +``` + +### Run some simple programs + + +```python +J('23 18 +') +``` + + 41 + + + +```python +J('45 30 gcd') +``` + + 15 + + +### With Viewer + +A `viewer` records each step of the evaluation of a Joy program. The `TracePrinter` has a facility for printing out a trace of the evaluation, one line per step. Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression ("continuation") on the right. I find these traces beautiful, like a kind of art. + + +```python +V('23 18 +') +``` + + . 23 18 + + 23 . 18 + + 23 18 . + + 41 . + + + +```python +V('45 30 gcd') +``` + + . 45 30 gcd + 45 . 30 gcd + 45 30 . gcd + 45 30 . 1 [tuck modulus dup 0 >] loop pop + 45 30 1 . [tuck modulus dup 0 >] loop pop + 45 30 1 [tuck modulus dup 0 >] . loop pop + 45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 30 15 . dup 0 > [tuck modulus dup 0 >] loop pop + 30 15 15 . 0 > [tuck modulus dup 0 >] loop pop + 30 15 15 0 . > [tuck modulus dup 0 >] loop pop + 30 15 True . [tuck modulus dup 0 >] loop pop + 30 15 True [tuck modulus dup 0 >] . loop pop + 30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 0 . dup 0 > [tuck modulus dup 0 >] loop pop + 15 0 0 . 0 > [tuck modulus dup 0 >] loop pop + 15 0 0 0 . > [tuck modulus dup 0 >] loop pop + 15 0 False . [tuck modulus dup 0 >] loop pop + 15 0 False [tuck modulus dup 0 >] . loop pop + 15 0 . pop + 15 . + + +Here's a longer trace. + + +```python +V('96 27 gcd') +``` + + . 96 27 gcd + 96 . 27 gcd + 96 27 . gcd + 96 27 . 1 [tuck modulus dup 0 >] loop pop + 96 27 1 . [tuck modulus dup 0 >] loop pop + 96 27 1 [tuck modulus dup 0 >] . loop pop + 96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 27 15 . dup 0 > [tuck modulus dup 0 >] loop pop + 27 15 15 . 0 > [tuck modulus dup 0 >] loop pop + 27 15 15 0 . > [tuck modulus dup 0 >] loop pop + 27 15 True . [tuck modulus dup 0 >] loop pop + 27 15 True [tuck modulus dup 0 >] . loop pop + 27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 12 . dup 0 > [tuck modulus dup 0 >] loop pop + 15 12 12 . 0 > [tuck modulus dup 0 >] loop pop + 15 12 12 0 . > [tuck modulus dup 0 >] loop pop + 15 12 True . [tuck modulus dup 0 >] loop pop + 15 12 True [tuck modulus dup 0 >] . loop pop + 15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 12 3 . dup 0 > [tuck modulus dup 0 >] loop pop + 12 3 3 . 0 > [tuck modulus dup 0 >] loop pop + 12 3 3 0 . > [tuck modulus dup 0 >] loop pop + 12 3 True . [tuck modulus dup 0 >] loop pop + 12 3 True [tuck modulus dup 0 >] . loop pop + 12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 3 0 . dup 0 > [tuck modulus dup 0 >] loop pop + 3 0 0 . 0 > [tuck modulus dup 0 >] loop pop + 3 0 0 0 . > [tuck modulus dup 0 >] loop pop + 3 0 False . [tuck modulus dup 0 >] loop pop + 3 0 False [tuck modulus dup 0 >] . loop pop + 3 0 . pop + 3 . + diff --git a/docs/1._Basic_Use_of_Joy_in_a_Notebook.rst b/docs/1._Basic_Use_of_Joy_in_a_Notebook.rst new file mode 100644 index 0000000..15f23e3 --- /dev/null +++ b/docs/1._Basic_Use_of_Joy_in_a_Notebook.rst @@ -0,0 +1,154 @@ + +Preamble +~~~~~~~~ + +First, import what we need. + +.. code:: ipython2 + + from joy.joy import run + from joy.library import initialize + from joy.utils.stack import stack_to_string + from joy.utils.pretty_print import TracePrinter + +Define a dictionary, an initial stack, and two helper functions to run +Joy code and print results for us. + +.. code:: ipython2 + + D = initialize() + S = () + + + def J(text): + print stack_to_string(run(text, S, D)[0]) + + + def V(text): + tp = TracePrinter() + run(text, S, D, tp.viewer) + tp.print_() + +Run some simple programs +~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 18 +') + + +.. parsed-literal:: + + 41 + + +.. code:: ipython2 + + J('45 30 gcd') + + +.. parsed-literal:: + + 15 + + +With Viewer +~~~~~~~~~~~ + +A ``viewer`` records each step of the evaluation of a Joy program. The +``TracePrinter`` has a facility for printing out a trace of the +evaluation, one line per step. Each step is aligned to the current +interpreter position, signified by a period separating the stack on the +left from the pending expression ("continuation") on the right. I find +these traces beautiful, like a kind of art. + +.. code:: ipython2 + + V('23 18 +') + + +.. parsed-literal:: + + . 23 18 + + 23 . 18 + + 23 18 . + + 41 . + + +.. code:: ipython2 + + V('45 30 gcd') + + +.. parsed-literal:: + + . 45 30 gcd + 45 . 30 gcd + 45 30 . gcd + 45 30 . 1 [tuck modulus dup 0 >] loop pop + 45 30 1 . [tuck modulus dup 0 >] loop pop + 45 30 1 [tuck modulus dup 0 >] . loop pop + 45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 30 15 . dup 0 > [tuck modulus dup 0 >] loop pop + 30 15 15 . 0 > [tuck modulus dup 0 >] loop pop + 30 15 15 0 . > [tuck modulus dup 0 >] loop pop + 30 15 True . [tuck modulus dup 0 >] loop pop + 30 15 True [tuck modulus dup 0 >] . loop pop + 30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 0 . dup 0 > [tuck modulus dup 0 >] loop pop + 15 0 0 . 0 > [tuck modulus dup 0 >] loop pop + 15 0 0 0 . > [tuck modulus dup 0 >] loop pop + 15 0 False . [tuck modulus dup 0 >] loop pop + 15 0 False [tuck modulus dup 0 >] . loop pop + 15 0 . pop + 15 . + + +Here's a longer trace. + +.. code:: ipython2 + + V('96 27 gcd') + + +.. parsed-literal:: + + . 96 27 gcd + 96 . 27 gcd + 96 27 . gcd + 96 27 . 1 [tuck modulus dup 0 >] loop pop + 96 27 1 . [tuck modulus dup 0 >] loop pop + 96 27 1 [tuck modulus dup 0 >] . loop pop + 96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 27 15 . dup 0 > [tuck modulus dup 0 >] loop pop + 27 15 15 . 0 > [tuck modulus dup 0 >] loop pop + 27 15 15 0 . > [tuck modulus dup 0 >] loop pop + 27 15 True . [tuck modulus dup 0 >] loop pop + 27 15 True [tuck modulus dup 0 >] . loop pop + 27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 15 12 . dup 0 > [tuck modulus dup 0 >] loop pop + 15 12 12 . 0 > [tuck modulus dup 0 >] loop pop + 15 12 12 0 . > [tuck modulus dup 0 >] loop pop + 15 12 True . [tuck modulus dup 0 >] loop pop + 15 12 True [tuck modulus dup 0 >] . loop pop + 15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 12 3 . dup 0 > [tuck modulus dup 0 >] loop pop + 12 3 3 . 0 > [tuck modulus dup 0 >] loop pop + 12 3 3 0 . > [tuck modulus dup 0 >] loop pop + 12 3 True . [tuck modulus dup 0 >] loop pop + 12 3 True [tuck modulus dup 0 >] . loop pop + 12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop + 3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop + 3 0 . dup 0 > [tuck modulus dup 0 >] loop pop + 3 0 0 . 0 > [tuck modulus dup 0 >] loop pop + 3 0 0 0 . > [tuck modulus dup 0 >] loop pop + 3 0 False . [tuck modulus dup 0 >] loop pop + 3 0 False [tuck modulus dup 0 >] . loop pop + 3 0 . pop + 3 . + diff --git a/docs/2._Library_Examples.html b/docs/2._Library_Examples.html new file mode 100644 index 0000000..62c49ae --- /dev/null +++ b/docs/2._Library_Examples.html @@ -0,0 +1,16523 @@ + + + +2._Library_Examples + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Examples (and some documentation) for the Words in the Library

+
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import J, V
+
+ +
+
+
+ +
+
+
+
+
+

Stack Chatter

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.)

+ +
+
+
+
+
+
+
+

clear

+
+
+
+
+
+
In [2]:
+
+
+
J('1 2 3 clear')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
+
+
+
+ +
+
+ +
+
+
+
+
+

dup dupd

+
+
+
+
+
+
In [3]:
+
+
+
J('1 2 3 dup')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 3
+
+
+
+ +
+
+ +
+
+
+
In [4]:
+
+
+
J('1 2 3 dupd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 2 3
+
+
+
+ +
+
+ +
+
+
+
+
+

enstacken disenstacken stack unstack

(I may have these paired up wrong. I.e. disenstacken should be unstack and vice versa.)

+ +
+
+
+
+
+
In [5]:
+
+
+
J('1 2 3 enstacken') # Replace the stack with a quote of itself.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[3 2 1]
+
+
+
+ +
+
+ +
+
+
+
In [6]:
+
+
+
J('4 5 6 [3 2 1] disenstacken')  # Unpack a list onto the stack.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4 5 6 3 2 1
+
+
+
+ +
+
+ +
+
+
+
In [7]:
+
+
+
J('1 2 3 stack')  # Get the stack on the stack.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 [3 2 1]
+
+
+
+ +
+
+ +
+
+
+
In [8]:
+
+
+
J('1 2 3 [4 5 6] unstack')  # Replace the stack with the list on top.
+                            # The items appear reversed but they are not,
+                            # 4 is on the top of both the list and the stack.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6 5 4
+
+
+
+ +
+
+ +
+
+
+
+
+

pop popd popop

+
+
+
+
+
+
In [9]:
+
+
+
J('1 2 3 pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2
+
+
+
+ +
+
+ +
+
+
+
In [10]:
+
+
+
J('1 2 3 popd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 3
+
+
+
+ +
+
+ +
+
+
+
In [11]:
+
+
+
J('1 2 3 popop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1
+
+
+
+ +
+
+ +
+
+
+
+
+

roll< rolldown roll> rollup

The "down" and "up" refer to the movement of two of the top three items (displacing the third.)

+ +
+
+
+
+
+
In [12]:
+
+
+
J('1 2 3 roll<')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2 3 1
+
+
+
+ +
+
+ +
+
+
+
In [13]:
+
+
+
J('1 2 3 roll>')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 1 2
+
+
+
+ +
+
+ +
+
+
+
+
+

swap

+
+
+
+
+
+
In [14]:
+
+
+
J('1 2 3 swap')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 3 2
+
+
+
+ +
+
+ +
+
+
+
+
+

tuck over

+
+
+
+
+
+
In [15]:
+
+
+
J('1 2 3 tuck')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 3 2 3
+
+
+
+ +
+
+ +
+
+
+
In [16]:
+
+
+
J('1 2 3 over')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 2
+
+
+
+ +
+
+ +
+
+
+
+
+

unit quoted unquoted

+
+
+
+
+
+
In [17]:
+
+
+
J('1 2 3 unit')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 [3]
+
+
+
+ +
+
+ +
+
+
+
In [18]:
+
+
+
J('1 2 3 quoted')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 [2] 3
+
+
+
+ +
+
+ +
+
+
+
In [19]:
+
+
+
J('1 [2] 3 unquoted')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3
+
+
+
+ +
+
+ +
+
+
+
In [20]:
+
+
+
V('1 [dup] 3 unquoted')  # Unquoting evaluates.  Be aware.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
              . 1 [dup] 3 unquoted
+            1 . [dup] 3 unquoted
+      1 [dup] . 3 unquoted
+    1 [dup] 3 . unquoted
+    1 [dup] 3 . [i] dip
+1 [dup] 3 [i] . dip
+      1 [dup] . i 3
+            1 . dup 3
+          1 1 . 3
+        1 1 3 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

List words

+
+
+
+
+
+
+
+

concat swoncat shunt

+
+
+
+
+
+
In [21]:
+
+
+
J('[1 2 3] [4 5 6] concat')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 2 3 4 5 6]
+
+
+
+ +
+
+ +
+
+
+
In [22]:
+
+
+
J('[1 2 3] [4 5 6] swoncat')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[4 5 6 1 2 3]
+
+
+
+ +
+
+ +
+
+
+
In [23]:
+
+
+
J('[1 2 3] [4 5 6] shunt')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[6 5 4 1 2 3]
+
+
+
+ +
+
+ +
+
+
+
+
+

cons swons uncons

+
+
+
+
+
+
In [24]:
+
+
+
J('1 [2 3] cons')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 2 3]
+
+
+
+ +
+
+ +
+
+
+
In [25]:
+
+
+
J('[2 3] 1 swons')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 2 3]
+
+
+
+ +
+
+ +
+
+
+
In [26]:
+
+
+
J('[1 2 3] uncons')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 [2 3]
+
+
+
+ +
+
+ +
+
+
+
+
+

first second third rest

+
+
+
+
+
+
In [27]:
+
+
+
J('[1 2 3 4] first')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1
+
+
+
+ +
+
+ +
+
+
+
In [28]:
+
+
+
J('[1 2 3 4] second')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [29]:
+
+
+
J('[1 2 3 4] third')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3
+
+
+
+ +
+
+ +
+
+
+
In [30]:
+
+
+
J('[1 2 3 4] rest')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[2 3 4]
+
+
+
+ +
+
+ +
+
+
+
+
+

flatten

+
+
+
+
+
+
In [31]:
+
+
+
J('[[1] [2 [3] 4] [5 6]] flatten')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 2 [3] 4 5 6]
+
+
+
+ +
+
+ +
+
+
+
+
+

getitem at of drop take

at and getitem are the same function. of == swap at

+ +
+
+
+
+
+
In [32]:
+
+
+
J('[10 11 12 13 14] 2 getitem')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
12
+
+
+
+ +
+
+ +
+
+
+
In [33]:
+
+
+
J('[1 2 3 4] 0 at')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1
+
+
+
+ +
+
+ +
+
+
+
In [34]:
+
+
+
J('2 [1 2 3 4] of')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3
+
+
+
+ +
+
+ +
+
+
+
In [35]:
+
+
+
J('[1 2 3 4] 2 drop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[3 4]
+
+
+
+ +
+
+ +
+
+
+
In [36]:
+
+
+
J('[1 2 3 4] 2 take')  # reverses the order
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[2 1]
+
+
+
+ +
+
+ +
+
+
+
+
+

reverse could be defines as reverse == dup size take

+ +
+
+
+
+
+
+
+

remove

+
+
+
+
+
+
In [37]:
+
+
+
J('[1 2 3 1 4] 1 remove')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[2 3 1 4]
+
+
+
+ +
+
+ +
+
+
+
+
+

reverse

+
+
+
+
+
+
In [38]:
+
+
+
J('[1 2 3 4] reverse')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[4 3 2 1]
+
+
+
+ +
+
+ +
+
+
+
+
+

size

+
+
+
+
+
+
In [39]:
+
+
+
J('[1 1 1 1] size')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4
+
+
+
+ +
+
+ +
+
+
+
+
+

swaack

"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.

+ +
+
+
+
+
+
In [40]:
+
+
+
J('1 2 3 [4 5 6] swaack')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6 5 4 [3 2 1]
+
+
+
+ +
+
+ +
+
+
+
+
+

choice select

+
+
+
+
+
+
In [41]:
+
+
+
J('23 9 1 choice')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9
+
+
+
+ +
+
+ +
+
+
+
In [42]:
+
+
+
J('23 9 0 choice')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
23
+
+
+
+ +
+
+ +
+
+
+
In [43]:
+
+
+
J('[23 9 7] 1 select')  # select is basically getitem, should retire it?
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9
+
+
+
+ +
+
+ +
+
+
+
In [44]:
+
+
+
J('[23 9 7] 0 select')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
23
+
+
+
+ +
+
+ +
+
+
+
+
+

zip

+
+
+
+
+
+
In [45]:
+
+
+
J('[1 2 3] [6 5 4] zip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[6 1] [5 2] [4 3]]
+
+
+
+ +
+
+ +
+
+
+
In [46]:
+
+
+
J('[1 2 3] [6 5 4] zip [sum] map')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[7 7 7]
+
+
+
+ +
+
+ +
+
+
+
+
+

Math words

+
+
+
+
+
+
+
+

+ add

+
+
+
+
+
+
In [47]:
+
+
+
J('23 9 +')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
32
+
+
+
+ +
+
+ +
+
+
+
+
+

- sub

+
+
+
+
+
+
In [48]:
+
+
+
J('23 9 -')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
14
+
+
+
+ +
+
+ +
+
+
+
+
+

* mul

+
+
+
+
+
+
In [49]:
+
+
+
J('23 9 *')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
207
+
+
+
+ +
+
+ +
+
+
+
+
+

/ div floordiv truediv

+
+
+
+
+
+
In [50]:
+
+
+
J('23 9 /')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2.5555555555555554
+
+
+
+ +
+
+ +
+
+
+
In [51]:
+
+
+
J('23 -9 truediv')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
-2.5555555555555554
+
+
+
+ +
+
+ +
+
+
+
In [52]:
+
+
+
J('23 9 div')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [53]:
+
+
+
J('23 9 floordiv')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [54]:
+
+
+
J('23 -9 div')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
-3
+
+
+
+ +
+
+ +
+
+
+
In [55]:
+
+
+
J('23 -9 floordiv')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
-3
+
+
+
+ +
+
+ +
+
+
+
+
+

% mod modulus rem remainder

+
+
+
+
+
+
In [56]:
+
+
+
J('23 9 %')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
5
+
+
+
+ +
+
+ +
+
+
+
+
+

neg

+
+
+
+
+
+
In [57]:
+
+
+
J('23 neg -5 neg')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
-23 5
+
+
+
+ +
+
+ +
+
+
+
+
+

pow

+
+
+
+
+
+
In [58]:
+
+
+
J('2 10 pow')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1024
+
+
+
+ +
+
+ +
+
+
+
+
+

sqr sqrt

+
+
+
+
+
+
In [59]:
+
+
+
J('23 sqr')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
529
+
+
+
+ +
+
+ +
+
+
+
In [60]:
+
+
+
J('23 sqrt')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4.795831523312719
+
+
+
+ +
+
+ +
+
+
+
+
+

++ succ -- pred

+
+
+
+
+
+
In [61]:
+
+
+
J('1 ++')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [62]:
+
+
+
J('1 --')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0
+
+
+
+ +
+
+ +
+
+
+
+
+

<< lshift >> rshift

+
+
+
+
+
+
In [63]:
+
+
+
J('8 1 <<')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
16
+
+
+
+ +
+
+ +
+
+
+
In [64]:
+
+
+
J('8 1 >>')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4
+
+
+
+ +
+
+ +
+
+
+
+
+

average

+
+
+
+
+
+
In [65]:
+
+
+
J('[1 2 3 5] average')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2.75
+
+
+
+ +
+
+ +
+
+
+
+
+

range range_to_zero down_to_zero

+
+
+
+
+
+
In [66]:
+
+
+
J('5 range')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[4 3 2 1 0]
+
+
+
+ +
+
+ +
+
+
+
In [67]:
+
+
+
J('5 range_to_zero')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0 1 2 3 4 5]
+
+
+
+ +
+
+ +
+
+
+
In [68]:
+
+
+
J('5 down_to_zero')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
5 4 3 2 1 0
+
+
+
+ +
+
+ +
+
+
+
+
+

product

+
+
+
+
+
+
In [69]:
+
+
+
J('[1 2 3 5] product')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
30
+
+
+
+ +
+
+ +
+
+
+
+
+

sum

+
+
+
+
+
+
In [70]:
+
+
+
J('[1 2 3 5] sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
11
+
+
+
+ +
+
+ +
+
+
+
+
+

min

+
+
+
+
+
+
In [71]:
+
+
+
J('[1 2 3 5] min')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1
+
+
+
+ +
+
+ +
+
+
+
+
+

gcd

+
+
+
+
+
+
In [72]:
+
+
+
J('45 30 gcd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
15
+
+
+
+ +
+
+ +
+
+
+
+
+

least_fraction

If we represent fractions as a quoted pair of integers [q d] this word reduces them to their ... least common factors or whatever.

+ +
+
+
+
+
+
In [73]:
+
+
+
J('[45 30] least_fraction')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[3 2]
+
+
+
+ +
+
+ +
+
+
+
In [74]:
+
+
+
J('[23 12] least_fraction')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[23 12]
+
+
+
+ +
+
+ +
+
+
+
+
+

Logic and Comparison

+
+
+
+
+
+
+
+

? truthy

Get the Boolean value of the item on the top of the stack.

+ +
+
+
+
+
+
In [75]:
+
+
+
J('23 truthy')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
True
+
+
+
+ +
+
+ +
+
+
+
In [76]:
+
+
+
J('[] truthy')  # Python semantics.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
False
+
+
+
+ +
+
+ +
+
+
+
In [77]:
+
+
+
J('0 truthy')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
False
+
+
+
+ +
+
+ +
+
+
+
+
+ +
? == dup truthy
+ +
+
+
+
+
+
In [78]:
+
+
+
V('23 ?')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
        . 23 ?
+     23 . ?
+     23 . dup truthy
+  23 23 . truthy
+23 True . 
+
+
+
+ +
+
+ +
+
+
+
In [79]:
+
+
+
J('[] ?')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[] False
+
+
+
+ +
+
+ +
+
+
+
In [80]:
+
+
+
J('0 ?')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0 False
+
+
+
+ +
+
+ +
+
+
+
+
+

& and

+
+
+
+
+
+
In [81]:
+
+
+
J('23 9 &')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1
+
+
+
+ +
+
+ +
+
+
+
+
+

!= <> ne

+
+
+
+
+
+
In [82]:
+
+
+
J('23 9 !=')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
True
+
+
+
+ +
+
+ +
+
+
+
+
+

The usual suspects:

+
    +
  • < lt
  • +
  • <= le
  • +
  • = eq
  • +
  • > gt
  • +
  • >= ge
  • +
  • not
  • +
  • or
  • +
+ +
+
+
+
+
+
+
+

^ xor

+
+
+
+
+
+
In [83]:
+
+
+
J('1 1 ^')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0
+
+
+
+ +
+
+ +
+
+
+
In [84]:
+
+
+
J('1 0 ^')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1
+
+
+
+ +
+
+ +
+
+
+
+
+

Miscellaneous

+
+
+
+
+
+
+
+

help

+
+
+
+
+
+
In [85]:
+
+
+
J('[help] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
Accepts a quoted symbol on the top of the stack and prints its docs.
+
+
+
+
+ +
+
+ +
+
+
+
+
+

parse

+
+
+
+
+
+
In [86]:
+
+
+
J('[parse] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
Parse the string on the stack to a Joy expression.
+
+
+
+
+ +
+
+ +
+
+
+
In [87]:
+
+
+
J('1 "2 [3] dup" parse')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 [2 [3] dup]
+
+
+
+ +
+
+ +
+
+
+
+
+

run

Evaluate a quoted Joy sequence.

+ +
+
+
+
+
+
In [88]:
+
+
+
J('[1 2 dup + +] run')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[5]
+
+
+
+ +
+
+ +
+
+
+
+
+

Combinators

+
+
+
+
+
+
+
+

app1 app2 app3

+
+
+
+
+
+
In [89]:
+
+
+
J('[app1] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
Given a quoted program on TOS and anything as the second stack item run
+the program and replace the two args with the first result of the
+program.
+
+            ... x [Q] . app1
+   -----------------------------------
+      ... [x ...] [Q] . infra first
+
+
+
+
+ +
+
+ +
+
+
+
In [90]:
+
+
+
J('10 4 [sqr *] app1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10 160
+
+
+
+ +
+
+ +
+
+
+
In [91]:
+
+
+
J('10 3 4 [sqr *] app2')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10 90 160
+
+
+
+ +
+
+ +
+
+
+
In [92]:
+
+
+
J('[app2] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
Like app1 with two items.
+
+       ... y x [Q] . app2
+-----------------------------------
+   ... [y ...] [Q] . infra first
+       [x ...] [Q]   infra first
+
+
+
+
+ +
+
+ +
+
+
+
In [93]:
+
+
+
J('10 2 3 4 [sqr *] app3')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10 40 90 160
+
+
+
+ +
+
+ +
+
+
+
+
+

anamorphism

Given an initial value, a predicate function [P], and a generator function [G], the anamorphism combinator creates a sequence.

+ +
   n [P] [G] anamorphism
+---------------------------
+          [...]
+
+
+

Example, range:

+ +
range == [0 <=] [1 - dup] anamorphism
+ +
+
+
+
+
+
In [94]:
+
+
+
J('3 [0 <=] [1 - dup] anamorphism')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[2 1 0]
+
+
+
+ +
+
+ +
+
+
+
+
+

branch

+
+
+
+
+
+
In [95]:
+
+
+
J('3 4 1 [+] [*] branch')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
12
+
+
+
+ +
+
+ +
+
+
+
In [96]:
+
+
+
J('3 4 0 [+] [*] branch')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
7
+
+
+
+ +
+
+ +
+
+
+
+
+

cleave

+
... x [P] [Q] cleave
+
+
+

From the original Joy docs: "The cleave combinator expects two quotations, and below that an item x +It first executes [P], with x on top, and saves the top result element. +Then it executes [Q], again with x, and saves the top result. +Finally it restores the stack to what it was below x and pushes the two +results P(X) and Q(X)."

+

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:

+ +
cleave == [i] app2 [popd] dip
+ +
+
+
+
+
+
In [97]:
+
+
+
J('10 2 [+] [-] cleave')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10 12 8
+
+
+
+ +
+
+ +
+
+
+
+
+

dip dipd dipdd

+
+
+
+
+
+
In [98]:
+
+
+
J('1 2 3 4 5 [+] dip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 7 5
+
+
+
+ +
+
+ +
+
+
+
In [99]:
+
+
+
J('1 2 3 4 5 [+] dipd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 5 4 5
+
+
+
+ +
+
+ +
+
+
+
In [100]:
+
+
+
J('1 2 3 4 5 [+] dipdd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 3 4 5
+
+
+
+ +
+
+ +
+
+
+
+
+

dupdip

Expects a quoted program [Q] on the stack and some item under it, dup the item and dip the quoted program under it.

+ +
n [Q] dupdip == n Q n
+ +
+
+
+
+
+
In [101]:
+
+
+
V('23 [++] dupdip *')  # N(N + 1)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
        . 23 [++] dupdip *
+     23 . [++] dupdip *
+23 [++] . dupdip *
+     23 . ++ 23 *
+     24 . 23 *
+  24 23 . *
+    552 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

genrec primrec

+
+
+
+
+
+
In [102]:
+
+
+
J('[genrec] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
General Recursion Combinator.
+
+                        [if] [then] [rec1] [rec2] genrec
+  ---------------------------------------------------------------------
+     [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
+
+From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun:
+"The genrec combinator takes four program parameters in addition to
+whatever data parameters it needs. Fourth from the top is an if-part,
+followed by a then-part. If the if-part yields true, then the then-part
+is executed and the combinator terminates. The other two parameters are
+the rec1-part and the rec2-part. If the if-part yields false, the
+rec1-part is executed. Following that the four program parameters and
+the combinator are again pushed onto the stack bundled up in a quoted
+form. Then the rec2-part is executed, where it will find the bundled
+form. Typically it will then execute the bundled form, either with i or
+with app2, or some other combinator."
+
+The way to design one of these is to fix your base case [then] and the
+test [if], and then treat rec1 and rec2 as an else-part "sandwiching"
+a quotation of the whole function.
+
+For example, given a (general recursive) function 'F':
+
+    F == [I] [T] [R1] [R2] genrec
+
+If the [I] if-part fails you must derive R1 and R2 from:
+
+    ... R1 [F] R2
+
+Just set the stack arguments in front, and figure out what R1 and R2
+have to do to apply the quoted [F] in the proper way.  In effect, the
+genrec combinator turns into an ifte combinator with a quoted copy of
+the original definition in the else-part:
+
+    F == [I] [T] [R1]   [R2] genrec
+      == [I] [T] [R1 [F] R2] ifte
+
+(Primitive recursive functions are those where R2 == i.
+
+    P == [I] [T] [R] primrec
+      == [I] [T] [R [P] i] ifte
+      == [I] [T] [R P] ifte
+)
+
+
+
+
+ +
+
+ +
+
+
+
In [103]:
+
+
+
J('3 [1 <=] [] [dup --] [i *] genrec')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6
+
+
+
+ +
+
+ +
+
+
+
+
+

i

+
+
+
+
+
+
In [104]:
+
+
+
V('1 2 3 [+ +] i')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
            . 1 2 3 [+ +] i
+          1 . 2 3 [+ +] i
+        1 2 . 3 [+ +] i
+      1 2 3 . [+ +] i
+1 2 3 [+ +] . i
+      1 2 3 . + +
+        1 5 . +
+          6 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

ifte

+
[predicate] [then] [else] ifte
+ +
+
+
+
+
+
In [105]:
+
+
+
J('1 2 [1] [+] [*] ifte')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3
+
+
+
+ +
+
+ +
+
+
+
In [106]:
+
+
+
J('1 2 [0] [+] [*] ifte')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
+
+

infra

+
+
+
+
+
+
In [107]:
+
+
+
V('1 2 3 [4 5 6] [* +] infra')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                    . 1 2 3 [4 5 6] [* +] infra
+                  1 . 2 3 [4 5 6] [* +] infra
+                1 2 . 3 [4 5 6] [* +] infra
+              1 2 3 . [4 5 6] [* +] infra
+      1 2 3 [4 5 6] . [* +] infra
+1 2 3 [4 5 6] [* +] . infra
+              6 5 4 . * + [3 2 1] swaack
+               6 20 . + [3 2 1] swaack
+                 26 . [3 2 1] swaack
+         26 [3 2 1] . swaack
+         1 2 3 [26] . 
+
+
+
+ +
+
+ +
+
+
+
+
+

loop

+
+
+
+
+
+
In [108]:
+
+
+
J('[loop] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
Basic loop combinator.
+
+   ... True [Q] loop
+-----------------------
+     ... Q [Q] loop
+
+   ... False [Q] loop
+------------------------
+          ...
+
+
+
+
+ +
+
+ +
+
+
+
In [109]:
+
+
+
V('3 dup [1 - dup] loop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
              . 3 dup [1 - dup] loop
+            3 . dup [1 - dup] loop
+          3 3 . [1 - dup] loop
+3 3 [1 - dup] . loop
+            3 . 1 - dup [1 - dup] loop
+          3 1 . - dup [1 - dup] loop
+            2 . dup [1 - dup] loop
+          2 2 . [1 - dup] loop
+2 2 [1 - dup] . loop
+            2 . 1 - dup [1 - dup] loop
+          2 1 . - dup [1 - dup] loop
+            1 . dup [1 - dup] loop
+          1 1 . [1 - dup] loop
+1 1 [1 - dup] . loop
+            1 . 1 - dup [1 - dup] loop
+          1 1 . - dup [1 - dup] loop
+            0 . dup [1 - dup] loop
+          0 0 . [1 - dup] loop
+0 0 [1 - dup] . loop
+            0 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

map pam

+
+
+
+
+
+
In [110]:
+
+
+
J('10 [1 2 3] [*] map')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10 [10 20 30]
+
+
+
+ +
+
+ +
+
+
+
In [111]:
+
+
+
J('10 5 [[*][/][+][-]] pam')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10 5 [50 2.0 15 5]
+
+
+
+ +
+
+ +
+
+
+
+
+

nullary unary binary ternary

Run a quoted program enforcing arity.

+ +
+
+
+
+
+
In [112]:
+
+
+
J('1 2 3 4 5 [+] nullary')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 4 5 9
+
+
+
+ +
+
+ +
+
+
+
In [113]:
+
+
+
J('1 2 3 4 5 [+] unary')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 4 9
+
+
+
+ +
+
+ +
+
+
+
In [114]:
+
+
+
J('1 2 3 4 5 [+] binary')  # + has arity 2 so this is technically pointless...
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 9
+
+
+
+ +
+
+ +
+
+
+
In [115]:
+
+
+
J('1 2 3 4 5 [+] ternary')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 9
+
+
+
+ +
+
+ +
+
+
+
+
+

step

+
+
+
+
+
+
In [116]:
+
+
+
J('[step] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
Run a quoted program on each item in a sequence.
+
+        ... [] [Q] . step
+     -----------------------
+               ... .
+
+
+       ... [a] [Q] . step
+    ------------------------
+             ... a . Q
+
+
+   ... [a b c] [Q] . step
+----------------------------------------
+             ... a . Q [b c] [Q] step
+
+The step combinator executes the quotation on each member of the list
+on top of the stack.
+
+
+
+
+ +
+
+ +
+
+
+
In [117]:
+
+
+
V('0 [1 2 3] [+] step')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
              . 0 [1 2 3] [+] step
+            0 . [1 2 3] [+] step
+    0 [1 2 3] . [+] step
+0 [1 2 3] [+] . step
+      0 1 [+] . i [2 3] [+] step
+          0 1 . + [2 3] [+] step
+            1 . [2 3] [+] step
+      1 [2 3] . [+] step
+  1 [2 3] [+] . step
+      1 2 [+] . i [3] [+] step
+          1 2 . + [3] [+] step
+            3 . [3] [+] step
+        3 [3] . [+] step
+    3 [3] [+] . step
+      3 3 [+] . i
+          3 3 . +
+            6 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

times

+
+
+
+
+
+
In [118]:
+
+
+
V('3 2 1 2 [+] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
            . 3 2 1 2 [+] times
+          3 . 2 1 2 [+] times
+        3 2 . 1 2 [+] times
+      3 2 1 . 2 [+] times
+    3 2 1 2 . [+] times
+3 2 1 2 [+] . times
+      3 2 1 . + 1 [+] times
+        3 3 . 1 [+] times
+      3 3 1 . [+] times
+  3 3 1 [+] . times
+        3 3 . +
+          6 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

b

+
+
+
+
+
+
In [119]:
+
+
+
J('[b] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
b == [i] dip i
+
+... [P] [Q] b == ... [P] i [Q] i
+... [P] [Q] b == ... P Q
+
+
+
+
+ +
+
+ +
+
+
+
In [120]:
+
+
+
V('1 2 [3] [4] b')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
            . 1 2 [3] [4] b
+          1 . 2 [3] [4] b
+        1 2 . [3] [4] b
+    1 2 [3] . [4] b
+1 2 [3] [4] . b
+        1 2 . 3 4
+      1 2 3 . 4
+    1 2 3 4 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

while

+
[predicate] [body] while
+ +
+
+
+
+
+
In [121]:
+
+
+
J('3 [0 >] [dup --] while')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 0
+
+
+
+ +
+
+ +
+
+
+
+
+

x

+
+
+
+
+
+
In [122]:
+
+
+
J('[x] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
x == dup i
+
+... [Q] x = ... [Q] dup i
+... [Q] x = ... [Q] [Q] i
+... [Q] x = ... [Q]  Q
+
+
+
+
+ +
+
+ +
+
+
+
In [123]:
+
+
+
V('1 [2] [i 3] x')  # Kind of a pointless example.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
            . 1 [2] [i 3] x
+          1 . [2] [i 3] x
+      1 [2] . [i 3] x
+1 [2] [i 3] . x
+1 [2] [i 3] . i 3
+      1 [2] . i 3 3
+          1 . 2 3 3
+        1 2 . 3 3
+      1 2 3 . 3
+    1 2 3 3 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

void

Implements Laws of Form arithmetic over quote-only datastructures (that is, datastructures that consist soley of containers, without strings or numbers or anything else.)

+ +
+
+
+
+
+
In [124]:
+
+
+
J('[] void')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
False
+
+
+
+ +
+
+ +
+
+
+
In [125]:
+
+
+
J('[[]] void')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
True
+
+
+
+ +
+
+ +
+
+
+
In [126]:
+
+
+
J('[[][[]]] void')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
True
+
+
+
+ +
+
+ +
+
+
+
In [127]:
+
+
+
J('[[[]][[][]]] void')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
False
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/2._Library_Examples.ipynb b/docs/2._Library_Examples.ipynb new file mode 100644 index 0000000..1349eb4 --- /dev/null +++ b/docs/2._Library_Examples.ipynb @@ -0,0 +1,2964 @@ +{ + "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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 5 6 1 2 3]\n" + ] + } + ], + "source": [ + "J('[1 2 3] [4 5 6] swoncat')" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6 5 4 1 2 3]\n" + ] + } + ], + "source": [ + "J('[1 2 3] [4 5 6] shunt')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `cons` `swons` `uncons`" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ] + } + ], + "source": [ + "J('1 [2 3] cons')" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ] + } + ], + "source": [ + "J('[2 3] 1 swons')" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 [2 3]\n" + ] + } + ], + "source": [ + "J('[1 2 3] uncons')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `first` `second` `third` `rest`" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] first')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] second')" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] third')" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 3 4]\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] rest')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `flatten`" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 [3] 4 5 6]\n" + ] + } + ], + "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": 32, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], + "source": [ + "J('[10 11 12 13 14] 2 getitem')" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] 0 at')" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "J('2 [1 2 3 4] of')" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3 4]\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] 2 drop')" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 1]\n" + ] + } + ], + "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": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 3 1 4]\n" + ] + } + ], + "source": [ + "J('[1 2 3 1 4] 1 remove')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `reverse`" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 3 2 1]\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] reverse')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `size`" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "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": 40, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6 5 4 [3 2 1]\n" + ] + } + ], + "source": [ + "J('1 2 3 [4 5 6] swaack')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `choice` `select`" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "J('23 9 1 choice')" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], + "source": [ + "J('23 9 0 choice')" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "scrolled": true + }, + "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": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], + "source": [ + "J('[23 9 7] 0 select')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `zip`" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "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": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[7 7 7]\n" + ] + } + ], + "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": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "32\n" + ] + } + ], + "source": [ + "J('23 9 +')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `-` `sub`" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "J('23 9 -')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `*` `mul`" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "207\n" + ] + } + ], + "source": [ + "J('23 9 *')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `/` `div` `floordiv` `truediv`" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.5555555555555554\n" + ] + } + ], + "source": [ + "J('23 9 /')" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-2.5555555555555554\n" + ] + } + ], + "source": [ + "J('23 -9 truediv')" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('23 9 div')" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('23 9 floordiv')" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-3\n" + ] + } + ], + "source": [ + "J('23 -9 div')" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-3\n" + ] + } + ], + "source": [ + "J('23 -9 floordiv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `%` `mod` `modulus` `rem` `remainder`" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "J('23 9 %')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `neg`" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-23 5\n" + ] + } + ], + "source": [ + "J('23 neg -5 neg')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### pow" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1024\n" + ] + } + ], + "source": [ + "J('2 10 pow')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `sqr` `sqrt`" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "529\n" + ] + } + ], + "source": [ + "J('23 sqr')" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4.795831523312719\n" + ] + } + ], + "source": [ + "J('23 sqrt')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `++` `succ` `--` `pred`" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('1 ++')" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "J('1 --')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `<<` `lshift` `>>` `rshift`" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n" + ] + } + ], + "source": [ + "J('8 1 <<')" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "J('8 1 >>')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `average`" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.75\n" + ] + } + ], + "source": [ + "J('[1 2 3 5] average')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `range` `range_to_zero` `down_to_zero`" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 3 2 1 0]\n" + ] + } + ], + "source": [ + "J('5 range')" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "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": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 4 3 2 1 0\n" + ] + } + ], + "source": [ + "J('5 down_to_zero')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `product`" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n" + ] + } + ], + "source": [ + "J('[1 2 3 5] product')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `sum`" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11\n" + ] + } + ], + "source": [ + "J('[1 2 3 5] sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `min`" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "J('[1 2 3 5] min')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `gcd`" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n" + ] + } + ], + "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": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3 2]\n" + ] + } + ], + "source": [ + "J('[45 30] least_fraction')" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[23 12]\n" + ] + } + ], + "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": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "J('23 truthy')" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "J('[] truthy') # Python semantics." + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "J('0 truthy')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " ? == dup truthy" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "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": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[] False\n" + ] + } + ], + "source": [ + "J('[] ?')" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 False\n" + ] + } + ], + "source": [ + "J('0 ?')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `&` `and` " + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "J('23 9 &')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `!=` `<>` `ne`" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "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": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "J('1 1 ^')" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "J('1 0 ^')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Miscellaneous" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `help`" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accepts a quoted symbol on the top of the stack and prints its docs.\n", + "\n" + ] + } + ], + "source": [ + "J('[help] help')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `parse`" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Parse the string on the stack to a Joy expression.\n", + "\n" + ] + } + ], + "source": [ + "J('[parse] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 [2 [3] dup]\n" + ] + } + ], + "source": [ + "J('1 \"2 [3] dup\" parse')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `run`\n", + "Evaluate a quoted Joy sequence." + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5]\n" + ] + } + ], + "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": 89, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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", + " ... x [Q] . app1\n", + " -----------------------------------\n", + " ... [x ...] [Q] . infra first\n", + "\n" + ] + } + ], + "source": [ + "J('[app1] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 160\n" + ] + } + ], + "source": [ + "J('10 4 [sqr *] app1')" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 90 160\n" + ] + } + ], + "source": [ + "J('10 3 4 [sqr *] app2')" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Like app1 with two items.\n", + "\n", + " ... y x [Q] . app2\n", + "-----------------------------------\n", + " ... [y ...] [Q] . infra first\n", + " [x ...] [Q] infra first\n", + "\n" + ] + } + ], + "source": [ + "J('[app2] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 40 90 160\n" + ] + } + ], + "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": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 1 0]\n" + ] + } + ], + "source": [ + "J('3 [0 <=] [1 - dup] anamorphism')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `branch`" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], + "source": [ + "J('3 4 1 [+] [*] branch')" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], + "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": 97, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 12 8\n" + ] + } + ], + "source": [ + "J('10 2 [+] [-] cleave')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `dip` `dipd` `dipdd`" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "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": 99, + "metadata": {}, + "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": 100, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 3 4 5\n" + ] + } + ], + "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": 101, + "metadata": {}, + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `genrec` `primrec`" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "General Recursion Combinator.\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", + " F == [I] [T] [R1] [R2] genrec\n", + "\n", + "If the [I] if-part fails you must derive R1 and R2 from:\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", + " 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", + " P == [I] [T] [R] primrec\n", + " == [I] [T] [R [P] i] ifte\n", + " == [I] [T] [R P] ifte\n", + ")\n", + "\n" + ] + } + ], + "source": [ + "J('[genrec] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "J('3 [1 <=] [] [dup --] [i *] genrec')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `i`" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "scrolled": true + }, + "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')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `ifte`\n", + " [predicate] [then] [else] ifte" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "J('1 2 [1] [+] [*] ifte')" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('1 2 [0] [+] [*] ifte')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `infra`" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "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')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `loop`" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Basic loop combinator.\n", + "\n", + " ... True [Q] loop\n", + "-----------------------\n", + " ... Q [Q] loop\n", + "\n", + " ... False [Q] loop\n", + "------------------------\n", + " ...\n", + "\n" + ] + } + ], + "source": [ + "J('[loop] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "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')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `map` `pam`" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 [10 20 30]\n" + ] + } + ], + "source": [ + "J('10 [1 2 3] [*] map')" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 5 [50 2.0 15 5]\n" + ] + } + ], + "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": 112, + "metadata": {}, + "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": 113, + "metadata": {}, + "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": 114, + "metadata": {}, + "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": 115, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 9\n" + ] + } + ], + "source": [ + "J('1 2 3 4 5 [+] ternary')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `step`" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Run a quoted program on each item in a sequence.\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" + ] + } + ], + "source": [ + "J('[step] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": { + "scrolled": true + }, + "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')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `times`" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "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')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `b`" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b == [i] dip i\n", + "\n", + "... [P] [Q] b == ... [P] i [Q] i\n", + "... [P] [Q] b == ... P Q\n", + "\n" + ] + } + ], + "source": [ + "J('[b] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": {}, + "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')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `while`\n", + " [predicate] [body] while" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 0\n" + ] + } + ], + "source": [ + "J('3 [0 >] [dup --] while')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `x`" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x == dup i\n", + "\n", + "... [Q] x = ... [Q] dup i\n", + "... [Q] x = ... [Q] [Q] i\n", + "... [Q] x = ... [Q] Q\n", + "\n" + ] + } + ], + "source": [ + "J('[x] help')" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "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." + ] + }, + { + "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": 124, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "J('[] void')" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "J('[[]] void')" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "J('[[][[]]] void')" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/2._Library_Examples.md b/docs/2._Library_Examples.md new file mode 100644 index 0000000..fd70ce7 --- /dev/null +++ b/docs/2._Library_Examples.md @@ -0,0 +1,1395 @@ + +# Examples (and some documentation) for the Words in the Library + + +```python +from notebook_preamble import J, V +``` + +# Stack Chatter +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.) + +### `clear` + + +```python +J('1 2 3 clear') +``` + + + + +### `dup` `dupd` + + +```python +J('1 2 3 dup') +``` + + 1 2 3 3 + + + +```python +J('1 2 3 dupd') +``` + + 1 2 2 3 + + +### `enstacken` `disenstacken` `stack` `unstack` +(I may have these paired up wrong. I.e. `disenstacken` should be `unstack` and vice versa.) + + +```python +J('1 2 3 enstacken') # Replace the stack with a quote of itself. +``` + + [3 2 1] + + + +```python +J('4 5 6 [3 2 1] disenstacken') # Unpack a list onto the stack. +``` + + 4 5 6 3 2 1 + + + +```python +J('1 2 3 stack') # Get the stack on the stack. +``` + + 1 2 3 [3 2 1] + + + +```python +J('1 2 3 [4 5 6] unstack') # Replace the stack with the list on top. + # The items appear reversed but they are not, + # 4 is on the top of both the list and the stack. +``` + + 6 5 4 + + +### `pop` `popd` `popop` + + +```python +J('1 2 3 pop') +``` + + 1 2 + + + +```python +J('1 2 3 popd') +``` + + 1 3 + + + +```python +J('1 2 3 popop') +``` + + 1 + + +### `roll<` `rolldown` `roll>` `rollup` +The "down" and "up" refer to the movement of two of the top three items (displacing the third.) + + +```python +J('1 2 3 roll<') +``` + + 2 3 1 + + + +```python +J('1 2 3 roll>') +``` + + 3 1 2 + + +### `swap` + + +```python +J('1 2 3 swap') +``` + + 1 3 2 + + +### `tuck` `over` + + +```python +J('1 2 3 tuck') +``` + + 1 3 2 3 + + + +```python +J('1 2 3 over') +``` + + 1 2 3 2 + + +### `unit` `quoted` `unquoted` + + +```python +J('1 2 3 unit') +``` + + 1 2 [3] + + + +```python +J('1 2 3 quoted') +``` + + 1 [2] 3 + + + +```python +J('1 [2] 3 unquoted') +``` + + 1 2 3 + + + +```python +V('1 [dup] 3 unquoted') # Unquoting evaluates. Be aware. +``` + + . 1 [dup] 3 unquoted + 1 . [dup] 3 unquoted + 1 [dup] . 3 unquoted + 1 [dup] 3 . unquoted + 1 [dup] 3 . [i] dip + 1 [dup] 3 [i] . dip + 1 [dup] . i 3 + 1 . dup 3 + 1 1 . 3 + 1 1 3 . + + +# List words + +### `concat` `swoncat` `shunt` + + +```python +J('[1 2 3] [4 5 6] concat') +``` + + [1 2 3 4 5 6] + + + +```python +J('[1 2 3] [4 5 6] swoncat') +``` + + [4 5 6 1 2 3] + + + +```python +J('[1 2 3] [4 5 6] shunt') +``` + + [6 5 4 1 2 3] + + +### `cons` `swons` `uncons` + + +```python +J('1 [2 3] cons') +``` + + [1 2 3] + + + +```python +J('[2 3] 1 swons') +``` + + [1 2 3] + + + +```python +J('[1 2 3] uncons') +``` + + 1 [2 3] + + +### `first` `second` `third` `rest` + + +```python +J('[1 2 3 4] first') +``` + + 1 + + + +```python +J('[1 2 3 4] second') +``` + + 2 + + + +```python +J('[1 2 3 4] third') +``` + + 3 + + + +```python +J('[1 2 3 4] rest') +``` + + [2 3 4] + + +### `flatten` + + +```python +J('[[1] [2 [3] 4] [5 6]] flatten') +``` + + [1 2 [3] 4 5 6] + + +### `getitem` `at` `of` `drop` `take` + +`at` and `getitem` are the same function. `of == swap at` + + +```python +J('[10 11 12 13 14] 2 getitem') +``` + + 12 + + + +```python +J('[1 2 3 4] 0 at') +``` + + 1 + + + +```python +J('2 [1 2 3 4] of') +``` + + 3 + + + +```python +J('[1 2 3 4] 2 drop') +``` + + [3 4] + + + +```python +J('[1 2 3 4] 2 take') # reverses the order +``` + + [2 1] + + +`reverse` could be defines as `reverse == dup size take` + +### `remove` + + +```python +J('[1 2 3 1 4] 1 remove') +``` + + [2 3 1 4] + + +### `reverse` + + +```python +J('[1 2 3 4] reverse') +``` + + [4 3 2 1] + + +### `size` + + +```python +J('[1 1 1 1] size') +``` + + 4 + + +### `swaack` +"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. + + +```python +J('1 2 3 [4 5 6] swaack') +``` + + 6 5 4 [3 2 1] + + +### `choice` `select` + + +```python +J('23 9 1 choice') +``` + + 9 + + + +```python +J('23 9 0 choice') +``` + + 23 + + + +```python +J('[23 9 7] 1 select') # select is basically getitem, should retire it? +``` + + 9 + + + +```python +J('[23 9 7] 0 select') +``` + + 23 + + +### `zip` + + +```python +J('[1 2 3] [6 5 4] zip') +``` + + [[6 1] [5 2] [4 3]] + + + +```python +J('[1 2 3] [6 5 4] zip [sum] map') +``` + + [7 7 7] + + +# Math words + +### `+` `add` + + +```python +J('23 9 +') +``` + + 32 + + +### `-` `sub` + + +```python +J('23 9 -') +``` + + 14 + + +### `*` `mul` + + +```python +J('23 9 *') +``` + + 207 + + +### `/` `div` `floordiv` `truediv` + + +```python +J('23 9 /') +``` + + 2.5555555555555554 + + + +```python +J('23 -9 truediv') +``` + + -2.5555555555555554 + + + +```python +J('23 9 div') +``` + + 2 + + + +```python +J('23 9 floordiv') +``` + + 2 + + + +```python +J('23 -9 div') +``` + + -3 + + + +```python +J('23 -9 floordiv') +``` + + -3 + + +### `%` `mod` `modulus` `rem` `remainder` + + +```python +J('23 9 %') +``` + + 5 + + +### `neg` + + +```python +J('23 neg -5 neg') +``` + + -23 5 + + +### pow + + +```python +J('2 10 pow') +``` + + 1024 + + +### `sqr` `sqrt` + + +```python +J('23 sqr') +``` + + 529 + + + +```python +J('23 sqrt') +``` + + 4.795831523312719 + + +### `++` `succ` `--` `pred` + + +```python +J('1 ++') +``` + + 2 + + + +```python +J('1 --') +``` + + 0 + + +### `<<` `lshift` `>>` `rshift` + + +```python +J('8 1 <<') +``` + + 16 + + + +```python +J('8 1 >>') +``` + + 4 + + +### `average` + + +```python +J('[1 2 3 5] average') +``` + + 2.75 + + +### `range` `range_to_zero` `down_to_zero` + + +```python +J('5 range') +``` + + [4 3 2 1 0] + + + +```python +J('5 range_to_zero') +``` + + [0 1 2 3 4 5] + + + +```python +J('5 down_to_zero') +``` + + 5 4 3 2 1 0 + + +### `product` + + +```python +J('[1 2 3 5] product') +``` + + 30 + + +### `sum` + + +```python +J('[1 2 3 5] sum') +``` + + 11 + + +### `min` + + +```python +J('[1 2 3 5] min') +``` + + 1 + + +### `gcd` + + +```python +J('45 30 gcd') +``` + + 15 + + +### `least_fraction` +If we represent fractions as a quoted pair of integers [q d] this word reduces them to their ... least common factors or whatever. + + +```python +J('[45 30] least_fraction') +``` + + [3 2] + + + +```python +J('[23 12] least_fraction') +``` + + [23 12] + + +# Logic and Comparison + +### `?` `truthy` +Get the Boolean value of the item on the top of the stack. + + +```python +J('23 truthy') +``` + + True + + + +```python +J('[] truthy') # Python semantics. +``` + + False + + + +```python +J('0 truthy') +``` + + False + + + ? == dup truthy + + +```python +V('23 ?') +``` + + . 23 ? + 23 . ? + 23 . dup truthy + 23 23 . truthy + 23 True . + + + +```python +J('[] ?') +``` + + [] False + + + +```python +J('0 ?') +``` + + 0 False + + +### `&` `and` + + +```python +J('23 9 &') +``` + + 1 + + +### `!=` `<>` `ne` + + +```python +J('23 9 !=') +``` + + True + + +The usual suspects: +- `<` `lt` +- `<=` `le` +- `=` `eq` +- `>` `gt` +- `>=` `ge` +- `not` +- `or` + +### `^` `xor` + + +```python +J('1 1 ^') +``` + + 0 + + + +```python +J('1 0 ^') +``` + + 1 + + +# Miscellaneous + +### `help` + + +```python +J('[help] help') +``` + + Accepts a quoted symbol on the top of the stack and prints its docs. + + + +### `parse` + + +```python +J('[parse] help') +``` + + Parse the string on the stack to a Joy expression. + + + + +```python +J('1 "2 [3] dup" parse') +``` + + 1 [2 [3] dup] + + +### `run` +Evaluate a quoted Joy sequence. + + +```python +J('[1 2 dup + +] run') +``` + + [5] + + +# Combinators + +### `app1` `app2` `app3` + + +```python +J('[app1] help') +``` + + Given a quoted program on TOS and anything as the second stack item run + the program and replace the two args with the first result of the + program. + + ... x [Q] . app1 + ----------------------------------- + ... [x ...] [Q] . infra first + + + + +```python +J('10 4 [sqr *] app1') +``` + + 10 160 + + + +```python +J('10 3 4 [sqr *] app2') +``` + + 10 90 160 + + + +```python +J('[app2] help') +``` + + Like app1 with two items. + + ... y x [Q] . app2 + ----------------------------------- + ... [y ...] [Q] . infra first + [x ...] [Q] infra first + + + + +```python +J('10 2 3 4 [sqr *] app3') +``` + + 10 40 90 160 + + +### `anamorphism` +Given an initial value, a predicate function `[P]`, and a generator function `[G]`, the `anamorphism` combinator creates a sequence. + + n [P] [G] anamorphism + --------------------------- + [...] + +Example, `range`: + + range == [0 <=] [1 - dup] anamorphism + + +```python +J('3 [0 <=] [1 - dup] anamorphism') +``` + + [2 1 0] + + +### `branch` + + +```python +J('3 4 1 [+] [*] branch') +``` + + 12 + + + +```python +J('3 4 0 [+] [*] branch') +``` + + 7 + + +### `cleave` + ... x [P] [Q] cleave + +From the original Joy docs: "The cleave combinator expects two quotations, and below that an item `x` +It first executes `[P]`, with `x` on top, and saves the top result element. +Then it executes `[Q]`, again with `x`, and saves the top result. +Finally it restores the stack to what it was below `x` and pushes the two +results P(X) and Q(X)." + +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`: + + cleave == [i] app2 [popd] dip + + +```python +J('10 2 [+] [-] cleave') +``` + + 10 12 8 + + +### `dip` `dipd` `dipdd` + + +```python +J('1 2 3 4 5 [+] dip') +``` + + 1 2 7 5 + + + +```python +J('1 2 3 4 5 [+] dipd') +``` + + 1 5 4 5 + + + +```python +J('1 2 3 4 5 [+] dipdd') +``` + + 3 3 4 5 + + +### `dupdip` +Expects a quoted program `[Q]` on the stack and some item under it, `dup` the item and `dip` the quoted program under it. + + n [Q] dupdip == n Q n + + +```python +V('23 [++] dupdip *') # N(N + 1) +``` + + . 23 [++] dupdip * + 23 . [++] dupdip * + 23 [++] . dupdip * + 23 . ++ 23 * + 24 . 23 * + 24 23 . * + 552 . + + +### `genrec` `primrec` + + +```python +J('[genrec] help') +``` + + General Recursion Combinator. + + [if] [then] [rec1] [rec2] genrec + --------------------------------------------------------------------- + [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte + + From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun: + "The genrec combinator takes four program parameters in addition to + whatever data parameters it needs. Fourth from the top is an if-part, + followed by a then-part. If the if-part yields true, then the then-part + is executed and the combinator terminates. The other two parameters are + the rec1-part and the rec2-part. If the if-part yields false, the + rec1-part is executed. Following that the four program parameters and + the combinator are again pushed onto the stack bundled up in a quoted + form. Then the rec2-part is executed, where it will find the bundled + form. Typically it will then execute the bundled form, either with i or + with app2, or some other combinator." + + The way to design one of these is to fix your base case [then] and the + test [if], and then treat rec1 and rec2 as an else-part "sandwiching" + a quotation of the whole function. + + For example, given a (general recursive) function 'F': + + F == [I] [T] [R1] [R2] genrec + + If the [I] if-part fails you must derive R1 and R2 from: + + ... R1 [F] R2 + + Just set the stack arguments in front, and figure out what R1 and R2 + have to do to apply the quoted [F] in the proper way. In effect, the + genrec combinator turns into an ifte combinator with a quoted copy of + the original definition in the else-part: + + F == [I] [T] [R1] [R2] genrec + == [I] [T] [R1 [F] R2] ifte + + (Primitive recursive functions are those where R2 == i. + + P == [I] [T] [R] primrec + == [I] [T] [R [P] i] ifte + == [I] [T] [R P] ifte + ) + + + + +```python +J('3 [1 <=] [] [dup --] [i *] genrec') +``` + + 6 + + +### `i` + + +```python +V('1 2 3 [+ +] i') +``` + + . 1 2 3 [+ +] i + 1 . 2 3 [+ +] i + 1 2 . 3 [+ +] i + 1 2 3 . [+ +] i + 1 2 3 [+ +] . i + 1 2 3 . + + + 1 5 . + + 6 . + + +### `ifte` + [predicate] [then] [else] ifte + + +```python +J('1 2 [1] [+] [*] ifte') +``` + + 3 + + + +```python +J('1 2 [0] [+] [*] ifte') +``` + + 2 + + +### `infra` + + +```python +V('1 2 3 [4 5 6] [* +] infra') +``` + + . 1 2 3 [4 5 6] [* +] infra + 1 . 2 3 [4 5 6] [* +] infra + 1 2 . 3 [4 5 6] [* +] infra + 1 2 3 . [4 5 6] [* +] infra + 1 2 3 [4 5 6] . [* +] infra + 1 2 3 [4 5 6] [* +] . infra + 6 5 4 . * + [3 2 1] swaack + 6 20 . + [3 2 1] swaack + 26 . [3 2 1] swaack + 26 [3 2 1] . swaack + 1 2 3 [26] . + + +### `loop` + + +```python +J('[loop] help') +``` + + Basic loop combinator. + + ... True [Q] loop + ----------------------- + ... Q [Q] loop + + ... False [Q] loop + ------------------------ + ... + + + + +```python +V('3 dup [1 - dup] loop') +``` + + . 3 dup [1 - dup] loop + 3 . dup [1 - dup] loop + 3 3 . [1 - dup] loop + 3 3 [1 - dup] . loop + 3 . 1 - dup [1 - dup] loop + 3 1 . - dup [1 - dup] loop + 2 . dup [1 - dup] loop + 2 2 . [1 - dup] loop + 2 2 [1 - dup] . loop + 2 . 1 - dup [1 - dup] loop + 2 1 . - dup [1 - dup] loop + 1 . dup [1 - dup] loop + 1 1 . [1 - dup] loop + 1 1 [1 - dup] . loop + 1 . 1 - dup [1 - dup] loop + 1 1 . - dup [1 - dup] loop + 0 . dup [1 - dup] loop + 0 0 . [1 - dup] loop + 0 0 [1 - dup] . loop + 0 . + + +### `map` `pam` + + +```python +J('10 [1 2 3] [*] map') +``` + + 10 [10 20 30] + + + +```python +J('10 5 [[*][/][+][-]] pam') +``` + + 10 5 [50 2.0 15 5] + + +### `nullary` `unary` `binary` `ternary` +Run a quoted program enforcing [arity](https://en.wikipedia.org/wiki/Arity). + + +```python +J('1 2 3 4 5 [+] nullary') +``` + + 1 2 3 4 5 9 + + + +```python +J('1 2 3 4 5 [+] unary') +``` + + 1 2 3 4 9 + + + +```python +J('1 2 3 4 5 [+] binary') # + has arity 2 so this is technically pointless... +``` + + 1 2 3 9 + + + +```python +J('1 2 3 4 5 [+] ternary') +``` + + 1 2 9 + + +### `step` + + +```python +J('[step] help') +``` + + Run a quoted program on each item in a sequence. + + ... [] [Q] . step + ----------------------- + ... . + + + ... [a] [Q] . step + ------------------------ + ... a . Q + + + ... [a b c] [Q] . step + ---------------------------------------- + ... a . Q [b c] [Q] step + + The step combinator executes the quotation on each member of the list + on top of the stack. + + + + +```python +V('0 [1 2 3] [+] step') +``` + + . 0 [1 2 3] [+] step + 0 . [1 2 3] [+] step + 0 [1 2 3] . [+] step + 0 [1 2 3] [+] . step + 0 1 [+] . i [2 3] [+] step + 0 1 . + [2 3] [+] step + 1 . [2 3] [+] step + 1 [2 3] . [+] step + 1 [2 3] [+] . step + 1 2 [+] . i [3] [+] step + 1 2 . + [3] [+] step + 3 . [3] [+] step + 3 [3] . [+] step + 3 [3] [+] . step + 3 3 [+] . i + 3 3 . + + 6 . + + +### `times` + + +```python +V('3 2 1 2 [+] times') +``` + + . 3 2 1 2 [+] times + 3 . 2 1 2 [+] times + 3 2 . 1 2 [+] times + 3 2 1 . 2 [+] times + 3 2 1 2 . [+] times + 3 2 1 2 [+] . times + 3 2 1 . + 1 [+] times + 3 3 . 1 [+] times + 3 3 1 . [+] times + 3 3 1 [+] . times + 3 3 . + + 6 . + + +### `b` + + +```python +J('[b] help') +``` + + b == [i] dip i + + ... [P] [Q] b == ... [P] i [Q] i + ... [P] [Q] b == ... P Q + + + + +```python +V('1 2 [3] [4] b') +``` + + . 1 2 [3] [4] b + 1 . 2 [3] [4] b + 1 2 . [3] [4] b + 1 2 [3] . [4] b + 1 2 [3] [4] . b + 1 2 . 3 4 + 1 2 3 . 4 + 1 2 3 4 . + + +### `while` + [predicate] [body] while + + +```python +J('3 [0 >] [dup --] while') +``` + + 3 2 1 0 + + +### `x` + + +```python +J('[x] help') +``` + + x == dup i + + ... [Q] x = ... [Q] dup i + ... [Q] x = ... [Q] [Q] i + ... [Q] x = ... [Q] Q + + + + +```python +V('1 [2] [i 3] x') # Kind of a pointless example. +``` + + . 1 [2] [i 3] x + 1 . [2] [i 3] x + 1 [2] . [i 3] x + 1 [2] [i 3] . x + 1 [2] [i 3] . i 3 + 1 [2] . i 3 3 + 1 . 2 3 3 + 1 2 . 3 3 + 1 2 3 . 3 + 1 2 3 3 . + + +# `void` +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.) + + +```python +J('[] void') +``` + + False + + + +```python +J('[[]] void') +``` + + True + + + +```python +J('[[][[]]] void') +``` + + True + + + +```python +J('[[[]][[][]]] void') +``` + + False + diff --git a/docs/2._Library_Examples.rst b/docs/2._Library_Examples.rst new file mode 100644 index 0000000..fe7bbb4 --- /dev/null +++ b/docs/2._Library_Examples.rst @@ -0,0 +1,1761 @@ + +Examples (and some documentation) for the Words in the Library +============================================================== + +.. code:: ipython2 + + from notebook_preamble import J, V + +Stack Chatter +============= + +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.) + +``clear`` +~~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 clear') + + +.. parsed-literal:: + + + + +``dup`` ``dupd`` +~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 dup') + + +.. parsed-literal:: + + 1 2 3 3 + + +.. code:: ipython2 + + J('1 2 3 dupd') + + +.. parsed-literal:: + + 1 2 2 3 + + +``enstacken`` ``disenstacken`` ``stack`` ``unstack`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +(I may have these paired up wrong. I.e. ``disenstacken`` should be +``unstack`` and vice versa.) + +.. code:: ipython2 + + J('1 2 3 enstacken') # Replace the stack with a quote of itself. + + +.. parsed-literal:: + + [3 2 1] + + +.. code:: ipython2 + + J('4 5 6 [3 2 1] disenstacken') # Unpack a list onto the stack. + + +.. parsed-literal:: + + 4 5 6 3 2 1 + + +.. code:: ipython2 + + J('1 2 3 stack') # Get the stack on the stack. + + +.. parsed-literal:: + + 1 2 3 [3 2 1] + + +.. code:: ipython2 + + J('1 2 3 [4 5 6] unstack') # Replace the stack with the list on top. + # The items appear reversed but they are not, + # 4 is on the top of both the list and the stack. + + +.. parsed-literal:: + + 6 5 4 + + +``pop`` ``popd`` ``popop`` +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 pop') + + +.. parsed-literal:: + + 1 2 + + +.. code:: ipython2 + + J('1 2 3 popd') + + +.. parsed-literal:: + + 1 3 + + +.. code:: ipython2 + + J('1 2 3 popop') + + +.. parsed-literal:: + + 1 + + +``roll<`` ``rolldown`` ``roll>`` ``rollup`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The "down" and "up" refer to the movement of two of the top three items +(displacing the third.) + +.. code:: ipython2 + + J('1 2 3 roll<') + + +.. parsed-literal:: + + 2 3 1 + + +.. code:: ipython2 + + J('1 2 3 roll>') + + +.. parsed-literal:: + + 3 1 2 + + +``swap`` +~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 swap') + + +.. parsed-literal:: + + 1 3 2 + + +``tuck`` ``over`` +~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 tuck') + + +.. parsed-literal:: + + 1 3 2 3 + + +.. code:: ipython2 + + J('1 2 3 over') + + +.. parsed-literal:: + + 1 2 3 2 + + +``unit`` ``quoted`` ``unquoted`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 unit') + + +.. parsed-literal:: + + 1 2 [3] + + +.. code:: ipython2 + + J('1 2 3 quoted') + + +.. parsed-literal:: + + 1 [2] 3 + + +.. code:: ipython2 + + J('1 [2] 3 unquoted') + + +.. parsed-literal:: + + 1 2 3 + + +.. code:: ipython2 + + V('1 [dup] 3 unquoted') # Unquoting evaluates. Be aware. + + +.. parsed-literal:: + + . 1 [dup] 3 unquoted + 1 . [dup] 3 unquoted + 1 [dup] . 3 unquoted + 1 [dup] 3 . unquoted + 1 [dup] 3 . [i] dip + 1 [dup] 3 [i] . dip + 1 [dup] . i 3 + 1 . dup 3 + 1 1 . 3 + 1 1 3 . + + +List words +========== + +``concat`` ``swoncat`` ``shunt`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3] [4 5 6] concat') + + +.. parsed-literal:: + + [1 2 3 4 5 6] + + +.. code:: ipython2 + + J('[1 2 3] [4 5 6] swoncat') + + +.. parsed-literal:: + + [4 5 6 1 2 3] + + +.. code:: ipython2 + + J('[1 2 3] [4 5 6] shunt') + + +.. parsed-literal:: + + [6 5 4 1 2 3] + + +``cons`` ``swons`` ``uncons`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 [2 3] cons') + + +.. parsed-literal:: + + [1 2 3] + + +.. code:: ipython2 + + J('[2 3] 1 swons') + + +.. parsed-literal:: + + [1 2 3] + + +.. code:: ipython2 + + J('[1 2 3] uncons') + + +.. parsed-literal:: + + 1 [2 3] + + +``first`` ``second`` ``third`` ``rest`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 4] first') + + +.. parsed-literal:: + + 1 + + +.. code:: ipython2 + + J('[1 2 3 4] second') + + +.. parsed-literal:: + + 2 + + +.. code:: ipython2 + + J('[1 2 3 4] third') + + +.. parsed-literal:: + + 3 + + +.. code:: ipython2 + + J('[1 2 3 4] rest') + + +.. parsed-literal:: + + [2 3 4] + + +``flatten`` +~~~~~~~~~~~ + +.. code:: ipython2 + + J('[[1] [2 [3] 4] [5 6]] flatten') + + +.. parsed-literal:: + + [1 2 [3] 4 5 6] + + +``getitem`` ``at`` ``of`` ``drop`` ``take`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``at`` and ``getitem`` are the same function. ``of == swap at`` + +.. code:: ipython2 + + J('[10 11 12 13 14] 2 getitem') + + +.. parsed-literal:: + + 12 + + +.. code:: ipython2 + + J('[1 2 3 4] 0 at') + + +.. parsed-literal:: + + 1 + + +.. code:: ipython2 + + J('2 [1 2 3 4] of') + + +.. parsed-literal:: + + 3 + + +.. code:: ipython2 + + J('[1 2 3 4] 2 drop') + + +.. parsed-literal:: + + [3 4] + + +.. code:: ipython2 + + J('[1 2 3 4] 2 take') # reverses the order + + +.. parsed-literal:: + + [2 1] + + +``reverse`` could be defines as ``reverse == dup size take`` + +``remove`` +~~~~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 1 4] 1 remove') + + +.. parsed-literal:: + + [2 3 1 4] + + +``reverse`` +~~~~~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 4] reverse') + + +.. parsed-literal:: + + [4 3 2 1] + + +``size`` +~~~~~~~~ + +.. code:: ipython2 + + J('[1 1 1 1] size') + + +.. parsed-literal:: + + 4 + + +``swaack`` +~~~~~~~~~~ + +"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. + +.. code:: ipython2 + + J('1 2 3 [4 5 6] swaack') + + +.. parsed-literal:: + + 6 5 4 [3 2 1] + + +``choice`` ``select`` +~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 1 choice') + + +.. parsed-literal:: + + 9 + + +.. code:: ipython2 + + J('23 9 0 choice') + + +.. parsed-literal:: + + 23 + + +.. code:: ipython2 + + J('[23 9 7] 1 select') # select is basically getitem, should retire it? + + +.. parsed-literal:: + + 9 + + +.. code:: ipython2 + + J('[23 9 7] 0 select') + + +.. parsed-literal:: + + 23 + + +``zip`` +~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3] [6 5 4] zip') + + +.. parsed-literal:: + + [[6 1] [5 2] [4 3]] + + +.. code:: ipython2 + + J('[1 2 3] [6 5 4] zip [sum] map') + + +.. parsed-literal:: + + [7 7 7] + + +Math words +========== + +``+`` ``add`` +~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 +') + + +.. parsed-literal:: + + 32 + + +``-`` ``sub`` +~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 -') + + +.. parsed-literal:: + + 14 + + +``*`` ``mul`` +~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 *') + + +.. parsed-literal:: + + 207 + + +``/`` ``div`` ``floordiv`` ``truediv`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 /') + + +.. parsed-literal:: + + 2.5555555555555554 + + +.. code:: ipython2 + + J('23 -9 truediv') + + +.. parsed-literal:: + + -2.5555555555555554 + + +.. code:: ipython2 + + J('23 9 div') + + +.. parsed-literal:: + + 2 + + +.. code:: ipython2 + + J('23 9 floordiv') + + +.. parsed-literal:: + + 2 + + +.. code:: ipython2 + + J('23 -9 div') + + +.. parsed-literal:: + + -3 + + +.. code:: ipython2 + + J('23 -9 floordiv') + + +.. parsed-literal:: + + -3 + + +``%`` ``mod`` ``modulus`` ``rem`` ``remainder`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 %') + + +.. parsed-literal:: + + 5 + + +``neg`` +~~~~~~~ + +.. code:: ipython2 + + J('23 neg -5 neg') + + +.. parsed-literal:: + + -23 5 + + +pow +~~~ + +.. code:: ipython2 + + J('2 10 pow') + + +.. parsed-literal:: + + 1024 + + +``sqr`` ``sqrt`` +~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 sqr') + + +.. parsed-literal:: + + 529 + + +.. code:: ipython2 + + J('23 sqrt') + + +.. parsed-literal:: + + 4.795831523312719 + + +``++`` ``succ`` ``--`` ``pred`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 ++') + + +.. parsed-literal:: + + 2 + + +.. code:: ipython2 + + J('1 --') + + +.. parsed-literal:: + + 0 + + +``<<`` ``lshift`` ``>>`` ``rshift`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('8 1 <<') + + +.. parsed-literal:: + + 16 + + +.. code:: ipython2 + + J('8 1 >>') + + +.. parsed-literal:: + + 4 + + +``average`` +~~~~~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 5] average') + + +.. parsed-literal:: + + 2.75 + + +``range`` ``range_to_zero`` ``down_to_zero`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('5 range') + + +.. parsed-literal:: + + [4 3 2 1 0] + + +.. code:: ipython2 + + J('5 range_to_zero') + + +.. parsed-literal:: + + [0 1 2 3 4 5] + + +.. code:: ipython2 + + J('5 down_to_zero') + + +.. parsed-literal:: + + 5 4 3 2 1 0 + + +``product`` +~~~~~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 5] product') + + +.. parsed-literal:: + + 30 + + +``sum`` +~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 5] sum') + + +.. parsed-literal:: + + 11 + + +``min`` +~~~~~~~ + +.. code:: ipython2 + + J('[1 2 3 5] min') + + +.. parsed-literal:: + + 1 + + +``gcd`` +~~~~~~~ + +.. code:: ipython2 + + J('45 30 gcd') + + +.. parsed-literal:: + + 15 + + +``least_fraction`` +~~~~~~~~~~~~~~~~~~ + +If we represent fractions as a quoted pair of integers [q d] this word +reduces them to their ... least common factors or whatever. + +.. code:: ipython2 + + J('[45 30] least_fraction') + + +.. parsed-literal:: + + [3 2] + + +.. code:: ipython2 + + J('[23 12] least_fraction') + + +.. parsed-literal:: + + [23 12] + + +Logic and Comparison +==================== + +``?`` ``truthy`` +~~~~~~~~~~~~~~~~ + +Get the Boolean value of the item on the top of the stack. + +.. code:: ipython2 + + J('23 truthy') + + +.. parsed-literal:: + + True + + +.. code:: ipython2 + + J('[] truthy') # Python semantics. + + +.. parsed-literal:: + + False + + +.. code:: ipython2 + + J('0 truthy') + + +.. parsed-literal:: + + False + + +:: + + ? == dup truthy + +.. code:: ipython2 + + V('23 ?') + + +.. parsed-literal:: + + . 23 ? + 23 . ? + 23 . dup truthy + 23 23 . truthy + 23 True . + + +.. code:: ipython2 + + J('[] ?') + + +.. parsed-literal:: + + [] False + + +.. code:: ipython2 + + J('0 ?') + + +.. parsed-literal:: + + 0 False + + +``&`` ``and`` +~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 &') + + +.. parsed-literal:: + + 1 + + +``!=`` ``<>`` ``ne`` +~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('23 9 !=') + + +.. parsed-literal:: + + True + + +| The usual suspects: - ``<`` ``lt`` - ``<=`` ``le`` +| - ``=`` ``eq`` - ``>`` ``gt`` - ``>=`` ``ge`` - ``not`` - ``or`` + +``^`` ``xor`` +~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 1 ^') + + +.. parsed-literal:: + + 0 + + +.. code:: ipython2 + + J('1 0 ^') + + +.. parsed-literal:: + + 1 + + +Miscellaneous +============= + +``help`` +~~~~~~~~ + +.. code:: ipython2 + + J('[help] help') + + +.. parsed-literal:: + + Accepts a quoted symbol on the top of the stack and prints its docs. + + + +``parse`` +~~~~~~~~~ + +.. code:: ipython2 + + J('[parse] help') + + +.. parsed-literal:: + + Parse the string on the stack to a Joy expression. + + + +.. code:: ipython2 + + J('1 "2 [3] dup" parse') + + +.. parsed-literal:: + + 1 [2 [3] dup] + + +``run`` +~~~~~~~ + +Evaluate a quoted Joy sequence. + +.. code:: ipython2 + + J('[1 2 dup + +] run') + + +.. parsed-literal:: + + [5] + + +Combinators +=========== + +``app1`` ``app2`` ``app3`` +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('[app1] help') + + +.. parsed-literal:: + + Given a quoted program on TOS and anything as the second stack item run + the program and replace the two args with the first result of the + program. + + ... x [Q] . app1 + ----------------------------------- + ... [x ...] [Q] . infra first + + + +.. code:: ipython2 + + J('10 4 [sqr *] app1') + + +.. parsed-literal:: + + 10 160 + + +.. code:: ipython2 + + J('10 3 4 [sqr *] app2') + + +.. parsed-literal:: + + 10 90 160 + + +.. code:: ipython2 + + J('[app2] help') + + +.. parsed-literal:: + + Like app1 with two items. + + ... y x [Q] . app2 + ----------------------------------- + ... [y ...] [Q] . infra first + [x ...] [Q] infra first + + + +.. code:: ipython2 + + J('10 2 3 4 [sqr *] app3') + + +.. parsed-literal:: + + 10 40 90 160 + + +``anamorphism`` +~~~~~~~~~~~~~~~ + +Given an initial value, a predicate function ``[P]``, and a generator +function ``[G]``, the ``anamorphism`` combinator creates a sequence. + +:: + + n [P] [G] anamorphism + --------------------------- + [...] + +Example, ``range``: + +:: + + range == [0 <=] [1 - dup] anamorphism + +.. code:: ipython2 + + J('3 [0 <=] [1 - dup] anamorphism') + + +.. parsed-literal:: + + [2 1 0] + + +``branch`` +~~~~~~~~~~ + +.. code:: ipython2 + + J('3 4 1 [+] [*] branch') + + +.. parsed-literal:: + + 12 + + +.. code:: ipython2 + + J('3 4 0 [+] [*] branch') + + +.. parsed-literal:: + + 7 + + +``cleave`` +~~~~~~~~~~ + +:: + + ... x [P] [Q] cleave + +From the original Joy docs: "The cleave combinator expects two +quotations, and below that an item ``x`` It first executes ``[P]``, with +``x`` on top, and saves the top result element. Then it executes +``[Q]``, again with ``x``, and saves the top result. Finally it restores +the stack to what it was below ``x`` and pushes the two results P(X) and +Q(X)." + +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``: + +:: + + cleave == [i] app2 [popd] dip + +.. code:: ipython2 + + J('10 2 [+] [-] cleave') + + +.. parsed-literal:: + + 10 12 8 + + +``dip`` ``dipd`` ``dipdd`` +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 2 3 4 5 [+] dip') + + +.. parsed-literal:: + + 1 2 7 5 + + +.. code:: ipython2 + + J('1 2 3 4 5 [+] dipd') + + +.. parsed-literal:: + + 1 5 4 5 + + +.. code:: ipython2 + + J('1 2 3 4 5 [+] dipdd') + + +.. parsed-literal:: + + 3 3 4 5 + + +``dupdip`` +~~~~~~~~~~ + +Expects a quoted program ``[Q]`` on the stack and some item under it, +``dup`` the item and ``dip`` the quoted program under it. + +:: + + n [Q] dupdip == n Q n + +.. code:: ipython2 + + V('23 [++] dupdip *') # N(N + 1) + + +.. parsed-literal:: + + . 23 [++] dupdip * + 23 . [++] dupdip * + 23 [++] . dupdip * + 23 . ++ 23 * + 24 . 23 * + 24 23 . * + 552 . + + +``genrec`` ``primrec`` +~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('[genrec] help') + + +.. parsed-literal:: + + General Recursion Combinator. + + [if] [then] [rec1] [rec2] genrec + --------------------------------------------------------------------- + [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte + + From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun: + "The genrec combinator takes four program parameters in addition to + whatever data parameters it needs. Fourth from the top is an if-part, + followed by a then-part. If the if-part yields true, then the then-part + is executed and the combinator terminates. The other two parameters are + the rec1-part and the rec2-part. If the if-part yields false, the + rec1-part is executed. Following that the four program parameters and + the combinator are again pushed onto the stack bundled up in a quoted + form. Then the rec2-part is executed, where it will find the bundled + form. Typically it will then execute the bundled form, either with i or + with app2, or some other combinator." + + The way to design one of these is to fix your base case [then] and the + test [if], and then treat rec1 and rec2 as an else-part "sandwiching" + a quotation of the whole function. + + For example, given a (general recursive) function 'F': + + F == [I] [T] [R1] [R2] genrec + + If the [I] if-part fails you must derive R1 and R2 from: + + ... R1 [F] R2 + + Just set the stack arguments in front, and figure out what R1 and R2 + have to do to apply the quoted [F] in the proper way. In effect, the + genrec combinator turns into an ifte combinator with a quoted copy of + the original definition in the else-part: + + F == [I] [T] [R1] [R2] genrec + == [I] [T] [R1 [F] R2] ifte + + (Primitive recursive functions are those where R2 == i. + + P == [I] [T] [R] primrec + == [I] [T] [R [P] i] ifte + == [I] [T] [R P] ifte + ) + + + +.. code:: ipython2 + + J('3 [1 <=] [] [dup --] [i *] genrec') + + +.. parsed-literal:: + + 6 + + +``i`` +~~~~~ + +.. code:: ipython2 + + V('1 2 3 [+ +] i') + + +.. parsed-literal:: + + . 1 2 3 [+ +] i + 1 . 2 3 [+ +] i + 1 2 . 3 [+ +] i + 1 2 3 . [+ +] i + 1 2 3 [+ +] . i + 1 2 3 . + + + 1 5 . + + 6 . + + +``ifte`` +~~~~~~~~ + +:: + + [predicate] [then] [else] ifte + +.. code:: ipython2 + + J('1 2 [1] [+] [*] ifte') + + +.. parsed-literal:: + + 3 + + +.. code:: ipython2 + + J('1 2 [0] [+] [*] ifte') + + +.. parsed-literal:: + + 2 + + +``infra`` +~~~~~~~~~ + +.. code:: ipython2 + + V('1 2 3 [4 5 6] [* +] infra') + + +.. parsed-literal:: + + . 1 2 3 [4 5 6] [* +] infra + 1 . 2 3 [4 5 6] [* +] infra + 1 2 . 3 [4 5 6] [* +] infra + 1 2 3 . [4 5 6] [* +] infra + 1 2 3 [4 5 6] . [* +] infra + 1 2 3 [4 5 6] [* +] . infra + 6 5 4 . * + [3 2 1] swaack + 6 20 . + [3 2 1] swaack + 26 . [3 2 1] swaack + 26 [3 2 1] . swaack + 1 2 3 [26] . + + +``loop`` +~~~~~~~~ + +.. code:: ipython2 + + J('[loop] help') + + +.. parsed-literal:: + + Basic loop combinator. + + ... True [Q] loop + ----------------------- + ... Q [Q] loop + + ... False [Q] loop + ------------------------ + ... + + + +.. code:: ipython2 + + V('3 dup [1 - dup] loop') + + +.. parsed-literal:: + + . 3 dup [1 - dup] loop + 3 . dup [1 - dup] loop + 3 3 . [1 - dup] loop + 3 3 [1 - dup] . loop + 3 . 1 - dup [1 - dup] loop + 3 1 . - dup [1 - dup] loop + 2 . dup [1 - dup] loop + 2 2 . [1 - dup] loop + 2 2 [1 - dup] . loop + 2 . 1 - dup [1 - dup] loop + 2 1 . - dup [1 - dup] loop + 1 . dup [1 - dup] loop + 1 1 . [1 - dup] loop + 1 1 [1 - dup] . loop + 1 . 1 - dup [1 - dup] loop + 1 1 . - dup [1 - dup] loop + 0 . dup [1 - dup] loop + 0 0 . [1 - dup] loop + 0 0 [1 - dup] . loop + 0 . + + +``map`` ``pam`` +~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('10 [1 2 3] [*] map') + + +.. parsed-literal:: + + 10 [10 20 30] + + +.. code:: ipython2 + + J('10 5 [[*][/][+][-]] pam') + + +.. parsed-literal:: + + 10 5 [50 2.0 15 5] + + +``nullary`` ``unary`` ``binary`` ``ternary`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Run a quoted program enforcing +`arity `__. + +.. code:: ipython2 + + J('1 2 3 4 5 [+] nullary') + + +.. parsed-literal:: + + 1 2 3 4 5 9 + + +.. code:: ipython2 + + J('1 2 3 4 5 [+] unary') + + +.. parsed-literal:: + + 1 2 3 4 9 + + +.. code:: ipython2 + + J('1 2 3 4 5 [+] binary') # + has arity 2 so this is technically pointless... + + +.. parsed-literal:: + + 1 2 3 9 + + +.. code:: ipython2 + + J('1 2 3 4 5 [+] ternary') + + +.. parsed-literal:: + + 1 2 9 + + +``step`` +~~~~~~~~ + +.. code:: ipython2 + + J('[step] help') + + +.. parsed-literal:: + + Run a quoted program on each item in a sequence. + + ... [] [Q] . step + ----------------------- + ... . + + + ... [a] [Q] . step + ------------------------ + ... a . Q + + + ... [a b c] [Q] . step + ---------------------------------------- + ... a . Q [b c] [Q] step + + The step combinator executes the quotation on each member of the list + on top of the stack. + + + +.. code:: ipython2 + + V('0 [1 2 3] [+] step') + + +.. parsed-literal:: + + . 0 [1 2 3] [+] step + 0 . [1 2 3] [+] step + 0 [1 2 3] . [+] step + 0 [1 2 3] [+] . step + 0 1 [+] . i [2 3] [+] step + 0 1 . + [2 3] [+] step + 1 . [2 3] [+] step + 1 [2 3] . [+] step + 1 [2 3] [+] . step + 1 2 [+] . i [3] [+] step + 1 2 . + [3] [+] step + 3 . [3] [+] step + 3 [3] . [+] step + 3 [3] [+] . step + 3 3 [+] . i + 3 3 . + + 6 . + + +``times`` +~~~~~~~~~ + +.. code:: ipython2 + + V('3 2 1 2 [+] times') + + +.. parsed-literal:: + + . 3 2 1 2 [+] times + 3 . 2 1 2 [+] times + 3 2 . 1 2 [+] times + 3 2 1 . 2 [+] times + 3 2 1 2 . [+] times + 3 2 1 2 [+] . times + 3 2 1 . + 1 [+] times + 3 3 . 1 [+] times + 3 3 1 . [+] times + 3 3 1 [+] . times + 3 3 . + + 6 . + + +``b`` +~~~~~ + +.. code:: ipython2 + + J('[b] help') + + +.. parsed-literal:: + + b == [i] dip i + + ... [P] [Q] b == ... [P] i [Q] i + ... [P] [Q] b == ... P Q + + + +.. code:: ipython2 + + V('1 2 [3] [4] b') + + +.. parsed-literal:: + + . 1 2 [3] [4] b + 1 . 2 [3] [4] b + 1 2 . [3] [4] b + 1 2 [3] . [4] b + 1 2 [3] [4] . b + 1 2 . 3 4 + 1 2 3 . 4 + 1 2 3 4 . + + +``while`` +~~~~~~~~~ + +:: + + [predicate] [body] while + +.. code:: ipython2 + + J('3 [0 >] [dup --] while') + + +.. parsed-literal:: + + 3 2 1 0 + + +``x`` +~~~~~ + +.. code:: ipython2 + + J('[x] help') + + +.. parsed-literal:: + + x == dup i + + ... [Q] x = ... [Q] dup i + ... [Q] x = ... [Q] [Q] i + ... [Q] x = ... [Q] Q + + + +.. code:: ipython2 + + V('1 [2] [i 3] x') # Kind of a pointless example. + + +.. parsed-literal:: + + . 1 [2] [i 3] x + 1 . [2] [i 3] x + 1 [2] . [i 3] x + 1 [2] [i 3] . x + 1 [2] [i 3] . i 3 + 1 [2] . i 3 3 + 1 . 2 3 3 + 1 2 . 3 3 + 1 2 3 . 3 + 1 2 3 3 . + + +``void`` +======== + +Implements `**Laws of Form** +*arithmetic* `__ +over quote-only datastructures (that is, datastructures that consist +soley of containers, without strings or numbers or anything else.) + +.. code:: ipython2 + + J('[] void') + + +.. parsed-literal:: + + False + + +.. code:: ipython2 + + J('[[]] void') + + +.. parsed-literal:: + + True + + +.. code:: ipython2 + + J('[[][[]]] void') + + +.. parsed-literal:: + + True + + +.. code:: ipython2 + + J('[[[]][[][]]] void') + + +.. parsed-literal:: + + False + diff --git a/docs/3._Developing_a_Program.html b/docs/3._Developing_a_Program.html new file mode 100644 index 0000000..15f357b --- /dev/null +++ b/docs/3._Developing_a_Program.html @@ -0,0 +1,13283 @@ + + + +3._Developing_a_Program + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Project Euler, first problem: "Multiples of 3 and 5"

+
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
+
+Find the sum of all the multiples of 3 or 5 below 1000.
+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

Let's create a predicate that returns True if a number is a multiple of 3 or 5 and False otherwise.

+ +
+
+
+
+
+
In [2]:
+
+
+
define('P == [3 % not] dupdip 5 % not or')
+
+ +
+
+
+ +
+
+
+
In [3]:
+
+
+
V('80 P')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
             . 80 P
+          80 . P
+          80 . [3 % not] dupdip 5 % not or
+80 [3 % not] . dupdip 5 % not or
+          80 . 3 % not 80 5 % not or
+        80 3 . % not 80 5 % not or
+           2 . not 80 5 % not or
+       False . 80 5 % not or
+    False 80 . 5 % not or
+  False 80 5 . % not or
+     False 0 . not or
+  False True . or
+        True . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Given the predicate function P a suitable program is:

+ +
PE1 == 1000 range [P] filter sum
+
+
+

This function generates a list of the integers from 0 to 999, filters +that list by P, and then sums the result.

+

Logically this is fine, but pragmatically we are doing more work than we +should be; we generate one thousand integers but actually use less than +half of them. A better solution would be to generate just the multiples +we want to sum, and to add them as we go rather than storing them and +adding summing them at the end.

+

At first I had the idea to use two counters and increase them by three +and five, respectively. This way we only generate the terms that we +actually want to sum. We have to proceed by incrementing the counter +that is lower, or if they are equal, the three counter, and we have to +take care not to double add numbers like 15 that are multiples of both +three and five.

+

This seemed a little clunky, so I tried a different approach.

+

Consider the first few terms in the series:

+ +
3 5 6 9 10 12 15 18 20 21 ...
+
+
+

Subtract each number from the one after it (subtracting 0 from 3):

+ +
3 5 6 9 10 12 15 18 20 21 24 25 27 30 ...
+0 3 5 6  9 10 12 15 18 20 21 24 25 27 ...
+-------------------------------------------
+3 2 1 3  1  2  3  3  2  1  3  1  2  3 ...
+
+
+

You get this lovely repeating palindromic sequence:

+ +
3 2 1 3 1 2 3
+
+
+

To make a counter that increments by factors of 3 and 5 you just add +these differences to the counter one-by-one in a loop.

+

To make use of this sequence to increment a counter and sum terms as we +go we need a function that will accept the sum, the counter, and the next +term to add, and that adds the term to the counter and a copy of the +counter to the running sum. This function will do that:

+ +
PE1.1 == + [+] dupdip
+ +
+
+
+
+
+
In [4]:
+
+
+
define('PE1.1 == + [+] dupdip')
+
+ +
+
+
+ +
+
+
+
In [5]:
+
+
+
V('0 0 3 PE1.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
        . 0 0 3 PE1.1
+      0 . 0 3 PE1.1
+    0 0 . 3 PE1.1
+  0 0 3 . PE1.1
+  0 0 3 . + [+] dupdip
+    0 3 . [+] dupdip
+0 3 [+] . dupdip
+    0 3 . + 3
+      3 . 3
+    3 3 . 
+
+
+
+ +
+
+ +
+
+
+
In [6]:
+
+
+
V('0 0 [3 2 1 3 1 2 3] [PE1.1] step')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                            . 0 0 [3 2 1 3 1 2 3] [PE1.1] step
+                          0 . 0 [3 2 1 3 1 2 3] [PE1.1] step
+                        0 0 . [3 2 1 3 1 2 3] [PE1.1] step
+        0 0 [3 2 1 3 1 2 3] . [PE1.1] step
+0 0 [3 2 1 3 1 2 3] [PE1.1] . step
+              0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step
+                      0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step
+                      0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
+                        0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step
+                    0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step
+                        0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step
+                          3 . 3 [2 1 3 1 2 3] [PE1.1] step
+                        3 3 . [2 1 3 1 2 3] [PE1.1] step
+          3 3 [2 1 3 1 2 3] . [PE1.1] step
+  3 3 [2 1 3 1 2 3] [PE1.1] . step
+              3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step
+                      3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step
+                      3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step
+                        3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step
+                    3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step
+                        3 5 . + 5 [1 3 1 2 3] [PE1.1] step
+                          8 . 5 [1 3 1 2 3] [PE1.1] step
+                        8 5 . [1 3 1 2 3] [PE1.1] step
+            8 5 [1 3 1 2 3] . [PE1.1] step
+    8 5 [1 3 1 2 3] [PE1.1] . step
+              8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step
+                      8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step
+                      8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step
+                        8 6 . [+] dupdip [3 1 2 3] [PE1.1] step
+                    8 6 [+] . dupdip [3 1 2 3] [PE1.1] step
+                        8 6 . + 6 [3 1 2 3] [PE1.1] step
+                         14 . 6 [3 1 2 3] [PE1.1] step
+                       14 6 . [3 1 2 3] [PE1.1] step
+             14 6 [3 1 2 3] . [PE1.1] step
+     14 6 [3 1 2 3] [PE1.1] . step
+             14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step
+                     14 6 3 . PE1.1 [1 2 3] [PE1.1] step
+                     14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step
+                       14 9 . [+] dupdip [1 2 3] [PE1.1] step
+                   14 9 [+] . dupdip [1 2 3] [PE1.1] step
+                       14 9 . + 9 [1 2 3] [PE1.1] step
+                         23 . 9 [1 2 3] [PE1.1] step
+                       23 9 . [1 2 3] [PE1.1] step
+               23 9 [1 2 3] . [PE1.1] step
+       23 9 [1 2 3] [PE1.1] . step
+             23 9 1 [PE1.1] . i [2 3] [PE1.1] step
+                     23 9 1 . PE1.1 [2 3] [PE1.1] step
+                     23 9 1 . + [+] dupdip [2 3] [PE1.1] step
+                      23 10 . [+] dupdip [2 3] [PE1.1] step
+                  23 10 [+] . dupdip [2 3] [PE1.1] step
+                      23 10 . + 10 [2 3] [PE1.1] step
+                         33 . 10 [2 3] [PE1.1] step
+                      33 10 . [2 3] [PE1.1] step
+                33 10 [2 3] . [PE1.1] step
+        33 10 [2 3] [PE1.1] . step
+            33 10 2 [PE1.1] . i [3] [PE1.1] step
+                    33 10 2 . PE1.1 [3] [PE1.1] step
+                    33 10 2 . + [+] dupdip [3] [PE1.1] step
+                      33 12 . [+] dupdip [3] [PE1.1] step
+                  33 12 [+] . dupdip [3] [PE1.1] step
+                      33 12 . + 12 [3] [PE1.1] step
+                         45 . 12 [3] [PE1.1] step
+                      45 12 . [3] [PE1.1] step
+                  45 12 [3] . [PE1.1] step
+          45 12 [3] [PE1.1] . step
+            45 12 3 [PE1.1] . i
+                    45 12 3 . PE1.1
+                    45 12 3 . + [+] dupdip
+                      45 15 . [+] dupdip
+                  45 15 [+] . dupdip
+                      45 15 . + 15
+                         60 . 15
+                      60 15 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

So one step through all seven terms brings the counter to 15 and the total to 60.

+ +
+
+
+
+
+
In [7]:
+
+
+
1000 / 15
+
+ +
+
+
+ +
+
+ + +
+ +
Out[7]:
+ + + + +
+
66
+
+ +
+ +
+
+ +
+
+
+
In [8]:
+
+
+
66 * 15
+
+ +
+
+
+ +
+
+ + +
+ +
Out[8]:
+ + + + +
+
990
+
+ +
+ +
+
+ +
+
+
+
In [9]:
+
+
+
1000 - 990
+
+ +
+
+
+ +
+
+ + +
+ +
Out[9]:
+ + + + +
+
10
+
+ +
+ +
+
+ +
+
+
+
+
+

We only want the terms less than 1000.

+ +
+
+
+
+
+
In [10]:
+
+
+
999 - 990
+
+ +
+
+
+ +
+
+ + +
+ +
Out[10]:
+ + + + +
+
9
+
+ +
+ +
+
+ +
+
+
+
+
+

That means we want to run the full list of numbers sixty-six times to get to 990 and then the first four numbers 3 2 1 3 to get to 999.

+ +
+
+
+
+
+
In [11]:
+
+
+
define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')
+
+ +
+
+
+ +
+
+
+
In [12]:
+
+
+
J('PE1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
233168
+
+
+
+ +
+
+ +
+
+
+
+
+

This form uses no extra storage and produces no unused summands. It's +good but there's one more trick we can apply. The list of seven terms +takes up at least seven bytes. But notice that all of the terms are less +than four, and so each can fit in just two bits. We could store all +seven terms in just fourteen bits and use masking and shifts to pick out +each term as we go. This will use less space and save time loading whole +integer terms from the list.

+ +
    3  2  1  3  1  2  3
+0b 11 10 01 11 01 10 11 == 14811
+ +
+
+
+
+
+
In [13]:
+
+
+
0b11100111011011
+
+ +
+
+
+ +
+
+ + +
+ +
Out[13]:
+ + + + +
+
14811
+
+ +
+ +
+
+ +
+
+
+
In [14]:
+
+
+
define('PE1.2 == [3 & PE1.1] dupdip 2 >>')
+
+ +
+
+
+ +
+
+
+
In [15]:
+
+
+
V('0 0 14811 PE1.2')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                      . 0 0 14811 PE1.2
+                    0 . 0 14811 PE1.2
+                  0 0 . 14811 PE1.2
+            0 0 14811 . PE1.2
+            0 0 14811 . [3 & PE1.1] dupdip 2 >>
+0 0 14811 [3 & PE1.1] . dupdip 2 >>
+            0 0 14811 . 3 & PE1.1 14811 2 >>
+          0 0 14811 3 . & PE1.1 14811 2 >>
+                0 0 3 . PE1.1 14811 2 >>
+                0 0 3 . + [+] dupdip 14811 2 >>
+                  0 3 . [+] dupdip 14811 2 >>
+              0 3 [+] . dupdip 14811 2 >>
+                  0 3 . + 3 14811 2 >>
+                    3 . 3 14811 2 >>
+                  3 3 . 14811 2 >>
+            3 3 14811 . 2 >>
+          3 3 14811 2 . >>
+             3 3 3702 . 
+
+
+
+ +
+
+ +
+
+
+
In [16]:
+
+
+
V('3 3 3702 PE1.2')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                     . 3 3 3702 PE1.2
+                   3 . 3 3702 PE1.2
+                 3 3 . 3702 PE1.2
+            3 3 3702 . PE1.2
+            3 3 3702 . [3 & PE1.1] dupdip 2 >>
+3 3 3702 [3 & PE1.1] . dupdip 2 >>
+            3 3 3702 . 3 & PE1.1 3702 2 >>
+          3 3 3702 3 . & PE1.1 3702 2 >>
+               3 3 2 . PE1.1 3702 2 >>
+               3 3 2 . + [+] dupdip 3702 2 >>
+                 3 5 . [+] dupdip 3702 2 >>
+             3 5 [+] . dupdip 3702 2 >>
+                 3 5 . + 5 3702 2 >>
+                   8 . 5 3702 2 >>
+                 8 5 . 3702 2 >>
+            8 5 3702 . 2 >>
+          8 5 3702 2 . >>
+             8 5 925 . 
+
+
+
+ +
+
+ +
+
+
+
In [17]:
+
+
+
V('0 0 14811 7 [PE1.2] times pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                      . 0 0 14811 7 [PE1.2] times pop
+                    0 . 0 14811 7 [PE1.2] times pop
+                  0 0 . 14811 7 [PE1.2] times pop
+            0 0 14811 . 7 [PE1.2] times pop
+          0 0 14811 7 . [PE1.2] times pop
+  0 0 14811 7 [PE1.2] . times pop
+    0 0 14811 [PE1.2] . i 6 [PE1.2] times pop
+            0 0 14811 . PE1.2 6 [PE1.2] times pop
+            0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop
+0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop
+            0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop
+          0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop
+                0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop
+                0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop
+                  0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop
+              0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop
+                  0 3 . + 3 14811 2 >> 6 [PE1.2] times pop
+                    3 . 3 14811 2 >> 6 [PE1.2] times pop
+                  3 3 . 14811 2 >> 6 [PE1.2] times pop
+            3 3 14811 . 2 >> 6 [PE1.2] times pop
+          3 3 14811 2 . >> 6 [PE1.2] times pop
+             3 3 3702 . 6 [PE1.2] times pop
+           3 3 3702 6 . [PE1.2] times pop
+   3 3 3702 6 [PE1.2] . times pop
+     3 3 3702 [PE1.2] . i 5 [PE1.2] times pop
+             3 3 3702 . PE1.2 5 [PE1.2] times pop
+             3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop
+ 3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop
+             3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop
+           3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop
+                3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop
+                3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop
+                  3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop
+              3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop
+                  3 5 . + 5 3702 2 >> 5 [PE1.2] times pop
+                    8 . 5 3702 2 >> 5 [PE1.2] times pop
+                  8 5 . 3702 2 >> 5 [PE1.2] times pop
+             8 5 3702 . 2 >> 5 [PE1.2] times pop
+           8 5 3702 2 . >> 5 [PE1.2] times pop
+              8 5 925 . 5 [PE1.2] times pop
+            8 5 925 5 . [PE1.2] times pop
+    8 5 925 5 [PE1.2] . times pop
+      8 5 925 [PE1.2] . i 4 [PE1.2] times pop
+              8 5 925 . PE1.2 4 [PE1.2] times pop
+              8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop
+  8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop
+              8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop
+            8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop
+                8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop
+                8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop
+                  8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop
+              8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop
+                  8 6 . + 6 925 2 >> 4 [PE1.2] times pop
+                   14 . 6 925 2 >> 4 [PE1.2] times pop
+                 14 6 . 925 2 >> 4 [PE1.2] times pop
+             14 6 925 . 2 >> 4 [PE1.2] times pop
+           14 6 925 2 . >> 4 [PE1.2] times pop
+             14 6 231 . 4 [PE1.2] times pop
+           14 6 231 4 . [PE1.2] times pop
+   14 6 231 4 [PE1.2] . times pop
+     14 6 231 [PE1.2] . i 3 [PE1.2] times pop
+             14 6 231 . PE1.2 3 [PE1.2] times pop
+             14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop
+ 14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop
+             14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop
+           14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop
+               14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop
+               14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop
+                 14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop
+             14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop
+                 14 9 . + 9 231 2 >> 3 [PE1.2] times pop
+                   23 . 9 231 2 >> 3 [PE1.2] times pop
+                 23 9 . 231 2 >> 3 [PE1.2] times pop
+             23 9 231 . 2 >> 3 [PE1.2] times pop
+           23 9 231 2 . >> 3 [PE1.2] times pop
+              23 9 57 . 3 [PE1.2] times pop
+            23 9 57 3 . [PE1.2] times pop
+    23 9 57 3 [PE1.2] . times pop
+      23 9 57 [PE1.2] . i 2 [PE1.2] times pop
+              23 9 57 . PE1.2 2 [PE1.2] times pop
+              23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop
+  23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop
+              23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop
+            23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop
+               23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop
+               23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop
+                23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop
+            23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop
+                23 10 . + 10 57 2 >> 2 [PE1.2] times pop
+                   33 . 10 57 2 >> 2 [PE1.2] times pop
+                33 10 . 57 2 >> 2 [PE1.2] times pop
+             33 10 57 . 2 >> 2 [PE1.2] times pop
+           33 10 57 2 . >> 2 [PE1.2] times pop
+             33 10 14 . 2 [PE1.2] times pop
+           33 10 14 2 . [PE1.2] times pop
+   33 10 14 2 [PE1.2] . times pop
+     33 10 14 [PE1.2] . i 1 [PE1.2] times pop
+             33 10 14 . PE1.2 1 [PE1.2] times pop
+             33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop
+ 33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop
+             33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop
+           33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop
+              33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop
+              33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop
+                33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop
+            33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop
+                33 12 . + 12 14 2 >> 1 [PE1.2] times pop
+                   45 . 12 14 2 >> 1 [PE1.2] times pop
+                45 12 . 14 2 >> 1 [PE1.2] times pop
+             45 12 14 . 2 >> 1 [PE1.2] times pop
+           45 12 14 2 . >> 1 [PE1.2] times pop
+              45 12 3 . 1 [PE1.2] times pop
+            45 12 3 1 . [PE1.2] times pop
+    45 12 3 1 [PE1.2] . times pop
+      45 12 3 [PE1.2] . i pop
+              45 12 3 . PE1.2 pop
+              45 12 3 . [3 & PE1.1] dupdip 2 >> pop
+  45 12 3 [3 & PE1.1] . dupdip 2 >> pop
+              45 12 3 . 3 & PE1.1 3 2 >> pop
+            45 12 3 3 . & PE1.1 3 2 >> pop
+              45 12 3 . PE1.1 3 2 >> pop
+              45 12 3 . + [+] dupdip 3 2 >> pop
+                45 15 . [+] dupdip 3 2 >> pop
+            45 15 [+] . dupdip 3 2 >> pop
+                45 15 . + 15 3 2 >> pop
+                   60 . 15 3 2 >> pop
+                60 15 . 3 2 >> pop
+              60 15 3 . 2 >> pop
+            60 15 3 2 . >> pop
+              60 15 0 . pop
+                60 15 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

And so we have at last:

+ +
+
+
+
+
+
In [18]:
+
+
+
define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')
+
+ +
+
+
+ +
+
+
+
In [19]:
+
+
+
J('PE1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
233168
+
+
+
+ +
+
+ +
+
+
+
+
+

Let's refactor.

+ +
  14811 7 [PE1.2] times pop
+  14811 4 [PE1.2] times pop
+  14811 n [PE1.2] times pop
+n 14811 swap [PE1.2] times pop
+ +
+
+
+
+
+
In [20]:
+
+
+
define('PE1.3 == 14811 swap [PE1.2] times pop')
+
+ +
+
+
+ +
+
+
+
+
+

Now we can simplify the definition above:

+ +
+
+
+
+
+
In [21]:
+
+
+
define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop')
+
+ +
+
+
+ +
+
+
+
In [22]:
+
+
+
J('PE1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
233168
+
+
+
+ +
+
+ +
+
+
+
+
+

Here's our joy program all in one place. It doesn't make so much sense, but if you have read through the above description of how it was derived I hope it's clear.

+ +
PE1.1 == + [+] dupdip
+PE1.2 == [3 & PE1.1] dupdip 2 >>
+PE1.3 == 14811 swap [PE1.2] times pop
+PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop
+ +
+
+
+
+
+
+
+

Generator Version

It's a little clunky iterating sixty-six times though the seven numbers then four more. In the Generator Programs notebook we derive a generator that can be repeatedly driven by the x combinator to produce a stream of the seven numbers repeating over and over again.

+ +
+
+
+
+
+
In [23]:
+
+
+
define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')
+
+ +
+
+
+ +
+
+
+
In [24]:
+
+
+
J('PE1.terms 21 [x] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]
+
+
+
+ +
+
+ +
+
+
+
+
+

We know from above that we need sixty-six times seven then four more terms to reach up to but not over one thousand.

+ +
+
+
+
+
+
In [25]:
+
+
+
J('7 66 * 4 +')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
466
+
+
+
+ +
+
+ +
+
+
+
+
+

Here they are...

+
+
+
+
+
+
In [26]:
+
+
+
J('PE1.terms 466 [x] times pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3
+
+
+
+ +
+
+ +
+
+
+
+
+

...and they do sum to 999.

+
+
+
+
+
+
In [27]:
+
+
+
J('[PE1.terms 466 [x] times pop] run sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
999
+
+
+
+ +
+
+ +
+
+
+
+
+

Now we can use PE1.1 to accumulate the terms as we go, and then pop the generator and the counter from the stack when we're done, leaving just the sum.

+ +
+
+
+
+
+
In [28]:
+
+
+
J('0 0 PE1.terms 466 [x [PE1.1] dip] times popop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
233168
+
+
+
+ +
+
+ +
+
+
+
+
+

A little further analysis renders iteration unnecessary.

Consider finding the sum of the positive integers less than or equal to ten.

+ +
+
+
+
+
+
In [29]:
+
+
+
J('[10 9 8 7 6 5 4 3 2 1] sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
55
+
+
+
+ +
+
+ +
+
+
+
+
+

Instead of summing them, observe:

+ +
  10  9  8  7  6
++  1  2  3  4  5
+---- -- -- -- --
+  11 11 11 11 11
+
+  11 * 5 = 55
+
+
+

From the above example we can deduce that the sum of the first N positive integers is:

+ +
(N + 1) * N / 2 
+
+
+

(The formula also works for odd values of N, I'll leave that to you if you want to work it out or you can take my word for it.)

+ +
+
+
+
+
+
In [30]:
+
+
+
define('F == dup ++ * 2 floordiv')
+
+ +
+
+
+ +
+
+
+
In [31]:
+
+
+
V('10 F')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
      . 10 F
+   10 . F
+   10 . dup ++ * 2 floordiv
+10 10 . ++ * 2 floordiv
+10 11 . * 2 floordiv
+  110 . 2 floordiv
+110 2 . floordiv
+   55 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Generalizing to Blocks of Terms

We can apply the same reasoning to the PE1 problem.

+

Between 0 and 990 inclusive there are sixty-six "blocks" of seven terms each, starting with:

+ +
[3 5 6 9 10 12 15]
+
+
+

And ending with:

+ +
[978 980 981 984 985 987 990]
+
+
+

If we reverse one of these two blocks and sum pairs...

+ +
+
+
+
+
+
In [32]:
+
+
+
J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[978 15] [980 12] [981 10] [984 9] [985 6] [987 5] [990 3]]
+
+
+
+ +
+
+ +
+
+
+
In [33]:
+
+
+
J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[993 992 991 993 991 992 993]
+
+
+
+ +
+
+ +
+
+
+
+
+

(Interesting that the sequence of seven numbers appears again in the rightmost digit of each term.)

+ +
+
+
+
+
+
In [34]:
+
+
+
J('[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6945
+
+
+
+ +
+
+ +
+
+
+
+
+

Since there are sixty-six blocks and we are pairing them up, there must be thirty-three pairs, each of which sums to 6945. We also have these additional unpaired terms between 990 and 1000:

+ +
993 995 996 999
+
+
+

So we can give the "sum of all the multiples of 3 or 5 below 1000" like so:

+ +
+
+
+
+
+
In [35]:
+
+
+
J('6945 33 * [993 995 996 999] cons sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
233168
+
+
+
+ +
+
+ +
+
+
+
+
+

It's worth noting, I think, that this same reasoning holds for any two numbers $n$ and $m$ the multiples of which we hope to sum. The multiples would have a cycle of differences of length $k$ and so we could compute the sum of $Nk$ multiples as above.

+

The sequence of differences will always be a palidrome. Consider an interval spanning the least common multiple of $n$ and $m$:

+ +
|   |   |   |   |   |   |   |
+|      |      |      |      |
+
+
+

Here we have 4 and 7, and you can read off the sequence of differences directly from the diagram: 4 3 1 4 2 2 4 1 3 4.

+

Geometrically, the actual values of $n$ and $m$ and their lcm don't matter, the pattern they make will always be symmetrical around its midpoint. The same reasoning holds for multiples of more than two numbers.

+ +
+
+
+
+
+
+
+

The Simplest Program

+
+
+
+
+
+
+
+

Of course, the simplest joy program for the first Project Euler problem is just:

+ +
PE1 == 233168
+
+
+

Fin.

+ +
+
+
+
+
+ + + + + + diff --git a/docs/3._Developing_a_Program.ipynb b/docs/3._Developing_a_Program.ipynb new file mode 100644 index 0000000..0cb1730 --- /dev/null +++ b/docs/3._Developing_a_Program.ipynb @@ -0,0 +1,1121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# [Project Euler, first problem: \"Multiples of 3 and 5\"](https://projecteuler.net/problem=1)\n", + "\n", + " If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n", + "\n", + " Find the sum of all the multiples of 3 or 5 below 1000." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's create a predicate that returns `True` if a number is a multiple of 3 or 5 and `False` otherwise." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "define('P == [3 % not] dupdip 5 % not or')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 80 P\n", + " 80 . P\n", + " 80 . [3 % not] dupdip 5 % not or\n", + "80 [3 % not] . dupdip 5 % not or\n", + " 80 . 3 % not 80 5 % not or\n", + " 80 3 . % not 80 5 % not or\n", + " 2 . not 80 5 % not or\n", + " False . 80 5 % not or\n", + " False 80 . 5 % not or\n", + " False 80 5 . % not or\n", + " False 0 . not or\n", + " False True . or\n", + " True . \n" + ] + } + ], + "source": [ + "V('80 P')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Given the predicate function `P` a suitable program is:\n", + "\n", + " PE1 == 1000 range [P] filter sum\n", + "\n", + "This function generates a list of the integers from 0 to 999, filters\n", + "that list by `P`, and then sums the result.\n", + "\n", + "Logically this is fine, but pragmatically we are doing more work than we\n", + "should be; we generate one thousand integers but actually use less than\n", + "half of them. A better solution would be to generate just the multiples\n", + "we want to sum, and to add them as we go rather than storing them and\n", + "adding summing them at the end.\n", + "\n", + "At first I had the idea to use two counters and increase them by three\n", + "and five, respectively. This way we only generate the terms that we\n", + "actually want to sum. We have to proceed by incrementing the counter\n", + "that is lower, or if they are equal, the three counter, and we have to\n", + "take care not to double add numbers like 15 that are multiples of both\n", + "three and five.\n", + "\n", + "This seemed a little clunky, so I tried a different approach.\n", + "\n", + "Consider the first few terms in the series:\n", + "\n", + " 3 5 6 9 10 12 15 18 20 21 ...\n", + "\n", + "Subtract each number from the one after it (subtracting 0 from 3):\n", + "\n", + " 3 5 6 9 10 12 15 18 20 21 24 25 27 30 ...\n", + " 0 3 5 6 9 10 12 15 18 20 21 24 25 27 ...\n", + " -------------------------------------------\n", + " 3 2 1 3 1 2 3 3 2 1 3 1 2 3 ...\n", + "\n", + "You get this lovely repeating palindromic sequence:\n", + "\n", + " 3 2 1 3 1 2 3\n", + "\n", + "To make a counter that increments by factors of 3 and 5 you just add\n", + "these differences to the counter one-by-one in a loop.\n", + "\n", + "\n", + "To make use of this sequence to increment a counter and sum terms as we\n", + "go we need a function that will accept the sum, the counter, and the next\n", + "term to add, and that adds the term to the counter and a copy of the\n", + "counter to the running sum. This function will do that:\n", + "\n", + " PE1.1 == + [+] dupdip" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.1 == + [+] dupdip')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 0 0 3 PE1.1\n", + " 0 . 0 3 PE1.1\n", + " 0 0 . 3 PE1.1\n", + " 0 0 3 . PE1.1\n", + " 0 0 3 . + [+] dupdip\n", + " 0 3 . [+] dupdip\n", + "0 3 [+] . dupdip\n", + " 0 3 . + 3\n", + " 3 . 3\n", + " 3 3 . \n" + ] + } + ], + "source": [ + "V('0 0 3 PE1.1')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 0 0 [3 2 1 3 1 2 3] [PE1.1] step\n", + " 0 . 0 [3 2 1 3 1 2 3] [PE1.1] step\n", + " 0 0 . [3 2 1 3 1 2 3] [PE1.1] step\n", + " 0 0 [3 2 1 3 1 2 3] . [PE1.1] step\n", + "0 0 [3 2 1 3 1 2 3] [PE1.1] . step\n", + " 0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step\n", + " 0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step\n", + " 0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n", + " 0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n", + " 0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step\n", + " 0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step\n", + " 3 . 3 [2 1 3 1 2 3] [PE1.1] step\n", + " 3 3 . [2 1 3 1 2 3] [PE1.1] step\n", + " 3 3 [2 1 3 1 2 3] . [PE1.1] step\n", + " 3 3 [2 1 3 1 2 3] [PE1.1] . step\n", + " 3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step\n", + " 3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step\n", + " 3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step\n", + " 3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step\n", + " 3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step\n", + " 3 5 . + 5 [1 3 1 2 3] [PE1.1] step\n", + " 8 . 5 [1 3 1 2 3] [PE1.1] step\n", + " 8 5 . [1 3 1 2 3] [PE1.1] step\n", + " 8 5 [1 3 1 2 3] . [PE1.1] step\n", + " 8 5 [1 3 1 2 3] [PE1.1] . step\n", + " 8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step\n", + " 8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step\n", + " 8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step\n", + " 8 6 . [+] dupdip [3 1 2 3] [PE1.1] step\n", + " 8 6 [+] . dupdip [3 1 2 3] [PE1.1] step\n", + " 8 6 . + 6 [3 1 2 3] [PE1.1] step\n", + " 14 . 6 [3 1 2 3] [PE1.1] step\n", + " 14 6 . [3 1 2 3] [PE1.1] step\n", + " 14 6 [3 1 2 3] . [PE1.1] step\n", + " 14 6 [3 1 2 3] [PE1.1] . step\n", + " 14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step\n", + " 14 6 3 . PE1.1 [1 2 3] [PE1.1] step\n", + " 14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step\n", + " 14 9 . [+] dupdip [1 2 3] [PE1.1] step\n", + " 14 9 [+] . dupdip [1 2 3] [PE1.1] step\n", + " 14 9 . + 9 [1 2 3] [PE1.1] step\n", + " 23 . 9 [1 2 3] [PE1.1] step\n", + " 23 9 . [1 2 3] [PE1.1] step\n", + " 23 9 [1 2 3] . [PE1.1] step\n", + " 23 9 [1 2 3] [PE1.1] . step\n", + " 23 9 1 [PE1.1] . i [2 3] [PE1.1] step\n", + " 23 9 1 . PE1.1 [2 3] [PE1.1] step\n", + " 23 9 1 . + [+] dupdip [2 3] [PE1.1] step\n", + " 23 10 . [+] dupdip [2 3] [PE1.1] step\n", + " 23 10 [+] . dupdip [2 3] [PE1.1] step\n", + " 23 10 . + 10 [2 3] [PE1.1] step\n", + " 33 . 10 [2 3] [PE1.1] step\n", + " 33 10 . [2 3] [PE1.1] step\n", + " 33 10 [2 3] . [PE1.1] step\n", + " 33 10 [2 3] [PE1.1] . step\n", + " 33 10 2 [PE1.1] . i [3] [PE1.1] step\n", + " 33 10 2 . PE1.1 [3] [PE1.1] step\n", + " 33 10 2 . + [+] dupdip [3] [PE1.1] step\n", + " 33 12 . [+] dupdip [3] [PE1.1] step\n", + " 33 12 [+] . dupdip [3] [PE1.1] step\n", + " 33 12 . + 12 [3] [PE1.1] step\n", + " 45 . 12 [3] [PE1.1] step\n", + " 45 12 . [3] [PE1.1] step\n", + " 45 12 [3] . [PE1.1] step\n", + " 45 12 [3] [PE1.1] . step\n", + " 45 12 3 [PE1.1] . i\n", + " 45 12 3 . PE1.1\n", + " 45 12 3 . + [+] dupdip\n", + " 45 15 . [+] dupdip\n", + " 45 15 [+] . dupdip\n", + " 45 15 . + 15\n", + " 60 . 15\n", + " 60 15 . \n" + ] + } + ], + "source": [ + "V('0 0 [3 2 1 3 1 2 3] [PE1.1] step')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So one `step` through all seven terms brings the counter to 15 and the total to 60." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "66" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1000 / 15" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "990" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "66 * 15" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1000 - 990" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We only want the terms *less than* 1000." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "999 - 990" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "That means we want to run the full list of numbers sixty-six times to get to 990 and then the first four numbers 3 2 1 3 to get to 999." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168\n" + ] + } + ], + "source": [ + "J('PE1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This form uses no extra storage and produces no unused summands. It's\n", + "good but there's one more trick we can apply. The list of seven terms\n", + "takes up at least seven bytes. But notice that all of the terms are less\n", + "than four, and so each can fit in just two bits. We could store all\n", + "seven terms in just fourteen bits and use masking and shifts to pick out\n", + "each term as we go. This will use less space and save time loading whole\n", + "integer terms from the list.\n", + "\n", + " 3 2 1 3 1 2 3\n", + " 0b 11 10 01 11 01 10 11 == 14811" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14811" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0b11100111011011" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.2 == [3 & PE1.1] dupdip 2 >>')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 0 0 14811 PE1.2\n", + " 0 . 0 14811 PE1.2\n", + " 0 0 . 14811 PE1.2\n", + " 0 0 14811 . PE1.2\n", + " 0 0 14811 . [3 & PE1.1] dupdip 2 >>\n", + "0 0 14811 [3 & PE1.1] . dupdip 2 >>\n", + " 0 0 14811 . 3 & PE1.1 14811 2 >>\n", + " 0 0 14811 3 . & PE1.1 14811 2 >>\n", + " 0 0 3 . PE1.1 14811 2 >>\n", + " 0 0 3 . + [+] dupdip 14811 2 >>\n", + " 0 3 . [+] dupdip 14811 2 >>\n", + " 0 3 [+] . dupdip 14811 2 >>\n", + " 0 3 . + 3 14811 2 >>\n", + " 3 . 3 14811 2 >>\n", + " 3 3 . 14811 2 >>\n", + " 3 3 14811 . 2 >>\n", + " 3 3 14811 2 . >>\n", + " 3 3 3702 . \n" + ] + } + ], + "source": [ + "V('0 0 14811 PE1.2')" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 3 3 3702 PE1.2\n", + " 3 . 3 3702 PE1.2\n", + " 3 3 . 3702 PE1.2\n", + " 3 3 3702 . PE1.2\n", + " 3 3 3702 . [3 & PE1.1] dupdip 2 >>\n", + "3 3 3702 [3 & PE1.1] . dupdip 2 >>\n", + " 3 3 3702 . 3 & PE1.1 3702 2 >>\n", + " 3 3 3702 3 . & PE1.1 3702 2 >>\n", + " 3 3 2 . PE1.1 3702 2 >>\n", + " 3 3 2 . + [+] dupdip 3702 2 >>\n", + " 3 5 . [+] dupdip 3702 2 >>\n", + " 3 5 [+] . dupdip 3702 2 >>\n", + " 3 5 . + 5 3702 2 >>\n", + " 8 . 5 3702 2 >>\n", + " 8 5 . 3702 2 >>\n", + " 8 5 3702 . 2 >>\n", + " 8 5 3702 2 . >>\n", + " 8 5 925 . \n" + ] + } + ], + "source": [ + "V('3 3 3702 PE1.2')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 0 0 14811 7 [PE1.2] times pop\n", + " 0 . 0 14811 7 [PE1.2] times pop\n", + " 0 0 . 14811 7 [PE1.2] times pop\n", + " 0 0 14811 . 7 [PE1.2] times pop\n", + " 0 0 14811 7 . [PE1.2] times pop\n", + " 0 0 14811 7 [PE1.2] . times pop\n", + " 0 0 14811 [PE1.2] . i 6 [PE1.2] times pop\n", + " 0 0 14811 . PE1.2 6 [PE1.2] times pop\n", + " 0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop\n", + "0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop\n", + " 0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop\n", + " 0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop\n", + " 0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop\n", + " 0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop\n", + " 0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop\n", + " 0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop\n", + " 0 3 . + 3 14811 2 >> 6 [PE1.2] times pop\n", + " 3 . 3 14811 2 >> 6 [PE1.2] times pop\n", + " 3 3 . 14811 2 >> 6 [PE1.2] times pop\n", + " 3 3 14811 . 2 >> 6 [PE1.2] times pop\n", + " 3 3 14811 2 . >> 6 [PE1.2] times pop\n", + " 3 3 3702 . 6 [PE1.2] times pop\n", + " 3 3 3702 6 . [PE1.2] times pop\n", + " 3 3 3702 6 [PE1.2] . times pop\n", + " 3 3 3702 [PE1.2] . i 5 [PE1.2] times pop\n", + " 3 3 3702 . PE1.2 5 [PE1.2] times pop\n", + " 3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop\n", + " 3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop\n", + " 3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop\n", + " 3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop\n", + " 3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop\n", + " 3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop\n", + " 3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop\n", + " 3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop\n", + " 3 5 . + 5 3702 2 >> 5 [PE1.2] times pop\n", + " 8 . 5 3702 2 >> 5 [PE1.2] times pop\n", + " 8 5 . 3702 2 >> 5 [PE1.2] times pop\n", + " 8 5 3702 . 2 >> 5 [PE1.2] times pop\n", + " 8 5 3702 2 . >> 5 [PE1.2] times pop\n", + " 8 5 925 . 5 [PE1.2] times pop\n", + " 8 5 925 5 . [PE1.2] times pop\n", + " 8 5 925 5 [PE1.2] . times pop\n", + " 8 5 925 [PE1.2] . i 4 [PE1.2] times pop\n", + " 8 5 925 . PE1.2 4 [PE1.2] times pop\n", + " 8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop\n", + " 8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop\n", + " 8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop\n", + " 8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop\n", + " 8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop\n", + " 8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop\n", + " 8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop\n", + " 8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop\n", + " 8 6 . + 6 925 2 >> 4 [PE1.2] times pop\n", + " 14 . 6 925 2 >> 4 [PE1.2] times pop\n", + " 14 6 . 925 2 >> 4 [PE1.2] times pop\n", + " 14 6 925 . 2 >> 4 [PE1.2] times pop\n", + " 14 6 925 2 . >> 4 [PE1.2] times pop\n", + " 14 6 231 . 4 [PE1.2] times pop\n", + " 14 6 231 4 . [PE1.2] times pop\n", + " 14 6 231 4 [PE1.2] . times pop\n", + " 14 6 231 [PE1.2] . i 3 [PE1.2] times pop\n", + " 14 6 231 . PE1.2 3 [PE1.2] times pop\n", + " 14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop\n", + " 14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop\n", + " 14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop\n", + " 14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop\n", + " 14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop\n", + " 14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop\n", + " 14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop\n", + " 14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop\n", + " 14 9 . + 9 231 2 >> 3 [PE1.2] times pop\n", + " 23 . 9 231 2 >> 3 [PE1.2] times pop\n", + " 23 9 . 231 2 >> 3 [PE1.2] times pop\n", + " 23 9 231 . 2 >> 3 [PE1.2] times pop\n", + " 23 9 231 2 . >> 3 [PE1.2] times pop\n", + " 23 9 57 . 3 [PE1.2] times pop\n", + " 23 9 57 3 . [PE1.2] times pop\n", + " 23 9 57 3 [PE1.2] . times pop\n", + " 23 9 57 [PE1.2] . i 2 [PE1.2] times pop\n", + " 23 9 57 . PE1.2 2 [PE1.2] times pop\n", + " 23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop\n", + " 23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop\n", + " 23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop\n", + " 23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop\n", + " 23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop\n", + " 23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop\n", + " 23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop\n", + " 23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop\n", + " 23 10 . + 10 57 2 >> 2 [PE1.2] times pop\n", + " 33 . 10 57 2 >> 2 [PE1.2] times pop\n", + " 33 10 . 57 2 >> 2 [PE1.2] times pop\n", + " 33 10 57 . 2 >> 2 [PE1.2] times pop\n", + " 33 10 57 2 . >> 2 [PE1.2] times pop\n", + " 33 10 14 . 2 [PE1.2] times pop\n", + " 33 10 14 2 . [PE1.2] times pop\n", + " 33 10 14 2 [PE1.2] . times pop\n", + " 33 10 14 [PE1.2] . i 1 [PE1.2] times pop\n", + " 33 10 14 . PE1.2 1 [PE1.2] times pop\n", + " 33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop\n", + " 33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop\n", + " 33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop\n", + " 33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop\n", + " 33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop\n", + " 33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop\n", + " 33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop\n", + " 33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop\n", + " 33 12 . + 12 14 2 >> 1 [PE1.2] times pop\n", + " 45 . 12 14 2 >> 1 [PE1.2] times pop\n", + " 45 12 . 14 2 >> 1 [PE1.2] times pop\n", + " 45 12 14 . 2 >> 1 [PE1.2] times pop\n", + " 45 12 14 2 . >> 1 [PE1.2] times pop\n", + " 45 12 3 . 1 [PE1.2] times pop\n", + " 45 12 3 1 . [PE1.2] times pop\n", + " 45 12 3 1 [PE1.2] . times pop\n", + " 45 12 3 [PE1.2] . i pop\n", + " 45 12 3 . PE1.2 pop\n", + " 45 12 3 . [3 & PE1.1] dupdip 2 >> pop\n", + " 45 12 3 [3 & PE1.1] . dupdip 2 >> pop\n", + " 45 12 3 . 3 & PE1.1 3 2 >> pop\n", + " 45 12 3 3 . & PE1.1 3 2 >> pop\n", + " 45 12 3 . PE1.1 3 2 >> pop\n", + " 45 12 3 . + [+] dupdip 3 2 >> pop\n", + " 45 15 . [+] dupdip 3 2 >> pop\n", + " 45 15 [+] . dupdip 3 2 >> pop\n", + " 45 15 . + 15 3 2 >> pop\n", + " 60 . 15 3 2 >> pop\n", + " 60 15 . 3 2 >> pop\n", + " 60 15 3 . 2 >> pop\n", + " 60 15 3 2 . >> pop\n", + " 60 15 0 . pop\n", + " 60 15 . \n" + ] + } + ], + "source": [ + "V('0 0 14811 7 [PE1.2] times pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And so we have at last:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168\n" + ] + } + ], + "source": [ + "J('PE1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's refactor.\n", + "\n", + " 14811 7 [PE1.2] times pop\n", + " 14811 4 [PE1.2] times pop\n", + " 14811 n [PE1.2] times pop\n", + " n 14811 swap [PE1.2] times pop" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.3 == 14811 swap [PE1.2] times pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can simplify the definition above:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop')" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168\n" + ] + } + ], + "source": [ + "J('PE1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here's our joy program all in one place. It doesn't make so much sense, but if you have read through the above description of how it was derived I hope it's clear.\n", + "\n", + " PE1.1 == + [+] dupdip\n", + " PE1.2 == [3 & PE1.1] dupdip 2 >>\n", + " PE1.3 == 14811 swap [PE1.2] times pop\n", + " PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Generator Version\n", + "It's a little clunky iterating sixty-six times though the seven numbers then four more. In the _Generator Programs_ notebook we derive a generator that can be repeatedly driven by the `x` combinator to produce a stream of the seven numbers repeating over and over again." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]\n" + ] + } + ], + "source": [ + "J('PE1.terms 21 [x] times')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We know from above that we need sixty-six times seven then four more terms to reach up to but not over one thousand." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "466\n" + ] + } + ], + "source": [ + "J('7 66 * 4 +')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Here they are..." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3\n" + ] + } + ], + "source": [ + "J('PE1.terms 466 [x] times pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### ...and they do sum to 999." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "999\n" + ] + } + ], + "source": [ + "J('[PE1.terms 466 [x] times pop] run sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can use `PE1.1` to accumulate the terms as we go, and then `pop` the generator and the counter from the stack when we're done, leaving just the sum." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168\n" + ] + } + ], + "source": [ + "J('0 0 PE1.terms 466 [x [PE1.1] dip] times popop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# A little further analysis renders iteration unnecessary.\n", + "Consider finding the sum of the positive integers less than or equal to ten." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], + "source": [ + "J('[10 9 8 7 6 5 4 3 2 1] sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Instead of summing them, [observe](https://en.wikipedia.org/wiki/File:Animated_proof_for_the_formula_giving_the_sum_of_the_first_integers_1%2B2%2B...%2Bn.gif):\n", + "\n", + " 10 9 8 7 6\n", + " + 1 2 3 4 5\n", + " ---- -- -- -- --\n", + " 11 11 11 11 11\n", + " \n", + " 11 * 5 = 55\n", + "\n", + "From the above example we can deduce that the sum of the first N positive integers is:\n", + "\n", + " (N + 1) * N / 2 \n", + "\n", + "(The formula also works for odd values of N, I'll leave that to you if you want to work it out or you can take my word for it.)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "define('F == dup ++ * 2 floordiv')" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 10 F\n", + " 10 . F\n", + " 10 . dup ++ * 2 floordiv\n", + "10 10 . ++ * 2 floordiv\n", + "10 11 . * 2 floordiv\n", + " 110 . 2 floordiv\n", + "110 2 . floordiv\n", + " 55 . \n" + ] + } + ], + "source": [ + "V('10 F')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Generalizing to Blocks of Terms\n", + "We can apply the same reasoning to the PE1 problem.\n", + "\n", + "Between 0 and 990 inclusive there are sixty-six \"blocks\" of seven terms each, starting with:\n", + "\n", + " [3 5 6 9 10 12 15]\n", + " \n", + "And ending with:\n", + "\n", + " [978 980 981 984 985 987 990]\n", + " \n", + "If we reverse one of these two blocks and sum pairs..." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[978 15] [980 12] [981 10] [984 9] [985 6] [987 5] [990 3]]\n" + ] + } + ], + "source": [ + "J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip')" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[993 992 991 993 991 992 993]\n" + ] + } + ], + "source": [ + "J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "(Interesting that the sequence of seven numbers appears again in the rightmost digit of each term.)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6945\n" + ] + } + ], + "source": [ + "J('[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Since there are sixty-six blocks and we are pairing them up, there must be thirty-three pairs, each of which sums to 6945. We also have these additional unpaired terms between 990 and 1000:\n", + "\n", + " 993 995 996 999\n", + " \n", + "So we can give the \"sum of all the multiples of 3 or 5 below 1000\" like so:" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168\n" + ] + } + ], + "source": [ + "J('6945 33 * [993 995 996 999] cons sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It's worth noting, I think, that this same reasoning holds for any two numbers $n$ and $m$ the multiples of which we hope to sum. The multiples would have a cycle of differences of length $k$ and so we could compute the sum of $Nk$ multiples as above.\n", + "\n", + "The sequence of differences will always be a palidrome. Consider an interval spanning the least common multiple of $n$ and $m$:\n", + "\n", + " | | | | | | | |\n", + " | | | | |\n", + " \n", + "Here we have 4 and 7, and you can read off the sequence of differences directly from the diagram: 4 3 1 4 2 2 4 1 3 4.\n", + " \n", + "Geometrically, the actual values of $n$ and $m$ and their *lcm* don't matter, the pattern they make will always be symmetrical around its midpoint. The same reasoning holds for multiples of more than two numbers." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The Simplest Program" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Of course, the simplest joy program for the first Project Euler problem is just:\n", + "\n", + " PE1 == 233168\n", + "\n", + "Fin." + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/3._Developing_a_Program.md b/docs/3._Developing_a_Program.md new file mode 100644 index 0000000..f68f30a --- /dev/null +++ b/docs/3._Developing_a_Program.md @@ -0,0 +1,694 @@ + +# [Project Euler, first problem: "Multiples of 3 and 5"](https://projecteuler.net/problem=1) + + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. + + Find the sum of all the multiples of 3 or 5 below 1000. + + +```python +from notebook_preamble import J, V, define +``` + +Let's create a predicate that returns `True` if a number is a multiple of 3 or 5 and `False` otherwise. + + +```python +define('P == [3 % not] dupdip 5 % not or') +``` + + +```python +V('80 P') +``` + + . 80 P + 80 . P + 80 . [3 % not] dupdip 5 % not or + 80 [3 % not] . dupdip 5 % not or + 80 . 3 % not 80 5 % not or + 80 3 . % not 80 5 % not or + 2 . not 80 5 % not or + False . 80 5 % not or + False 80 . 5 % not or + False 80 5 . % not or + False 0 . not or + False True . or + True . + + +Given the predicate function `P` a suitable program is: + + PE1 == 1000 range [P] filter sum + +This function generates a list of the integers from 0 to 999, filters +that list by `P`, and then sums the result. + +Logically this is fine, but pragmatically we are doing more work than we +should be; we generate one thousand integers but actually use less than +half of them. A better solution would be to generate just the multiples +we want to sum, and to add them as we go rather than storing them and +adding summing them at the end. + +At first I had the idea to use two counters and increase them by three +and five, respectively. This way we only generate the terms that we +actually want to sum. We have to proceed by incrementing the counter +that is lower, or if they are equal, the three counter, and we have to +take care not to double add numbers like 15 that are multiples of both +three and five. + +This seemed a little clunky, so I tried a different approach. + +Consider the first few terms in the series: + + 3 5 6 9 10 12 15 18 20 21 ... + +Subtract each number from the one after it (subtracting 0 from 3): + + 3 5 6 9 10 12 15 18 20 21 24 25 27 30 ... + 0 3 5 6 9 10 12 15 18 20 21 24 25 27 ... + ------------------------------------------- + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 ... + +You get this lovely repeating palindromic sequence: + + 3 2 1 3 1 2 3 + +To make a counter that increments by factors of 3 and 5 you just add +these differences to the counter one-by-one in a loop. + + +To make use of this sequence to increment a counter and sum terms as we +go we need a function that will accept the sum, the counter, and the next +term to add, and that adds the term to the counter and a copy of the +counter to the running sum. This function will do that: + + PE1.1 == + [+] dupdip + + +```python +define('PE1.1 == + [+] dupdip') +``` + + +```python +V('0 0 3 PE1.1') +``` + + . 0 0 3 PE1.1 + 0 . 0 3 PE1.1 + 0 0 . 3 PE1.1 + 0 0 3 . PE1.1 + 0 0 3 . + [+] dupdip + 0 3 . [+] dupdip + 0 3 [+] . dupdip + 0 3 . + 3 + 3 . 3 + 3 3 . + + + +```python +V('0 0 [3 2 1 3 1 2 3] [PE1.1] step') +``` + + . 0 0 [3 2 1 3 1 2 3] [PE1.1] step + 0 . 0 [3 2 1 3 1 2 3] [PE1.1] step + 0 0 . [3 2 1 3 1 2 3] [PE1.1] step + 0 0 [3 2 1 3 1 2 3] . [PE1.1] step + 0 0 [3 2 1 3 1 2 3] [PE1.1] . step + 0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step + 0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step + 0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step + 0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step + 0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step + 0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step + 3 . 3 [2 1 3 1 2 3] [PE1.1] step + 3 3 . [2 1 3 1 2 3] [PE1.1] step + 3 3 [2 1 3 1 2 3] . [PE1.1] step + 3 3 [2 1 3 1 2 3] [PE1.1] . step + 3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step + 3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step + 3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step + 3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step + 3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step + 3 5 . + 5 [1 3 1 2 3] [PE1.1] step + 8 . 5 [1 3 1 2 3] [PE1.1] step + 8 5 . [1 3 1 2 3] [PE1.1] step + 8 5 [1 3 1 2 3] . [PE1.1] step + 8 5 [1 3 1 2 3] [PE1.1] . step + 8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step + 8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step + 8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step + 8 6 . [+] dupdip [3 1 2 3] [PE1.1] step + 8 6 [+] . dupdip [3 1 2 3] [PE1.1] step + 8 6 . + 6 [3 1 2 3] [PE1.1] step + 14 . 6 [3 1 2 3] [PE1.1] step + 14 6 . [3 1 2 3] [PE1.1] step + 14 6 [3 1 2 3] . [PE1.1] step + 14 6 [3 1 2 3] [PE1.1] . step + 14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step + 14 6 3 . PE1.1 [1 2 3] [PE1.1] step + 14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step + 14 9 . [+] dupdip [1 2 3] [PE1.1] step + 14 9 [+] . dupdip [1 2 3] [PE1.1] step + 14 9 . + 9 [1 2 3] [PE1.1] step + 23 . 9 [1 2 3] [PE1.1] step + 23 9 . [1 2 3] [PE1.1] step + 23 9 [1 2 3] . [PE1.1] step + 23 9 [1 2 3] [PE1.1] . step + 23 9 1 [PE1.1] . i [2 3] [PE1.1] step + 23 9 1 . PE1.1 [2 3] [PE1.1] step + 23 9 1 . + [+] dupdip [2 3] [PE1.1] step + 23 10 . [+] dupdip [2 3] [PE1.1] step + 23 10 [+] . dupdip [2 3] [PE1.1] step + 23 10 . + 10 [2 3] [PE1.1] step + 33 . 10 [2 3] [PE1.1] step + 33 10 . [2 3] [PE1.1] step + 33 10 [2 3] . [PE1.1] step + 33 10 [2 3] [PE1.1] . step + 33 10 2 [PE1.1] . i [3] [PE1.1] step + 33 10 2 . PE1.1 [3] [PE1.1] step + 33 10 2 . + [+] dupdip [3] [PE1.1] step + 33 12 . [+] dupdip [3] [PE1.1] step + 33 12 [+] . dupdip [3] [PE1.1] step + 33 12 . + 12 [3] [PE1.1] step + 45 . 12 [3] [PE1.1] step + 45 12 . [3] [PE1.1] step + 45 12 [3] . [PE1.1] step + 45 12 [3] [PE1.1] . step + 45 12 3 [PE1.1] . i + 45 12 3 . PE1.1 + 45 12 3 . + [+] dupdip + 45 15 . [+] dupdip + 45 15 [+] . dupdip + 45 15 . + 15 + 60 . 15 + 60 15 . + + +So one `step` through all seven terms brings the counter to 15 and the total to 60. + + +```python +1000 / 15 +``` + + + + + 66 + + + + +```python +66 * 15 +``` + + + + + 990 + + + + +```python +1000 - 990 +``` + + + + + 10 + + + +We only want the terms *less than* 1000. + + +```python +999 - 990 +``` + + + + + 9 + + + +That means we want to run the full list of numbers sixty-six times to get to 990 and then the first four numbers 3 2 1 3 to get to 999. + + +```python +define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop') +``` + + +```python +J('PE1') +``` + + 233168 + + +This form uses no extra storage and produces no unused summands. It's +good but there's one more trick we can apply. The list of seven terms +takes up at least seven bytes. But notice that all of the terms are less +than four, and so each can fit in just two bits. We could store all +seven terms in just fourteen bits and use masking and shifts to pick out +each term as we go. This will use less space and save time loading whole +integer terms from the list. + + 3 2 1 3 1 2 3 + 0b 11 10 01 11 01 10 11 == 14811 + + +```python +0b11100111011011 +``` + + + + + 14811 + + + + +```python +define('PE1.2 == [3 & PE1.1] dupdip 2 >>') +``` + + +```python +V('0 0 14811 PE1.2') +``` + + . 0 0 14811 PE1.2 + 0 . 0 14811 PE1.2 + 0 0 . 14811 PE1.2 + 0 0 14811 . PE1.2 + 0 0 14811 . [3 & PE1.1] dupdip 2 >> + 0 0 14811 [3 & PE1.1] . dupdip 2 >> + 0 0 14811 . 3 & PE1.1 14811 2 >> + 0 0 14811 3 . & PE1.1 14811 2 >> + 0 0 3 . PE1.1 14811 2 >> + 0 0 3 . + [+] dupdip 14811 2 >> + 0 3 . [+] dupdip 14811 2 >> + 0 3 [+] . dupdip 14811 2 >> + 0 3 . + 3 14811 2 >> + 3 . 3 14811 2 >> + 3 3 . 14811 2 >> + 3 3 14811 . 2 >> + 3 3 14811 2 . >> + 3 3 3702 . + + + +```python +V('3 3 3702 PE1.2') +``` + + . 3 3 3702 PE1.2 + 3 . 3 3702 PE1.2 + 3 3 . 3702 PE1.2 + 3 3 3702 . PE1.2 + 3 3 3702 . [3 & PE1.1] dupdip 2 >> + 3 3 3702 [3 & PE1.1] . dupdip 2 >> + 3 3 3702 . 3 & PE1.1 3702 2 >> + 3 3 3702 3 . & PE1.1 3702 2 >> + 3 3 2 . PE1.1 3702 2 >> + 3 3 2 . + [+] dupdip 3702 2 >> + 3 5 . [+] dupdip 3702 2 >> + 3 5 [+] . dupdip 3702 2 >> + 3 5 . + 5 3702 2 >> + 8 . 5 3702 2 >> + 8 5 . 3702 2 >> + 8 5 3702 . 2 >> + 8 5 3702 2 . >> + 8 5 925 . + + + +```python +V('0 0 14811 7 [PE1.2] times pop') +``` + + . 0 0 14811 7 [PE1.2] times pop + 0 . 0 14811 7 [PE1.2] times pop + 0 0 . 14811 7 [PE1.2] times pop + 0 0 14811 . 7 [PE1.2] times pop + 0 0 14811 7 . [PE1.2] times pop + 0 0 14811 7 [PE1.2] . times pop + 0 0 14811 [PE1.2] . i 6 [PE1.2] times pop + 0 0 14811 . PE1.2 6 [PE1.2] times pop + 0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop + 0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop + 0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop + 0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop + 0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop + 0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop + 0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop + 0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop + 0 3 . + 3 14811 2 >> 6 [PE1.2] times pop + 3 . 3 14811 2 >> 6 [PE1.2] times pop + 3 3 . 14811 2 >> 6 [PE1.2] times pop + 3 3 14811 . 2 >> 6 [PE1.2] times pop + 3 3 14811 2 . >> 6 [PE1.2] times pop + 3 3 3702 . 6 [PE1.2] times pop + 3 3 3702 6 . [PE1.2] times pop + 3 3 3702 6 [PE1.2] . times pop + 3 3 3702 [PE1.2] . i 5 [PE1.2] times pop + 3 3 3702 . PE1.2 5 [PE1.2] times pop + 3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop + 3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop + 3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop + 3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop + 3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop + 3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop + 3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop + 3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop + 3 5 . + 5 3702 2 >> 5 [PE1.2] times pop + 8 . 5 3702 2 >> 5 [PE1.2] times pop + 8 5 . 3702 2 >> 5 [PE1.2] times pop + 8 5 3702 . 2 >> 5 [PE1.2] times pop + 8 5 3702 2 . >> 5 [PE1.2] times pop + 8 5 925 . 5 [PE1.2] times pop + 8 5 925 5 . [PE1.2] times pop + 8 5 925 5 [PE1.2] . times pop + 8 5 925 [PE1.2] . i 4 [PE1.2] times pop + 8 5 925 . PE1.2 4 [PE1.2] times pop + 8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop + 8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop + 8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop + 8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop + 8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop + 8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop + 8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop + 8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop + 8 6 . + 6 925 2 >> 4 [PE1.2] times pop + 14 . 6 925 2 >> 4 [PE1.2] times pop + 14 6 . 925 2 >> 4 [PE1.2] times pop + 14 6 925 . 2 >> 4 [PE1.2] times pop + 14 6 925 2 . >> 4 [PE1.2] times pop + 14 6 231 . 4 [PE1.2] times pop + 14 6 231 4 . [PE1.2] times pop + 14 6 231 4 [PE1.2] . times pop + 14 6 231 [PE1.2] . i 3 [PE1.2] times pop + 14 6 231 . PE1.2 3 [PE1.2] times pop + 14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop + 14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop + 14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop + 14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop + 14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop + 14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop + 14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop + 14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop + 14 9 . + 9 231 2 >> 3 [PE1.2] times pop + 23 . 9 231 2 >> 3 [PE1.2] times pop + 23 9 . 231 2 >> 3 [PE1.2] times pop + 23 9 231 . 2 >> 3 [PE1.2] times pop + 23 9 231 2 . >> 3 [PE1.2] times pop + 23 9 57 . 3 [PE1.2] times pop + 23 9 57 3 . [PE1.2] times pop + 23 9 57 3 [PE1.2] . times pop + 23 9 57 [PE1.2] . i 2 [PE1.2] times pop + 23 9 57 . PE1.2 2 [PE1.2] times pop + 23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop + 23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop + 23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop + 23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop + 23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop + 23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop + 23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop + 23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop + 23 10 . + 10 57 2 >> 2 [PE1.2] times pop + 33 . 10 57 2 >> 2 [PE1.2] times pop + 33 10 . 57 2 >> 2 [PE1.2] times pop + 33 10 57 . 2 >> 2 [PE1.2] times pop + 33 10 57 2 . >> 2 [PE1.2] times pop + 33 10 14 . 2 [PE1.2] times pop + 33 10 14 2 . [PE1.2] times pop + 33 10 14 2 [PE1.2] . times pop + 33 10 14 [PE1.2] . i 1 [PE1.2] times pop + 33 10 14 . PE1.2 1 [PE1.2] times pop + 33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop + 33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop + 33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop + 33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop + 33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop + 33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop + 33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop + 33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop + 33 12 . + 12 14 2 >> 1 [PE1.2] times pop + 45 . 12 14 2 >> 1 [PE1.2] times pop + 45 12 . 14 2 >> 1 [PE1.2] times pop + 45 12 14 . 2 >> 1 [PE1.2] times pop + 45 12 14 2 . >> 1 [PE1.2] times pop + 45 12 3 . 1 [PE1.2] times pop + 45 12 3 1 . [PE1.2] times pop + 45 12 3 1 [PE1.2] . times pop + 45 12 3 [PE1.2] . i pop + 45 12 3 . PE1.2 pop + 45 12 3 . [3 & PE1.1] dupdip 2 >> pop + 45 12 3 [3 & PE1.1] . dupdip 2 >> pop + 45 12 3 . 3 & PE1.1 3 2 >> pop + 45 12 3 3 . & PE1.1 3 2 >> pop + 45 12 3 . PE1.1 3 2 >> pop + 45 12 3 . + [+] dupdip 3 2 >> pop + 45 15 . [+] dupdip 3 2 >> pop + 45 15 [+] . dupdip 3 2 >> pop + 45 15 . + 15 3 2 >> pop + 60 . 15 3 2 >> pop + 60 15 . 3 2 >> pop + 60 15 3 . 2 >> pop + 60 15 3 2 . >> pop + 60 15 0 . pop + 60 15 . + + +And so we have at last: + + +```python +define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop') +``` + + +```python +J('PE1') +``` + + 233168 + + +Let's refactor. + + 14811 7 [PE1.2] times pop + 14811 4 [PE1.2] times pop + 14811 n [PE1.2] times pop + n 14811 swap [PE1.2] times pop + + +```python +define('PE1.3 == 14811 swap [PE1.2] times pop') +``` + +Now we can simplify the definition above: + + +```python +define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop') +``` + + +```python +J('PE1') +``` + + 233168 + + +Here's our joy program all in one place. It doesn't make so much sense, but if you have read through the above description of how it was derived I hope it's clear. + + PE1.1 == + [+] dupdip + PE1.2 == [3 & PE1.1] dupdip 2 >> + PE1.3 == 14811 swap [PE1.2] times pop + PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop + +# Generator Version +It's a little clunky iterating sixty-six times though the seven numbers then four more. In the _Generator Programs_ notebook we derive a generator that can be repeatedly driven by the `x` combinator to produce a stream of the seven numbers repeating over and over again. + + +```python +define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]') +``` + + +```python +J('PE1.terms 21 [x] times') +``` + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons] + + +We know from above that we need sixty-six times seven then four more terms to reach up to but not over one thousand. + + +```python +J('7 66 * 4 +') +``` + + 466 + + +### Here they are... + + +```python +J('PE1.terms 466 [x] times pop') +``` + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 + + +### ...and they do sum to 999. + + +```python +J('[PE1.terms 466 [x] times pop] run sum') +``` + + 999 + + +Now we can use `PE1.1` to accumulate the terms as we go, and then `pop` the generator and the counter from the stack when we're done, leaving just the sum. + + +```python +J('0 0 PE1.terms 466 [x [PE1.1] dip] times popop') +``` + + 233168 + + +# A little further analysis renders iteration unnecessary. +Consider finding the sum of the positive integers less than or equal to ten. + + +```python +J('[10 9 8 7 6 5 4 3 2 1] sum') +``` + + 55 + + +Instead of summing them, [observe](https://en.wikipedia.org/wiki/File:Animated_proof_for_the_formula_giving_the_sum_of_the_first_integers_1%2B2%2B...%2Bn.gif): + + 10 9 8 7 6 + + 1 2 3 4 5 + ---- -- -- -- -- + 11 11 11 11 11 + + 11 * 5 = 55 + +From the above example we can deduce that the sum of the first N positive integers is: + + (N + 1) * N / 2 + +(The formula also works for odd values of N, I'll leave that to you if you want to work it out or you can take my word for it.) + + +```python +define('F == dup ++ * 2 floordiv') +``` + + +```python +V('10 F') +``` + + . 10 F + 10 . F + 10 . dup ++ * 2 floordiv + 10 10 . ++ * 2 floordiv + 10 11 . * 2 floordiv + 110 . 2 floordiv + 110 2 . floordiv + 55 . + + +## Generalizing to Blocks of Terms +We can apply the same reasoning to the PE1 problem. + +Between 0 and 990 inclusive there are sixty-six "blocks" of seven terms each, starting with: + + [3 5 6 9 10 12 15] + +And ending with: + + [978 980 981 984 985 987 990] + +If we reverse one of these two blocks and sum pairs... + + +```python +J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip') +``` + + [[978 15] [980 12] [981 10] [984 9] [985 6] [987 5] [990 3]] + + + +```python +J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map') +``` + + [993 992 991 993 991 992 993] + + +(Interesting that the sequence of seven numbers appears again in the rightmost digit of each term.) + + +```python +J('[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum') +``` + + 6945 + + +Since there are sixty-six blocks and we are pairing them up, there must be thirty-three pairs, each of which sums to 6945. We also have these additional unpaired terms between 990 and 1000: + + 993 995 996 999 + +So we can give the "sum of all the multiples of 3 or 5 below 1000" like so: + + +```python +J('6945 33 * [993 995 996 999] cons sum') +``` + + 233168 + + +It's worth noting, I think, that this same reasoning holds for any two numbers $n$ and $m$ the multiples of which we hope to sum. The multiples would have a cycle of differences of length $k$ and so we could compute the sum of $Nk$ multiples as above. + +The sequence of differences will always be a palidrome. Consider an interval spanning the least common multiple of $n$ and $m$: + + | | | | | | | | + | | | | | + +Here we have 4 and 7, and you can read off the sequence of differences directly from the diagram: 4 3 1 4 2 2 4 1 3 4. + +Geometrically, the actual values of $n$ and $m$ and their *lcm* don't matter, the pattern they make will always be symmetrical around its midpoint. The same reasoning holds for multiples of more than two numbers. + +# The Simplest Program + +Of course, the simplest joy program for the first Project Euler problem is just: + + PE1 == 233168 + +Fin. diff --git a/docs/3._Developing_a_Program.rst b/docs/3._Developing_a_Program.rst new file mode 100644 index 0000000..29da686 --- /dev/null +++ b/docs/3._Developing_a_Program.rst @@ -0,0 +1,799 @@ + +`Project Euler, first problem: "Multiples of 3 and 5" `__ +============================================================================================= + +:: + + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. + + Find the sum of all the multiples of 3 or 5 below 1000. + +.. code:: ipython2 + + from notebook_preamble import J, V, define + +Let's create a predicate that returns ``True`` if a number is a multiple +of 3 or 5 and ``False`` otherwise. + +.. code:: ipython2 + + define('P == [3 % not] dupdip 5 % not or') + +.. code:: ipython2 + + V('80 P') + + +.. parsed-literal:: + + . 80 P + 80 . P + 80 . [3 % not] dupdip 5 % not or + 80 [3 % not] . dupdip 5 % not or + 80 . 3 % not 80 5 % not or + 80 3 . % not 80 5 % not or + 2 . not 80 5 % not or + False . 80 5 % not or + False 80 . 5 % not or + False 80 5 . % not or + False 0 . not or + False True . or + True . + + +Given the predicate function ``P`` a suitable program is: + +:: + + PE1 == 1000 range [P] filter sum + +This function generates a list of the integers from 0 to 999, filters +that list by ``P``, and then sums the result. + +Logically this is fine, but pragmatically we are doing more work than we +should be; we generate one thousand integers but actually use less than +half of them. A better solution would be to generate just the multiples +we want to sum, and to add them as we go rather than storing them and +adding summing them at the end. + +At first I had the idea to use two counters and increase them by three +and five, respectively. This way we only generate the terms that we +actually want to sum. We have to proceed by incrementing the counter +that is lower, or if they are equal, the three counter, and we have to +take care not to double add numbers like 15 that are multiples of both +three and five. + +This seemed a little clunky, so I tried a different approach. + +Consider the first few terms in the series: + +:: + + 3 5 6 9 10 12 15 18 20 21 ... + +Subtract each number from the one after it (subtracting 0 from 3): + +:: + + 3 5 6 9 10 12 15 18 20 21 24 25 27 30 ... + 0 3 5 6 9 10 12 15 18 20 21 24 25 27 ... + ------------------------------------------- + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 ... + +You get this lovely repeating palindromic sequence: + +:: + + 3 2 1 3 1 2 3 + +To make a counter that increments by factors of 3 and 5 you just add +these differences to the counter one-by-one in a loop. + +To make use of this sequence to increment a counter and sum terms as we +go we need a function that will accept the sum, the counter, and the +next term to add, and that adds the term to the counter and a copy of +the counter to the running sum. This function will do that: + +:: + + PE1.1 == + [+] dupdip + +.. code:: ipython2 + + define('PE1.1 == + [+] dupdip') + +.. code:: ipython2 + + V('0 0 3 PE1.1') + + +.. parsed-literal:: + + . 0 0 3 PE1.1 + 0 . 0 3 PE1.1 + 0 0 . 3 PE1.1 + 0 0 3 . PE1.1 + 0 0 3 . + [+] dupdip + 0 3 . [+] dupdip + 0 3 [+] . dupdip + 0 3 . + 3 + 3 . 3 + 3 3 . + + +.. code:: ipython2 + + V('0 0 [3 2 1 3 1 2 3] [PE1.1] step') + + +.. parsed-literal:: + + . 0 0 [3 2 1 3 1 2 3] [PE1.1] step + 0 . 0 [3 2 1 3 1 2 3] [PE1.1] step + 0 0 . [3 2 1 3 1 2 3] [PE1.1] step + 0 0 [3 2 1 3 1 2 3] . [PE1.1] step + 0 0 [3 2 1 3 1 2 3] [PE1.1] . step + 0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step + 0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step + 0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step + 0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step + 0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step + 0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step + 3 . 3 [2 1 3 1 2 3] [PE1.1] step + 3 3 . [2 1 3 1 2 3] [PE1.1] step + 3 3 [2 1 3 1 2 3] . [PE1.1] step + 3 3 [2 1 3 1 2 3] [PE1.1] . step + 3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step + 3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step + 3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step + 3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step + 3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step + 3 5 . + 5 [1 3 1 2 3] [PE1.1] step + 8 . 5 [1 3 1 2 3] [PE1.1] step + 8 5 . [1 3 1 2 3] [PE1.1] step + 8 5 [1 3 1 2 3] . [PE1.1] step + 8 5 [1 3 1 2 3] [PE1.1] . step + 8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step + 8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step + 8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step + 8 6 . [+] dupdip [3 1 2 3] [PE1.1] step + 8 6 [+] . dupdip [3 1 2 3] [PE1.1] step + 8 6 . + 6 [3 1 2 3] [PE1.1] step + 14 . 6 [3 1 2 3] [PE1.1] step + 14 6 . [3 1 2 3] [PE1.1] step + 14 6 [3 1 2 3] . [PE1.1] step + 14 6 [3 1 2 3] [PE1.1] . step + 14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step + 14 6 3 . PE1.1 [1 2 3] [PE1.1] step + 14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step + 14 9 . [+] dupdip [1 2 3] [PE1.1] step + 14 9 [+] . dupdip [1 2 3] [PE1.1] step + 14 9 . + 9 [1 2 3] [PE1.1] step + 23 . 9 [1 2 3] [PE1.1] step + 23 9 . [1 2 3] [PE1.1] step + 23 9 [1 2 3] . [PE1.1] step + 23 9 [1 2 3] [PE1.1] . step + 23 9 1 [PE1.1] . i [2 3] [PE1.1] step + 23 9 1 . PE1.1 [2 3] [PE1.1] step + 23 9 1 . + [+] dupdip [2 3] [PE1.1] step + 23 10 . [+] dupdip [2 3] [PE1.1] step + 23 10 [+] . dupdip [2 3] [PE1.1] step + 23 10 . + 10 [2 3] [PE1.1] step + 33 . 10 [2 3] [PE1.1] step + 33 10 . [2 3] [PE1.1] step + 33 10 [2 3] . [PE1.1] step + 33 10 [2 3] [PE1.1] . step + 33 10 2 [PE1.1] . i [3] [PE1.1] step + 33 10 2 . PE1.1 [3] [PE1.1] step + 33 10 2 . + [+] dupdip [3] [PE1.1] step + 33 12 . [+] dupdip [3] [PE1.1] step + 33 12 [+] . dupdip [3] [PE1.1] step + 33 12 . + 12 [3] [PE1.1] step + 45 . 12 [3] [PE1.1] step + 45 12 . [3] [PE1.1] step + 45 12 [3] . [PE1.1] step + 45 12 [3] [PE1.1] . step + 45 12 3 [PE1.1] . i + 45 12 3 . PE1.1 + 45 12 3 . + [+] dupdip + 45 15 . [+] dupdip + 45 15 [+] . dupdip + 45 15 . + 15 + 60 . 15 + 60 15 . + + +So one ``step`` through all seven terms brings the counter to 15 and the +total to 60. + +.. code:: ipython2 + + 1000 / 15 + + + + +.. parsed-literal:: + + 66 + + + +.. code:: ipython2 + + 66 * 15 + + + + +.. parsed-literal:: + + 990 + + + +.. code:: ipython2 + + 1000 - 990 + + + + +.. parsed-literal:: + + 10 + + + +We only want the terms *less than* 1000. + +.. code:: ipython2 + + 999 - 990 + + + + +.. parsed-literal:: + + 9 + + + +That means we want to run the full list of numbers sixty-six times to +get to 990 and then the first four numbers 3 2 1 3 to get to 999. + +.. code:: ipython2 + + define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop') + +.. code:: ipython2 + + J('PE1') + + +.. parsed-literal:: + + 233168 + + +This form uses no extra storage and produces no unused summands. It's +good but there's one more trick we can apply. The list of seven terms +takes up at least seven bytes. But notice that all of the terms are less +than four, and so each can fit in just two bits. We could store all +seven terms in just fourteen bits and use masking and shifts to pick out +each term as we go. This will use less space and save time loading whole +integer terms from the list. + +:: + + 3 2 1 3 1 2 3 + 0b 11 10 01 11 01 10 11 == 14811 + +.. code:: ipython2 + + 0b11100111011011 + + + + +.. parsed-literal:: + + 14811 + + + +.. code:: ipython2 + + define('PE1.2 == [3 & PE1.1] dupdip 2 >>') + +.. code:: ipython2 + + V('0 0 14811 PE1.2') + + +.. parsed-literal:: + + . 0 0 14811 PE1.2 + 0 . 0 14811 PE1.2 + 0 0 . 14811 PE1.2 + 0 0 14811 . PE1.2 + 0 0 14811 . [3 & PE1.1] dupdip 2 >> + 0 0 14811 [3 & PE1.1] . dupdip 2 >> + 0 0 14811 . 3 & PE1.1 14811 2 >> + 0 0 14811 3 . & PE1.1 14811 2 >> + 0 0 3 . PE1.1 14811 2 >> + 0 0 3 . + [+] dupdip 14811 2 >> + 0 3 . [+] dupdip 14811 2 >> + 0 3 [+] . dupdip 14811 2 >> + 0 3 . + 3 14811 2 >> + 3 . 3 14811 2 >> + 3 3 . 14811 2 >> + 3 3 14811 . 2 >> + 3 3 14811 2 . >> + 3 3 3702 . + + +.. code:: ipython2 + + V('3 3 3702 PE1.2') + + +.. parsed-literal:: + + . 3 3 3702 PE1.2 + 3 . 3 3702 PE1.2 + 3 3 . 3702 PE1.2 + 3 3 3702 . PE1.2 + 3 3 3702 . [3 & PE1.1] dupdip 2 >> + 3 3 3702 [3 & PE1.1] . dupdip 2 >> + 3 3 3702 . 3 & PE1.1 3702 2 >> + 3 3 3702 3 . & PE1.1 3702 2 >> + 3 3 2 . PE1.1 3702 2 >> + 3 3 2 . + [+] dupdip 3702 2 >> + 3 5 . [+] dupdip 3702 2 >> + 3 5 [+] . dupdip 3702 2 >> + 3 5 . + 5 3702 2 >> + 8 . 5 3702 2 >> + 8 5 . 3702 2 >> + 8 5 3702 . 2 >> + 8 5 3702 2 . >> + 8 5 925 . + + +.. code:: ipython2 + + V('0 0 14811 7 [PE1.2] times pop') + + +.. parsed-literal:: + + . 0 0 14811 7 [PE1.2] times pop + 0 . 0 14811 7 [PE1.2] times pop + 0 0 . 14811 7 [PE1.2] times pop + 0 0 14811 . 7 [PE1.2] times pop + 0 0 14811 7 . [PE1.2] times pop + 0 0 14811 7 [PE1.2] . times pop + 0 0 14811 [PE1.2] . i 6 [PE1.2] times pop + 0 0 14811 . PE1.2 6 [PE1.2] times pop + 0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop + 0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop + 0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop + 0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop + 0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop + 0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop + 0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop + 0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop + 0 3 . + 3 14811 2 >> 6 [PE1.2] times pop + 3 . 3 14811 2 >> 6 [PE1.2] times pop + 3 3 . 14811 2 >> 6 [PE1.2] times pop + 3 3 14811 . 2 >> 6 [PE1.2] times pop + 3 3 14811 2 . >> 6 [PE1.2] times pop + 3 3 3702 . 6 [PE1.2] times pop + 3 3 3702 6 . [PE1.2] times pop + 3 3 3702 6 [PE1.2] . times pop + 3 3 3702 [PE1.2] . i 5 [PE1.2] times pop + 3 3 3702 . PE1.2 5 [PE1.2] times pop + 3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop + 3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop + 3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop + 3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop + 3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop + 3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop + 3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop + 3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop + 3 5 . + 5 3702 2 >> 5 [PE1.2] times pop + 8 . 5 3702 2 >> 5 [PE1.2] times pop + 8 5 . 3702 2 >> 5 [PE1.2] times pop + 8 5 3702 . 2 >> 5 [PE1.2] times pop + 8 5 3702 2 . >> 5 [PE1.2] times pop + 8 5 925 . 5 [PE1.2] times pop + 8 5 925 5 . [PE1.2] times pop + 8 5 925 5 [PE1.2] . times pop + 8 5 925 [PE1.2] . i 4 [PE1.2] times pop + 8 5 925 . PE1.2 4 [PE1.2] times pop + 8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop + 8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop + 8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop + 8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop + 8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop + 8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop + 8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop + 8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop + 8 6 . + 6 925 2 >> 4 [PE1.2] times pop + 14 . 6 925 2 >> 4 [PE1.2] times pop + 14 6 . 925 2 >> 4 [PE1.2] times pop + 14 6 925 . 2 >> 4 [PE1.2] times pop + 14 6 925 2 . >> 4 [PE1.2] times pop + 14 6 231 . 4 [PE1.2] times pop + 14 6 231 4 . [PE1.2] times pop + 14 6 231 4 [PE1.2] . times pop + 14 6 231 [PE1.2] . i 3 [PE1.2] times pop + 14 6 231 . PE1.2 3 [PE1.2] times pop + 14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop + 14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop + 14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop + 14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop + 14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop + 14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop + 14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop + 14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop + 14 9 . + 9 231 2 >> 3 [PE1.2] times pop + 23 . 9 231 2 >> 3 [PE1.2] times pop + 23 9 . 231 2 >> 3 [PE1.2] times pop + 23 9 231 . 2 >> 3 [PE1.2] times pop + 23 9 231 2 . >> 3 [PE1.2] times pop + 23 9 57 . 3 [PE1.2] times pop + 23 9 57 3 . [PE1.2] times pop + 23 9 57 3 [PE1.2] . times pop + 23 9 57 [PE1.2] . i 2 [PE1.2] times pop + 23 9 57 . PE1.2 2 [PE1.2] times pop + 23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop + 23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop + 23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop + 23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop + 23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop + 23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop + 23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop + 23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop + 23 10 . + 10 57 2 >> 2 [PE1.2] times pop + 33 . 10 57 2 >> 2 [PE1.2] times pop + 33 10 . 57 2 >> 2 [PE1.2] times pop + 33 10 57 . 2 >> 2 [PE1.2] times pop + 33 10 57 2 . >> 2 [PE1.2] times pop + 33 10 14 . 2 [PE1.2] times pop + 33 10 14 2 . [PE1.2] times pop + 33 10 14 2 [PE1.2] . times pop + 33 10 14 [PE1.2] . i 1 [PE1.2] times pop + 33 10 14 . PE1.2 1 [PE1.2] times pop + 33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop + 33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop + 33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop + 33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop + 33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop + 33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop + 33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop + 33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop + 33 12 . + 12 14 2 >> 1 [PE1.2] times pop + 45 . 12 14 2 >> 1 [PE1.2] times pop + 45 12 . 14 2 >> 1 [PE1.2] times pop + 45 12 14 . 2 >> 1 [PE1.2] times pop + 45 12 14 2 . >> 1 [PE1.2] times pop + 45 12 3 . 1 [PE1.2] times pop + 45 12 3 1 . [PE1.2] times pop + 45 12 3 1 [PE1.2] . times pop + 45 12 3 [PE1.2] . i pop + 45 12 3 . PE1.2 pop + 45 12 3 . [3 & PE1.1] dupdip 2 >> pop + 45 12 3 [3 & PE1.1] . dupdip 2 >> pop + 45 12 3 . 3 & PE1.1 3 2 >> pop + 45 12 3 3 . & PE1.1 3 2 >> pop + 45 12 3 . PE1.1 3 2 >> pop + 45 12 3 . + [+] dupdip 3 2 >> pop + 45 15 . [+] dupdip 3 2 >> pop + 45 15 [+] . dupdip 3 2 >> pop + 45 15 . + 15 3 2 >> pop + 60 . 15 3 2 >> pop + 60 15 . 3 2 >> pop + 60 15 3 . 2 >> pop + 60 15 3 2 . >> pop + 60 15 0 . pop + 60 15 . + + +And so we have at last: + +.. code:: ipython2 + + define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop') + +.. code:: ipython2 + + J('PE1') + + +.. parsed-literal:: + + 233168 + + +Let's refactor. + +:: + + 14811 7 [PE1.2] times pop + 14811 4 [PE1.2] times pop + 14811 n [PE1.2] times pop + n 14811 swap [PE1.2] times pop + +.. code:: ipython2 + + define('PE1.3 == 14811 swap [PE1.2] times pop') + +Now we can simplify the definition above: + +.. code:: ipython2 + + define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop') + +.. code:: ipython2 + + J('PE1') + + +.. parsed-literal:: + + 233168 + + +Here's our joy program all in one place. It doesn't make so much sense, +but if you have read through the above description of how it was derived +I hope it's clear. + +:: + + PE1.1 == + [+] dupdip + PE1.2 == [3 & PE1.1] dupdip 2 >> + PE1.3 == 14811 swap [PE1.2] times pop + PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop + +Generator Version +================= + +It's a little clunky iterating sixty-six times though the seven numbers +then four more. In the *Generator Programs* notebook we derive a +generator that can be repeatedly driven by the ``x`` combinator to +produce a stream of the seven numbers repeating over and over again. + +.. code:: ipython2 + + define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]') + +.. code:: ipython2 + + J('PE1.terms 21 [x] times') + + +.. parsed-literal:: + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons] + + +We know from above that we need sixty-six times seven then four more +terms to reach up to but not over one thousand. + +.. code:: ipython2 + + J('7 66 * 4 +') + + +.. parsed-literal:: + + 466 + + +Here they are... +~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('PE1.terms 466 [x] times pop') + + +.. parsed-literal:: + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 + + +...and they do sum to 999. +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + J('[PE1.terms 466 [x] times pop] run sum') + + +.. parsed-literal:: + + 999 + + +Now we can use ``PE1.1`` to accumulate the terms as we go, and then +``pop`` the generator and the counter from the stack when we're done, +leaving just the sum. + +.. code:: ipython2 + + J('0 0 PE1.terms 466 [x [PE1.1] dip] times popop') + + +.. parsed-literal:: + + 233168 + + +A little further analysis renders iteration unnecessary. +======================================================== + +Consider finding the sum of the positive integers less than or equal to +ten. + +.. code:: ipython2 + + J('[10 9 8 7 6 5 4 3 2 1] sum') + + +.. parsed-literal:: + + 55 + + +Instead of summing them, +`observe `__: + +:: + + 10 9 8 7 6 + + 1 2 3 4 5 + ---- -- -- -- -- + 11 11 11 11 11 + + 11 * 5 = 55 + +From the above example we can deduce that the sum of the first N +positive integers is: + +:: + + (N + 1) * N / 2 + +(The formula also works for odd values of N, I'll leave that to you if +you want to work it out or you can take my word for it.) + +.. code:: ipython2 + + define('F == dup ++ * 2 floordiv') + +.. code:: ipython2 + + V('10 F') + + +.. parsed-literal:: + + . 10 F + 10 . F + 10 . dup ++ * 2 floordiv + 10 10 . ++ * 2 floordiv + 10 11 . * 2 floordiv + 110 . 2 floordiv + 110 2 . floordiv + 55 . + + +Generalizing to Blocks of Terms +------------------------------- + +We can apply the same reasoning to the PE1 problem. + +Between 0 and 990 inclusive there are sixty-six "blocks" of seven terms +each, starting with: + +:: + + [3 5 6 9 10 12 15] + +And ending with: + +:: + + [978 980 981 984 985 987 990] + +If we reverse one of these two blocks and sum pairs... + +.. code:: ipython2 + + J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip') + + +.. parsed-literal:: + + [[978 15] [980 12] [981 10] [984 9] [985 6] [987 5] [990 3]] + + +.. code:: ipython2 + + J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map') + + +.. parsed-literal:: + + [993 992 991 993 991 992 993] + + +(Interesting that the sequence of seven numbers appears again in the +rightmost digit of each term.) + +.. code:: ipython2 + + J('[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum') + + +.. parsed-literal:: + + 6945 + + +Since there are sixty-six blocks and we are pairing them up, there must +be thirty-three pairs, each of which sums to 6945. We also have these +additional unpaired terms between 990 and 1000: + +:: + + 993 995 996 999 + +So we can give the "sum of all the multiples of 3 or 5 below 1000" like +so: + +.. code:: ipython2 + + J('6945 33 * [993 995 996 999] cons sum') + + +.. parsed-literal:: + + 233168 + + +It's worth noting, I think, that this same reasoning holds for any two +numbers :math:`n` and :math:`m` the multiples of which we hope to sum. +The multiples would have a cycle of differences of length :math:`k` and +so we could compute the sum of :math:`Nk` multiples as above. + +The sequence of differences will always be a palidrome. Consider an +interval spanning the least common multiple of :math:`n` and :math:`m`: + +:: + + | | | | | | | | + | | | | | + +Here we have 4 and 7, and you can read off the sequence of differences +directly from the diagram: 4 3 1 4 2 2 4 1 3 4. + +Geometrically, the actual values of :math:`n` and :math:`m` and their +*lcm* don't matter, the pattern they make will always be symmetrical +around its midpoint. The same reasoning holds for multiples of more than +two numbers. + +The Simplest Program +==================== + +Of course, the simplest joy program for the first Project Euler problem +is just: + +:: + + PE1 == 233168 + +Fin. diff --git a/docs/Advent_of_Code_2017_December_1st.html b/docs/Advent_of_Code_2017_December_1st.html new file mode 100644 index 0000000..f015c9f --- /dev/null +++ b/docs/Advent_of_Code_2017_December_1st.html @@ -0,0 +1,12432 @@ + + + +Advent_of_Code_2017_December_1st + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Advent of Code 2017

December 1st

[Given] a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.

+

For example:

+
    +
  • 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
  • +
  • 1111 produces 4 because each digit (all 1) matches the next.
  • +
  • 1234 produces 0 because no digit matches the next.
  • +
  • 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
  • +
+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

I'll assume the input is a Joy sequence of integers (as opposed to a string or something else.)

+

We might proceed by creating a word that makes a copy of the sequence with the first item moved to the last, and zips it with the original to make a list of pairs, and a another word that adds (one of) each pair to a total if the pair matches.

+ +
AoC2017.1 == pair_up total_matches
+
+
+

Let's derive pair_up:

+ +
     [a b c] pair_up
+-------------------------
+   [[a b] [b c] [c a]]
+ +
+
+
+
+
+
+
+

Straightforward (although the order of each pair is reversed, due to the way zip works, but it doesn't matter for this program):

+ +
[a b c] dup
+[a b c] [a b c] uncons swap
+[a b c] [b c] a unit concat
+[a b c] [b c a] zip
+[[b a] [c b] [a c]]
+ +
+
+
+
+
+
In [2]:
+
+
+
define('pair_up == dup uncons swap unit concat zip')
+
+ +
+
+
+ +
+
+
+
In [3]:
+
+
+
J('[1 2 3] pair_up')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[2 1] [3 2] [1 3]]
+
+
+
+ +
+
+ +
+
+
+
In [4]:
+
+
+
J('[1 2 2 3] pair_up')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[2 1] [2 2] [3 2] [1 3]]
+
+
+
+ +
+
+ +
+
+
+
+
+

Now we need to derive total_matches. It will be a step function:

+ +
total_matches == 0 swap [F] step
+
+
+

Where F will have the pair to work with, and it will basically be a branch or ifte.

+ +
total [n m] F
+
+
+

It will probably be easier to write if we dequote the pair:

+ +
   total [n m] i F′
+----------------------
+     total n m F′
+
+
+

Now F′ becomes just:

+ +
total n m [=] [pop +] [popop] ifte
+
+
+

So:

+ +
F == i [=] [pop +] [popop] ifte
+
+
+

And thus:

+ +
total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
+ +
+
+
+
+
+
In [5]:
+
+
+
define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step')
+
+ +
+
+
+ +
+
+
+
In [6]:
+
+
+
J('[1 2 3] pair_up total_matches')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0
+
+
+
+ +
+
+ +
+
+
+
In [7]:
+
+
+
J('[1 2 2 3] pair_up total_matches')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
+
+

Now we can define our main program and evaluate it on the examples.

+ +
+
+
+
+
+
In [8]:
+
+
+
define('AoC2017.1 == pair_up total_matches')
+
+ +
+
+
+ +
+
+
+
In [9]:
+
+
+
J('[1 1 2 2] AoC2017.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3
+
+
+
+ +
+
+ +
+
+
+
In [10]:
+
+
+
J('[1 1 1 1] AoC2017.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4
+
+
+
+ +
+
+ +
+
+
+
In [11]:
+
+
+
J('[1 2 3 4] AoC2017.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0
+
+
+
+ +
+
+ +
+
+
+
In [12]:
+
+
+
J('[9 1 2 1 2 1 2 9] AoC2017.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9
+
+
+
+ +
+
+ +
+
+
+
In [13]:
+
+
+
J('[9 1 2 1 2 1 2 9] AoC2017.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9
+
+
+
+ +
+
+ +
+
+
+
+
+ +
      pair_up == dup uncons swap unit concat zip
+total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
+
+    AoC2017.1 == pair_up total_matches
+ +
+
+
+
+
+
+
+

Now the paired digit is "halfway" round.

+ +
[a b c d] dup size 2 / [drop] [take reverse] cleave concat zip
+ +
+
+
+
+
+
In [14]:
+
+
+
J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[3 1] [4 2] [1 3] [2 4]]
+
+
+
+ +
+
+ +
+
+
+
+
+

I realized that each pair is repeated...

+ +
+
+
+
+
+
In [15]:
+
+
+
J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave  zip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 2 3 4] [[1 3] [2 4]]
+
+
+
+ +
+
+ +
+
+
+
In [16]:
+
+
+
define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave  zip swap pop total_matches 2 *')
+
+ +
+
+
+ +
+
+
+
In [17]:
+
+
+
J('[1 2 1 2] AoC2017.1.extra')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6
+
+
+
+ +
+
+ +
+
+
+
In [18]:
+
+
+
J('[1 2 2 1] AoC2017.1.extra')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0
+
+
+
+ +
+
+ +
+
+
+
In [19]:
+
+
+
J('[1 2 3 4 2 5] AoC2017.1.extra')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4
+
+
+
+ +
+
+ +
+
+
+
+
+

Refactor FTW

With Joy a great deal of the heuristics from Forth programming carry over nicely. For example, refactoring into small, well-scoped commands with mnemonic names...

+ +
         rotate_seq == uncons swap unit concat
+            pair_up == dup rotate_seq zip
+       add_if_match == [=] [pop +] [popop] ifte
+      total_matches == [i add_if_match] step_zero
+
+          AoC2017.1 == pair_up total_matches
+
+       half_of_size == dup size 2 /
+           split_at == [drop] [take reverse] cleave
+      pair_up.extra == half_of_size split_at zip swap pop
+
+    AoC2017.1.extra == pair_up.extra total_matches 2 *
+ +
+
+
+
+
+ + + + + + diff --git a/docs/Advent_of_Code_2017_December_1st.ipynb b/docs/Advent_of_Code_2017_December_1st.ipynb new file mode 100644 index 0000000..c83a62d --- /dev/null +++ b/docs/Advent_of_Code_2017_December_1st.ipynb @@ -0,0 +1,455 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advent of Code 2017\n", + "\n", + "## December 1st\n", + "\n", + "\\[Given\\] a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.\n", + "\n", + "For example:\n", + "\n", + "* 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.\n", + "* 1111 produces 4 because each digit (all 1) matches the next.\n", + "* 1234 produces 0 because no digit matches the next.\n", + "* 91212129 produces 9 because the only digit that matches the next one is the last digit, 9." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I'll assume the input is a Joy sequence of integers (as opposed to a string or something else.)\n", + "\n", + "We might proceed by creating a word that makes a copy of the sequence with the first item moved to the last, and zips it with the original to make a list of pairs, and a another word that adds (one of) each pair to a total if the pair matches.\n", + "\n", + " AoC2017.1 == pair_up total_matches\n", + "\n", + "Let's derive `pair_up`:\n", + "\n", + " [a b c] pair_up\n", + " -------------------------\n", + " [[a b] [b c] [c a]]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Straightforward (although the order of each pair is reversed, due to the way `zip` works, but it doesn't matter for this program):\n", + "\n", + " [a b c] dup\n", + " [a b c] [a b c] uncons swap\n", + " [a b c] [b c] a unit concat\n", + " [a b c] [b c a] zip\n", + " [[b a] [c b] [a c]]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "define('pair_up == dup uncons swap unit concat zip')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 1] [3 2] [1 3]]\n" + ] + } + ], + "source": [ + "J('[1 2 3] pair_up')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 1] [2 2] [3 2] [1 3]]\n" + ] + } + ], + "source": [ + "J('[1 2 2 3] pair_up')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we need to derive `total_matches`. It will be a `step` function:\n", + "\n", + " total_matches == 0 swap [F] step\n", + "\n", + "Where `F` will have the pair to work with, and it will basically be a `branch` or `ifte`.\n", + "\n", + " total [n m] F\n", + "\n", + "It will probably be easier to write if we dequote the pair:\n", + "\n", + " total [n m] i F′\n", + " ----------------------\n", + " total n m F′\n", + "\n", + "Now `F′` becomes just:\n", + "\n", + " total n m [=] [pop +] [popop] ifte\n", + "\n", + "So:\n", + "\n", + " F == i [=] [pop +] [popop] ifte\n", + "\n", + "And thus:\n", + "\n", + " total_matches == 0 swap [i [=] [pop +] [popop] ifte] step" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "J('[1 2 3] pair_up total_matches')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('[1 2 2 3] pair_up total_matches')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can define our main program and evaluate it on the examples." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "define('AoC2017.1 == pair_up total_matches')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "J('[1 1 2 2] AoC2017.1')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "J('[1 1 1 1] AoC2017.1')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] AoC2017.1')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "J('[9 1 2 1 2 1 2 9] AoC2017.1')" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "J('[9 1 2 1 2 1 2 9] AoC2017.1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " pair_up == dup uncons swap unit concat zip\n", + " total_matches == 0 swap [i [=] [pop +] [popop] ifte] step\n", + "\n", + " AoC2017.1 == pair_up total_matches" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now the paired digit is \"halfway\" round.\n", + "\n", + " [a b c d] dup size 2 / [drop] [take reverse] cleave concat zip" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[3 1] [4 2] [1 3] [2 4]]\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I realized that each pair is repeated..." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4] [[1 3] [2 4]]\n" + ] + } + ], + "source": [ + "J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip')" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "J('[1 2 1 2] AoC2017.1.extra')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "J('[1 2 2 1] AoC2017.1.extra')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "J('[1 2 3 4 2 5] AoC2017.1.extra')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Refactor FTW\n", + "\n", + "With Joy a great deal of the heuristics from Forth programming carry over nicely. For example, refactoring into small, well-scoped commands with mnemonic names...\n", + "\n", + " rotate_seq == uncons swap unit concat\n", + " pair_up == dup rotate_seq zip\n", + " add_if_match == [=] [pop +] [popop] ifte\n", + " total_matches == [i add_if_match] step_zero\n", + "\n", + " AoC2017.1 == pair_up total_matches\n", + "\n", + " half_of_size == dup size 2 /\n", + " split_at == [drop] [take reverse] cleave\n", + " pair_up.extra == half_of_size split_at zip swap pop\n", + "\n", + " AoC2017.1.extra == pair_up.extra total_matches 2 *\n" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Advent_of_Code_2017_December_1st.md b/docs/Advent_of_Code_2017_December_1st.md new file mode 100644 index 0000000..c08b171 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_1st.md @@ -0,0 +1,228 @@ + +# Advent of Code 2017 + +## December 1st + +\[Given\] a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list. + +For example: + +* 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit. +* 1111 produces 4 because each digit (all 1) matches the next. +* 1234 produces 0 because no digit matches the next. +* 91212129 produces 9 because the only digit that matches the next one is the last digit, 9. + + +```python +from notebook_preamble import J, V, define +``` + +I'll assume the input is a Joy sequence of integers (as opposed to a string or something else.) + +We might proceed by creating a word that makes a copy of the sequence with the first item moved to the last, and zips it with the original to make a list of pairs, and a another word that adds (one of) each pair to a total if the pair matches. + + AoC2017.1 == pair_up total_matches + +Let's derive `pair_up`: + + [a b c] pair_up + ------------------------- + [[a b] [b c] [c a]] + + +Straightforward (although the order of each pair is reversed, due to the way `zip` works, but it doesn't matter for this program): + + [a b c] dup + [a b c] [a b c] uncons swap + [a b c] [b c] a unit concat + [a b c] [b c a] zip + [[b a] [c b] [a c]] + + +```python +define('pair_up == dup uncons swap unit concat zip') +``` + + +```python +J('[1 2 3] pair_up') +``` + + [[2 1] [3 2] [1 3]] + + + +```python +J('[1 2 2 3] pair_up') +``` + + [[2 1] [2 2] [3 2] [1 3]] + + +Now we need to derive `total_matches`. It will be a `step` function: + + total_matches == 0 swap [F] step + +Where `F` will have the pair to work with, and it will basically be a `branch` or `ifte`. + + total [n m] F + +It will probably be easier to write if we dequote the pair: + + total [n m] i F′ + ---------------------- + total n m F′ + +Now `F′` becomes just: + + total n m [=] [pop +] [popop] ifte + +So: + + F == i [=] [pop +] [popop] ifte + +And thus: + + total_matches == 0 swap [i [=] [pop +] [popop] ifte] step + + +```python +define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step') +``` + + +```python +J('[1 2 3] pair_up total_matches') +``` + + 0 + + + +```python +J('[1 2 2 3] pair_up total_matches') +``` + + 2 + + +Now we can define our main program and evaluate it on the examples. + + +```python +define('AoC2017.1 == pair_up total_matches') +``` + + +```python +J('[1 1 2 2] AoC2017.1') +``` + + 3 + + + +```python +J('[1 1 1 1] AoC2017.1') +``` + + 4 + + + +```python +J('[1 2 3 4] AoC2017.1') +``` + + 0 + + + +```python +J('[9 1 2 1 2 1 2 9] AoC2017.1') +``` + + 9 + + + +```python +J('[9 1 2 1 2 1 2 9] AoC2017.1') +``` + + 9 + + + pair_up == dup uncons swap unit concat zip + total_matches == 0 swap [i [=] [pop +] [popop] ifte] step + + AoC2017.1 == pair_up total_matches + +Now the paired digit is "halfway" round. + + [a b c d] dup size 2 / [drop] [take reverse] cleave concat zip + + +```python +J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip') +``` + + [[3 1] [4 2] [1 3] [2 4]] + + +I realized that each pair is repeated... + + +```python +J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip') +``` + + [1 2 3 4] [[1 3] [2 4]] + + + +```python +define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *') +``` + + +```python +J('[1 2 1 2] AoC2017.1.extra') +``` + + 6 + + + +```python +J('[1 2 2 1] AoC2017.1.extra') +``` + + 0 + + + +```python +J('[1 2 3 4 2 5] AoC2017.1.extra') +``` + + 4 + + +# Refactor FTW + +With Joy a great deal of the heuristics from Forth programming carry over nicely. For example, refactoring into small, well-scoped commands with mnemonic names... + + rotate_seq == uncons swap unit concat + pair_up == dup rotate_seq zip + add_if_match == [=] [pop +] [popop] ifte + total_matches == [i add_if_match] step_zero + + AoC2017.1 == pair_up total_matches + + half_of_size == dup size 2 / + split_at == [drop] [take reverse] cleave + pair_up.extra == half_of_size split_at zip swap pop + + AoC2017.1.extra == pair_up.extra total_matches 2 * + diff --git a/docs/Advent_of_Code_2017_December_1st.rst b/docs/Advent_of_Code_2017_December_1st.rst new file mode 100644 index 0000000..d5bb543 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_1st.rst @@ -0,0 +1,288 @@ + +Advent of Code 2017 +=================== + +December 1st +------------ + +[Given] a sequence of digits (your puzzle input) and find the sum of all +digits that match the next digit in the list. The list is circular, so +the digit after the last digit is the first digit in the list. + +For example: + +- 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches + the second digit and the third digit (2) matches the fourth digit. +- 1111 produces 4 because each digit (all 1) matches the next. +- 1234 produces 0 because no digit matches the next. +- 91212129 produces 9 because the only digit that matches the next one + is the last digit, 9. + +.. code:: ipython2 + + from notebook_preamble import J, V, define + +I'll assume the input is a Joy sequence of integers (as opposed to a +string or something else.) + +We might proceed by creating a word that makes a copy of the sequence +with the first item moved to the last, and zips it with the original to +make a list of pairs, and a another word that adds (one of) each pair to +a total if the pair matches. + +:: + + AoC2017.1 == pair_up total_matches + +Let's derive ``pair_up``: + +:: + + [a b c] pair_up + ------------------------- + [[a b] [b c] [c a]] + +Straightforward (although the order of each pair is reversed, due to the +way ``zip`` works, but it doesn't matter for this program): + +:: + + [a b c] dup + [a b c] [a b c] uncons swap + [a b c] [b c] a unit concat + [a b c] [b c a] zip + [[b a] [c b] [a c]] + +.. code:: ipython2 + + define('pair_up == dup uncons swap unit concat zip') + +.. code:: ipython2 + + J('[1 2 3] pair_up') + + +.. parsed-literal:: + + [[2 1] [3 2] [1 3]] + + +.. code:: ipython2 + + J('[1 2 2 3] pair_up') + + +.. parsed-literal:: + + [[2 1] [2 2] [3 2] [1 3]] + + +Now we need to derive ``total_matches``. It will be a ``step`` function: + +:: + + total_matches == 0 swap [F] step + +Where ``F`` will have the pair to work with, and it will basically be a +``branch`` or ``ifte``. + +:: + + total [n m] F + +It will probably be easier to write if we dequote the pair: + +:: + + total [n m] i F′ + ---------------------- + total n m F′ + +Now ``F′`` becomes just: + +:: + + total n m [=] [pop +] [popop] ifte + +So: + +:: + + F == i [=] [pop +] [popop] ifte + +And thus: + +:: + + total_matches == 0 swap [i [=] [pop +] [popop] ifte] step + +.. code:: ipython2 + + define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step') + +.. code:: ipython2 + + J('[1 2 3] pair_up total_matches') + + +.. parsed-literal:: + + 0 + + +.. code:: ipython2 + + J('[1 2 2 3] pair_up total_matches') + + +.. parsed-literal:: + + 2 + + +Now we can define our main program and evaluate it on the examples. + +.. code:: ipython2 + + define('AoC2017.1 == pair_up total_matches') + +.. code:: ipython2 + + J('[1 1 2 2] AoC2017.1') + + +.. parsed-literal:: + + 3 + + +.. code:: ipython2 + + J('[1 1 1 1] AoC2017.1') + + +.. parsed-literal:: + + 4 + + +.. code:: ipython2 + + J('[1 2 3 4] AoC2017.1') + + +.. parsed-literal:: + + 0 + + +.. code:: ipython2 + + J('[9 1 2 1 2 1 2 9] AoC2017.1') + + +.. parsed-literal:: + + 9 + + +.. code:: ipython2 + + J('[9 1 2 1 2 1 2 9] AoC2017.1') + + +.. parsed-literal:: + + 9 + + +:: + + pair_up == dup uncons swap unit concat zip + total_matches == 0 swap [i [=] [pop +] [popop] ifte] step + + AoC2017.1 == pair_up total_matches + +Now the paired digit is "halfway" round. + +:: + + [a b c d] dup size 2 / [drop] [take reverse] cleave concat zip + +.. code:: ipython2 + + J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip') + + +.. parsed-literal:: + + [[3 1] [4 2] [1 3] [2 4]] + + +I realized that each pair is repeated... + +.. code:: ipython2 + + J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip') + + +.. parsed-literal:: + + [1 2 3 4] [[1 3] [2 4]] + + +.. code:: ipython2 + + define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *') + +.. code:: ipython2 + + J('[1 2 1 2] AoC2017.1.extra') + + +.. parsed-literal:: + + 6 + + +.. code:: ipython2 + + J('[1 2 2 1] AoC2017.1.extra') + + +.. parsed-literal:: + + 0 + + +.. code:: ipython2 + + J('[1 2 3 4 2 5] AoC2017.1.extra') + + +.. parsed-literal:: + + 4 + + +Refactor FTW +============ + +With Joy a great deal of the heuristics from Forth programming carry +over nicely. For example, refactoring into small, well-scoped commands +with mnemonic names... + +:: + + rotate_seq == uncons swap unit concat + pair_up == dup rotate_seq zip + add_if_match == [=] [pop +] [popop] ifte + total_matches == [i add_if_match] step_zero + + AoC2017.1 == pair_up total_matches + + half_of_size == dup size 2 / + split_at == [drop] [take reverse] cleave + pair_up.extra == half_of_size split_at zip swap pop + + AoC2017.1.extra == pair_up.extra total_matches 2 * diff --git a/docs/Advent_of_Code_2017_December_2nd.html b/docs/Advent_of_Code_2017_December_2nd.html new file mode 100644 index 0000000..5595c07 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_2nd.html @@ -0,0 +1,12487 @@ + + + +Advent_of_Code_2017_December_2nd + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Advent of Code 2017

December 2nd

For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.

+

For example, given the following spreadsheet:

+ +
5 1 9 5
+7 5 3
+2 4 6 8
+
+
+
    +
  • The first row's largest and smallest values are 9 and 1, and their difference is 8.
  • +
  • The second row's largest and smallest values are 7 and 3, and their difference is 4.
  • +
  • The third row's difference is 6.
  • +
+

In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.

+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

I'll assume the input is a Joy sequence of sequences of integers.

+ +
[[5 1 9 5]
+ [7 5 3]
+ [2 4 6 8]]
+
+
+

So, obviously, the initial form will be a step function:

+ +
AoC2017.2 == 0 swap [F +] step
+ +
+
+
+
+
+
+
+

This function F must get the max and min of a row of numbers and subtract. We can define a helper function maxmin which does this:

+ +
+
+
+
+
+
In [2]:
+
+
+
define('maxmin == [max] [min] cleave')
+
+ +
+
+
+ +
+
+
+
In [3]:
+
+
+
J('[1 2 3] maxmin')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 1
+
+
+
+ +
+
+ +
+
+
+
+
+

Then F just does that then subtracts the min from the max:

+ +
F == maxmin -
+
+
+

So:

+ +
+
+
+
+
+
In [4]:
+
+
+
define('AoC2017.2 == [maxmin - +] step_zero')
+
+ +
+
+
+ +
+
+
+
In [5]:
+
+
+
J('''
+
+[[5 1 9 5]
+ [7 5 3]
+ [2 4 6 8]] AoC2017.2
+
+''')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
18
+
+
+
+ +
+
+ +
+
+
+
+
+

...find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.

+

For example, given the following spreadsheet:

+ +
5 9 2 8
+9 4 7 3
+3 8 6 5
+
+
+
    +
  • In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4.
  • +
  • In the second row, the two numbers are 9 and 3; the result is 3.
  • +
  • In the third row, the result is 2.
  • +
+

In this example, the sum of the results would be 4 + 3 + 2 = 9.

+

What is the sum of each row's result in your puzzle input?

+ +
+
+
+
+
+
In [6]:
+
+
+
J('[5 9 2 8] sort reverse')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[9 8 5 2]
+
+
+
+ +
+
+ +
+
+
+
In [7]:
+
+
+
J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[8 5 2] [9 divmod] [8 5 2]
+
+
+
+ +
+
+ +
+
+
+
+
+ +
[9 8 5 2] uncons [swap [divmod] cons F] dupdip G
+  [8 5 2]            [9 divmod]      F [8 5 2] G
+ +
+
+
+
+
+
In [8]:
+
+
+
V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                      . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
+                              [8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip
+                   [8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip
+     [8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip
+                              [8 5 2] . uncons swap [9 divmod] dup [i not] dip
+                              8 [5 2] . swap [9 divmod] dup [i not] dip
+                              [5 2] 8 . [9 divmod] dup [i not] dip
+                   [5 2] 8 [9 divmod] . dup [i not] dip
+        [5 2] 8 [9 divmod] [9 divmod] . [i not] dip
+[5 2] 8 [9 divmod] [9 divmod] [i not] . dip
+                   [5 2] 8 [9 divmod] . i not [9 divmod]
+                              [5 2] 8 . 9 divmod not [9 divmod]
+                            [5 2] 8 9 . divmod not [9 divmod]
+                            [5 2] 1 1 . not [9 divmod]
+                        [5 2] 1 False . [9 divmod]
+             [5 2] 1 False [9 divmod] . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Tricky

Let's think.

+

Given a sorted sequence (from highest to lowest) we want to

+
    +
  • for head, tail in sequence
      +
    • for term in tail:
        +
      • check if the head % term == 0
          +
        • if so compute head / term and terminate loop
        • +
        • else continue
        • +
        +
      • +
      +
    • +
    +
  • +
+ +
+
+
+
+
+
+
+

So we want a loop I think

+
[a b c d] True [Q] loop
+[a b c d] Q    [Q] loop
+
+
+

Q should either leave the result and False, or the rest and True.

+ +
   [a b c d] Q
+-----------------
+    result 0
+
+   [a b c d] Q
+-----------------
+    [b c d] 1
+ +
+
+
+
+
+
+
+

This suggests that Q should start with:

+ +
[a b c d] uncons dup roll<
+[b c d] [b c d] a
+
+
+

Now we just have to pop it if we don't need it.

+ +
[b c d] [b c d] a [P] [T] [cons] app2 popdd [E] primrec
+[b c d] [b c d] [a P] [a T]                 [E] primrec
+
+
+
+ +
w/ Q == [% not] [T] [F] primrec
+
+        [a b c d] uncons
+        a [b c d] tuck
+[b c d] a [b c d] uncons
+[b c d] a b [c d] roll>
+[b c d] [c d] a b Q
+[b c d] [c d] a b [% not] [T] [F] primrec
+
+[b c d] [c d] a b T
+[b c d] [c d] a b / roll> popop 0
+
+[b c d] [c d] a b F                   Q
+[b c d] [c d] a b pop swap uncons ... Q
+[b c d] [c d] a       swap uncons ... Q
+[b c d] a [c d]            uncons ... Q
+[b c d] a c [d]                   roll> Q
+[b c d] [d] a c Q
+
+Q == [% not] [/ roll> popop 0] [pop swap uncons roll>] primrec
+
+uncons tuck uncons roll> Q
+ +
+
+
+
+
+
In [9]:
+
+
+
J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[8 5 3 2] [9 swap] [9 % not]
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
        [a b c d] uncons
+        a [b c d] tuck
+[b c d] a [b c d] [not] [popop 1] [Q] ifte
+
+[b c d] a [] popop 1
+[b c d] 1
+
+[b c d] a [b c d] Q 
+
+
+   a [...] Q
+---------------
+   result 0
+
+   a [...] Q
+---------------
+       1
+
+
+w/ Q == [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
+
+
+
+a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
+a [b c d]  first % not
+a b % not
+a%b not
+bool(a%b)
+
+a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
+a [b c d]                first / 0
+a b / 0
+a/b 0
+
+a [b c d] [first % not] [first / 0] [rest [not] [popop 1]]   [ifte]
+a [b c d]                            rest [not] [popop 1] [Q] ifte
+a [c d]                                   [not] [popop 1] [Q] ifte
+a [c d]                                   [not] [popop 1] [Q] ifte
+
+a [c d] [not] [popop 1] [Q] ifte
+a [c d]  not
+
+a [] popop 1
+1
+
+a [c d] Q
+
+
+uncons tuck [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
+ +
+
+
+
+
+
+
+

I finally sat down with a piece of paper and blocked it out.

First, I made a function G that expects a number and a sequence of candidates and return the result or zero:

+ +
   n [...] G
+---------------
+    result
+
+   n [...] G
+---------------
+       0
+
+
+

It's a recursive function that conditionally executes the recursive part of its recursive branch

+ +
[Pg] [E] [R1 [Pi] [T]] [ifte] genrec
+
+
+

The recursive branch is the else-part of the inner ifte:

+ +
G == [Pg] [E] [R1 [Pi] [T]]   [ifte] genrec
+  == [Pg] [E] [R1 [Pi] [T] [G] ifte] ifte
+
+
+

But this is in hindsight. Going forward I derived:

+ +
G == [first % not]
+     [first /]
+     [rest [not] [popop 0]]
+     [ifte] genrec
+
+
+

The predicate detects if the n can be evenly divided by the first item in the list. If so, the then-part returns the result. Otherwise, we have:

+ +
n [m ...] rest [not] [popop 0] [G] ifte
+n [...]        [not] [popop 0] [G] ifte
+
+
+

This ifte guards against empty sequences and returns zero in that case, otherwise it executes G.

+ +
+
+
+
+
+
In [10]:
+
+
+
define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')
+
+ +
+
+
+ +
+
+
+
+
+

Now we need a word that uses G on each (head, tail) pair of a sequence until it finds a (non-zero) result. It's going to be designed to work on a stack that has some candidate n, a sequence of possible divisors, and a result that is zero to signal to continue (a non-zero value implies that it is the discovered result):

+ +
   n [...] p find-result
+---------------------------
+          result
+
+
+

It applies G using nullary because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by G.)

+ +
find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
+
+n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
+
+
+

The base-case is trivial, return the (non-zero) result. The recursive branch...

+ +
n [...] p roll< popop uncons [G] nullary find-result
+[...] p n       popop uncons [G] nullary find-result
+[...]                 uncons [G] nullary find-result
+m [..]                       [G] nullary find-result
+m [..] p                                 find-result
+
+
+

The puzzle states that the input is well-formed, meaning that we can expect a result before the row sequence empties and so do not need to guard the uncons.

+ +
+
+
+
+
+
In [11]:
+
+
+
define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')
+
+ +
+
+
+ +
+
+
+
In [14]:
+
+
+
J('[11 9 8 7 3 2] 0 tuck find-result')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3.0
+
+
+
+ +
+
+ +
+
+
+
+
+

In order to get the thing started, we need to sort the list in descending order, then prime the find-result function with a dummy candidate value and zero ("continue") flag.

+ +
+
+
+
+
+
In [12]:
+
+
+
define('prep-row == sort reverse 0 tuck')
+
+ +
+
+
+ +
+
+
+
+
+

Now we can define our program.

+ +
+
+
+
+
+
In [13]:
+
+
+
define('AoC20017.2.extra == [prep-row find-result +] step_zero')
+
+ +
+
+
+ +
+
+
+
In [15]:
+
+
+
J('''
+
+[[5 9 2 8]
+ [9 4 7 3]
+ [3 8 6 5]] AoC20017.2.extra
+
+''')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9.0
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/Advent_of_Code_2017_December_2nd.ipynb b/docs/Advent_of_Code_2017_December_2nd.ipynb new file mode 100644 index 0000000..871349d --- /dev/null +++ b/docs/Advent_of_Code_2017_December_2nd.ipynb @@ -0,0 +1,554 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advent of Code 2017\n", + "\n", + "## December 2nd\n", + "\n", + "For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.\n", + "\n", + "For example, given the following spreadsheet:\n", + "\n", + " 5 1 9 5\n", + " 7 5 3\n", + " 2 4 6 8\n", + "\n", + "* The first row's largest and smallest values are 9 and 1, and their difference is 8.\n", + "* The second row's largest and smallest values are 7 and 3, and their difference is 4.\n", + "* The third row's difference is 6.\n", + "\n", + "In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I'll assume the input is a Joy sequence of sequences of integers.\n", + "\n", + " [[5 1 9 5]\n", + " [7 5 3]\n", + " [2 4 6 8]]\n", + "\n", + "So, obviously, the initial form will be a `step` function:\n", + "\n", + " AoC2017.2 == 0 swap [F +] step" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This function `F` must get the `max` and `min` of a row of numbers and subtract. We can define a helper function `maxmin` which does this:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "define('maxmin == [max] [min] cleave')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 1\n" + ] + } + ], + "source": [ + "J('[1 2 3] maxmin')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then `F` just does that then subtracts the min from the max:\n", + "\n", + " F == maxmin -\n", + "\n", + "So:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "define('AoC2017.2 == [maxmin - +] step_zero')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18\n" + ] + } + ], + "source": [ + "J('''\n", + "\n", + "[[5 1 9 5]\n", + " [7 5 3]\n", + " [2 4 6 8]] AoC2017.2\n", + "\n", + "''')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.\n", + "\n", + "For example, given the following spreadsheet:\n", + "\n", + " 5 9 2 8\n", + " 9 4 7 3\n", + " 3 8 6 5\n", + "\n", + "* In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4.\n", + "* In the second row, the two numbers are 9 and 3; the result is 3.\n", + "* In the third row, the result is 2.\n", + "\n", + "In this example, the sum of the results would be 4 + 3 + 2 = 9.\n", + "\n", + "What is the sum of each row's result in your puzzle input?" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9 8 5 2]\n" + ] + } + ], + "source": [ + "J('[5 9 2 8] sort reverse')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8 5 2] [9 divmod] [8 5 2]\n" + ] + } + ], + "source": [ + "J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " [9 8 5 2] uncons [swap [divmod] cons F] dupdip G\n", + " [8 5 2] [9 divmod] F [8 5 2] G\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n", + " [8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip\n", + " [8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip\n", + " [8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip\n", + " [8 5 2] . uncons swap [9 divmod] dup [i not] dip\n", + " 8 [5 2] . swap [9 divmod] dup [i not] dip\n", + " [5 2] 8 . [9 divmod] dup [i not] dip\n", + " [5 2] 8 [9 divmod] . dup [i not] dip\n", + " [5 2] 8 [9 divmod] [9 divmod] . [i not] dip\n", + "[5 2] 8 [9 divmod] [9 divmod] [i not] . dip\n", + " [5 2] 8 [9 divmod] . i not [9 divmod]\n", + " [5 2] 8 . 9 divmod not [9 divmod]\n", + " [5 2] 8 9 . divmod not [9 divmod]\n", + " [5 2] 1 1 . not [9 divmod]\n", + " [5 2] 1 False . [9 divmod]\n", + " [5 2] 1 False [9 divmod] . \n" + ] + } + ], + "source": [ + "V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tricky\n", + "\n", + "Let's think.\n", + "\n", + "Given a *sorted* sequence (from highest to lowest) we want to \n", + "* for head, tail in sequence\n", + " * for term in tail:\n", + " * check if the head % term == 0\n", + " * if so compute head / term and terminate loop\n", + " * else continue" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### So we want a `loop` I think\n", + "\n", + " [a b c d] True [Q] loop\n", + " [a b c d] Q [Q] loop\n", + "\n", + "`Q` should either leave the result and False, or the `rest` and True.\n", + "\n", + " [a b c d] Q\n", + " -----------------\n", + " result 0\n", + "\n", + " [a b c d] Q\n", + " -----------------\n", + " [b c d] 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This suggests that `Q` should start with:\n", + "\n", + " [a b c d] uncons dup roll<\n", + " [b c d] [b c d] a\n", + "\n", + "Now we just have to `pop` it if we don't need it.\n", + "\n", + " [b c d] [b c d] a [P] [T] [cons] app2 popdd [E] primrec\n", + " [b c d] [b c d] [a P] [a T] [E] primrec\n", + "\n", + "-------------------\n", + "\n", + " w/ Q == [% not] [T] [F] primrec\n", + "\n", + " [a b c d] uncons\n", + " a [b c d] tuck\n", + " [b c d] a [b c d] uncons\n", + " [b c d] a b [c d] roll>\n", + " [b c d] [c d] a b Q\n", + " [b c d] [c d] a b [% not] [T] [F] primrec\n", + "\n", + " [b c d] [c d] a b T\n", + " [b c d] [c d] a b / roll> popop 0\n", + "\n", + " [b c d] [c d] a b F Q\n", + " [b c d] [c d] a b pop swap uncons ... Q\n", + " [b c d] [c d] a swap uncons ... Q\n", + " [b c d] a [c d] uncons ... Q\n", + " [b c d] a c [d] roll> Q\n", + " [b c d] [d] a c Q\n", + "\n", + " Q == [% not] [/ roll> popop 0] [pop swap uncons roll>] primrec\n", + " \n", + " uncons tuck uncons roll> Q" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8 5 3 2] [9 swap] [9 % not]\n" + ] + } + ], + "source": [ + "J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-------------------\n", + "\n", + " [a b c d] uncons\n", + " a [b c d] tuck\n", + " [b c d] a [b c d] [not] [popop 1] [Q] ifte\n", + "\n", + " [b c d] a [] popop 1\n", + " [b c d] 1\n", + "\n", + " [b c d] a [b c d] Q \n", + "\n", + "\n", + " a [...] Q\n", + " ---------------\n", + " result 0\n", + "\n", + " a [...] Q\n", + " ---------------\n", + " 1\n", + "\n", + "\n", + " w/ Q == [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n", + "\n", + "\n", + "\n", + " a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n", + " a [b c d] first % not\n", + " a b % not\n", + " a%b not\n", + " bool(a%b)\n", + "\n", + " a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n", + " a [b c d] first / 0\n", + " a b / 0\n", + " a/b 0\n", + "\n", + " a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n", + " a [b c d] rest [not] [popop 1] [Q] ifte\n", + " a [c d] [not] [popop 1] [Q] ifte\n", + " a [c d] [not] [popop 1] [Q] ifte\n", + "\n", + " a [c d] [not] [popop 1] [Q] ifte\n", + " a [c d] not\n", + "\n", + " a [] popop 1\n", + " 1\n", + "\n", + " a [c d] Q\n", + "\n", + "\n", + " uncons tuck [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n", + " \n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### I finally sat down with a piece of paper and blocked it out.\n", + "\n", + "First, I made a function `G` that expects a number and a sequence of candidates and return the result or zero:\n", + "\n", + " n [...] G\n", + " ---------------\n", + " result\n", + "\n", + " n [...] G\n", + " ---------------\n", + " 0\n", + "\n", + "It's a recursive function that conditionally executes the recursive part of its recursive branch\n", + "\n", + " [Pg] [E] [R1 [Pi] [T]] [ifte] genrec\n", + "\n", + "The recursive branch is the else-part of the inner `ifte`:\n", + "\n", + " G == [Pg] [E] [R1 [Pi] [T]] [ifte] genrec\n", + " == [Pg] [E] [R1 [Pi] [T] [G] ifte] ifte\n", + "\n", + "But this is in hindsight. Going forward I derived:\n", + "\n", + " G == [first % not]\n", + " [first /]\n", + " [rest [not] [popop 0]]\n", + " [ifte] genrec\n", + "\n", + "The predicate detects if the `n` can be evenly divided by the `first` item in the list. If so, the then-part returns the result. Otherwise, we have:\n", + "\n", + " n [m ...] rest [not] [popop 0] [G] ifte\n", + " n [...] [not] [popop 0] [G] ifte\n", + "\n", + "This `ifte` guards against empty sequences and returns zero in that case, otherwise it executes `G`." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we need a word that uses `G` on each (head, tail) pair of a sequence until it finds a (non-zero) result. It's going to be designed to work on a stack that has some candidate `n`, a sequence of possible divisors, and a result that is zero to signal to continue (a non-zero value implies that it is the discovered result):\n", + "\n", + " n [...] p find-result\n", + " ---------------------------\n", + " result\n", + "\n", + "It applies `G` using `nullary` because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by `G`.)\n", + "\n", + " find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec\n", + "\n", + " n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec\n", + "\n", + "The base-case is trivial, return the (non-zero) result. The recursive branch...\n", + "\n", + " n [...] p roll< popop uncons [G] nullary find-result\n", + " [...] p n popop uncons [G] nullary find-result\n", + " [...] uncons [G] nullary find-result\n", + " m [..] [G] nullary find-result\n", + " m [..] p find-result\n", + "\n", + "The puzzle states that the input is well-formed, meaning that we can expect a result before the row sequence empties and so do not need to guard the `uncons`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.0\n" + ] + } + ], + "source": [ + "J('[11 9 8 7 3 2] 0 tuck find-result')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In order to get the thing started, we need to `sort` the list in descending order, then prime the `find-result` function with a dummy candidate value and zero (\"continue\") flag." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "define('prep-row == sort reverse 0 tuck')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can define our program." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "define('AoC20017.2.extra == [prep-row find-result +] step_zero')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.0\n" + ] + } + ], + "source": [ + "J('''\n", + "\n", + "[[5 9 2 8]\n", + " [9 4 7 3]\n", + " [3 8 6 5]] AoC20017.2.extra\n", + "\n", + "''')" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Advent_of_Code_2017_December_2nd.md b/docs/Advent_of_Code_2017_December_2nd.md new file mode 100644 index 0000000..28b6908 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_2nd.md @@ -0,0 +1,361 @@ + +# Advent of Code 2017 + +## December 2nd + +For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences. + +For example, given the following spreadsheet: + + 5 1 9 5 + 7 5 3 + 2 4 6 8 + +* The first row's largest and smallest values are 9 and 1, and their difference is 8. +* The second row's largest and smallest values are 7 and 3, and their difference is 4. +* The third row's difference is 6. + +In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18. + + +```python +from notebook_preamble import J, V, define +``` + +I'll assume the input is a Joy sequence of sequences of integers. + + [[5 1 9 5] + [7 5 3] + [2 4 6 8]] + +So, obviously, the initial form will be a `step` function: + + AoC2017.2 == 0 swap [F +] step + +This function `F` must get the `max` and `min` of a row of numbers and subtract. We can define a helper function `maxmin` which does this: + + +```python +define('maxmin == [max] [min] cleave') +``` + + +```python +J('[1 2 3] maxmin') +``` + + 3 1 + + +Then `F` just does that then subtracts the min from the max: + + F == maxmin - + +So: + + +```python +define('AoC2017.2 == [maxmin - +] step_zero') +``` + + +```python +J(''' + +[[5 1 9 5] + [7 5 3] + [2 4 6 8]] AoC2017.2 + +''') +``` + + 18 + + +...find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result. + +For example, given the following spreadsheet: + + 5 9 2 8 + 9 4 7 3 + 3 8 6 5 + +* In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4. +* In the second row, the two numbers are 9 and 3; the result is 3. +* In the third row, the result is 2. + +In this example, the sum of the results would be 4 + 3 + 2 = 9. + +What is the sum of each row's result in your puzzle input? + + +```python +J('[5 9 2 8] sort reverse') +``` + + [9 8 5 2] + + + +```python +J('[9 8 5 2] uncons [swap [divmod] cons] dupdip') +``` + + [8 5 2] [9 divmod] [8 5 2] + + + + [9 8 5 2] uncons [swap [divmod] cons F] dupdip G + [8 5 2] [9 divmod] F [8 5 2] G + + + + +```python +V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip') +``` + + . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip + [8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip + [8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip + [8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip + [8 5 2] . uncons swap [9 divmod] dup [i not] dip + 8 [5 2] . swap [9 divmod] dup [i not] dip + [5 2] 8 . [9 divmod] dup [i not] dip + [5 2] 8 [9 divmod] . dup [i not] dip + [5 2] 8 [9 divmod] [9 divmod] . [i not] dip + [5 2] 8 [9 divmod] [9 divmod] [i not] . dip + [5 2] 8 [9 divmod] . i not [9 divmod] + [5 2] 8 . 9 divmod not [9 divmod] + [5 2] 8 9 . divmod not [9 divmod] + [5 2] 1 1 . not [9 divmod] + [5 2] 1 False . [9 divmod] + [5 2] 1 False [9 divmod] . + + +## Tricky + +Let's think. + +Given a *sorted* sequence (from highest to lowest) we want to +* for head, tail in sequence + * for term in tail: + * check if the head % term == 0 + * if so compute head / term and terminate loop + * else continue + +### So we want a `loop` I think + + [a b c d] True [Q] loop + [a b c d] Q [Q] loop + +`Q` should either leave the result and False, or the `rest` and True. + + [a b c d] Q + ----------------- + result 0 + + [a b c d] Q + ----------------- + [b c d] 1 + +This suggests that `Q` should start with: + + [a b c d] uncons dup roll< + [b c d] [b c d] a + +Now we just have to `pop` it if we don't need it. + + [b c d] [b c d] a [P] [T] [cons] app2 popdd [E] primrec + [b c d] [b c d] [a P] [a T] [E] primrec + +------------------- + + w/ Q == [% not] [T] [F] primrec + + [a b c d] uncons + a [b c d] tuck + [b c d] a [b c d] uncons + [b c d] a b [c d] roll> + [b c d] [c d] a b Q + [b c d] [c d] a b [% not] [T] [F] primrec + + [b c d] [c d] a b T + [b c d] [c d] a b / roll> popop 0 + + [b c d] [c d] a b F Q + [b c d] [c d] a b pop swap uncons ... Q + [b c d] [c d] a swap uncons ... Q + [b c d] a [c d] uncons ... Q + [b c d] a c [d] roll> Q + [b c d] [d] a c Q + + Q == [% not] [/ roll> popop 0] [pop swap uncons roll>] primrec + + uncons tuck uncons roll> Q + + +```python +J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd') +``` + + [8 5 3 2] [9 swap] [9 % not] + + +------------------- + + [a b c d] uncons + a [b c d] tuck + [b c d] a [b c d] [not] [popop 1] [Q] ifte + + [b c d] a [] popop 1 + [b c d] 1 + + [b c d] a [b c d] Q + + + a [...] Q + --------------- + result 0 + + a [...] Q + --------------- + 1 + + + w/ Q == [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + + + + a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + a [b c d] first % not + a b % not + a%b not + bool(a%b) + + a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + a [b c d] first / 0 + a b / 0 + a/b 0 + + a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + a [b c d] rest [not] [popop 1] [Q] ifte + a [c d] [not] [popop 1] [Q] ifte + a [c d] [not] [popop 1] [Q] ifte + + a [c d] [not] [popop 1] [Q] ifte + a [c d] not + + a [] popop 1 + 1 + + a [c d] Q + + + uncons tuck [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + + + + +### I finally sat down with a piece of paper and blocked it out. + +First, I made a function `G` that expects a number and a sequence of candidates and return the result or zero: + + n [...] G + --------------- + result + + n [...] G + --------------- + 0 + +It's a recursive function that conditionally executes the recursive part of its recursive branch + + [Pg] [E] [R1 [Pi] [T]] [ifte] genrec + +The recursive branch is the else-part of the inner `ifte`: + + G == [Pg] [E] [R1 [Pi] [T]] [ifte] genrec + == [Pg] [E] [R1 [Pi] [T] [G] ifte] ifte + +But this is in hindsight. Going forward I derived: + + G == [first % not] + [first /] + [rest [not] [popop 0]] + [ifte] genrec + +The predicate detects if the `n` can be evenly divided by the `first` item in the list. If so, the then-part returns the result. Otherwise, we have: + + n [m ...] rest [not] [popop 0] [G] ifte + n [...] [not] [popop 0] [G] ifte + +This `ifte` guards against empty sequences and returns zero in that case, otherwise it executes `G`. + + +```python +define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec') +``` + +Now we need a word that uses `G` on each (head, tail) pair of a sequence until it finds a (non-zero) result. It's going to be designed to work on a stack that has some candidate `n`, a sequence of possible divisors, and a result that is zero to signal to continue (a non-zero value implies that it is the discovered result): + + n [...] p find-result + --------------------------- + result + +It applies `G` using `nullary` because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by `G`.) + + find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec + + n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec + +The base-case is trivial, return the (non-zero) result. The recursive branch... + + n [...] p roll< popop uncons [G] nullary find-result + [...] p n popop uncons [G] nullary find-result + [...] uncons [G] nullary find-result + m [..] [G] nullary find-result + m [..] p find-result + +The puzzle states that the input is well-formed, meaning that we can expect a result before the row sequence empties and so do not need to guard the `uncons`. + + +```python +define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec') +``` + + +```python +J('[11 9 8 7 3 2] 0 tuck find-result') +``` + + 3.0 + + +In order to get the thing started, we need to `sort` the list in descending order, then prime the `find-result` function with a dummy candidate value and zero ("continue") flag. + + +```python +define('prep-row == sort reverse 0 tuck') +``` + +Now we can define our program. + + +```python +define('AoC20017.2.extra == [prep-row find-result +] step_zero') +``` + + +```python +J(''' + +[[5 9 2 8] + [9 4 7 3] + [3 8 6 5]] AoC20017.2.extra + +''') +``` + + 9.0 + diff --git a/docs/Advent_of_Code_2017_December_2nd.rst b/docs/Advent_of_Code_2017_December_2nd.rst new file mode 100644 index 0000000..7b85b26 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_2nd.rst @@ -0,0 +1,432 @@ + +Advent of Code 2017 +=================== + +December 2nd +------------ + +For each row, determine the difference between the largest value and the +smallest value; the checksum is the sum of all of these differences. + +For example, given the following spreadsheet: + +:: + + 5 1 9 5 + 7 5 3 + 2 4 6 8 + +- The first row's largest and smallest values are 9 and 1, and their + difference is 8. +- The second row's largest and smallest values are 7 and 3, and their + difference is 4. +- The third row's difference is 6. + +In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18. + +.. code:: ipython2 + + from notebook_preamble import J, V, define + +I'll assume the input is a Joy sequence of sequences of integers. + +:: + + [[5 1 9 5] + [7 5 3] + [2 4 6 8]] + +So, obviously, the initial form will be a ``step`` function: + +:: + + AoC2017.2 == 0 swap [F +] step + +This function ``F`` must get the ``max`` and ``min`` of a row of numbers +and subtract. We can define a helper function ``maxmin`` which does +this: + +.. code:: ipython2 + + define('maxmin == [max] [min] cleave') + +.. code:: ipython2 + + J('[1 2 3] maxmin') + + +.. parsed-literal:: + + 3 1 + + +Then ``F`` just does that then subtracts the min from the max: + +:: + + F == maxmin - + +So: + +.. code:: ipython2 + + define('AoC2017.2 == [maxmin - +] step_zero') + +.. code:: ipython2 + + J(''' + + [[5 1 9 5] + [7 5 3] + [2 4 6 8]] AoC2017.2 + + ''') + + +.. parsed-literal:: + + 18 + + +...find the only two numbers in each row where one evenly divides the +other - that is, where the result of the division operation is a whole +number. They would like you to find those numbers on each line, divide +them, and add up each line's result. + +For example, given the following spreadsheet: + +:: + + 5 9 2 8 + 9 4 7 3 + 3 8 6 5 + +- In the first row, the only two numbers that evenly divide are 8 and + 2; the result of this division is 4. +- In the second row, the two numbers are 9 and 3; the result is 3. +- In the third row, the result is 2. + +In this example, the sum of the results would be 4 + 3 + 2 = 9. + +What is the sum of each row's result in your puzzle input? + +.. code:: ipython2 + + J('[5 9 2 8] sort reverse') + + +.. parsed-literal:: + + [9 8 5 2] + + +.. code:: ipython2 + + J('[9 8 5 2] uncons [swap [divmod] cons] dupdip') + + +.. parsed-literal:: + + [8 5 2] [9 divmod] [8 5 2] + + +:: + + [9 8 5 2] uncons [swap [divmod] cons F] dupdip G + [8 5 2] [9 divmod] F [8 5 2] G + +.. code:: ipython2 + + V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip') + + +.. parsed-literal:: + + . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip + [8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip + [8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip + [8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip + [8 5 2] . uncons swap [9 divmod] dup [i not] dip + 8 [5 2] . swap [9 divmod] dup [i not] dip + [5 2] 8 . [9 divmod] dup [i not] dip + [5 2] 8 [9 divmod] . dup [i not] dip + [5 2] 8 [9 divmod] [9 divmod] . [i not] dip + [5 2] 8 [9 divmod] [9 divmod] [i not] . dip + [5 2] 8 [9 divmod] . i not [9 divmod] + [5 2] 8 . 9 divmod not [9 divmod] + [5 2] 8 9 . divmod not [9 divmod] + [5 2] 1 1 . not [9 divmod] + [5 2] 1 False . [9 divmod] + [5 2] 1 False [9 divmod] . + + +Tricky +------ + +Let's think. + +Given a *sorted* sequence (from highest to lowest) we want to \* for +head, tail in sequence \* for term in tail: \* check if the head % term +== 0 \* if so compute head / term and terminate loop \* else continue + +So we want a ``loop`` I think +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + [a b c d] True [Q] loop + [a b c d] Q [Q] loop + +``Q`` should either leave the result and False, or the ``rest`` and +True. + +:: + + [a b c d] Q + ----------------- + result 0 + + [a b c d] Q + ----------------- + [b c d] 1 + +This suggests that ``Q`` should start with: + +:: + + [a b c d] uncons dup roll< + [b c d] [b c d] a + +Now we just have to ``pop`` it if we don't need it. + +:: + + [b c d] [b c d] a [P] [T] [cons] app2 popdd [E] primrec + [b c d] [b c d] [a P] [a T] [E] primrec + +-------------- + +:: + + w/ Q == [% not] [T] [F] primrec + + [a b c d] uncons + a [b c d] tuck + [b c d] a [b c d] uncons + [b c d] a b [c d] roll> + [b c d] [c d] a b Q + [b c d] [c d] a b [% not] [T] [F] primrec + + [b c d] [c d] a b T + [b c d] [c d] a b / roll> popop 0 + + [b c d] [c d] a b F Q + [b c d] [c d] a b pop swap uncons ... Q + [b c d] [c d] a swap uncons ... Q + [b c d] a [c d] uncons ... Q + [b c d] a c [d] roll> Q + [b c d] [d] a c Q + + Q == [% not] [/ roll> popop 0] [pop swap uncons roll>] primrec + + uncons tuck uncons roll> Q + +.. code:: ipython2 + + J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd') + + +.. parsed-literal:: + + [8 5 3 2] [9 swap] [9 % not] + + +-------------- + +:: + + [a b c d] uncons + a [b c d] tuck + [b c d] a [b c d] [not] [popop 1] [Q] ifte + + [b c d] a [] popop 1 + [b c d] 1 + + [b c d] a [b c d] Q + + + a [...] Q + --------------- + result 0 + + a [...] Q + --------------- + 1 + + + w/ Q == [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + + + + a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + a [b c d] first % not + a b % not + a%b not + bool(a%b) + + a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + a [b c d] first / 0 + a b / 0 + a/b 0 + + a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + a [b c d] rest [not] [popop 1] [Q] ifte + a [c d] [not] [popop 1] [Q] ifte + a [c d] [not] [popop 1] [Q] ifte + + a [c d] [not] [popop 1] [Q] ifte + a [c d] not + + a [] popop 1 + 1 + + a [c d] Q + + + uncons tuck [first % not] [first / 0] [rest [not] [popop 1]] [ifte] + +I finally sat down with a piece of paper and blocked it out. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +First, I made a function ``G`` that expects a number and a sequence of +candidates and return the result or zero: + +:: + + n [...] G + --------------- + result + + n [...] G + --------------- + 0 + +It's a recursive function that conditionally executes the recursive part +of its recursive branch + +:: + + [Pg] [E] [R1 [Pi] [T]] [ifte] genrec + +The recursive branch is the else-part of the inner ``ifte``: + +:: + + G == [Pg] [E] [R1 [Pi] [T]] [ifte] genrec + == [Pg] [E] [R1 [Pi] [T] [G] ifte] ifte + +But this is in hindsight. Going forward I derived: + +:: + + G == [first % not] + [first /] + [rest [not] [popop 0]] + [ifte] genrec + +The predicate detects if the ``n`` can be evenly divided by the +``first`` item in the list. If so, the then-part returns the result. +Otherwise, we have: + +:: + + n [m ...] rest [not] [popop 0] [G] ifte + n [...] [not] [popop 0] [G] ifte + +This ``ifte`` guards against empty sequences and returns zero in that +case, otherwise it executes ``G``. + +.. code:: ipython2 + + define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec') + +Now we need a word that uses ``G`` on each (head, tail) pair of a +sequence until it finds a (non-zero) result. It's going to be designed +to work on a stack that has some candidate ``n``, a sequence of possible +divisors, and a result that is zero to signal to continue (a non-zero +value implies that it is the discovered result): + +:: + + n [...] p find-result + --------------------------- + result + +It applies ``G`` using ``nullary`` because if it fails with one +candidate it needs the list to get the next one (the list is otherwise +consumed by ``G``.) + +:: + + find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec + + n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec + +The base-case is trivial, return the (non-zero) result. The recursive +branch... + +:: + + n [...] p roll< popop uncons [G] nullary find-result + [...] p n popop uncons [G] nullary find-result + [...] uncons [G] nullary find-result + m [..] [G] nullary find-result + m [..] p find-result + +The puzzle states that the input is well-formed, meaning that we can +expect a result before the row sequence empties and so do not need to +guard the ``uncons``. + +.. code:: ipython2 + + define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec') + +.. code:: ipython2 + + J('[11 9 8 7 3 2] 0 tuck find-result') + + +.. parsed-literal:: + + 3.0 + + +In order to get the thing started, we need to ``sort`` the list in +descending order, then prime the ``find-result`` function with a dummy +candidate value and zero ("continue") flag. + +.. code:: ipython2 + + define('prep-row == sort reverse 0 tuck') + +Now we can define our program. + +.. code:: ipython2 + + define('AoC20017.2.extra == [prep-row find-result +] step_zero') + +.. code:: ipython2 + + J(''' + + [[5 9 2 8] + [9 4 7 3] + [3 8 6 5]] AoC20017.2.extra + + ''') + + +.. parsed-literal:: + + 9.0 + diff --git a/docs/Advent_of_Code_2017_December_3rd.html b/docs/Advent_of_Code_2017_December_3rd.html new file mode 100644 index 0000000..be387c8 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_3rd.html @@ -0,0 +1,13592 @@ + + + +Advent_of_Code_2017_December_3rd + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Advent of Code 2017

December 3rd

You come across an experimental new kind of memory stored on an infinite two-dimensional grid.

+

Each square on the grid is allocated in a spiral pattern starting at a location marked 1 and then counting up while spiraling outward. For example, the first few squares are allocated like this:

+ +
17  16  15  14  13
+18   5   4   3  12
+19   6   1   2  11
+20   7   8   9  10
+21  22  23---> ...
+
+
+

While this is very space-efficient (no squares are skipped), requested data must be carried back to square 1 (the location of the only access port for this memory system) by programs that can only move up, down, left, or right. They always take the shortest path: the Manhattan Distance between the location of the data and square 1.

+

For example:

+
    +
  • Data from square 1 is carried 0 steps, since it's at the access port.
  • +
  • Data from square 12 is carried 3 steps, such as: down, left, left.
  • +
  • Data from square 23 is carried only 2 steps: up twice.
  • +
  • Data from square 1024 must be carried 31 steps.
  • +
+

How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port?

+ +
+
+
+
+
+
+
+

Analysis

I freely admit that I worked out the program I wanted to write using graph paper and some Python doodles. There's no point in trying to write a Joy program until I'm sure I understand the problem well enough.

+

The first thing I did was to write a column of numbers from 1 to n (32 as it happens) and next to them the desired output number, to look for patterns directly:

+ +
1  0
+2  1
+3  2
+4  1
+5  2
+6  1
+7  2
+8  1
+9  2
+10 3
+11 2
+12 3
+13 4
+14 3
+15 2
+16 3
+17 4
+18 3
+19 2
+20 3
+21 4
+22 3
+23 2
+24 3
+25 4
+26 5
+27 4
+28 3
+29 4
+30 5
+31 6
+32 5
+ +
+
+
+
+
+
+
+

There are four groups repeating for a given "rank", then the pattern enlarges and four groups repeat again, etc.

+ +
        1 2
+      3 2 3 4
+    5 4 3 4 5 6
+  7 6 5 4 5 6 7 8
+9 8 7 6 5 6 7 8 9 10
+
+
+

Four of this pyramid interlock to tile the plane extending from the initial "1" square.

+ +
         2   3   |    4  5   |    6  7   |    8  9
+      10 11 12 13|14 15 16 17|18 19 20 21|22 23 24 25
+
+
+

And so on.

+ +
+
+
+
+
+
+
+

We can figure out the pattern for a row of the pyramid at a given "rank" $k$:

+

$2k - 1, 2k - 2, ..., k, k + 1, k + 2, ..., 2k$

+

or

+

$k + (k - 1), k + (k - 2), ..., k, k + 1, k + 2, ..., k + k$

+

This shows that the series consists at each place of $k$ plus some number that begins at $k - 1$, decreases to zero, then increases to $k$. Each row has $2k$ members.

+ +
+
+
+
+
+
+
+

Let's figure out how, given an index into a row, we can calculate the value there. The index will be from 0 to $k - 1$.

+

Let's look at an example, with $k = 4$:

+ +
0 1 2 3 4 5 6 7
+7 6 5 4 5 6 7 8
+ +
+
+
+
+
+
In [1]:
+
+
+
k = 4
+
+ +
+
+
+ +
+
+
+
+
+

Subtract $k$ from the index and take the absolute value:

+ +
+
+
+
+
+
In [2]:
+
+
+
for n in range(2 * k):
+    print abs(n - k),
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4 3 2 1 0 1 2 3
+
+
+
+ +
+
+ +
+
+
+
+
+

Not quite. Subtract $k - 1$ from the index and take the absolute value:

+ +
+
+
+
+
+
In [3]:
+
+
+
for n in range(2 * k):
+    print abs(n - (k - 1)),
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 0 1 2 3 4
+
+
+
+ +
+
+ +
+
+
+
+
+

Great, now add $k$...

+ +
+
+
+
+
+
In [4]:
+
+
+
for n in range(2 * k):
+    print abs(n - (k - 1)) + k,
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
7 6 5 4 5 6 7 8
+
+
+
+ +
+
+ +
+
+
+
+
+

So to write a function that can give us the value of a row at a given index:

+ +
+
+
+
+
+
In [5]:
+
+
+
def row_value(k, i):
+    i %= (2 * k)  # wrap the index at the row boundary.
+    return abs(i - (k - 1)) + k
+
+ +
+
+
+ +
+
+
+
In [6]:
+
+
+
k = 5
+for i in range(2 * k):
+    print row_value(k, i),
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9 8 7 6 5 6 7 8 9 10
+
+
+
+ +
+
+ +
+
+
+
+
+

(I'm leaving out details of how I figured this all out and just giving the relevent bits. It took a little while to zero in of the aspects of the pattern that were important for the task.)

+ +
+
+
+
+
+
+
+

Finding the rank and offset of a number.

Now that we can compute the desired output value for a given rank and the offset (index) into that rank, we need to determine how to find the rank and offset of a number.

+

The rank is easy to find by iteratively stripping off the amount already covered by previous ranks until you find the one that brackets the target number. Because each row is $2k$ places and there are $4$ per rank each rank contains $8k$ places. Counting the initial square we have:

+

$corner_k = 1 + \sum_{n=1}^k 8n$

+

I'm not mathematically sophisticated enough to turn this directly into a formula (but Sympy is, see below.) I'm going to write a simple Python function to iterate and search:

+ +
+
+
+
+
+
In [7]:
+
+
+
def rank_and_offset(n):
+    assert n >= 2  # Guard the domain.
+    n -= 2  # Subtract two,
+            # one for the initial square,
+            # and one because we are counting from 1 instead of 0.
+    k = 1
+    while True:
+        m = 8 * k  # The number of places total in this rank, 4(2k).
+        if n < m:
+            return k, n % (2 * k)
+        n -= m  # Remove this rank's worth.
+        k += 1
+
+ +
+
+
+ +
+
+
+
In [8]:
+
+
+
for n in range(2, 51):
+    print n, rank_and_offset(n)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2 (1, 0)
+3 (1, 1)
+4 (1, 0)
+5 (1, 1)
+6 (1, 0)
+7 (1, 1)
+8 (1, 0)
+9 (1, 1)
+10 (2, 0)
+11 (2, 1)
+12 (2, 2)
+13 (2, 3)
+14 (2, 0)
+15 (2, 1)
+16 (2, 2)
+17 (2, 3)
+18 (2, 0)
+19 (2, 1)
+20 (2, 2)
+21 (2, 3)
+22 (2, 0)
+23 (2, 1)
+24 (2, 2)
+25 (2, 3)
+26 (3, 0)
+27 (3, 1)
+28 (3, 2)
+29 (3, 3)
+30 (3, 4)
+31 (3, 5)
+32 (3, 0)
+33 (3, 1)
+34 (3, 2)
+35 (3, 3)
+36 (3, 4)
+37 (3, 5)
+38 (3, 0)
+39 (3, 1)
+40 (3, 2)
+41 (3, 3)
+42 (3, 4)
+43 (3, 5)
+44 (3, 0)
+45 (3, 1)
+46 (3, 2)
+47 (3, 3)
+48 (3, 4)
+49 (3, 5)
+50 (4, 0)
+
+
+
+ +
+
+ +
+
+
+
In [9]:
+
+
+
for n in range(2, 51):
+    k, i = rank_and_offset(n)
+    print n, row_value(k, i)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2 1
+3 2
+4 1
+5 2
+6 1
+7 2
+8 1
+9 2
+10 3
+11 2
+12 3
+13 4
+14 3
+15 2
+16 3
+17 4
+18 3
+19 2
+20 3
+21 4
+22 3
+23 2
+24 3
+25 4
+26 5
+27 4
+28 3
+29 4
+30 5
+31 6
+32 5
+33 4
+34 3
+35 4
+36 5
+37 6
+38 5
+39 4
+40 3
+41 4
+42 5
+43 6
+44 5
+45 4
+46 3
+47 4
+48 5
+49 6
+50 7
+
+
+
+ +
+
+ +
+
+
+
+
+

Putting it all together

+
+
+
+
+
+
In [10]:
+
+
+
def row_value(k, i):
+    return abs(i - (k - 1)) + k
+
+
+def rank_and_offset(n):
+    n -= 2  # Subtract two,
+            # one for the initial square,
+            # and one because we are counting from 1 instead of 0.
+    k = 1
+    while True:
+        m = 8 * k  # The number of places total in this rank, 4(2k).
+        if n < m:
+            return k, n % (2 * k)
+        n -= m  # Remove this rank's worth.
+        k += 1
+
+
+def aoc20173(n):
+    if n <= 1:
+        return 0
+    k, i = rank_and_offset(n)
+    return row_value(k, i)
+
+ +
+
+
+ +
+
+
+
In [11]:
+
+
+
aoc20173(23)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[11]:
+ + + + +
+
2
+
+ +
+ +
+
+ +
+
+
+
In [12]:
+
+
+
aoc20173(23000)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[12]:
+ + + + +
+
105
+
+ +
+ +
+
+ +
+
+
+
In [13]:
+
+
+
aoc20173(23000000000000)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[13]:
+ + + + +
+
4572225
+
+ +
+ +
+
+ +
+
+
+
+
+

Sympy to the Rescue

Find the rank for large numbers

Using e.g. Sympy we can find the rank directly by solving for the roots of an equation. For large numbers this will (eventually) be faster than iterating as rank_and_offset() does.

+ +
+
+
+
+
+
In [14]:
+
+
+
from sympy import floor, lambdify, solve, symbols
+from sympy import init_printing
+init_printing() 
+
+ +
+
+
+ +
+
+
+
In [15]:
+
+
+
k = symbols('k')
+
+ +
+
+
+ +
+
+
+
+
+

Since

+

$1 + 2 + 3 + ... + N = \frac{N(N + 1)}{2}$

+

and

+

$\sum_{n=1}^k 8n = 8(\sum_{n=1}^k n) = 8\frac{k(k + 1)}{2}$

+

We want:

+ +
+
+
+
+
+
In [16]:
+
+
+
E = 2 + 8 * k * (k + 1) / 2  # For the reason for adding 2 see above.
+
+E
+
+ +
+
+
+ +
+
+ + +
+ +
Out[16]:
+ + + + +
+$$4 k \left(k + 1\right) + 2$$ +
+ +
+ +
+
+ +
+
+
+
+
+

We can write a function to solve for $k$ given some $n$...

+ +
+
+
+
+
+
In [17]:
+
+
+
def rank_of(n):
+    return floor(max(solve(E - n, k))) + 1
+
+ +
+
+
+ +
+
+
+
+
+

First solve() for $E - n = 0$ which has two solutions (because the equation is quadratic so it has two roots) and since we only care about the larger one we use max() to select it. It will generally not be a nice integer (unless $n$ is the number of an end-corner of a rank) so we take the floor() and add 1 to get the integer rank of $n$. (Taking the ceiling() gives off-by-one errors on the rank boundaries. I don't know why. I'm basically like a monkey doing math here.) =-D

+

It gives correct answers:

+ +
+
+
+
+
+
In [18]:
+
+
+
for n in (9, 10, 25, 26, 49, 50):
+    print n, rank_of(n)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9 1
+10 2
+25 2
+26 3
+49 3
+50 4
+
+
+
+ +
+
+ +
+
+
+
+
+

And it runs much faster (at least for large numbers):

+ +
+
+
+
+
+
In [19]:
+
+
+
%time rank_of(23000000000000)  # Compare runtime with rank_and_offset()!
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
CPU times: user 68 ms, sys: 8 ms, total: 76 ms
+Wall time: 73.8 ms
+
+
+
+ +
+ +
Out[19]:
+ + + + +
+$$2397916$$ +
+ +
+ +
+
+ +
+
+
+
In [20]:
+
+
+
%time rank_and_offset(23000000000000)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
CPU times: user 308 ms, sys: 0 ns, total: 308 ms
+Wall time: 306 ms
+
+
+
+ +
+ +
Out[20]:
+ + + + +
+$$\left ( 2397916, \quad 223606\right )$$ +
+ +
+ +
+
+ +
+
+
+
+
+

After finding the rank you would still have to find the actual value of the rank's first corner and subtract it (plus 2) from the number and compute the offset as above and then the final output, but this overhead is partially shared by the other method, and overshadowed by the time it (the other iterative method) would take for really big inputs.

+

The fun thing to do here would be to graph the actual runtime of both methods against each other to find the trade-off point.

+ +
+
+
+
+
+
+
+

It took me a second to realize I could do this...

Sympy is a symbolic math library, and it supports symbolic manipulation of equations. I can put in $y$ (instead of a value) and ask it to solve for $k$.

+ +
+
+
+
+
+
In [21]:
+
+
+
y = symbols('y')
+
+ +
+
+
+ +
+
+
+
In [22]:
+
+
+
g, f = solve(E - y, k)
+
+ +
+
+
+ +
+
+
+
+
+

The equation is quadratic so there are two roots, we are interested in the greater one...

+ +
+
+
+
+
+
In [23]:
+
+
+
g
+
+ +
+
+
+ +
+
+ + +
+ +
Out[23]:
+ + + + +
+$$- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$ +
+ +
+ +
+
+ +
+
+
+
In [24]:
+
+
+
f
+
+ +
+
+
+ +
+
+ + +
+ +
Out[24]:
+ + + + +
+$$\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$ +
+ +
+ +
+
+ +
+
+
+
+
+

Now we can take the floor(), add 1, and lambdify() the equation to get a Python function that calculates the rank directly.

+ +
+
+
+
+
+
In [25]:
+
+
+
floor(f) + 1
+
+ +
+
+
+ +
+
+ + +
+ +
Out[25]:
+ + + + +
+$$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$ +
+ +
+ +
+
+ +
+
+
+
In [26]:
+
+
+
F = lambdify(y, floor(f) + 1)
+
+ +
+
+
+ +
+
+
+
In [27]:
+
+
+
for n in (9, 10, 25, 26, 49, 50):
+    print n, int(F(n))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
9 1
+10 2
+25 2
+26 3
+49 3
+50 4
+
+
+
+ +
+
+ +
+
+
+
+
+

It's pretty fast.

+ +
+
+
+
+
+
In [28]:
+
+
+
%time int(F(23000000000000))  # The clear winner.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
+Wall time: 11.9 µs
+
+
+
+ +
+ +
Out[28]:
+ + + + +
+$$2397916$$ +
+ +
+ +
+
+ +
+
+
+
+
+

Knowing the equation we could write our own function manually, but the speed is no better.

+ +
+
+
+
+
+
In [29]:
+
+
+
from math import floor as mfloor, sqrt
+
+def mrank_of(n):
+    return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1)
+
+ +
+
+
+ +
+
+
+
In [30]:
+
+
+
%time mrank_of(23000000000000)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
+Wall time: 12.9 µs
+
+
+
+ +
+ +
Out[30]:
+ + + + +
+$$2397916$$ +
+ +
+ +
+
+ +
+
+
+
+
+

Given $n$ and a rank, compute the offset.

Now that we have a fast way to get the rank, we still need to use it to compute the offset into a pyramid row.

+ +
+
+
+
+
+
In [31]:
+
+
+
def offset_of(n, k):
+    return (n - 2 + 4 * k * (k - 1)) % (2 * k)
+
+ +
+
+
+ +
+
+
+
+
+

(Note the sneaky way the sign changes from $k(k + 1)$ to $k(k - 1)$. This is because we want to subract the $(k - 1)$th rank's total places (its own and those of lesser rank) from our $n$ of rank $k$. Substituting $k - 1$ for $k$ in $k(k + 1)$ gives $(k - 1)(k - 1 + 1)$, which of course simplifies to $k(k - 1)$.)

+ +
+
+
+
+
+
In [32]:
+
+
+
offset_of(23000000000000, 2397916)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[32]:
+ + + + +
+$$223606$$ +
+ +
+ +
+
+ +
+
+
+
+
+

So, we can compute the rank, then the offset, then the row value.

+ +
+
+
+
+
+
In [33]:
+
+
+
def rank_of(n):
+    return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)
+
+
+def offset_of(n, k):
+    return (n - 2 + 4 * k * (k - 1)) % (2 * k)
+
+
+def row_value(k, i):
+    return abs(i - (k - 1)) + k
+
+
+def aoc20173(n):
+    k = rank_of(n)
+    i = offset_of(n, k)
+    return row_value(k, i)
+
+ +
+
+
+ +
+
+
+
In [34]:
+
+
+
aoc20173(23)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[34]:
+ + + + +
+$$2$$ +
+ +
+ +
+
+ +
+
+
+
In [35]:
+
+
+
aoc20173(23000)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[35]:
+ + + + +
+$$105$$ +
+ +
+ +
+
+ +
+
+
+
In [36]:
+
+
+
aoc20173(23000000000000)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[36]:
+ + + + +
+$$4572225$$ +
+ +
+ +
+
+ +
+
+
+
In [37]:
+
+
+
%time aoc20173(23000000000000000000000000)  # Fast for large values.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
+Wall time: 20 µs
+
+
+
+ +
+ +
Out[37]:
+ + + + +
+$$2690062495969$$ +
+ +
+ +
+
+ +
+
+
+
+
+

A Joy Version

At this point I feel confident that I can implement a concise version of this code in Joy. ;-)

+ +
+
+
+
+
+
In [38]:
+
+
+
from notebook_preamble import J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

rank_of

+
   n rank_of
+---------------
+      k
+
+
+

The translation is straightforward.

+ +
int(floor(sqrt(n - 1) / 2 - 0.5) + 1)
+
+rank_of == -- sqrt 2 / 0.5 - floor ++
+ +
+
+
+
+
+
In [39]:
+
+
+
define('rank_of == -- sqrt 2 / 0.5 - floor ++')
+
+ +
+
+
+ +
+
+
+
+
+

offset_of

+
   n k offset_of
+-------------------
+         i
+
+(n - 2 + 4 * k * (k - 1)) % (2 * k)
+
+
+

A little tricky...

+ +
n k dup 2 *
+n k k 2 *
+n k k*2 [Q] dip %
+n k Q k*2 %
+
+n k dup --
+n k k --
+n k k-1 4 * * 2 + -
+n k*k-1*4     2 + -
+n k*k-1*4+2       -
+n-k*k-1*4+2
+
+n-k*k-1*4+2 k*2 %
+n-k*k-1*4+2%k*2
+
+
+

Ergo:

+ +
offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %
+ +
+
+
+
+
+
In [40]:
+
+
+
define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %')
+
+ +
+
+
+ +
+
+
+
+
+

row_value

+
   k i row_value
+-------------------
+        n
+
+abs(i - (k - 1)) + k
+
+k i over -- - abs +
+k i k    -- - abs +
+k i k-1     - abs +
+k i-k-1       abs +
+k |i-k-1|         +
+k+|i-k-1|
+ +
+
+
+
+
+
In [41]:
+
+
+
define('row_value == over -- - abs +')
+
+ +
+
+
+ +
+
+
+
+
+

aoc2017.3

+
   n aoc2017.3
+-----------------
+        m
+
+n dup rank_of
+n k [offset_of] dupdip
+n k offset_of k
+i             k swap row_value
+k i row_value
+m
+ +
+
+
+
+
+
In [42]:
+
+
+
define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value')
+
+ +
+
+
+ +
+
+
+
In [43]:
+
+
+
J('23 aoc2017.3')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [44]:
+
+
+
J('23000 aoc2017.3')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
105
+
+
+
+ +
+
+ +
+
+
+
In [45]:
+
+
+
V('23000000000000 aoc2017.3')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                    . 23000000000000 aoc2017.3
+                                     23000000000000 . aoc2017.3
+                                     23000000000000 . dup rank_of [offset_of] dupdip swap row_value
+                      23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value
+                      23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
+                      23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
+                   23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
+                 23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value
+                  23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value
+              23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value
+                  23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value
+                             23000000000000 2397915 . ++ [offset_of] dupdip swap row_value
+                             23000000000000 2397916 . [offset_of] dupdip swap row_value
+                 23000000000000 2397916 [offset_of] . dupdip swap row_value
+                             23000000000000 2397916 . offset_of 2397916 swap row_value
+                             23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
+                     23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
+                   23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
+                     23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
+23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value
+                             23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
+                     23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value
+                     23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value
+                   23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value
+                     23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value
+                      23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value
+                    23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value
+                      23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value
+                                            5019438 . 4795832 % 2397916 swap row_value
+                                    5019438 4795832 . % 2397916 swap row_value
+                                             223606 . 2397916 swap row_value
+                                     223606 2397916 . swap row_value
+                                     2397916 223606 . row_value
+                                     2397916 223606 . over -- - abs +
+                             2397916 223606 2397916 . -- - abs +
+                             2397916 223606 2397915 . - abs +
+                                   2397916 -2174309 . abs +
+                                    2397916 2174309 . +
+                                            4572225 . 
+
+
+
+ +
+
+ +
+
+
+
+
+ +
  rank_of == -- sqrt 2 / 0.5 - floor ++
+offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %
+row_value == over -- - abs +
+
+aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value
+ +
+
+
+
+
+ + + + + + diff --git a/docs/Advent_of_Code_2017_December_3rd.ipynb b/docs/Advent_of_Code_2017_December_3rd.ipynb new file mode 100644 index 0000000..cdb7d80 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_3rd.ipynb @@ -0,0 +1,1426 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advent of Code 2017\n", + "\n", + "## December 3rd\n", + "\n", + "You come across an experimental new kind of memory stored on an infinite two-dimensional grid.\n", + "\n", + "Each square on the grid is allocated in a spiral pattern starting at a location marked 1 and then counting up while spiraling outward. For example, the first few squares are allocated like this:\n", + "\n", + " 17 16 15 14 13\n", + " 18 5 4 3 12\n", + " 19 6 1 2 11\n", + " 20 7 8 9 10\n", + " 21 22 23---> ...\n", + "\n", + "While this is very space-efficient (no squares are skipped), requested data must be carried back to square 1 (the location of the only access port for this memory system) by programs that can only move up, down, left, or right. They always take the shortest path: the Manhattan Distance between the location of the data and square 1.\n", + "\n", + "For example:\n", + "\n", + "* Data from square 1 is carried 0 steps, since it's at the access port.\n", + "* Data from square 12 is carried 3 steps, such as: down, left, left.\n", + "* Data from square 23 is carried only 2 steps: up twice.\n", + "* Data from square 1024 must be carried 31 steps.\n", + "\n", + "How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Analysis\n", + "\n", + "I freely admit that I worked out the program I wanted to write using graph paper and some Python doodles. There's no point in trying to write a Joy program until I'm sure I understand the problem well enough.\n", + "\n", + "The first thing I did was to write a column of numbers from 1 to n (32 as it happens) and next to them the desired output number, to look for patterns directly:\n", + "\n", + " 1 0\n", + " 2 1\n", + " 3 2\n", + " 4 1\n", + " 5 2\n", + " 6 1\n", + " 7 2\n", + " 8 1\n", + " 9 2\n", + " 10 3\n", + " 11 2\n", + " 12 3\n", + " 13 4\n", + " 14 3\n", + " 15 2\n", + " 16 3\n", + " 17 4\n", + " 18 3\n", + " 19 2\n", + " 20 3\n", + " 21 4\n", + " 22 3\n", + " 23 2\n", + " 24 3\n", + " 25 4\n", + " 26 5\n", + " 27 4\n", + " 28 3\n", + " 29 4\n", + " 30 5\n", + " 31 6\n", + " 32 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are four groups repeating for a given \"rank\", then the pattern enlarges and four groups repeat again, etc.\n", + "\n", + " 1 2\n", + " 3 2 3 4\n", + " 5 4 3 4 5 6\n", + " 7 6 5 4 5 6 7 8\n", + " 9 8 7 6 5 6 7 8 9 10\n", + "\n", + "Four of this pyramid interlock to tile the plane extending from the initial \"1\" square.\n", + "\n", + "\n", + " 2 3 | 4 5 | 6 7 | 8 9\n", + " 10 11 12 13|14 15 16 17|18 19 20 21|22 23 24 25\n", + "\n", + "And so on." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can figure out the pattern for a row of the pyramid at a given \"rank\" $k$:\n", + "\n", + "$2k - 1, 2k - 2, ..., k, k + 1, k + 2, ..., 2k$\n", + "\n", + "or\n", + "\n", + "$k + (k - 1), k + (k - 2), ..., k, k + 1, k + 2, ..., k + k$\n", + "\n", + "This shows that the series consists at each place of $k$ plus some number that begins at $k - 1$, decreases to zero, then increases to $k$. Each row has $2k$ members." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's figure out how, given an index into a row, we can calculate the value there. The index will be from 0 to $k - 1$. \n", + "\n", + " Let's look at an example, with $k = 4$:\n", + "\n", + " 0 1 2 3 4 5 6 7\n", + " 7 6 5 4 5 6 7 8" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "k = 4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Subtract $k$ from the index and take the absolute value:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 3 2 1 0 1 2 3\n" + ] + } + ], + "source": [ + "for n in range(2 * k):\n", + " print abs(n - k)," + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Not quite. Subtract $k - 1$ from the index and take the absolute value:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 0 1 2 3 4\n" + ] + } + ], + "source": [ + "for n in range(2 * k):\n", + " print abs(n - (k - 1))," + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Great, now add $k$..." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7 6 5 4 5 6 7 8\n" + ] + } + ], + "source": [ + "for n in range(2 * k):\n", + " print abs(n - (k - 1)) + k," + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So to write a function that can give us the value of a row at a given index:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def row_value(k, i):\n", + " i %= (2 * k) # wrap the index at the row boundary.\n", + " return abs(i - (k - 1)) + k" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 8 7 6 5 6 7 8 9 10\n" + ] + } + ], + "source": [ + "k = 5\n", + "for i in range(2 * k):\n", + " print row_value(k, i)," + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "(I'm leaving out details of how I figured this all out and just giving the relevent bits. It took a little while to zero in of the aspects of the pattern that were important for the task.)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding the rank and offset of a number.\n", + "Now that we can compute the desired output value for a given rank and the offset (index) into that rank, we need to determine how to find the rank and offset of a number.\n", + "\n", + "The rank is easy to find by iteratively stripping off the amount already covered by previous ranks until you find the one that brackets the target number. Because each row is $2k$ places and there are $4$ per rank each rank contains $8k$ places. Counting the initial square we have:\n", + "\n", + "$corner_k = 1 + \\sum_{n=1}^k 8n$\n", + "\n", + "I'm not mathematically sophisticated enough to turn this directly into a formula (but Sympy is, see below.) I'm going to write a simple Python function to iterate and search:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def rank_and_offset(n):\n", + " assert n >= 2 # Guard the domain.\n", + " n -= 2 # Subtract two,\n", + " # one for the initial square,\n", + " # and one because we are counting from 1 instead of 0.\n", + " k = 1\n", + " while True:\n", + " m = 8 * k # The number of places total in this rank, 4(2k).\n", + " if n < m:\n", + " return k, n % (2 * k)\n", + " n -= m # Remove this rank's worth.\n", + " k += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 (1, 0)\n", + "3 (1, 1)\n", + "4 (1, 0)\n", + "5 (1, 1)\n", + "6 (1, 0)\n", + "7 (1, 1)\n", + "8 (1, 0)\n", + "9 (1, 1)\n", + "10 (2, 0)\n", + "11 (2, 1)\n", + "12 (2, 2)\n", + "13 (2, 3)\n", + "14 (2, 0)\n", + "15 (2, 1)\n", + "16 (2, 2)\n", + "17 (2, 3)\n", + "18 (2, 0)\n", + "19 (2, 1)\n", + "20 (2, 2)\n", + "21 (2, 3)\n", + "22 (2, 0)\n", + "23 (2, 1)\n", + "24 (2, 2)\n", + "25 (2, 3)\n", + "26 (3, 0)\n", + "27 (3, 1)\n", + "28 (3, 2)\n", + "29 (3, 3)\n", + "30 (3, 4)\n", + "31 (3, 5)\n", + "32 (3, 0)\n", + "33 (3, 1)\n", + "34 (3, 2)\n", + "35 (3, 3)\n", + "36 (3, 4)\n", + "37 (3, 5)\n", + "38 (3, 0)\n", + "39 (3, 1)\n", + "40 (3, 2)\n", + "41 (3, 3)\n", + "42 (3, 4)\n", + "43 (3, 5)\n", + "44 (3, 0)\n", + "45 (3, 1)\n", + "46 (3, 2)\n", + "47 (3, 3)\n", + "48 (3, 4)\n", + "49 (3, 5)\n", + "50 (4, 0)\n" + ] + } + ], + "source": [ + "for n in range(2, 51):\n", + " print n, rank_and_offset(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 1\n", + "3 2\n", + "4 1\n", + "5 2\n", + "6 1\n", + "7 2\n", + "8 1\n", + "9 2\n", + "10 3\n", + "11 2\n", + "12 3\n", + "13 4\n", + "14 3\n", + "15 2\n", + "16 3\n", + "17 4\n", + "18 3\n", + "19 2\n", + "20 3\n", + "21 4\n", + "22 3\n", + "23 2\n", + "24 3\n", + "25 4\n", + "26 5\n", + "27 4\n", + "28 3\n", + "29 4\n", + "30 5\n", + "31 6\n", + "32 5\n", + "33 4\n", + "34 3\n", + "35 4\n", + "36 5\n", + "37 6\n", + "38 5\n", + "39 4\n", + "40 3\n", + "41 4\n", + "42 5\n", + "43 6\n", + "44 5\n", + "45 4\n", + "46 3\n", + "47 4\n", + "48 5\n", + "49 6\n", + "50 7\n" + ] + } + ], + "source": [ + "for n in range(2, 51):\n", + " k, i = rank_and_offset(n)\n", + " print n, row_value(k, i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Putting it all together" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def row_value(k, i):\n", + " return abs(i - (k - 1)) + k\n", + "\n", + "\n", + "def rank_and_offset(n):\n", + " n -= 2 # Subtract two,\n", + " # one for the initial square,\n", + " # and one because we are counting from 1 instead of 0.\n", + " k = 1\n", + " while True:\n", + " m = 8 * k # The number of places total in this rank, 4(2k).\n", + " if n < m:\n", + " return k, n % (2 * k)\n", + " n -= m # Remove this rank's worth.\n", + " k += 1\n", + "\n", + "\n", + "def aoc20173(n):\n", + " if n <= 1:\n", + " return 0\n", + " k, i = rank_and_offset(n)\n", + " return row_value(k, i)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aoc20173(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "105" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aoc20173(23000)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4572225" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aoc20173(23000000000000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Sympy to the Rescue\n", + "### Find the rank for large numbers\n", + "Using e.g. Sympy we can find the rank directly by solving for the roots of an equation. For large numbers this will (eventually) be faster than iterating as `rank_and_offset()` does." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "from sympy import floor, lambdify, solve, symbols\n", + "from sympy import init_printing\n", + "init_printing() " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "k = symbols('k')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Since\n", + "\n", + "$1 + 2 + 3 + ... + N = \\frac{N(N + 1)}{2}$\n", + "\n", + "and\n", + "\n", + "$\\sum_{n=1}^k 8n = 8(\\sum_{n=1}^k n) = 8\\frac{k(k + 1)}{2}$\n", + "\n", + "We want:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHwAAAAUBAMAAAC9l0S6AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBU\nq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACAElEQVQ4EW1UzWsTQRx9NB+bzSZt6D/gHvRS\nKOZQWhAK6UHrsRShhyIW2rvBiznp0ltPxdJLDi2h9aIXc7C0emlA6k38wKKXYsA/IFZEhITG95vZ\nj5klD3bmvff7vczsMBvARsOWiSrUE26zSeD1uracGrw7Vbscqm9p99H1BWXNcNzRRZdruL7mxvi+\nDbwxtFDvE151Oec2OAz4EJt8JjrCBGd68r78YLy4pFXkltvIr9Fq7ALjfV2b49Rjr0YYBx7Q8tZs\nN19F+ZJWi/FiVdUcmQ7Dtnh1Hcd+6Ic/6vZVvFBn3PVL9yt8D58tOzj7rDut1XFgx6ky3PMRGD/t\nrPQCIMvHG+Se/IOCHd/SZvTuVL0W9y7xj0ftE+pMByj1V72AnLDj77SZuJhnd0XiN31Vy3eBsd+6\n7UWzOdts7ikhR4drQk13zAfeQuLz95akKPHM+W2hAnt1FTfdZYoPFxd/z70r50r6ZfOn3add4YQd\n/6nN2C35eCzWBgq/vEGRLBsA2zzHFhTseProeNdXpO0PyjVcSrzoA18xUWmRE3Z8SpuR69x6OS3X\n5PnwezbA8irpOPVd5G7ISRFRfPrhXgA8U17susPhUOIm5NImiOLKUTdSmOUmzcLkk0lwnFDjk7Fc\ns4NnV7e1odIfrFGKqVOLaZospo1RujHKFG/0n9V/fdOG1o/ONcUAAAAASUVORK5CYII=\n", + "text/latex": [ + "$$4 k \\left(k + 1\\right) + 2$$" + ], + "text/plain": [ + "4⋅k⋅(k + 1) + 2" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "E = 2 + 8 * k * (k + 1) / 2 # For the reason for adding 2 see above.\n", + "\n", + "E" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can write a function to solve for $k$ given some $n$..." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def rank_of(n):\n", + " return floor(max(solve(E - n, k))) + 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First `solve()` for $E - n = 0$ which has two solutions (because the equation is quadratic so it has two roots) and since we only care about the larger one we use `max()` to select it. It will generally not be a nice integer (unless $n$ is the number of an end-corner of a rank) so we take the `floor()` and add 1 to get the integer rank of $n$. (Taking the `ceiling()` gives off-by-one errors on the rank boundaries. I don't know why. I'm basically like a monkey doing math here.) =-D\n", + "\n", + "It gives correct answers:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 1\n", + "10 2\n", + "25 2\n", + "26 3\n", + "49 3\n", + "50 4\n" + ] + } + ], + "source": [ + "for n in (9, 10, 25, 26, 49, 50):\n", + " print n, rank_of(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And it runs much faster (at least for large numbers):" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 68 ms, sys: 8 ms, total: 76 ms\n", + "Wall time: 73.8 ms\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAPBAMAAABElc8tAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkUlEQVQoFY2QPUscURSGn4l7d4y7q6ONxGqZ\ngEbSCFZWTmfpYCHEFK6CRdIoJmQaYacNBBJJ4wcqKNhYaKNYBFeCnYKLjdhNK4j5NrKbOHlnxx+Q\nAzP33nOf+77nHCy338MKXlUxPW8cMps9QcBIMMF92HOvYRT7lg4KdzyLrHla4zi+Mas8cuDCB7PP\nU5iCRU6r1HgBkzzQZSn7gWzRTE4LairSD0sw7b0NTZ06lLHB9tp2sL/CqaD3egQVXxCyMz+U1o53\njPeR/5lCA0o0YlsvxmZYllKoRB8PpXSbQvWhz0mO5t/Que7Li0oktyjxyt01IFOPWBFDS0k/e4Hc\nYaFchXGdPnH+N4Vin14Z4epTiz5XR2UPjnVoPRm6r6kGX0LIF6EdBiVC0vSGVsj+SmvaEhTBGZYj\n0Qa0p+ndNKBcKYU0PClliuSdj7DtXDqZb5D5Lrd5hp0UGlZN0BXMvuSawh+O/ecSLgjK75oD6SXD\nzM4YdVeJ4xrN7uMQ232iGyvpeNYNoXttL9K221PiP+If4oN7f8ioQFsAAAAASUVORK5CYII=\n", + "text/latex": [ + "$$2397916$$" + ], + "text/plain": [ + "2397916" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%time rank_of(23000000000000) # Compare runtime with rank_and_offset()!" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 308 ms, sys: 0 ns, total: 308 ms\n", + "Wall time: 306 ms\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAALAAAAAUBAMAAADfFKqVAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIky\nEKtZsEGBAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADLElEQVQ4Ea2VT2hcVRjFf28yfzNzZ6ZIC9LN\nQwpSXDQahCpiYzdCqWagnYUGw4h/EAo1QkMXin0b3biYqYqkwcJsrLiQBHQMMmJm46qLeUJUumlS\nQaG0UhvHWGrT6bn3PYszuBih3+Lcf+ee993zvncfeD73PBJlSd4HO488Abvb++GH9iUKx2fabczM\nt3bVRfvNTty723jVyQ4RtL8IIL/yjbY8ZHmWfUntU5gNVptehbpv3uf+cqLf79/mgu+dhuxZMdaa\nqSk1A3GR/E0c7CS7iTnEUZKBeTFmr0GiQjEk18jcoFQrNihWUsp0g8vwCxdntyX3KGMTA6oa/AZn\nIjgVsMVYhUn2wpcxOz9HukyuRvHW+CLrU6Up8r/ndbAOPViFjITHrw+ravwRXOs4+KxpeixZ256E\nehixTYNdkNyWsBbq/voEGdv7FPMHnOo44bGGZoZjOpSwA/m1yWN2/TYsBTH7EPNuS1qZmWPklPFN\nTejkHyrjphMuHV553pGG4PVQExZ+XKZ3fr5p/pTwXMxu8YijLy3jffWCfCC5KSd858O074TXD5IL\nHGsAxv/W0MLuV0LT81nw1O8ux+w9vObYxy1+3eRp3rsB6xok57KrQSS8TcoWx1CkNzThIL9o+iHP\nfa+MJRyx32XR8lMVi7mzJE6el8cn7OjBt1Yjj0s1EtafoajasQPOhFuw73MJy4qI3Y2E58ErM6Zk\nKcrtlyKNa6HLOD1FQv4MRcbmYmEHHAhelrCvl1cPYnbXWZGtsKO0HQknNzBi2HhWO/UYFfd/ZPyO\nTcWCXDgQqOr3NScFYcw+517ed/BGboL09cRpumUKOhN7ywXVrxUuyLWGe9K/oFAhU3bwgUqj3JXH\n9gM5+g97Dy25cKw9W0v41Ocyz5iDqksrfCJc8yNhrnAh0EcwELvarV9x8ADZvygumwXSgfkYx4YZ\nzqkAdDnU+KT6OLSqTT1oQRr5qoo3dbn3s+1ehVfDAeHpfn8LB+PVI9rTmu1gVt7uxGwO2096pMiO\nyIvEPNVGZSRddIP8j9AlxMOj8X8ajRaz1tTqoh8l/FFIdzk2W88X3OPQr+kOAx0PXfbg7EwAAAAA\nSUVORK5CYII=\n", + "text/latex": [ + "$$\\left ( 2397916, \\quad 223606\\right )$$" + ], + "text/plain": [ + "(2397916, 223606)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%time rank_and_offset(23000000000000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "After finding the rank you would still have to find the actual value of the rank's first corner and subtract it (plus 2) from the number and compute the offset as above and then the final output, but this overhead is partially shared by the other method, and overshadowed by the time it (the other iterative method) would take for really big inputs.\n", + "\n", + "The fun thing to do here would be to graph the actual runtime of both methods against each other to find the trade-off point." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### It took me a second to realize I could do this...\n", + "Sympy is a *symbolic* math library, and it supports symbolic manipulation of equations. I can put in $y$ (instead of a value) and ask it to solve for $k$." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "y = symbols('y')" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "g, f = solve(E - y, k)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The equation is quadratic so there are two roots, we are interested in the greater one..." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIkAAAAqBAMAAAB1gmW6AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEM3dMlTvq5l2ZiK7\niUTiBfEGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB6klEQVRIDe2Uv0vDQBTHv9eiTU2pRQdxsqLo\n4CI6CEKxo5tObqKOThYHEUEUlyKIOklFwf4J4iQ42EEQt86C6Ca62IogqKh3lzQ/nkm8BEdvuL73\nve/ny/WlDfDXayoTJdFNseW1CCk/qaMIKQClaK/2/ShF+/8UYwLuuex8eS86LTdFnpleoXafPjAl\ncedDUTkw5YS6/XqSUlzoL9vWbbsMrNwUsbIsESK12qQX1jTnpfpr7V5HHcW6l+yvXXseJcOlsMG/\nSEmWgc5N6GQOIe8S51epTiFZc19JPYUJcB/QJzeQqiimtA2L1QvI/14Nek6AfCwM82jd5bXlAIy7\nsB6BDOWF0aRE6VyPfbxrqfIt/Y6JvPOokeLWvLuZMhDP8DMtiz1iUZ9L8yAwLehUAZeRU5KfQLeg\ntUr6LXIK+0DTuqDZaundnaItPa+4lZ/d6daFFHeOY8fGKZ/Mr6tBmUZWwO2dqM+r91JaRJfsZeO3\nWZRpSGTQPCvqVG1ASqO4kp+Bm0WZLv5sEi+iTr8WpPRQysvPwM2iTFesbqZgTIFuRNtUQ0HceH0c\nWoJSYVKW96lqlSEKSo2EYG0robR1+0i9olRJHXU4CcV/92eOU8WSUuPAgSLqsBFKz90U+Ush5KJU\njL/8wqcY1Dcp15UsZrvPlwAAAABJRU5ErkJggg==\n", + "text/latex": [ + "$$- \\frac{1}{2} \\sqrt{y - 1} - \\frac{1}{2}$$" + ], + "text/plain": [ + " _______ \n", + " ╲╱ y - 1 1\n", + "- ───────── - ─\n", + " 2 2" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "g" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHgAAAAqBAMAAACKDIIdAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiuzKJ\nRN0MreaOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB9ElEQVRIDe2Vu0vDUBTGvzRNSrSNILgX3TVD\nwU2LD3DRVqgIOthF0akdXETQbi4OVXDQoTg46aCLexedCvY/aEHBRVQQfOCj3nOrmCa3ubGuHmjP\nyXe+3z1tb24K/C06rVZ4pYdR6miiFTiWeuAjM63A0P/h322Y8AfTauK4d6wthPccpmaXQni8mduh\ni2Aj6zA1u6zDqb7l3I9DK//UXpU29DTm6ve7lF8Ik0Kvv4OnVgWw34MXzglgwN/BGxCyPuFF9nSY\nT2PbalzE3+QqEFaiyDSy8sk0TE8DW1oWhz5hfgzugUKRASZ7WYE0HllW125ZXJ+w8mtyPwm3qyR0\ncIwqishGlL1vUlnJt79StoXsOyvPzHxJwDT0KGVbyGD0AkqRgAO0xSnbQgpnygjxHZpBJWcDqZTC\ng/vo4kzsfC7fCDcePHtvZ+aIX2ovWPnSp+19cW1MHbJ9UOO4KJPBeIvEKQez6jtl74ghzFxBC4Eo\nNx6H+Kc1c21pb5C6w0A3EKgiWN/WyghnIguzcpb2JnGC0MM3bDpvDa811i2CWZj1Pzx938vt7t1Z\npFWK7o5cibxxz5XcKXCYcRK1pKAll6a4ZV5uFDh0PlJJYlfQlEldMPLAKbAkc7r77UnoeRg3pVTV\n3ZQphdLZBBBiD5QW4PVa7QOfZ0qXehvwRMIAAAAASUVORK5CYII=\n", + "text/latex": [ + "$$\\frac{1}{2} \\sqrt{y - 1} - \\frac{1}{2}$$" + ], + "text/plain": [ + " _______ \n", + "╲╱ y - 1 1\n", + "───────── - ─\n", + " 2 2" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can take the `floor()`, add 1, and `lambdify()` the equation to get a Python function that calculates the rank directly." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAK0AAAAqBAMAAAAzNMYQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEGYiq+/dVJl2zbsy\niUT7WiApAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACUUlEQVRIDe2Uv2/TQBTHv1YgsdMmRCUwdCEz\nElIHCANLqAAx0TLASpEgAwxMSGXrUImqVCJigimRqBgipHZg60D+BEZAQup/EH5ISCBKuPfsc8/2\nw2fHYuOk5L177/v9+Hw+G/ino3J9GrzV1Vz/MgU3g8ubhgu7y66Q7sfusiv+c6Ud8Gv23Ysq3Ik8\nPscuEXXFmjyNKi5IEqEWdQmC2Em8JkmEWhp3jvURRWkgMKRS4PKk3lkqupd/Lh423f3DPC3Trqok\nYm60MR+dWmdZuY9F0sOGWFbFBJekyfU6KwLAubpk5T7yfb40ya21BC7Qz8r1pUnuSRFbnPsMcJ73\ncC62vsLrXQFqlWX0Y8suwqUlej3gvDvAXnHuMf7MAO13ilVWv8bRHn6o6LzqqnFvpNJgf+ep0H1N\nBe3yz9nH4fDucLhNDZYaz61+elkVz1BnvDD7i6IxCuxD5avi3CbWJjy6hjkKcHETqNBe4A1mOiZU\n5UW4/X1UG8R7gnGLojGKcE/tYpVRzQ9PFwymStdvbbeilXCmvw/Be+xL+bmVNvZGJHO/4WUg3wxi\nStCuOJctzG2i9ptmpYN6h+KRgcNzyv8+tEtzj5tS5l4EbnDxbZVvv9ya6ZkiOdcuzY2omKvOwdKI\nyuNL3Ky/2OKY/qddIvdE9w6w0wi45fjbkEb2Xe3u/TTRAz5f3m6aJtnzXcl6WKkfhGmOxO4qd3Lg\nQqndtRFq8yRWl7eWB6e1dtcqSnxwtSNbtLpm1+Dl59pd7U/vr2Rboqmyu3Ymk++mI1ue5voDRu6s\n5n/E50UAAAAASUVORK5CYII=\n", + "text/latex": [ + "$$\\lfloor{\\frac{1}{2} \\sqrt{y - 1} - \\frac{1}{2}}\\rfloor + 1$$" + ], + "text/plain": [ + "⎢ _______ ⎥ \n", + "⎢╲╱ y - 1 1⎥ \n", + "⎢───────── - ─⎥ + 1\n", + "⎣ 2 2⎦ " + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "floor(f) + 1" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "F = lambdify(y, floor(f) + 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 1\n", + "10 2\n", + "25 2\n", + "26 3\n", + "49 3\n", + "50 4\n" + ] + } + ], + "source": [ + "for n in (9, 10, 25, 26, 49, 50):\n", + " print n, int(F(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It's pretty fast." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", + "Wall time: 11.9 µs\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAPBAMAAABElc8tAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkUlEQVQoFY2QPUscURSGn4l7d4y7q6ONxGqZ\ngEbSCFZWTmfpYCHEFK6CRdIoJmQaYacNBBJJ4wcqKNhYaKNYBFeCnYKLjdhNK4j5NrKbOHlnxx+Q\nAzP33nOf+77nHCy338MKXlUxPW8cMps9QcBIMMF92HOvYRT7lg4KdzyLrHla4zi+Mas8cuDCB7PP\nU5iCRU6r1HgBkzzQZSn7gWzRTE4LairSD0sw7b0NTZ06lLHB9tp2sL/CqaD3egQVXxCyMz+U1o53\njPeR/5lCA0o0YlsvxmZYllKoRB8PpXSbQvWhz0mO5t/Que7Li0oktyjxyt01IFOPWBFDS0k/e4Hc\nYaFchXGdPnH+N4Vin14Z4epTiz5XR2UPjnVoPRm6r6kGX0LIF6EdBiVC0vSGVsj+SmvaEhTBGZYj\n0Qa0p+ndNKBcKYU0PClliuSdj7DtXDqZb5D5Lrd5hp0UGlZN0BXMvuSawh+O/ecSLgjK75oD6SXD\nzM4YdVeJ4xrN7uMQ232iGyvpeNYNoXttL9K221PiP+If4oN7f8ioQFsAAAAASUVORK5CYII=\n", + "text/latex": [ + "$$2397916$$" + ], + "text/plain": [ + "2397916" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%time int(F(23000000000000)) # The clear winner." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Knowing the equation we could write our own function manually, but the speed is no better." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "from math import floor as mfloor, sqrt\n", + "\n", + "def mrank_of(n):\n", + " return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", + "Wall time: 12.9 µs\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAPBAMAAABElc8tAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkUlEQVQoFY2QPUscURSGn4l7d4y7q6ONxGqZ\ngEbSCFZWTmfpYCHEFK6CRdIoJmQaYacNBBJJ4wcqKNhYaKNYBFeCnYKLjdhNK4j5NrKbOHlnxx+Q\nAzP33nOf+77nHCy338MKXlUxPW8cMps9QcBIMMF92HOvYRT7lg4KdzyLrHla4zi+Mas8cuDCB7PP\nU5iCRU6r1HgBkzzQZSn7gWzRTE4LairSD0sw7b0NTZ06lLHB9tp2sL/CqaD3egQVXxCyMz+U1o53\njPeR/5lCA0o0YlsvxmZYllKoRB8PpXSbQvWhz0mO5t/Que7Li0oktyjxyt01IFOPWBFDS0k/e4Hc\nYaFchXGdPnH+N4Vin14Z4epTiz5XR2UPjnVoPRm6r6kGX0LIF6EdBiVC0vSGVsj+SmvaEhTBGZYj\n0Qa0p+ndNKBcKYU0PClliuSdj7DtXDqZb5D5Lrd5hp0UGlZN0BXMvuSawh+O/ecSLgjK75oD6SXD\nzM4YdVeJ4xrN7uMQ232iGyvpeNYNoXttL9K221PiP+If4oN7f8ioQFsAAAAASUVORK5CYII=\n", + "text/latex": [ + "$$2397916$$" + ], + "text/plain": [ + "2397916" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%time mrank_of(23000000000000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Given $n$ and a rank, compute the offset.\n", + "\n", + "Now that we have a fast way to get the rank, we still need to use it to compute the offset into a pyramid row." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "def offset_of(n, k):\n", + " return (n - 2 + 4 * k * (k - 1)) % (2 * k)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "(Note the sneaky way the sign changes from $k(k + 1)$ to $k(k - 1)$. This is because we want to subract the $(k - 1)$th rank's total places (its own and those of lesser rank) from our $n$ of rank $k$. Substituting $k - 1$ for $k$ in $k(k + 1)$ gives $(k - 1)(k - 1 + 1)$, which of course simplifies to $k(k - 1)$.)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAADsAAAAOBAMAAABjvHmeAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABIElEQVQYGTWQoU8DMRSHvwuULWNLDgwJ6jIS\nCJlcQoLaOeSKQaAwCDAsg2WCiTMI1AQIRjIBFoHFjSCwNPwDmyMYSBBAdoTjtR2iX76+X/r6WoJy\nNcajfWgg1zlCrbzE3tgi9+0xT+kXdUeFWaOuvLELPY8nw5ipiCqvcOyNSziIHU4TldINgTUYamcM\ntMQO2ObrkvIJXePM7m71BNsN0o2HRH1IfG/NpvmvCRautUpH9AMp1FvWbFzY+UfuQmWa1U05XW9Z\ns23Lsjzo6TG8n7jm1hIoRpJazEHN3EhxJKMNvcEzQegg3Wpmz56pCrQzpiOKocOZvCGsy432Wyo4\nY7Hd3Pd4o/TDTEP1KRh17o1Blo098uUlGaW5HKM6j7G3P7xTb/ft54xWAAAAAElFTkSuQmCC\n", + "text/latex": [ + "$$223606$$" + ], + "text/plain": [ + "223606" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "offset_of(23000000000000, 2397916)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So, we can compute the rank, then the offset, then the row value." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "def rank_of(n):\n", + " return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)\n", + "\n", + "\n", + "def offset_of(n, k):\n", + " return (n - 2 + 4 * k * (k - 1)) % (2 * k)\n", + "\n", + "\n", + "def row_value(k, i):\n", + " return abs(i - (k - 1)) + k\n", + "\n", + "\n", + "def aoc20173(n):\n", + " k = rank_of(n)\n", + " i = offset_of(n, k)\n", + " return row_value(k, i)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAkAAAAOBAMAAAAPuiubAAAALVBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAOrOgAAAADnRSTlMAIpm7MhCriUTv3c12VLge\nopIAAAAJcEhZcwAADsQAAA7EAZUrDhsAAABOSURBVAgdY2BUMnZgYAhjYH/BwJDKwDCTgWEWA0Oe\nA8O+ABAJBOsCgATHcxCTKwFEKoEIHgUQeYmBUYCBRYGBR4BBqrwoi4Fh37t3rxgAK5QOlzv7snYA\nAAAASUVORK5CYII=\n", + "text/latex": [ + "$$2$$" + ], + "text/plain": [ + "2" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aoc20173(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB0AAAAPBAMAAADqo9msAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWaJMt0i\nu0SCRuA9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAArElEQVQIHWNgAAHmyM4FDOzJXIFANqMyAwO7\nAPMeBqb//ycwMJiEfGZgaGJgmM7APiUHpJYNyL/CwCBvwALiQfhfGBjeCyD4zF+B/ASWjtQFEHme\nnwwM6yfwGvD8g/KB8uuBZjPchvAh6oHs+ANw8+QF3BkY5j+A8O8yMPQbqAPlDSB8oHvCGQIYGLZD\n9DNwCzBrMRxl4NBhYGB1+u7BwDwtZQEDT6QrUDkaAADf+TCFzC1FdQAAAABJRU5ErkJggg==\n", + "text/latex": [ + "$$105$$" + ], + "text/plain": [ + "105" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aoc20173(23000)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEgAAAAPBAMAAAC1npSgAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBU\nq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABGklEQVQoFYWQsUrDUBRAT6I1pVYJ/YLYURcH\nV9HBzS9wtjgJPnRx00XnDLq4tIhTQOjiIojiLghSKEjRxR9QUajU5715hfc2Qzi83HMgjwvQgNNu\n4y5ani8KkuZaGqA00rAEO3ZI1Vo74obab4DSSFNpwVnPEBt45Bm2ApRGov0TlVCTN2UTXlKP0ojs\njCM5vkG7K5HHOKoaifpHc9KwqmClG8CZKyRa5+BV/nYoltlhCGc6GsHkItzqgQm9oIeaeuqi+Bs2\nyqip9EDMNRLN5LodXZhsJAvhzMNg8NWbyol/mB5pdE9iPJyRcYtYLpETvctHlFExHs7I/JMk49hQ\n12ivOH8K4Axc2D67lwuQbEvUtvYjgDMy//f5A0ICeXTNG40LAAAAAElFTkSuQmCC\n", + "text/latex": [ + "$$4572225$$" + ], + "text/plain": [ + "4572225" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aoc20173(23000000000000)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", + "Wall time: 20 µs\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIUAAAAPBAMAAAA43xGxAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACa0lEQVQoFaXTT0gUYRjH8e/+mZ11d50duhWE\nw9JfvAxJQaeGIDw6eCjMg1uUUUQuInpIci6dXaGCCsvOEe2hfwfTJaKIhLYIOrqeQhLUcl3RdHve\neTfo3h6GnefzPj/e99l3ieQ6PDBHhzAODtvy+C6v3TeGgJH3HpqJO5qZ9k9W6B451+Swh9OYGxiv\naKenGpkgXTEewWvO2vQG0ZJmSLmaedioYzxgd5PDHi7CXWIOHVyG8yzCdawiZokjxFzN8MHVzMur\nNokiCUdz2MM9GPDGbdnbFoxxDOb9WJ7WWnJVapqJ/HA1k5datoS5ojnsYdaXjOMixi/45K3DeCWb\nJ7kdK0pRM2ba1Rxm9Llk1kJuFmXdU3+r803AfdnHzZ+SUe5zSP7OPhs9pFKEWUi7IQdcW9pHi+xj\nQ7PqCWRNsm5sVZmUgzC7UIeuQluBeL1vhpZKyBhlyVBc5ShtgblCekezLsrM80bD57CfLreOfZZ9\nSIajMmpEb0tGKo+JZChWmbEppvm2rflvMQebsByw9Hbs1D9nmcLakB7hrypDsSfv0VWsuc61rGZd\nzDjwWDKq4gO+zHRezbR1O1XC2gFhoxBmKE6oUcjCRK3JqghfiNjyM8s+4IVcE5Z9uRdWTW6B2ofw\n3v7+gTvlkGWc0Zp8S+ebrHrULc7YXTIPFu34qrpj7VhFoqW4zKOoGVpczVGZT8maoMvWHPawZ2Tw\nComCMclHv7dKqmLcgif0eFyip6JZrpWrOeJIVua5MYPmsIfZRkMmOnjAw8zJfTBG33lwZu6C/A9z\n8tBsnlivhsyu4f2yOhc0WRflcP/7+QN1gvGXxRfSRAAAAABJRU5ErkJggg==\n", + "text/latex": [ + "$$2690062495969$$" + ], + "text/plain": [ + "2690062495969" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%time aoc20173(23000000000000000000000000) # Fast for large values." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# A Joy Version\n", + "At this point I feel confident that I can implement a concise version of this code in Joy. ;-)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `rank_of`\n", + "\n", + " n rank_of\n", + " ---------------\n", + " k\n", + "\n", + "The translation is straightforward.\n", + "\n", + " int(floor(sqrt(n - 1) / 2 - 0.5) + 1)\n", + "\n", + " rank_of == -- sqrt 2 / 0.5 - floor ++" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "define('rank_of == -- sqrt 2 / 0.5 - floor ++')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `offset_of`\n", + "\n", + " n k offset_of\n", + " -------------------\n", + " i\n", + "\n", + " (n - 2 + 4 * k * (k - 1)) % (2 * k)\n", + "\n", + "A little tricky...\n", + "\n", + " n k dup 2 *\n", + " n k k 2 *\n", + " n k k*2 [Q] dip %\n", + " n k Q k*2 %\n", + "\n", + " n k dup --\n", + " n k k --\n", + " n k k-1 4 * * 2 + -\n", + " n k*k-1*4 2 + -\n", + " n k*k-1*4+2 -\n", + " n-k*k-1*4+2\n", + "\n", + " n-k*k-1*4+2 k*2 %\n", + " n-k*k-1*4+2%k*2\n", + "\n", + "Ergo:\n", + "\n", + " offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `row_value`\n", + "\n", + " k i row_value\n", + " -------------------\n", + " n\n", + "\n", + " abs(i - (k - 1)) + k\n", + "\n", + " k i over -- - abs +\n", + " k i k -- - abs +\n", + " k i k-1 - abs +\n", + " k i-k-1 abs +\n", + " k |i-k-1| +\n", + " k+|i-k-1|" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "define('row_value == over -- - abs +')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `aoc2017.3`\n", + "\n", + " n aoc2017.3\n", + " -----------------\n", + " m\n", + "\n", + " n dup rank_of\n", + " n k [offset_of] dupdip\n", + " n k offset_of k\n", + " i k swap row_value\n", + " k i row_value\n", + " m" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value')" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('23 aoc2017.3')" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "105\n" + ] + } + ], + "source": [ + "J('23000 aoc2017.3')" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 23000000000000 aoc2017.3\n", + " 23000000000000 . aoc2017.3\n", + " 23000000000000 . dup rank_of [offset_of] dupdip swap row_value\n", + " 23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value\n", + " 23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 2397915 . ++ [offset_of] dupdip swap row_value\n", + " 23000000000000 2397916 . [offset_of] dupdip swap row_value\n", + " 23000000000000 2397916 [offset_of] . dupdip swap row_value\n", + " 23000000000000 2397916 . offset_of 2397916 swap row_value\n", + " 23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n", + " 23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n", + " 23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n", + " 23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n", + "23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value\n", + " 23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value\n", + " 23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value\n", + " 5019438 . 4795832 % 2397916 swap row_value\n", + " 5019438 4795832 . % 2397916 swap row_value\n", + " 223606 . 2397916 swap row_value\n", + " 223606 2397916 . swap row_value\n", + " 2397916 223606 . row_value\n", + " 2397916 223606 . over -- - abs +\n", + " 2397916 223606 2397916 . -- - abs +\n", + " 2397916 223606 2397915 . - abs +\n", + " 2397916 -2174309 . abs +\n", + " 2397916 2174309 . +\n", + " 4572225 . \n" + ] + } + ], + "source": [ + "V('23000000000000 aoc2017.3')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " rank_of == -- sqrt 2 / 0.5 - floor ++\n", + " offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %\n", + " row_value == over -- - abs +\n", + "\n", + " aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Advent_of_Code_2017_December_3rd.md b/docs/Advent_of_Code_2017_December_3rd.md new file mode 100644 index 0000000..d6de378 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_3rd.md @@ -0,0 +1,843 @@ + +# Advent of Code 2017 + +## December 3rd + +You come across an experimental new kind of memory stored on an infinite two-dimensional grid. + +Each square on the grid is allocated in a spiral pattern starting at a location marked 1 and then counting up while spiraling outward. For example, the first few squares are allocated like this: + + 17 16 15 14 13 + 18 5 4 3 12 + 19 6 1 2 11 + 20 7 8 9 10 + 21 22 23---> ... + +While this is very space-efficient (no squares are skipped), requested data must be carried back to square 1 (the location of the only access port for this memory system) by programs that can only move up, down, left, or right. They always take the shortest path: the Manhattan Distance between the location of the data and square 1. + +For example: + +* Data from square 1 is carried 0 steps, since it's at the access port. +* Data from square 12 is carried 3 steps, such as: down, left, left. +* Data from square 23 is carried only 2 steps: up twice. +* Data from square 1024 must be carried 31 steps. + +How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port? + +### Analysis + +I freely admit that I worked out the program I wanted to write using graph paper and some Python doodles. There's no point in trying to write a Joy program until I'm sure I understand the problem well enough. + +The first thing I did was to write a column of numbers from 1 to n (32 as it happens) and next to them the desired output number, to look for patterns directly: + + 1 0 + 2 1 + 3 2 + 4 1 + 5 2 + 6 1 + 7 2 + 8 1 + 9 2 + 10 3 + 11 2 + 12 3 + 13 4 + 14 3 + 15 2 + 16 3 + 17 4 + 18 3 + 19 2 + 20 3 + 21 4 + 22 3 + 23 2 + 24 3 + 25 4 + 26 5 + 27 4 + 28 3 + 29 4 + 30 5 + 31 6 + 32 5 + +There are four groups repeating for a given "rank", then the pattern enlarges and four groups repeat again, etc. + + 1 2 + 3 2 3 4 + 5 4 3 4 5 6 + 7 6 5 4 5 6 7 8 + 9 8 7 6 5 6 7 8 9 10 + +Four of this pyramid interlock to tile the plane extending from the initial "1" square. + + + 2 3 | 4 5 | 6 7 | 8 9 + 10 11 12 13|14 15 16 17|18 19 20 21|22 23 24 25 + +And so on. + +We can figure out the pattern for a row of the pyramid at a given "rank" $k$: + +$2k - 1, 2k - 2, ..., k, k + 1, k + 2, ..., 2k$ + +or + +$k + (k - 1), k + (k - 2), ..., k, k + 1, k + 2, ..., k + k$ + +This shows that the series consists at each place of $k$ plus some number that begins at $k - 1$, decreases to zero, then increases to $k$. Each row has $2k$ members. + +Let's figure out how, given an index into a row, we can calculate the value there. The index will be from 0 to $k - 1$. + + Let's look at an example, with $k = 4$: + + 0 1 2 3 4 5 6 7 + 7 6 5 4 5 6 7 8 + + +```python +k = 4 +``` + +Subtract $k$ from the index and take the absolute value: + + +```python +for n in range(2 * k): + print abs(n - k), +``` + + 4 3 2 1 0 1 2 3 + + +Not quite. Subtract $k - 1$ from the index and take the absolute value: + + +```python +for n in range(2 * k): + print abs(n - (k - 1)), +``` + + 3 2 1 0 1 2 3 4 + + +Great, now add $k$... + + +```python +for n in range(2 * k): + print abs(n - (k - 1)) + k, +``` + + 7 6 5 4 5 6 7 8 + + +So to write a function that can give us the value of a row at a given index: + + +```python +def row_value(k, i): + i %= (2 * k) # wrap the index at the row boundary. + return abs(i - (k - 1)) + k +``` + + +```python +k = 5 +for i in range(2 * k): + print row_value(k, i), +``` + + 9 8 7 6 5 6 7 8 9 10 + + +(I'm leaving out details of how I figured this all out and just giving the relevent bits. It took a little while to zero in of the aspects of the pattern that were important for the task.) + +### Finding the rank and offset of a number. +Now that we can compute the desired output value for a given rank and the offset (index) into that rank, we need to determine how to find the rank and offset of a number. + +The rank is easy to find by iteratively stripping off the amount already covered by previous ranks until you find the one that brackets the target number. Because each row is $2k$ places and there are $4$ per rank each rank contains $8k$ places. Counting the initial square we have: + +$corner_k = 1 + \sum_{n=1}^k 8n$ + +I'm not mathematically sophisticated enough to turn this directly into a formula (but Sympy is, see below.) I'm going to write a simple Python function to iterate and search: + + +```python +def rank_and_offset(n): + assert n >= 2 # Guard the domain. + n -= 2 # Subtract two, + # one for the initial square, + # and one because we are counting from 1 instead of 0. + k = 1 + while True: + m = 8 * k # The number of places total in this rank, 4(2k). + if n < m: + return k, n % (2 * k) + n -= m # Remove this rank's worth. + k += 1 +``` + + +```python +for n in range(2, 51): + print n, rank_and_offset(n) +``` + + 2 (1, 0) + 3 (1, 1) + 4 (1, 0) + 5 (1, 1) + 6 (1, 0) + 7 (1, 1) + 8 (1, 0) + 9 (1, 1) + 10 (2, 0) + 11 (2, 1) + 12 (2, 2) + 13 (2, 3) + 14 (2, 0) + 15 (2, 1) + 16 (2, 2) + 17 (2, 3) + 18 (2, 0) + 19 (2, 1) + 20 (2, 2) + 21 (2, 3) + 22 (2, 0) + 23 (2, 1) + 24 (2, 2) + 25 (2, 3) + 26 (3, 0) + 27 (3, 1) + 28 (3, 2) + 29 (3, 3) + 30 (3, 4) + 31 (3, 5) + 32 (3, 0) + 33 (3, 1) + 34 (3, 2) + 35 (3, 3) + 36 (3, 4) + 37 (3, 5) + 38 (3, 0) + 39 (3, 1) + 40 (3, 2) + 41 (3, 3) + 42 (3, 4) + 43 (3, 5) + 44 (3, 0) + 45 (3, 1) + 46 (3, 2) + 47 (3, 3) + 48 (3, 4) + 49 (3, 5) + 50 (4, 0) + + + +```python +for n in range(2, 51): + k, i = rank_and_offset(n) + print n, row_value(k, i) +``` + + 2 1 + 3 2 + 4 1 + 5 2 + 6 1 + 7 2 + 8 1 + 9 2 + 10 3 + 11 2 + 12 3 + 13 4 + 14 3 + 15 2 + 16 3 + 17 4 + 18 3 + 19 2 + 20 3 + 21 4 + 22 3 + 23 2 + 24 3 + 25 4 + 26 5 + 27 4 + 28 3 + 29 4 + 30 5 + 31 6 + 32 5 + 33 4 + 34 3 + 35 4 + 36 5 + 37 6 + 38 5 + 39 4 + 40 3 + 41 4 + 42 5 + 43 6 + 44 5 + 45 4 + 46 3 + 47 4 + 48 5 + 49 6 + 50 7 + + +### Putting it all together + + +```python +def row_value(k, i): + return abs(i - (k - 1)) + k + + +def rank_and_offset(n): + n -= 2 # Subtract two, + # one for the initial square, + # and one because we are counting from 1 instead of 0. + k = 1 + while True: + m = 8 * k # The number of places total in this rank, 4(2k). + if n < m: + return k, n % (2 * k) + n -= m # Remove this rank's worth. + k += 1 + + +def aoc20173(n): + if n <= 1: + return 0 + k, i = rank_and_offset(n) + return row_value(k, i) +``` + + +```python +aoc20173(23) +``` + + + + + 2 + + + + +```python +aoc20173(23000) +``` + + + + + 105 + + + + +```python +aoc20173(23000000000000) +``` + + + + + 4572225 + + + +# Sympy to the Rescue +### Find the rank for large numbers +Using e.g. Sympy we can find the rank directly by solving for the roots of an equation. For large numbers this will (eventually) be faster than iterating as `rank_and_offset()` does. + + +```python +from sympy import floor, lambdify, solve, symbols +from sympy import init_printing +init_printing() +``` + + +```python +k = symbols('k') +``` + +Since + +$1 + 2 + 3 + ... + N = \frac{N(N + 1)}{2}$ + +and + +$\sum_{n=1}^k 8n = 8(\sum_{n=1}^k n) = 8\frac{k(k + 1)}{2}$ + +We want: + + +```python +E = 2 + 8 * k * (k + 1) / 2 # For the reason for adding 2 see above. + +E +``` + + + + +$$4 k \left(k + 1\right) + 2$$ + + + +We can write a function to solve for $k$ given some $n$... + + +```python +def rank_of(n): + return floor(max(solve(E - n, k))) + 1 +``` + +First `solve()` for $E - n = 0$ which has two solutions (because the equation is quadratic so it has two roots) and since we only care about the larger one we use `max()` to select it. It will generally not be a nice integer (unless $n$ is the number of an end-corner of a rank) so we take the `floor()` and add 1 to get the integer rank of $n$. (Taking the `ceiling()` gives off-by-one errors on the rank boundaries. I don't know why. I'm basically like a monkey doing math here.) =-D + +It gives correct answers: + + +```python +for n in (9, 10, 25, 26, 49, 50): + print n, rank_of(n) +``` + + 9 1 + 10 2 + 25 2 + 26 3 + 49 3 + 50 4 + + +And it runs much faster (at least for large numbers): + + +```python +%time rank_of(23000000000000) # Compare runtime with rank_and_offset()! +``` + + CPU times: user 68 ms, sys: 8 ms, total: 76 ms + Wall time: 73.8 ms + + + + + +$$2397916$$ + + + + +```python +%time rank_and_offset(23000000000000) +``` + + CPU times: user 308 ms, sys: 0 ns, total: 308 ms + Wall time: 306 ms + + + + + +$$\left ( 2397916, \quad 223606\right )$$ + + + +After finding the rank you would still have to find the actual value of the rank's first corner and subtract it (plus 2) from the number and compute the offset as above and then the final output, but this overhead is partially shared by the other method, and overshadowed by the time it (the other iterative method) would take for really big inputs. + +The fun thing to do here would be to graph the actual runtime of both methods against each other to find the trade-off point. + +### It took me a second to realize I could do this... +Sympy is a *symbolic* math library, and it supports symbolic manipulation of equations. I can put in $y$ (instead of a value) and ask it to solve for $k$. + + +```python +y = symbols('y') +``` + + +```python +g, f = solve(E - y, k) +``` + +The equation is quadratic so there are two roots, we are interested in the greater one... + + +```python +g +``` + + + + +$$- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$ + + + + +```python +f +``` + + + + +$$\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$ + + + +Now we can take the `floor()`, add 1, and `lambdify()` the equation to get a Python function that calculates the rank directly. + + +```python +floor(f) + 1 +``` + + + + +$$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$ + + + + +```python +F = lambdify(y, floor(f) + 1) +``` + + +```python +for n in (9, 10, 25, 26, 49, 50): + print n, int(F(n)) +``` + + 9 1 + 10 2 + 25 2 + 26 3 + 49 3 + 50 4 + + +It's pretty fast. + + +```python +%time int(F(23000000000000)) # The clear winner. +``` + + CPU times: user 0 ns, sys: 0 ns, total: 0 ns + Wall time: 11.9 µs + + + + + +$$2397916$$ + + + +Knowing the equation we could write our own function manually, but the speed is no better. + + +```python +from math import floor as mfloor, sqrt + +def mrank_of(n): + return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1) +``` + + +```python +%time mrank_of(23000000000000) +``` + + CPU times: user 0 ns, sys: 0 ns, total: 0 ns + Wall time: 12.9 µs + + + + + +$$2397916$$ + + + +### Given $n$ and a rank, compute the offset. + +Now that we have a fast way to get the rank, we still need to use it to compute the offset into a pyramid row. + + +```python +def offset_of(n, k): + return (n - 2 + 4 * k * (k - 1)) % (2 * k) +``` + +(Note the sneaky way the sign changes from $k(k + 1)$ to $k(k - 1)$. This is because we want to subract the $(k - 1)$th rank's total places (its own and those of lesser rank) from our $n$ of rank $k$. Substituting $k - 1$ for $k$ in $k(k + 1)$ gives $(k - 1)(k - 1 + 1)$, which of course simplifies to $k(k - 1)$.) + + +```python +offset_of(23000000000000, 2397916) +``` + + + + +$$223606$$ + + + +So, we can compute the rank, then the offset, then the row value. + + +```python +def rank_of(n): + return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1) + + +def offset_of(n, k): + return (n - 2 + 4 * k * (k - 1)) % (2 * k) + + +def row_value(k, i): + return abs(i - (k - 1)) + k + + +def aoc20173(n): + k = rank_of(n) + i = offset_of(n, k) + return row_value(k, i) +``` + + +```python +aoc20173(23) +``` + + + + +$$2$$ + + + + +```python +aoc20173(23000) +``` + + + + +$$105$$ + + + + +```python +aoc20173(23000000000000) +``` + + + + +$$4572225$$ + + + + +```python +%time aoc20173(23000000000000000000000000) # Fast for large values. +``` + + CPU times: user 0 ns, sys: 0 ns, total: 0 ns + Wall time: 20 µs + + + + + +$$2690062495969$$ + + + +# A Joy Version +At this point I feel confident that I can implement a concise version of this code in Joy. ;-) + + +```python +from notebook_preamble import J, V, define +``` + +### `rank_of` + + n rank_of + --------------- + k + +The translation is straightforward. + + int(floor(sqrt(n - 1) / 2 - 0.5) + 1) + + rank_of == -- sqrt 2 / 0.5 - floor ++ + + +```python +define('rank_of == -- sqrt 2 / 0.5 - floor ++') +``` + +### `offset_of` + + n k offset_of + ------------------- + i + + (n - 2 + 4 * k * (k - 1)) % (2 * k) + +A little tricky... + + n k dup 2 * + n k k 2 * + n k k*2 [Q] dip % + n k Q k*2 % + + n k dup -- + n k k -- + n k k-1 4 * * 2 + - + n k*k-1*4 2 + - + n k*k-1*4+2 - + n-k*k-1*4+2 + + n-k*k-1*4+2 k*2 % + n-k*k-1*4+2%k*2 + +Ergo: + + offset_of == dup 2 * [dup -- 4 * * 2 + -] dip % + + +```python +define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %') +``` + +### `row_value` + + k i row_value + ------------------- + n + + abs(i - (k - 1)) + k + + k i over -- - abs + + k i k -- - abs + + k i k-1 - abs + + k i-k-1 abs + + k |i-k-1| + + k+|i-k-1| + + +```python +define('row_value == over -- - abs +') +``` + +### `aoc2017.3` + + n aoc2017.3 + ----------------- + m + + n dup rank_of + n k [offset_of] dupdip + n k offset_of k + i k swap row_value + k i row_value + m + + +```python +define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value') +``` + + +```python +J('23 aoc2017.3') +``` + + 2 + + + +```python +J('23000 aoc2017.3') +``` + + 105 + + + +```python +V('23000000000000 aoc2017.3') +``` + + . 23000000000000 aoc2017.3 + 23000000000000 . aoc2017.3 + 23000000000000 . dup rank_of [offset_of] dupdip swap row_value + 23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value + 23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915 . ++ [offset_of] dupdip swap row_value + 23000000000000 2397916 . [offset_of] dupdip swap row_value + 23000000000000 2397916 [offset_of] . dupdip swap row_value + 23000000000000 2397916 . offset_of 2397916 swap row_value + 23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value + 23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value + 23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value + 23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value + 5019438 . 4795832 % 2397916 swap row_value + 5019438 4795832 . % 2397916 swap row_value + 223606 . 2397916 swap row_value + 223606 2397916 . swap row_value + 2397916 223606 . row_value + 2397916 223606 . over -- - abs + + 2397916 223606 2397916 . -- - abs + + 2397916 223606 2397915 . - abs + + 2397916 -2174309 . abs + + 2397916 2174309 . + + 4572225 . + + + rank_of == -- sqrt 2 / 0.5 - floor ++ + offset_of == dup 2 * [dup -- 4 * * 2 + -] dip % + row_value == over -- - abs + + + aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value diff --git a/docs/Advent_of_Code_2017_December_3rd.rst b/docs/Advent_of_Code_2017_December_3rd.rst new file mode 100644 index 0000000..2283c7f --- /dev/null +++ b/docs/Advent_of_Code_2017_December_3rd.rst @@ -0,0 +1,973 @@ + +Advent of Code 2017 +=================== + +December 3rd +------------ + +You come across an experimental new kind of memory stored on an infinite +two-dimensional grid. + +Each square on the grid is allocated in a spiral pattern starting at a +location marked 1 and then counting up while spiraling outward. For +example, the first few squares are allocated like this: + +:: + + 17 16 15 14 13 + 18 5 4 3 12 + 19 6 1 2 11 + 20 7 8 9 10 + 21 22 23---> ... + +While this is very space-efficient (no squares are skipped), requested +data must be carried back to square 1 (the location of the only access +port for this memory system) by programs that can only move up, down, +left, or right. They always take the shortest path: the Manhattan +Distance between the location of the data and square 1. + +For example: + +- Data from square 1 is carried 0 steps, since it's at the access port. +- Data from square 12 is carried 3 steps, such as: down, left, left. +- Data from square 23 is carried only 2 steps: up twice. +- Data from square 1024 must be carried 31 steps. + +How many steps are required to carry the data from the square identified +in your puzzle input all the way to the access port? + +Analysis +~~~~~~~~ + +I freely admit that I worked out the program I wanted to write using +graph paper and some Python doodles. There's no point in trying to write +a Joy program until I'm sure I understand the problem well enough. + +The first thing I did was to write a column of numbers from 1 to n (32 +as it happens) and next to them the desired output number, to look for +patterns directly: + +:: + + 1 0 + 2 1 + 3 2 + 4 1 + 5 2 + 6 1 + 7 2 + 8 1 + 9 2 + 10 3 + 11 2 + 12 3 + 13 4 + 14 3 + 15 2 + 16 3 + 17 4 + 18 3 + 19 2 + 20 3 + 21 4 + 22 3 + 23 2 + 24 3 + 25 4 + 26 5 + 27 4 + 28 3 + 29 4 + 30 5 + 31 6 + 32 5 + +There are four groups repeating for a given "rank", then the pattern +enlarges and four groups repeat again, etc. + +:: + + 1 2 + 3 2 3 4 + 5 4 3 4 5 6 + 7 6 5 4 5 6 7 8 + 9 8 7 6 5 6 7 8 9 10 + +Four of this pyramid interlock to tile the plane extending from the +initial "1" square. + +:: + + 2 3 | 4 5 | 6 7 | 8 9 + 10 11 12 13|14 15 16 17|18 19 20 21|22 23 24 25 + +And so on. + +We can figure out the pattern for a row of the pyramid at a given "rank" +:math:`k`: + +:math:`2k - 1, 2k - 2, ..., k, k + 1, k + 2, ..., 2k` + +or + +:math:`k + (k - 1), k + (k - 2), ..., k, k + 1, k + 2, ..., k + k` + +This shows that the series consists at each place of :math:`k` plus some +number that begins at :math:`k - 1`, decreases to zero, then increases +to :math:`k`. Each row has :math:`2k` members. + +Let's figure out how, given an index into a row, we can calculate the +value there. The index will be from 0 to :math:`k - 1`. + +Let's look at an example, with :math:`k = 4`: + +:: + + 0 1 2 3 4 5 6 7 + 7 6 5 4 5 6 7 8 + +.. code:: ipython2 + + k = 4 + +Subtract :math:`k` from the index and take the absolute value: + +.. code:: ipython2 + + for n in range(2 * k): + print abs(n - k), + + +.. parsed-literal:: + + 4 3 2 1 0 1 2 3 + + +Not quite. Subtract :math:`k - 1` from the index and take the absolute +value: + +.. code:: ipython2 + + for n in range(2 * k): + print abs(n - (k - 1)), + + +.. parsed-literal:: + + 3 2 1 0 1 2 3 4 + + +Great, now add :math:`k`... + +.. code:: ipython2 + + for n in range(2 * k): + print abs(n - (k - 1)) + k, + + +.. parsed-literal:: + + 7 6 5 4 5 6 7 8 + + +So to write a function that can give us the value of a row at a given +index: + +.. code:: ipython2 + + def row_value(k, i): + i %= (2 * k) # wrap the index at the row boundary. + return abs(i - (k - 1)) + k + +.. code:: ipython2 + + k = 5 + for i in range(2 * k): + print row_value(k, i), + + +.. parsed-literal:: + + 9 8 7 6 5 6 7 8 9 10 + + +(I'm leaving out details of how I figured this all out and just giving +the relevent bits. It took a little while to zero in of the aspects of +the pattern that were important for the task.) + +Finding the rank and offset of a number. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Now that we can compute the desired output value for a given rank and +the offset (index) into that rank, we need to determine how to find the +rank and offset of a number. + +The rank is easy to find by iteratively stripping off the amount already +covered by previous ranks until you find the one that brackets the +target number. Because each row is :math:`2k` places and there are +:math:`4` per rank each rank contains :math:`8k` places. Counting the +initial square we have: + +:math:`corner_k = 1 + \sum_{n=1}^k 8n` + +I'm not mathematically sophisticated enough to turn this directly into a +formula (but Sympy is, see below.) I'm going to write a simple Python +function to iterate and search: + +.. code:: ipython2 + + def rank_and_offset(n): + assert n >= 2 # Guard the domain. + n -= 2 # Subtract two, + # one for the initial square, + # and one because we are counting from 1 instead of 0. + k = 1 + while True: + m = 8 * k # The number of places total in this rank, 4(2k). + if n < m: + return k, n % (2 * k) + n -= m # Remove this rank's worth. + k += 1 + +.. code:: ipython2 + + for n in range(2, 51): + print n, rank_and_offset(n) + + +.. parsed-literal:: + + 2 (1, 0) + 3 (1, 1) + 4 (1, 0) + 5 (1, 1) + 6 (1, 0) + 7 (1, 1) + 8 (1, 0) + 9 (1, 1) + 10 (2, 0) + 11 (2, 1) + 12 (2, 2) + 13 (2, 3) + 14 (2, 0) + 15 (2, 1) + 16 (2, 2) + 17 (2, 3) + 18 (2, 0) + 19 (2, 1) + 20 (2, 2) + 21 (2, 3) + 22 (2, 0) + 23 (2, 1) + 24 (2, 2) + 25 (2, 3) + 26 (3, 0) + 27 (3, 1) + 28 (3, 2) + 29 (3, 3) + 30 (3, 4) + 31 (3, 5) + 32 (3, 0) + 33 (3, 1) + 34 (3, 2) + 35 (3, 3) + 36 (3, 4) + 37 (3, 5) + 38 (3, 0) + 39 (3, 1) + 40 (3, 2) + 41 (3, 3) + 42 (3, 4) + 43 (3, 5) + 44 (3, 0) + 45 (3, 1) + 46 (3, 2) + 47 (3, 3) + 48 (3, 4) + 49 (3, 5) + 50 (4, 0) + + +.. code:: ipython2 + + for n in range(2, 51): + k, i = rank_and_offset(n) + print n, row_value(k, i) + + +.. parsed-literal:: + + 2 1 + 3 2 + 4 1 + 5 2 + 6 1 + 7 2 + 8 1 + 9 2 + 10 3 + 11 2 + 12 3 + 13 4 + 14 3 + 15 2 + 16 3 + 17 4 + 18 3 + 19 2 + 20 3 + 21 4 + 22 3 + 23 2 + 24 3 + 25 4 + 26 5 + 27 4 + 28 3 + 29 4 + 30 5 + 31 6 + 32 5 + 33 4 + 34 3 + 35 4 + 36 5 + 37 6 + 38 5 + 39 4 + 40 3 + 41 4 + 42 5 + 43 6 + 44 5 + 45 4 + 46 3 + 47 4 + 48 5 + 49 6 + 50 7 + + +Putting it all together +~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + def row_value(k, i): + return abs(i - (k - 1)) + k + + + def rank_and_offset(n): + n -= 2 # Subtract two, + # one for the initial square, + # and one because we are counting from 1 instead of 0. + k = 1 + while True: + m = 8 * k # The number of places total in this rank, 4(2k). + if n < m: + return k, n % (2 * k) + n -= m # Remove this rank's worth. + k += 1 + + + def aoc20173(n): + if n <= 1: + return 0 + k, i = rank_and_offset(n) + return row_value(k, i) + +.. code:: ipython2 + + aoc20173(23) + + + + +.. parsed-literal:: + + 2 + + + +.. code:: ipython2 + + aoc20173(23000) + + + + +.. parsed-literal:: + + 105 + + + +.. code:: ipython2 + + aoc20173(23000000000000) + + + + +.. parsed-literal:: + + 4572225 + + + +Sympy to the Rescue +=================== + +Find the rank for large numbers +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Using e.g. Sympy we can find the rank directly by solving for the roots +of an equation. For large numbers this will (eventually) be faster than +iterating as ``rank_and_offset()`` does. + +.. code:: ipython2 + + from sympy import floor, lambdify, solve, symbols + from sympy import init_printing + init_printing() + +.. code:: ipython2 + + k = symbols('k') + +Since + +:math:`1 + 2 + 3 + ... + N = \frac{N(N + 1)}{2}` + +and + +:math:`\sum_{n=1}^k 8n = 8(\sum_{n=1}^k n) = 8\frac{k(k + 1)}{2}` + +We want: + +.. code:: ipython2 + + E = 2 + 8 * k * (k + 1) / 2 # For the reason for adding 2 see above. + + E + + + + +.. math:: + + 4 k \left(k + 1\right) + 2 + + + +We can write a function to solve for :math:`k` given some :math:`n`... + +.. code:: ipython2 + + def rank_of(n): + return floor(max(solve(E - n, k))) + 1 + +First ``solve()`` for :math:`E - n = 0` which has two solutions (because +the equation is quadratic so it has two roots) and since we only care +about the larger one we use ``max()`` to select it. It will generally +not be a nice integer (unless :math:`n` is the number of an end-corner +of a rank) so we take the ``floor()`` and add 1 to get the integer rank +of :math:`n`. (Taking the ``ceiling()`` gives off-by-one errors on the +rank boundaries. I don't know why. I'm basically like a monkey doing +math here.) =-D + +It gives correct answers: + +.. code:: ipython2 + + for n in (9, 10, 25, 26, 49, 50): + print n, rank_of(n) + + +.. parsed-literal:: + + 9 1 + 10 2 + 25 2 + 26 3 + 49 3 + 50 4 + + +And it runs much faster (at least for large numbers): + +.. code:: ipython2 + + %time rank_of(23000000000000) # Compare runtime with rank_and_offset()! + + +.. parsed-literal:: + + CPU times: user 68 ms, sys: 8 ms, total: 76 ms + Wall time: 73.8 ms + + + + +.. math:: + + 2397916 + + + +.. code:: ipython2 + + %time rank_and_offset(23000000000000) + + +.. parsed-literal:: + + CPU times: user 308 ms, sys: 0 ns, total: 308 ms + Wall time: 306 ms + + + + +.. math:: + + \left ( 2397916, \quad 223606\right ) + + + +After finding the rank you would still have to find the actual value of +the rank's first corner and subtract it (plus 2) from the number and +compute the offset as above and then the final output, but this overhead +is partially shared by the other method, and overshadowed by the time it +(the other iterative method) would take for really big inputs. + +The fun thing to do here would be to graph the actual runtime of both +methods against each other to find the trade-off point. + +It took me a second to realize I could do this... +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sympy is a *symbolic* math library, and it supports symbolic +manipulation of equations. I can put in :math:`y` (instead of a value) +and ask it to solve for :math:`k`. + +.. code:: ipython2 + + y = symbols('y') + +.. code:: ipython2 + + g, f = solve(E - y, k) + +The equation is quadratic so there are two roots, we are interested in +the greater one... + +.. code:: ipython2 + + g + + + + +.. math:: + + - \frac{1}{2} \sqrt{y - 1} - \frac{1}{2} + + + +.. code:: ipython2 + + f + + + + +.. math:: + + \frac{1}{2} \sqrt{y - 1} - \frac{1}{2} + + + +Now we can take the ``floor()``, add 1, and ``lambdify()`` the equation +to get a Python function that calculates the rank directly. + +.. code:: ipython2 + + floor(f) + 1 + + + + +.. math:: + + \lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1 + + + +.. code:: ipython2 + + F = lambdify(y, floor(f) + 1) + +.. code:: ipython2 + + for n in (9, 10, 25, 26, 49, 50): + print n, int(F(n)) + + +.. parsed-literal:: + + 9 1 + 10 2 + 25 2 + 26 3 + 49 3 + 50 4 + + +It's pretty fast. + +.. code:: ipython2 + + %time int(F(23000000000000)) # The clear winner. + + +.. parsed-literal:: + + CPU times: user 0 ns, sys: 0 ns, total: 0 ns + Wall time: 11.9 µs + + + + +.. math:: + + 2397916 + + + +Knowing the equation we could write our own function manually, but the +speed is no better. + +.. code:: ipython2 + + from math import floor as mfloor, sqrt + + def mrank_of(n): + return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1) + +.. code:: ipython2 + + %time mrank_of(23000000000000) + + +.. parsed-literal:: + + CPU times: user 0 ns, sys: 0 ns, total: 0 ns + Wall time: 12.9 µs + + + + +.. math:: + + 2397916 + + + +Given :math:`n` and a rank, compute the offset. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Now that we have a fast way to get the rank, we still need to use it to +compute the offset into a pyramid row. + +.. code:: ipython2 + + def offset_of(n, k): + return (n - 2 + 4 * k * (k - 1)) % (2 * k) + +(Note the sneaky way the sign changes from :math:`k(k + 1)` to +:math:`k(k - 1)`. This is because we want to subract the +:math:`(k - 1)`\ th rank's total places (its own and those of lesser +rank) from our :math:`n` of rank :math:`k`. Substituting :math:`k - 1` +for :math:`k` in :math:`k(k + 1)` gives :math:`(k - 1)(k - 1 + 1)`, +which of course simplifies to :math:`k(k - 1)`.) + +.. code:: ipython2 + + offset_of(23000000000000, 2397916) + + + + +.. math:: + + 223606 + + + +So, we can compute the rank, then the offset, then the row value. + +.. code:: ipython2 + + def rank_of(n): + return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1) + + + def offset_of(n, k): + return (n - 2 + 4 * k * (k - 1)) % (2 * k) + + + def row_value(k, i): + return abs(i - (k - 1)) + k + + + def aoc20173(n): + k = rank_of(n) + i = offset_of(n, k) + return row_value(k, i) + +.. code:: ipython2 + + aoc20173(23) + + + + +.. math:: + + 2 + + + +.. code:: ipython2 + + aoc20173(23000) + + + + +.. math:: + + 105 + + + +.. code:: ipython2 + + aoc20173(23000000000000) + + + + +.. math:: + + 4572225 + + + +.. code:: ipython2 + + %time aoc20173(23000000000000000000000000) # Fast for large values. + + +.. parsed-literal:: + + CPU times: user 0 ns, sys: 0 ns, total: 0 ns + Wall time: 20 µs + + + + +.. math:: + + 2690062495969 + + + +A Joy Version +============= + +At this point I feel confident that I can implement a concise version of +this code in Joy. ;-) + +.. code:: ipython2 + + from notebook_preamble import J, V, define + +``rank_of`` +~~~~~~~~~~~ + +:: + + n rank_of + --------------- + k + +The translation is straightforward. + +:: + + int(floor(sqrt(n - 1) / 2 - 0.5) + 1) + + rank_of == -- sqrt 2 / 0.5 - floor ++ + +.. code:: ipython2 + + define('rank_of == -- sqrt 2 / 0.5 - floor ++') + +``offset_of`` +~~~~~~~~~~~~~ + +:: + + n k offset_of + ------------------- + i + + (n - 2 + 4 * k * (k - 1)) % (2 * k) + +A little tricky... + +:: + + n k dup 2 * + n k k 2 * + n k k*2 [Q] dip % + n k Q k*2 % + + n k dup -- + n k k -- + n k k-1 4 * * 2 + - + n k*k-1*4 2 + - + n k*k-1*4+2 - + n-k*k-1*4+2 + + n-k*k-1*4+2 k*2 % + n-k*k-1*4+2%k*2 + +Ergo: + +:: + + offset_of == dup 2 * [dup -- 4 * * 2 + -] dip % + +.. code:: ipython2 + + define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %') + +``row_value`` +~~~~~~~~~~~~~ + +:: + + k i row_value + ------------------- + n + + abs(i - (k - 1)) + k + + k i over -- - abs + + k i k -- - abs + + k i k-1 - abs + + k i-k-1 abs + + k |i-k-1| + + k+|i-k-1| + +.. code:: ipython2 + + define('row_value == over -- - abs +') + +``aoc2017.3`` +~~~~~~~~~~~~~ + +:: + + n aoc2017.3 + ----------------- + m + + n dup rank_of + n k [offset_of] dupdip + n k offset_of k + i k swap row_value + k i row_value + m + +.. code:: ipython2 + + define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value') + +.. code:: ipython2 + + J('23 aoc2017.3') + + +.. parsed-literal:: + + 2 + + +.. code:: ipython2 + + J('23000 aoc2017.3') + + +.. parsed-literal:: + + 105 + + +.. code:: ipython2 + + V('23000000000000 aoc2017.3') + + +.. parsed-literal:: + + . 23000000000000 aoc2017.3 + 23000000000000 . aoc2017.3 + 23000000000000 . dup rank_of [offset_of] dupdip swap row_value + 23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value + 23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value + 23000000000000 2397915 . ++ [offset_of] dupdip swap row_value + 23000000000000 2397916 . [offset_of] dupdip swap row_value + 23000000000000 2397916 [offset_of] . dupdip swap row_value + 23000000000000 2397916 . offset_of 2397916 swap row_value + 23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value + 23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value + 23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value + 23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value + 23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value + 23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value + 5019438 . 4795832 % 2397916 swap row_value + 5019438 4795832 . % 2397916 swap row_value + 223606 . 2397916 swap row_value + 223606 2397916 . swap row_value + 2397916 223606 . row_value + 2397916 223606 . over -- - abs + + 2397916 223606 2397916 . -- - abs + + 2397916 223606 2397915 . - abs + + 2397916 -2174309 . abs + + 2397916 2174309 . + + 4572225 . + + +:: + + rank_of == -- sqrt 2 / 0.5 - floor ++ + offset_of == dup 2 * [dup -- 4 * * 2 + -] dip % + row_value == over -- - abs + + + aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_29_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_29_0.png new file mode 100644 index 0000000..4e6c0d8 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_29_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_35_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_35_1.png new file mode 100644 index 0000000..d4b2fda Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_35_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_36_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_36_1.png new file mode 100644 index 0000000..e24b781 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_36_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_42_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_42_0.png new file mode 100644 index 0000000..02b91a1 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_42_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_43_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_43_0.png new file mode 100644 index 0000000..2161606 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_43_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_45_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_45_0.png new file mode 100644 index 0000000..2e44b89 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_45_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_49_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_49_1.png new file mode 100644 index 0000000..d4b2fda Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_49_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_52_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_52_1.png new file mode 100644 index 0000000..d4b2fda Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_52_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_56_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_56_0.png new file mode 100644 index 0000000..8af9c3c Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_56_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_59_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_59_0.png new file mode 100644 index 0000000..ed4ba2d Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_59_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_60_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_60_0.png new file mode 100644 index 0000000..9034b06 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_60_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_61_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_61_0.png new file mode 100644 index 0000000..5714d79 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_61_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_62_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_62_1.png new file mode 100644 index 0000000..1a257f5 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent of Code 2017 December 3rd_62_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_29_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_29_0.png new file mode 100644 index 0000000..4e6c0d8 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_29_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_35_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_35_1.png new file mode 100644 index 0000000..d4b2fda Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_35_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_36_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_36_1.png new file mode 100644 index 0000000..e24b781 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_36_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_42_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_42_0.png new file mode 100644 index 0000000..02b91a1 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_42_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_43_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_43_0.png new file mode 100644 index 0000000..2161606 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_43_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_45_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_45_0.png new file mode 100644 index 0000000..2e44b89 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_45_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_49_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_49_1.png new file mode 100644 index 0000000..d4b2fda Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_49_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_52_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_52_1.png new file mode 100644 index 0000000..d4b2fda Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_52_1.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_56_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_56_0.png new file mode 100644 index 0000000..8af9c3c Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_56_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_59_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_59_0.png new file mode 100644 index 0000000..ed4ba2d Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_59_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_60_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_60_0.png new file mode 100644 index 0000000..9034b06 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_60_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_61_0.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_61_0.png new file mode 100644 index 0000000..5714d79 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_61_0.png differ diff --git a/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_62_1.png b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_62_1.png new file mode 100644 index 0000000..1a257f5 Binary files /dev/null and b/docs/Advent_of_Code_2017_December_3rd_files/Advent_of_Code_2017_December_3rd_62_1.png differ diff --git a/docs/Advent_of_Code_2017_December_4th.html b/docs/Advent_of_Code_2017_December_4th.html new file mode 100644 index 0000000..df6e062 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_4th.html @@ -0,0 +1,11939 @@ + + + +Advent_of_Code_2017_December_4th + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Advent of Code 2017

December 4th

To ensure security, a valid passphrase must contain no duplicate words.

+

For example:

+
    +
  • aa bb cc dd ee is valid.
  • +
  • aa bb cc dd aa is not valid - the word aa appears more than once.
  • +
  • aa bb cc dd aaa is valid - aa and aaa count as different words.
  • +
+

The system's full passphrase list is available as your puzzle input. How many passphrases are valid?

+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

I'll assume the input is a Joy sequence of sequences of integers.

+ +
[[5 1 9 5]
+ [7 5 4 3]
+ [2 4 6 8]]
+
+
+

So, obviously, the initial form will be a step function:

+ +
AoC2017.4 == 0 swap [F +] step
+ +
+
+
+
+
+
+
+ +
F == [size] [unique size] cleave =
+ +
+
+
+
+
+
+
+

The step_zero combinator includes the 0 swap that would normally open one of these definitions:

+ +
+
+
+
+
+
In [2]:
+
+
+
J('[step_zero] help')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0 roll> step
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
AoC2017.4 == [F +] step_zero
+ +
+
+
+
+
+
In [3]:
+
+
+
define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero')
+
+ +
+
+
+ +
+
+
+
In [4]:
+
+
+
J('''
+
+[[5 1 9 5]
+ [7 5 4 3]
+ [2 4 6 8]] AoC2017.4
+
+''')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/Advent_of_Code_2017_December_4th.ipynb b/docs/Advent_of_Code_2017_December_4th.ipynb new file mode 100644 index 0000000..cd1bd55 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_4th.ipynb @@ -0,0 +1,139 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advent of Code 2017\n", + "\n", + "## December 4th\n", + "To ensure security, a valid passphrase must contain no duplicate words.\n", + "\n", + "For example:\n", + "\n", + "* aa bb cc dd ee is valid.\n", + "* aa bb cc dd aa is not valid - the word aa appears more than once.\n", + "* aa bb cc dd aaa is valid - aa and aaa count as different words.\n", + "\n", + "The system's full passphrase list is available as your puzzle input. How many passphrases are valid?" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I'll assume the input is a Joy sequence of sequences of integers.\n", + "\n", + " [[5 1 9 5]\n", + " [7 5 4 3]\n", + " [2 4 6 8]]\n", + "\n", + "So, obviously, the initial form will be a `step` function:\n", + "\n", + " AoC2017.4 == 0 swap [F +] step" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " F == [size] [unique size] cleave =\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `step_zero` combinator includes the `0 swap` that would normally open one of these definitions:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 roll> step\n", + "\n" + ] + } + ], + "source": [ + "J('[step_zero] help')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " AoC2017.4 == [F +] step_zero" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('''\n", + "\n", + "[[5 1 9 5]\n", + " [7 5 4 3]\n", + " [2 4 6 8]] AoC2017.4\n", + "\n", + "''')" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Advent_of_Code_2017_December_4th.md b/docs/Advent_of_Code_2017_December_4th.md new file mode 100644 index 0000000..96f3c29 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_4th.md @@ -0,0 +1,64 @@ + +# Advent of Code 2017 + +## December 4th +To ensure security, a valid passphrase must contain no duplicate words. + +For example: + +* aa bb cc dd ee is valid. +* aa bb cc dd aa is not valid - the word aa appears more than once. +* aa bb cc dd aaa is valid - aa and aaa count as different words. + +The system's full passphrase list is available as your puzzle input. How many passphrases are valid? + + +```python +from notebook_preamble import J, V, define +``` + +I'll assume the input is a Joy sequence of sequences of integers. + + [[5 1 9 5] + [7 5 4 3] + [2 4 6 8]] + +So, obviously, the initial form will be a `step` function: + + AoC2017.4 == 0 swap [F +] step + + + F == [size] [unique size] cleave = + + +The `step_zero` combinator includes the `0 swap` that would normally open one of these definitions: + + +```python +J('[step_zero] help') +``` + + 0 roll> step + + + + AoC2017.4 == [F +] step_zero + + +```python +define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero') +``` + + +```python +J(''' + +[[5 1 9 5] + [7 5 4 3] + [2 4 6 8]] AoC2017.4 + +''') +``` + + 2 + diff --git a/docs/Advent_of_Code_2017_December_4th.rst b/docs/Advent_of_Code_2017_December_4th.rst new file mode 100644 index 0000000..e51ca12 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_4th.rst @@ -0,0 +1,77 @@ + +Advent of Code 2017 +=================== + +December 4th +------------ + +To ensure security, a valid passphrase must contain no duplicate words. + +For example: + +- aa bb cc dd ee is valid. +- aa bb cc dd aa is not valid - the word aa appears more than once. +- aa bb cc dd aaa is valid - aa and aaa count as different words. + +The system's full passphrase list is available as your puzzle input. How +many passphrases are valid? + +.. code:: ipython2 + + from notebook_preamble import J, V, define + +I'll assume the input is a Joy sequence of sequences of integers. + +:: + + [[5 1 9 5] + [7 5 4 3] + [2 4 6 8]] + +So, obviously, the initial form will be a ``step`` function: + +:: + + AoC2017.4 == 0 swap [F +] step + +:: + + F == [size] [unique size] cleave = + +The ``step_zero`` combinator includes the ``0 swap`` that would normally +open one of these definitions: + +.. code:: ipython2 + + J('[step_zero] help') + + +.. parsed-literal:: + + 0 roll> step + + + +:: + + AoC2017.4 == [F +] step_zero + +.. code:: ipython2 + + define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero') + +.. code:: ipython2 + + J(''' + + [[5 1 9 5] + [7 5 4 3] + [2 4 6 8]] AoC2017.4 + + ''') + + +.. parsed-literal:: + + 2 + diff --git a/docs/Advent_of_Code_2017_December_5th.html b/docs/Advent_of_Code_2017_December_5th.html new file mode 100644 index 0000000..66c6fcf --- /dev/null +++ b/docs/Advent_of_Code_2017_December_5th.html @@ -0,0 +1,12207 @@ + + + +Advent_of_Code_2017_December_5th + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Advent of Code 2017

December 5th

...a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.

+

In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.

+

For example, consider the following list of jump offsets:

+ +
0
+3
+0
+1
+-3
+
+
+

Positive jumps ("forward") move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:

+
    +
  • (0) 3 0 1 -3 - before we have taken any steps.
  • +
  • (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
  • +
  • 2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
  • +
  • 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.
  • +
  • 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.
  • +
  • 2 5 0 1 -2 - jump 4 steps forward, escaping the maze.
  • +
+

In this example, the exit is reached in 5 steps.

+

How many steps does it take to reach the exit?

+ +
+
+
+
+
+
+
+

Breakdown

For now, I'm going to assume a starting state with the size of the sequence pre-computed. We need it to define the exit condition and it is a trivial preamble to generate it. We then need and index and a step-count, which are both initially zero. Then we have the sequence itself, and some recursive function F that does the work.

+ +
   size index step-count [...] F
+-----------------------------------
+            step-count
+
+F == [P] [T] [R1] [R2] genrec
+
+
+

Later on I was thinking about it and the Forth heuristic came to mind, to wit: four things on the stack are kind of much. Immediately I realized that the size properly belongs in the predicate of F! D'oh!

+ +
   index step-count [...] F
+------------------------------
+         step-count
+ +
+
+
+
+
+
+
+

So, let's start by nailing down the predicate:

+ +
F == [P] [T] [R1]   [R2] genrec
+  == [P] [T] [R1 [F] R2] ifte
+
+0 0 [0 3 0 1 -3] popop 5 >=
+
+P == popop 5 >=
+ +
+
+
+
+
+
+
+

Now we need the else-part:

+ +
index step-count [0 3 0 1 -3] roll< popop
+
+E == roll< popop
+ +
+
+
+
+
+
+
+

Last but not least, the recursive branch

+ +
0 0 [0 3 0 1 -3] R1 [F] R2
+
+
+

The R1 function has a big job:

+ +
R1 == get the value at index
+      increment the value at the index
+      add the value gotten to the index
+      increment the step count
+
+
+

The only tricky thing there is incrementing an integer in the sequence. Joy sequences are not particularly good for random access. We could encode the list of jump offsets in a big integer and use math to do the processing for a good speed-up, but it still wouldn't beat the performance of e.g. a mutable array. This is just one of those places where "plain vanilla" Joypy doesn't shine (in default performance. The legendary Sufficiently-Smart Compiler would of course rewrite this function to use an array "under the hood".)

+

In the meantime, I'm going to write a primitive function that just does what we need.

+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import D, J, V, define
+from joy.library import SimpleFunctionWrapper
+from joy.utils.stack import list_to_stack
+
+
+@SimpleFunctionWrapper
+def incr_at(stack):
+    '''Given a index and a sequence of integers, increment the integer at the index.
+
+    E.g.:
+
+       3 [0 1 2 3 4 5] incr_at
+    -----------------------------
+         [0 1 2 4 4 5]
+    
+    '''
+    sequence, (i, stack) = stack
+    mem = []
+    while i >= 0:
+        term, sequence = sequence
+        mem.append(term)
+        i -= 1
+    mem[-1] += 1
+    return list_to_stack(mem, sequence), stack
+
+
+D['incr_at'] = incr_at
+
+ +
+
+
+ +
+
+
+
In [2]:
+
+
+
J('3 [0 1 2 3 4 5] incr_at')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0 1 2 4 4 5]
+
+
+
+ +
+
+ +
+
+
+
+
+

get the value at index

+
3 0 [0 1 2 3 4] [roll< at] nullary
+3 0 [0 1 2 n 4] n
+ +
+
+
+
+
+
+
+

increment the value at the index

+
3 0 [0 1 2 n 4] n [Q] dip
+3 0 [0 1 2 n 4] Q n
+3 0 [0 1 2 n 4] [popd incr_at] unary n
+3 0 [0 1 2 n+1 4] n
+ +
+
+
+
+
+
+
+

add the value gotten to the index

+
3 0 [0 1 2 n+1 4] n [+] cons dipd
+3 0 [0 1 2 n+1 4] [n +]      dipd
+3 n + 0 [0 1 2 n+1 4]
+3+n   0 [0 1 2 n+1 4]
+ +
+
+
+
+
+
+
+

increment the step count

+
3+n 0 [0 1 2 n+1 4] [++] dip
+3+n 1 [0 1 2 n+1 4]
+ +
+
+
+
+
+
+
+

All together now...

+
get_value == [roll< at] nullary
+incr_value == [[popd incr_at] unary] dip
+add_value == [+] cons dipd
+incr_step_count == [++] dip
+
+R1 == get_value incr_value add_value incr_step_count
+
+F == [P] [T] [R1] primrec
+
+F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec
+ +
+
+
+
+
+
In [3]:
+
+
+
from joy.library import DefinitionWrapper
+
+
+DefinitionWrapper.add_definitions('''
+
+      get_value == [roll< at] nullary
+     incr_value == [[popd incr_at] unary] dip
+      add_value == [+] cons dipd
+incr_step_count == [++] dip
+
+     AoC2017.5.0 == get_value incr_value add_value incr_step_count
+
+''', D)
+
+ +
+
+
+ +
+
+
+
In [4]:
+
+
+
define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')
+
+ +
+
+
+ +
+
+
+
In [5]:
+
+
+
J('0 0 [0 3 0 1 -3] F')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
5
+
+
+
+ +
+
+ +
+
+
+
+
+

Preamble for setting up predicate, index, and step-count

We want to go from this to this:

+ +
   [...] AoC2017.5.preamble
+------------------------------
+    0 0 [...] [popop n >=]
+
+
+

Where n is the size of the sequence.

+

The first part is obviously 0 0 roll<, then dup size:

+ +
[...] 0 0 roll< dup size
+0 0 [...] n
+
+
+

Then:

+ +
0 0 [...] n [>=] cons [popop] swoncat
+
+
+

So:

+ +
init-index-and-step-count == 0 0 roll<
+prepare-predicate == dup size [>=] cons [popop] swoncat
+
+AoC2017.5.preamble == init-index-and-step-count prepare-predicate
+ +
+
+
+
+
+
In [6]:
+
+
+
DefinitionWrapper.add_definitions('''
+
+init-index-and-step-count == 0 0 roll<
+        prepare-predicate == dup size [>=] cons [popop] swoncat
+
+       AoC2017.5.preamble == init-index-and-step-count prepare-predicate
+
+                AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
+
+''', D)
+
+ +
+
+
+ +
+
+
+
In [7]:
+
+
+
J('[0 3 0 1 -3] AoC2017.5')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
5
+
+
+
+ +
+
+ +
+
+
+
+
+ +
                AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
+
+              AoC2017.5.0 == get_value incr_value add_value incr_step_count
+       AoC2017.5.preamble == init-index-and-step-count prepare-predicate
+
+                get_value == [roll< at] nullary
+               incr_value == [[popd incr_at] unary] dip
+                add_value == [+] cons dipd
+          incr_step_count == [++] dip
+
+init-index-and-step-count == 0 0 roll<
+        prepare-predicate == dup size [>=] cons [popop] swoncat
+ +
+
+
+
+
+
+
+

This is by far the largest program I have yet written in Joy. Even with the incr_at function it is still a bear. There may be an arrangement of the parameters that would permit more elegant definitions, but it still wouldn't be as efficient as something written in assembly, C, or even Python.

+ +
+
+
+
+
+ + + + + + diff --git a/docs/Advent_of_Code_2017_December_5th.ipynb b/docs/Advent_of_Code_2017_December_5th.ipynb new file mode 100644 index 0000000..32d264e --- /dev/null +++ b/docs/Advent_of_Code_2017_December_5th.ipynb @@ -0,0 +1,380 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advent of Code 2017\n", + "\n", + "## December 5th\n", + "...a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.\n", + "\n", + "In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.\n", + "\n", + "For example, consider the following list of jump offsets:\n", + "\n", + " 0\n", + " 3\n", + " 0\n", + " 1\n", + " -3\n", + "\n", + "Positive jumps (\"forward\") move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:\n", + "\n", + "* (0) 3 0 1 -3 - before we have taken any steps.\n", + "* (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.\n", + "* 2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.\n", + "* 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.\n", + "* 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.\n", + "* 2 5 0 1 -2 - jump 4 steps forward, escaping the maze.\n", + "\n", + "In this example, the exit is reached in 5 steps.\n", + "\n", + "How many steps does it take to reach the exit?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Breakdown\n", + "For now, I'm going to assume a starting state with the size of the sequence pre-computed. We need it to define the exit condition and it is a trivial preamble to generate it. We then need and `index` and a `step-count`, which are both initially zero. Then we have the sequence itself, and some recursive function `F` that does the work.\n", + "\n", + " size index step-count [...] F\n", + " -----------------------------------\n", + " step-count\n", + "\n", + " F == [P] [T] [R1] [R2] genrec\n", + "\n", + "Later on I was thinking about it and the Forth heuristic came to mind, to wit: four things on the stack are kind of much. Immediately I realized that the size properly belongs in the predicate of `F`! D'oh!\n", + "\n", + " index step-count [...] F\n", + " ------------------------------\n", + " step-count" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So, let's start by nailing down the predicate:\n", + "\n", + " F == [P] [T] [R1] [R2] genrec\n", + " == [P] [T] [R1 [F] R2] ifte\n", + "\n", + " 0 0 [0 3 0 1 -3] popop 5 >=\n", + "\n", + " P == popop 5 >=" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we need the else-part:\n", + "\n", + " index step-count [0 3 0 1 -3] roll< popop\n", + "\n", + " E == roll< popop" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Last but not least, the recursive branch\n", + "\n", + " 0 0 [0 3 0 1 -3] R1 [F] R2\n", + "\n", + "The `R1` function has a big job:\n", + "\n", + " R1 == get the value at index\n", + " increment the value at the index\n", + " add the value gotten to the index\n", + " increment the step count\n", + "\n", + "The only tricky thing there is incrementing an integer in the sequence. Joy sequences are not particularly good for random access. We could encode the list of jump offsets in a big integer and use math to do the processing for a good speed-up, but it still wouldn't beat the performance of e.g. a mutable array. This is just one of those places where \"plain vanilla\" Joypy doesn't shine (in default performance. The legendary *Sufficiently-Smart Compiler* would of course rewrite this function to use an array \"under the hood\".)\n", + "\n", + "In the meantime, I'm going to write a primitive function that just does what we need." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import D, J, V, define\n", + "from joy.library import SimpleFunctionWrapper\n", + "from joy.utils.stack import list_to_stack\n", + "\n", + "\n", + "@SimpleFunctionWrapper\n", + "def incr_at(stack):\n", + " '''Given a index and a sequence of integers, increment the integer at the index.\n", + "\n", + " E.g.:\n", + "\n", + " 3 [0 1 2 3 4 5] incr_at\n", + " -----------------------------\n", + " [0 1 2 4 4 5]\n", + " \n", + " '''\n", + " sequence, (i, stack) = stack\n", + " mem = []\n", + " while i >= 0:\n", + " term, sequence = sequence\n", + " mem.append(term)\n", + " i -= 1\n", + " mem[-1] += 1\n", + " return list_to_stack(mem, sequence), stack\n", + "\n", + "\n", + "D['incr_at'] = incr_at" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 2 4 4 5]\n" + ] + } + ], + "source": [ + "J('3 [0 1 2 3 4 5] incr_at')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### get the value at index\n", + "\n", + " 3 0 [0 1 2 3 4] [roll< at] nullary\n", + " 3 0 [0 1 2 n 4] n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### increment the value at the index\n", + "\n", + " 3 0 [0 1 2 n 4] n [Q] dip\n", + " 3 0 [0 1 2 n 4] Q n\n", + " 3 0 [0 1 2 n 4] [popd incr_at] unary n\n", + " 3 0 [0 1 2 n+1 4] n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### add the value gotten to the index\n", + "\n", + " 3 0 [0 1 2 n+1 4] n [+] cons dipd\n", + " 3 0 [0 1 2 n+1 4] [n +] dipd\n", + " 3 n + 0 [0 1 2 n+1 4]\n", + " 3+n 0 [0 1 2 n+1 4]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### increment the step count\n", + "\n", + " 3+n 0 [0 1 2 n+1 4] [++] dip\n", + " 3+n 1 [0 1 2 n+1 4]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### All together now...\n", + "\n", + " get_value == [roll< at] nullary\n", + " incr_value == [[popd incr_at] unary] dip\n", + " add_value == [+] cons dipd\n", + " incr_step_count == [++] dip\n", + "\n", + " R1 == get_value incr_value add_value incr_step_count\n", + "\n", + " F == [P] [T] [R1] primrec\n", + " \n", + " F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from joy.library import DefinitionWrapper\n", + "\n", + "\n", + "DefinitionWrapper.add_definitions('''\n", + "\n", + " get_value == [roll< at] nullary\n", + " incr_value == [[popd incr_at] unary] dip\n", + " add_value == [+] cons dipd\n", + "incr_step_count == [++] dip\n", + "\n", + " AoC2017.5.0 == get_value incr_value add_value incr_step_count\n", + "\n", + "''', D)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "J('0 0 [0 3 0 1 -3] F')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Preamble for setting up predicate, `index`, and `step-count`\n", + "\n", + "We want to go from this to this:\n", + "\n", + " [...] AoC2017.5.preamble\n", + " ------------------------------\n", + " 0 0 [...] [popop n >=]\n", + "\n", + "Where `n` is the size of the sequence.\n", + "\n", + "The first part is obviously `0 0 roll<`, then `dup size`:\n", + "\n", + " [...] 0 0 roll< dup size\n", + " 0 0 [...] n\n", + "\n", + "Then:\n", + "\n", + " 0 0 [...] n [>=] cons [popop] swoncat\n", + "\n", + "So:\n", + "\n", + " init-index-and-step-count == 0 0 roll<\n", + " prepare-predicate == dup size [>=] cons [popop] swoncat\n", + "\n", + " AoC2017.5.preamble == init-index-and-step-count prepare-predicate" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "DefinitionWrapper.add_definitions('''\n", + "\n", + "init-index-and-step-count == 0 0 roll<\n", + " prepare-predicate == dup size [>=] cons [popop] swoncat\n", + "\n", + " AoC2017.5.preamble == init-index-and-step-count prepare-predicate\n", + "\n", + " AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec\n", + "\n", + "''', D)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "J('[0 3 0 1 -3] AoC2017.5')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec\n", + "\n", + " AoC2017.5.0 == get_value incr_value add_value incr_step_count\n", + " AoC2017.5.preamble == init-index-and-step-count prepare-predicate\n", + "\n", + " get_value == [roll< at] nullary\n", + " incr_value == [[popd incr_at] unary] dip\n", + " add_value == [+] cons dipd\n", + " incr_step_count == [++] dip\n", + "\n", + " init-index-and-step-count == 0 0 roll<\n", + " prepare-predicate == dup size [>=] cons [popop] swoncat\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is by far the largest program I have yet written in Joy. Even with the `incr_at` function it is still a bear. There may be an arrangement of the parameters that would permit more elegant definitions, but it still wouldn't be as efficient as something written in assembly, C, or even Python." + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Advent_of_Code_2017_December_5th.md b/docs/Advent_of_Code_2017_December_5th.md new file mode 100644 index 0000000..8edfe6f --- /dev/null +++ b/docs/Advent_of_Code_2017_December_5th.md @@ -0,0 +1,244 @@ + +# Advent of Code 2017 + +## December 5th +...a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list. + +In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered. + +For example, consider the following list of jump offsets: + + 0 + 3 + 0 + 1 + -3 + +Positive jumps ("forward") move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found: + +* (0) 3 0 1 -3 - before we have taken any steps. +* (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1. +* 2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2. +* 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind. +* 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2. +* 2 5 0 1 -2 - jump 4 steps forward, escaping the maze. + +In this example, the exit is reached in 5 steps. + +How many steps does it take to reach the exit? + +## Breakdown +For now, I'm going to assume a starting state with the size of the sequence pre-computed. We need it to define the exit condition and it is a trivial preamble to generate it. We then need and `index` and a `step-count`, which are both initially zero. Then we have the sequence itself, and some recursive function `F` that does the work. + + size index step-count [...] F + ----------------------------------- + step-count + + F == [P] [T] [R1] [R2] genrec + +Later on I was thinking about it and the Forth heuristic came to mind, to wit: four things on the stack are kind of much. Immediately I realized that the size properly belongs in the predicate of `F`! D'oh! + + index step-count [...] F + ------------------------------ + step-count + +So, let's start by nailing down the predicate: + + F == [P] [T] [R1] [R2] genrec + == [P] [T] [R1 [F] R2] ifte + + 0 0 [0 3 0 1 -3] popop 5 >= + + P == popop 5 >= + +Now we need the else-part: + + index step-count [0 3 0 1 -3] roll< popop + + E == roll< popop + +Last but not least, the recursive branch + + 0 0 [0 3 0 1 -3] R1 [F] R2 + +The `R1` function has a big job: + + R1 == get the value at index + increment the value at the index + add the value gotten to the index + increment the step count + +The only tricky thing there is incrementing an integer in the sequence. Joy sequences are not particularly good for random access. We could encode the list of jump offsets in a big integer and use math to do the processing for a good speed-up, but it still wouldn't beat the performance of e.g. a mutable array. This is just one of those places where "plain vanilla" Joypy doesn't shine (in default performance. The legendary *Sufficiently-Smart Compiler* would of course rewrite this function to use an array "under the hood".) + +In the meantime, I'm going to write a primitive function that just does what we need. + + +```python +from notebook_preamble import D, J, V, define +from joy.library import SimpleFunctionWrapper +from joy.utils.stack import list_to_stack + + +@SimpleFunctionWrapper +def incr_at(stack): + '''Given a index and a sequence of integers, increment the integer at the index. + + E.g.: + + 3 [0 1 2 3 4 5] incr_at + ----------------------------- + [0 1 2 4 4 5] + + ''' + sequence, (i, stack) = stack + mem = [] + while i >= 0: + term, sequence = sequence + mem.append(term) + i -= 1 + mem[-1] += 1 + return list_to_stack(mem, sequence), stack + + +D['incr_at'] = incr_at +``` + + +```python +J('3 [0 1 2 3 4 5] incr_at') +``` + + [0 1 2 4 4 5] + + +### get the value at index + + 3 0 [0 1 2 3 4] [roll< at] nullary + 3 0 [0 1 2 n 4] n + +### increment the value at the index + + 3 0 [0 1 2 n 4] n [Q] dip + 3 0 [0 1 2 n 4] Q n + 3 0 [0 1 2 n 4] [popd incr_at] unary n + 3 0 [0 1 2 n+1 4] n + +### add the value gotten to the index + + 3 0 [0 1 2 n+1 4] n [+] cons dipd + 3 0 [0 1 2 n+1 4] [n +] dipd + 3 n + 0 [0 1 2 n+1 4] + 3+n 0 [0 1 2 n+1 4] + +### increment the step count + + 3+n 0 [0 1 2 n+1 4] [++] dip + 3+n 1 [0 1 2 n+1 4] + +### All together now... + + get_value == [roll< at] nullary + incr_value == [[popd incr_at] unary] dip + add_value == [+] cons dipd + incr_step_count == [++] dip + + R1 == get_value incr_value add_value incr_step_count + + F == [P] [T] [R1] primrec + + F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec + + +```python +from joy.library import DefinitionWrapper + + +DefinitionWrapper.add_definitions(''' + + get_value == [roll< at] nullary + incr_value == [[popd incr_at] unary] dip + add_value == [+] cons dipd +incr_step_count == [++] dip + + AoC2017.5.0 == get_value incr_value add_value incr_step_count + +''', D) +``` + + +```python +define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec') +``` + + +```python +J('0 0 [0 3 0 1 -3] F') +``` + + 5 + + +### Preamble for setting up predicate, `index`, and `step-count` + +We want to go from this to this: + + [...] AoC2017.5.preamble + ------------------------------ + 0 0 [...] [popop n >=] + +Where `n` is the size of the sequence. + +The first part is obviously `0 0 roll<`, then `dup size`: + + [...] 0 0 roll< dup size + 0 0 [...] n + +Then: + + 0 0 [...] n [>=] cons [popop] swoncat + +So: + + init-index-and-step-count == 0 0 roll< + prepare-predicate == dup size [>=] cons [popop] swoncat + + AoC2017.5.preamble == init-index-and-step-count prepare-predicate + + +```python +DefinitionWrapper.add_definitions(''' + +init-index-and-step-count == 0 0 roll< + prepare-predicate == dup size [>=] cons [popop] swoncat + + AoC2017.5.preamble == init-index-and-step-count prepare-predicate + + AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec + +''', D) +``` + + +```python +J('[0 3 0 1 -3] AoC2017.5') +``` + + 5 + + + + AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec + + AoC2017.5.0 == get_value incr_value add_value incr_step_count + AoC2017.5.preamble == init-index-and-step-count prepare-predicate + + get_value == [roll< at] nullary + incr_value == [[popd incr_at] unary] dip + add_value == [+] cons dipd + incr_step_count == [++] dip + + init-index-and-step-count == 0 0 roll< + prepare-predicate == dup size [>=] cons [popop] swoncat + + +This is by far the largest program I have yet written in Joy. Even with the `incr_at` function it is still a bear. There may be an arrangement of the parameters that would permit more elegant definitions, but it still wouldn't be as efficient as something written in assembly, C, or even Python. diff --git a/docs/Advent_of_Code_2017_December_5th.rst b/docs/Advent_of_Code_2017_December_5th.rst new file mode 100644 index 0000000..5040c69 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_5th.rst @@ -0,0 +1,324 @@ + +Advent of Code 2017 +=================== + +December 5th +------------ + +...a list of the offsets for each jump. Jumps are relative: -1 moves to +the previous instruction, and 2 skips the next one. Start at the first +instruction in the list. The goal is to follow the jumps until one leads +outside the list. + +In addition, these instructions are a little strange; after each jump, +the offset of that instruction increases by 1. So, if you come across an +offset of 3, you would move three instructions forward, but change it to +a 4 for the next time it is encountered. + +For example, consider the following list of jump offsets: + +:: + + 0 + 3 + 0 + 1 + -3 + +Positive jumps ("forward") move downward; negative jumps move upward. +For legibility in this example, these offset values will be written all +on one line, with the current instruction marked in parentheses. The +following steps would be taken before an exit is found: + +- + + (0) 3 0 1 -3 - before we have taken any steps. + +- + + (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). + Fortunately, the instruction is then incremented to 1. + +- 2 (3) 0 1 -3 - step forward because of the instruction we just + modified. The first instruction is incremented again, now to 2. +- 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind. +- 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2. +- 2 5 0 1 -2 - jump 4 steps forward, escaping the maze. + +In this example, the exit is reached in 5 steps. + +How many steps does it take to reach the exit? + +Breakdown +--------- + +For now, I'm going to assume a starting state with the size of the +sequence pre-computed. We need it to define the exit condition and it is +a trivial preamble to generate it. We then need and ``index`` and a +``step-count``, which are both initially zero. Then we have the sequence +itself, and some recursive function ``F`` that does the work. + +:: + + size index step-count [...] F + ----------------------------------- + step-count + + F == [P] [T] [R1] [R2] genrec + +Later on I was thinking about it and the Forth heuristic came to mind, +to wit: four things on the stack are kind of much. Immediately I +realized that the size properly belongs in the predicate of ``F``! D'oh! + +:: + + index step-count [...] F + ------------------------------ + step-count + +So, let's start by nailing down the predicate: + +:: + + F == [P] [T] [R1] [R2] genrec + == [P] [T] [R1 [F] R2] ifte + + 0 0 [0 3 0 1 -3] popop 5 >= + + P == popop 5 >= + +Now we need the else-part: + +:: + + index step-count [0 3 0 1 -3] roll< popop + + E == roll< popop + +Last but not least, the recursive branch + +:: + + 0 0 [0 3 0 1 -3] R1 [F] R2 + +The ``R1`` function has a big job: + +:: + + R1 == get the value at index + increment the value at the index + add the value gotten to the index + increment the step count + +The only tricky thing there is incrementing an integer in the sequence. +Joy sequences are not particularly good for random access. We could +encode the list of jump offsets in a big integer and use math to do the +processing for a good speed-up, but it still wouldn't beat the +performance of e.g. a mutable array. This is just one of those places +where "plain vanilla" Joypy doesn't shine (in default performance. The +legendary *Sufficiently-Smart Compiler* would of course rewrite this +function to use an array "under the hood".) + +In the meantime, I'm going to write a primitive function that just does +what we need. + +.. code:: ipython2 + + from notebook_preamble import D, J, V, define + from joy.library import SimpleFunctionWrapper + from joy.utils.stack import list_to_stack + + + @SimpleFunctionWrapper + def incr_at(stack): + '''Given a index and a sequence of integers, increment the integer at the index. + + E.g.: + + 3 [0 1 2 3 4 5] incr_at + ----------------------------- + [0 1 2 4 4 5] + + ''' + sequence, (i, stack) = stack + mem = [] + while i >= 0: + term, sequence = sequence + mem.append(term) + i -= 1 + mem[-1] += 1 + return list_to_stack(mem, sequence), stack + + + D['incr_at'] = incr_at + +.. code:: ipython2 + + J('3 [0 1 2 3 4 5] incr_at') + + +.. parsed-literal:: + + [0 1 2 4 4 5] + + +get the value at index +~~~~~~~~~~~~~~~~~~~~~~ + +:: + + 3 0 [0 1 2 3 4] [roll< at] nullary + 3 0 [0 1 2 n 4] n + +increment the value at the index +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + 3 0 [0 1 2 n 4] n [Q] dip + 3 0 [0 1 2 n 4] Q n + 3 0 [0 1 2 n 4] [popd incr_at] unary n + 3 0 [0 1 2 n+1 4] n + +add the value gotten to the index +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + 3 0 [0 1 2 n+1 4] n [+] cons dipd + 3 0 [0 1 2 n+1 4] [n +] dipd + 3 n + 0 [0 1 2 n+1 4] + 3+n 0 [0 1 2 n+1 4] + +increment the step count +~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + 3+n 0 [0 1 2 n+1 4] [++] dip + 3+n 1 [0 1 2 n+1 4] + +All together now... +~~~~~~~~~~~~~~~~~~~ + +:: + + get_value == [roll< at] nullary + incr_value == [[popd incr_at] unary] dip + add_value == [+] cons dipd + incr_step_count == [++] dip + + R1 == get_value incr_value add_value incr_step_count + + F == [P] [T] [R1] primrec + + F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec + +.. code:: ipython2 + + from joy.library import DefinitionWrapper + + + DefinitionWrapper.add_definitions(''' + + get_value == [roll< at] nullary + incr_value == [[popd incr_at] unary] dip + add_value == [+] cons dipd + incr_step_count == [++] dip + + AoC2017.5.0 == get_value incr_value add_value incr_step_count + + ''', D) + +.. code:: ipython2 + + define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec') + +.. code:: ipython2 + + J('0 0 [0 3 0 1 -3] F') + + +.. parsed-literal:: + + 5 + + +Preamble for setting up predicate, ``index``, and ``step-count`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +We want to go from this to this: + +:: + + [...] AoC2017.5.preamble + ------------------------------ + 0 0 [...] [popop n >=] + +Where ``n`` is the size of the sequence. + +The first part is obviously ``0 0 roll<``, then ``dup size``: + +:: + + [...] 0 0 roll< dup size + 0 0 [...] n + +Then: + +:: + + 0 0 [...] n [>=] cons [popop] swoncat + +So: + +:: + + init-index-and-step-count == 0 0 roll< + prepare-predicate == dup size [>=] cons [popop] swoncat + + AoC2017.5.preamble == init-index-and-step-count prepare-predicate + +.. code:: ipython2 + + DefinitionWrapper.add_definitions(''' + + init-index-and-step-count == 0 0 roll< + prepare-predicate == dup size [>=] cons [popop] swoncat + + AoC2017.5.preamble == init-index-and-step-count prepare-predicate + + AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec + + ''', D) + +.. code:: ipython2 + + J('[0 3 0 1 -3] AoC2017.5') + + +.. parsed-literal:: + + 5 + + +:: + + AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec + + AoC2017.5.0 == get_value incr_value add_value incr_step_count + AoC2017.5.preamble == init-index-and-step-count prepare-predicate + + get_value == [roll< at] nullary + incr_value == [[popd incr_at] unary] dip + add_value == [+] cons dipd + incr_step_count == [++] dip + + init-index-and-step-count == 0 0 roll< + prepare-predicate == dup size [>=] cons [popop] swoncat + +This is by far the largest program I have yet written in Joy. Even with +the ``incr_at`` function it is still a bear. There may be an arrangement +of the parameters that would permit more elegant definitions, but it +still wouldn't be as efficient as something written in assembly, C, or +even Python. diff --git a/docs/Advent_of_Code_2017_December_6th.html b/docs/Advent_of_Code_2017_December_6th.html new file mode 100644 index 0000000..c7092e2 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_6th.html @@ -0,0 +1,12415 @@ + + + +Advent_of_Code_2017_December_6th + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Advent of Code 2017

December 6th

+
[0 2 7 0] dup max
+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import D, J, V, define
+
+ +
+
+
+ +
+
+
+
In [2]:
+
+
+
J('[0 2 7 0] dup max')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0 2 7 0] 7
+
+
+
+ +
+
+ +
+
+
+
In [3]:
+
+
+
from joy.library import SimpleFunctionWrapper
+from joy.utils.stack import list_to_stack
+
+
+@SimpleFunctionWrapper
+def index_of(stack):
+    '''Given a sequence and a item, return the index of the item, or -1 if not found.
+
+    E.g.:
+
+       [a b c] a index_of
+    ------------------------
+               0
+
+       [a b c] d index_of
+    ------------------------
+            -1
+
+    '''
+    item, (sequence, stack) = stack
+    i = 0
+    while sequence:
+        term, sequence = sequence
+        if term == item:
+            break
+        i += 1
+    else:
+        i = -1
+    return i, stack
+
+
+D['index_of'] = index_of
+
+ +
+
+
+ +
+
+
+
In [4]:
+
+
+
J('[0 2 7 0] 7 index_of')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [5]:
+
+
+
J('[0 2 7 0] 23 index_of')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
-1
+
+
+
+ +
+
+ +
+
+
+
+
+

Starting at index distribute count "blocks" to the "banks" in the sequence.

+ +
[...] count index distribute
+----------------------------
+           [...]
+
+
+

This seems like it would be a PITA to implement in Joypy...

+ +
+
+
+
+
+
In [6]:
+
+
+
from joy.utils.stack import iter_stack, list_to_stack
+
+
+@SimpleFunctionWrapper
+def distribute(stack):
+    '''Starting at index+1 distribute count "blocks" to the "banks" in the sequence.
+
+    [...] count index distribute
+    ----------------------------
+               [...]
+
+    '''
+    index, (count, (sequence, stack)) = stack
+    assert count >= 0
+    cheat = list(iter_stack(sequence))
+    n = len(cheat)
+    assert index < n
+    cheat[index] = 0
+    while count:
+        index += 1
+        index %= n
+        cheat[index] += 1
+        count -= 1
+    return list_to_stack(cheat), stack
+
+
+D['distribute'] = distribute
+
+ +
+
+
+ +
+
+
+
In [7]:
+
+
+
J('[0 2 7 0] dup max [index_of] nullary distribute')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[2 4 1 2]
+
+
+
+ +
+
+ +
+
+
+
In [8]:
+
+
+
J('[2 4 1 2] dup max [index_of] nullary distribute')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[3 1 2 3]
+
+
+
+ +
+
+ +
+
+
+
In [9]:
+
+
+
J('[3 1 2 3] dup max [index_of] nullary distribute')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0 2 3 4]
+
+
+
+ +
+
+ +
+
+
+
In [10]:
+
+
+
J('[0 2 3 4] dup max [index_of] nullary distribute')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 3 4 1]
+
+
+
+ +
+
+ +
+
+
+
In [11]:
+
+
+
J('[1 3 4 1] dup max [index_of] nullary distribute')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[2 4 1 2]
+
+
+
+ +
+
+ +
+
+
+
+
+

Recalling "Generator Programs"

+
[a F] x
+[a F] a F 
+
+[a F] a swap [C] dip rest cons
+a   [a F]    [C] dip rest cons
+a C [a F]            rest cons
+a C   [F]                 cons
+
+w/ C == dup G
+
+a dup G [F] cons
+a a   G [F] cons
+
+w/ G == dup max [index_of] nullary distribute
+ +
+
+
+
+
+
In [12]:
+
+
+
define('direco == dip rest cons')
+
+ +
+
+
+ +
+
+
+
In [13]:
+
+
+
define('G == [direco] cons [swap] swoncat cons')
+
+ +
+
+
+ +
+
+
+
In [14]:
+
+
+
define('make_distributor == [dup dup max [index_of] nullary distribute] G')
+
+ +
+
+
+ +
+
+
+
In [15]:
+
+
+
J('[0 2 7 0] make_distributor 6 [x] times pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2]
+
+
+
+ +
+
+ +
+
+
+
+
+

A function to drive a generator and count how many states before a repeat.

First draft:

+ +
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
+
+
+

(?)

+ +
[]       [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
+[] [...] [GEN]   [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
+[] [...] [GEN]    pop index_of 0 >=
+[] [...]              index_of 0 >=
+                            -1 0 >=
+                             False
+
+
+

Base case

+ +
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
+[] [...] [GEN]                      pop size --
+[] [...]                                size --
+[] [...]                                size --
+
+
+

A mistake, popop and no need for --

+ +
[] [...] [GEN] popop size
+[]                   size
+n
+
+
+

Recursive case

+ +
[] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
+[] [...] [GEN]                                   [swons] dip x  F
+[] [...] swons [GEN]                                         x  F
+[[...]]        [GEN]                                         x  F
+[[...]] [...]  [GEN]                                            F
+
+[[...]] [...] [GEN] F
+
+
+

What have we learned?

+ +
F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
+ +
+
+
+
+
+
In [16]:
+
+
+
define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec')
+
+ +
+
+
+ +
+
+
+
In [17]:
+
+
+
define('AoC2017.6 == make_distributor count_states')
+
+ +
+
+
+ +
+
+
+
In [18]:
+
+
+
J('[0 2 7 0] AoC2017.6')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
5
+
+
+
+ +
+
+ +
+
+
+
In [19]:
+
+
+
J('[1 1 1] AoC2017.6')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4
+
+
+
+ +
+
+ +
+
+
+
In [20]:
+
+
+
J('[8 0 0 0 0 0] AoC2017.6')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
15
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/Advent_of_Code_2017_December_6th.ipynb b/docs/Advent_of_Code_2017_December_6th.ipynb new file mode 100644 index 0000000..6d7f188 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_6th.ipynb @@ -0,0 +1,457 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advent of Code 2017\n", + "\n", + "## December 6th\n", + "\n", + "\n", + " [0 2 7 0] dup max\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import D, J, V, define" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 2 7 0] 7\n" + ] + } + ], + "source": [ + "J('[0 2 7 0] dup max')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from joy.library import SimpleFunctionWrapper\n", + "from joy.utils.stack import list_to_stack\n", + "\n", + "\n", + "@SimpleFunctionWrapper\n", + "def index_of(stack):\n", + " '''Given a sequence and a item, return the index of the item, or -1 if not found.\n", + "\n", + " E.g.:\n", + "\n", + " [a b c] a index_of\n", + " ------------------------\n", + " 0\n", + "\n", + " [a b c] d index_of\n", + " ------------------------\n", + " -1\n", + "\n", + " '''\n", + " item, (sequence, stack) = stack\n", + " i = 0\n", + " while sequence:\n", + " term, sequence = sequence\n", + " if term == item:\n", + " break\n", + " i += 1\n", + " else:\n", + " i = -1\n", + " return i, stack\n", + "\n", + "\n", + "D['index_of'] = index_of" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "J('[0 2 7 0] 7 index_of')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-1\n" + ] + } + ], + "source": [ + "J('[0 2 7 0] 23 index_of')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Starting at `index` distribute `count` \"blocks\" to the \"banks\" in the sequence.\n", + "\n", + " [...] count index distribute\n", + " ----------------------------\n", + " [...]\n", + "\n", + "This seems like it would be a PITA to implement in Joypy..." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "from joy.utils.stack import iter_stack, list_to_stack\n", + "\n", + "\n", + "@SimpleFunctionWrapper\n", + "def distribute(stack):\n", + " '''Starting at index+1 distribute count \"blocks\" to the \"banks\" in the sequence.\n", + "\n", + " [...] count index distribute\n", + " ----------------------------\n", + " [...]\n", + "\n", + " '''\n", + " index, (count, (sequence, stack)) = stack\n", + " assert count >= 0\n", + " cheat = list(iter_stack(sequence))\n", + " n = len(cheat)\n", + " assert index < n\n", + " cheat[index] = 0\n", + " while count:\n", + " index += 1\n", + " index %= n\n", + " cheat[index] += 1\n", + " count -= 1\n", + " return list_to_stack(cheat), stack\n", + "\n", + "\n", + "D['distribute'] = distribute" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 4 1 2]\n" + ] + } + ], + "source": [ + "J('[0 2 7 0] dup max [index_of] nullary distribute')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3 1 2 3]\n" + ] + } + ], + "source": [ + "J('[2 4 1 2] dup max [index_of] nullary distribute')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 2 3 4]\n" + ] + } + ], + "source": [ + "J('[3 1 2 3] dup max [index_of] nullary distribute')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 3 4 1]\n" + ] + } + ], + "source": [ + "J('[0 2 3 4] dup max [index_of] nullary distribute')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2 4 1 2]\n" + ] + } + ], + "source": [ + "J('[1 3 4 1] dup max [index_of] nullary distribute')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Recalling \"Generator Programs\"\n", + "\n", + " [a F] x\n", + " [a F] a F \n", + " \n", + " [a F] a swap [C] dip rest cons\n", + " a [a F] [C] dip rest cons\n", + " a C [a F] rest cons\n", + " a C [F] cons\n", + "\n", + " w/ C == dup G\n", + "\n", + " a dup G [F] cons\n", + " a a G [F] cons\n", + "\n", + " w/ G == dup max [index_of] nullary distribute" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "define('direco == dip rest cons')" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "define('G == [direco] cons [swap] swoncat cons')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "define('make_distributor == [dup dup max [index_of] nullary distribute] G')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2]\n" + ] + } + ], + "source": [ + "J('[0 2 7 0] make_distributor 6 [x] times pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### A function to drive a generator and count how many states before a repeat.\n", + "First draft:\n", + "\n", + " [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n", + "\n", + "(?)\n", + "\n", + " [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n", + " [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n", + " [] [...] [GEN] pop index_of 0 >=\n", + " [] [...] index_of 0 >=\n", + " -1 0 >=\n", + " False\n", + "\n", + "Base case\n", + "\n", + " [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n", + " [] [...] [GEN] pop size --\n", + " [] [...] size --\n", + " [] [...] size --\n", + "\n", + "A mistake, `popop` and no need for `--`\n", + "\n", + " [] [...] [GEN] popop size\n", + " [] size\n", + " n\n", + "\n", + "Recursive case\n", + "\n", + " [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec\n", + " [] [...] [GEN] [swons] dip x F\n", + " [] [...] swons [GEN] x F\n", + " [[...]] [GEN] x F\n", + " [[...]] [...] [GEN] F\n", + "\n", + " [[...]] [...] [GEN] F\n", + "\n", + "What have we learned?\n", + "\n", + " F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "define('AoC2017.6 == make_distributor count_states')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "J('[0 2 7 0] AoC2017.6')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "J('[1 1 1] AoC2017.6')" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n" + ] + } + ], + "source": [ + "J('[8 0 0 0 0 0] AoC2017.6')" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Advent_of_Code_2017_December_6th.md b/docs/Advent_of_Code_2017_December_6th.md new file mode 100644 index 0000000..0697b11 --- /dev/null +++ b/docs/Advent_of_Code_2017_December_6th.md @@ -0,0 +1,267 @@ + +# Advent of Code 2017 + +## December 6th + + + [0 2 7 0] dup max + + + +```python +from notebook_preamble import D, J, V, define +``` + + +```python +J('[0 2 7 0] dup max') +``` + + [0 2 7 0] 7 + + + +```python +from joy.library import SimpleFunctionWrapper +from joy.utils.stack import list_to_stack + + +@SimpleFunctionWrapper +def index_of(stack): + '''Given a sequence and a item, return the index of the item, or -1 if not found. + + E.g.: + + [a b c] a index_of + ------------------------ + 0 + + [a b c] d index_of + ------------------------ + -1 + + ''' + item, (sequence, stack) = stack + i = 0 + while sequence: + term, sequence = sequence + if term == item: + break + i += 1 + else: + i = -1 + return i, stack + + +D['index_of'] = index_of +``` + + +```python +J('[0 2 7 0] 7 index_of') +``` + + 2 + + + +```python +J('[0 2 7 0] 23 index_of') +``` + + -1 + + +Starting at `index` distribute `count` "blocks" to the "banks" in the sequence. + + [...] count index distribute + ---------------------------- + [...] + +This seems like it would be a PITA to implement in Joypy... + + +```python +from joy.utils.stack import iter_stack, list_to_stack + + +@SimpleFunctionWrapper +def distribute(stack): + '''Starting at index+1 distribute count "blocks" to the "banks" in the sequence. + + [...] count index distribute + ---------------------------- + [...] + + ''' + index, (count, (sequence, stack)) = stack + assert count >= 0 + cheat = list(iter_stack(sequence)) + n = len(cheat) + assert index < n + cheat[index] = 0 + while count: + index += 1 + index %= n + cheat[index] += 1 + count -= 1 + return list_to_stack(cheat), stack + + +D['distribute'] = distribute +``` + + +```python +J('[0 2 7 0] dup max [index_of] nullary distribute') +``` + + [2 4 1 2] + + + +```python +J('[2 4 1 2] dup max [index_of] nullary distribute') +``` + + [3 1 2 3] + + + +```python +J('[3 1 2 3] dup max [index_of] nullary distribute') +``` + + [0 2 3 4] + + + +```python +J('[0 2 3 4] dup max [index_of] nullary distribute') +``` + + [1 3 4 1] + + + +```python +J('[1 3 4 1] dup max [index_of] nullary distribute') +``` + + [2 4 1 2] + + +### Recalling "Generator Programs" + + [a F] x + [a F] a F + + [a F] a swap [C] dip rest cons + a [a F] [C] dip rest cons + a C [a F] rest cons + a C [F] cons + + w/ C == dup G + + a dup G [F] cons + a a G [F] cons + + w/ G == dup max [index_of] nullary distribute + + +```python +define('direco == dip rest cons') +``` + + +```python +define('G == [direco] cons [swap] swoncat cons') +``` + + +```python +define('make_distributor == [dup dup max [index_of] nullary distribute] G') +``` + + +```python +J('[0 2 7 0] make_distributor 6 [x] times pop') +``` + + [0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2] + + +### A function to drive a generator and count how many states before a repeat. +First draft: + + [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + +(?) + + [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + [] [...] [GEN] pop index_of 0 >= + [] [...] index_of 0 >= + -1 0 >= + False + +Base case + + [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + [] [...] [GEN] pop size -- + [] [...] size -- + [] [...] size -- + +A mistake, `popop` and no need for `--` + + [] [...] [GEN] popop size + [] size + n + +Recursive case + + [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec + [] [...] [GEN] [swons] dip x F + [] [...] swons [GEN] x F + [[...]] [GEN] x F + [[...]] [...] [GEN] F + + [[...]] [...] [GEN] F + +What have we learned? + + F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec + + +```python +define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec') +``` + + +```python +define('AoC2017.6 == make_distributor count_states') +``` + + +```python +J('[0 2 7 0] AoC2017.6') +``` + + 5 + + + +```python +J('[1 1 1] AoC2017.6') +``` + + 4 + + + +```python +J('[8 0 0 0 0 0] AoC2017.6') +``` + + 15 + diff --git a/docs/Advent_of_Code_2017_December_6th.rst b/docs/Advent_of_Code_2017_December_6th.rst new file mode 100644 index 0000000..5da2f1b --- /dev/null +++ b/docs/Advent_of_Code_2017_December_6th.rst @@ -0,0 +1,305 @@ + +Advent of Code 2017 +=================== + +December 6th +------------ + +:: + + [0 2 7 0] dup max + +.. code:: ipython2 + + from notebook_preamble import D, J, V, define + +.. code:: ipython2 + + J('[0 2 7 0] dup max') + + +.. parsed-literal:: + + [0 2 7 0] 7 + + +.. code:: ipython2 + + from joy.library import SimpleFunctionWrapper + from joy.utils.stack import list_to_stack + + + @SimpleFunctionWrapper + def index_of(stack): + '''Given a sequence and a item, return the index of the item, or -1 if not found. + + E.g.: + + [a b c] a index_of + ------------------------ + 0 + + [a b c] d index_of + ------------------------ + -1 + + ''' + item, (sequence, stack) = stack + i = 0 + while sequence: + term, sequence = sequence + if term == item: + break + i += 1 + else: + i = -1 + return i, stack + + + D['index_of'] = index_of + +.. code:: ipython2 + + J('[0 2 7 0] 7 index_of') + + +.. parsed-literal:: + + 2 + + +.. code:: ipython2 + + J('[0 2 7 0] 23 index_of') + + +.. parsed-literal:: + + -1 + + +Starting at ``index`` distribute ``count`` "blocks" to the "banks" in +the sequence. + +:: + + [...] count index distribute + ---------------------------- + [...] + +This seems like it would be a PITA to implement in Joypy... + +.. code:: ipython2 + + from joy.utils.stack import iter_stack, list_to_stack + + + @SimpleFunctionWrapper + def distribute(stack): + '''Starting at index+1 distribute count "blocks" to the "banks" in the sequence. + + [...] count index distribute + ---------------------------- + [...] + + ''' + index, (count, (sequence, stack)) = stack + assert count >= 0 + cheat = list(iter_stack(sequence)) + n = len(cheat) + assert index < n + cheat[index] = 0 + while count: + index += 1 + index %= n + cheat[index] += 1 + count -= 1 + return list_to_stack(cheat), stack + + + D['distribute'] = distribute + +.. code:: ipython2 + + J('[0 2 7 0] dup max [index_of] nullary distribute') + + +.. parsed-literal:: + + [2 4 1 2] + + +.. code:: ipython2 + + J('[2 4 1 2] dup max [index_of] nullary distribute') + + +.. parsed-literal:: + + [3 1 2 3] + + +.. code:: ipython2 + + J('[3 1 2 3] dup max [index_of] nullary distribute') + + +.. parsed-literal:: + + [0 2 3 4] + + +.. code:: ipython2 + + J('[0 2 3 4] dup max [index_of] nullary distribute') + + +.. parsed-literal:: + + [1 3 4 1] + + +.. code:: ipython2 + + J('[1 3 4 1] dup max [index_of] nullary distribute') + + +.. parsed-literal:: + + [2 4 1 2] + + +Recalling "Generator Programs" +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + [a F] x + [a F] a F + + [a F] a swap [C] dip rest cons + a [a F] [C] dip rest cons + a C [a F] rest cons + a C [F] cons + + w/ C == dup G + + a dup G [F] cons + a a G [F] cons + + w/ G == dup max [index_of] nullary distribute + +.. code:: ipython2 + + define('direco == dip rest cons') + +.. code:: ipython2 + + define('G == [direco] cons [swap] swoncat cons') + +.. code:: ipython2 + + define('make_distributor == [dup dup max [index_of] nullary distribute] G') + +.. code:: ipython2 + + J('[0 2 7 0] make_distributor 6 [x] times pop') + + +.. parsed-literal:: + + [0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2] + + +A function to drive a generator and count how many states before a repeat. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +First draft: + +:: + + [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + +(?) + +:: + + [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + [] [...] [GEN] pop index_of 0 >= + [] [...] index_of 0 >= + -1 0 >= + False + +Base case + +:: + + [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec + [] [...] [GEN] pop size -- + [] [...] size -- + [] [...] size -- + +A mistake, ``popop`` and no need for ``--`` + +:: + + [] [...] [GEN] popop size + [] size + n + +Recursive case + +:: + + [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec + [] [...] [GEN] [swons] dip x F + [] [...] swons [GEN] x F + [[...]] [GEN] x F + [[...]] [...] [GEN] F + + [[...]] [...] [GEN] F + +What have we learned? + +:: + + F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec + +.. code:: ipython2 + + define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec') + +.. code:: ipython2 + + define('AoC2017.6 == make_distributor count_states') + +.. code:: ipython2 + + J('[0 2 7 0] AoC2017.6') + + +.. parsed-literal:: + + 5 + + +.. code:: ipython2 + + J('[1 1 1] AoC2017.6') + + +.. parsed-literal:: + + 4 + + +.. code:: ipython2 + + J('[8 0 0 0 0 0] AoC2017.6') + + +.. parsed-literal:: + + 15 + diff --git a/docs/Generator_Programs.html b/docs/Generator_Programs.html new file mode 100644 index 0000000..a8d71e6 --- /dev/null +++ b/docs/Generator_Programs.html @@ -0,0 +1,13245 @@ + + + +Generator_Programs + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Using x to Generate Values

Cf. jp-reprod.html

+ +
+
+
+
+
+
In [1]:
+
+
+
from notebook_preamble import J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

Consider the x combinator:

+ +
x == dup i
+ +
+
+
+
+
+
+
+

We can apply it to a quoted program consisting of some value a and some function B:

+ +
[a B] x
+[a B] a B
+ +
+
+
+
+
+
+
+

Let B function swap the a with the quote and run some function C on it to generate a new value b:

+ +
B == swap [C] dip
+
+[a B] a B
+[a B] a swap [C] dip
+a [a B]      [C] dip
+a C [a B]
+b [a B]
+ +
+
+
+
+
+
+
+

Now discard the quoted a with rest then cons b:

+ +
b [a B] rest cons
+b [B]        cons
+[b B]
+ +
+
+
+
+
+
+
+

Altogether, this is the definition of B:

+ +
B == swap [C] dip rest cons
+ +
+
+
+
+
+
+
+

We can make a generator for the Natural numbers (0, 1, 2, ...) by using 0 for a and [dup ++] for [C]:

+ +
[0 swap [dup ++] dip rest cons]
+
+
+

Let's try it:

+ +
+
+
+
+
+
In [2]:
+
+
+
V('[0 swap [dup ++] dip rest cons] x')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                           . [0 swap [dup ++] dip rest cons] x
+           [0 swap [dup ++] dip rest cons] . x
+           [0 swap [dup ++] dip rest cons] . 0 swap [dup ++] dip rest cons
+         [0 swap [dup ++] dip rest cons] 0 . swap [dup ++] dip rest cons
+         0 [0 swap [dup ++] dip rest cons] . [dup ++] dip rest cons
+0 [0 swap [dup ++] dip rest cons] [dup ++] . dip rest cons
+                                         0 . dup ++ [0 swap [dup ++] dip rest cons] rest cons
+                                       0 0 . ++ [0 swap [dup ++] dip rest cons] rest cons
+                                       0 1 . [0 swap [dup ++] dip rest cons] rest cons
+       0 1 [0 swap [dup ++] dip rest cons] . rest cons
+         0 1 [swap [dup ++] dip rest cons] . cons
+         0 [1 swap [dup ++] dip rest cons] . 
+
+
+
+ +
+
+ +
+
+
+
+
+

After one application of x the quoted program contains 1 and 0 is below it on the stack.

+ +
+
+
+
+
+
In [3]:
+
+
+
J('[0 swap [dup ++] dip rest cons] x x x x x pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0 1 2 3 4
+
+
+
+ +
+
+ +
+
+
+
+
+

direco

+
+
+
+
+
+
In [4]:
+
+
+
define('direco == dip rest cons')
+
+ +
+
+
+ +
+
+
+
In [5]:
+
+
+
V('[0 swap [dup ++] direco] x')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                    . [0 swap [dup ++] direco] x
+           [0 swap [dup ++] direco] . x
+           [0 swap [dup ++] direco] . 0 swap [dup ++] direco
+         [0 swap [dup ++] direco] 0 . swap [dup ++] direco
+         0 [0 swap [dup ++] direco] . [dup ++] direco
+0 [0 swap [dup ++] direco] [dup ++] . direco
+0 [0 swap [dup ++] direco] [dup ++] . dip rest cons
+                                  0 . dup ++ [0 swap [dup ++] direco] rest cons
+                                0 0 . ++ [0 swap [dup ++] direco] rest cons
+                                0 1 . [0 swap [dup ++] direco] rest cons
+       0 1 [0 swap [dup ++] direco] . rest cons
+         0 1 [swap [dup ++] direco] . cons
+         0 [1 swap [dup ++] direco] . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Making Generators

We want to define a function that accepts a and [C] and builds our quoted program:

+ +
         a [C] G
+-------------------------
+   [a swap [C] direco]
+ +
+
+
+
+
+
+
+

Working in reverse:

+ +
[a swap   [C] direco] cons
+a [swap   [C] direco] concat
+a [swap] [[C] direco] swap
+a [[C] direco] [swap]
+a [C] [direco] cons [swap]
+
+
+

Reading from the bottom up:

+ +
G == [direco] cons [swap] swap concat cons
+G == [direco] cons [swap] swoncat cons
+ +
+
+
+
+
+
In [6]:
+
+
+
define('G == [direco] cons [swap] swoncat cons')
+
+ +
+
+
+ +
+
+
+
+
+

Let's try it out:

+ +
+
+
+
+
+
In [7]:
+
+
+
J('0 [dup ++] G')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0 swap [dup ++] direco]
+
+
+
+ +
+
+ +
+
+
+
In [8]:
+
+
+
J('0 [dup ++] G x x x pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0 1 2
+
+
+
+ +
+
+ +
+
+
+
+
+

Powers of 2

+
+
+
+
+
+
In [9]:
+
+
+
J('1 [dup 1 <<] G x x x x x x x x x pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 4 8 16 32 64 128 256
+
+
+
+ +
+
+ +
+
+
+
+
+

[x] times

If we have one of these quoted programs we can drive it using times with the x combinator.

+ +
+
+
+
+
+
In [10]:
+
+
+
J('23 [dup ++] G 5 [x] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
23 24 25 26 27 [28 swap [dup ++] direco]
+
+
+
+ +
+
+ +
+
+
+
+
+

Generating Multiples of Three and Five

Look at the treatment of the Project Euler Problem One in the "Developing a Program" notebook and you'll see that we might be interested in generating an endless cycle of:

+ +
3 2 1 3 1 2 3
+
+
+

To do this we want to encode the numbers as pairs of bits in a single int:

+ +
    3  2  1  3  1  2  3
+0b 11 10 01 11 01 10 11 == 14811
+
+
+

And pick them off by masking with 3 (binary 11) and then shifting the int right two bits.

+ +
+
+
+
+
+
In [11]:
+
+
+
define('PE1.1 == dup [3 &] dip 2 >>')
+
+ +
+
+
+ +
+
+
+
In [12]:
+
+
+
V('14811 PE1.1')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                  . 14811 PE1.1
+            14811 . PE1.1
+            14811 . dup [3 &] dip 2 >>
+      14811 14811 . [3 &] dip 2 >>
+14811 14811 [3 &] . dip 2 >>
+            14811 . 3 & 14811 2 >>
+          14811 3 . & 14811 2 >>
+                3 . 14811 2 >>
+          3 14811 . 2 >>
+        3 14811 2 . >>
+           3 3702 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

If we plug 14811 and [PE1.1] into our generator form...

+ +
+
+
+
+
+
In [13]:
+
+
+
J('14811 [PE1.1] G')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[14811 swap [PE1.1] direco]
+
+
+
+ +
+
+ +
+
+
+
+
+

...we get a generator that works for seven cycles before it reaches zero:

+ +
+
+
+
+
+
In [14]:
+
+
+
J('[14811 swap [PE1.1] direco] 7 [x] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 3 1 2 3 [0 swap [PE1.1] direco]
+
+
+
+ +
+
+ +
+
+
+
+
+

Reset at Zero

We need a function that checks if the int has reached zero and resets it if so.

+ +
+
+
+
+
+
In [15]:
+
+
+
define('PE1.1.check == dup [pop 14811] [] branch')
+
+ +
+
+
+ +
+
+
+
In [16]:
+
+
+
J('14811 [PE1.1.check PE1.1] G')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[14811 swap [PE1.1.check PE1.1] direco]
+
+
+
+ +
+
+ +
+
+
+
In [17]:
+
+
+
J('[14811 swap [PE1.1.check PE1.1] direco] 21 [x] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [PE1.1.check PE1.1] direco]
+
+
+
+ +
+
+ +
+
+
+
+
+

(It would be more efficient to reset the int every seven cycles but that's a little beyond the scope of this article. This solution does extra work, but not much, and we're not using it "in production" as they say.)

+ +
+
+
+
+
+
+
+

Run 466 times

In the PE1 problem we are asked to sum all the multiples of three and five less than 1000. It's worked out that we need to use all seven numbers sixty-six times and then four more.

+ +
+
+
+
+
+
In [18]:
+
+
+
J('7 66 * 4 +')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
466
+
+
+
+ +
+
+ +
+
+
+
+
+

If we drive our generator 466 times and sum the stack we get 999.

+ +
+
+
+
+
+
In [19]:
+
+
+
J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 [57 swap [PE1.1.check PE1.1] direco]
+
+
+
+ +
+
+ +
+
+
+
In [20]:
+
+
+
J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times pop enstacken sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
999
+
+
+
+ +
+
+ +
+
+
+
+
+

Project Euler Problem One

+
+
+
+
+
+
In [21]:
+
+
+
define('PE1.2 == + dup [+] dip')
+
+ +
+
+
+ +
+
+
+
+
+

Now we can add PE1.2 to the quoted program given to G.

+ +
+
+
+
+
+
In [22]:
+
+
+
J('0 0 0 [PE1.1.check PE1.1] G 466 [x [PE1.2] dip] times popop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
233168
+
+
+
+ +
+
+ +
+
+
+
+
+

A generator for the Fibonacci Sequence.

Consider:

+ +
[b a F] x
+[b a F] b a F
+ +
+
+
+
+
+
+
+

The obvious first thing to do is just add b and a:

+ +
[b a F] b a +
+[b a F] b+a
+ +
+
+
+
+
+
+
+

From here we want to arrive at:

+ +
b [b+a b F]
+ +
+
+
+
+
+
+
+

Let's start with swons:

+ +
[b a F] b+a swons
+[b+a b a F]
+ +
+
+
+
+
+
+
+

Considering this quote as a stack:

+ +
F a b b+a
+ +
+
+
+
+
+
+
+

We want to get it to:

+ +
F b b+a b
+ +
+
+
+
+
+
+
+

So:

+ +
F a b b+a popdd over
+F b b+a b
+ +
+
+
+
+
+
+
+

And therefore:

+ +
[b+a b a F] [popdd over] infra
+[b b+a b F]
+ +
+
+
+
+
+
+
+

But we can just use cons to carry b+a into the quote:

+ +
[b a F] b+a [popdd over] cons infra
+[b a F] [b+a popdd over]      infra
+[b b+a b F]
+ +
+
+
+
+
+
+
+

Lastly:

+ +
[b b+a b F] uncons
+b [b+a b F]
+ +
+
+
+
+
+
+
+

Putting it all together:

+ +
F == + [popdd over] cons infra uncons
+fib_gen == [1 1 F]
+ +
+
+
+
+
+
In [23]:
+
+
+
define('fib == + [popdd over] cons infra uncons')
+
+ +
+
+
+ +
+
+
+
In [24]:
+
+
+
define('fib_gen == [1 1 fib]')
+
+ +
+
+
+ +
+
+
+
In [25]:
+
+
+
J('fib_gen 10 [x] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3 5 8 13 21 34 55 89 [144 89 fib]
+
+
+
+ +
+
+ +
+
+
+
+
+

Project Euler Problem Two

+
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
+find the sum of the even-valued terms.
+
+
+

Now that we have a generator for the Fibonacci sequence, we need a function that adds a term in the sequence to a sum if it is even, and pops it otherwise.

+ +
+
+
+
+
+
In [26]:
+
+
+
define('PE2.1 == dup 2 % [+] [pop] branch')
+
+ +
+
+
+ +
+
+
+
+
+

And a predicate function that detects when the terms in the series "exceed four million".

+ +
+
+
+
+
+
In [27]:
+
+
+
define('>4M == 4000000 >')
+
+ +
+
+
+ +
+
+
+
+
+

Now it's straightforward to define PE2 as a recursive function that generates terms in the Fibonacci sequence until they exceed four million and sums the even ones.

+ +
+
+
+
+
+
In [28]:
+
+
+
define('PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec')
+
+ +
+
+
+ +
+
+
+
In [29]:
+
+
+
J('PE2')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4613732
+
+
+
+ +
+
+ +
+
+
+
+
+

Here's the collected program definitions:

+ +
fib == + swons [popdd over] infra uncons
+fib_gen == [1 1 fib]
+
+even == dup 2 %
+>4M == 4000000 >
+
+PE2.1 == even [+] [pop] branch
+PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec
+ +
+
+
+
+
+
+
+

Even-valued Fibonacci Terms

Using o for odd and e for even:

+ +
o + o = e
+e + e = e
+o + e = o
+
+
+

So the Fibonacci sequence considered in terms of just parity would be:

+ +
o o e o o e o o e o o e o o e o o e
+1 1 2 3 5 8 . . .
+
+
+

Every third term is even.

+ +
+
+
+
+
+
In [30]:
+
+
+
J('[1 0 fib] x x x')  # To start the sequence with 1 1 2 3 instead of 1 2 3.
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 1 2 [3 2 fib]
+
+
+
+ +
+
+ +
+
+
+
+
+

Drive the generator three times and popop the two odd terms.

+ +
+
+
+
+
+
In [31]:
+
+
+
J('[1 0 fib] x x x [popop] dipd')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2 [3 2 fib]
+
+
+
+ +
+
+ +
+
+
+
In [32]:
+
+
+
define('PE2.2 == x x x [popop] dipd')
+
+ +
+
+
+ +
+
+
+
In [33]:
+
+
+
J('[1 0 fib] 10 [PE2.2] times')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2 8 34 144 610 2584 10946 46368 196418 832040 [1346269 832040 fib]
+
+
+
+ +
+
+ +
+
+
+
+
+

Replace x with our new driver function PE2.2 and start our fib generator at 1 0.

+ +
+
+
+
+
+
In [34]:
+
+
+
J('0 [1 0 fib] PE2.2 [pop >4M] [popop] [[PE2.1] dip PE2.2] primrec')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
4613732
+
+
+
+ +
+
+ +
+
+
+
+
+

How to compile these?

You would probably start with a special version of G, and perhaps modifications to the default x?

+ +
+
+
+
+
+
+
+

An Interesting Variation

+
+
+
+
+
+
In [35]:
+
+
+
define('codireco == cons dip rest cons')
+
+ +
+
+
+ +
+
+
+
In [36]:
+
+
+
V('[0 [dup ++] codireco] x')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                 . [0 [dup ++] codireco] x
+           [0 [dup ++] codireco] . x
+           [0 [dup ++] codireco] . 0 [dup ++] codireco
+         [0 [dup ++] codireco] 0 . [dup ++] codireco
+[0 [dup ++] codireco] 0 [dup ++] . codireco
+[0 [dup ++] codireco] 0 [dup ++] . cons dip rest cons
+[0 [dup ++] codireco] [0 dup ++] . dip rest cons
+                                 . 0 dup ++ [0 [dup ++] codireco] rest cons
+                               0 . dup ++ [0 [dup ++] codireco] rest cons
+                             0 0 . ++ [0 [dup ++] codireco] rest cons
+                             0 1 . [0 [dup ++] codireco] rest cons
+       0 1 [0 [dup ++] codireco] . rest cons
+         0 1 [[dup ++] codireco] . cons
+         0 [1 [dup ++] codireco] . 
+
+
+
+ +
+
+ +
+
+
+
In [37]:
+
+
+
define('G == [codireco] cons cons')
+
+ +
+
+
+ +
+
+
+
In [38]:
+
+
+
J('230 [dup ++] G 5 [x] times pop')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
230 231 232 233 234
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/Generator_Programs.ipynb b/docs/Generator_Programs.ipynb new file mode 100644 index 0000000..dcb3f61 --- /dev/null +++ b/docs/Generator_Programs.ipynb @@ -0,0 +1,1027 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Using `x` to Generate Values\n", + "\n", + "Cf. jp-reprod.html" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider the `x` combinator:\n", + "\n", + " x == dup i" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can apply it to a quoted program consisting of some value `a` and some function `B`:\n", + "\n", + " [a B] x\n", + " [a B] a B" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let `B` function `swap` the `a` with the quote and run some function `C` on it to generate a new value `b`:\n", + "\n", + " B == swap [C] dip\n", + "\n", + " [a B] a B\n", + " [a B] a swap [C] dip\n", + " a [a B] [C] dip\n", + " a C [a B]\n", + " b [a B]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now discard the quoted `a` with `rest` then `cons` `b`:\n", + "\n", + " b [a B] rest cons\n", + " b [B] cons\n", + " [b B]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Altogether, this is the definition of `B`:\n", + "\n", + " B == swap [C] dip rest cons" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can make a generator for the Natural numbers (0, 1, 2, ...) by using `0` for `a` and `[dup ++]` for `[C]`:\n", + "\n", + " [0 swap [dup ++] dip rest cons]\n", + "\n", + "Let's try it:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [0 swap [dup ++] dip rest cons] x\n", + " [0 swap [dup ++] dip rest cons] . x\n", + " [0 swap [dup ++] dip rest cons] . 0 swap [dup ++] dip rest cons\n", + " [0 swap [dup ++] dip rest cons] 0 . swap [dup ++] dip rest cons\n", + " 0 [0 swap [dup ++] dip rest cons] . [dup ++] dip rest cons\n", + "0 [0 swap [dup ++] dip rest cons] [dup ++] . dip rest cons\n", + " 0 . dup ++ [0 swap [dup ++] dip rest cons] rest cons\n", + " 0 0 . ++ [0 swap [dup ++] dip rest cons] rest cons\n", + " 0 1 . [0 swap [dup ++] dip rest cons] rest cons\n", + " 0 1 [0 swap [dup ++] dip rest cons] . rest cons\n", + " 0 1 [swap [dup ++] dip rest cons] . cons\n", + " 0 [1 swap [dup ++] dip rest cons] . \n" + ] + } + ], + "source": [ + "V('[0 swap [dup ++] dip rest cons] x')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "After one application of `x` the quoted program contains `1` and `0` is below it on the stack." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 1 2 3 4\n" + ] + } + ], + "source": [ + "J('[0 swap [dup ++] dip rest cons] x x x x x pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## `direco`" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "define('direco == dip rest cons')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [0 swap [dup ++] direco] x\n", + " [0 swap [dup ++] direco] . x\n", + " [0 swap [dup ++] direco] . 0 swap [dup ++] direco\n", + " [0 swap [dup ++] direco] 0 . swap [dup ++] direco\n", + " 0 [0 swap [dup ++] direco] . [dup ++] direco\n", + "0 [0 swap [dup ++] direco] [dup ++] . direco\n", + "0 [0 swap [dup ++] direco] [dup ++] . dip rest cons\n", + " 0 . dup ++ [0 swap [dup ++] direco] rest cons\n", + " 0 0 . ++ [0 swap [dup ++] direco] rest cons\n", + " 0 1 . [0 swap [dup ++] direco] rest cons\n", + " 0 1 [0 swap [dup ++] direco] . rest cons\n", + " 0 1 [swap [dup ++] direco] . cons\n", + " 0 [1 swap [dup ++] direco] . \n" + ] + } + ], + "source": [ + "V('[0 swap [dup ++] direco] x')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Making Generators\n", + "We want to define a function that accepts `a` and `[C]` and builds our quoted program:\n", + "\n", + " a [C] G\n", + " -------------------------\n", + " [a swap [C] direco]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Working in reverse:\n", + "\n", + " [a swap [C] direco] cons\n", + " a [swap [C] direco] concat\n", + " a [swap] [[C] direco] swap\n", + " a [[C] direco] [swap]\n", + " a [C] [direco] cons [swap]\n", + "\n", + "Reading from the bottom up:\n", + "\n", + " G == [direco] cons [swap] swap concat cons\n", + " G == [direco] cons [swap] swoncat cons" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "define('G == [direco] cons [swap] swoncat cons')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try it out:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 swap [dup ++] direco]\n" + ] + } + ], + "source": [ + "J('0 [dup ++] G')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 1 2\n" + ] + } + ], + "source": [ + "J('0 [dup ++] G x x x pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Powers of 2" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 4 8 16 32 64 128 256\n" + ] + } + ], + "source": [ + "J('1 [dup 1 <<] G x x x x x x x x x pop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `[x] times`\n", + "If we have one of these quoted programs we can drive it using `times` with the `x` combinator." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 24 25 26 27 [28 swap [dup ++] direco]\n" + ] + } + ], + "source": [ + "J('23 [dup ++] G 5 [x] times')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Generating Multiples of Three and Five\n", + "Look at the treatment of the Project Euler Problem One in the \"Developing a Program\" notebook and you'll see that we might be interested in generating an endless cycle of:\n", + "\n", + " 3 2 1 3 1 2 3\n", + "\n", + "To do this we want to encode the numbers as pairs of bits in a single int:\n", + "\n", + " 3 2 1 3 1 2 3\n", + " 0b 11 10 01 11 01 10 11 == 14811\n", + "\n", + "And pick them off by masking with 3 (binary 11) and then shifting the int right two bits." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.1 == dup [3 &] dip 2 >>')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 14811 PE1.1\n", + " 14811 . PE1.1\n", + " 14811 . dup [3 &] dip 2 >>\n", + " 14811 14811 . [3 &] dip 2 >>\n", + "14811 14811 [3 &] . dip 2 >>\n", + " 14811 . 3 & 14811 2 >>\n", + " 14811 3 . & 14811 2 >>\n", + " 3 . 14811 2 >>\n", + " 3 14811 . 2 >>\n", + " 3 14811 2 . >>\n", + " 3 3702 . \n" + ] + } + ], + "source": [ + "V('14811 PE1.1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we plug `14811` and `[PE1.1]` into our generator form..." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[14811 swap [PE1.1] direco]\n" + ] + } + ], + "source": [ + "J('14811 [PE1.1] G')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...we get a generator that works for seven cycles before it reaches zero:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 [0 swap [PE1.1] direco]\n" + ] + } + ], + "source": [ + "J('[14811 swap [PE1.1] direco] 7 [x] times')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Reset at Zero\n", + "We need a function that checks if the int has reached zero and resets it if so." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.1.check == dup [pop 14811] [] branch')" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[14811 swap [PE1.1.check PE1.1] direco]\n" + ] + } + ], + "source": [ + "J('14811 [PE1.1.check PE1.1] G')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [PE1.1.check PE1.1] direco]\n" + ] + } + ], + "source": [ + "J('[14811 swap [PE1.1.check PE1.1] direco] 21 [x] times')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "(It would be more efficient to reset the int every seven cycles but that's a little beyond the scope of this article. This solution does extra work, but not much, and we're not using it \"in production\" as they say.)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run 466 times\n", + "In the PE1 problem we are asked to sum all the multiples of three and five less than 1000. It's worked out that we need to use all seven numbers sixty-six times and then four more." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "466\n" + ] + } + ], + "source": [ + "J('7 66 * 4 +')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we drive our generator 466 times and sum the stack we get 999." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 [57 swap [PE1.1.check PE1.1] direco]\n" + ] + } + ], + "source": [ + "J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times')" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "999\n" + ] + } + ], + "source": [ + "J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times pop enstacken sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Project Euler Problem One" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE1.2 == + dup [+] dip')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can add `PE1.2` to the quoted program given to `G`." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168\n" + ] + } + ], + "source": [ + "J('0 0 0 [PE1.1.check PE1.1] G 466 [x [PE1.2] dip] times popop')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A generator for the Fibonacci Sequence.\n", + "Consider:\n", + "\n", + " [b a F] x\n", + " [b a F] b a F" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The obvious first thing to do is just add `b` and `a`:\n", + "\n", + " [b a F] b a +\n", + " [b a F] b+a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "From here we want to arrive at:\n", + "\n", + " b [b+a b F]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's start with `swons`:\n", + "\n", + " [b a F] b+a swons\n", + " [b+a b a F]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Considering this quote as a stack:\n", + "\n", + " F a b b+a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We want to get it to:\n", + "\n", + " F b b+a b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So:\n", + "\n", + " F a b b+a popdd over\n", + " F b b+a b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And therefore:\n", + "\n", + " [b+a b a F] [popdd over] infra\n", + " [b b+a b F]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "But we can just use `cons` to carry `b+a` into the quote:\n", + "\n", + " [b a F] b+a [popdd over] cons infra\n", + " [b a F] [b+a popdd over] infra\n", + " [b b+a b F]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lastly:\n", + "\n", + " [b b+a b F] uncons\n", + " b [b+a b F]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Putting it all together:\n", + "\n", + " F == + [popdd over] cons infra uncons\n", + " fib_gen == [1 1 F]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "define('fib == + [popdd over] cons infra uncons')" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "define('fib_gen == [1 1 fib]')" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 5 8 13 21 34 55 89 [144 89 fib]\n" + ] + } + ], + "source": [ + "J('fib_gen 10 [x] times')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Project Euler Problem Two\n", + " By considering the terms in the Fibonacci sequence whose values do not exceed four million,\n", + " find the sum of the even-valued terms.\n", + "\n", + "Now that we have a generator for the Fibonacci sequence, we need a function that adds a term in the sequence to a sum if it is even, and `pop`s it otherwise." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE2.1 == dup 2 % [+] [pop] branch')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And a predicate function that detects when the terms in the series \"exceed four million\"." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "define('>4M == 4000000 >')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now it's straightforward to define `PE2` as a recursive function that generates terms in the Fibonacci sequence until they exceed four million and sums the even ones." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4613732\n" + ] + } + ], + "source": [ + "J('PE2')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here's the collected program definitions:\n", + "\n", + " fib == + swons [popdd over] infra uncons\n", + " fib_gen == [1 1 fib]\n", + "\n", + " even == dup 2 %\n", + " >4M == 4000000 >\n", + "\n", + " PE2.1 == even [+] [pop] branch\n", + " PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Even-valued Fibonacci Terms\n", + "\n", + "Using `o` for odd and `e` for even:\n", + "\n", + " o + o = e\n", + " e + e = e\n", + " o + e = o\n", + "\n", + "So the Fibonacci sequence considered in terms of just parity would be:\n", + "\n", + " o o e o o e o o e o o e o o e o o e\n", + " 1 1 2 3 5 8 . . .\n", + "\n", + "Every third term is even.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 1 2 [3 2 fib]\n" + ] + } + ], + "source": [ + "J('[1 0 fib] x x x') # To start the sequence with 1 1 2 3 instead of 1 2 3." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Drive the generator three times and `popop` the two odd terms." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 [3 2 fib]\n" + ] + } + ], + "source": [ + "J('[1 0 fib] x x x [popop] dipd')" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "define('PE2.2 == x x x [popop] dipd')" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 8 34 144 610 2584 10946 46368 196418 832040 [1346269 832040 fib]\n" + ] + } + ], + "source": [ + "J('[1 0 fib] 10 [PE2.2] times')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Replace `x` with our new driver function `PE2.2` and start our `fib` generator at `1 0`." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4613732\n" + ] + } + ], + "source": [ + "J('0 [1 0 fib] PE2.2 [pop >4M] [popop] [[PE2.1] dip PE2.2] primrec')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## How to compile these?\n", + "You would probably start with a special version of `G`, and perhaps modifications to the default `x`?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## An Interesting Variation" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "define('codireco == cons dip rest cons')" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [0 [dup ++] codireco] x\n", + " [0 [dup ++] codireco] . x\n", + " [0 [dup ++] codireco] . 0 [dup ++] codireco\n", + " [0 [dup ++] codireco] 0 . [dup ++] codireco\n", + "[0 [dup ++] codireco] 0 [dup ++] . codireco\n", + "[0 [dup ++] codireco] 0 [dup ++] . cons dip rest cons\n", + "[0 [dup ++] codireco] [0 dup ++] . dip rest cons\n", + " . 0 dup ++ [0 [dup ++] codireco] rest cons\n", + " 0 . dup ++ [0 [dup ++] codireco] rest cons\n", + " 0 0 . ++ [0 [dup ++] codireco] rest cons\n", + " 0 1 . [0 [dup ++] codireco] rest cons\n", + " 0 1 [0 [dup ++] codireco] . rest cons\n", + " 0 1 [[dup ++] codireco] . cons\n", + " 0 [1 [dup ++] codireco] . \n" + ] + } + ], + "source": [ + "V('[0 [dup ++] codireco] x')" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "define('G == [codireco] cons cons')" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "230 231 232 233 234\n" + ] + } + ], + "source": [ + "J('230 [dup ++] G 5 [x] times pop')" + ] + } + ], + "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 +} diff --git a/docs/Generator_Programs.md b/docs/Generator_Programs.md new file mode 100644 index 0000000..b10028c --- /dev/null +++ b/docs/Generator_Programs.md @@ -0,0 +1,508 @@ + +# Using `x` to Generate Values + +Cf. jp-reprod.html + + +```python +from notebook_preamble import J, V, define +``` + +Consider the `x` combinator: + + x == dup i + +We can apply it to a quoted program consisting of some value `a` and some function `B`: + + [a B] x + [a B] a B + +Let `B` function `swap` the `a` with the quote and run some function `C` on it to generate a new value `b`: + + B == swap [C] dip + + [a B] a B + [a B] a swap [C] dip + a [a B] [C] dip + a C [a B] + b [a B] + +Now discard the quoted `a` with `rest` then `cons` `b`: + + b [a B] rest cons + b [B] cons + [b B] + +Altogether, this is the definition of `B`: + + B == swap [C] dip rest cons + +We can make a generator for the Natural numbers (0, 1, 2, ...) by using `0` for `a` and `[dup ++]` for `[C]`: + + [0 swap [dup ++] dip rest cons] + +Let's try it: + + +```python +V('[0 swap [dup ++] dip rest cons] x') +``` + + . [0 swap [dup ++] dip rest cons] x + [0 swap [dup ++] dip rest cons] . x + [0 swap [dup ++] dip rest cons] . 0 swap [dup ++] dip rest cons + [0 swap [dup ++] dip rest cons] 0 . swap [dup ++] dip rest cons + 0 [0 swap [dup ++] dip rest cons] . [dup ++] dip rest cons + 0 [0 swap [dup ++] dip rest cons] [dup ++] . dip rest cons + 0 . dup ++ [0 swap [dup ++] dip rest cons] rest cons + 0 0 . ++ [0 swap [dup ++] dip rest cons] rest cons + 0 1 . [0 swap [dup ++] dip rest cons] rest cons + 0 1 [0 swap [dup ++] dip rest cons] . rest cons + 0 1 [swap [dup ++] dip rest cons] . cons + 0 [1 swap [dup ++] dip rest cons] . + + +After one application of `x` the quoted program contains `1` and `0` is below it on the stack. + + +```python +J('[0 swap [dup ++] dip rest cons] x x x x x pop') +``` + + 0 1 2 3 4 + + +## `direco` + + +```python +define('direco == dip rest cons') +``` + + +```python +V('[0 swap [dup ++] direco] x') +``` + + . [0 swap [dup ++] direco] x + [0 swap [dup ++] direco] . x + [0 swap [dup ++] direco] . 0 swap [dup ++] direco + [0 swap [dup ++] direco] 0 . swap [dup ++] direco + 0 [0 swap [dup ++] direco] . [dup ++] direco + 0 [0 swap [dup ++] direco] [dup ++] . direco + 0 [0 swap [dup ++] direco] [dup ++] . dip rest cons + 0 . dup ++ [0 swap [dup ++] direco] rest cons + 0 0 . ++ [0 swap [dup ++] direco] rest cons + 0 1 . [0 swap [dup ++] direco] rest cons + 0 1 [0 swap [dup ++] direco] . rest cons + 0 1 [swap [dup ++] direco] . cons + 0 [1 swap [dup ++] direco] . + + +## Making Generators +We want to define a function that accepts `a` and `[C]` and builds our quoted program: + + a [C] G + ------------------------- + [a swap [C] direco] + +Working in reverse: + + [a swap [C] direco] cons + a [swap [C] direco] concat + a [swap] [[C] direco] swap + a [[C] direco] [swap] + a [C] [direco] cons [swap] + +Reading from the bottom up: + + G == [direco] cons [swap] swap concat cons + G == [direco] cons [swap] swoncat cons + + +```python +define('G == [direco] cons [swap] swoncat cons') +``` + +Let's try it out: + + +```python +J('0 [dup ++] G') +``` + + [0 swap [dup ++] direco] + + + +```python +J('0 [dup ++] G x x x pop') +``` + + 0 1 2 + + +### Powers of 2 + + +```python +J('1 [dup 1 <<] G x x x x x x x x x pop') +``` + + 1 2 4 8 16 32 64 128 256 + + +### `[x] times` +If we have one of these quoted programs we can drive it using `times` with the `x` combinator. + + +```python +J('23 [dup ++] G 5 [x] times') +``` + + 23 24 25 26 27 [28 swap [dup ++] direco] + + +## Generating Multiples of Three and Five +Look at the treatment of the Project Euler Problem One in the "Developing a Program" notebook and you'll see that we might be interested in generating an endless cycle of: + + 3 2 1 3 1 2 3 + +To do this we want to encode the numbers as pairs of bits in a single int: + + 3 2 1 3 1 2 3 + 0b 11 10 01 11 01 10 11 == 14811 + +And pick them off by masking with 3 (binary 11) and then shifting the int right two bits. + + +```python +define('PE1.1 == dup [3 &] dip 2 >>') +``` + + +```python +V('14811 PE1.1') +``` + + . 14811 PE1.1 + 14811 . PE1.1 + 14811 . dup [3 &] dip 2 >> + 14811 14811 . [3 &] dip 2 >> + 14811 14811 [3 &] . dip 2 >> + 14811 . 3 & 14811 2 >> + 14811 3 . & 14811 2 >> + 3 . 14811 2 >> + 3 14811 . 2 >> + 3 14811 2 . >> + 3 3702 . + + +If we plug `14811` and `[PE1.1]` into our generator form... + + +```python +J('14811 [PE1.1] G') +``` + + [14811 swap [PE1.1] direco] + + +...we get a generator that works for seven cycles before it reaches zero: + + +```python +J('[14811 swap [PE1.1] direco] 7 [x] times') +``` + + 3 2 1 3 1 2 3 [0 swap [PE1.1] direco] + + +### Reset at Zero +We need a function that checks if the int has reached zero and resets it if so. + + +```python +define('PE1.1.check == dup [pop 14811] [] branch') +``` + + +```python +J('14811 [PE1.1.check PE1.1] G') +``` + + [14811 swap [PE1.1.check PE1.1] direco] + + + +```python +J('[14811 swap [PE1.1.check PE1.1] direco] 21 [x] times') +``` + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [PE1.1.check PE1.1] direco] + + +(It would be more efficient to reset the int every seven cycles but that's a little beyond the scope of this article. This solution does extra work, but not much, and we're not using it "in production" as they say.) + +### Run 466 times +In the PE1 problem we are asked to sum all the multiples of three and five less than 1000. It's worked out that we need to use all seven numbers sixty-six times and then four more. + + +```python +J('7 66 * 4 +') +``` + + 466 + + +If we drive our generator 466 times and sum the stack we get 999. + + +```python +J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times') +``` + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 [57 swap [PE1.1.check PE1.1] direco] + + + +```python +J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times pop enstacken sum') +``` + + 999 + + +## Project Euler Problem One + + +```python +define('PE1.2 == + dup [+] dip') +``` + +Now we can add `PE1.2` to the quoted program given to `G`. + + +```python +J('0 0 0 [PE1.1.check PE1.1] G 466 [x [PE1.2] dip] times popop') +``` + + 233168 + + +## A generator for the Fibonacci Sequence. +Consider: + + [b a F] x + [b a F] b a F + +The obvious first thing to do is just add `b` and `a`: + + [b a F] b a + + [b a F] b+a + +From here we want to arrive at: + + b [b+a b F] + +Let's start with `swons`: + + [b a F] b+a swons + [b+a b a F] + +Considering this quote as a stack: + + F a b b+a + +We want to get it to: + + F b b+a b + +So: + + F a b b+a popdd over + F b b+a b + +And therefore: + + [b+a b a F] [popdd over] infra + [b b+a b F] + +But we can just use `cons` to carry `b+a` into the quote: + + [b a F] b+a [popdd over] cons infra + [b a F] [b+a popdd over] infra + [b b+a b F] + +Lastly: + + [b b+a b F] uncons + b [b+a b F] + +Putting it all together: + + F == + [popdd over] cons infra uncons + fib_gen == [1 1 F] + + +```python +define('fib == + [popdd over] cons infra uncons') +``` + + +```python +define('fib_gen == [1 1 fib]') +``` + + +```python +J('fib_gen 10 [x] times') +``` + + 1 2 3 5 8 13 21 34 55 89 [144 89 fib] + + +## Project Euler Problem Two + By considering the terms in the Fibonacci sequence whose values do not exceed four million, + find the sum of the even-valued terms. + +Now that we have a generator for the Fibonacci sequence, we need a function that adds a term in the sequence to a sum if it is even, and `pop`s it otherwise. + + +```python +define('PE2.1 == dup 2 % [+] [pop] branch') +``` + +And a predicate function that detects when the terms in the series "exceed four million". + + +```python +define('>4M == 4000000 >') +``` + +Now it's straightforward to define `PE2` as a recursive function that generates terms in the Fibonacci sequence until they exceed four million and sums the even ones. + + +```python +define('PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec') +``` + + +```python +J('PE2') +``` + + 4613732 + + +Here's the collected program definitions: + + fib == + swons [popdd over] infra uncons + fib_gen == [1 1 fib] + + even == dup 2 % + >4M == 4000000 > + + PE2.1 == even [+] [pop] branch + PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec + +### Even-valued Fibonacci Terms + +Using `o` for odd and `e` for even: + + o + o = e + e + e = e + o + e = o + +So the Fibonacci sequence considered in terms of just parity would be: + + o o e o o e o o e o o e o o e o o e + 1 1 2 3 5 8 . . . + +Every third term is even. + + + +```python +J('[1 0 fib] x x x') # To start the sequence with 1 1 2 3 instead of 1 2 3. +``` + + 1 1 2 [3 2 fib] + + +Drive the generator three times and `popop` the two odd terms. + + +```python +J('[1 0 fib] x x x [popop] dipd') +``` + + 2 [3 2 fib] + + + +```python +define('PE2.2 == x x x [popop] dipd') +``` + + +```python +J('[1 0 fib] 10 [PE2.2] times') +``` + + 2 8 34 144 610 2584 10946 46368 196418 832040 [1346269 832040 fib] + + +Replace `x` with our new driver function `PE2.2` and start our `fib` generator at `1 0`. + + +```python +J('0 [1 0 fib] PE2.2 [pop >4M] [popop] [[PE2.1] dip PE2.2] primrec') +``` + + 4613732 + + +## How to compile these? +You would probably start with a special version of `G`, and perhaps modifications to the default `x`? + +## An Interesting Variation + + +```python +define('codireco == cons dip rest cons') +``` + + +```python +V('[0 [dup ++] codireco] x') +``` + + . [0 [dup ++] codireco] x + [0 [dup ++] codireco] . x + [0 [dup ++] codireco] . 0 [dup ++] codireco + [0 [dup ++] codireco] 0 . [dup ++] codireco + [0 [dup ++] codireco] 0 [dup ++] . codireco + [0 [dup ++] codireco] 0 [dup ++] . cons dip rest cons + [0 [dup ++] codireco] [0 dup ++] . dip rest cons + . 0 dup ++ [0 [dup ++] codireco] rest cons + 0 . dup ++ [0 [dup ++] codireco] rest cons + 0 0 . ++ [0 [dup ++] codireco] rest cons + 0 1 . [0 [dup ++] codireco] rest cons + 0 1 [0 [dup ++] codireco] . rest cons + 0 1 [[dup ++] codireco] . cons + 0 [1 [dup ++] codireco] . + + + +```python +define('G == [codireco] cons cons') +``` + + +```python +J('230 [dup ++] G 5 [x] times pop') +``` + + 230 231 232 233 234 + diff --git a/docs/Generator_Programs.rst b/docs/Generator_Programs.rst new file mode 100644 index 0000000..a201c0d --- /dev/null +++ b/docs/Generator_Programs.rst @@ -0,0 +1,639 @@ + +Using ``x`` to Generate Values +============================== + +Cf. jp-reprod.html + +.. code:: ipython2 + + from notebook_preamble import J, V, define + +Consider the ``x`` combinator: + +:: + + x == dup i + +We can apply it to a quoted program consisting of some value ``a`` and +some function ``B``: + +:: + + [a B] x + [a B] a B + +Let ``B`` function ``swap`` the ``a`` with the quote and run some +function ``C`` on it to generate a new value ``b``: + +:: + + B == swap [C] dip + + [a B] a B + [a B] a swap [C] dip + a [a B] [C] dip + a C [a B] + b [a B] + +Now discard the quoted ``a`` with ``rest`` then ``cons`` ``b``: + +:: + + b [a B] rest cons + b [B] cons + [b B] + +Altogether, this is the definition of ``B``: + +:: + + B == swap [C] dip rest cons + +We can make a generator for the Natural numbers (0, 1, 2, ...) by using +``0`` for ``a`` and ``[dup ++]`` for ``[C]``: + +:: + + [0 swap [dup ++] dip rest cons] + +Let's try it: + +.. code:: ipython2 + + V('[0 swap [dup ++] dip rest cons] x') + + +.. parsed-literal:: + + . [0 swap [dup ++] dip rest cons] x + [0 swap [dup ++] dip rest cons] . x + [0 swap [dup ++] dip rest cons] . 0 swap [dup ++] dip rest cons + [0 swap [dup ++] dip rest cons] 0 . swap [dup ++] dip rest cons + 0 [0 swap [dup ++] dip rest cons] . [dup ++] dip rest cons + 0 [0 swap [dup ++] dip rest cons] [dup ++] . dip rest cons + 0 . dup ++ [0 swap [dup ++] dip rest cons] rest cons + 0 0 . ++ [0 swap [dup ++] dip rest cons] rest cons + 0 1 . [0 swap [dup ++] dip rest cons] rest cons + 0 1 [0 swap [dup ++] dip rest cons] . rest cons + 0 1 [swap [dup ++] dip rest cons] . cons + 0 [1 swap [dup ++] dip rest cons] . + + +After one application of ``x`` the quoted program contains ``1`` and +``0`` is below it on the stack. + +.. code:: ipython2 + + J('[0 swap [dup ++] dip rest cons] x x x x x pop') + + +.. parsed-literal:: + + 0 1 2 3 4 + + +``direco`` +---------- + +.. code:: ipython2 + + define('direco == dip rest cons') + +.. code:: ipython2 + + V('[0 swap [dup ++] direco] x') + + +.. parsed-literal:: + + . [0 swap [dup ++] direco] x + [0 swap [dup ++] direco] . x + [0 swap [dup ++] direco] . 0 swap [dup ++] direco + [0 swap [dup ++] direco] 0 . swap [dup ++] direco + 0 [0 swap [dup ++] direco] . [dup ++] direco + 0 [0 swap [dup ++] direco] [dup ++] . direco + 0 [0 swap [dup ++] direco] [dup ++] . dip rest cons + 0 . dup ++ [0 swap [dup ++] direco] rest cons + 0 0 . ++ [0 swap [dup ++] direco] rest cons + 0 1 . [0 swap [dup ++] direco] rest cons + 0 1 [0 swap [dup ++] direco] . rest cons + 0 1 [swap [dup ++] direco] . cons + 0 [1 swap [dup ++] direco] . + + +Making Generators +----------------- + +We want to define a function that accepts ``a`` and ``[C]`` and builds +our quoted program: + +:: + + a [C] G + ------------------------- + [a swap [C] direco] + +Working in reverse: + +:: + + [a swap [C] direco] cons + a [swap [C] direco] concat + a [swap] [[C] direco] swap + a [[C] direco] [swap] + a [C] [direco] cons [swap] + +Reading from the bottom up: + +:: + + G == [direco] cons [swap] swap concat cons + G == [direco] cons [swap] swoncat cons + +.. code:: ipython2 + + define('G == [direco] cons [swap] swoncat cons') + +Let's try it out: + +.. code:: ipython2 + + J('0 [dup ++] G') + + +.. parsed-literal:: + + [0 swap [dup ++] direco] + + +.. code:: ipython2 + + J('0 [dup ++] G x x x pop') + + +.. parsed-literal:: + + 0 1 2 + + +Powers of 2 +~~~~~~~~~~~ + +.. code:: ipython2 + + J('1 [dup 1 <<] G x x x x x x x x x pop') + + +.. parsed-literal:: + + 1 2 4 8 16 32 64 128 256 + + +``[x] times`` +~~~~~~~~~~~~~ + +If we have one of these quoted programs we can drive it using ``times`` +with the ``x`` combinator. + +.. code:: ipython2 + + J('23 [dup ++] G 5 [x] times') + + +.. parsed-literal:: + + 23 24 25 26 27 [28 swap [dup ++] direco] + + +Generating Multiples of Three and Five +-------------------------------------- + +Look at the treatment of the Project Euler Problem One in the +"Developing a Program" notebook and you'll see that we might be +interested in generating an endless cycle of: + +:: + + 3 2 1 3 1 2 3 + +To do this we want to encode the numbers as pairs of bits in a single +int: + +:: + + 3 2 1 3 1 2 3 + 0b 11 10 01 11 01 10 11 == 14811 + +And pick them off by masking with 3 (binary 11) and then shifting the +int right two bits. + +.. code:: ipython2 + + define('PE1.1 == dup [3 &] dip 2 >>') + +.. code:: ipython2 + + V('14811 PE1.1') + + +.. parsed-literal:: + + . 14811 PE1.1 + 14811 . PE1.1 + 14811 . dup [3 &] dip 2 >> + 14811 14811 . [3 &] dip 2 >> + 14811 14811 [3 &] . dip 2 >> + 14811 . 3 & 14811 2 >> + 14811 3 . & 14811 2 >> + 3 . 14811 2 >> + 3 14811 . 2 >> + 3 14811 2 . >> + 3 3702 . + + +If we plug ``14811`` and ``[PE1.1]`` into our generator form... + +.. code:: ipython2 + + J('14811 [PE1.1] G') + + +.. parsed-literal:: + + [14811 swap [PE1.1] direco] + + +...we get a generator that works for seven cycles before it reaches +zero: + +.. code:: ipython2 + + J('[14811 swap [PE1.1] direco] 7 [x] times') + + +.. parsed-literal:: + + 3 2 1 3 1 2 3 [0 swap [PE1.1] direco] + + +Reset at Zero +~~~~~~~~~~~~~ + +We need a function that checks if the int has reached zero and resets it +if so. + +.. code:: ipython2 + + define('PE1.1.check == dup [pop 14811] [] branch') + +.. code:: ipython2 + + J('14811 [PE1.1.check PE1.1] G') + + +.. parsed-literal:: + + [14811 swap [PE1.1.check PE1.1] direco] + + +.. code:: ipython2 + + J('[14811 swap [PE1.1.check PE1.1] direco] 21 [x] times') + + +.. parsed-literal:: + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [PE1.1.check PE1.1] direco] + + +(It would be more efficient to reset the int every seven cycles but +that's a little beyond the scope of this article. This solution does +extra work, but not much, and we're not using it "in production" as they +say.) + +Run 466 times +~~~~~~~~~~~~~ + +In the PE1 problem we are asked to sum all the multiples of three and +five less than 1000. It's worked out that we need to use all seven +numbers sixty-six times and then four more. + +.. code:: ipython2 + + J('7 66 * 4 +') + + +.. parsed-literal:: + + 466 + + +If we drive our generator 466 times and sum the stack we get 999. + +.. code:: ipython2 + + J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times') + + +.. parsed-literal:: + + 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 [57 swap [PE1.1.check PE1.1] direco] + + +.. code:: ipython2 + + J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times pop enstacken sum') + + +.. parsed-literal:: + + 999 + + +Project Euler Problem One +------------------------- + +.. code:: ipython2 + + define('PE1.2 == + dup [+] dip') + +Now we can add ``PE1.2`` to the quoted program given to ``G``. + +.. code:: ipython2 + + J('0 0 0 [PE1.1.check PE1.1] G 466 [x [PE1.2] dip] times popop') + + +.. parsed-literal:: + + 233168 + + +A generator for the Fibonacci Sequence. +--------------------------------------- + +Consider: + +:: + + [b a F] x + [b a F] b a F + +The obvious first thing to do is just add ``b`` and ``a``: + +:: + + [b a F] b a + + [b a F] b+a + +From here we want to arrive at: + +:: + + b [b+a b F] + +Let's start with ``swons``: + +:: + + [b a F] b+a swons + [b+a b a F] + +Considering this quote as a stack: + +:: + + F a b b+a + +We want to get it to: + +:: + + F b b+a b + +So: + +:: + + F a b b+a popdd over + F b b+a b + +And therefore: + +:: + + [b+a b a F] [popdd over] infra + [b b+a b F] + +But we can just use ``cons`` to carry ``b+a`` into the quote: + +:: + + [b a F] b+a [popdd over] cons infra + [b a F] [b+a popdd over] infra + [b b+a b F] + +Lastly: + +:: + + [b b+a b F] uncons + b [b+a b F] + +Putting it all together: + +:: + + F == + [popdd over] cons infra uncons + fib_gen == [1 1 F] + +.. code:: ipython2 + + define('fib == + [popdd over] cons infra uncons') + +.. code:: ipython2 + + define('fib_gen == [1 1 fib]') + +.. code:: ipython2 + + J('fib_gen 10 [x] times') + + +.. parsed-literal:: + + 1 2 3 5 8 13 21 34 55 89 [144 89 fib] + + +Project Euler Problem Two +------------------------- + +:: + + By considering the terms in the Fibonacci sequence whose values do not exceed four million, + find the sum of the even-valued terms. + +Now that we have a generator for the Fibonacci sequence, we need a +function that adds a term in the sequence to a sum if it is even, and +``pop``\ s it otherwise. + +.. code:: ipython2 + + define('PE2.1 == dup 2 % [+] [pop] branch') + +And a predicate function that detects when the terms in the series +"exceed four million". + +.. code:: ipython2 + + define('>4M == 4000000 >') + +Now it's straightforward to define ``PE2`` as a recursive function that +generates terms in the Fibonacci sequence until they exceed four million +and sums the even ones. + +.. code:: ipython2 + + define('PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec') + +.. code:: ipython2 + + J('PE2') + + +.. parsed-literal:: + + 4613732 + + +Here's the collected program definitions: + +:: + + fib == + swons [popdd over] infra uncons + fib_gen == [1 1 fib] + + even == dup 2 % + >4M == 4000000 > + + PE2.1 == even [+] [pop] branch + PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec + +Even-valued Fibonacci Terms +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Using ``o`` for odd and ``e`` for even: + +:: + + o + o = e + e + e = e + o + e = o + +So the Fibonacci sequence considered in terms of just parity would be: + +:: + + o o e o o e o o e o o e o o e o o e + 1 1 2 3 5 8 . . . + +Every third term is even. + +.. code:: ipython2 + + J('[1 0 fib] x x x') # To start the sequence with 1 1 2 3 instead of 1 2 3. + + +.. parsed-literal:: + + 1 1 2 [3 2 fib] + + +Drive the generator three times and ``popop`` the two odd terms. + +.. code:: ipython2 + + J('[1 0 fib] x x x [popop] dipd') + + +.. parsed-literal:: + + 2 [3 2 fib] + + +.. code:: ipython2 + + define('PE2.2 == x x x [popop] dipd') + +.. code:: ipython2 + + J('[1 0 fib] 10 [PE2.2] times') + + +.. parsed-literal:: + + 2 8 34 144 610 2584 10946 46368 196418 832040 [1346269 832040 fib] + + +Replace ``x`` with our new driver function ``PE2.2`` and start our +``fib`` generator at ``1 0``. + +.. code:: ipython2 + + J('0 [1 0 fib] PE2.2 [pop >4M] [popop] [[PE2.1] dip PE2.2] primrec') + + +.. parsed-literal:: + + 4613732 + + +How to compile these? +--------------------- + +You would probably start with a special version of ``G``, and perhaps +modifications to the default ``x``? + +An Interesting Variation +------------------------ + +.. code:: ipython2 + + define('codireco == cons dip rest cons') + +.. code:: ipython2 + + V('[0 [dup ++] codireco] x') + + +.. parsed-literal:: + + . [0 [dup ++] codireco] x + [0 [dup ++] codireco] . x + [0 [dup ++] codireco] . 0 [dup ++] codireco + [0 [dup ++] codireco] 0 . [dup ++] codireco + [0 [dup ++] codireco] 0 [dup ++] . codireco + [0 [dup ++] codireco] 0 [dup ++] . cons dip rest cons + [0 [dup ++] codireco] [0 dup ++] . dip rest cons + . 0 dup ++ [0 [dup ++] codireco] rest cons + 0 . dup ++ [0 [dup ++] codireco] rest cons + 0 0 . ++ [0 [dup ++] codireco] rest cons + 0 1 . [0 [dup ++] codireco] rest cons + 0 1 [0 [dup ++] codireco] . rest cons + 0 1 [[dup ++] codireco] . cons + 0 [1 [dup ++] codireco] . + + +.. code:: ipython2 + + define('G == [codireco] cons cons') + +.. code:: ipython2 + + J('230 [dup ++] G 5 [x] times pop') + + +.. parsed-literal:: + + 230 231 232 233 234 + diff --git a/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.html b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.html new file mode 100644 index 0000000..8bc2a6b --- /dev/null +++ b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.html @@ -0,0 +1,15184 @@ + + + +Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators + + + + + + + + + + + + + + + + + + + +
+
+ + +
+
+
+
+

Hylomorphism

A hylomorphism H :: A -> B converts a value of type A into a value of type B by means of:

+
    +
  • A generator G :: A -> (A, B)
  • +
  • A combiner F :: (B, B) -> B
  • +
  • A predicate P :: A -> Bool to detect the base case
  • +
  • A base case value c :: B
  • +
  • Recursive calls (zero or more); it has a "call stack in the form of a cons list".
  • +
+

It may be helpful to see this function implemented in imperative Python code.

+ +
+
+
+
+
+
In [1]:
+
+
+
def hylomorphism(c, F, P, G):
+    '''Return a hylomorphism function H.'''
+
+    def H(a):
+        if P(a):
+            result = c
+        else:
+            b, aa = G(a)
+            result = F(b, H(aa))
+        return result
+
+    return H
+
+ +
+
+
+ +
+
+
+
+
+

Finding Triangular Numbers

As a concrete example let's use a function that, given a positive integer, returns the sum of all positive integers less than that one. (In this case the types A and B are both int.)

+

With range() and sum()

+
+
+
+
+
+
In [2]:
+
+
+
r = range(10)
+r
+
+ +
+
+
+ +
+
+ + +
+ +
Out[2]:
+ + + + +
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+ +
+ +
+
+ +
+
+
+
In [3]:
+
+
+
sum(r)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[3]:
+ + + + +
+
45
+
+ +
+ +
+
+ +
+
+
+
In [4]:
+
+
+
range_sum = lambda n: sum(range(n))
+range_sum(10)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[4]:
+ + + + +
+
45
+
+ +
+ +
+
+ +
+
+
+
+
+

As a hylomorphism

+
+
+
+
+
+
In [5]:
+
+
+
G = lambda n: (n - 1, n - 1)
+F = lambda a, b: a + b
+P = lambda n: n <= 1
+
+H = hylomorphism(0, F, P, G)
+
+ +
+
+
+ +
+
+
+
In [6]:
+
+
+
H(10)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[6]:
+ + + + +
+
45
+
+ +
+ +
+
+ +
+
+
+
+
+

If you were to run the above code in a debugger and check out the call stack you would find that the variable b in each call to H() is storing the intermediate values as H() recurses. This is what was meant by "call stack in the form of a cons list".

+ +
+
+
+
+
+
+
+

Joy Preamble

+
+
+
+
+
+
In [7]:
+
+
+
from notebook_preamble import D, DefinitionWrapper, J, V, define
+
+ +
+
+
+ +
+
+
+
+
+

Hylomorphism in Joy

We can define a combinator hylomorphism that will make a hylomorphism combinator H from constituent parts.

+ +
H == c [F] [P] [G] hylomorphism
+
+
+

The function H is recursive, so we start with ifte and set the else-part to +some function J that will contain a quoted copy of H. (The then-part just +discards the leftover a and replaces it with the base case value c.)

+ +
H == [P] [pop c] [J] ifte
+
+
+

The else-part J gets just the argument a on the stack.

+ +
a J
+a G              The first thing to do is use the generator G
+aa b             which produces b and a new aa
+aa b [H] dip     we recur with H on the new aa
+aa H b F         and run F on the result.
+
+
+

This gives us a definition for J.

+ +
J == G [H] dip F
+
+
+

Plug it in and convert to genrec.

+ +
H == [P] [pop c] [G [H] dip F] ifte
+H == [P] [pop c] [G]   [dip F] genrec
+
+
+

This is the form of a hylomorphism in Joy, which nicely illustrates that +it is a simple specialization of the general recursion combinator.

+ +
H == [P] [pop c] [G] [dip F] genrec
+ +
+
+
+
+
+
+
+

Derivation of hylomorphism

Now we just need to derive a definition that builds the genrec arguments +out of the pieces given to the hylomorphism combinator.

+ +
H == [P] [pop c]              [G]                  [dip F] genrec
+     [P] [c]    [pop] swoncat [G]        [F] [dip] swoncat genrec
+     [P] c unit [pop] swoncat [G]        [F] [dip] swoncat genrec
+     [P] c [G] [F] [unit [pop] swoncat] dipd [dip] swoncat genrec
+
+
+

Working in reverse:

+
    +
  • Use swoncat twice to decouple [c] and [F].
  • +
  • Use unit to dequote c.
  • +
  • Use dipd to untangle [unit [pop] swoncat] from the givens.
  • +
+

At this point all of the arguments (givens) to the hylomorphism are to the left so we have +a definition for hylomorphism:

+ +
hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec
+
+
+

The order of parameters is different than the one we started with but +that hardly matters, you can rearrange them or just supply them in the +expected order.

+ +
[P] c [G] [F] hylomorphism == H
+ +
+
+
+
+
+
In [8]:
+
+
+
define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec')
+
+ +
+
+
+ +
+
+
+
+
+

Demonstrate summing a range of integers from 0 to n-1.

+
    +
  • [P] is [0 <=]
  • +
  • c is 0
  • +
  • [G] is [1 - dup]
  • +
  • [F] is [+]
  • +
+

So to sum the positive integers less than five we can do this.

+ +
+
+
+
+
+
In [9]:
+
+
+
V('5 [0 <=] 0 [1 - dup] [+] hylomorphism')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                               . 5 [0 <=] 0 [1 - dup] [+] hylomorphism
+                                                                             5 . [0 <=] 0 [1 - dup] [+] hylomorphism
+                                                                      5 [0 <=] . 0 [1 - dup] [+] hylomorphism
+                                                                    5 [0 <=] 0 . [1 - dup] [+] hylomorphism
+                                                          5 [0 <=] 0 [1 - dup] . [+] hylomorphism
+                                                      5 [0 <=] 0 [1 - dup] [+] . hylomorphism
+                                                      5 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec
+                                 5 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec
+                                                                    5 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                                    5 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                                 5 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                                  5 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                            5 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                            5 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec
+                                                            5 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec
+                                                              5 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec
+                                                    5 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec
+                                                5 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec
+                                          5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec
+                                          5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec
+                                          5 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec
+                                            5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec
+    5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte
+5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i
+                                                                             5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i
+                                                                           5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i
+                                                                         False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i
+   False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i
+   5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i
+     5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i
+                   5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i
+                                                                             5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                                                           5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                                                             4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                                                           4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip +
+                                                                             4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 +
+                                                                      4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 +
+                                                              4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 +
+                                                    4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 +
+                                            4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 +
+    4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 +
+4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 +
+                                                                             4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 +
+                                                                           4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 +
+                                                                         False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 +
+   False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 +
+   4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 +
+     4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 +
+                   4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 +
+                                                                             4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +
+                                                                           4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +
+                                                                             3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +
+                                                                           3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +
+                                 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 +
+                                                                             3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 +
+                                                                      3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 +
+                                                              3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 +
+                                                    3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 +
+                                            3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 +
+    3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 +
+3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 +
+                                                                             3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 +
+                                                                           3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 +
+                                                                         False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 +
+   False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 +
+   3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 +
+     3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 +
+                   3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 +
+                                                                             3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +
+                                                                           3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +
+                                                                             2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +
+                                                                           2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +
+                                 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 +
+                                                                             2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 +
+                                                                      2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 +
+                                                              2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 +
+                                                    2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 +
+                                            2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 +
+    2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 +
+2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 +
+                                                                             2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 +
+                                                                           2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 +
+                                                                         False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 +
+   False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 +
+   2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 +
+     2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 +
+                   2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 +
+                                                                             2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +
+                                                                           2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +
+                                                                             1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +
+                                                                           1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +
+                                 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 +
+                                                                             1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 +
+                                                                      1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 +
+                                                              1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 +
+                                                    1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 +
+                                            1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 +
+    1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 +
+1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 +
+                                                                             1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 +
+                                                                           1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 +
+                                                                         False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 +
+   False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 +
+   1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 +
+     1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 +
+                   1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 +
+                                                                             1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +
+                                                                           1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +
+                                                                             0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +
+                                                                           0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +
+                                 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 +
+                                                                             0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 +
+                                                                      0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 +
+                                                              0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 +
+                                                    0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 +
+                                            0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 +
+    0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 +
+0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 +
+                                                                             0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 +
+                                                                           0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 +
+                                                                          True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 +
+    True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 +
+    0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 +
+      0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 +
+                                                                     0 [pop 0] . i 0 + 1 + 2 + 3 + 4 +
+                                                                             0 . pop 0 0 + 1 + 2 + 3 + 4 +
+                                                                               . 0 0 + 1 + 2 + 3 + 4 +
+                                                                             0 . 0 + 1 + 2 + 3 + 4 +
+                                                                           0 0 . + 1 + 2 + 3 + 4 +
+                                                                             0 . 1 + 2 + 3 + 4 +
+                                                                           0 1 . + 2 + 3 + 4 +
+                                                                             1 . 2 + 3 + 4 +
+                                                                           1 2 . + 3 + 4 +
+                                                                             3 . 3 + 4 +
+                                                                           3 3 . + 4 +
+                                                                             6 . 4 +
+                                                                           6 4 . +
+                                                                            10 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Anamorphism

An anamorphism can be defined as a hylomorphism that uses [] for c and +swons for F.

+ +
[P] [G] anamorphism == [P] [] [G] [swons] hylomorphism == A
+
+
+

This allows us to define an anamorphism combinator in terms of +the hylomorphism combinator.

+ +
[] swap [swons] hylomorphism == anamorphism
+
+
+

Partial evaluation gives us a "pre-cooked" form.

+ +
[P] [G] . anamorphism
+[P] [G] . [] swap [swons] hylomorphism
+[P] [G] [] . swap [swons] hylomorphism
+[P] [] [G] . [swons] hylomorphism
+[P] [] [G] [swons] . hylomorphism
+[P] [] [G] [swons] . [unit [pop] swoncat] dipd [dip] swoncat genrec
+[P] [] [G] [swons] [unit [pop] swoncat] . dipd [dip] swoncat genrec
+[P] [] . unit [pop] swoncat [G] [swons] [dip] swoncat genrec
+[P] [[]] [pop] . swoncat [G] [swons] [dip] swoncat genrec
+[P] [pop []] [G] [swons] [dip] . swoncat genrec
+
+[P] [pop []] [G] [dip swons] genrec
+
+
+

(We could also have just substituted for c and F in the definition of H.)

+ +
H == [P] [pop c ] [G] [dip F    ] genrec
+A == [P] [pop []] [G] [dip swons] genrec
+
+
+

The partial evaluation is overkill in this case but it serves as a +reminder that this sort of program specialization can, in many cases, be +carried out automatically.)

+

Untangle [G] from [pop []] using swap.

+ +
[P] [G] [pop []] swap [dip swons] genrec
+
+
+

All of the arguments to anamorphism are to the left, so we have a definition for it.

+ +
anamorphism == [pop []] swap [dip swons] genrec
+
+
+

An example of an anamorphism is the range function.

+ +
range == [0 <=] [1 - dup] anamorphism
+ +
+
+
+
+
+
+
+

Catamorphism

A catamorphism can be defined as a hylomorphism that uses [uncons swap] for [G] +and [[] =] for the predicate [P].

+ +
c [F] catamorphism == [[] =] c [uncons swap] [F] hylomorphism == C
+
+
+

This allows us to define a catamorphism combinator in terms of +the hylomorphism combinator.

+ +
[[] =] roll> [uncons swap] swap hylomorphism == catamorphism
+
+
+

Partial evaluation doesn't help much.

+ +
c [F] . catamorphism
+c [F] . [[] =] roll> [uncons swap] swap hylomorphism
+c [F] [[] =] . roll> [uncons swap] swap hylomorphism
+[[] =] c [F] [uncons swap] . swap hylomorphism
+[[] =] c [uncons swap] [F] . hylomorphism
+[[] =] c [uncons swap] [F] [unit [pop] swoncat] . dipd [dip] swoncat genrec
+[[] =] c . unit [pop] swoncat [uncons swap] [F] [dip] swoncat genrec
+[[] =] [c] [pop] . swoncat [uncons swap] [F] [dip] swoncat genrec
+[[] =] [pop c] [uncons swap] [F] [dip] . swoncat genrec
+[[] =] [pop c] [uncons swap] [dip F] genrec
+
+
+

Because the arguments to catamorphism have to be prepared (unlike the arguments +to anamorphism, which only need to be rearranged slightly) there isn't much point +to "pre-cooking" the definition.

+ +
catamorphism == [[] =] roll> [uncons swap] swap hylomorphism
+
+
+

An example of a catamorphism is the sum function.

+ +
sum == 0 [+] catamorphism
+ +
+
+
+
+
+
+
+

"Fusion Law" for catas (UNFINISHED!!!)

I'm not sure exactly how to translate the "Fusion Law" for catamorphisms into Joy.

+

I know that a map composed with a cata can be expressed as a new cata:

+ +
[F] map b [B] cata == b [F B] cata
+
+
+

But this isn't the one described in "Bananas...". That's more like:

+

A cata composed with some function can be expressed as some other cata:

+ +
b [B] catamorphism F == c [C] catamorphism
+
+
+

Given:

+ +
b F == c
+
+...
+
+B F == [F] dip C
+
+...
+
+b[B]cata F == c[C]cata
+
+F(B(head, tail)) == C(head, F(tail))
+
+1 [2 3] B F         1 [2 3] F C
+
+
+b F == c
+B F == F C
+
+b [B] catamorphism F == c [C] catamorphism
+b [B] catamorphism F == b F [C] catamorphism
+
+...
+
+
+

Or maybe,

+ +
[F] map b [B] cata == c [C] cata     ???
+
+[F] map b [B] cata == b [F B] cata    I think this is generally true, unless F consumes stack items
+                                        instead of just transforming TOS.  Of course, there's always [F] unary.
+b [F] unary [[F] unary B] cata
+
+[10 *] map 0 swap [+] step == 0 swap [10 * +] step
+
+
+
+

For example:

+ +
F == 10 *
+b == 0
+B == +
+c == 0
+C == F +
+
+b F    == c
+0 10 * == 0
+
+B F    == [F]    dip C
++ 10 * == [10 *] dip F +
++ 10 * == [10 *] dip 10 * +
+
+n m + 10 * == 10(n+m)
+
+n m [10 *] dip 10 * +
+n 10 * m 10 * +
+10n m 10 * +
+10n 10m +
+10n+10m
+
+10n+10m = 10(n+m)
+
+
+

Ergo:

+ +
0 [+] catamorphism 10 * == 0 [10 * +] catamorphism
+ +
+
+
+
+
+
+
+

The step combinator will usually be better to use than catamorphism.

+
sum == 0 swap [+] step
+sum == 0 [+] catamorphism
+ +
+
+
+
+
+
+
+

anamorphism catamorphism == hylomorphism

Here is (part of) the payoff.

+

An anamorphism followed by (composed with) a +catamorphism is a hylomorphism, with the advantage that the hylomorphism +does not create the intermediate list structure. The values are stored in +either the call stack, for those implementations that use one, or in the pending +expression ("continuation") for the Joypy interpreter. They still have to +be somewhere, converting from an anamorphism and catamorphism to a hylomorphism +just prevents using additional storage and doing additional processing.

+ +
    range == [0 <=] [1 - dup] anamorphism
+      sum == 0 [+] catamorphism
+
+range sum == [0 <=] [1 - dup] anamorphism 0 [+] catamorphism
+          == [0 <=] 0 [1 - dup] [+] hylomorphism
+
+
+

We can let the hylomorphism combinator build range_sum for us or just +substitute ourselves.

+ +
        H == [P]    [pop c] [G]       [dip F] genrec
+range_sum == [0 <=] [pop 0] [1 - dup] [dip +] genrec
+ +
+
+
+
+
+
In [8]:
+
+
+
defs = '''
+anamorphism == [pop []] swap [dip swons] genrec
+hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec
+catamorphism == [[] =] roll> [uncons swap] swap hylomorphism
+range == [0 <=] [1 - dup] anamorphism
+sum == 0 [+] catamorphism
+range_sum == [0 <=] 0 [1 - dup] [+] hylomorphism
+'''
+
+DefinitionWrapper.add_definitions(defs, D)
+
+ +
+
+
+ +
+
+
+
In [9]:
+
+
+
J('10 range')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[9 8 7 6 5 4 3 2 1 0]
+
+
+
+ +
+
+ +
+
+
+
In [10]:
+
+
+
J('[9 8 7 6 5 4 3 2 1 0] sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
45
+
+
+
+ +
+
+ +
+
+
+
In [11]:
+
+
+
V('10 range sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                                                               . 10 range sum
+                                                                                                                            10 . range sum
+                                                                                                                            10 . [0 <=] [1 - dup] anamorphism sum
+                                                                                                                     10 [0 <=] . [1 - dup] anamorphism sum
+                                                                                                           10 [0 <=] [1 - dup] . anamorphism sum
+                                                                                                           10 [0 <=] [1 - dup] . [pop []] swap [dip swons] genrec sum
+                                                                                                  10 [0 <=] [1 - dup] [pop []] . swap [dip swons] genrec sum
+                                                                                                  10 [0 <=] [pop []] [1 - dup] . [dip swons] genrec sum
+                                                                                      10 [0 <=] [pop []] [1 - dup] [dip swons] . genrec sum
+                                         10 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte sum
+                                    10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [10] [0 <=] . infra first choice i sum
+                                                                                                                            10 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum
+                                                                                                                          10 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum
+                                        False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] . swaack first choice i sum
+                                        10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i sum
+                                          10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i sum
+                                                         10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i sum
+                                                                                                                            10 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum
+                                                                                                                          10 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum
+                                                                                                                             9 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum
+                                                                                                                           9 9 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum
+                                                                            9 9 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons sum
+                                                                                                                             9 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 9 swons sum
+                                                                                                                      9 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 9 swons sum
+                                                                                                             9 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 9 swons sum
+                                                                                                   9 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 9 swons sum
+                                                                                       9 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 9 swons sum
+                                          9 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 9 swons sum
+                                      9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [9] [0 <=] . infra first choice i 9 swons sum
+                                                                                                                             9 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum
+                                                                                                                           9 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] . swaack first choice i 9 swons sum
+                                         9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 9 swons sum
+                                           9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 9 swons sum
+                                                          9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 9 swons sum
+                                                                                                                             9 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum
+                                                                                                                           9 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum
+                                                                                                                             8 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum
+                                                                                                                           8 8 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum
+                                                                            8 8 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 9 swons sum
+                                                                                                                             8 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum
+                                                                                                                      8 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum
+                                                                                                             8 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 8 swons 9 swons sum
+                                                                                                   8 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 8 swons 9 swons sum
+                                                                                       8 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 8 swons 9 swons sum
+                                          8 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 8 swons 9 swons sum
+                                      8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [8] [0 <=] . infra first choice i 8 swons 9 swons sum
+                                                                                                                             8 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum
+                                                                                                                           8 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] . swaack first choice i 8 swons 9 swons sum
+                                         8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 8 swons 9 swons sum
+                                           8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 8 swons 9 swons sum
+                                                          8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 8 swons 9 swons sum
+                                                                                                                             8 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum
+                                                                                                                           8 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum
+                                                                                                                             7 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum
+                                                                                                                           7 7 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum
+                                                                            7 7 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 8 swons 9 swons sum
+                                                                                                                             7 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum
+                                                                                                                      7 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum
+                                                                                                             7 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum
+                                                                                                   7 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 7 swons 8 swons 9 swons sum
+                                                                                       7 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 7 swons 8 swons 9 swons sum
+                                          7 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 7 swons 8 swons 9 swons sum
+                                      7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [7] [0 <=] . infra first choice i 7 swons 8 swons 9 swons sum
+                                                                                                                             7 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum
+                                                                                                                           7 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] . swaack first choice i 7 swons 8 swons 9 swons sum
+                                         7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 7 swons 8 swons 9 swons sum
+                                           7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 7 swons 8 swons 9 swons sum
+                                                          7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 7 swons 8 swons 9 swons sum
+                                                                                                                             7 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum
+                                                                                                                           7 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum
+                                                                                                                             6 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum
+                                                                                                                           6 6 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum
+                                                                            6 6 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 7 swons 8 swons 9 swons sum
+                                                                                                                             6 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      6 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             6 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   6 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       6 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 6 swons 7 swons 8 swons 9 swons sum
+                                          6 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 6 swons 7 swons 8 swons 9 swons sum
+                                      6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [6] [0 <=] . infra first choice i 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             6 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           6 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] . swaack first choice i 6 swons 7 swons 8 swons 9 swons sum
+                                         6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 6 swons 7 swons 8 swons 9 swons sum
+                                           6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 6 swons 7 swons 8 swons 9 swons sum
+                                                          6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             6 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           6 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             5 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           5 5 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                            5 5 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             5 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      5 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             5 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   5 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       5 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          5 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                      5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [5] [0 <=] . infra first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             5 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           5 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] . swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                           5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                          5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             5 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           5 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             4 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           4 4 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                            4 4 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             4 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      4 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             4 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   4 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       4 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          4 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                      4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [4] [0 <=] . infra first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             4 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           4 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] . swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                           4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                          4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             4 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           4 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             3 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           3 3 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                            3 3 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             3 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      3 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             3 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   3 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       3 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          3 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                      3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [3] [0 <=] . infra first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             3 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           3 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] . swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                           3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                          3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             3 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           3 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             2 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           2 2 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                            2 2 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             2 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      2 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             2 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   2 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       2 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          2 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                      2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [2] [0 <=] . infra first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             2 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           2 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                           2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                          2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             2 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           2 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             1 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           1 1 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                            1 1 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             1 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      1 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             1 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   1 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       1 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          1 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                      1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [1] [0 <=] . infra first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             1 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           1 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                         1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                           1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                          1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             1 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           1 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             0 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           0 0 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                            0 0 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             0 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                      0 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                             0 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                   0 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                       0 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          0 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                      0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [0] [0 <=] . infra first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             0 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           0 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                          True . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          True [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] . swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                          0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                            0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] True . choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                    0 [pop []] . i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                             0 . pop [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                               . [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                            [] . 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                          [] 0 . swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                          [] 0 . swap cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                          0 [] . cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                           [0] . 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         [0] 1 . swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         [0] 1 . swap cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         1 [0] . cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                         [1 0] . 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                       [1 0] 2 . swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                       [1 0] 2 . swap cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                       2 [1 0] . cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                       [2 1 0] . 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                     [2 1 0] 3 . swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                     [2 1 0] 3 . swap cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                     3 [2 1 0] . cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                     [3 2 1 0] . 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                   [3 2 1 0] 4 . swons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                   [3 2 1 0] 4 . swap cons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                   4 [3 2 1 0] . cons 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                   [4 3 2 1 0] . 5 swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                 [4 3 2 1 0] 5 . swons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                 [4 3 2 1 0] 5 . swap cons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                 5 [4 3 2 1 0] . cons 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                                 [5 4 3 2 1 0] . 6 swons 7 swons 8 swons 9 swons sum
+                                                                                                               [5 4 3 2 1 0] 6 . swons 7 swons 8 swons 9 swons sum
+                                                                                                               [5 4 3 2 1 0] 6 . swap cons 7 swons 8 swons 9 swons sum
+                                                                                                               6 [5 4 3 2 1 0] . cons 7 swons 8 swons 9 swons sum
+                                                                                                               [6 5 4 3 2 1 0] . 7 swons 8 swons 9 swons sum
+                                                                                                             [6 5 4 3 2 1 0] 7 . swons 8 swons 9 swons sum
+                                                                                                             [6 5 4 3 2 1 0] 7 . swap cons 8 swons 9 swons sum
+                                                                                                             7 [6 5 4 3 2 1 0] . cons 8 swons 9 swons sum
+                                                                                                             [7 6 5 4 3 2 1 0] . 8 swons 9 swons sum
+                                                                                                           [7 6 5 4 3 2 1 0] 8 . swons 9 swons sum
+                                                                                                           [7 6 5 4 3 2 1 0] 8 . swap cons 9 swons sum
+                                                                                                           8 [7 6 5 4 3 2 1 0] . cons 9 swons sum
+                                                                                                           [8 7 6 5 4 3 2 1 0] . 9 swons sum
+                                                                                                         [8 7 6 5 4 3 2 1 0] 9 . swons sum
+                                                                                                         [8 7 6 5 4 3 2 1 0] 9 . swap cons sum
+                                                                                                         9 [8 7 6 5 4 3 2 1 0] . cons sum
+                                                                                                         [9 8 7 6 5 4 3 2 1 0] . sum
+                                                                                                         [9 8 7 6 5 4 3 2 1 0] . 0 [+] catamorphism
+                                                                                                       [9 8 7 6 5 4 3 2 1 0] 0 . [+] catamorphism
+                                                                                                   [9 8 7 6 5 4 3 2 1 0] 0 [+] . catamorphism
+                                                                                                   [9 8 7 6 5 4 3 2 1 0] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism
+                                                                                            [9 8 7 6 5 4 3 2 1 0] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism
+                                                                                            [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] . [uncons swap] swap hylomorphism
+                                                                              [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] [uncons swap] . swap hylomorphism
+                                                                              [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . hylomorphism
+                                                                              [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec
+                                                         [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec
+                                                                                                [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec
+                                                                                                [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec
+                                                                                             [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec
+                                                                                              [9 8 7 6 5 4 3 2 1 0] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec
+                                                                                        [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec
+                                                                                        [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec
+                                                                                        [9 8 7 6 5 4 3 2 1 0] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec
+                                                                                          [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec
+                                                                            [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec
+                                                                        [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec
+                                                                  [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec
+                                                                  [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec
+                                                                  [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec
+                                                                    [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec
+                        [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte
+[9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[9 8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i
+                                                                                                         [9 8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i
+                                                                                                      [9 8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i
+                       False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] . swaack first choice i
+                       [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i
+                         [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i
+                                       [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i
+                                                                                                         [9 8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +
+                                                                                                         9 [8 7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +
+                                                                                                         [8 7 6 5 4 3 2 1 0] 9 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +
+                                                           [8 7 6 5 4 3 2 1 0] 9 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip +
+                                                                                                           [8 7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 9 +
+                                                                                                    [8 7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 9 +
+                                                                                            [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 9 +
+                                                                              [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 9 +
+                                                                      [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 9 +
+                          [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 9 +
+    [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 9 +
+                                                                                                           [8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 +
+                                                                                                        [8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 +
+                         False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] . swaack first choice i 9 +
+                         [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 +
+                           [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 9 +
+                                         [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 9 +
+                                                                                                           [8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 +
+                                                                                                           8 [7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 +
+                                                                                                           [7 6 5 4 3 2 1 0] 8 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 +
+                                                             [7 6 5 4 3 2 1 0] 8 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 9 +
+                                                                                                             [7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 8 + 9 +
+                                                                                                      [7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 8 + 9 +
+                                                                                              [7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 8 + 9 +
+                                                                                [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 8 + 9 +
+                                                                        [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 8 + 9 +
+                            [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 8 + 9 +
+        [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 8 + 9 +
+                                                                                                             [7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 +
+                                                                                                          [7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 +
+                           False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] . swaack first choice i 8 + 9 +
+                           [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 +
+                             [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 +
+                                           [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 8 + 9 +
+                                                                                                             [7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 +
+                                                                                                             7 [6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 +
+                                                                                                             [6 5 4 3 2 1 0] 7 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 +
+                                                               [6 5 4 3 2 1 0] 7 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 8 + 9 +
+                                                                                                               [6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 +
+                                                                                                        [6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 +
+                                                                                                [6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 7 + 8 + 9 +
+                                                                                  [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 7 + 8 + 9 +
+                                                                          [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 7 + 8 + 9 +
+                              [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 7 + 8 + 9 +
+            [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[6 5 4 3 2 1 0]] [[] =] . infra first choice i 7 + 8 + 9 +
+                                                                                                               [6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 +
+                                                                                                            [6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 +
+                             False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] . swaack first choice i 7 + 8 + 9 +
+                             [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 +
+                               [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 +
+                                             [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 7 + 8 + 9 +
+                                                                                                               [6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 +
+                                                                                                               6 [5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 +
+                                                                                                               [5 4 3 2 1 0] 6 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 +
+                                                                 [5 4 3 2 1 0] 6 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 7 + 8 + 9 +
+                                                                                                                 [5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 +
+                                                                                                          [5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 +
+                                                                                                  [5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 +
+                                                                                    [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 6 + 7 + 8 + 9 +
+                                                                            [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 6 + 7 + 8 + 9 +
+                                [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 +
+                [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[5 4 3 2 1 0]] [[] =] . infra first choice i 6 + 7 + 8 + 9 +
+                                                                                                                 [5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 +
+                                                                                                              [5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 +
+                               False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] . swaack first choice i 6 + 7 + 8 + 9 +
+                               [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 +
+                                 [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 +
+                                               [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 +
+                                                                                                                 [5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                                                                                                 5 [4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                                                                                                 [4 3 2 1 0] 5 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                                                   [4 3 2 1 0] 5 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 6 + 7 + 8 + 9 +
+                                                                                                                   [4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                                                                            [4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                                                                    [4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                                                      [4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                                              [4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 5 + 6 + 7 + 8 + 9 +
+                                  [4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 +
+                    [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[4 3 2 1 0]] [[] =] . infra first choice i 5 + 6 + 7 + 8 + 9 +
+                                                                                                                   [4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 +
+                                                                                                                [4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 +
+                                 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] . swaack first choice i 5 + 6 + 7 + 8 + 9 +
+                                 [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 +
+                                   [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 +
+                                                 [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 +
+                                                                                                                   [4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                   4 [3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                   [3 2 1 0] 4 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                                                     [3 2 1 0] 4 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                     [3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                              [3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                      [3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                        [3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                [3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                    [3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 +
+                        [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3 2 1 0]] [[] =] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                     [3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                  [3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                   False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                   [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                     [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                   [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                     [3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                     3 [2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                     [2 1 0] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                       [2 1 0] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                       [2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                [2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                        [2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                          [2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                  [2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                      [2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                            [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 1 0]] [[] =] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                       [2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                    [2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                     False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                     [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                       [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                     [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                       [2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                       2 [1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                       [1 0] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                         [1 0] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         [1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                  [1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                          [1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                            [1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                    [1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                        [1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[1 0]] [[] =] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         [1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                      [1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                       False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                       [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                         [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                       [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         [1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         1 [0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         [0] 1 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                           [0] 1 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           [0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                    [0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                            [0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                              [0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                      [0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                          [0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                    [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[0]] [[] =] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           [0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                        [0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                         False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                         [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                           [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                         [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           [0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                          0 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                          [] 0 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                            [] 0 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                            [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                     [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                             [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                               [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                       [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                           [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                      [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[]] [[] =] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                            [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                         [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                          True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                           True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                           [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                             [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                    [] [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                            [] . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                               . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                             0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                             0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                             1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                             3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           3 3 . + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                             6 . 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                           6 4 . + 5 + 6 + 7 + 8 + 9 +
+                                                                                                                            10 . 5 + 6 + 7 + 8 + 9 +
+                                                                                                                          10 5 . + 6 + 7 + 8 + 9 +
+                                                                                                                            15 . 6 + 7 + 8 + 9 +
+                                                                                                                          15 6 . + 7 + 8 + 9 +
+                                                                                                                            21 . 7 + 8 + 9 +
+                                                                                                                          21 7 . + 8 + 9 +
+                                                                                                                            28 . 8 + 9 +
+                                                                                                                          28 8 . + 9 +
+                                                                                                                            36 . 9 +
+                                                                                                                          36 9 . +
+                                                                                                                            45 . 
+
+
+
+ +
+
+ +
+
+
+
In [12]:
+
+
+
V('10 range_sum')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                 . 10 range_sum
+                                                                              10 . range_sum
+                                                                              10 . [0 <=] 0 [1 - dup] [+] hylomorphism
+                                                                       10 [0 <=] . 0 [1 - dup] [+] hylomorphism
+                                                                     10 [0 <=] 0 . [1 - dup] [+] hylomorphism
+                                                           10 [0 <=] 0 [1 - dup] . [+] hylomorphism
+                                                       10 [0 <=] 0 [1 - dup] [+] . hylomorphism
+                                                       10 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec
+                                  10 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec
+                                                                     10 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                                     10 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                                  10 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                                   10 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                             10 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec
+                                                             10 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec
+                                                             10 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec
+                                                               10 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec
+                                                     10 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec
+                                                 10 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec
+                                           10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec
+                                           10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec
+                                           10 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec
+                                             10 [0 <=] [pop 0] [1 - dup] [dip +] . genrec
+     10 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte
+10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [10] [0 <=] . infra first choice i
+                                                                              10 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i
+                                                                            10 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i
+    False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] . swaack first choice i
+    10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i
+      10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i
+                    10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i
+                                                                              10 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                                                            10 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                                                               9 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                                                             9 9 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +
+                                   9 9 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip +
+                                                                               9 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 9 +
+                                                                        9 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 9 +
+                                                                9 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 9 +
+                                                      9 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 9 +
+                                              9 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 9 +
+      9 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 9 +
+  9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [9] [0 <=] . infra first choice i 9 +
+                                                                               9 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 +
+                                                                             9 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] . swaack first choice i 9 +
+     9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 +
+       9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 9 +
+                     9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 9 +
+                                                                               9 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +
+                                                                             9 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +
+                                                                               8 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +
+                                                                             8 8 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +
+                                   8 8 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 9 +
+                                                                               8 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 8 + 9 +
+                                                                        8 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 8 + 9 +
+                                                                8 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 8 + 9 +
+                                                      8 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 8 + 9 +
+                                              8 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 8 + 9 +
+      8 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 8 + 9 +
+  8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [8] [0 <=] . infra first choice i 8 + 9 +
+                                                                               8 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 +
+                                                                             8 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] . swaack first choice i 8 + 9 +
+     8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 +
+       8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 +
+                     8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 8 + 9 +
+                                                                               8 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +
+                                                                             8 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +
+                                                                               7 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +
+                                                                             7 7 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +
+                                   7 7 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 8 + 9 +
+                                                                               7 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 +
+                                                                        7 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 +
+                                                                7 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 7 + 8 + 9 +
+                                                      7 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 7 + 8 + 9 +
+                                              7 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 7 + 8 + 9 +
+      7 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 7 + 8 + 9 +
+  7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [7] [0 <=] . infra first choice i 7 + 8 + 9 +
+                                                                               7 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 +
+                                                                             7 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] . swaack first choice i 7 + 8 + 9 +
+     7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 +
+       7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 +
+                     7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 7 + 8 + 9 +
+                                                                               7 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +
+                                                                             7 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +
+                                                                               6 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +
+                                                                             6 6 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +
+                                   6 6 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 7 + 8 + 9 +
+                                                                               6 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 +
+                                                                        6 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 +
+                                                                6 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 +
+                                                      6 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 6 + 7 + 8 + 9 +
+                                              6 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 6 + 7 + 8 + 9 +
+      6 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 +
+  6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [6] [0 <=] . infra first choice i 6 + 7 + 8 + 9 +
+                                                                               6 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 +
+                                                                             6 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] . swaack first choice i 6 + 7 + 8 + 9 +
+     6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 +
+       6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 +
+                     6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 +
+                                                                               6 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                                                             6 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                                                               5 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                                                             5 5 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +
+                                   5 5 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 6 + 7 + 8 + 9 +
+                                                                               5 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                                        5 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                                5 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                                      5 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 5 + 6 + 7 + 8 + 9 +
+                                              5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 5 + 6 + 7 + 8 + 9 +
+      5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 +
+  5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i 5 + 6 + 7 + 8 + 9 +
+                                                                               5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 +
+                                                                             5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i 5 + 6 + 7 + 8 + 9 +
+     5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 +
+       5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 +
+                     5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 +
+                                                                               5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                                                             5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                                                               4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                                                             4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +
+                                   4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 +
+                                                                               4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                        4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                                      4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +
+                                              4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 +
+      4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 +
+  4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+     4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 +
+       4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 +
+                     4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                   3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                        3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                      3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                              3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+      3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+  3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+     3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+       3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                     3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                   2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                        2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                      2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                              2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+      2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+  2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+     2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+       2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                     2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                   1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                        1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                      1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                              1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+      1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+  1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                           False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+     False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+     1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+       1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                     1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                   0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                        0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                      0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                              0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+      0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+  0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                            True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+      True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+      0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+        0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                       0 [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               0 . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                                 . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             3 3 . + 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                               6 . 4 + 5 + 6 + 7 + 8 + 9 +
+                                                                             6 4 . + 5 + 6 + 7 + 8 + 9 +
+                                                                              10 . 5 + 6 + 7 + 8 + 9 +
+                                                                            10 5 . + 6 + 7 + 8 + 9 +
+                                                                              15 . 6 + 7 + 8 + 9 +
+                                                                            15 6 . + 7 + 8 + 9 +
+                                                                              21 . 7 + 8 + 9 +
+                                                                            21 7 . + 8 + 9 +
+                                                                              28 . 8 + 9 +
+                                                                            28 8 . + 9 +
+                                                                              36 . 9 +
+                                                                            36 9 . +
+                                                                              45 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Factorial Function and Paramorphisms

A paramorphism P :: B -> A is a recursion combinator that uses dup on intermediate values.

+ +
n swap [P] [pop] [[F] dupdip G] primrec
+
+
+

With

+
    +
  • n :: A is the "identity" for F (like 1 for multiplication, 0 for addition)
  • +
  • F :: (A, B) -> A
  • +
  • G :: B -> B generates the next B value.
  • +
  • and lastly P :: B -> Bool detects the end of the series.
  • +
+ +
+
+
+
+
+
+
+

For Factorial function (types A and B are both integer):

+ +
n == 1
+F == *
+G == --
+P == 1 <=
+ +
+
+
+
+
+
In [15]:
+
+
+
define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec')
+
+ +
+
+
+ +
+
+
+
+
+

Try it with input 3 (omitting evaluation of predicate):

+ +
3 1 swap [1 <=] [pop] [[*] dupdip --] primrec
+1 3      [1 <=] [pop] [[*] dupdip --] primrec
+
+1 3 [*] dupdip --
+1 3  * 3      --
+3      3      --
+3      2
+
+3 2 [*] dupdip --
+3 2  *  2      --
+6       2      --
+6       1
+
+6 1 [1 <=] [pop] [[*] dupdip --] primrec
+
+6 1 pop
+6
+ +
+
+
+
+
+
In [16]:
+
+
+
J('3 factorial')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6
+
+
+
+ +
+
+ +
+
+
+
+
+

Derive paramorphism from the form above.

+
n swap [P] [pop] [[F] dupdip G] primrec
+
+n swap [P]       [pop]     [[F] dupdip G]                  primrec
+n [P] [swap] dip [pop]     [[F] dupdip G]                  primrec
+n [P] [[F] dupdip G]                [[swap] dip [pop]] dip primrec
+n [P] [F] [dupdip G]           cons [[swap] dip [pop]] dip primrec
+n [P] [F] [G] [dupdip] swoncat cons [[swap] dip [pop]] dip primrec
+
+paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec
+ +
+
+
+
+
+
In [17]:
+
+
+
define('paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec')
+define('factorial == 1 [1 <=] [*] [--] paramorphism')
+
+ +
+
+
+ +
+
+
+
In [18]:
+
+
+
J('3 factorial')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
6
+
+
+
+ +
+
+ +
+
+
+
+
+

tails

An example of a paramorphism for lists given in the "Bananas..." paper is tails which returns the list of "tails" of a list.

+ +
[1 2 3] tails == [[] [3] [2 3]]
+
+
+

Using paramorphism we would write:

+ +
n == []
+F == rest swons
+G == rest
+P == not
+
+tails == [] [not] [rest swons] [rest] paramorphism
+ +
+
+
+
+
+
In [19]:
+
+
+
define('tails == [] [not] [rest swons] [rest] paramorphism')
+
+ +
+
+
+ +
+
+
+
In [20]:
+
+
+
J('[1 2 3] tails')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[] [3] [2 3]]
+
+
+
+ +
+
+ +
+
+
+
In [21]:
+
+
+
J('25 range tails [popop] infra [sum] map')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276]
+
+
+
+ +
+
+ +
+
+
+
In [22]:
+
+
+
J('25 range [range_sum] map')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[276 253 231 210 190 171 153 136 120 105 91 78 66 55 45 36 28 21 15 10 6 3 1 0 0]
+
+
+
+ +
+
+ +
+
+
+
+
+

Factoring rest

Right before the recursion begins we have:

+ +
[] [1 2 3] [not] [pop] [[rest swons] dupdip rest] primrec
+
+
+

But we might prefer to factor rest in the quote:

+ +
[] [1 2 3] [not] [pop] [rest [swons] dupdip] primrec
+
+
+

There's no way to do that with the paramorphism combinator as defined. We would have to write and use a slightly different recursion combinator that accepted an additional "preprocessor" function [H] and built:

+ +
n swap [P] [pop] [H [F] dupdip G] primrec
+
+
+

Or just write it out manually. This is yet another place where the sufficiently smart compiler will one day automatically refactor the code. We could write a paramorphism combinator that checked [F] and [G] for common prefix and extracted it.

+ +
+
+
+
+
+
+
+

Patterns of Recursion

Our story so far...

+
    +
  • A combiner F :: (B, B) -> B
  • +
  • A predicate P :: A -> Bool to detect the base case
  • +
  • A base case value c :: B
  • +
+

Hylo-, Ana-, Cata-

+
w/ G :: A -> (A, B)
+
+H == [P   ] [pop c ] [G          ] [dip F    ] genrec
+A == [P   ] [pop []] [G          ] [dip swons] genrec
+C == [[] =] [pop c ] [uncons swap] [dip F    ] genrec
+
+
+

Para-, ?-, ?-

+
w/ G :: B -> B
+
+P == c  swap [P   ] [pop] [[F    ] dupdip G          ] primrec
+? == [] swap [P   ] [pop] [[swons] dupdip G          ] primrec
+? == c  swap [[] =] [pop] [[F    ] dupdip uncons swap] primrec
+ +
+
+
+
+
+
+
+

Four Generalizations

There are at least four kinds of recursive combinator, depending on two choices. The first choice is whether the combiner function should be evaluated during the recursion or pushed into the pending expression to be "collapsed" at the end. The second choice is whether the combiner needs to operate on the current value of the datastructure or the generator's output.

+ +
H ==        [P] [pop c] [G             ] [dip F] genrec
+H == c swap [P] [pop]   [G [F]    dip  ] [i]     genrec
+H ==        [P] [pop c] [  [G] dupdip  ] [dip F] genrec
+H == c swap [P] [pop]   [  [F] dupdip G] [i]     genrec
+
+
+

Consider:

+ +
... a G [H] dip F                w/ a G == a' b
+... c a G [F] dip H                 a G == b  a'
+... a [G] dupdip [H] dip F          a G == a'
+... c a [F] dupdip G H              a G == a'
+ +
+
+
+
+
+
+
+

1

+
H == [P] [pop c] [G] [dip F] genrec
+
+
+

Iterate n times.

+ +
... a [P] [pop c] [G] [dip F] genrec
+... a  G [H] dip F
+... a' b [H] dip F
+... a' H b F
+... a'  G [H] dip F b F
+... a'' b [H] dip F b F
+... a'' H b F b F
+... a''  G [H] dip F b F b F
+... a''' b [H] dip F b F b F
+... a''' H b F b F b F
+... a''' pop c b F b F b F
+... c b F b F b F
+
+
+

This form builds up a continuation that contains the intermediate results along with the pending combiner functions. When the base case is reached the last term is replaced by the identity value c and the continuation "collapses" into the final result.

+ +
+
+
+
+
+
+
+

2

When you can start with the identity value c on the stack and the combiner can operate as you go, using the intermediate results immediately rather than queuing them up, use this form. An important difference is that the generator function must return its results in the reverse order.

+ +
H == c swap [P] [pop] [G [F] dip] primrec
+
+... c a G [F] dip H
+... c b a' [F] dip H
+... c b F a' H
+... c b F a' G [F] dip H
+... c b F b a'' [F] dip H
+... c b F b F a'' H
+... c b F b F a'' G [F] dip H
+... c b F b F b a''' [F] dip H
+... c b F b F b F a''' H
+... c b F b F b F a''' pop
+... c b F b F b F
+
+
+

The end line here is the same as for above, but only because we didn't evaluate F when it normally would have been.

+ +
+
+
+
+
+
+
+

3

If the combiner and the generator both need to work on the current value then dup must be used at some point, and the generator must produce one item instead of two (the b is instead the duplicate of a.)

+ +
H == [P] [pop c] [[G] dupdip] [dip F] genrec
+
+... a [G] dupdip [H] dip F
+... a  G a       [H] dip F
+... a'   a       [H] dip F
+... a' H a F
+... a' [G] dupdip [H] dip F a F
+... a'  G  a'     [H] dip F a F
+... a''    a'     [H] dip F a F
+... a'' H  a' F a F
+... a'' [G] dupdip [H] dip F a' F a F
+... a''  G  a''    [H] dip F a' F a F
+... a'''    a''    [H] dip F a' F a F
+... a''' H  a'' F a' F a F
+... a''' pop c  a'' F a' F a F
+...          c  a'' F a' F a F
+ +
+
+
+
+
+
+
+

4

And, last but not least, if you can combine as you go, starting with c, and the combiner needs to work on the current item, this is the form:

+ +
W == c swap [P] [pop] [[F] dupdip G] primrec
+
+... a c swap [P] [pop] [[F] dupdip G] primrec
+... c a [P] [pop] [[F] dupdip G] primrec
+... c a [F] dupdip G W
+... c a  F a G W
+... c a  F a'  W
+... c a  F a'  [F] dupdip G W
+... c a  F a'   F  a'     G W
+... c a  F a'   F  a''      W
+... c a  F a'   F  a''      [F] dupdip G W
+... c a  F a'   F  a''       F  a''    G W
+... c a  F a'   F  a''       F  a'''     W
+... c a  F a'   F  a''       F  a'''     pop
+... c a  F a'   F  a''       F
+ +
+
+
+
+
+
+
+

Each of the four variations above can be specialized to ana- and catamorphic forms.

+ +
+
+
+
+
+
In [23]:
+
+
+
def WTFmorphism(c, F, P, G):
+    '''Return a hylomorphism function H.'''
+
+    def H(a, d=c):
+        if P(a):
+            result = d
+        else:
+            a, b = G(a)
+            result = H(a, F(d, b))
+        return result
+
+    return H
+
+ +
+
+
+ +
+
+
+
In [24]:
+
+
+
F = lambda a, b: a + b
+P = lambda n: n <= 1
+G = lambda n: (n - 1, n - 1)
+
+wtf = WTFmorphism(0, F, P, G)
+
+print wtf(5)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10
+
+
+
+ +
+
+ +
+
+
+
+
+ +
H == [P   ] [pop c ] [G          ] [dip F    ] genrec
+ +
+
+
+
+
+
In [25]:
+
+
+
DefinitionWrapper.add_definitions('''
+P == 1 <=
+Ga == -- dup
+Gb == --
+c == 0
+F == +
+''', D)
+
+ +
+
+
+ +
+
+
+
In [26]:
+
+
+
V('[1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                                             . [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec
+                                                                                                     [1 2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec
+                                                                                              [1 2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec
+                                                                                     [1 2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec
+                                                                       [1 2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec
+                                                           [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec
+          [1 2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte
+[1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[1 2 3]] [[] =] . infra first choice i
+                                                                                                     [1 2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i
+                                                                                                  [1 2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i
+                                                                                                       False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i
+         False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] . swaack first choice i
+         [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i
+           [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i
+                          [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i
+                                                                                                     [1 2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons
+                                                                                                     1 [2 3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons
+                                                                                                     [2 3] 1 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons
+                                                  [2 3] 1 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons
+                                                                                                       [2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 1 swons
+                                                                                                [2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 1 swons
+                                                                                       [2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 1 swons
+                                                                         [2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 1 swons
+                                                             [2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 1 swons
+            [2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 1 swons
+    [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[2 3]] [[] =] . infra first choice i 1 swons
+                                                                                                       [2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons
+                                                                                                    [2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons
+                                                                                                       False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons
+           False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] . swaack first choice i 1 swons
+           [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons
+             [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons
+                            [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 1 swons
+                                                                                                       [2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons
+                                                                                                       2 [3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons
+                                                                                                       [3] 2 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons
+                                                    [3] 2 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 1 swons
+                                                                                                         [3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons
+                                                                                                  [3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons
+                                                                                         [3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 2 swons 1 swons
+                                                                           [3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 2 swons 1 swons
+                                                               [3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 2 swons 1 swons
+              [3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 2 swons 1 swons
+        [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[3]] [[] =] . infra first choice i 2 swons 1 swons
+                                                                                                         [3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons
+                                                                                                      [3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons
+                                                                                                       False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons
+             False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] . swaack first choice i 2 swons 1 swons
+             [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 1 swons
+               [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 1 swons
+                              [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 2 swons 1 swons
+                                                                                                         [3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons
+                                                                                                        3 [] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons
+                                                                                                        [] 3 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons
+                                                     [] 3 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 2 swons 1 swons
+                                                                                                          [] . [[] =] [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons
+                                                                                                   [] [[] =] . [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons
+                                                                                          [] [[] =] [pop []] . [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons
+                                                                            [] [[] =] [pop []] [uncons swap] . [dip swons] genrec 3 swons 2 swons 1 swons
+                                                                [] [[] =] [pop []] [uncons swap] [dip swons] . genrec 3 swons 2 swons 1 swons
+               [] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 3 swons 2 swons 1 swons
+          [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[]] [[] =] . infra first choice i 3 swons 2 swons 1 swons
+                                                                                                          [] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons
+                                                                                                       [] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons
+                                                                                                        True . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons
+               True [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] . swaack first choice i 3 swons 2 swons 1 swons
+               [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 3 swons 2 swons 1 swons
+                 [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] True . choice i 3 swons 2 swons 1 swons
+                                                                                                 [] [pop []] . i 3 swons 2 swons 1 swons
+                                                                                                          [] . pop [] 3 swons 2 swons 1 swons
+                                                                                                             . [] 3 swons 2 swons 1 swons
+                                                                                                          [] . 3 swons 2 swons 1 swons
+                                                                                                        [] 3 . swons 2 swons 1 swons
+                                                                                                        [] 3 . swap cons 2 swons 1 swons
+                                                                                                        3 [] . cons 2 swons 1 swons
+                                                                                                         [3] . 2 swons 1 swons
+                                                                                                       [3] 2 . swons 1 swons
+                                                                                                       [3] 2 . swap cons 1 swons
+                                                                                                       2 [3] . cons 1 swons
+                                                                                                       [2 3] . 1 swons
+                                                                                                     [2 3] 1 . swons
+                                                                                                     [2 3] 1 . swap cons
+                                                                                                     1 [2 3] . cons
+                                                                                                     [1 2 3] . 
+
+
+
+ +
+
+ +
+
+
+
In [27]:
+
+
+
V('3 [P] [pop c] [Ga] [dip F] genrec')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                               . 3 [P] [pop c] [Ga] [dip F] genrec
+                                                             3 . [P] [pop c] [Ga] [dip F] genrec
+                                                         3 [P] . [pop c] [Ga] [dip F] genrec
+                                                 3 [P] [pop c] . [Ga] [dip F] genrec
+                                            3 [P] [pop c] [Ga] . [dip F] genrec
+                                    3 [P] [pop c] [Ga] [dip F] . genrec
+    3 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte
+3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [3] [P] . infra first choice i
+                                                             3 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i
+                                                             3 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i
+                                                           3 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i
+                                                         False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i
+False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] . swaack first choice i
+3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i
+  3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i
+                3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i
+                                                             3 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F
+                                                             3 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F
+                                                             2 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F
+                                                           2 2 . [[P] [pop c] [Ga] [dip F] genrec] dip F
+                         2 2 [[P] [pop c] [Ga] [dip F] genrec] . dip F
+                                                             2 . [P] [pop c] [Ga] [dip F] genrec 2 F
+                                                         2 [P] . [pop c] [Ga] [dip F] genrec 2 F
+                                                 2 [P] [pop c] . [Ga] [dip F] genrec 2 F
+                                            2 [P] [pop c] [Ga] . [dip F] genrec 2 F
+                                    2 [P] [pop c] [Ga] [dip F] . genrec 2 F
+    2 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 2 F
+2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [2] [P] . infra first choice i 2 F
+                                                             2 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F
+                                                             2 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F
+                                                           2 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F
+                                                         False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F
+False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] . swaack first choice i 2 F
+2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F
+  2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i 2 F
+                2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i 2 F
+                                                             2 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F
+                                                             2 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F
+                                                             1 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F
+                                                           1 1 . [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F
+                         1 1 [[P] [pop c] [Ga] [dip F] genrec] . dip F 2 F
+                                                             1 . [P] [pop c] [Ga] [dip F] genrec 1 F 2 F
+                                                         1 [P] . [pop c] [Ga] [dip F] genrec 1 F 2 F
+                                                 1 [P] [pop c] . [Ga] [dip F] genrec 1 F 2 F
+                                            1 [P] [pop c] [Ga] . [dip F] genrec 1 F 2 F
+                                    1 [P] [pop c] [Ga] [dip F] . genrec 1 F 2 F
+    1 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 1 F 2 F
+1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [1] [P] . infra first choice i 1 F 2 F
+                                                             1 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F
+                                                             1 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F
+                                                           1 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F
+                                                          True . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F
+ True [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] . swaack first choice i 1 F 2 F
+ 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F
+   1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F
+                                                     1 [pop c] . i 1 F 2 F
+                                                             1 . pop c 1 F 2 F
+                                                               . c 1 F 2 F
+                                                               . 0 1 F 2 F
+                                                             0 . 1 F 2 F
+                                                           0 1 . F 2 F
+                                                           0 1 . + 2 F
+                                                             1 . 2 F
+                                                           1 2 . F
+                                                           1 2 . +
+                                                             3 . 
+
+
+
+ +
+
+ +
+
+
+
In [28]:
+
+
+
V('3 [P] [pop []] [Ga] [dip swons] genrec')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                         . 3 [P] [pop []] [Ga] [dip swons] genrec
+                                                                       3 . [P] [pop []] [Ga] [dip swons] genrec
+                                                                   3 [P] . [pop []] [Ga] [dip swons] genrec
+                                                          3 [P] [pop []] . [Ga] [dip swons] genrec
+                                                     3 [P] [pop []] [Ga] . [dip swons] genrec
+                                         3 [P] [pop []] [Ga] [dip swons] . genrec
+    3 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte
+3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [3] [P] . infra first choice i
+                                                                       3 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i
+                                                                       3 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i
+                                                                     3 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i
+                                                                   False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i
+False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] . swaack first choice i
+3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i
+  3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i
+                 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i
+                                                                       3 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons
+                                                                       3 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons
+                                                                       2 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons
+                                                                     2 2 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons
+                              2 2 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons
+                                                                       2 . [P] [pop []] [Ga] [dip swons] genrec 2 swons
+                                                                   2 [P] . [pop []] [Ga] [dip swons] genrec 2 swons
+                                                          2 [P] [pop []] . [Ga] [dip swons] genrec 2 swons
+                                                     2 [P] [pop []] [Ga] . [dip swons] genrec 2 swons
+                                         2 [P] [pop []] [Ga] [dip swons] . genrec 2 swons
+    2 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 2 swons
+2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [2] [P] . infra first choice i 2 swons
+                                                                       2 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons
+                                                                       2 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons
+                                                                     2 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons
+                                                                   False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons
+False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons
+2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons
+  2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons
+                 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i 2 swons
+                                                                       2 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons
+                                                                       2 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons
+                                                                       1 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons
+                                                                     1 1 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons
+                              1 1 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons 2 swons
+                                                                       1 . [P] [pop []] [Ga] [dip swons] genrec 1 swons 2 swons
+                                                                   1 [P] . [pop []] [Ga] [dip swons] genrec 1 swons 2 swons
+                                                          1 [P] [pop []] . [Ga] [dip swons] genrec 1 swons 2 swons
+                                                     1 [P] [pop []] [Ga] . [dip swons] genrec 1 swons 2 swons
+                                         1 [P] [pop []] [Ga] [dip swons] . genrec 1 swons 2 swons
+    1 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons
+1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [1] [P] . infra first choice i 1 swons 2 swons
+                                                                       1 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons
+                                                                       1 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons
+                                                                     1 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons
+                                                                    True . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons
+ True [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons
+ 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 1 swons 2 swons
+   1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] True . choice i 1 swons 2 swons
+                                                              1 [pop []] . i 1 swons 2 swons
+                                                                       1 . pop [] 1 swons 2 swons
+                                                                         . [] 1 swons 2 swons
+                                                                      [] . 1 swons 2 swons
+                                                                    [] 1 . swons 2 swons
+                                                                    [] 1 . swap cons 2 swons
+                                                                    1 [] . cons 2 swons
+                                                                     [1] . 2 swons
+                                                                   [1] 2 . swons
+                                                                   [1] 2 . swap cons
+                                                                   2 [1] . cons
+                                                                   [2 1] . 
+
+
+
+ +
+
+ +
+
+
+
In [29]:
+
+
+
V('[2 1] [[] =] [pop c ] [uncons swap] [dip F] genrec')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                               . [2 1] [[] =] [pop c] [uncons swap] [dip F] genrec
+                                                                                         [2 1] . [[] =] [pop c] [uncons swap] [dip F] genrec
+                                                                                  [2 1] [[] =] . [pop c] [uncons swap] [dip F] genrec
+                                                                          [2 1] [[] =] [pop c] . [uncons swap] [dip F] genrec
+                                                            [2 1] [[] =] [pop c] [uncons swap] . [dip F] genrec
+                                                    [2 1] [[] =] [pop c] [uncons swap] [dip F] . genrec
+        [2 1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte
+[2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[2 1]] [[] =] . infra first choice i
+                                                                                         [2 1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i
+                                                                                      [2 1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i
+                                                                                         False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i
+       False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] . swaack first choice i
+       [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i
+         [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i
+                       [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i
+                                                                                         [2 1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F
+                                                                                         2 [1] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F
+                                                                                         [1] 2 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F
+                                           [1] 2 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F
+                                                                                           [1] . [[] =] [pop c] [uncons swap] [dip F] genrec 2 F
+                                                                                    [1] [[] =] . [pop c] [uncons swap] [dip F] genrec 2 F
+                                                                            [1] [[] =] [pop c] . [uncons swap] [dip F] genrec 2 F
+                                                              [1] [[] =] [pop c] [uncons swap] . [dip F] genrec 2 F
+                                                      [1] [[] =] [pop c] [uncons swap] [dip F] . genrec 2 F
+          [1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 2 F
+    [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[1]] [[] =] . infra first choice i 2 F
+                                                                                           [1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F
+                                                                                        [1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F
+                                                                                         False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F
+         False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] . swaack first choice i 2 F
+         [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F
+           [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i 2 F
+                         [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i 2 F
+                                                                                           [1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F
+                                                                                          1 [] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F
+                                                                                          [] 1 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F
+                                            [] 1 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F 2 F
+                                                                                            [] . [[] =] [pop c] [uncons swap] [dip F] genrec 1 F 2 F
+                                                                                     [] [[] =] . [pop c] [uncons swap] [dip F] genrec 1 F 2 F
+                                                                             [] [[] =] [pop c] . [uncons swap] [dip F] genrec 1 F 2 F
+                                                               [] [[] =] [pop c] [uncons swap] . [dip F] genrec 1 F 2 F
+                                                       [] [[] =] [pop c] [uncons swap] [dip F] . genrec 1 F 2 F
+           [] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 1 F 2 F
+      [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[]] [[] =] . infra first choice i 1 F 2 F
+                                                                                            [] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F
+                                                                                         [] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F
+                                                                                          True . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F
+           True [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] . swaack first choice i 1 F 2 F
+           [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F
+             [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F
+                                                                                    [] [pop c] . i 1 F 2 F
+                                                                                            [] . pop c 1 F 2 F
+                                                                                               . c 1 F 2 F
+                                                                                               . 0 1 F 2 F
+                                                                                             0 . 1 F 2 F
+                                                                                           0 1 . F 2 F
+                                                                                           0 1 . + 2 F
+                                                                                             1 . 2 F
+                                                                                           1 2 . F
+                                                                                           1 2 . +
+                                                                                             3 . 
+
+
+
+ +
+
+ +
+
+
+
+
+

Appendix - Fun with Symbols

+
|[ (c, F), (G, P) ]| == (|c, F|) • [(G, P)]
+
+
+

"Bananas, Lenses, & Barbed Wire"

+ +
(|...|)  [(...)]  [<...>]
+
+
+

I think they are having slightly too much fun with the symbols.

+

"Too much is always better than not enough."

+ +
+
+
+
+
+
+
+

Tree with node and list of trees.

+
tree = [] | [node [tree*]]
+ +
+
+
+
+
+
+
+

treestep

+
tree z [C] [N] treestep
+
+
+   [] z [C] [N] treestep
+---------------------------
+      z
+
+
+   [node [tree*]] z [C] [N] treestep
+--------------------------------------- w/ K == z [C] [N] treestep
+       node N [tree*] [K] map C
+ +
+
+
+
+
+
+
+

Derive the recursive form.

+
K == [not] [pop z] [J] ifte
+
+
+       [node [tree*]] J
+------------------------------
+   node N [tree*] [K] map C
+
+
+J == .. [N] .. [K] .. [C] ..
+
+[node [tree*]] uncons [N] dip
+node [[tree*]]        [N] dip
+node N [[tree*]]
+
+node N [[tree*]] i [K] map
+node N  [tree*]    [K] map
+node N  [K.tree*]
+
+J == uncons [N] dip i [K] map [C] i
+
+K == [not] [pop z] [uncons [N] dip i [K] map [C] i] ifte
+K == [not] [pop z] [uncons [N] dip i]   [map [C] i] genrec
+ +
+
+
+
+
+
+
+

Extract the givens to parameterize the program.

+
[not] [pop z]                   [uncons [N] dip unquote] [map [C] i] genrec
+[not] [z]         [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec
+[not]  z     unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec
+z [not] swap unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec
+  \............TS0............/
+z TS0 [uncons [N] dip unquote]                      [map [C] i] genrec
+z [uncons [N] dip unquote]                [TS0] dip [map [C] i] genrec
+z [[N] dip unquote]      [uncons] swoncat [TS0] dip [map [C] i] genrec
+z [N] [dip unquote] cons [uncons] swoncat [TS0] dip [map [C] i] genrec
+      \...........TS1.................../
+z [N] TS1 [TS0] dip [map [C] i]                       genrec
+z [N]               [map [C] i]            [TS1 [TS0] dip] dip      genrec
+z [N]               [map  C   ]            [TS1 [TS0] dip] dip      genrec
+z [N]                    [C] [map] swoncat [TS1 [TS0] dip] dip genrec
+z [C] [N] swap               [map] swoncat [TS1 [TS0] dip] dip genrec
+ +
+
+
+
+
+
+
+ +
     TS0 == [not] swap unit [pop] swoncat
+     TS1 == [dip i] cons [uncons] swoncat
+treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec
+ +
+
+
+
+
+
+
+ +
   [] 0 [C] [N] treestep
+---------------------------
+      0
+
+
+      [n [tree*]] 0 [sum +] [] treestep
+   --------------------------------------------------
+       n [tree*] [0 [sum +] [] treestep] map sum +
+ +
+
+
+
+
+
In [40]:
+
+
+
DefinitionWrapper.add_definitions('''
+
+     TS0 == [not] swap unit [pop] swoncat
+     TS1 == [dip i] cons [uncons] swoncat
+treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec
+
+''', D)
+
+ +
+
+
+ +
+
+
+
In [31]:
+
+
+
V('[] 0 [sum +] [] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                                       . [] 0 [sum +] [] treestep
+                                                                                                    [] . 0 [sum +] [] treestep
+                                                                                                  [] 0 . [sum +] [] treestep
+                                                                                          [] 0 [sum +] . [] treestep
+                                                                                       [] 0 [sum +] [] . treestep
+                                                                                       [] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec
+                                                                                       [] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec
+                                                                                 [] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec
+                                                                                 [] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec
+                                                                                 [] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec
+                                                                                   [] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec
+                                                                   [] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec
+                                                                                               [] 0 [] . TS1 [TS0] dip [map sum +] genrec
+                                                                                               [] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                       [] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                       [] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                              [] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec
+                                                                              [] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec
+                                                                              [] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec
+                                                                                [] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec
+                                                                          [] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec
+                                                                                                  [] 0 . TS0 [uncons [] dip i] [map sum +] genrec
+                                                                                                  [] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                            [] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                            [] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                            [] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                         [] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                          [] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                    [] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                    [] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec
+                                                                                    [] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec
+                                                                                      [] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec
+                                                                    [] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec
+                                                        [] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec
+     [] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte
+[] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[]] [not] . infra first choice i
+                                                                                                    [] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i
+                                                                                                  True . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i
+    True [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] . swaack first choice i
+    [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [True] . first choice i
+      [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] True . choice i
+                                                                                            [] [pop 0] . i
+                                                                                                    [] . pop 0
+                                                                                                       . 0
+                                                                                                     0 . 
+
+
+
+ +
+
+ +
+
+
+
In [32]:
+
+
+
V('[23 []] 0 [sum +] [] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                                                 . [23 []] 0 [sum +] [] treestep
+                                                                                                         [23 []] . 0 [sum +] [] treestep
+                                                                                                       [23 []] 0 . [sum +] [] treestep
+                                                                                               [23 []] 0 [sum +] . [] treestep
+                                                                                            [23 []] 0 [sum +] [] . treestep
+                                                                                            [23 []] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec
+                                                                                            [23 []] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec
+                                                                                      [23 []] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec
+                                                                                      [23 []] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec
+                                                                                      [23 []] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec
+                                                                                        [23 []] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec
+                                                                        [23 []] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec
+                                                                                                    [23 []] 0 [] . TS1 [TS0] dip [map sum +] genrec
+                                                                                                    [23 []] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                            [23 []] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                            [23 []] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                   [23 []] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec
+                                                                                   [23 []] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec
+                                                                                   [23 []] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec
+                                                                                     [23 []] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec
+                                                                               [23 []] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec
+                                                                                                       [23 []] 0 . TS0 [uncons [] dip i] [map sum +] genrec
+                                                                                                       [23 []] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                 [23 []] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                 [23 []] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                 [23 []] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                              [23 []] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                               [23 []] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                         [23 []] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                         [23 []] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec
+                                                                                         [23 []] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec
+                                                                                           [23 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec
+                                                                         [23 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec
+                                                             [23 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec
+          [23 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte
+[23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 []]] [not] . infra first choice i
+                                                                                                         [23 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i
+                                                                                                           False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i
+        False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] . swaack first choice i
+        [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i
+          [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i
+                        [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i
+                                                                                                         [23 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                         23 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                      23 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                              23 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                         23 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                              23 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                           23 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                      23 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum +
+                                                                                                           23 [] . sum +
+                                                                                                           23 [] . 0 [+] catamorphism +
+                                                                                                         23 [] 0 . [+] catamorphism +
+                                                                                                     23 [] 0 [+] . catamorphism +
+                                                                                                     23 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism +
+                                                                                              23 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism +
+                                                                                              23 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism +
+                                                                                23 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism +
+                                                                                23 [] [[] =] 0 [uncons swap] [+] . hylomorphism +
+                                                                                23 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec +
+                                                           23 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec +
+                                                                                                  23 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                  23 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                               23 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                23 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                          23 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                          23 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                          23 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                            23 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec +
+                                                                              23 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec +
+                                                                          23 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec +
+                                                                    23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec +
+                                                                    23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec +
+                                                                    23 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec +
+                                                                      23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec +
+                          23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte +
+                  23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i +
+                                                                                                           23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i +
+                                                                                                        23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i +
+                                                                                                         23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i +
+                       23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i +
+                       23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i +
+                            23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i +
+                                                                                                   23 [] [pop 0] . i +
+                                                                                                           23 [] . pop 0 +
+                                                                                                              23 . 0 +
+                                                                                                            23 0 . +
+                                                                                                              23 . 
+
+
+
+ +
+
+ +
+
+
+
In [33]:
+
+
+
V('[23 [[2 []] [3 []]]] 0 [sum +] [] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
                                                                                                                                                                  . [23 [[2 []] [3 []]]] 0 [sum +] [] treestep
+                                                                                                                                             [23 [[2 []] [3 []]]] . 0 [sum +] [] treestep
+                                                                                                                                           [23 [[2 []] [3 []]]] 0 . [sum +] [] treestep
+                                                                                                                                   [23 [[2 []] [3 []]]] 0 [sum +] . [] treestep
+                                                                                                                                [23 [[2 []] [3 []]]] 0 [sum +] [] . treestep
+                                                                                                                                [23 [[2 []] [3 []]]] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec
+                                                                                                                                [23 [[2 []] [3 []]]] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec
+                                                                                                                          [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec
+                                                                                                                          [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec
+                                                                                                                          [23 [[2 []] [3 []]]] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec
+                                                                                                                            [23 [[2 []] [3 []]]] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec
+                                                                                                            [23 [[2 []] [3 []]]] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec
+                                                                                                                                        [23 [[2 []] [3 []]]] 0 [] . TS1 [TS0] dip [map sum +] genrec
+                                                                                                                                        [23 [[2 []] [3 []]]] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                                                                [23 [[2 []] [3 []]]] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                                                                [23 [[2 []] [3 []]]] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec
+                                                                                                                       [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec
+                                                                                                                       [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec
+                                                                                                                       [23 [[2 []] [3 []]]] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec
+                                                                                                                         [23 [[2 []] [3 []]]] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec
+                                                                                                                   [23 [[2 []] [3 []]]] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec
+                                                                                                                                           [23 [[2 []] [3 []]]] 0 . TS0 [uncons [] dip i] [map sum +] genrec
+                                                                                                                                           [23 [[2 []] [3 []]]] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                                     [23 [[2 []] [3 []]]] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                                     [23 [[2 []] [3 []]]] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                                     [23 [[2 []] [3 []]]] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                                  [23 [[2 []] [3 []]]] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                                   [23 [[2 []] [3 []]]] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                             [23 [[2 []] [3 []]]] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec
+                                                                                                                             [23 [[2 []] [3 []]]] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec
+                                                                                                                             [23 [[2 []] [3 []]]] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec
+                                                                                                                               [23 [[2 []] [3 []]]] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec
+                                                                                                             [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec
+                                                                                                 [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec
+                                              [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte
+                       [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 [[2 []] [3 []]]]] [not] . infra first choice i
+                                                                                                                                             [23 [[2 []] [3 []]]] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i
+                                                                                                                                                            False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i
+                                            False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] . swaack first choice i
+                                            [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i
+                                              [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i
+                                                            [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i
+                                                                                                                                             [23 [[2 []] [3 []]]] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                                                             23 [[[2 []] [3 []]]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                                                          23 [[[2 []] [3 []]]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                                                                               23 . [[[2 []] [3 []]]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                                                             23 [[[2 []] [3 []]]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                                                                               23 . [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                                                                               23 [[2 []] [3 []]] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +
+                                                                                          23 [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum +
+23 [] [[[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first] . infra sum +
+                                                                                                                                                                  . [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                      [[3 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                 [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 [3 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                  23 [3 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                          23 [3 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                        23 [3 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                            23 [3 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                         23 [3 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                             23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[3 []] 23] [not] . infra first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 [3 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                         23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                    23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] . swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                    23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                         23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                       23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 [3 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 3 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                     23 3 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                             23 3 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 3 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                             23 3 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                          23 3 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                     23 3 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                          23 3 [] . sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                          23 3 [] . 0 [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 3 [] 0 . [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                    23 3 [] 0 [+] . catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                    23 3 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                             23 3 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                             23 3 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                               23 3 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                               23 3 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                               23 3 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                          23 3 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                 23 3 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                 23 3 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                              23 3 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                               23 3 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                         23 3 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                         23 3 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                         23 3 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                           23 3 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                             23 3 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                         23 3 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                   23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                   23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                   23 3 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                     23 3 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                         23 3 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                               23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 3 23] [[] =] . infra first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                          23 3 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                       23 3 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                        23 3 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                    23 3 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] . swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                    23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 3 23] . first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                           23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                  23 3 [] [pop 0] . i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                          23 3 [] . pop 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                             23 3 . 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                           23 3 0 . + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                             23 3 . [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                          23 3 [] . swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                           [3 23] . first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                                3 . [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                                                                                    3 [[2 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +
+                                                                                               3 [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [23] swaack sum +
+                                                                                                                                                        23 [2 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum +
+                                                                                                                                                  23 [2 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum +
+                                                                                                                                          23 [2 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum +
+                                                                                                                        23 [2 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [3] swaack first [23] swaack sum +
+                                                                                                            23 [2 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [3] swaack first [23] swaack sum +
+                                                         23 [2 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [3] swaack first [23] swaack sum +
+                                             23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[2 []] 23] [not] . infra first choice i [3] swaack first [23] swaack sum +
+                                                                                                                                                        23 [2 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum +
+                                                                                                                                                         23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum +
+                                                    23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] . swaack first choice i [3] swaack first [23] swaack sum +
+                                                    23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [3] swaack first [23] swaack sum +
+                                                         23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [3] swaack first [23] swaack sum +
+                                                                       23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [3] swaack first [23] swaack sum +
+                                                                                                                                                        23 [2 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                        23 2 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                     23 2 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                             23 2 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                        23 2 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                             23 2 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                          23 2 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +
+                                                                                                     23 2 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                          23 2 [] . sum + [3] swaack first [23] swaack sum +
+                                                                                                                                                          23 2 [] . 0 [+] catamorphism + [3] swaack first [23] swaack sum +
+                                                                                                                                                        23 2 [] 0 . [+] catamorphism + [3] swaack first [23] swaack sum +
+                                                                                                                                                    23 2 [] 0 [+] . catamorphism + [3] swaack first [23] swaack sum +
+                                                                                                                                                    23 2 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum +
+                                                                                                                                             23 2 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum +
+                                                                                                                                             23 2 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum +
+                                                                                                                               23 2 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [3] swaack first [23] swaack sum +
+                                                                                                                               23 2 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [3] swaack first [23] swaack sum +
+                                                                                                                               23 2 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                          23 2 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                                 23 2 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                                 23 2 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                              23 2 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                               23 2 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                         23 2 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                         23 2 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                         23 2 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                                           23 2 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                             23 2 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                         23 2 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                   23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                   23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                   23 2 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [3] swaack first [23] swaack sum +
+                                                                                                                     23 2 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [3] swaack first [23] swaack sum +
+                                                                         23 2 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [3] swaack first [23] swaack sum +
+                                                               23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 2 23] [[] =] . infra first choice i + [3] swaack first [23] swaack sum +
+                                                                                                                                                          23 2 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum +
+                                                                                                                                                       23 2 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum +
+                                                                                                                                                        23 2 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum +
+                                                                    23 2 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] . swaack first choice i + [3] swaack first [23] swaack sum +
+                                                                    23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 2 23] . first choice i + [3] swaack first [23] swaack sum +
+                                                                           23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [3] swaack first [23] swaack sum +
+                                                                                                                                                  23 2 [] [pop 0] . i + [3] swaack first [23] swaack sum +
+                                                                                                                                                          23 2 [] . pop 0 + [3] swaack first [23] swaack sum +
+                                                                                                                                                             23 2 . 0 + [3] swaack first [23] swaack sum +
+                                                                                                                                                           23 2 0 . + [3] swaack first [23] swaack sum +
+                                                                                                                                                             23 2 . [3] swaack first [23] swaack sum +
+                                                                                                                                                         23 2 [3] . swaack first [23] swaack sum +
+                                                                                                                                                         3 [2 23] . first [23] swaack sum +
+                                                                                                                                                              3 2 . [23] swaack sum +
+                                                                                                                                                         3 2 [23] . swaack sum +
+                                                                                                                                                         23 [2 3] . sum +
+                                                                                                                                                         23 [2 3] . 0 [+] catamorphism +
+                                                                                                                                                       23 [2 3] 0 . [+] catamorphism +
+                                                                                                                                                   23 [2 3] 0 [+] . catamorphism +
+                                                                                                                                                   23 [2 3] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism +
+                                                                                                                                            23 [2 3] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism +
+                                                                                                                                            23 [2 3] [[] =] 0 [+] . [uncons swap] swap hylomorphism +
+                                                                                                                              23 [2 3] [[] =] 0 [+] [uncons swap] . swap hylomorphism +
+                                                                                                                              23 [2 3] [[] =] 0 [uncons swap] [+] . hylomorphism +
+                                                                                                                              23 [2 3] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec +
+                                                                                                         23 [2 3] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec +
+                                                                                                                                                23 [2 3] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                                23 [2 3] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                             23 [2 3] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                              23 [2 3] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                        23 [2 3] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                        23 [2 3] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                        23 [2 3] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                                          23 [2 3] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec +
+                                                                                                                            23 [2 3] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec +
+                                                                                                                        23 [2 3] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec +
+                                                                                                                  23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec +
+                                                                                                                  23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec +
+                                                                                                                  23 [2 3] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec +
+                                                                                                                    23 [2 3] [[] =] [pop 0] [uncons swap] [dip +] . genrec +
+                                                                        23 [2 3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte +
+                                                             23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 3] 23] [[] =] . infra first choice i +
+                                                                                                                                                         23 [2 3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i +
+                                                                                                                                                      23 [2 3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i +
+                                                                                                                                                         23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i +
+                                                                    23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] . swaack first choice i +
+                                                                    23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i +
+                                                                         23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i +
+                                                                                       23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i +
+                                                                                                                                                         23 [2 3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + +
+                                                                                                                                                         23 2 [3] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + +
+                                                                                                                                                         23 [3] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + +
+                                                                                                           23 [3] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + +
+                                                                                                                                                           23 [3] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + +
+                                                                                                                                                    23 [3] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + +
+                                                                                                                                            23 [3] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + +
+                                                                                                                              23 [3] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + +
+                                                                                                                      23 [3] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + +
+                                                                          23 [3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + +
+                                                                 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3] 23] [[] =] . infra first choice i 2 + +
+                                                                                                                                                           23 [3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + +
+                                                                                                                                                        23 [3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + +
+                                                                                                                                                         23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + +
+                                                                      23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] . swaack first choice i 2 + +
+                                                                      23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i 2 + +
+                                                                           23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + +
+                                                                                         23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + +
+                                                                                                                                                           23 [3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + +
+                                                                                                                                                          23 3 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + +
+                                                                                                                                                          23 [] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + +
+                                                                                                            23 [] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + +
+                                                                                                                                                            23 [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 2 + +
+                                                                                                                                                     23 [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 2 + +
+                                                                                                                                             23 [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 2 + +
+                                                                                                                               23 [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 2 + +
+                                                                                                                       23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 2 + +
+                                                                           23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 2 + +
+                                                                   23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i 3 + 2 + +
+                                                                                                                                                            23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + +
+                                                                                                                                                         23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + +
+                                                                                                                                                          23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + +
+                                                                        23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i 3 + 2 + +
+                                                                        23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i 3 + 2 + +
+                                                                             23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 3 + 2 + +
+                                                                                                                                                    23 [] [pop 0] . i 3 + 2 + +
+                                                                                                                                                            23 [] . pop 0 3 + 2 + +
+                                                                                                                                                               23 . 0 3 + 2 + +
+                                                                                                                                                             23 0 . 3 + 2 + +
+                                                                                                                                                           23 0 3 . + 2 + +
+                                                                                                                                                             23 3 . 2 + +
+                                                                                                                                                           23 3 2 . + +
+                                                                                                                                                             23 5 . +
+                                                                                                                                                               28 . 
+
+
+
+ +
+
+ +
+
+
+
In [34]:
+
+
+
J('[23 [[2 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]] [3 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]]]] 0 [sum +] [] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
140
+
+
+
+ +
+
+ +
+
+
+
In [35]:
+
+
+
J('[] [] [unit cons] [23 +] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[]
+
+
+
+ +
+
+ +
+
+
+
In [36]:
+
+
+
J('[23 []] [] [unit cons] [23 +] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[46 []]
+
+
+
+ +
+
+ +
+
+
+
In [37]:
+
+
+
J('[23 [[2 []] [3 []]]] [] [unit cons] [23 +] treestep')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[46 [[25 []] [26 []]]]
+
+
+
+ +
+
+ +
+
+
+
In [38]:
+
+
+
define('treemap == [] [unit cons] roll< treestep')
+
+ +
+
+
+ +
+
+
+
In [39]:
+
+
+
J('[23 [[2 []] [3 []]]] [23 +] treemap')
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[46 [[25 []] [26 []]]]
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.ipynb b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.ipynb new file mode 100644 index 0000000..fde17ad --- /dev/null +++ b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.ipynb @@ -0,0 +1,2987 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Cf. [\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# [Hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29)\n", + "A [hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29) `H :: A -> B` converts a value of type A into a value of type B by means of:\n", + "\n", + "- A generator `G :: A -> (A, B)`\n", + "- A combiner `F :: (B, B) -> B`\n", + "- A predicate `P :: A -> Bool` to detect the base case\n", + "- A base case value `c :: B`\n", + "- Recursive calls (zero or more); it has a \"call stack in the form of a cons list\".\n", + "\n", + "It may be helpful to see this function implemented in imperative Python code." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def hylomorphism(c, F, P, G):\n", + " '''Return a hylomorphism function H.'''\n", + "\n", + " def H(a):\n", + " if P(a):\n", + " result = c\n", + " else:\n", + " b, aa = G(a)\n", + " result = F(b, H(aa))\n", + " return result\n", + "\n", + " return H" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding [Triangular Numbers](https://en.wikipedia.org/wiki/Triangular_number)\n", + "As a concrete example let's use a function that, given a positive integer, returns the sum of all positive integers less than that one. (In this case the types A and B are both `int`.)\n", + "### With `range()` and `sum()`" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = range(10)\n", + "r" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "45" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "45" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range_sum = lambda n: sum(range(n))\n", + "range_sum(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### As a hylomorphism" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "G = lambda n: (n - 1, n - 1)\n", + "F = lambda a, b: a + b\n", + "P = lambda n: n <= 1\n", + "\n", + "H = hylomorphism(0, F, P, G)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "45" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "H(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you were to run the above code in a debugger and check out the call stack you would find that the variable `b` in each call to `H()` is storing the intermediate values as `H()` recurses. This is what was meant by \"call stack in the form of a cons list\"." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Joy Preamble" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "from notebook_preamble import D, DefinitionWrapper, J, V, define" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Hylomorphism in Joy\n", + "We can define a combinator `hylomorphism` that will make a hylomorphism combinator `H` from constituent parts.\n", + "\n", + " H == c [F] [P] [G] hylomorphism\n", + "\n", + "The function `H` is recursive, so we start with `ifte` and set the else-part to\n", + "some function `J` that will contain a quoted copy of `H`. (The then-part just\n", + "discards the leftover `a` and replaces it with the base case value `c`.)\n", + "\n", + " H == [P] [pop c] [J] ifte\n", + "\n", + "The else-part `J` gets just the argument `a` on the stack.\n", + "\n", + " a J\n", + " a G The first thing to do is use the generator G\n", + " aa b which produces b and a new aa\n", + " aa b [H] dip we recur with H on the new aa\n", + " aa H b F and run F on the result.\n", + "\n", + "This gives us a definition for `J`.\n", + "\n", + " J == G [H] dip F\n", + "\n", + "Plug it in and convert to genrec.\n", + "\n", + " H == [P] [pop c] [G [H] dip F] ifte\n", + " H == [P] [pop c] [G] [dip F] genrec\n", + "\n", + "This is the form of a hylomorphism in Joy, which nicely illustrates that\n", + "it is a simple specialization of the general recursion combinator.\n", + "\n", + " H == [P] [pop c] [G] [dip F] genrec" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Derivation of `hylomorphism`\n", + "\n", + "Now we just need to derive a definition that builds the `genrec` arguments\n", + "out of the pieces given to the `hylomorphism` combinator.\n", + "\n", + " H == [P] [pop c] [G] [dip F] genrec\n", + " [P] [c] [pop] swoncat [G] [F] [dip] swoncat genrec\n", + " [P] c unit [pop] swoncat [G] [F] [dip] swoncat genrec\n", + " [P] c [G] [F] [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + "\n", + "Working in reverse:\n", + "- Use `swoncat` twice to decouple `[c]` and `[F]`.\n", + "- Use `unit` to dequote `c`.\n", + "- Use `dipd` to untangle `[unit [pop] swoncat]` from the givens.\n", + "\n", + "At this point all of the arguments (givens) to the hylomorphism are to the left so we have\n", + "a definition for `hylomorphism`:\n", + "\n", + " hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + "\n", + "The order of parameters is different than the one we started with but\n", + "that hardly matters, you can rearrange them or just supply them in the\n", + "expected order.\n", + "\n", + " [P] c [G] [F] hylomorphism == H\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Demonstrate summing a range of integers from 0 to n-1.\n", + "\n", + "- `[P]` is `[0 <=]`\n", + "- `c` is `0`\n", + "- `[G]` is `[1 - dup]`\n", + "- `[F]` is `[+]`\n", + "\n", + "So to sum the positive integers less than five we can do this." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 5 [0 <=] 0 [1 - dup] [+] hylomorphism\n", + " 5 . [0 <=] 0 [1 - dup] [+] hylomorphism\n", + " 5 [0 <=] . 0 [1 - dup] [+] hylomorphism\n", + " 5 [0 <=] 0 . [1 - dup] [+] hylomorphism\n", + " 5 [0 <=] 0 [1 - dup] . [+] hylomorphism\n", + " 5 [0 <=] 0 [1 - dup] [+] . hylomorphism\n", + " 5 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + " 5 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec\n", + " 5 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec\n", + " 5 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec\n", + " 5 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec\n", + " 5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec\n", + " 5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec\n", + " 5 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec\n", + " 5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec\n", + " 5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte\n", + "5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i\n", + " 5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i\n", + " 5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i\n", + " 5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip +\n", + " 4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 +\n", + " 4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 +\n", + " 4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 +\n", + " 4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 +\n", + " 4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 +\n", + " 4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 +\n", + "4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 +\n", + " 4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 +\n", + " 4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 +\n", + " 4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +\n", + " 4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +\n", + " 3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +\n", + " 3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 +\n", + " 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 +\n", + " 3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 +\n", + " 3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 +\n", + " 3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 +\n", + " 3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 +\n", + " 3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 +\n", + " 3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 +\n", + "3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 +\n", + " 3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 +\n", + " 3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 +\n", + " 3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +\n", + " 3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +\n", + " 2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +\n", + " 2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 +\n", + " 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 +\n", + " 2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 +\n", + " 2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 +\n", + " 2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 +\n", + " 2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 +\n", + " 2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 +\n", + " 2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 +\n", + "2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 +\n", + " 2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 +\n", + " 2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 +\n", + " 2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +\n", + " 2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +\n", + " 1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +\n", + " 1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 +\n", + " 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 +\n", + " 1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 +\n", + " 1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 +\n", + " 1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 +\n", + " 1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 +\n", + " 1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 +\n", + " 1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 +\n", + "1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 +\n", + " 1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 +\n", + " 1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 +\n", + " 1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +\n", + " 1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +\n", + " 0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +\n", + " 0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 +\n", + " 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 +\n", + " 0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 +\n", + " 0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 +\n", + " 0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 +\n", + " 0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 +\n", + " 0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 +\n", + " 0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 +\n", + "0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 +\n", + " 0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 +\n", + " 0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 +\n", + " True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 +\n", + " True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 +\n", + " 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 +\n", + " 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 +\n", + " 0 [pop 0] . i 0 + 1 + 2 + 3 + 4 +\n", + " 0 . pop 0 0 + 1 + 2 + 3 + 4 +\n", + " . 0 0 + 1 + 2 + 3 + 4 +\n", + " 0 . 0 + 1 + 2 + 3 + 4 +\n", + " 0 0 . + 1 + 2 + 3 + 4 +\n", + " 0 . 1 + 2 + 3 + 4 +\n", + " 0 1 . + 2 + 3 + 4 +\n", + " 1 . 2 + 3 + 4 +\n", + " 1 2 . + 3 + 4 +\n", + " 3 . 3 + 4 +\n", + " 3 3 . + 4 +\n", + " 6 . 4 +\n", + " 6 4 . +\n", + " 10 . \n" + ] + } + ], + "source": [ + "V('5 [0 <=] 0 [1 - dup] [+] hylomorphism')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Anamorphism\n", + "An anamorphism can be defined as a hylomorphism that uses `[]` for `c` and\n", + "`swons` for `F`.\n", + "\n", + " [P] [G] anamorphism == [P] [] [G] [swons] hylomorphism == A\n", + "\n", + "This allows us to define an anamorphism combinator in terms of\n", + "the hylomorphism combinator.\n", + "\n", + " [] swap [swons] hylomorphism == anamorphism\n", + "\n", + "Partial evaluation gives us a \"pre-cooked\" form.\n", + "\n", + " [P] [G] . anamorphism\n", + " [P] [G] . [] swap [swons] hylomorphism\n", + " [P] [G] [] . swap [swons] hylomorphism\n", + " [P] [] [G] . [swons] hylomorphism\n", + " [P] [] [G] [swons] . hylomorphism\n", + " [P] [] [G] [swons] . [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + " [P] [] [G] [swons] [unit [pop] swoncat] . dipd [dip] swoncat genrec\n", + " [P] [] . unit [pop] swoncat [G] [swons] [dip] swoncat genrec\n", + " [P] [[]] [pop] . swoncat [G] [swons] [dip] swoncat genrec\n", + " [P] [pop []] [G] [swons] [dip] . swoncat genrec\n", + "\n", + " [P] [pop []] [G] [dip swons] genrec\n", + "\n", + "(We could also have just substituted for `c` and `F` in the definition of `H`.)\n", + "\n", + " H == [P] [pop c ] [G] [dip F ] genrec\n", + " A == [P] [pop []] [G] [dip swons] genrec\n", + "\n", + "The partial evaluation is overkill in this case but it serves as a\n", + "reminder that this sort of program specialization can, in many cases, be\n", + "carried out automatically.)\n", + "\n", + "Untangle `[G]` from `[pop []]` using `swap`.\n", + "\n", + " [P] [G] [pop []] swap [dip swons] genrec\n", + "\n", + "All of the arguments to `anamorphism` are to the left, so we have a definition for it.\n", + "\n", + " anamorphism == [pop []] swap [dip swons] genrec\n", + "\n", + "An example of an anamorphism is the range function.\n", + "\n", + " range == [0 <=] [1 - dup] anamorphism\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Catamorphism\n", + "A catamorphism can be defined as a hylomorphism that uses `[uncons swap]` for `[G]`\n", + "and `[[] =]` for the predicate `[P]`.\n", + "\n", + " c [F] catamorphism == [[] =] c [uncons swap] [F] hylomorphism == C\n", + "\n", + "This allows us to define a `catamorphism` combinator in terms of\n", + "the `hylomorphism` combinator.\n", + "\n", + " [[] =] roll> [uncons swap] swap hylomorphism == catamorphism\n", + " \n", + "Partial evaluation doesn't help much.\n", + "\n", + " c [F] . catamorphism\n", + " c [F] . [[] =] roll> [uncons swap] swap hylomorphism\n", + " c [F] [[] =] . roll> [uncons swap] swap hylomorphism\n", + " [[] =] c [F] [uncons swap] . swap hylomorphism\n", + " [[] =] c [uncons swap] [F] . hylomorphism\n", + " [[] =] c [uncons swap] [F] [unit [pop] swoncat] . dipd [dip] swoncat genrec\n", + " [[] =] c . unit [pop] swoncat [uncons swap] [F] [dip] swoncat genrec\n", + " [[] =] [c] [pop] . swoncat [uncons swap] [F] [dip] swoncat genrec\n", + " [[] =] [pop c] [uncons swap] [F] [dip] . swoncat genrec\n", + " [[] =] [pop c] [uncons swap] [dip F] genrec\n", + "\n", + "Because the arguments to catamorphism have to be prepared (unlike the arguments\n", + "to anamorphism, which only need to be rearranged slightly) there isn't much point\n", + "to \"pre-cooking\" the definition.\n", + "\n", + " catamorphism == [[] =] roll> [uncons swap] swap hylomorphism\n", + "\n", + "An example of a catamorphism is the sum function.\n", + "\n", + " sum == 0 [+] catamorphism" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### \"Fusion Law\" for catas (UNFINISHED!!!)\n", + "\n", + "I'm not sure exactly how to translate the \"Fusion Law\" for catamorphisms into Joy.\n", + "\n", + "I know that a `map` composed with a cata can be expressed as a new cata:\n", + "\n", + " [F] map b [B] cata == b [F B] cata\n", + "\n", + "But this isn't the one described in \"Bananas...\". That's more like:\n", + "\n", + "A cata composed with some function can be expressed as some other cata:\n", + "\n", + " b [B] catamorphism F == c [C] catamorphism\n", + "\n", + "Given:\n", + "\n", + " b F == c\n", + "\n", + " ...\n", + "\n", + " B F == [F] dip C\n", + "\n", + " ...\n", + "\n", + " b[B]cata F == c[C]cata\n", + "\n", + " F(B(head, tail)) == C(head, F(tail))\n", + "\n", + " 1 [2 3] B F 1 [2 3] F C\n", + "\n", + "\n", + " b F == c\n", + " B F == F C\n", + "\n", + " b [B] catamorphism F == c [C] catamorphism\n", + " b [B] catamorphism F == b F [C] catamorphism\n", + "\n", + " ...\n", + "\n", + "Or maybe,\n", + "\n", + " [F] map b [B] cata == c [C] cata ???\n", + "\n", + " [F] map b [B] cata == b [F B] cata I think this is generally true, unless F consumes stack items\n", + " instead of just transforming TOS. Of course, there's always [F] unary.\n", + " b [F] unary [[F] unary B] cata\n", + "\n", + " [10 *] map 0 swap [+] step == 0 swap [10 * +] step\n", + "\n", + "\n", + "For example:\n", + "\n", + " F == 10 *\n", + " b == 0\n", + " B == +\n", + " c == 0\n", + " C == F +\n", + " \n", + " b F == c\n", + " 0 10 * == 0\n", + "\n", + " B F == [F] dip C\n", + " + 10 * == [10 *] dip F +\n", + " + 10 * == [10 *] dip 10 * +\n", + "\n", + " n m + 10 * == 10(n+m)\n", + "\n", + " n m [10 *] dip 10 * +\n", + " n 10 * m 10 * +\n", + " 10n m 10 * +\n", + " 10n 10m +\n", + " 10n+10m\n", + "\n", + " 10n+10m = 10(n+m)\n", + "\n", + "Ergo:\n", + "\n", + " 0 [+] catamorphism 10 * == 0 [10 * +] catamorphism" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## The `step` combinator will usually be better to use than `catamorphism`.\n", + "\n", + " sum == 0 swap [+] step\n", + " sum == 0 [+] catamorphism" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# anamorphism catamorphism == hylomorphism\n", + "Here is (part of) the payoff.\n", + "\n", + "An anamorphism followed by (composed with) a\n", + "catamorphism is a hylomorphism, with the advantage that the hylomorphism \n", + "does not create the intermediate list structure. The values are stored in\n", + "either the call stack, for those implementations that use one, or in the pending\n", + "expression (\"continuation\") for the Joypy interpreter. They still have to \n", + "be somewhere, converting from an anamorphism and catamorphism to a hylomorphism\n", + "just prevents using additional storage and doing additional processing.\n", + "\n", + " range == [0 <=] [1 - dup] anamorphism\n", + " sum == 0 [+] catamorphism\n", + "\n", + " range sum == [0 <=] [1 - dup] anamorphism 0 [+] catamorphism\n", + " == [0 <=] 0 [1 - dup] [+] hylomorphism\n", + "\n", + "We can let the `hylomorphism` combinator build `range_sum` for us or just\n", + "substitute ourselves.\n", + "\n", + " H == [P] [pop c] [G] [dip F] genrec\n", + " range_sum == [0 <=] [pop 0] [1 - dup] [dip +] genrec\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "defs = '''\n", + "anamorphism == [pop []] swap [dip swons] genrec\n", + "hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + "catamorphism == [[] =] roll> [uncons swap] swap hylomorphism\n", + "range == [0 <=] [1 - dup] anamorphism\n", + "sum == 0 [+] catamorphism\n", + "range_sum == [0 <=] 0 [1 - dup] [+] hylomorphism\n", + "'''\n", + "\n", + "DefinitionWrapper.add_definitions(defs, D)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ], + "source": [ + "J('10 range')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "45\n" + ] + } + ], + "source": [ + "J('[9 8 7 6 5 4 3 2 1 0] sum')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 10 range sum\n", + " 10 . range sum\n", + " 10 . [0 <=] [1 - dup] anamorphism sum\n", + " 10 [0 <=] . [1 - dup] anamorphism sum\n", + " 10 [0 <=] [1 - dup] . anamorphism sum\n", + " 10 [0 <=] [1 - dup] . [pop []] swap [dip swons] genrec sum\n", + " 10 [0 <=] [1 - dup] [pop []] . swap [dip swons] genrec sum\n", + " 10 [0 <=] [pop []] [1 - dup] . [dip swons] genrec sum\n", + " 10 [0 <=] [pop []] [1 - dup] [dip swons] . genrec sum\n", + " 10 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte sum\n", + " 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [10] [0 <=] . infra first choice i sum\n", + " 10 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum\n", + " 10 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] . swaack first choice i sum\n", + " 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i sum\n", + " 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i sum\n", + " 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i sum\n", + " 10 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum\n", + " 10 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum\n", + " 9 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum\n", + " 9 9 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum\n", + " 9 9 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons sum\n", + " 9 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 9 swons sum\n", + " 9 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 9 swons sum\n", + " 9 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 9 swons sum\n", + " 9 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 9 swons sum\n", + " 9 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 9 swons sum\n", + " 9 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 9 swons sum\n", + " 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [9] [0 <=] . infra first choice i 9 swons sum\n", + " 9 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum\n", + " 9 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] . swaack first choice i 9 swons sum\n", + " 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 9 swons sum\n", + " 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 9 swons sum\n", + " 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 9 swons sum\n", + " 9 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum\n", + " 9 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum\n", + " 8 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum\n", + " 8 8 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum\n", + " 8 8 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 9 swons sum\n", + " 8 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum\n", + " 8 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum\n", + " 8 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 8 swons 9 swons sum\n", + " 8 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 8 swons 9 swons sum\n", + " 8 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 8 swons 9 swons sum\n", + " 8 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 8 swons 9 swons sum\n", + " 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [8] [0 <=] . infra first choice i 8 swons 9 swons sum\n", + " 8 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum\n", + " 8 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] . swaack first choice i 8 swons 9 swons sum\n", + " 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 8 swons 9 swons sum\n", + " 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 8 swons 9 swons sum\n", + " 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 8 swons 9 swons sum\n", + " 8 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum\n", + " 8 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum\n", + " 7 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum\n", + " 7 7 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum\n", + " 7 7 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 8 swons 9 swons sum\n", + " 7 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum\n", + " 7 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum\n", + " 7 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum\n", + " 7 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 7 swons 8 swons 9 swons sum\n", + " 7 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 7 swons 8 swons 9 swons sum\n", + " 7 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 7 swons 8 swons 9 swons sum\n", + " 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [7] [0 <=] . infra first choice i 7 swons 8 swons 9 swons sum\n", + " 7 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum\n", + " 7 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] . swaack first choice i 7 swons 8 swons 9 swons sum\n", + " 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 7 swons 8 swons 9 swons sum\n", + " 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 7 swons 8 swons 9 swons sum\n", + " 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 7 swons 8 swons 9 swons sum\n", + " 7 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum\n", + " 7 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum\n", + " 6 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum\n", + " 6 6 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum\n", + " 6 6 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 7 swons 8 swons 9 swons sum\n", + " 6 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [6] [0 <=] . infra first choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] . swaack first choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 6 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 5 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 5 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [5] [0 <=] . infra first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] . swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 4 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 4 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [4] [0 <=] . infra first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] . swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 3 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 3 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [3] [0 <=] . infra first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] . swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 2 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 2 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [2] [0 <=] . infra first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 1 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 1 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [1] [0 <=] . infra first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 0 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 0 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [0] [0 <=] . infra first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " True . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " True [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] . swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] True . choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [pop []] . i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 . pop [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " . [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [] . 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [] 0 . swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [] 0 . swap cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 0 [] . cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [0] . 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [0] 1 . swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [0] 1 . swap cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 1 [0] . cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [1 0] . 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [1 0] 2 . swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [1 0] 2 . swap cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 2 [1 0] . cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [2 1 0] . 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [2 1 0] 3 . swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [2 1 0] 3 . swap cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 3 [2 1 0] . cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [3 2 1 0] . 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [3 2 1 0] 4 . swons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [3 2 1 0] 4 . swap cons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " 4 [3 2 1 0] . cons 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [4 3 2 1 0] . 5 swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [4 3 2 1 0] 5 . swons 6 swons 7 swons 8 swons 9 swons sum\n", + " [4 3 2 1 0] 5 . swap cons 6 swons 7 swons 8 swons 9 swons sum\n", + " 5 [4 3 2 1 0] . cons 6 swons 7 swons 8 swons 9 swons sum\n", + " [5 4 3 2 1 0] . 6 swons 7 swons 8 swons 9 swons sum\n", + " [5 4 3 2 1 0] 6 . swons 7 swons 8 swons 9 swons sum\n", + " [5 4 3 2 1 0] 6 . swap cons 7 swons 8 swons 9 swons sum\n", + " 6 [5 4 3 2 1 0] . cons 7 swons 8 swons 9 swons sum\n", + " [6 5 4 3 2 1 0] . 7 swons 8 swons 9 swons sum\n", + " [6 5 4 3 2 1 0] 7 . swons 8 swons 9 swons sum\n", + " [6 5 4 3 2 1 0] 7 . swap cons 8 swons 9 swons sum\n", + " 7 [6 5 4 3 2 1 0] . cons 8 swons 9 swons sum\n", + " [7 6 5 4 3 2 1 0] . 8 swons 9 swons sum\n", + " [7 6 5 4 3 2 1 0] 8 . swons 9 swons sum\n", + " [7 6 5 4 3 2 1 0] 8 . swap cons 9 swons sum\n", + " 8 [7 6 5 4 3 2 1 0] . cons 9 swons sum\n", + " [8 7 6 5 4 3 2 1 0] . 9 swons sum\n", + " [8 7 6 5 4 3 2 1 0] 9 . swons sum\n", + " [8 7 6 5 4 3 2 1 0] 9 . swap cons sum\n", + " 9 [8 7 6 5 4 3 2 1 0] . cons sum\n", + " [9 8 7 6 5 4 3 2 1 0] . sum\n", + " [9 8 7 6 5 4 3 2 1 0] . 0 [+] catamorphism\n", + " [9 8 7 6 5 4 3 2 1 0] 0 . [+] catamorphism\n", + " [9 8 7 6 5 4 3 2 1 0] 0 [+] . catamorphism\n", + " [9 8 7 6 5 4 3 2 1 0] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism\n", + " [9 8 7 6 5 4 3 2 1 0] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] . [uncons swap] swap hylomorphism\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] [uncons swap] . swap hylomorphism\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . hylomorphism\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec\n", + " [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte\n", + "[9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[9 8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i\n", + " [9 8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i\n", + " [9 8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] . swaack first choice i\n", + " [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i\n", + " [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i\n", + " [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i\n", + " [9 8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +\n", + " 9 [8 7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +\n", + " [8 7 6 5 4 3 2 1 0] 9 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +\n", + " [8 7 6 5 4 3 2 1 0] 9 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip +\n", + " [8 7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 9 +\n", + " [8 7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 9 +\n", + " [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 9 +\n", + " [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 9 +\n", + " [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 9 +\n", + " [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 9 +\n", + " [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 9 +\n", + " [8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 +\n", + " [8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] . swaack first choice i 9 +\n", + " [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 +\n", + " [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 9 +\n", + " [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 9 +\n", + " [8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 +\n", + " 8 [7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 +\n", + " [7 6 5 4 3 2 1 0] 8 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 +\n", + " [7 6 5 4 3 2 1 0] 8 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 9 +\n", + " [7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] . swaack first choice i 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 8 + 9 +\n", + " [7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 +\n", + " 7 [6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 +\n", + " [6 5 4 3 2 1 0] 7 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 +\n", + " [6 5 4 3 2 1 0] 7 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 8 + 9 +\n", + " [6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[6 5 4 3 2 1 0]] [[] =] . infra first choice i 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] . swaack first choice i 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 7 + 8 + 9 +\n", + " [6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " 6 [5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] 6 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] 6 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[5 4 3 2 1 0]] [[] =] . infra first choice i 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] . swaack first choice i 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 +\n", + " [5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " 5 [4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] 5 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] 5 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[4 3 2 1 0]] [[] =] . infra first choice i 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] . swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 +\n", + " [4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] 4 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] 4 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3 2 1 0]] [[] =] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 1 0]] [[] =] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[1 0]] [[] =] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] 1 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] 1 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[0]] [[] =] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] 0 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] 0 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[]] [[] =] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " [] . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 3 . + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 6 . 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 6 4 . + 5 + 6 + 7 + 8 + 9 +\n", + " 10 . 5 + 6 + 7 + 8 + 9 +\n", + " 10 5 . + 6 + 7 + 8 + 9 +\n", + " 15 . 6 + 7 + 8 + 9 +\n", + " 15 6 . + 7 + 8 + 9 +\n", + " 21 . 7 + 8 + 9 +\n", + " 21 7 . + 8 + 9 +\n", + " 28 . 8 + 9 +\n", + " 28 8 . + 9 +\n", + " 36 . 9 +\n", + " 36 9 . +\n", + " 45 . \n" + ] + } + ], + "source": [ + "V('10 range sum')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 10 range_sum\n", + " 10 . range_sum\n", + " 10 . [0 <=] 0 [1 - dup] [+] hylomorphism\n", + " 10 [0 <=] . 0 [1 - dup] [+] hylomorphism\n", + " 10 [0 <=] 0 . [1 - dup] [+] hylomorphism\n", + " 10 [0 <=] 0 [1 - dup] . [+] hylomorphism\n", + " 10 [0 <=] 0 [1 - dup] [+] . hylomorphism\n", + " 10 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec\n", + " 10 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec\n", + " 10 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec\n", + " 10 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec\n", + " 10 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec\n", + " 10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec\n", + " 10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec\n", + " 10 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec\n", + " 10 [0 <=] [pop 0] [1 - dup] [dip +] . genrec\n", + " 10 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte\n", + "10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [10] [0 <=] . infra first choice i\n", + " 10 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i\n", + " 10 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] . swaack first choice i\n", + " 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i\n", + " 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i\n", + " 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i\n", + " 10 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 10 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 9 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 9 9 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +\n", + " 9 9 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip +\n", + " 9 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 9 +\n", + " 9 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 9 +\n", + " 9 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 9 +\n", + " 9 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 9 +\n", + " 9 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 9 +\n", + " 9 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 9 +\n", + " 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [9] [0 <=] . infra first choice i 9 +\n", + " 9 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 +\n", + " 9 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] . swaack first choice i 9 +\n", + " 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 +\n", + " 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 9 +\n", + " 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 9 +\n", + " 9 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +\n", + " 9 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +\n", + " 8 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +\n", + " 8 8 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 +\n", + " 8 8 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 9 +\n", + " 8 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 8 + 9 +\n", + " 8 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 8 + 9 +\n", + " 8 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 8 + 9 +\n", + " 8 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 8 + 9 +\n", + " 8 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 8 + 9 +\n", + " 8 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 8 + 9 +\n", + " 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [8] [0 <=] . infra first choice i 8 + 9 +\n", + " 8 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 +\n", + " 8 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] . swaack first choice i 8 + 9 +\n", + " 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 +\n", + " 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 +\n", + " 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 8 + 9 +\n", + " 8 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +\n", + " 8 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +\n", + " 7 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +\n", + " 7 7 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 +\n", + " 7 7 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 8 + 9 +\n", + " 7 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 +\n", + " 7 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 +\n", + " 7 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 7 + 8 + 9 +\n", + " 7 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 7 + 8 + 9 +\n", + " 7 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 7 + 8 + 9 +\n", + " 7 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 7 + 8 + 9 +\n", + " 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [7] [0 <=] . infra first choice i 7 + 8 + 9 +\n", + " 7 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 +\n", + " 7 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] . swaack first choice i 7 + 8 + 9 +\n", + " 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 +\n", + " 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 +\n", + " 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 7 + 8 + 9 +\n", + " 7 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " 7 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " 6 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " 6 6 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 +\n", + " 6 6 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 7 + 8 + 9 +\n", + " 6 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 +\n", + " 6 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 +\n", + " 6 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 +\n", + " 6 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 6 + 7 + 8 + 9 +\n", + " 6 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 6 + 7 + 8 + 9 +\n", + " 6 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 +\n", + " 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [6] [0 <=] . infra first choice i 6 + 7 + 8 + 9 +\n", + " 6 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 +\n", + " 6 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] . swaack first choice i 6 + 7 + 8 + 9 +\n", + " 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 +\n", + " 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 +\n", + " 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 +\n", + " 6 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " 6 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " 5 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " 5 5 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 +\n", + " 5 5 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 6 + 7 + 8 + 9 +\n", + " 5 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " 5 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " 5 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " 5 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 5 + 6 + 7 + 8 + 9 +\n", + " 5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 5 + 6 + 7 + 8 + 9 +\n", + " 5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 +\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i 5 + 6 + 7 + 8 + 9 +\n", + " 5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " 5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i 5 + 6 + 7 + 8 + 9 +\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 +\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 +\n", + " 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 +\n", + " 5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " 5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " 4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " 4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 +\n", + " 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 +\n", + " 4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 3 3 . + 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 6 . 4 + 5 + 6 + 7 + 8 + 9 +\n", + " 6 4 . + 5 + 6 + 7 + 8 + 9 +\n", + " 10 . 5 + 6 + 7 + 8 + 9 +\n", + " 10 5 . + 6 + 7 + 8 + 9 +\n", + " 15 . 6 + 7 + 8 + 9 +\n", + " 15 6 . + 7 + 8 + 9 +\n", + " 21 . 7 + 8 + 9 +\n", + " 21 7 . + 8 + 9 +\n", + " 28 . 8 + 9 +\n", + " 28 8 . + 9 +\n", + " 36 . 9 +\n", + " 36 9 . +\n", + " 45 . \n" + ] + } + ], + "source": [ + "V('10 range_sum')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Factorial Function and Paramorphisms\n", + "A paramorphism `P :: B -> A` is a recursion combinator that uses `dup` on intermediate values.\n", + "\n", + " n swap [P] [pop] [[F] dupdip G] primrec\n", + "\n", + "With\n", + "- `n :: A` is the \"identity\" for `F` (like 1 for multiplication, 0 for addition)\n", + "- `F :: (A, B) -> A`\n", + "- `G :: B -> B` generates the next `B` value.\n", + "- and lastly `P :: B -> Bool` detects the end of the series." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For Factorial function (types `A` and `B` are both integer):\n", + "\n", + " n == 1\n", + " F == *\n", + " G == --\n", + " P == 1 <=" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Try it with input 3 (omitting evaluation of predicate):\n", + "\n", + " 3 1 swap [1 <=] [pop] [[*] dupdip --] primrec\n", + " 1 3 [1 <=] [pop] [[*] dupdip --] primrec\n", + "\n", + " 1 3 [*] dupdip --\n", + " 1 3 * 3 --\n", + " 3 3 --\n", + " 3 2\n", + "\n", + " 3 2 [*] dupdip --\n", + " 3 2 * 2 --\n", + " 6 2 --\n", + " 6 1\n", + "\n", + " 6 1 [1 <=] [pop] [[*] dupdip --] primrec\n", + "\n", + " 6 1 pop\n", + " 6" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "J('3 factorial')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Derive `paramorphism` from the form above.\n", + "\n", + " n swap [P] [pop] [[F] dupdip G] primrec\n", + "\n", + " n swap [P] [pop] [[F] dupdip G] primrec\n", + " n [P] [swap] dip [pop] [[F] dupdip G] primrec\n", + " n [P] [[F] dupdip G] [[swap] dip [pop]] dip primrec\n", + " n [P] [F] [dupdip G] cons [[swap] dip [pop]] dip primrec\n", + " n [P] [F] [G] [dupdip] swoncat cons [[swap] dip [pop]] dip primrec\n", + "\n", + " paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "define('paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec')\n", + "define('factorial == 1 [1 <=] [*] [--] paramorphism')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "J('3 factorial')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# `tails`\n", + "An example of a paramorphism for lists given in the [\"Bananas...\" paper](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) is `tails` which returns the list of \"tails\" of a list.\n", + "\n", + " [1 2 3] tails == [[] [3] [2 3]]\n", + " \n", + "Using `paramorphism` we would write:\n", + "\n", + " n == []\n", + " F == rest swons\n", + " G == rest\n", + " P == not\n", + "\n", + " tails == [] [not] [rest swons] [rest] paramorphism" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "define('tails == [] [not] [rest swons] [rest] paramorphism')" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[] [3] [2 3]]\n" + ] + } + ], + "source": [ + "J('[1 2 3] tails')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276]\n" + ] + } + ], + "source": [ + "J('25 range tails [popop] infra [sum] map')" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[276 253 231 210 190 171 153 136 120 105 91 78 66 55 45 36 28 21 15 10 6 3 1 0 0]\n" + ] + } + ], + "source": [ + "J('25 range [range_sum] map')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Factoring `rest`\n", + "Right before the recursion begins we have:\n", + " \n", + " [] [1 2 3] [not] [pop] [[rest swons] dupdip rest] primrec\n", + " \n", + "But we might prefer to factor `rest` in the quote:\n", + "\n", + " [] [1 2 3] [not] [pop] [rest [swons] dupdip] primrec\n", + "\n", + "There's no way to do that with the `paramorphism` combinator as defined. We would have to write and use a slightly different recursion combinator that accepted an additional \"preprocessor\" function `[H]` and built:\n", + "\n", + " n swap [P] [pop] [H [F] dupdip G] primrec\n", + "\n", + "Or just write it out manually. This is yet another place where the *sufficiently smart compiler* will one day automatically refactor the code. We could write a `paramorphism` combinator that checked `[F]` and `[G]` for common prefix and extracted it." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Patterns of Recursion\n", + "Our story so far...\n", + "\n", + "- A combiner `F :: (B, B) -> B`\n", + "- A predicate `P :: A -> Bool` to detect the base case\n", + "- A base case value `c :: B`\n", + "\n", + "\n", + "### Hylo-, Ana-, Cata-\n", + "\n", + " w/ G :: A -> (A, B)\n", + "\n", + " H == [P ] [pop c ] [G ] [dip F ] genrec\n", + " A == [P ] [pop []] [G ] [dip swons] genrec\n", + " C == [[] =] [pop c ] [uncons swap] [dip F ] genrec\n", + "\n", + "### Para-, ?-, ?-\n", + "\n", + " w/ G :: B -> B\n", + "\n", + " P == c swap [P ] [pop] [[F ] dupdip G ] primrec\n", + " ? == [] swap [P ] [pop] [[swons] dupdip G ] primrec\n", + " ? == c swap [[] =] [pop] [[F ] dupdip uncons swap] primrec\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Four Generalizations\n", + "There are at least four kinds of recursive combinator, depending on two choices. The first choice is whether the combiner function should be evaluated during the recursion or pushed into the pending expression to be \"collapsed\" at the end. The second choice is whether the combiner needs to operate on the current value of the datastructure or the generator's output.\n", + "\n", + " H == [P] [pop c] [G ] [dip F] genrec\n", + " H == c swap [P] [pop] [G [F] dip ] [i] genrec\n", + " H == [P] [pop c] [ [G] dupdip ] [dip F] genrec\n", + " H == c swap [P] [pop] [ [F] dupdip G] [i] genrec\n", + "\n", + "Consider:\n", + "\n", + " ... a G [H] dip F w/ a G == a' b\n", + " ... c a G [F] dip H a G == b a'\n", + " ... a [G] dupdip [H] dip F a G == a'\n", + " ... c a [F] dupdip G H a G == a'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1\n", + "\n", + " H == [P] [pop c] [G] [dip F] genrec\n", + "\n", + "Iterate n times.\n", + "\n", + " ... a [P] [pop c] [G] [dip F] genrec\n", + " ... a G [H] dip F\n", + " ... a' b [H] dip F\n", + " ... a' H b F\n", + " ... a' G [H] dip F b F\n", + " ... a'' b [H] dip F b F\n", + " ... a'' H b F b F\n", + " ... a'' G [H] dip F b F b F\n", + " ... a''' b [H] dip F b F b F\n", + " ... a''' H b F b F b F\n", + " ... a''' pop c b F b F b F\n", + " ... c b F b F b F\n", + "\n", + "This form builds up a continuation that contains the intermediate results along with the pending combiner functions. When the base case is reached the last term is replaced by the identity value c and the continuation \"collapses\" into the final result." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2\n", + "When you can start with the identity value c on the stack and the combiner can operate as you go, using the intermediate results immediately rather than queuing them up, use this form. An important difference is that the generator function must return its results in the reverse order.\n", + "\n", + " H == c swap [P] [pop] [G [F] dip] primrec\n", + "\n", + " ... c a G [F] dip H\n", + " ... c b a' [F] dip H\n", + " ... c b F a' H\n", + " ... c b F a' G [F] dip H\n", + " ... c b F b a'' [F] dip H\n", + " ... c b F b F a'' H\n", + " ... c b F b F a'' G [F] dip H\n", + " ... c b F b F b a''' [F] dip H\n", + " ... c b F b F b F a''' H\n", + " ... c b F b F b F a''' pop\n", + " ... c b F b F b F\n", + "\n", + "The end line here is the same as for above, but only because we didn't evaluate `F` when it normally would have been." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3\n", + "If the combiner and the generator both need to work on the current value then `dup` must be used at some point, and the generator must produce one item instead of two (the b is instead the duplicate of a.)\n", + "\n", + "\n", + " H == [P] [pop c] [[G] dupdip] [dip F] genrec\n", + "\n", + " ... a [G] dupdip [H] dip F\n", + " ... a G a [H] dip F\n", + " ... a' a [H] dip F\n", + " ... a' H a F\n", + " ... a' [G] dupdip [H] dip F a F\n", + " ... a' G a' [H] dip F a F\n", + " ... a'' a' [H] dip F a F\n", + " ... a'' H a' F a F\n", + " ... a'' [G] dupdip [H] dip F a' F a F\n", + " ... a'' G a'' [H] dip F a' F a F\n", + " ... a''' a'' [H] dip F a' F a F\n", + " ... a''' H a'' F a' F a F\n", + " ... a''' pop c a'' F a' F a F\n", + " ... c a'' F a' F a F" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4\n", + "And, last but not least, if you can combine as you go, starting with c, and the combiner needs to work on the current item, this is the form:\n", + "\n", + " W == c swap [P] [pop] [[F] dupdip G] primrec\n", + "\n", + " ... a c swap [P] [pop] [[F] dupdip G] primrec\n", + " ... c a [P] [pop] [[F] dupdip G] primrec\n", + " ... c a [F] dupdip G W\n", + " ... c a F a G W\n", + " ... c a F a' W\n", + " ... c a F a' [F] dupdip G W\n", + " ... c a F a' F a' G W\n", + " ... c a F a' F a'' W\n", + " ... c a F a' F a'' [F] dupdip G W\n", + " ... c a F a' F a'' F a'' G W\n", + " ... c a F a' F a'' F a''' W\n", + " ... c a F a' F a'' F a''' pop\n", + " ... c a F a' F a'' F" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each of the four variations above can be specialized to ana- and catamorphic forms." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "def WTFmorphism(c, F, P, G):\n", + " '''Return a hylomorphism function H.'''\n", + "\n", + " def H(a, d=c):\n", + " if P(a):\n", + " result = d\n", + " else:\n", + " a, b = G(a)\n", + " result = H(a, F(d, b))\n", + " return result\n", + "\n", + " return H" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "F = lambda a, b: a + b\n", + "P = lambda n: n <= 1\n", + "G = lambda n: (n - 1, n - 1)\n", + "\n", + "wtf = WTFmorphism(0, F, P, G)\n", + "\n", + "print wtf(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " H == [P ] [pop c ] [G ] [dip F ] genrec" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "DefinitionWrapper.add_definitions('''\n", + "P == 1 <=\n", + "Ga == -- dup\n", + "Gb == --\n", + "c == 0\n", + "F == +\n", + "''', D)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec\n", + " [1 2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec\n", + " [1 2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec\n", + " [1 2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec\n", + " [1 2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec\n", + " [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec\n", + " [1 2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte\n", + "[1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[1 2 3]] [[] =] . infra first choice i\n", + " [1 2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i\n", + " [1 2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i\n", + " False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i\n", + " False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] . swaack first choice i\n", + " [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i\n", + " [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i\n", + " [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i\n", + " [1 2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons\n", + " 1 [2 3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons\n", + " [2 3] 1 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons\n", + " [2 3] 1 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons\n", + " [2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 1 swons\n", + " [2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 1 swons\n", + " [2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 1 swons\n", + " [2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 1 swons\n", + " [2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 1 swons\n", + " [2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 1 swons\n", + " [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[2 3]] [[] =] . infra first choice i 1 swons\n", + " [2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons\n", + " [2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons\n", + " False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons\n", + " False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] . swaack first choice i 1 swons\n", + " [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons\n", + " [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons\n", + " [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 1 swons\n", + " [2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons\n", + " 2 [3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons\n", + " [3] 2 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons\n", + " [3] 2 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 1 swons\n", + " [3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons\n", + " [3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons\n", + " [3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 2 swons 1 swons\n", + " [3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 2 swons 1 swons\n", + " [3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 2 swons 1 swons\n", + " [3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 2 swons 1 swons\n", + " [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[3]] [[] =] . infra first choice i 2 swons 1 swons\n", + " [3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons\n", + " [3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons\n", + " False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons\n", + " False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] . swaack first choice i 2 swons 1 swons\n", + " [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 1 swons\n", + " [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 1 swons\n", + " [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 2 swons 1 swons\n", + " [3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons\n", + " 3 [] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons\n", + " [] 3 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons\n", + " [] 3 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 2 swons 1 swons\n", + " [] . [[] =] [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons\n", + " [] [[] =] . [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons\n", + " [] [[] =] [pop []] . [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons\n", + " [] [[] =] [pop []] [uncons swap] . [dip swons] genrec 3 swons 2 swons 1 swons\n", + " [] [[] =] [pop []] [uncons swap] [dip swons] . genrec 3 swons 2 swons 1 swons\n", + " [] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 3 swons 2 swons 1 swons\n", + " [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[]] [[] =] . infra first choice i 3 swons 2 swons 1 swons\n", + " [] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons\n", + " [] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons\n", + " True . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons\n", + " True [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] . swaack first choice i 3 swons 2 swons 1 swons\n", + " [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 3 swons 2 swons 1 swons\n", + " [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] True . choice i 3 swons 2 swons 1 swons\n", + " [] [pop []] . i 3 swons 2 swons 1 swons\n", + " [] . pop [] 3 swons 2 swons 1 swons\n", + " . [] 3 swons 2 swons 1 swons\n", + " [] . 3 swons 2 swons 1 swons\n", + " [] 3 . swons 2 swons 1 swons\n", + " [] 3 . swap cons 2 swons 1 swons\n", + " 3 [] . cons 2 swons 1 swons\n", + " [3] . 2 swons 1 swons\n", + " [3] 2 . swons 1 swons\n", + " [3] 2 . swap cons 1 swons\n", + " 2 [3] . cons 1 swons\n", + " [2 3] . 1 swons\n", + " [2 3] 1 . swons\n", + " [2 3] 1 . swap cons\n", + " 1 [2 3] . cons\n", + " [1 2 3] . \n" + ] + } + ], + "source": [ + "V('[1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 3 [P] [pop c] [Ga] [dip F] genrec\n", + " 3 . [P] [pop c] [Ga] [dip F] genrec\n", + " 3 [P] . [pop c] [Ga] [dip F] genrec\n", + " 3 [P] [pop c] . [Ga] [dip F] genrec\n", + " 3 [P] [pop c] [Ga] . [dip F] genrec\n", + " 3 [P] [pop c] [Ga] [dip F] . genrec\n", + " 3 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte\n", + "3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [3] [P] . infra first choice i\n", + " 3 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i\n", + " 3 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i\n", + " 3 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i\n", + " False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i\n", + "False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] . swaack first choice i\n", + "3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i\n", + " 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i\n", + " 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i\n", + " 3 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F\n", + " 3 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F\n", + " 2 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F\n", + " 2 2 . [[P] [pop c] [Ga] [dip F] genrec] dip F\n", + " 2 2 [[P] [pop c] [Ga] [dip F] genrec] . dip F\n", + " 2 . [P] [pop c] [Ga] [dip F] genrec 2 F\n", + " 2 [P] . [pop c] [Ga] [dip F] genrec 2 F\n", + " 2 [P] [pop c] . [Ga] [dip F] genrec 2 F\n", + " 2 [P] [pop c] [Ga] . [dip F] genrec 2 F\n", + " 2 [P] [pop c] [Ga] [dip F] . genrec 2 F\n", + " 2 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 2 F\n", + "2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [2] [P] . infra first choice i 2 F\n", + " 2 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F\n", + " 2 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F\n", + " 2 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F\n", + " False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F\n", + "False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] . swaack first choice i 2 F\n", + "2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F\n", + " 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i 2 F\n", + " 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i 2 F\n", + " 2 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F\n", + " 2 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F\n", + " 1 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F\n", + " 1 1 . [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F\n", + " 1 1 [[P] [pop c] [Ga] [dip F] genrec] . dip F 2 F\n", + " 1 . [P] [pop c] [Ga] [dip F] genrec 1 F 2 F\n", + " 1 [P] . [pop c] [Ga] [dip F] genrec 1 F 2 F\n", + " 1 [P] [pop c] . [Ga] [dip F] genrec 1 F 2 F\n", + " 1 [P] [pop c] [Ga] . [dip F] genrec 1 F 2 F\n", + " 1 [P] [pop c] [Ga] [dip F] . genrec 1 F 2 F\n", + " 1 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 1 F 2 F\n", + "1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [1] [P] . infra first choice i 1 F 2 F\n", + " 1 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F\n", + " 1 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F\n", + " 1 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F\n", + " True . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F\n", + " True [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] . swaack first choice i 1 F 2 F\n", + " 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F\n", + " 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F\n", + " 1 [pop c] . i 1 F 2 F\n", + " 1 . pop c 1 F 2 F\n", + " . c 1 F 2 F\n", + " . 0 1 F 2 F\n", + " 0 . 1 F 2 F\n", + " 0 1 . F 2 F\n", + " 0 1 . + 2 F\n", + " 1 . 2 F\n", + " 1 2 . F\n", + " 1 2 . +\n", + " 3 . \n" + ] + } + ], + "source": [ + "V('3 [P] [pop c] [Ga] [dip F] genrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . 3 [P] [pop []] [Ga] [dip swons] genrec\n", + " 3 . [P] [pop []] [Ga] [dip swons] genrec\n", + " 3 [P] . [pop []] [Ga] [dip swons] genrec\n", + " 3 [P] [pop []] . [Ga] [dip swons] genrec\n", + " 3 [P] [pop []] [Ga] . [dip swons] genrec\n", + " 3 [P] [pop []] [Ga] [dip swons] . genrec\n", + " 3 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte\n", + "3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [3] [P] . infra first choice i\n", + " 3 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i\n", + " 3 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i\n", + " 3 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i\n", + " False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i\n", + "False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] . swaack first choice i\n", + "3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i\n", + " 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i\n", + " 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i\n", + " 3 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons\n", + " 3 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons\n", + " 2 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons\n", + " 2 2 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons\n", + " 2 2 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons\n", + " 2 . [P] [pop []] [Ga] [dip swons] genrec 2 swons\n", + " 2 [P] . [pop []] [Ga] [dip swons] genrec 2 swons\n", + " 2 [P] [pop []] . [Ga] [dip swons] genrec 2 swons\n", + " 2 [P] [pop []] [Ga] . [dip swons] genrec 2 swons\n", + " 2 [P] [pop []] [Ga] [dip swons] . genrec 2 swons\n", + " 2 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 2 swons\n", + "2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [2] [P] . infra first choice i 2 swons\n", + " 2 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons\n", + " 2 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons\n", + " 2 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons\n", + " False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons\n", + "False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons\n", + "2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons\n", + " 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons\n", + " 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i 2 swons\n", + " 2 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons\n", + " 2 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons\n", + " 1 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons\n", + " 1 1 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons\n", + " 1 1 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons 2 swons\n", + " 1 . [P] [pop []] [Ga] [dip swons] genrec 1 swons 2 swons\n", + " 1 [P] . [pop []] [Ga] [dip swons] genrec 1 swons 2 swons\n", + " 1 [P] [pop []] . [Ga] [dip swons] genrec 1 swons 2 swons\n", + " 1 [P] [pop []] [Ga] . [dip swons] genrec 1 swons 2 swons\n", + " 1 [P] [pop []] [Ga] [dip swons] . genrec 1 swons 2 swons\n", + " 1 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons\n", + "1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [1] [P] . infra first choice i 1 swons 2 swons\n", + " 1 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons\n", + " 1 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons\n", + " 1 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons\n", + " True . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons\n", + " True [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons\n", + " 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 1 swons 2 swons\n", + " 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] True . choice i 1 swons 2 swons\n", + " 1 [pop []] . i 1 swons 2 swons\n", + " 1 . pop [] 1 swons 2 swons\n", + " . [] 1 swons 2 swons\n", + " [] . 1 swons 2 swons\n", + " [] 1 . swons 2 swons\n", + " [] 1 . swap cons 2 swons\n", + " 1 [] . cons 2 swons\n", + " [1] . 2 swons\n", + " [1] 2 . swons\n", + " [1] 2 . swap cons\n", + " 2 [1] . cons\n", + " [2 1] . \n" + ] + } + ], + "source": [ + "V('3 [P] [pop []] [Ga] [dip swons] genrec')" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [2 1] [[] =] [pop c] [uncons swap] [dip F] genrec\n", + " [2 1] . [[] =] [pop c] [uncons swap] [dip F] genrec\n", + " [2 1] [[] =] . [pop c] [uncons swap] [dip F] genrec\n", + " [2 1] [[] =] [pop c] . [uncons swap] [dip F] genrec\n", + " [2 1] [[] =] [pop c] [uncons swap] . [dip F] genrec\n", + " [2 1] [[] =] [pop c] [uncons swap] [dip F] . genrec\n", + " [2 1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte\n", + "[2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[2 1]] [[] =] . infra first choice i\n", + " [2 1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i\n", + " [2 1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i\n", + " False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i\n", + " False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] . swaack first choice i\n", + " [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i\n", + " [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i\n", + " [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i\n", + " [2 1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F\n", + " 2 [1] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F\n", + " [1] 2 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F\n", + " [1] 2 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F\n", + " [1] . [[] =] [pop c] [uncons swap] [dip F] genrec 2 F\n", + " [1] [[] =] . [pop c] [uncons swap] [dip F] genrec 2 F\n", + " [1] [[] =] [pop c] . [uncons swap] [dip F] genrec 2 F\n", + " [1] [[] =] [pop c] [uncons swap] . [dip F] genrec 2 F\n", + " [1] [[] =] [pop c] [uncons swap] [dip F] . genrec 2 F\n", + " [1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 2 F\n", + " [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[1]] [[] =] . infra first choice i 2 F\n", + " [1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F\n", + " [1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F\n", + " False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F\n", + " False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] . swaack first choice i 2 F\n", + " [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F\n", + " [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i 2 F\n", + " [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i 2 F\n", + " [1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F\n", + " 1 [] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F\n", + " [] 1 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F\n", + " [] 1 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F 2 F\n", + " [] . [[] =] [pop c] [uncons swap] [dip F] genrec 1 F 2 F\n", + " [] [[] =] . [pop c] [uncons swap] [dip F] genrec 1 F 2 F\n", + " [] [[] =] [pop c] . [uncons swap] [dip F] genrec 1 F 2 F\n", + " [] [[] =] [pop c] [uncons swap] . [dip F] genrec 1 F 2 F\n", + " [] [[] =] [pop c] [uncons swap] [dip F] . genrec 1 F 2 F\n", + " [] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 1 F 2 F\n", + " [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[]] [[] =] . infra first choice i 1 F 2 F\n", + " [] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F\n", + " [] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F\n", + " True . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F\n", + " True [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] . swaack first choice i 1 F 2 F\n", + " [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F\n", + " [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F\n", + " [] [pop c] . i 1 F 2 F\n", + " [] . pop c 1 F 2 F\n", + " . c 1 F 2 F\n", + " . 0 1 F 2 F\n", + " 0 . 1 F 2 F\n", + " 0 1 . F 2 F\n", + " 0 1 . + 2 F\n", + " 1 . 2 F\n", + " 1 2 . F\n", + " 1 2 . +\n", + " 3 . \n" + ] + } + ], + "source": [ + "V('[2 1] [[] =] [pop c ] [uncons swap] [dip F] genrec')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Appendix - Fun with Symbols\n", + "\n", + " |[ (c, F), (G, P) ]| == (|c, F|) • [(G, P)]\n", + "\n", + "[\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125)\n", + "\n", + " (|...|) [(...)] [<...>]\n", + "\n", + "I think they are having slightly too much fun with the symbols.\n", + "\n", + "\"Too much is always better than not enough.\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tree with node and list of trees.\n", + "\n", + " tree = [] | [node [tree*]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `treestep`\n", + "\n", + " tree z [C] [N] treestep\n", + "\n", + "\n", + " [] z [C] [N] treestep\n", + " ---------------------------\n", + " z\n", + "\n", + "\n", + " [node [tree*]] z [C] [N] treestep\n", + " --------------------------------------- w/ K == z [C] [N] treestep\n", + " node N [tree*] [K] map C" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Derive the recursive form.\n", + " K == [not] [pop z] [J] ifte\n", + "\n", + "\n", + " [node [tree*]] J\n", + " ------------------------------\n", + " node N [tree*] [K] map C\n", + "\n", + "\n", + " J == .. [N] .. [K] .. [C] ..\n", + "\n", + " [node [tree*]] uncons [N] dip\n", + " node [[tree*]] [N] dip\n", + " node N [[tree*]]\n", + "\n", + " node N [[tree*]] i [K] map\n", + " node N [tree*] [K] map\n", + " node N [K.tree*]\n", + "\n", + " J == uncons [N] dip i [K] map [C] i\n", + "\n", + " K == [not] [pop z] [uncons [N] dip i [K] map [C] i] ifte\n", + " K == [not] [pop z] [uncons [N] dip i] [map [C] i] genrec" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Extract the givens to parameterize the program.\n", + " [not] [pop z] [uncons [N] dip unquote] [map [C] i] genrec\n", + " [not] [z] [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec\n", + " [not] z unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec\n", + " z [not] swap unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec\n", + " \\............TS0............/\n", + " z TS0 [uncons [N] dip unquote] [map [C] i] genrec\n", + " z [uncons [N] dip unquote] [TS0] dip [map [C] i] genrec\n", + " z [[N] dip unquote] [uncons] swoncat [TS0] dip [map [C] i] genrec\n", + " z [N] [dip unquote] cons [uncons] swoncat [TS0] dip [map [C] i] genrec\n", + " \\...........TS1.................../\n", + " z [N] TS1 [TS0] dip [map [C] i] genrec\n", + " z [N] [map [C] i] [TS1 [TS0] dip] dip genrec\n", + " z [N] [map C ] [TS1 [TS0] dip] dip genrec\n", + " z [N] [C] [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " z [C] [N] swap [map] swoncat [TS1 [TS0] dip] dip genrec" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " TS0 == [not] swap unit [pop] swoncat\n", + " TS1 == [dip i] cons [uncons] swoncat\n", + " treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " [] 0 [C] [N] treestep\n", + " ---------------------------\n", + " 0\n", + "\n", + "\n", + " [n [tree*]] 0 [sum +] [] treestep\n", + " --------------------------------------------------\n", + " n [tree*] [0 [sum +] [] treestep] map sum +" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "DefinitionWrapper.add_definitions('''\n", + "\n", + " TS0 == [not] swap unit [pop] swoncat\n", + " TS1 == [dip i] cons [uncons] swoncat\n", + "treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec\n", + "\n", + "''', D)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [] 0 [sum +] [] treestep\n", + " [] . 0 [sum +] [] treestep\n", + " [] 0 . [sum +] [] treestep\n", + " [] 0 [sum +] . [] treestep\n", + " [] 0 [sum +] [] . treestep\n", + " [] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " [] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " [] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec\n", + " [] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec\n", + " [] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec\n", + " [] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec\n", + " [] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec\n", + " [] 0 [] . TS1 [TS0] dip [map sum +] genrec\n", + " [] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec\n", + " [] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec\n", + " [] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec\n", + " [] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec\n", + " [] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec\n", + " [] 0 . TS0 [uncons [] dip i] [map sum +] genrec\n", + " [] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec\n", + " [] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec\n", + " [] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec\n", + " [] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec\n", + " [] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte\n", + "[] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[]] [not] . infra first choice i\n", + " [] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i\n", + " True . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i\n", + " True [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] . swaack first choice i\n", + " [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [True] . first choice i\n", + " [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] True . choice i\n", + " [] [pop 0] . i\n", + " [] . pop 0\n", + " . 0\n", + " 0 . \n" + ] + } + ], + "source": [ + "V('[] 0 [sum +] [] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [23 []] 0 [sum +] [] treestep\n", + " [23 []] . 0 [sum +] [] treestep\n", + " [23 []] 0 . [sum +] [] treestep\n", + " [23 []] 0 [sum +] . [] treestep\n", + " [23 []] 0 [sum +] [] . treestep\n", + " [23 []] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " [23 []] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " [23 []] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec\n", + " [23 []] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec\n", + " [23 []] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec\n", + " [23 []] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec\n", + " [23 []] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec\n", + " [23 []] 0 [] . TS1 [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec\n", + " [23 []] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec\n", + " [23 []] 0 . TS0 [uncons [] dip i] [map sum +] genrec\n", + " [23 []] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec\n", + " [23 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec\n", + " [23 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec\n", + " [23 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte\n", + "[23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 []]] [not] . infra first choice i\n", + " [23 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i\n", + " False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i\n", + " False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] . swaack first choice i\n", + " [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i\n", + " [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i\n", + " [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i\n", + " [23 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum +\n", + " 23 [] . sum +\n", + " 23 [] . 0 [+] catamorphism +\n", + " 23 [] 0 . [+] catamorphism +\n", + " 23 [] 0 [+] . catamorphism +\n", + " 23 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism +\n", + " 23 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism +\n", + " 23 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism +\n", + " 23 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism +\n", + " 23 [] [[] =] 0 [uncons swap] [+] . hylomorphism +\n", + " 23 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec +\n", + " 23 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec +\n", + " 23 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec +\n", + " 23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte +\n", + " 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i +\n", + " 23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i +\n", + " 23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i +\n", + " 23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i +\n", + " 23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i +\n", + " 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i +\n", + " 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i +\n", + " 23 [] [pop 0] . i +\n", + " 23 [] . pop 0 +\n", + " 23 . 0 +\n", + " 23 0 . +\n", + " 23 . \n" + ] + } + ], + "source": [ + "V('[23 []] 0 [sum +] [] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " . [23 [[2 []] [3 []]]] 0 [sum +] [] treestep\n", + " [23 [[2 []] [3 []]]] . 0 [sum +] [] treestep\n", + " [23 [[2 []] [3 []]]] 0 . [sum +] [] treestep\n", + " [23 [[2 []] [3 []]]] 0 [sum +] . [] treestep\n", + " [23 [[2 []] [3 []]]] 0 [sum +] [] . treestep\n", + " [23 [[2 []] [3 []]]] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec\n", + " [23 [[2 []] [3 []]]] 0 [] . TS1 [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 . TS0 [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec\n", + " [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec\n", + " [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte\n", + " [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 [[2 []] [3 []]]]] [not] . infra first choice i\n", + " [23 [[2 []] [3 []]]] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i\n", + " False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i\n", + " False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] . swaack first choice i\n", + " [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i\n", + " [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i\n", + " [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i\n", + " [23 [[2 []] [3 []]]] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[[2 []] [3 []]]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[[2 []] [3 []]]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 . [[[2 []] [3 []]]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[[2 []] [3 []]]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 . [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[2 []] [3 []]] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +\n", + " 23 [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum +\n", + "23 [] [[[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first] . infra sum +\n", + " . [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " [[3 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[3 []] 23] [not] . infra first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] . swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 [3 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] . sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] . 0 [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] 0 . [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] 0 [+] . catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 3 23] [[] =] . infra first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] . swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 3 23] . first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] [pop 0] . i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] . pop 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 . 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 0 . + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 . [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 23 3 [] . swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " [3 23] . first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 3 . [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 3 [[2 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum +\n", + " 3 [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [23] swaack sum +\n", + " 23 [2 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[2 []] 23] [not] . infra first choice i [3] swaack first [23] swaack sum +\n", + " 23 [2 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum +\n", + " 23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum +\n", + " 23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] . swaack first choice i [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [3] swaack first [23] swaack sum +\n", + " 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [3] swaack first [23] swaack sum +\n", + " 23 [2 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [] . sum + [3] swaack first [23] swaack sum +\n", + " 23 2 [] . 0 [+] catamorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] 0 . [+] catamorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] 0 [+] . catamorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 2 23] [[] =] . infra first choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] . swaack first choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 2 23] . first choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [3] swaack first [23] swaack sum +\n", + " 23 2 [] [pop 0] . i + [3] swaack first [23] swaack sum +\n", + " 23 2 [] . pop 0 + [3] swaack first [23] swaack sum +\n", + " 23 2 . 0 + [3] swaack first [23] swaack sum +\n", + " 23 2 0 . + [3] swaack first [23] swaack sum +\n", + " 23 2 . [3] swaack first [23] swaack sum +\n", + " 23 2 [3] . swaack first [23] swaack sum +\n", + " 3 [2 23] . first [23] swaack sum +\n", + " 3 2 . [23] swaack sum +\n", + " 3 2 [23] . swaack sum +\n", + " 23 [2 3] . sum +\n", + " 23 [2 3] . 0 [+] catamorphism +\n", + " 23 [2 3] 0 . [+] catamorphism +\n", + " 23 [2 3] 0 [+] . catamorphism +\n", + " 23 [2 3] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism +\n", + " 23 [2 3] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism +\n", + " 23 [2 3] [[] =] 0 [+] . [uncons swap] swap hylomorphism +\n", + " 23 [2 3] [[] =] 0 [+] [uncons swap] . swap hylomorphism +\n", + " 23 [2 3] [[] =] 0 [uncons swap] [+] . hylomorphism +\n", + " 23 [2 3] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap] [dip +] . genrec +\n", + " 23 [2 3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte +\n", + " 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 3] 23] [[] =] . infra first choice i +\n", + " 23 [2 3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i +\n", + " 23 [2 3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i +\n", + " 23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i +\n", + " 23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] . swaack first choice i +\n", + " 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i +\n", + " 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i +\n", + " 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i +\n", + " 23 [2 3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + +\n", + " 23 2 [3] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + +\n", + " 23 [3] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + +\n", + " 23 [3] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + +\n", + " 23 [3] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + +\n", + " 23 [3] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + +\n", + " 23 [3] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + +\n", + " 23 [3] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + +\n", + " 23 [3] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + +\n", + " 23 [3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + +\n", + " 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3] 23] [[] =] . infra first choice i 2 + +\n", + " 23 [3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + +\n", + " 23 [3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + +\n", + " 23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + +\n", + " 23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] . swaack first choice i 2 + +\n", + " 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i 2 + +\n", + " 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + +\n", + " 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + +\n", + " 23 [3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + +\n", + " 23 3 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + +\n", + " 23 [] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + +\n", + " 23 [] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + +\n", + " 23 [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 2 + +\n", + " 23 [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 2 + +\n", + " 23 [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 2 + +\n", + " 23 [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 2 + +\n", + " 23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 2 + +\n", + " 23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 2 + +\n", + " 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i 3 + 2 + +\n", + " 23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + +\n", + " 23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + +\n", + " 23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + +\n", + " 23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i 3 + 2 + +\n", + " 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i 3 + 2 + +\n", + " 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 3 + 2 + +\n", + " 23 [] [pop 0] . i 3 + 2 + +\n", + " 23 [] . pop 0 3 + 2 + +\n", + " 23 . 0 3 + 2 + +\n", + " 23 0 . 3 + 2 + +\n", + " 23 0 3 . + 2 + +\n", + " 23 3 . 2 + +\n", + " 23 3 2 . + +\n", + " 23 5 . +\n", + " 28 . \n" + ] + } + ], + "source": [ + "V('[23 [[2 []] [3 []]]] 0 [sum +] [] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140\n" + ] + } + ], + "source": [ + "J('[23 [[2 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]] [3 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]]]] 0 [sum +] [] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "J('[] [] [unit cons] [23 +] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[46 []]\n" + ] + } + ], + "source": [ + "J('[23 []] [] [unit cons] [23 +] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[46 [[25 []] [26 []]]]\n" + ] + } + ], + "source": [ + "J('[23 [[2 []] [3 []]]] [] [unit cons] [23 +] treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "define('treemap == [] [unit cons] roll< treestep')" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[46 [[25 []] [26 []]]]\n" + ] + } + ], + "source": [ + "J('[23 [[2 []] [3 []]]] [23 +] treemap')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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 +} diff --git a/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.md b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.md new file mode 100644 index 0000000..dfb43dc --- /dev/null +++ b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.md @@ -0,0 +1,2449 @@ + +Cf. ["Bananas, Lenses, & Barbed Wire"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) + +# [Hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29) +A [hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29) `H :: A -> B` converts a value of type A into a value of type B by means of: + +- A generator `G :: A -> (A, B)` +- A combiner `F :: (B, B) -> B` +- A predicate `P :: A -> Bool` to detect the base case +- A base case value `c :: B` +- Recursive calls (zero or more); it has a "call stack in the form of a cons list". + +It may be helpful to see this function implemented in imperative Python code. + + +```python +def hylomorphism(c, F, P, G): + '''Return a hylomorphism function H.''' + + def H(a): + if P(a): + result = c + else: + b, aa = G(a) + result = F(b, H(aa)) + return result + + return H +``` + +### Finding [Triangular Numbers](https://en.wikipedia.org/wiki/Triangular_number) +As a concrete example let's use a function that, given a positive integer, returns the sum of all positive integers less than that one. (In this case the types A and B are both `int`.) +### With `range()` and `sum()` + + +```python +r = range(10) +r +``` + + + + + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + + + + +```python +sum(r) +``` + + + + + 45 + + + + +```python +range_sum = lambda n: sum(range(n)) +range_sum(10) +``` + + + + + 45 + + + +### As a hylomorphism + + +```python +G = lambda n: (n - 1, n - 1) +F = lambda a, b: a + b +P = lambda n: n <= 1 + +H = hylomorphism(0, F, P, G) +``` + + +```python +H(10) +``` + + + + + 45 + + + +If you were to run the above code in a debugger and check out the call stack you would find that the variable `b` in each call to `H()` is storing the intermediate values as `H()` recurses. This is what was meant by "call stack in the form of a cons list". + +### Joy Preamble + + +```python +from notebook_preamble import D, DefinitionWrapper, J, V, define +``` + +## Hylomorphism in Joy +We can define a combinator `hylomorphism` that will make a hylomorphism combinator `H` from constituent parts. + + H == c [F] [P] [G] hylomorphism + +The function `H` is recursive, so we start with `ifte` and set the else-part to +some function `J` that will contain a quoted copy of `H`. (The then-part just +discards the leftover `a` and replaces it with the base case value `c`.) + + H == [P] [pop c] [J] ifte + +The else-part `J` gets just the argument `a` on the stack. + + a J + a G The first thing to do is use the generator G + aa b which produces b and a new aa + aa b [H] dip we recur with H on the new aa + aa H b F and run F on the result. + +This gives us a definition for `J`. + + J == G [H] dip F + +Plug it in and convert to genrec. + + H == [P] [pop c] [G [H] dip F] ifte + H == [P] [pop c] [G] [dip F] genrec + +This is the form of a hylomorphism in Joy, which nicely illustrates that +it is a simple specialization of the general recursion combinator. + + H == [P] [pop c] [G] [dip F] genrec + +## Derivation of `hylomorphism` + +Now we just need to derive a definition that builds the `genrec` arguments +out of the pieces given to the `hylomorphism` combinator. + + H == [P] [pop c] [G] [dip F] genrec + [P] [c] [pop] swoncat [G] [F] [dip] swoncat genrec + [P] c unit [pop] swoncat [G] [F] [dip] swoncat genrec + [P] c [G] [F] [unit [pop] swoncat] dipd [dip] swoncat genrec + +Working in reverse: +- Use `swoncat` twice to decouple `[c]` and `[F]`. +- Use `unit` to dequote `c`. +- Use `dipd` to untangle `[unit [pop] swoncat]` from the givens. + +At this point all of the arguments (givens) to the hylomorphism are to the left so we have +a definition for `hylomorphism`: + + hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec + +The order of parameters is different than the one we started with but +that hardly matters, you can rearrange them or just supply them in the +expected order. + + [P] c [G] [F] hylomorphism == H + + + + +```python +define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec') +``` + +Demonstrate summing a range of integers from 0 to n-1. + +- `[P]` is `[0 <=]` +- `c` is `0` +- `[G]` is `[1 - dup]` +- `[F]` is `[+]` + +So to sum the positive integers less than five we can do this. + + +```python +V('5 [0 <=] 0 [1 - dup] [+] hylomorphism') +``` + + . 5 [0 <=] 0 [1 - dup] [+] hylomorphism + 5 . [0 <=] 0 [1 - dup] [+] hylomorphism + 5 [0 <=] . 0 [1 - dup] [+] hylomorphism + 5 [0 <=] 0 . [1 - dup] [+] hylomorphism + 5 [0 <=] 0 [1 - dup] . [+] hylomorphism + 5 [0 <=] 0 [1 - dup] [+] . hylomorphism + 5 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + 5 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + 5 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec + 5 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec + 5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec + 5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i + 5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i + 5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i + 5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + + 4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 + + 4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 + + 4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 + + 4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 + + 4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 + + 4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 + + 4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + + 4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 + + 4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 + + 3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 + + 3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 + + 3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 + + 3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 + + 3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 + + 3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 + + 3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + + 3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 + + 3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 + + 2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 + + 2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + + 2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 + + 2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 + + 1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 + + 1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + + 1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + + 1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + + 0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 + + 0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + + 0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + + True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + + True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + + 0 [pop 0] . i 0 + 1 + 2 + 3 + 4 + + 0 . pop 0 0 + 1 + 2 + 3 + 4 + + . 0 0 + 1 + 2 + 3 + 4 + + 0 . 0 + 1 + 2 + 3 + 4 + + 0 0 . + 1 + 2 + 3 + 4 + + 0 . 1 + 2 + 3 + 4 + + 0 1 . + 2 + 3 + 4 + + 1 . 2 + 3 + 4 + + 1 2 . + 3 + 4 + + 3 . 3 + 4 + + 3 3 . + 4 + + 6 . 4 + + 6 4 . + + 10 . + + +# Anamorphism +An anamorphism can be defined as a hylomorphism that uses `[]` for `c` and +`swons` for `F`. + + [P] [G] anamorphism == [P] [] [G] [swons] hylomorphism == A + +This allows us to define an anamorphism combinator in terms of +the hylomorphism combinator. + + [] swap [swons] hylomorphism == anamorphism + +Partial evaluation gives us a "pre-cooked" form. + + [P] [G] . anamorphism + [P] [G] . [] swap [swons] hylomorphism + [P] [G] [] . swap [swons] hylomorphism + [P] [] [G] . [swons] hylomorphism + [P] [] [G] [swons] . hylomorphism + [P] [] [G] [swons] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [P] [] [G] [swons] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [P] [] . unit [pop] swoncat [G] [swons] [dip] swoncat genrec + [P] [[]] [pop] . swoncat [G] [swons] [dip] swoncat genrec + [P] [pop []] [G] [swons] [dip] . swoncat genrec + + [P] [pop []] [G] [dip swons] genrec + +(We could also have just substituted for `c` and `F` in the definition of `H`.) + + H == [P] [pop c ] [G] [dip F ] genrec + A == [P] [pop []] [G] [dip swons] genrec + +The partial evaluation is overkill in this case but it serves as a +reminder that this sort of program specialization can, in many cases, be +carried out automatically.) + +Untangle `[G]` from `[pop []]` using `swap`. + + [P] [G] [pop []] swap [dip swons] genrec + +All of the arguments to `anamorphism` are to the left, so we have a definition for it. + + anamorphism == [pop []] swap [dip swons] genrec + +An example of an anamorphism is the range function. + + range == [0 <=] [1 - dup] anamorphism + + +# Catamorphism +A catamorphism can be defined as a hylomorphism that uses `[uncons swap]` for `[G]` +and `[[] =]` for the predicate `[P]`. + + c [F] catamorphism == [[] =] c [uncons swap] [F] hylomorphism == C + +This allows us to define a `catamorphism` combinator in terms of +the `hylomorphism` combinator. + + [[] =] roll> [uncons swap] swap hylomorphism == catamorphism + +Partial evaluation doesn't help much. + + c [F] . catamorphism + c [F] . [[] =] roll> [uncons swap] swap hylomorphism + c [F] [[] =] . roll> [uncons swap] swap hylomorphism + [[] =] c [F] [uncons swap] . swap hylomorphism + [[] =] c [uncons swap] [F] . hylomorphism + [[] =] c [uncons swap] [F] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [[] =] c . unit [pop] swoncat [uncons swap] [F] [dip] swoncat genrec + [[] =] [c] [pop] . swoncat [uncons swap] [F] [dip] swoncat genrec + [[] =] [pop c] [uncons swap] [F] [dip] . swoncat genrec + [[] =] [pop c] [uncons swap] [dip F] genrec + +Because the arguments to catamorphism have to be prepared (unlike the arguments +to anamorphism, which only need to be rearranged slightly) there isn't much point +to "pre-cooking" the definition. + + catamorphism == [[] =] roll> [uncons swap] swap hylomorphism + +An example of a catamorphism is the sum function. + + sum == 0 [+] catamorphism + +### "Fusion Law" for catas (UNFINISHED!!!) + +I'm not sure exactly how to translate the "Fusion Law" for catamorphisms into Joy. + +I know that a `map` composed with a cata can be expressed as a new cata: + + [F] map b [B] cata == b [F B] cata + +But this isn't the one described in "Bananas...". That's more like: + +A cata composed with some function can be expressed as some other cata: + + b [B] catamorphism F == c [C] catamorphism + +Given: + + b F == c + + ... + + B F == [F] dip C + + ... + + b[B]cata F == c[C]cata + + F(B(head, tail)) == C(head, F(tail)) + + 1 [2 3] B F 1 [2 3] F C + + + b F == c + B F == F C + + b [B] catamorphism F == c [C] catamorphism + b [B] catamorphism F == b F [C] catamorphism + + ... + +Or maybe, + + [F] map b [B] cata == c [C] cata ??? + + [F] map b [B] cata == b [F B] cata I think this is generally true, unless F consumes stack items + instead of just transforming TOS. Of course, there's always [F] unary. + b [F] unary [[F] unary B] cata + + [10 *] map 0 swap [+] step == 0 swap [10 * +] step + + +For example: + + F == 10 * + b == 0 + B == + + c == 0 + C == F + + + b F == c + 0 10 * == 0 + + B F == [F] dip C + + 10 * == [10 *] dip F + + + 10 * == [10 *] dip 10 * + + + n m + 10 * == 10(n+m) + + n m [10 *] dip 10 * + + n 10 * m 10 * + + 10n m 10 * + + 10n 10m + + 10n+10m + + 10n+10m = 10(n+m) + +Ergo: + + 0 [+] catamorphism 10 * == 0 [10 * +] catamorphism + +## The `step` combinator will usually be better to use than `catamorphism`. + + sum == 0 swap [+] step + sum == 0 [+] catamorphism + +# anamorphism catamorphism == hylomorphism +Here is (part of) the payoff. + +An anamorphism followed by (composed with) a +catamorphism is a hylomorphism, with the advantage that the hylomorphism +does not create the intermediate list structure. The values are stored in +either the call stack, for those implementations that use one, or in the pending +expression ("continuation") for the Joypy interpreter. They still have to +be somewhere, converting from an anamorphism and catamorphism to a hylomorphism +just prevents using additional storage and doing additional processing. + + range == [0 <=] [1 - dup] anamorphism + sum == 0 [+] catamorphism + + range sum == [0 <=] [1 - dup] anamorphism 0 [+] catamorphism + == [0 <=] 0 [1 - dup] [+] hylomorphism + +We can let the `hylomorphism` combinator build `range_sum` for us or just +substitute ourselves. + + H == [P] [pop c] [G] [dip F] genrec + range_sum == [0 <=] [pop 0] [1 - dup] [dip +] genrec + + + +```python +defs = ''' +anamorphism == [pop []] swap [dip swons] genrec +hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec +catamorphism == [[] =] roll> [uncons swap] swap hylomorphism +range == [0 <=] [1 - dup] anamorphism +sum == 0 [+] catamorphism +range_sum == [0 <=] 0 [1 - dup] [+] hylomorphism +''' + +DefinitionWrapper.add_definitions(defs, D) +``` + + +```python +J('10 range') +``` + + [9 8 7 6 5 4 3 2 1 0] + + + +```python +J('[9 8 7 6 5 4 3 2 1 0] sum') +``` + + 45 + + + +```python +V('10 range sum') +``` + + . 10 range sum + 10 . range sum + 10 . [0 <=] [1 - dup] anamorphism sum + 10 [0 <=] . [1 - dup] anamorphism sum + 10 [0 <=] [1 - dup] . anamorphism sum + 10 [0 <=] [1 - dup] . [pop []] swap [dip swons] genrec sum + 10 [0 <=] [1 - dup] [pop []] . swap [dip swons] genrec sum + 10 [0 <=] [pop []] [1 - dup] . [dip swons] genrec sum + 10 [0 <=] [pop []] [1 - dup] [dip swons] . genrec sum + 10 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [10] [0 <=] . infra first choice i sum + 10 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum + 10 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] . swaack first choice i sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i sum + 10 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 10 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 9 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 9 9 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 9 9 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons sum + 9 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 9 swons sum + 9 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 9 swons sum + 9 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 9 swons sum + 9 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 9 swons sum + 9 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 9 swons sum + 9 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [9] [0 <=] . infra first choice i 9 swons sum + 9 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum + 9 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] . swaack first choice i 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 9 swons sum + 9 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 9 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 8 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 8 8 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 8 8 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 9 swons sum + 8 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [8] [0 <=] . infra first choice i 8 swons 9 swons sum + 8 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum + 8 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] . swaack first choice i 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 8 swons 9 swons sum + 8 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 8 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 7 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 7 7 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 7 7 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 8 swons 9 swons sum + 7 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [7] [0 <=] . infra first choice i 7 swons 8 swons 9 swons sum + 7 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum + 7 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] . swaack first choice i 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 7 swons 8 swons 9 swons sum + 7 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 7 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 6 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 6 6 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 6 6 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 7 swons 8 swons 9 swons sum + 6 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [6] [0 <=] . infra first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] . swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 6 swons 7 swons 8 swons 9 swons sum + 6 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 6 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 5 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 5 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [5] [0 <=] . infra first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] . swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 4 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 4 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [4] [0 <=] . infra first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] . swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 3 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 3 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [3] [0 <=] . infra first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] . swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 2 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 2 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [2] [0 <=] . infra first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 1 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 1 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [1] [0 <=] . infra first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 0 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 0 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [0] [0 <=] . infra first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + True . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + True [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] . swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] True . choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [pop []] . i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . pop [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + . [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [] . 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [] 0 . swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [] 0 . swap cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [] . cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [0] . 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [0] 1 . swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [0] 1 . swap cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0] . cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [1 0] . 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [1 0] 2 . swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [1 0] 2 . swap cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 0] . cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [2 1 0] . 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [2 1 0] 3 . swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [2 1 0] 3 . swap cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [2 1 0] . cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [3 2 1 0] . 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [3 2 1 0] 4 . swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [3 2 1 0] 4 . swap cons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [3 2 1 0] . cons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [4 3 2 1 0] . 5 swons 6 swons 7 swons 8 swons 9 swons sum + [4 3 2 1 0] 5 . swons 6 swons 7 swons 8 swons 9 swons sum + [4 3 2 1 0] 5 . swap cons 6 swons 7 swons 8 swons 9 swons sum + 5 [4 3 2 1 0] . cons 6 swons 7 swons 8 swons 9 swons sum + [5 4 3 2 1 0] . 6 swons 7 swons 8 swons 9 swons sum + [5 4 3 2 1 0] 6 . swons 7 swons 8 swons 9 swons sum + [5 4 3 2 1 0] 6 . swap cons 7 swons 8 swons 9 swons sum + 6 [5 4 3 2 1 0] . cons 7 swons 8 swons 9 swons sum + [6 5 4 3 2 1 0] . 7 swons 8 swons 9 swons sum + [6 5 4 3 2 1 0] 7 . swons 8 swons 9 swons sum + [6 5 4 3 2 1 0] 7 . swap cons 8 swons 9 swons sum + 7 [6 5 4 3 2 1 0] . cons 8 swons 9 swons sum + [7 6 5 4 3 2 1 0] . 8 swons 9 swons sum + [7 6 5 4 3 2 1 0] 8 . swons 9 swons sum + [7 6 5 4 3 2 1 0] 8 . swap cons 9 swons sum + 8 [7 6 5 4 3 2 1 0] . cons 9 swons sum + [8 7 6 5 4 3 2 1 0] . 9 swons sum + [8 7 6 5 4 3 2 1 0] 9 . swons sum + [8 7 6 5 4 3 2 1 0] 9 . swap cons sum + 9 [8 7 6 5 4 3 2 1 0] . cons sum + [9 8 7 6 5 4 3 2 1 0] . sum + [9 8 7 6 5 4 3 2 1 0] . 0 [+] catamorphism + [9 8 7 6 5 4 3 2 1 0] 0 . [+] catamorphism + [9 8 7 6 5 4 3 2 1 0] 0 [+] . catamorphism + [9 8 7 6 5 4 3 2 1 0] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[9 8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i + [9 8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i + [9 8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] . swaack first choice i + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i + [9 8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + 9 [8 7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + [8 7 6 5 4 3 2 1 0] 9 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + [8 7 6 5 4 3 2 1 0] 9 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + + [8 7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 9 + + [8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 + + [8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] . swaack first choice i 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 9 + + [8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 + + 8 [7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 + + [7 6 5 4 3 2 1 0] 8 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 + + [7 6 5 4 3 2 1 0] 8 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 9 + + [7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] . swaack first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 8 + 9 + + [7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 + + 7 [6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 + + [6 5 4 3 2 1 0] 7 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 + + [6 5 4 3 2 1 0] 7 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 8 + 9 + + [6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[6 5 4 3 2 1 0]] [[] =] . infra first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] . swaack first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 7 + 8 + 9 + + [6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 + + 6 [5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 + + [5 4 3 2 1 0] 6 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 + + [5 4 3 2 1 0] 6 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 7 + 8 + 9 + + [5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[5 4 3 2 1 0]] [[] =] . infra first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] . swaack first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 [4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + [4 3 2 1 0] 5 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + [4 3 2 1 0] 5 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 6 + 7 + 8 + 9 + + [4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[4 3 2 1 0]] [[] =] . infra first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] . swaack first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 [3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] 4 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] 4 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3 2 1 0]] [[] =] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 1 0]] [[] =] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[1 0]] [[] =] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] 1 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] 1 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[0]] [[] =] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] 0 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] 0 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[]] [[] =] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 . + 4 + 5 + 6 + 7 + 8 + 9 + + 6 . 4 + 5 + 6 + 7 + 8 + 9 + + 6 4 . + 5 + 6 + 7 + 8 + 9 + + 10 . 5 + 6 + 7 + 8 + 9 + + 10 5 . + 6 + 7 + 8 + 9 + + 15 . 6 + 7 + 8 + 9 + + 15 6 . + 7 + 8 + 9 + + 21 . 7 + 8 + 9 + + 21 7 . + 8 + 9 + + 28 . 8 + 9 + + 28 8 . + 9 + + 36 . 9 + + 36 9 . + + 45 . + + + +```python +V('10 range_sum') +``` + + . 10 range_sum + 10 . range_sum + 10 . [0 <=] 0 [1 - dup] [+] hylomorphism + 10 [0 <=] . 0 [1 - dup] [+] hylomorphism + 10 [0 <=] 0 . [1 - dup] [+] hylomorphism + 10 [0 <=] 0 [1 - dup] . [+] hylomorphism + 10 [0 <=] 0 [1 - dup] [+] . hylomorphism + 10 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + 10 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + 10 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec + 10 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec + 10 [0 <=] [pop 0] [1 - dup] [dip +] . genrec + 10 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [10] [0 <=] . infra first choice i + 10 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i + 10 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] . swaack first choice i + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i + 10 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 10 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 9 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 9 9 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 9 9 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + + 9 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 9 + + 9 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 9 + + 9 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 9 + + 9 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 9 + + 9 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 9 + + 9 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [9] [0 <=] . infra first choice i 9 + + 9 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 + + 9 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] . swaack first choice i 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 9 + + 9 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 9 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 8 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 8 8 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 8 8 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 9 + + 8 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 8 + 9 + + 8 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 8 + 9 + + 8 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 8 + 9 + + 8 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 8 + 9 + + 8 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 8 + 9 + + 8 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [8] [0 <=] . infra first choice i 8 + 9 + + 8 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 + + 8 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] . swaack first choice i 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 8 + 9 + + 8 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 8 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 7 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 7 7 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 7 7 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 8 + 9 + + 7 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [7] [0 <=] . infra first choice i 7 + 8 + 9 + + 7 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 + + 7 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] . swaack first choice i 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 7 + 8 + 9 + + 7 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 7 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 6 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 6 6 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 6 6 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 7 + 8 + 9 + + 6 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [6] [0 <=] . infra first choice i 6 + 7 + 8 + 9 + + 6 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 + + 6 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] . swaack first choice i 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 + + 6 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 6 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 5 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 5 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 6 + 7 + 8 + 9 + + 5 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i 5 + 6 + 7 + 8 + 9 + + 5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 + + 5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 + + 5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 + + 4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 + + 4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 . + 4 + 5 + 6 + 7 + 8 + 9 + + 6 . 4 + 5 + 6 + 7 + 8 + 9 + + 6 4 . + 5 + 6 + 7 + 8 + 9 + + 10 . 5 + 6 + 7 + 8 + 9 + + 10 5 . + 6 + 7 + 8 + 9 + + 15 . 6 + 7 + 8 + 9 + + 15 6 . + 7 + 8 + 9 + + 21 . 7 + 8 + 9 + + 21 7 . + 8 + 9 + + 28 . 8 + 9 + + 28 8 . + 9 + + 36 . 9 + + 36 9 . + + 45 . + + +# Factorial Function and Paramorphisms +A paramorphism `P :: B -> A` is a recursion combinator that uses `dup` on intermediate values. + + n swap [P] [pop] [[F] dupdip G] primrec + +With +- `n :: A` is the "identity" for `F` (like 1 for multiplication, 0 for addition) +- `F :: (A, B) -> A` +- `G :: B -> B` generates the next `B` value. +- and lastly `P :: B -> Bool` detects the end of the series. + +For Factorial function (types `A` and `B` are both integer): + + n == 1 + F == * + G == -- + P == 1 <= + + +```python +define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec') +``` + +Try it with input 3 (omitting evaluation of predicate): + + 3 1 swap [1 <=] [pop] [[*] dupdip --] primrec + 1 3 [1 <=] [pop] [[*] dupdip --] primrec + + 1 3 [*] dupdip -- + 1 3 * 3 -- + 3 3 -- + 3 2 + + 3 2 [*] dupdip -- + 3 2 * 2 -- + 6 2 -- + 6 1 + + 6 1 [1 <=] [pop] [[*] dupdip --] primrec + + 6 1 pop + 6 + + +```python +J('3 factorial') +``` + + 6 + + +### Derive `paramorphism` from the form above. + + n swap [P] [pop] [[F] dupdip G] primrec + + n swap [P] [pop] [[F] dupdip G] primrec + n [P] [swap] dip [pop] [[F] dupdip G] primrec + n [P] [[F] dupdip G] [[swap] dip [pop]] dip primrec + n [P] [F] [dupdip G] cons [[swap] dip [pop]] dip primrec + n [P] [F] [G] [dupdip] swoncat cons [[swap] dip [pop]] dip primrec + + paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec + + +```python +define('paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec') +define('factorial == 1 [1 <=] [*] [--] paramorphism') +``` + + +```python +J('3 factorial') +``` + + 6 + + +# `tails` +An example of a paramorphism for lists given in the ["Bananas..." paper](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) is `tails` which returns the list of "tails" of a list. + + [1 2 3] tails == [[] [3] [2 3]] + +Using `paramorphism` we would write: + + n == [] + F == rest swons + G == rest + P == not + + tails == [] [not] [rest swons] [rest] paramorphism + + +```python +define('tails == [] [not] [rest swons] [rest] paramorphism') +``` + + +```python +J('[1 2 3] tails') +``` + + [[] [3] [2 3]] + + + +```python +J('25 range tails [popop] infra [sum] map') +``` + + [1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276] + + + +```python +J('25 range [range_sum] map') +``` + + [276 253 231 210 190 171 153 136 120 105 91 78 66 55 45 36 28 21 15 10 6 3 1 0 0] + + +### Factoring `rest` +Right before the recursion begins we have: + + [] [1 2 3] [not] [pop] [[rest swons] dupdip rest] primrec + +But we might prefer to factor `rest` in the quote: + + [] [1 2 3] [not] [pop] [rest [swons] dupdip] primrec + +There's no way to do that with the `paramorphism` combinator as defined. We would have to write and use a slightly different recursion combinator that accepted an additional "preprocessor" function `[H]` and built: + + n swap [P] [pop] [H [F] dupdip G] primrec + +Or just write it out manually. This is yet another place where the *sufficiently smart compiler* will one day automatically refactor the code. We could write a `paramorphism` combinator that checked `[F]` and `[G]` for common prefix and extracted it. + +# Patterns of Recursion +Our story so far... + +- A combiner `F :: (B, B) -> B` +- A predicate `P :: A -> Bool` to detect the base case +- A base case value `c :: B` + + +### Hylo-, Ana-, Cata- + + w/ G :: A -> (A, B) + + H == [P ] [pop c ] [G ] [dip F ] genrec + A == [P ] [pop []] [G ] [dip swons] genrec + C == [[] =] [pop c ] [uncons swap] [dip F ] genrec + +### Para-, ?-, ?- + + w/ G :: B -> B + + P == c swap [P ] [pop] [[F ] dupdip G ] primrec + ? == [] swap [P ] [pop] [[swons] dupdip G ] primrec + ? == c swap [[] =] [pop] [[F ] dupdip uncons swap] primrec + + +# Four Generalizations +There are at least four kinds of recursive combinator, depending on two choices. The first choice is whether the combiner function should be evaluated during the recursion or pushed into the pending expression to be "collapsed" at the end. The second choice is whether the combiner needs to operate on the current value of the datastructure or the generator's output. + + H == [P] [pop c] [G ] [dip F] genrec + H == c swap [P] [pop] [G [F] dip ] [i] genrec + H == [P] [pop c] [ [G] dupdip ] [dip F] genrec + H == c swap [P] [pop] [ [F] dupdip G] [i] genrec + +Consider: + + ... a G [H] dip F w/ a G == a' b + ... c a G [F] dip H a G == b a' + ... a [G] dupdip [H] dip F a G == a' + ... c a [F] dupdip G H a G == a' + +### 1 + + H == [P] [pop c] [G] [dip F] genrec + +Iterate n times. + + ... a [P] [pop c] [G] [dip F] genrec + ... a G [H] dip F + ... a' b [H] dip F + ... a' H b F + ... a' G [H] dip F b F + ... a'' b [H] dip F b F + ... a'' H b F b F + ... a'' G [H] dip F b F b F + ... a''' b [H] dip F b F b F + ... a''' H b F b F b F + ... a''' pop c b F b F b F + ... c b F b F b F + +This form builds up a continuation that contains the intermediate results along with the pending combiner functions. When the base case is reached the last term is replaced by the identity value c and the continuation "collapses" into the final result. + +### 2 +When you can start with the identity value c on the stack and the combiner can operate as you go, using the intermediate results immediately rather than queuing them up, use this form. An important difference is that the generator function must return its results in the reverse order. + + H == c swap [P] [pop] [G [F] dip] primrec + + ... c a G [F] dip H + ... c b a' [F] dip H + ... c b F a' H + ... c b F a' G [F] dip H + ... c b F b a'' [F] dip H + ... c b F b F a'' H + ... c b F b F a'' G [F] dip H + ... c b F b F b a''' [F] dip H + ... c b F b F b F a''' H + ... c b F b F b F a''' pop + ... c b F b F b F + +The end line here is the same as for above, but only because we didn't evaluate `F` when it normally would have been. + +### 3 +If the combiner and the generator both need to work on the current value then `dup` must be used at some point, and the generator must produce one item instead of two (the b is instead the duplicate of a.) + + + H == [P] [pop c] [[G] dupdip] [dip F] genrec + + ... a [G] dupdip [H] dip F + ... a G a [H] dip F + ... a' a [H] dip F + ... a' H a F + ... a' [G] dupdip [H] dip F a F + ... a' G a' [H] dip F a F + ... a'' a' [H] dip F a F + ... a'' H a' F a F + ... a'' [G] dupdip [H] dip F a' F a F + ... a'' G a'' [H] dip F a' F a F + ... a''' a'' [H] dip F a' F a F + ... a''' H a'' F a' F a F + ... a''' pop c a'' F a' F a F + ... c a'' F a' F a F + +### 4 +And, last but not least, if you can combine as you go, starting with c, and the combiner needs to work on the current item, this is the form: + + W == c swap [P] [pop] [[F] dupdip G] primrec + + ... a c swap [P] [pop] [[F] dupdip G] primrec + ... c a [P] [pop] [[F] dupdip G] primrec + ... c a [F] dupdip G W + ... c a F a G W + ... c a F a' W + ... c a F a' [F] dupdip G W + ... c a F a' F a' G W + ... c a F a' F a'' W + ... c a F a' F a'' [F] dupdip G W + ... c a F a' F a'' F a'' G W + ... c a F a' F a'' F a''' W + ... c a F a' F a'' F a''' pop + ... c a F a' F a'' F + +Each of the four variations above can be specialized to ana- and catamorphic forms. + + +```python +def WTFmorphism(c, F, P, G): + '''Return a hylomorphism function H.''' + + def H(a, d=c): + if P(a): + result = d + else: + a, b = G(a) + result = H(a, F(d, b)) + return result + + return H +``` + + +```python +F = lambda a, b: a + b +P = lambda n: n <= 1 +G = lambda n: (n - 1, n - 1) + +wtf = WTFmorphism(0, F, P, G) + +print wtf(5) +``` + + 10 + + + H == [P ] [pop c ] [G ] [dip F ] genrec + + +```python +DefinitionWrapper.add_definitions(''' +P == 1 <= +Ga == -- dup +Gb == -- +c == 0 +F == + +''', D) +``` + + +```python +V('[1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec') +``` + + . [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec + [1 2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec + [1 2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec + [1 2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec + [1 2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec + [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec + [1 2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[1 2 3]] [[] =] . infra first choice i + [1 2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i + [1 2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i + False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i + False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] . swaack first choice i + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i + [1 2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons + 1 [2 3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons + [2 3] 1 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons + [2 3] 1 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons + [2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 1 swons + [2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 1 swons + [2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 1 swons + [2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 1 swons + [2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 1 swons + [2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[2 3]] [[] =] . infra first choice i 1 swons + [2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons + [2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons + False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons + False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] . swaack first choice i 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 1 swons + [2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons + 2 [3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons + [3] 2 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons + [3] 2 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 1 swons + [3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons + [3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons + [3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 2 swons 1 swons + [3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 2 swons 1 swons + [3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 2 swons 1 swons + [3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[3]] [[] =] . infra first choice i 2 swons 1 swons + [3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons + [3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons + False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons + False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] . swaack first choice i 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 2 swons 1 swons + [3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons + 3 [] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons + [] 3 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons + [] 3 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 2 swons 1 swons + [] . [[] =] [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] . [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] . [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] [uncons swap] . [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] [uncons swap] [dip swons] . genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 3 swons 2 swons 1 swons + [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[]] [[] =] . infra first choice i 3 swons 2 swons 1 swons + [] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons + [] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons + True . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons + True [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] . swaack first choice i 3 swons 2 swons 1 swons + [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 3 swons 2 swons 1 swons + [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] True . choice i 3 swons 2 swons 1 swons + [] [pop []] . i 3 swons 2 swons 1 swons + [] . pop [] 3 swons 2 swons 1 swons + . [] 3 swons 2 swons 1 swons + [] . 3 swons 2 swons 1 swons + [] 3 . swons 2 swons 1 swons + [] 3 . swap cons 2 swons 1 swons + 3 [] . cons 2 swons 1 swons + [3] . 2 swons 1 swons + [3] 2 . swons 1 swons + [3] 2 . swap cons 1 swons + 2 [3] . cons 1 swons + [2 3] . 1 swons + [2 3] 1 . swons + [2 3] 1 . swap cons + 1 [2 3] . cons + [1 2 3] . + + + +```python +V('3 [P] [pop c] [Ga] [dip F] genrec') +``` + + . 3 [P] [pop c] [Ga] [dip F] genrec + 3 . [P] [pop c] [Ga] [dip F] genrec + 3 [P] . [pop c] [Ga] [dip F] genrec + 3 [P] [pop c] . [Ga] [dip F] genrec + 3 [P] [pop c] [Ga] . [dip F] genrec + 3 [P] [pop c] [Ga] [dip F] . genrec + 3 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [3] [P] . infra first choice i + 3 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + 3 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + 3 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] . swaack first choice i + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i + 3 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F + 3 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F + 2 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F + 2 2 . [[P] [pop c] [Ga] [dip F] genrec] dip F + 2 2 [[P] [pop c] [Ga] [dip F] genrec] . dip F + 2 . [P] [pop c] [Ga] [dip F] genrec 2 F + 2 [P] . [pop c] [Ga] [dip F] genrec 2 F + 2 [P] [pop c] . [Ga] [dip F] genrec 2 F + 2 [P] [pop c] [Ga] . [dip F] genrec 2 F + 2 [P] [pop c] [Ga] [dip F] . genrec 2 F + 2 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [2] [P] . infra first choice i 2 F + 2 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + 2 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + 2 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] . swaack first choice i 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i 2 F + 2 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 2 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 1 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 1 1 . [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 1 1 [[P] [pop c] [Ga] [dip F] genrec] . dip F 2 F + 1 . [P] [pop c] [Ga] [dip F] genrec 1 F 2 F + 1 [P] . [pop c] [Ga] [dip F] genrec 1 F 2 F + 1 [P] [pop c] . [Ga] [dip F] genrec 1 F 2 F + 1 [P] [pop c] [Ga] . [dip F] genrec 1 F 2 F + 1 [P] [pop c] [Ga] [dip F] . genrec 1 F 2 F + 1 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 1 F 2 F + 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [1] [P] . infra first choice i 1 F 2 F + 1 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + 1 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + 1 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + True . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + True [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] . swaack first choice i 1 F 2 F + 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F + 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F + 1 [pop c] . i 1 F 2 F + 1 . pop c 1 F 2 F + . c 1 F 2 F + . 0 1 F 2 F + 0 . 1 F 2 F + 0 1 . F 2 F + 0 1 . + 2 F + 1 . 2 F + 1 2 . F + 1 2 . + + 3 . + + + +```python +V('3 [P] [pop []] [Ga] [dip swons] genrec') +``` + + . 3 [P] [pop []] [Ga] [dip swons] genrec + 3 . [P] [pop []] [Ga] [dip swons] genrec + 3 [P] . [pop []] [Ga] [dip swons] genrec + 3 [P] [pop []] . [Ga] [dip swons] genrec + 3 [P] [pop []] [Ga] . [dip swons] genrec + 3 [P] [pop []] [Ga] [dip swons] . genrec + 3 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [3] [P] . infra first choice i + 3 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + 3 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + 3 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] . swaack first choice i + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i + 3 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 3 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 2 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 2 2 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 2 2 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons + 2 . [P] [pop []] [Ga] [dip swons] genrec 2 swons + 2 [P] . [pop []] [Ga] [dip swons] genrec 2 swons + 2 [P] [pop []] . [Ga] [dip swons] genrec 2 swons + 2 [P] [pop []] [Ga] . [dip swons] genrec 2 swons + 2 [P] [pop []] [Ga] [dip swons] . genrec 2 swons + 2 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [2] [P] . infra first choice i 2 swons + 2 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + 2 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + 2 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i 2 swons + 2 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 2 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 1 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 1 1 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 1 1 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons 2 swons + 1 . [P] [pop []] [Ga] [dip swons] genrec 1 swons 2 swons + 1 [P] . [pop []] [Ga] [dip swons] genrec 1 swons 2 swons + 1 [P] [pop []] . [Ga] [dip swons] genrec 1 swons 2 swons + 1 [P] [pop []] [Ga] . [dip swons] genrec 1 swons 2 swons + 1 [P] [pop []] [Ga] [dip swons] . genrec 1 swons 2 swons + 1 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons + 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [1] [P] . infra first choice i 1 swons 2 swons + 1 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + 1 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + 1 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + True . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + True [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons + 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 1 swons 2 swons + 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] True . choice i 1 swons 2 swons + 1 [pop []] . i 1 swons 2 swons + 1 . pop [] 1 swons 2 swons + . [] 1 swons 2 swons + [] . 1 swons 2 swons + [] 1 . swons 2 swons + [] 1 . swap cons 2 swons + 1 [] . cons 2 swons + [1] . 2 swons + [1] 2 . swons + [1] 2 . swap cons + 2 [1] . cons + [2 1] . + + + +```python +V('[2 1] [[] =] [pop c ] [uncons swap] [dip F] genrec') +``` + + . [2 1] [[] =] [pop c] [uncons swap] [dip F] genrec + [2 1] . [[] =] [pop c] [uncons swap] [dip F] genrec + [2 1] [[] =] . [pop c] [uncons swap] [dip F] genrec + [2 1] [[] =] [pop c] . [uncons swap] [dip F] genrec + [2 1] [[] =] [pop c] [uncons swap] . [dip F] genrec + [2 1] [[] =] [pop c] [uncons swap] [dip F] . genrec + [2 1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[2 1]] [[] =] . infra first choice i + [2 1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i + [2 1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i + False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i + False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] . swaack first choice i + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i + [2 1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F + 2 [1] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F + [1] 2 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F + [1] 2 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F + [1] . [[] =] [pop c] [uncons swap] [dip F] genrec 2 F + [1] [[] =] . [pop c] [uncons swap] [dip F] genrec 2 F + [1] [[] =] [pop c] . [uncons swap] [dip F] genrec 2 F + [1] [[] =] [pop c] [uncons swap] . [dip F] genrec 2 F + [1] [[] =] [pop c] [uncons swap] [dip F] . genrec 2 F + [1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[1]] [[] =] . infra first choice i 2 F + [1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F + [1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F + False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F + False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] . swaack first choice i 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i 2 F + [1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F + 1 [] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F + [] 1 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F + [] 1 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F 2 F + [] . [[] =] [pop c] [uncons swap] [dip F] genrec 1 F 2 F + [] [[] =] . [pop c] [uncons swap] [dip F] genrec 1 F 2 F + [] [[] =] [pop c] . [uncons swap] [dip F] genrec 1 F 2 F + [] [[] =] [pop c] [uncons swap] . [dip F] genrec 1 F 2 F + [] [[] =] [pop c] [uncons swap] [dip F] . genrec 1 F 2 F + [] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 1 F 2 F + [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[]] [[] =] . infra first choice i 1 F 2 F + [] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F + [] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F + True . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F + True [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] . swaack first choice i 1 F 2 F + [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F + [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F + [] [pop c] . i 1 F 2 F + [] . pop c 1 F 2 F + . c 1 F 2 F + . 0 1 F 2 F + 0 . 1 F 2 F + 0 1 . F 2 F + 0 1 . + 2 F + 1 . 2 F + 1 2 . F + 1 2 . + + 3 . + + +## Appendix - Fun with Symbols + + |[ (c, F), (G, P) ]| == (|c, F|) • [(G, P)] + +["Bananas, Lenses, & Barbed Wire"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) + + (|...|) [(...)] [<...>] + +I think they are having slightly too much fun with the symbols. + +"Too much is always better than not enough." + +# Tree with node and list of trees. + + tree = [] | [node [tree*]] + +### `treestep` + + tree z [C] [N] treestep + + + [] z [C] [N] treestep + --------------------------- + z + + + [node [tree*]] z [C] [N] treestep + --------------------------------------- w/ K == z [C] [N] treestep + node N [tree*] [K] map C + +### Derive the recursive form. + K == [not] [pop z] [J] ifte + + + [node [tree*]] J + ------------------------------ + node N [tree*] [K] map C + + + J == .. [N] .. [K] .. [C] .. + + [node [tree*]] uncons [N] dip + node [[tree*]] [N] dip + node N [[tree*]] + + node N [[tree*]] i [K] map + node N [tree*] [K] map + node N [K.tree*] + + J == uncons [N] dip i [K] map [C] i + + K == [not] [pop z] [uncons [N] dip i [K] map [C] i] ifte + K == [not] [pop z] [uncons [N] dip i] [map [C] i] genrec + +### Extract the givens to parameterize the program. + [not] [pop z] [uncons [N] dip unquote] [map [C] i] genrec + [not] [z] [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec + [not] z unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec + z [not] swap unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec + \............TS0............/ + z TS0 [uncons [N] dip unquote] [map [C] i] genrec + z [uncons [N] dip unquote] [TS0] dip [map [C] i] genrec + z [[N] dip unquote] [uncons] swoncat [TS0] dip [map [C] i] genrec + z [N] [dip unquote] cons [uncons] swoncat [TS0] dip [map [C] i] genrec + \...........TS1.................../ + z [N] TS1 [TS0] dip [map [C] i] genrec + z [N] [map [C] i] [TS1 [TS0] dip] dip genrec + z [N] [map C ] [TS1 [TS0] dip] dip genrec + z [N] [C] [map] swoncat [TS1 [TS0] dip] dip genrec + z [C] [N] swap [map] swoncat [TS1 [TS0] dip] dip genrec + + TS0 == [not] swap unit [pop] swoncat + TS1 == [dip i] cons [uncons] swoncat + treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec + + [] 0 [C] [N] treestep + --------------------------- + 0 + + + [n [tree*]] 0 [sum +] [] treestep + -------------------------------------------------- + n [tree*] [0 [sum +] [] treestep] map sum + + + +```python +DefinitionWrapper.add_definitions(''' + + TS0 == [not] swap unit [pop] swoncat + TS1 == [dip i] cons [uncons] swoncat +treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec + +''', D) +``` + + +```python +V('[] 0 [sum +] [] treestep') +``` + + . [] 0 [sum +] [] treestep + [] . 0 [sum +] [] treestep + [] 0 . [sum +] [] treestep + [] 0 [sum +] . [] treestep + [] 0 [sum +] [] . treestep + [] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec + [] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec + [] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec + [] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec + [] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec + [] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec + [] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec + [] 0 [] . TS1 [TS0] dip [map sum +] genrec + [] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec + [] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec + [] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec + [] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec + [] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec + [] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec + [] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec + [] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec + [] 0 . TS0 [uncons [] dip i] [map sum +] genrec + [] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec + [] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec + [] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec + [] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec + [] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec + [] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec + [] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte + [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[]] [not] . infra first choice i + [] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i + True . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i + True [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] . swaack first choice i + [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [True] . first choice i + [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] True . choice i + [] [pop 0] . i + [] . pop 0 + . 0 + 0 . + + + +```python +V('[23 []] 0 [sum +] [] treestep') +``` + + . [23 []] 0 [sum +] [] treestep + [23 []] . 0 [sum +] [] treestep + [23 []] 0 . [sum +] [] treestep + [23 []] 0 [sum +] . [] treestep + [23 []] 0 [sum +] [] . treestep + [23 []] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec + [23 []] 0 [] . TS1 [TS0] dip [map sum +] genrec + [23 []] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec + [23 []] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec + [23 []] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec + [23 []] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec + [23 []] 0 . TS0 [uncons [] dip i] [map sum +] genrec + [23 []] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec + [23 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec + [23 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec + [23 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 []]] [not] . infra first choice i + [23 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i + False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i + False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] . swaack first choice i + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i + [23 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + + 23 [] . sum + + 23 [] . 0 [+] catamorphism + + 23 [] 0 . [+] catamorphism + + 23 [] 0 [+] . catamorphism + + 23 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + + 23 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + + 23 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + + 23 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + + 23 [] [[] =] 0 [uncons swap] [+] . hylomorphism + + 23 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + + 23 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + + 23 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + + 23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i + + 23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i + + 23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i + + 23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i + + 23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + + 23 [] [pop 0] . i + + 23 [] . pop 0 + + 23 . 0 + + 23 0 . + + 23 . + + + +```python +V('[23 [[2 []] [3 []]]] 0 [sum +] [] treestep') +``` + + . [23 [[2 []] [3 []]]] 0 [sum +] [] treestep + [23 [[2 []] [3 []]]] . 0 [sum +] [] treestep + [23 [[2 []] [3 []]]] 0 . [sum +] [] treestep + [23 [[2 []] [3 []]]] 0 [sum +] . [] treestep + [23 [[2 []] [3 []]]] 0 [sum +] [] . treestep + [23 [[2 []] [3 []]]] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec + [23 [[2 []] [3 []]]] 0 [] . TS1 [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 . TS0 [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec + [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 [[2 []] [3 []]]]] [not] . infra first choice i + [23 [[2 []] [3 []]]] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i + False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i + False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] . swaack first choice i + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i + [23 [[2 []] [3 []]]] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[[2 []] [3 []]]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[[2 []] [3 []]]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [[[2 []] [3 []]]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[[2 []] [3 []]]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[2 []] [3 []]] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + + 23 [] [[[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first] . infra sum + + . [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + [[3 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[3 []] 23] [not] . infra first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] . swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . 0 [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 . [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 [+] . catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 3 23] [[] =] . infra first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] . swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 3 23] . first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [pop 0] . i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . pop 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 0 . + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + [3 23] . first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 3 . [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 3 [[2 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 3 [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [23] swaack sum + + 23 [2 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[2 []] 23] [not] . infra first choice i [3] swaack first [23] swaack sum + + 23 [2 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum + + 23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum + + 23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] . swaack first choice i [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [3] swaack first [23] swaack sum + + 23 [2 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [3] swaack first [23] swaack sum + + 23 2 [] . sum + [3] swaack first [23] swaack sum + + 23 2 [] . 0 [+] catamorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 . [+] catamorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 [+] . catamorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [3] swaack first [23] swaack sum + + 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 2 23] [[] =] . infra first choice i + [3] swaack first [23] swaack sum + + 23 2 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] . swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 2 23] . first choice i + [3] swaack first [23] swaack sum + + 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [3] swaack first [23] swaack sum + + 23 2 [] [pop 0] . i + [3] swaack first [23] swaack sum + + 23 2 [] . pop 0 + [3] swaack first [23] swaack sum + + 23 2 . 0 + [3] swaack first [23] swaack sum + + 23 2 0 . + [3] swaack first [23] swaack sum + + 23 2 . [3] swaack first [23] swaack sum + + 23 2 [3] . swaack first [23] swaack sum + + 3 [2 23] . first [23] swaack sum + + 3 2 . [23] swaack sum + + 3 2 [23] . swaack sum + + 23 [2 3] . sum + + 23 [2 3] . 0 [+] catamorphism + + 23 [2 3] 0 . [+] catamorphism + + 23 [2 3] 0 [+] . catamorphism + + 23 [2 3] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + + 23 [2 3] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + + 23 [2 3] [[] =] 0 [+] . [uncons swap] swap hylomorphism + + 23 [2 3] [[] =] 0 [+] [uncons swap] . swap hylomorphism + + 23 [2 3] [[] =] 0 [uncons swap] [+] . hylomorphism + + 23 [2 3] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + + 23 [2 3] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + + 23 [2 3] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [dip +] . genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 3] 23] [[] =] . infra first choice i + + 23 [2 3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i + + 23 [2 3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i + + 23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i + + 23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] . swaack first choice i + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i + + 23 [2 3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + + 23 2 [3] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + + 23 [3] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + + 23 [3] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + + + 23 [3] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + + + 23 [3] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + + + 23 [3] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + + + 23 [3] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + + + 23 [3] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + + + 23 [3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3] 23] [[] =] . infra first choice i 2 + + + 23 [3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + + + 23 [3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + + + 23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + + + 23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] . swaack first choice i 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + + + 23 [3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + + + 23 3 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + + + 23 [] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + + + 23 [] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + + + 23 [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 2 + + + 23 [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 2 + + + 23 [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 2 + + + 23 [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 2 + + + 23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 2 + + + 23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 2 + + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i 3 + 2 + + + 23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + + + 23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + + + 23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + + + 23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i 3 + 2 + + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i 3 + 2 + + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 3 + 2 + + + 23 [] [pop 0] . i 3 + 2 + + + 23 [] . pop 0 3 + 2 + + + 23 . 0 3 + 2 + + + 23 0 . 3 + 2 + + + 23 0 3 . + 2 + + + 23 3 . 2 + + + 23 3 2 . + + + 23 5 . + + 28 . + + + +```python +J('[23 [[2 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]] [3 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]]]] 0 [sum +] [] treestep') +``` + + 140 + + + +```python +J('[] [] [unit cons] [23 +] treestep') +``` + + [] + + + +```python +J('[23 []] [] [unit cons] [23 +] treestep') +``` + + [46 []] + + + +```python +J('[23 [[2 []] [3 []]]] [] [unit cons] [23 +] treestep') +``` + + [46 [[25 []] [26 []]]] + + + +```python +define('treemap == [] [unit cons] roll< treestep') +``` + + +```python +J('[23 [[2 []] [3 []]]] [23 +] treemap') +``` + + [46 [[25 []] [26 []]]] + diff --git a/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.rst b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.rst new file mode 100644 index 0000000..14cbbb7 --- /dev/null +++ b/docs/Hylo-,_Ana-,_Cata-,_and_Para-morphisms_-_Recursion_Combinators.rst @@ -0,0 +1,2680 @@ + +Cf. `"Bananas, Lenses, & Barbed +Wire" `__ + +`Hylomorphism `__ +==================================================================================== + +A +`hylomorphism `__ +``H :: A -> B`` converts a value of type A into a value of type B by +means of: + +- A generator ``G :: A -> (A, B)`` +- A combiner ``F :: (B, B) -> B`` +- A predicate ``P :: A -> Bool`` to detect the base case +- A base case value ``c :: B`` +- Recursive calls (zero or more); it has a "call stack in the form of a + cons list". + +It may be helpful to see this function implemented in imperative Python +code. + +.. code:: ipython2 + + def hylomorphism(c, F, P, G): + '''Return a hylomorphism function H.''' + + def H(a): + if P(a): + result = c + else: + b, aa = G(a) + result = F(b, H(aa)) + return result + + return H + +Finding `Triangular Numbers `__ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +As a concrete example let's use a function that, given a positive +integer, returns the sum of all positive integers less than that one. +(In this case the types A and B are both ``int``.) ### With ``range()`` +and ``sum()`` + +.. code:: ipython2 + + r = range(10) + r + + + + +.. parsed-literal:: + + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + + + +.. code:: ipython2 + + sum(r) + + + + +.. parsed-literal:: + + 45 + + + +.. code:: ipython2 + + range_sum = lambda n: sum(range(n)) + range_sum(10) + + + + +.. parsed-literal:: + + 45 + + + +As a hylomorphism +~~~~~~~~~~~~~~~~~ + +.. code:: ipython2 + + G = lambda n: (n - 1, n - 1) + F = lambda a, b: a + b + P = lambda n: n <= 1 + + H = hylomorphism(0, F, P, G) + +.. code:: ipython2 + + H(10) + + + + +.. parsed-literal:: + + 45 + + + +If you were to run the above code in a debugger and check out the call +stack you would find that the variable ``b`` in each call to ``H()`` is +storing the intermediate values as ``H()`` recurses. This is what was +meant by "call stack in the form of a cons list". + +Joy Preamble +~~~~~~~~~~~~ + +.. code:: ipython2 + + from notebook_preamble import D, DefinitionWrapper, J, V, define + +Hylomorphism in Joy +------------------- + +We can define a combinator ``hylomorphism`` that will make a +hylomorphism combinator ``H`` from constituent parts. + +:: + + H == c [F] [P] [G] hylomorphism + +The function ``H`` is recursive, so we start with ``ifte`` and set the +else-part to some function ``J`` that will contain a quoted copy of +``H``. (The then-part just discards the leftover ``a`` and replaces it +with the base case value ``c``.) + +:: + + H == [P] [pop c] [J] ifte + +The else-part ``J`` gets just the argument ``a`` on the stack. + +:: + + a J + a G The first thing to do is use the generator G + aa b which produces b and a new aa + aa b [H] dip we recur with H on the new aa + aa H b F and run F on the result. + +This gives us a definition for ``J``. + +:: + + J == G [H] dip F + +Plug it in and convert to genrec. + +:: + + H == [P] [pop c] [G [H] dip F] ifte + H == [P] [pop c] [G] [dip F] genrec + +This is the form of a hylomorphism in Joy, which nicely illustrates that +it is a simple specialization of the general recursion combinator. + +:: + + H == [P] [pop c] [G] [dip F] genrec + +Derivation of ``hylomorphism`` +------------------------------ + +Now we just need to derive a definition that builds the ``genrec`` +arguments out of the pieces given to the ``hylomorphism`` combinator. + +:: + + H == [P] [pop c] [G] [dip F] genrec + [P] [c] [pop] swoncat [G] [F] [dip] swoncat genrec + [P] c unit [pop] swoncat [G] [F] [dip] swoncat genrec + [P] c [G] [F] [unit [pop] swoncat] dipd [dip] swoncat genrec + +Working in reverse: - Use ``swoncat`` twice to decouple ``[c]`` and +``[F]``. - Use ``unit`` to dequote ``c``. - Use ``dipd`` to untangle +``[unit [pop] swoncat]`` from the givens. + +At this point all of the arguments (givens) to the hylomorphism are to +the left so we have a definition for ``hylomorphism``: + +:: + + hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec + +The order of parameters is different than the one we started with but +that hardly matters, you can rearrange them or just supply them in the +expected order. + +:: + + [P] c [G] [F] hylomorphism == H + +.. code:: ipython2 + + define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec') + +Demonstrate summing a range of integers from 0 to n-1. + +- ``[P]`` is ``[0 <=]`` +- ``c`` is ``0`` +- ``[G]`` is ``[1 - dup]`` +- ``[F]`` is ``[+]`` + +So to sum the positive integers less than five we can do this. + +.. code:: ipython2 + + V('5 [0 <=] 0 [1 - dup] [+] hylomorphism') + + +.. parsed-literal:: + + . 5 [0 <=] 0 [1 - dup] [+] hylomorphism + 5 . [0 <=] 0 [1 - dup] [+] hylomorphism + 5 [0 <=] . 0 [1 - dup] [+] hylomorphism + 5 [0 <=] 0 . [1 - dup] [+] hylomorphism + 5 [0 <=] 0 [1 - dup] . [+] hylomorphism + 5 [0 <=] 0 [1 - dup] [+] . hylomorphism + 5 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + 5 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + 5 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec + 5 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec + 5 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec + 5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec + 5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i + 5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i + 5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i + 5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + + 4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 + + 4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 + + 4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 + + 4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 + + 4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 + + 4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 + + 4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + + 4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 + + 4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + + 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 + + 3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 + + 3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 + + 3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 + + 3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 + + 3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 + + 3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 + + 3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + + 3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 + + 3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + + 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 + + 2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 + + 2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 + + 2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + + 2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 + + 2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + + 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 + + 1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 + + 1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 + + 1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + + 1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + + 1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + + 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + + 0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + + 0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 + + 0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + + 0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + + True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + + True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + + 0 [pop 0] . i 0 + 1 + 2 + 3 + 4 + + 0 . pop 0 0 + 1 + 2 + 3 + 4 + + . 0 0 + 1 + 2 + 3 + 4 + + 0 . 0 + 1 + 2 + 3 + 4 + + 0 0 . + 1 + 2 + 3 + 4 + + 0 . 1 + 2 + 3 + 4 + + 0 1 . + 2 + 3 + 4 + + 1 . 2 + 3 + 4 + + 1 2 . + 3 + 4 + + 3 . 3 + 4 + + 3 3 . + 4 + + 6 . 4 + + 6 4 . + + 10 . + + +Anamorphism +=========== + +An anamorphism can be defined as a hylomorphism that uses ``[]`` for +``c`` and ``swons`` for ``F``. + +:: + + [P] [G] anamorphism == [P] [] [G] [swons] hylomorphism == A + +This allows us to define an anamorphism combinator in terms of the +hylomorphism combinator. + +:: + + [] swap [swons] hylomorphism == anamorphism + +Partial evaluation gives us a "pre-cooked" form. + +:: + + [P] [G] . anamorphism + [P] [G] . [] swap [swons] hylomorphism + [P] [G] [] . swap [swons] hylomorphism + [P] [] [G] . [swons] hylomorphism + [P] [] [G] [swons] . hylomorphism + [P] [] [G] [swons] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [P] [] [G] [swons] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [P] [] . unit [pop] swoncat [G] [swons] [dip] swoncat genrec + [P] [[]] [pop] . swoncat [G] [swons] [dip] swoncat genrec + [P] [pop []] [G] [swons] [dip] . swoncat genrec + + [P] [pop []] [G] [dip swons] genrec + +(We could also have just substituted for ``c`` and ``F`` in the +definition of ``H``.) + +:: + + H == [P] [pop c ] [G] [dip F ] genrec + A == [P] [pop []] [G] [dip swons] genrec + +The partial evaluation is overkill in this case but it serves as a +reminder that this sort of program specialization can, in many cases, be +carried out automatically.) + +Untangle ``[G]`` from ``[pop []]`` using ``swap``. + +:: + + [P] [G] [pop []] swap [dip swons] genrec + +All of the arguments to ``anamorphism`` are to the left, so we have a +definition for it. + +:: + + anamorphism == [pop []] swap [dip swons] genrec + +An example of an anamorphism is the range function. + +:: + + range == [0 <=] [1 - dup] anamorphism + +Catamorphism +============ + +A catamorphism can be defined as a hylomorphism that uses +``[uncons swap]`` for ``[G]`` and ``[[] =]`` for the predicate ``[P]``. + +:: + + c [F] catamorphism == [[] =] c [uncons swap] [F] hylomorphism == C + +This allows us to define a ``catamorphism`` combinator in terms of the +``hylomorphism`` combinator. + +:: + + [[] =] roll> [uncons swap] swap hylomorphism == catamorphism + +Partial evaluation doesn't help much. + +:: + + c [F] . catamorphism + c [F] . [[] =] roll> [uncons swap] swap hylomorphism + c [F] [[] =] . roll> [uncons swap] swap hylomorphism + [[] =] c [F] [uncons swap] . swap hylomorphism + [[] =] c [uncons swap] [F] . hylomorphism + [[] =] c [uncons swap] [F] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [[] =] c . unit [pop] swoncat [uncons swap] [F] [dip] swoncat genrec + [[] =] [c] [pop] . swoncat [uncons swap] [F] [dip] swoncat genrec + [[] =] [pop c] [uncons swap] [F] [dip] . swoncat genrec + [[] =] [pop c] [uncons swap] [dip F] genrec + +Because the arguments to catamorphism have to be prepared (unlike the +arguments to anamorphism, which only need to be rearranged slightly) +there isn't much point to "pre-cooking" the definition. + +:: + + catamorphism == [[] =] roll> [uncons swap] swap hylomorphism + +An example of a catamorphism is the sum function. + +:: + + sum == 0 [+] catamorphism + +"Fusion Law" for catas (UNFINISHED!!!) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +I'm not sure exactly how to translate the "Fusion Law" for catamorphisms +into Joy. + +I know that a ``map`` composed with a cata can be expressed as a new +cata: + +:: + + [F] map b [B] cata == b [F B] cata + +But this isn't the one described in "Bananas...". That's more like: + +A cata composed with some function can be expressed as some other cata: + +:: + + b [B] catamorphism F == c [C] catamorphism + +Given: + +:: + + b F == c + + ... + + B F == [F] dip C + + ... + + b[B]cata F == c[C]cata + + F(B(head, tail)) == C(head, F(tail)) + + 1 [2 3] B F 1 [2 3] F C + + + b F == c + B F == F C + + b [B] catamorphism F == c [C] catamorphism + b [B] catamorphism F == b F [C] catamorphism + + ... + +Or maybe, + +:: + + [F] map b [B] cata == c [C] cata ??? + + [F] map b [B] cata == b [F B] cata I think this is generally true, unless F consumes stack items + instead of just transforming TOS. Of course, there's always [F] unary. + b [F] unary [[F] unary B] cata + + [10 *] map 0 swap [+] step == 0 swap [10 * +] step + +For example: + +:: + + F == 10 * + b == 0 + B == + + c == 0 + C == F + + + b F == c + 0 10 * == 0 + + B F == [F] dip C + + 10 * == [10 *] dip F + + + 10 * == [10 *] dip 10 * + + + n m + 10 * == 10(n+m) + + n m [10 *] dip 10 * + + n 10 * m 10 * + + 10n m 10 * + + 10n 10m + + 10n+10m + + 10n+10m = 10(n+m) + +Ergo: + +:: + + 0 [+] catamorphism 10 * == 0 [10 * +] catamorphism + +The ``step`` combinator will usually be better to use than ``catamorphism``. +---------------------------------------------------------------------------- + +:: + + sum == 0 swap [+] step + sum == 0 [+] catamorphism + +anamorphism catamorphism == hylomorphism +======================================== + +Here is (part of) the payoff. + +An anamorphism followed by (composed with) a catamorphism is a +hylomorphism, with the advantage that the hylomorphism does not create +the intermediate list structure. The values are stored in either the +call stack, for those implementations that use one, or in the pending +expression ("continuation") for the Joypy interpreter. They still have +to be somewhere, converting from an anamorphism and catamorphism to a +hylomorphism just prevents using additional storage and doing additional +processing. + +:: + + range == [0 <=] [1 - dup] anamorphism + sum == 0 [+] catamorphism + + range sum == [0 <=] [1 - dup] anamorphism 0 [+] catamorphism + == [0 <=] 0 [1 - dup] [+] hylomorphism + +We can let the ``hylomorphism`` combinator build ``range_sum`` for us or +just substitute ourselves. + +:: + + H == [P] [pop c] [G] [dip F] genrec + range_sum == [0 <=] [pop 0] [1 - dup] [dip +] genrec + +.. code:: ipython2 + + defs = ''' + anamorphism == [pop []] swap [dip swons] genrec + hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec + catamorphism == [[] =] roll> [uncons swap] swap hylomorphism + range == [0 <=] [1 - dup] anamorphism + sum == 0 [+] catamorphism + range_sum == [0 <=] 0 [1 - dup] [+] hylomorphism + ''' + + DefinitionWrapper.add_definitions(defs, D) + +.. code:: ipython2 + + J('10 range') + + +.. parsed-literal:: + + [9 8 7 6 5 4 3 2 1 0] + + +.. code:: ipython2 + + J('[9 8 7 6 5 4 3 2 1 0] sum') + + +.. parsed-literal:: + + 45 + + +.. code:: ipython2 + + V('10 range sum') + + +.. parsed-literal:: + + . 10 range sum + 10 . range sum + 10 . [0 <=] [1 - dup] anamorphism sum + 10 [0 <=] . [1 - dup] anamorphism sum + 10 [0 <=] [1 - dup] . anamorphism sum + 10 [0 <=] [1 - dup] . [pop []] swap [dip swons] genrec sum + 10 [0 <=] [1 - dup] [pop []] . swap [dip swons] genrec sum + 10 [0 <=] [pop []] [1 - dup] . [dip swons] genrec sum + 10 [0 <=] [pop []] [1 - dup] [dip swons] . genrec sum + 10 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [10] [0 <=] . infra first choice i sum + 10 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum + 10 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] swaack first choice i sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 10] . swaack first choice i sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i sum + 10 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i sum + 10 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 10 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 9 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 9 9 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons sum + 9 9 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons sum + 9 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 9 swons sum + 9 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 9 swons sum + 9 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 9 swons sum + 9 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 9 swons sum + 9 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 9 swons sum + 9 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [9] [0 <=] . infra first choice i 9 swons sum + 9 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum + 9 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] swaack first choice i 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 9] . swaack first choice i 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 9 swons sum + 9 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 9 swons sum + 9 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 9 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 8 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 8 8 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 9 swons sum + 8 8 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 9 swons sum + 8 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 8 swons 9 swons sum + 8 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [8] [0 <=] . infra first choice i 8 swons 9 swons sum + 8 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum + 8 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] swaack first choice i 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 8] . swaack first choice i 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 8 swons 9 swons sum + 8 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 8 swons 9 swons sum + 8 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 8 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 7 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 7 7 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 8 swons 9 swons sum + 7 7 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 8 swons 9 swons sum + 7 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 7 swons 8 swons 9 swons sum + 7 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [7] [0 <=] . infra first choice i 7 swons 8 swons 9 swons sum + 7 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum + 7 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] swaack first choice i 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 7] . swaack first choice i 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 7 swons 8 swons 9 swons sum + 7 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 7 swons 8 swons 9 swons sum + 7 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 7 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 6 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 6 6 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 7 swons 8 swons 9 swons sum + 6 6 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 7 swons 8 swons 9 swons sum + 6 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 6 swons 7 swons 8 swons 9 swons sum + 6 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [6] [0 <=] . infra first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 6] . swaack first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 6 swons 7 swons 8 swons 9 swons sum + 6 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 6 swons 7 swons 8 swons 9 swons sum + 6 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 6 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 5 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 5 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 6 swons 7 swons 8 swons 9 swons sum + 5 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [5] [0 <=] . infra first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 5] . swaack first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 5 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 4 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 4 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [4] [0 <=] . infra first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 4] . swaack first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 3 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 3 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [3] [0 <=] . infra first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 3] . swaack first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 2 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 2 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [2] [0 <=] . infra first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 1 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 1 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [1] [0 <=] . infra first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + False [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . i 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 . 1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 1 . - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 0 . [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 0 [[0 <=] [pop []] [1 - dup] [dip swons] genrec] . dip swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . [0 <=] [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] . [pop []] [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] . [1 - dup] [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] [1 - dup] . [dip swons] genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] [1 - dup] [dip swons] . genrec 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [0 <=] [pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] . ifte 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [0] [0 <=] . infra first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . 0 <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 0 . <= [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + True . [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + True [[pop []] [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] 0] . swaack first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [1 - dup [[0 <=] [pop []] [1 - dup] [dip swons] genrec] dip swons] [pop []] True . choice i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [pop []] . i 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 . pop [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + . [] 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [] . 0 swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [] 0 . swons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [] 0 . swap cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 0 [] . cons 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [0] . 1 swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [0] 1 . swons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [0] 1 . swap cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 1 [0] . cons 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [1 0] . 2 swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [1 0] 2 . swons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [1 0] 2 . swap cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 2 [1 0] . cons 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [2 1 0] . 3 swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [2 1 0] 3 . swons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [2 1 0] 3 . swap cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 3 [2 1 0] . cons 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [3 2 1 0] . 4 swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [3 2 1 0] 4 . swons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [3 2 1 0] 4 . swap cons 5 swons 6 swons 7 swons 8 swons 9 swons sum + 4 [3 2 1 0] . cons 5 swons 6 swons 7 swons 8 swons 9 swons sum + [4 3 2 1 0] . 5 swons 6 swons 7 swons 8 swons 9 swons sum + [4 3 2 1 0] 5 . swons 6 swons 7 swons 8 swons 9 swons sum + [4 3 2 1 0] 5 . swap cons 6 swons 7 swons 8 swons 9 swons sum + 5 [4 3 2 1 0] . cons 6 swons 7 swons 8 swons 9 swons sum + [5 4 3 2 1 0] . 6 swons 7 swons 8 swons 9 swons sum + [5 4 3 2 1 0] 6 . swons 7 swons 8 swons 9 swons sum + [5 4 3 2 1 0] 6 . swap cons 7 swons 8 swons 9 swons sum + 6 [5 4 3 2 1 0] . cons 7 swons 8 swons 9 swons sum + [6 5 4 3 2 1 0] . 7 swons 8 swons 9 swons sum + [6 5 4 3 2 1 0] 7 . swons 8 swons 9 swons sum + [6 5 4 3 2 1 0] 7 . swap cons 8 swons 9 swons sum + 7 [6 5 4 3 2 1 0] . cons 8 swons 9 swons sum + [7 6 5 4 3 2 1 0] . 8 swons 9 swons sum + [7 6 5 4 3 2 1 0] 8 . swons 9 swons sum + [7 6 5 4 3 2 1 0] 8 . swap cons 9 swons sum + 8 [7 6 5 4 3 2 1 0] . cons 9 swons sum + [8 7 6 5 4 3 2 1 0] . 9 swons sum + [8 7 6 5 4 3 2 1 0] 9 . swons sum + [8 7 6 5 4 3 2 1 0] 9 . swap cons sum + 9 [8 7 6 5 4 3 2 1 0] . cons sum + [9 8 7 6 5 4 3 2 1 0] . sum + [9 8 7 6 5 4 3 2 1 0] . 0 [+] catamorphism + [9 8 7 6 5 4 3 2 1 0] 0 . [+] catamorphism + [9 8 7 6 5 4 3 2 1 0] 0 [+] . catamorphism + [9 8 7 6 5 4 3 2 1 0] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . hylomorphism + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [9 8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[9 8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i + [9 8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i + [9 8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] swaack first choice i + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [9 8 7 6 5 4 3 2 1 0]] . swaack first choice i + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i + [9 8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i + [9 8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + 9 [8 7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + [8 7 6 5 4 3 2 1 0] 9 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + [8 7 6 5 4 3 2 1 0] 9 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + + [8 7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 9 + + [8 7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[8 7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 9 + + [8 7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 + + [8 7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] swaack first choice i 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [8 7 6 5 4 3 2 1 0]] . swaack first choice i 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 9 + + [8 7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 9 + + [8 7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 + + 8 [7 6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 + + [7 6 5 4 3 2 1 0] 8 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 9 + + [7 6 5 4 3 2 1 0] 8 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 9 + + [7 6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 8 + 9 + + [7 6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[7 6 5 4 3 2 1 0]] [[] =] . infra first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] swaack first choice i 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [7 6 5 4 3 2 1 0]] . swaack first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 + + [7 6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 8 + 9 + + [7 6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 + + 7 [6 5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 + + [6 5 4 3 2 1 0] 7 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 8 + 9 + + [6 5 4 3 2 1 0] 7 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 8 + 9 + + [6 5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 7 + 8 + 9 + + [6 5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[6 5 4 3 2 1 0]] [[] =] . infra first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] swaack first choice i 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [6 5 4 3 2 1 0]] . swaack first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 + + [6 5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 7 + 8 + 9 + + [6 5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 + + 6 [5 4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 + + [5 4 3 2 1 0] 6 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 7 + 8 + 9 + + [5 4 3 2 1 0] 6 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 7 + 8 + 9 + + [5 4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[5 4 3 2 1 0]] [[] =] . infra first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] swaack first choice i 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [5 4 3 2 1 0]] . swaack first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 + + [5 4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 [4 3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + [4 3 2 1 0] 5 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + [4 3 2 1 0] 5 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 6 + 7 + 8 + 9 + + [4 3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[4 3 2 1 0]] [[] =] . infra first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [4 3 2 1 0]] . swaack first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 + + [4 3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 [3 2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] 4 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] 4 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3 2 1 0]] [[] =] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3 2 1 0]] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 + + [3 2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [2 1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 1 0]] [[] =] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 1 0]] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [2 1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[1 0]] [[] =] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [1 0]] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [1 0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] 1 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] 1 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] . [[] =] [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] . [pop 0] [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] . [uncons swap] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] [uncons swap] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] [uncons swap] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[0]] [[] =] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [0]] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [0] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] 0 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] 0 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[]] [[] =] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] []] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + [] . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 . + 4 + 5 + 6 + 7 + 8 + 9 + + 6 . 4 + 5 + 6 + 7 + 8 + 9 + + 6 4 . + 5 + 6 + 7 + 8 + 9 + + 10 . 5 + 6 + 7 + 8 + 9 + + 10 5 . + 6 + 7 + 8 + 9 + + 15 . 6 + 7 + 8 + 9 + + 15 6 . + 7 + 8 + 9 + + 21 . 7 + 8 + 9 + + 21 7 . + 8 + 9 + + 28 . 8 + 9 + + 28 8 . + 9 + + 36 . 9 + + 36 9 . + + 45 . + + +.. code:: ipython2 + + V('10 range_sum') + + +.. parsed-literal:: + + . 10 range_sum + 10 . range_sum + 10 . [0 <=] 0 [1 - dup] [+] hylomorphism + 10 [0 <=] . 0 [1 - dup] [+] hylomorphism + 10 [0 <=] 0 . [1 - dup] [+] hylomorphism + 10 [0 <=] 0 [1 - dup] . [+] hylomorphism + 10 [0 <=] 0 [1 - dup] [+] . hylomorphism + 10 [0 <=] 0 [1 - dup] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + 10 [0 <=] 0 [1 - dup] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + 10 [0 <=] 0 . unit [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] 0 . [] cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] 0 [] . cons [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [0] . [pop] swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [0] [pop] . swoncat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [0] [pop] . swap concat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [pop] [0] . concat [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [pop 0] . [1 - dup] [+] [dip] swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] . [+] [dip] swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] [+] . [dip] swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swoncat genrec + 10 [0 <=] [pop 0] [1 - dup] [+] [dip] . swap concat genrec + 10 [0 <=] [pop 0] [1 - dup] [dip] [+] . concat genrec + 10 [0 <=] [pop 0] [1 - dup] [dip +] . genrec + 10 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [10] [0 <=] . infra first choice i + 10 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i + 10 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] swaack first choice i + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 10] . swaack first choice i + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i + 10 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i + 10 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 10 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 9 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 9 9 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + + 9 9 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + + 9 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 9 + + 9 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 9 + + 9 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 9 + + 9 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 9 + + 9 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 9 + + 9 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [9] [0 <=] . infra first choice i 9 + + 9 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 + + 9 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] swaack first choice i 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 9] . swaack first choice i 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 9 + + 9 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 9 + + 9 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 9 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 8 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 8 8 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 9 + + 8 8 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 9 + + 8 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 8 + 9 + + 8 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 8 + 9 + + 8 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 8 + 9 + + 8 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 8 + 9 + + 8 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 8 + 9 + + 8 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [8] [0 <=] . infra first choice i 8 + 9 + + 8 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 + + 8 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] swaack first choice i 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 8] . swaack first choice i 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 8 + 9 + + 8 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 8 + 9 + + 8 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 8 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 7 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 7 7 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 8 + 9 + + 7 7 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 8 + 9 + + 7 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 7 + 8 + 9 + + 7 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [7] [0 <=] . infra first choice i 7 + 8 + 9 + + 7 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 + + 7 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] swaack first choice i 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 7] . swaack first choice i 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 7 + 8 + 9 + + 7 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 7 + 8 + 9 + + 7 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 7 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 6 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 6 6 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 7 + 8 + 9 + + 6 6 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 7 + 8 + 9 + + 6 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 6 + 7 + 8 + 9 + + 6 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [6] [0 <=] . infra first choice i 6 + 7 + 8 + 9 + + 6 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 + + 6 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] swaack first choice i 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 6] . swaack first choice i 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 6 + 7 + 8 + 9 + + 6 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 6 + 7 + 8 + 9 + + 6 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 6 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 5 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 6 + 7 + 8 + 9 + + 5 5 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 6 + 7 + 8 + 9 + + 5 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 5 + 6 + 7 + 8 + 9 + + 5 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [5] [0 <=] . infra first choice i 5 + 6 + 7 + 8 + 9 + + 5 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 + + 5 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] swaack first choice i 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 5] . swaack first choice i 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 5 + 6 + 7 + 8 + 9 + + 5 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 5 + 6 + 7 + 8 + 9 + + 5 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 5 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 4 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 5 + 6 + 7 + 8 + 9 + + 4 4 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 5 + 6 + 7 + 8 + 9 + + 4 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 4 + 5 + 6 + 7 + 8 + 9 + + 4 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [4] [0 <=] . infra first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 4] . swaack first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 4 + 5 + 6 + 7 + 8 + 9 + + 4 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 4 + 5 + 6 + 7 + 8 + 9 + + 4 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 4 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [3] [0 <=] . infra first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 3] . swaack first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 2 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 2 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [2] [0 <=] . infra first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 2] . swaack first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 2 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 1 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 1 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [1] [0 <=] . infra first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + False [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 1] . swaack first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [False] . first choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] False . choice i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . i 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 1 . - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 [[0 <=] [pop 0] [1 - dup] [dip +] genrec] . dip + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . [0 <=] [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] . [pop 0] [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] . [1 - dup] [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] [1 - dup] . [dip +] genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] [1 - dup] [dip +] . genrec 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [0 <=] [pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] . ifte 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [0] [0 <=] . infra first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 0 <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . <= [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True . [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + True [[pop 0] [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] 0] . swaack first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] [True] . first choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [1 - dup [[0 <=] [pop 0] [1 - dup] [dip +] genrec] dip +] [pop 0] True . choice i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 [pop 0] . i 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . pop 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + . 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 0 . + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 . 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 0 1 . + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 . 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 1 2 . + 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 . 3 + 4 + 5 + 6 + 7 + 8 + 9 + + 3 3 . + 4 + 5 + 6 + 7 + 8 + 9 + + 6 . 4 + 5 + 6 + 7 + 8 + 9 + + 6 4 . + 5 + 6 + 7 + 8 + 9 + + 10 . 5 + 6 + 7 + 8 + 9 + + 10 5 . + 6 + 7 + 8 + 9 + + 15 . 6 + 7 + 8 + 9 + + 15 6 . + 7 + 8 + 9 + + 21 . 7 + 8 + 9 + + 21 7 . + 8 + 9 + + 28 . 8 + 9 + + 28 8 . + 9 + + 36 . 9 + + 36 9 . + + 45 . + + +Factorial Function and Paramorphisms +==================================== + +A paramorphism ``P :: B -> A`` is a recursion combinator that uses +``dup`` on intermediate values. + +:: + + n swap [P] [pop] [[F] dupdip G] primrec + +With - ``n :: A`` is the "identity" for ``F`` (like 1 for +multiplication, 0 for addition) - ``F :: (A, B) -> A`` - ``G :: B -> B`` +generates the next ``B`` value. - and lastly ``P :: B -> Bool`` detects +the end of the series. + +For Factorial function (types ``A`` and ``B`` are both integer): + +:: + + n == 1 + F == * + G == -- + P == 1 <= + +.. code:: ipython2 + + define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec') + +Try it with input 3 (omitting evaluation of predicate): + +:: + + 3 1 swap [1 <=] [pop] [[*] dupdip --] primrec + 1 3 [1 <=] [pop] [[*] dupdip --] primrec + + 1 3 [*] dupdip -- + 1 3 * 3 -- + 3 3 -- + 3 2 + + 3 2 [*] dupdip -- + 3 2 * 2 -- + 6 2 -- + 6 1 + + 6 1 [1 <=] [pop] [[*] dupdip --] primrec + + 6 1 pop + 6 + +.. code:: ipython2 + + J('3 factorial') + + +.. parsed-literal:: + + 6 + + +Derive ``paramorphism`` from the form above. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + n swap [P] [pop] [[F] dupdip G] primrec + + n swap [P] [pop] [[F] dupdip G] primrec + n [P] [swap] dip [pop] [[F] dupdip G] primrec + n [P] [[F] dupdip G] [[swap] dip [pop]] dip primrec + n [P] [F] [dupdip G] cons [[swap] dip [pop]] dip primrec + n [P] [F] [G] [dupdip] swoncat cons [[swap] dip [pop]] dip primrec + + paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec + +.. code:: ipython2 + + define('paramorphism == [dupdip] swoncat cons [[swap] dip [pop]] dip primrec') + define('factorial == 1 [1 <=] [*] [--] paramorphism') + +.. code:: ipython2 + + J('3 factorial') + + +.. parsed-literal:: + + 6 + + +``tails`` +========= + +An example of a paramorphism for lists given in the `"Bananas..." +paper `__ +is ``tails`` which returns the list of "tails" of a list. + +:: + + [1 2 3] tails == [[] [3] [2 3]] + +Using ``paramorphism`` we would write: + +:: + + n == [] + F == rest swons + G == rest + P == not + + tails == [] [not] [rest swons] [rest] paramorphism + +.. code:: ipython2 + + define('tails == [] [not] [rest swons] [rest] paramorphism') + +.. code:: ipython2 + + J('[1 2 3] tails') + + +.. parsed-literal:: + + [[] [3] [2 3]] + + +.. code:: ipython2 + + J('25 range tails [popop] infra [sum] map') + + +.. parsed-literal:: + + [1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276] + + +.. code:: ipython2 + + J('25 range [range_sum] map') + + +.. parsed-literal:: + + [276 253 231 210 190 171 153 136 120 105 91 78 66 55 45 36 28 21 15 10 6 3 1 0 0] + + +Factoring ``rest`` +~~~~~~~~~~~~~~~~~~ + +Right before the recursion begins we have: + +:: + + [] [1 2 3] [not] [pop] [[rest swons] dupdip rest] primrec + +But we might prefer to factor ``rest`` in the quote: + +:: + + [] [1 2 3] [not] [pop] [rest [swons] dupdip] primrec + +There's no way to do that with the ``paramorphism`` combinator as +defined. We would have to write and use a slightly different recursion +combinator that accepted an additional "preprocessor" function ``[H]`` +and built: + +:: + + n swap [P] [pop] [H [F] dupdip G] primrec + +Or just write it out manually. This is yet another place where the +*sufficiently smart compiler* will one day automatically refactor the +code. We could write a ``paramorphism`` combinator that checked ``[F]`` +and ``[G]`` for common prefix and extracted it. + +Patterns of Recursion +===================== + +Our story so far... + +- A combiner ``F :: (B, B) -> B`` +- A predicate ``P :: A -> Bool`` to detect the base case +- A base case value ``c :: B`` + +Hylo-, Ana-, Cata- +~~~~~~~~~~~~~~~~~~ + +:: + + w/ G :: A -> (A, B) + + H == [P ] [pop c ] [G ] [dip F ] genrec + A == [P ] [pop []] [G ] [dip swons] genrec + C == [[] =] [pop c ] [uncons swap] [dip F ] genrec + +Para-, ?-, ?- +~~~~~~~~~~~~~ + +:: + + w/ G :: B -> B + + P == c swap [P ] [pop] [[F ] dupdip G ] primrec + ? == [] swap [P ] [pop] [[swons] dupdip G ] primrec + ? == c swap [[] =] [pop] [[F ] dupdip uncons swap] primrec + +Four Generalizations +==================== + +There are at least four kinds of recursive combinator, depending on two +choices. The first choice is whether the combiner function should be +evaluated during the recursion or pushed into the pending expression to +be "collapsed" at the end. The second choice is whether the combiner +needs to operate on the current value of the datastructure or the +generator's output. + +:: + + H == [P] [pop c] [G ] [dip F] genrec + H == c swap [P] [pop] [G [F] dip ] [i] genrec + H == [P] [pop c] [ [G] dupdip ] [dip F] genrec + H == c swap [P] [pop] [ [F] dupdip G] [i] genrec + +Consider: + +:: + + ... a G [H] dip F w/ a G == a' b + ... c a G [F] dip H a G == b a' + ... a [G] dupdip [H] dip F a G == a' + ... c a [F] dupdip G H a G == a' + +1 +~ + +:: + + H == [P] [pop c] [G] [dip F] genrec + +Iterate n times. + +:: + + ... a [P] [pop c] [G] [dip F] genrec + ... a G [H] dip F + ... a' b [H] dip F + ... a' H b F + ... a' G [H] dip F b F + ... a'' b [H] dip F b F + ... a'' H b F b F + ... a'' G [H] dip F b F b F + ... a''' b [H] dip F b F b F + ... a''' H b F b F b F + ... a''' pop c b F b F b F + ... c b F b F b F + +This form builds up a continuation that contains the intermediate +results along with the pending combiner functions. When the base case is +reached the last term is replaced by the identity value c and the +continuation "collapses" into the final result. + +2 +~ + +When you can start with the identity value c on the stack and the +combiner can operate as you go, using the intermediate results +immediately rather than queuing them up, use this form. An important +difference is that the generator function must return its results in the +reverse order. + +:: + + H == c swap [P] [pop] [G [F] dip] primrec + + ... c a G [F] dip H + ... c b a' [F] dip H + ... c b F a' H + ... c b F a' G [F] dip H + ... c b F b a'' [F] dip H + ... c b F b F a'' H + ... c b F b F a'' G [F] dip H + ... c b F b F b a''' [F] dip H + ... c b F b F b F a''' H + ... c b F b F b F a''' pop + ... c b F b F b F + +The end line here is the same as for above, but only because we didn't +evaluate ``F`` when it normally would have been. + +3 +~ + +If the combiner and the generator both need to work on the current value +then ``dup`` must be used at some point, and the generator must produce +one item instead of two (the b is instead the duplicate of a.) + +:: + + H == [P] [pop c] [[G] dupdip] [dip F] genrec + + ... a [G] dupdip [H] dip F + ... a G a [H] dip F + ... a' a [H] dip F + ... a' H a F + ... a' [G] dupdip [H] dip F a F + ... a' G a' [H] dip F a F + ... a'' a' [H] dip F a F + ... a'' H a' F a F + ... a'' [G] dupdip [H] dip F a' F a F + ... a'' G a'' [H] dip F a' F a F + ... a''' a'' [H] dip F a' F a F + ... a''' H a'' F a' F a F + ... a''' pop c a'' F a' F a F + ... c a'' F a' F a F + +4 +~ + +And, last but not least, if you can combine as you go, starting with c, +and the combiner needs to work on the current item, this is the form: + +:: + + W == c swap [P] [pop] [[F] dupdip G] primrec + + ... a c swap [P] [pop] [[F] dupdip G] primrec + ... c a [P] [pop] [[F] dupdip G] primrec + ... c a [F] dupdip G W + ... c a F a G W + ... c a F a' W + ... c a F a' [F] dupdip G W + ... c a F a' F a' G W + ... c a F a' F a'' W + ... c a F a' F a'' [F] dupdip G W + ... c a F a' F a'' F a'' G W + ... c a F a' F a'' F a''' W + ... c a F a' F a'' F a''' pop + ... c a F a' F a'' F + +Each of the four variations above can be specialized to ana- and +catamorphic forms. + +.. code:: ipython2 + + def WTFmorphism(c, F, P, G): + '''Return a hylomorphism function H.''' + + def H(a, d=c): + if P(a): + result = d + else: + a, b = G(a) + result = H(a, F(d, b)) + return result + + return H + +.. code:: ipython2 + + F = lambda a, b: a + b + P = lambda n: n <= 1 + G = lambda n: (n - 1, n - 1) + + wtf = WTFmorphism(0, F, P, G) + + print wtf(5) + + +.. parsed-literal:: + + 10 + + +:: + + H == [P ] [pop c ] [G ] [dip F ] genrec + +.. code:: ipython2 + + DefinitionWrapper.add_definitions(''' + P == 1 <= + Ga == -- dup + Gb == -- + c == 0 + F == + + ''', D) + +.. code:: ipython2 + + V('[1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec') + + +.. parsed-literal:: + + . [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] genrec + [1 2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec + [1 2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec + [1 2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec + [1 2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec + [1 2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec + [1 2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[1 2 3]] [[] =] . infra first choice i + [1 2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i + [1 2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i + False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] swaack first choice i + False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [1 2 3]] . swaack first choice i + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i + [1 2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i + [1 2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons + 1 [2 3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons + [2 3] 1 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons + [2 3] 1 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons + [2 3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 1 swons + [2 3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 1 swons + [2 3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 1 swons + [2 3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 1 swons + [2 3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 1 swons + [2 3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[2 3]] [[] =] . infra first choice i 1 swons + [2 3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons + [2 3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons + False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] swaack first choice i 1 swons + False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [2 3]] . swaack first choice i 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 1 swons + [2 3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 1 swons + [2 3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons + 2 [3] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons + [3] 2 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 1 swons + [3] 2 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 1 swons + [3] . [[] =] [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons + [3] [[] =] . [pop []] [uncons swap] [dip swons] genrec 2 swons 1 swons + [3] [[] =] [pop []] . [uncons swap] [dip swons] genrec 2 swons 1 swons + [3] [[] =] [pop []] [uncons swap] . [dip swons] genrec 2 swons 1 swons + [3] [[] =] [pop []] [uncons swap] [dip swons] . genrec 2 swons 1 swons + [3] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[3]] [[] =] . infra first choice i 2 swons 1 swons + [3] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons + [3] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons + False . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] swaack first choice i 2 swons 1 swons + False [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [3]] . swaack first choice i 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons 1 swons + [3] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . i 2 swons 1 swons + [3] . uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons + 3 [] . swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons + [] 3 . [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons 2 swons 1 swons + [] 3 [[[] =] [pop []] [uncons swap] [dip swons] genrec] . dip swons 2 swons 1 swons + [] . [[] =] [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] . [pop []] [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] . [uncons swap] [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] [uncons swap] . [dip swons] genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] [uncons swap] [dip swons] . genrec 3 swons 2 swons 1 swons + [] [[] =] [pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] . ifte 3 swons 2 swons 1 swons + [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [[]] [[] =] . infra first choice i 3 swons 2 swons 1 swons + [] . [] = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons + [] [] . = [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons + True . [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] swaack first choice i 3 swons 2 swons 1 swons + True [[pop []] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] []] . swaack first choice i 3 swons 2 swons 1 swons + [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 3 swons 2 swons 1 swons + [] [uncons swap [[[] =] [pop []] [uncons swap] [dip swons] genrec] dip swons] [pop []] True . choice i 3 swons 2 swons 1 swons + [] [pop []] . i 3 swons 2 swons 1 swons + [] . pop [] 3 swons 2 swons 1 swons + . [] 3 swons 2 swons 1 swons + [] . 3 swons 2 swons 1 swons + [] 3 . swons 2 swons 1 swons + [] 3 . swap cons 2 swons 1 swons + 3 [] . cons 2 swons 1 swons + [3] . 2 swons 1 swons + [3] 2 . swons 1 swons + [3] 2 . swap cons 1 swons + 2 [3] . cons 1 swons + [2 3] . 1 swons + [2 3] 1 . swons + [2 3] 1 . swap cons + 1 [2 3] . cons + [1 2 3] . + + +.. code:: ipython2 + + V('3 [P] [pop c] [Ga] [dip F] genrec') + + +.. parsed-literal:: + + . 3 [P] [pop c] [Ga] [dip F] genrec + 3 . [P] [pop c] [Ga] [dip F] genrec + 3 [P] . [pop c] [Ga] [dip F] genrec + 3 [P] [pop c] . [Ga] [dip F] genrec + 3 [P] [pop c] [Ga] . [dip F] genrec + 3 [P] [pop c] [Ga] [dip F] . genrec + 3 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [3] [P] . infra first choice i + 3 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + 3 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + 3 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] swaack first choice i + False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 3] . swaack first choice i + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i + 3 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i + 3 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F + 3 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F + 2 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F + 2 2 . [[P] [pop c] [Ga] [dip F] genrec] dip F + 2 2 [[P] [pop c] [Ga] [dip F] genrec] . dip F + 2 . [P] [pop c] [Ga] [dip F] genrec 2 F + 2 [P] . [pop c] [Ga] [dip F] genrec 2 F + 2 [P] [pop c] . [Ga] [dip F] genrec 2 F + 2 [P] [pop c] [Ga] . [dip F] genrec 2 F + 2 [P] [pop c] [Ga] [dip F] . genrec 2 F + 2 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [2] [P] . infra first choice i 2 F + 2 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + 2 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + 2 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + False . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] swaack first choice i 2 F + False [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 2] . swaack first choice i 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] False . choice i 2 F + 2 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . i 2 F + 2 . Ga [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 2 . -- dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 1 . dup [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 1 1 . [[P] [pop c] [Ga] [dip F] genrec] dip F 2 F + 1 1 [[P] [pop c] [Ga] [dip F] genrec] . dip F 2 F + 1 . [P] [pop c] [Ga] [dip F] genrec 1 F 2 F + 1 [P] . [pop c] [Ga] [dip F] genrec 1 F 2 F + 1 [P] [pop c] . [Ga] [dip F] genrec 1 F 2 F + 1 [P] [pop c] [Ga] . [dip F] genrec 1 F 2 F + 1 [P] [pop c] [Ga] [dip F] . genrec 1 F 2 F + 1 [P] [pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] . ifte 1 F 2 F + 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [1] [P] . infra first choice i 1 F 2 F + 1 . P [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + 1 . 1 <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + 1 1 . <= [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + True . [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] swaack first choice i 1 F 2 F + True [[pop c] [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] 1] . swaack first choice i 1 F 2 F + 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F + 1 [Ga [[P] [pop c] [Ga] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F + 1 [pop c] . i 1 F 2 F + 1 . pop c 1 F 2 F + . c 1 F 2 F + . 0 1 F 2 F + 0 . 1 F 2 F + 0 1 . F 2 F + 0 1 . + 2 F + 1 . 2 F + 1 2 . F + 1 2 . + + 3 . + + +.. code:: ipython2 + + V('3 [P] [pop []] [Ga] [dip swons] genrec') + + +.. parsed-literal:: + + . 3 [P] [pop []] [Ga] [dip swons] genrec + 3 . [P] [pop []] [Ga] [dip swons] genrec + 3 [P] . [pop []] [Ga] [dip swons] genrec + 3 [P] [pop []] . [Ga] [dip swons] genrec + 3 [P] [pop []] [Ga] . [dip swons] genrec + 3 [P] [pop []] [Ga] [dip swons] . genrec + 3 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [3] [P] . infra first choice i + 3 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + 3 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + 3 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] swaack first choice i + False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 3] . swaack first choice i + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i + 3 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i + 3 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 3 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 2 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 2 2 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons + 2 2 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons + 2 . [P] [pop []] [Ga] [dip swons] genrec 2 swons + 2 [P] . [pop []] [Ga] [dip swons] genrec 2 swons + 2 [P] [pop []] . [Ga] [dip swons] genrec 2 swons + 2 [P] [pop []] [Ga] . [dip swons] genrec 2 swons + 2 [P] [pop []] [Ga] [dip swons] . genrec 2 swons + 2 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [2] [P] . infra first choice i 2 swons + 2 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + 2 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + 2 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + False . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] swaack first choice i 2 swons + False [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 2] . swaack first choice i 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [False] . first choice i 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] False . choice i 2 swons + 2 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . i 2 swons + 2 . Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 2 . -- dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 1 . dup [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 1 1 . [[P] [pop []] [Ga] [dip swons] genrec] dip swons 2 swons + 1 1 [[P] [pop []] [Ga] [dip swons] genrec] . dip swons 2 swons + 1 . [P] [pop []] [Ga] [dip swons] genrec 1 swons 2 swons + 1 [P] . [pop []] [Ga] [dip swons] genrec 1 swons 2 swons + 1 [P] [pop []] . [Ga] [dip swons] genrec 1 swons 2 swons + 1 [P] [pop []] [Ga] . [dip swons] genrec 1 swons 2 swons + 1 [P] [pop []] [Ga] [dip swons] . genrec 1 swons 2 swons + 1 [P] [pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] . ifte 1 swons 2 swons + 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [1] [P] . infra first choice i 1 swons 2 swons + 1 . P [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + 1 . 1 <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + 1 1 . <= [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + True . [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] swaack first choice i 1 swons 2 swons + True [[pop []] [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] 1] . swaack first choice i 1 swons 2 swons + 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] [True] . first choice i 1 swons 2 swons + 1 [Ga [[P] [pop []] [Ga] [dip swons] genrec] dip swons] [pop []] True . choice i 1 swons 2 swons + 1 [pop []] . i 1 swons 2 swons + 1 . pop [] 1 swons 2 swons + . [] 1 swons 2 swons + [] . 1 swons 2 swons + [] 1 . swons 2 swons + [] 1 . swap cons 2 swons + 1 [] . cons 2 swons + [1] . 2 swons + [1] 2 . swons + [1] 2 . swap cons + 2 [1] . cons + [2 1] . + + +.. code:: ipython2 + + V('[2 1] [[] =] [pop c ] [uncons swap] [dip F] genrec') + + +.. parsed-literal:: + + . [2 1] [[] =] [pop c] [uncons swap] [dip F] genrec + [2 1] . [[] =] [pop c] [uncons swap] [dip F] genrec + [2 1] [[] =] . [pop c] [uncons swap] [dip F] genrec + [2 1] [[] =] [pop c] . [uncons swap] [dip F] genrec + [2 1] [[] =] [pop c] [uncons swap] . [dip F] genrec + [2 1] [[] =] [pop c] [uncons swap] [dip F] . genrec + [2 1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[2 1]] [[] =] . infra first choice i + [2 1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i + [2 1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i + False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] swaack first choice i + False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [2 1]] . swaack first choice i + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i + [2 1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i + [2 1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F + 2 [1] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F + [1] 2 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F + [1] 2 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F + [1] . [[] =] [pop c] [uncons swap] [dip F] genrec 2 F + [1] [[] =] . [pop c] [uncons swap] [dip F] genrec 2 F + [1] [[] =] [pop c] . [uncons swap] [dip F] genrec 2 F + [1] [[] =] [pop c] [uncons swap] . [dip F] genrec 2 F + [1] [[] =] [pop c] [uncons swap] [dip F] . genrec 2 F + [1] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[1]] [[] =] . infra first choice i 2 F + [1] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F + [1] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F + False . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] swaack first choice i 2 F + False [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [1]] . swaack first choice i 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [False] . first choice i 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] False . choice i 2 F + [1] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . i 2 F + [1] . uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F + 1 [] . swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F + [] 1 . [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F 2 F + [] 1 [[[] =] [pop c] [uncons swap] [dip F] genrec] . dip F 2 F + [] . [[] =] [pop c] [uncons swap] [dip F] genrec 1 F 2 F + [] [[] =] . [pop c] [uncons swap] [dip F] genrec 1 F 2 F + [] [[] =] [pop c] . [uncons swap] [dip F] genrec 1 F 2 F + [] [[] =] [pop c] [uncons swap] . [dip F] genrec 1 F 2 F + [] [[] =] [pop c] [uncons swap] [dip F] . genrec 1 F 2 F + [] [[] =] [pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] . ifte 1 F 2 F + [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [[]] [[] =] . infra first choice i 1 F 2 F + [] . [] = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F + [] [] . = [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F + True . [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] swaack first choice i 1 F 2 F + True [[pop c] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] []] . swaack first choice i 1 F 2 F + [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] [True] . first choice i 1 F 2 F + [] [uncons swap [[[] =] [pop c] [uncons swap] [dip F] genrec] dip F] [pop c] True . choice i 1 F 2 F + [] [pop c] . i 1 F 2 F + [] . pop c 1 F 2 F + . c 1 F 2 F + . 0 1 F 2 F + 0 . 1 F 2 F + 0 1 . F 2 F + 0 1 . + 2 F + 1 . 2 F + 1 2 . F + 1 2 . + + 3 . + + +Appendix - Fun with Symbols +--------------------------- + +:: + + |[ (c, F), (G, P) ]| == (|c, F|) • [(G, P)] + +`"Bananas, Lenses, & Barbed +Wire" `__ + +:: + + (|...|) [(...)] [<...>] + +I think they are having slightly too much fun with the symbols. + +"Too much is always better than not enough." + +Tree with node and list of trees. +================================= + +:: + + tree = [] | [node [tree*]] + +``treestep`` +~~~~~~~~~~~~ + +:: + + tree z [C] [N] treestep + + + [] z [C] [N] treestep + --------------------------- + z + + + [node [tree*]] z [C] [N] treestep + --------------------------------------- w/ K == z [C] [N] treestep + node N [tree*] [K] map C + +Derive the recursive form. +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + K == [not] [pop z] [J] ifte + + + [node [tree*]] J + ------------------------------ + node N [tree*] [K] map C + + + J == .. [N] .. [K] .. [C] .. + + [node [tree*]] uncons [N] dip + node [[tree*]] [N] dip + node N [[tree*]] + + node N [[tree*]] i [K] map + node N [tree*] [K] map + node N [K.tree*] + + J == uncons [N] dip i [K] map [C] i + + K == [not] [pop z] [uncons [N] dip i [K] map [C] i] ifte + K == [not] [pop z] [uncons [N] dip i] [map [C] i] genrec + +Extract the givens to parameterize the program. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + [not] [pop z] [uncons [N] dip unquote] [map [C] i] genrec + [not] [z] [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec + [not] z unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec + z [not] swap unit [pop] swoncat [uncons [N] dip unquote] [map [C] i] genrec + \............TS0............/ + z TS0 [uncons [N] dip unquote] [map [C] i] genrec + z [uncons [N] dip unquote] [TS0] dip [map [C] i] genrec + z [[N] dip unquote] [uncons] swoncat [TS0] dip [map [C] i] genrec + z [N] [dip unquote] cons [uncons] swoncat [TS0] dip [map [C] i] genrec + \...........TS1.................../ + z [N] TS1 [TS0] dip [map [C] i] genrec + z [N] [map [C] i] [TS1 [TS0] dip] dip genrec + z [N] [map C ] [TS1 [TS0] dip] dip genrec + z [N] [C] [map] swoncat [TS1 [TS0] dip] dip genrec + z [C] [N] swap [map] swoncat [TS1 [TS0] dip] dip genrec + +:: + + TS0 == [not] swap unit [pop] swoncat + TS1 == [dip i] cons [uncons] swoncat + treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec + +:: + + [] 0 [C] [N] treestep + --------------------------- + 0 + + + [n [tree*]] 0 [sum +] [] treestep + -------------------------------------------------- + n [tree*] [0 [sum +] [] treestep] map sum + + +.. code:: ipython2 + + DefinitionWrapper.add_definitions(''' + + TS0 == [not] swap unit [pop] swoncat + TS1 == [dip i] cons [uncons] swoncat + treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec + + ''', D) + +.. code:: ipython2 + + V('[] 0 [sum +] [] treestep') + + +.. parsed-literal:: + + . [] 0 [sum +] [] treestep + [] . 0 [sum +] [] treestep + [] 0 . [sum +] [] treestep + [] 0 [sum +] . [] treestep + [] 0 [sum +] [] . treestep + [] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec + [] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec + [] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec + [] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec + [] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec + [] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec + [] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec + [] 0 [] . TS1 [TS0] dip [map sum +] genrec + [] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec + [] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec + [] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec + [] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec + [] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec + [] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec + [] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec + [] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec + [] 0 . TS0 [uncons [] dip i] [map sum +] genrec + [] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec + [] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec + [] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec + [] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec + [] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec + [] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec + [] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec + [] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte + [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[]] [not] . infra first choice i + [] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i + True . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] swaack first choice i + True [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] []] . swaack first choice i + [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [True] . first choice i + [] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] True . choice i + [] [pop 0] . i + [] . pop 0 + . 0 + 0 . + + +.. code:: ipython2 + + V('[23 []] 0 [sum +] [] treestep') + + +.. parsed-literal:: + + . [23 []] 0 [sum +] [] treestep + [23 []] . 0 [sum +] [] treestep + [23 []] 0 . [sum +] [] treestep + [23 []] 0 [sum +] . [] treestep + [23 []] 0 [sum +] [] . treestep + [23 []] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec + [23 []] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec + [23 []] 0 [] . TS1 [TS0] dip [map sum +] genrec + [23 []] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec + [23 []] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec + [23 []] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec + [23 []] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec + [23 []] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec + [23 []] 0 . TS0 [uncons [] dip i] [map sum +] genrec + [23 []] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec + [23 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec + [23 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec + [23 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec + [23 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 []]] [not] . infra first choice i + [23 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i + False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] swaack first choice i + False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 []]] . swaack first choice i + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i + [23 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i + [23 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + + 23 [] . sum + + 23 [] . 0 [+] catamorphism + + 23 [] 0 . [+] catamorphism + + 23 [] 0 [+] . catamorphism + + 23 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + + 23 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + + 23 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + + 23 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + + 23 [] [[] =] 0 [uncons swap] [+] . hylomorphism + + 23 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + + 23 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + + 23 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + + 23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + + 23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i + + 23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i + + 23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i + + 23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i + + 23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + + 23 [] [pop 0] . i + + 23 [] . pop 0 + + 23 . 0 + + 23 0 . + + 23 . + + +.. code:: ipython2 + + V('[23 [[2 []] [3 []]]] 0 [sum +] [] treestep') + + +.. parsed-literal:: + + . [23 [[2 []] [3 []]]] 0 [sum +] [] treestep + [23 [[2 []] [3 []]]] . 0 [sum +] [] treestep + [23 [[2 []] [3 []]]] 0 . [sum +] [] treestep + [23 [[2 []] [3 []]]] 0 [sum +] . [] treestep + [23 [[2 []] [3 []]]] 0 [sum +] [] . treestep + [23 [[2 []] [3 []]]] 0 [sum +] [] . swap [map] swoncat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [sum +] . [map] swoncat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swoncat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [sum +] [map] . swap concat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [map] [sum +] . concat [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [map sum +] . [TS1 [TS0] dip] dip genrec + [23 [[2 []] [3 []]]] 0 [] [map sum +] [TS1 [TS0] dip] . dip genrec + [23 [[2 []] [3 []]]] 0 [] . TS1 [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [] . [dip i] cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [] [dip i] . cons [uncons] swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [[] dip i] . [uncons] swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swoncat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [[] dip i] [uncons] . swap concat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [uncons] [[] dip i] . concat [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [uncons [] dip i] . [TS0] dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [uncons [] dip i] [TS0] . dip [map sum +] genrec + [23 [[2 []] [3 []]]] 0 . TS0 [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] 0 . [not] swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] 0 [not] . swap unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] 0 . unit [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] 0 . [] cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] 0 [] . cons [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [0] . [pop] swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [0] [pop] . swoncat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [0] [pop] . swap concat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop] [0] . concat [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec + [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec + [23 [[2 []] [3 []]]] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[23 [[2 []] [3 []]]]] [not] . infra first choice i + [23 [[2 []] [3 []]]] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i + False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] swaack first choice i + False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [23 [[2 []] [3 []]]]] . swaack first choice i + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False] . first choice i + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i + [23 [[2 []] [3 []]]] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i + [23 [[2 []] [3 []]]] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[[2 []] [3 []]]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[[2 []] [3 []]]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [[[2 []] [3 []]]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[[2 []] [3 []]]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 . [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[2 []] [3 []]] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + + 23 [[2 []] [3 []]] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + + 23 [] [[[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first] . infra sum + + . [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + [[3 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + [[3 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[3 []] 23] [not] . infra first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [3 []] 23] . swaack first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 [3 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . sum + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . 0 [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 . [+] catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 [+] . catamorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 3 23] [[] =] . infra first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 3 23] . swaack first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 3 23] . first choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] [pop 0] . i + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . pop 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . 0 + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 0 . + [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 . [] swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 23 3 [] . swaack first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + [3 23] . first [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 3 . [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 3 [[2 []] 23] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] infra first [23] swaack sum + + 3 [[2 []] 23] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . infra first [23] swaack sum + + 23 [2 []] . [not] [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] . [pop 0] [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] . [uncons [] dip i] [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] [uncons [] dip i] . [map sum +] genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] [uncons [] dip i] [map sum +] . genrec [3] swaack first [23] swaack sum + + 23 [2 []] [not] [pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . ifte [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [[2 []] 23] [not] . infra first choice i [3] swaack first [23] swaack sum + + 23 [2 []] . not [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum + + 23 False . [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] swaack first choice i [3] swaack first [23] swaack sum + + 23 False [[pop 0] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [2 []] 23] . swaack first choice i [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] [False 23] . first choice i [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] [pop 0] False . choice i [3] swaack first [23] swaack sum + + 23 [2 []] [uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum +] . i [3] swaack first [23] swaack sum + + 23 [2 []] . uncons [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [[]] . [] dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [[]] [] . dip i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 . [[]] i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [[]] . i [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 . [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [] . [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] map sum + [3] swaack first [23] swaack sum + + 23 2 [] [[not] [pop 0] [uncons [] dip i] [map sum +] genrec] . map sum + [3] swaack first [23] swaack sum + + 23 2 [] . sum + [3] swaack first [23] swaack sum + + 23 2 [] . 0 [+] catamorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 . [+] catamorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 [+] . catamorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [+] . [uncons swap] swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [+] [uncons swap] . swap hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [uncons swap] [+] . hylomorphism + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec + [3] swaack first [23] swaack sum + + 23 2 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + [3] swaack first [23] swaack sum + + 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 2 23] [[] =] . infra first choice i + [3] swaack first [23] swaack sum + + 23 2 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 2 23] . swaack first choice i + [3] swaack first [23] swaack sum + + 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 2 23] . first choice i + [3] swaack first [23] swaack sum + + 23 2 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i + [3] swaack first [23] swaack sum + + 23 2 [] [pop 0] . i + [3] swaack first [23] swaack sum + + 23 2 [] . pop 0 + [3] swaack first [23] swaack sum + + 23 2 . 0 + [3] swaack first [23] swaack sum + + 23 2 0 . + [3] swaack first [23] swaack sum + + 23 2 . [3] swaack first [23] swaack sum + + 23 2 [3] . swaack first [23] swaack sum + + 3 [2 23] . first [23] swaack sum + + 3 2 . [23] swaack sum + + 3 2 [23] . swaack sum + + 23 [2 3] . sum + + 23 [2 3] . 0 [+] catamorphism + + 23 [2 3] 0 . [+] catamorphism + + 23 [2 3] 0 [+] . catamorphism + + 23 [2 3] 0 [+] . [[] =] roll> [uncons swap] swap hylomorphism + + 23 [2 3] 0 [+] [[] =] . roll> [uncons swap] swap hylomorphism + + 23 [2 3] [[] =] 0 [+] . [uncons swap] swap hylomorphism + + 23 [2 3] [[] =] 0 [+] [uncons swap] . swap hylomorphism + + 23 [2 3] [[] =] 0 [uncons swap] [+] . hylomorphism + + 23 [2 3] [[] =] 0 [uncons swap] [+] . [unit [pop] swoncat] dipd [dip] swoncat genrec + + 23 [2 3] [[] =] 0 [uncons swap] [+] [unit [pop] swoncat] . dipd [dip] swoncat genrec + + 23 [2 3] [[] =] 0 . unit [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] 0 . [] cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] 0 [] . cons [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [0] . [pop] swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [0] [pop] . swoncat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [0] [pop] . swap concat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop] [0] . concat [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] . [uncons swap] [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] . [+] [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [+] . [dip] swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swoncat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [+] [dip] . swap concat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [dip] [+] . concat genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap] [dip +] . genrec + + 23 [2 3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[2 3] 23] [[] =] . infra first choice i + + 23 [2 3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i + + 23 [2 3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i + + 23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] swaack first choice i + + 23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [2 3] 23] . swaack first choice i + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i + + 23 [2 3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i + + 23 [2 3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + + 23 2 [3] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + + 23 [3] 2 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + + + 23 [3] 2 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + + + 23 [3] . [[] =] [pop 0] [uncons swap] [dip +] genrec 2 + + + 23 [3] [[] =] . [pop 0] [uncons swap] [dip +] genrec 2 + + + 23 [3] [[] =] [pop 0] . [uncons swap] [dip +] genrec 2 + + + 23 [3] [[] =] [pop 0] [uncons swap] . [dip +] genrec 2 + + + 23 [3] [[] =] [pop 0] [uncons swap] [dip +] . genrec 2 + + + 23 [3] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[3] 23] [[] =] . infra first choice i 2 + + + 23 [3] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + + + 23 [3] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + + + 23 False . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] swaack first choice i 2 + + + 23 False [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [3] 23] . swaack first choice i 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [False 23] . first choice i 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] False . choice i 2 + + + 23 [3] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . i 2 + + + 23 [3] . uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + + + 23 3 [] . swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + + + 23 [] 3 . [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip + 2 + + + 23 [] 3 [[[] =] [pop 0] [uncons swap] [dip +] genrec] . dip + 2 + + + 23 [] . [[] =] [pop 0] [uncons swap] [dip +] genrec 3 + 2 + + + 23 [] [[] =] . [pop 0] [uncons swap] [dip +] genrec 3 + 2 + + + 23 [] [[] =] [pop 0] . [uncons swap] [dip +] genrec 3 + 2 + + + 23 [] [[] =] [pop 0] [uncons swap] . [dip +] genrec 3 + 2 + + + 23 [] [[] =] [pop 0] [uncons swap] [dip +] . genrec 3 + 2 + + + 23 [] [[] =] [pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] . ifte 3 + 2 + + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [[] 23] [[] =] . infra first choice i 3 + 2 + + + 23 [] . [] = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + + + 23 [] [] . = [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + + + 23 True . [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] swaack first choice i 3 + 2 + + + 23 True [[pop 0] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [] 23] . swaack first choice i 3 + 2 + + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] [True 23] . first choice i 3 + 2 + + + 23 [] [uncons swap [[[] =] [pop 0] [uncons swap] [dip +] genrec] dip +] [pop 0] True . choice i 3 + 2 + + + 23 [] [pop 0] . i 3 + 2 + + + 23 [] . pop 0 3 + 2 + + + 23 . 0 3 + 2 + + + 23 0 . 3 + 2 + + + 23 0 3 . + 2 + + + 23 3 . 2 + + + 23 3 2 . + + + 23 5 . + + 28 . + + +.. code:: ipython2 + + J('[23 [[2 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]] [3 [[23 [[2 []] [3 []]]][23 [[2 []] [3 []]]]]]]] 0 [sum +] [] treestep') + + +.. parsed-literal:: + + 140 + + +.. code:: ipython2 + + J('[] [] [unit cons] [23 +] treestep') + + +.. parsed-literal:: + + [] + + +.. code:: ipython2 + + J('[23 []] [] [unit cons] [23 +] treestep') + + +.. parsed-literal:: + + [46 []] + + +.. code:: ipython2 + + J('[23 [[2 []] [3 []]]] [] [unit cons] [23 +] treestep') + + +.. parsed-literal:: + + [46 [[25 []] [26 []]]] + + +.. code:: ipython2 + + define('treemap == [] [unit cons] roll< treestep') + +.. code:: ipython2 + + J('[23 [[2 []] [3 []]]] [23 +] treemap') + + +.. parsed-literal:: + + [46 [[25 []] [26 []]]] + diff --git a/docs/Library_Examples.pdf b/docs/Library_Examples.pdf new file mode 100644 index 0000000..c534cac Binary files /dev/null and b/docs/Library_Examples.pdf differ diff --git a/docs/Makefile b/docs/Makefile index 27eae21..762197e 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -1,16 +1,7 @@ #docs := $(wildcard *.ipynb) #docs_html := $(patsubst %.ipynb,%.html,$(docs)) -.PHONY: poop nospaces yesspaces - -# https://stackoverflow.com/a/45531875 -# Make cannot handle spaces in filenames, so temporarily rename them -nospaces: - rename -v 's/ /%20/g' *\ * - -# After Make is done, rename files back to having spaces -yesspaces: - rename -v 's/%20/ /g' *%20* +.PHONY: poop poop: *.ipynb diff --git a/docs/notebook_preamble.pyc b/docs/notebook_preamble.pyc new file mode 100644 index 0000000..32b6331 Binary files /dev/null and b/docs/notebook_preamble.pyc differ