{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Blissful Elegance of Typing Joy\n",
    "\n",
    "This notebook presents a simple type inferencer for Joy code.  It can infer the stack effect of most Joy expressions.  It's built largely by means of existing ideas and research.  (A great overview of the existing knowledge is a talk [\"Type Inference in Stack-Based Programming Languages\"](http://prl.ccs.neu.edu/blog/2017/03/10/type-inference-in-stack-based-programming-languages/) given by Rob Kleffner on or about 2017-03-10 as part of a course on the history of programming languages.)\n",
    "\n",
    "The notebook starts with a simple inferencer based on the work of Jaanus Pöial which we then progressively elaborate to cover more Joy semantics.  Along the way we write a simple \"compiler\" that emits Python code for what I like to call Yin functions.  (Yin functions are those that only rearrange values in stacks, as opposed to Yang functions that actually work on the values themselves.)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part I: Pöial's Rules\n",
    "\n",
    "[\"Typing Tools for Typeless Stack Languages\" by Jaanus Pöial\n",
    "](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.212.6026)\n",
    "\n",
    "    @INPROCEEDINGS{Pöial06typingtools,\n",
    "        author = {Jaanus Pöial},\n",
    "        title = {Typing tools for typeless stack languages},\n",
    "        booktitle = {In 23rd Euro-Forth Conference},\n",
    "        year = {2006},\n",
    "        pages = {40--46}\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### First Rule\n",
    "This rule deals with functions (and literals) that put items on the stack `(-- d)`:\n",
    "\n",
    "\n",
    "       (a -- b)∘(-- d)\n",
    "    ---------------------\n",
    "         (a -- b d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Second Rule\n",
    "This rule deals with functions that consume items from the stack `(a --)`:\n",
    "\n",
    "       (a --)∘(c -- d)\n",
    "    ---------------------\n",
    "         (c a -- d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Third Rule\n",
    "The third rule is actually two rules.  These two rules deal with composing functions when the second one will consume one of items the first one produces.  The two types must be [*unified*](https://en.wikipedia.org/wiki/Robinson's_unification_algorithm) or a type conflict declared.\n",
    "\n",
    "       (a -- b t[i])∘(c u[j] -- d)   t <= u (t is subtype of u)\n",
    "    -------------------------------\n",
    "       (a -- b     )∘(c      -- d)   t[i] == t[k] == u[j]\n",
    "                                             ^\n",
    "\n",
    "       (a -- b t[i])∘(c u[j] -- d)   u <= t (u is subtype of t)\n",
    "    -------------------------------\n",
    "       (a -- b     )∘(c      -- d)   t[i] == u[k] == u[j]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's work through some examples by hand to develop an intuition for the algorithm."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There's a function in one of the other notebooks.\n",
    "\n",
    "    F == pop swap roll< rest rest cons cons\n",
    "\n",
    "It's all \"stack chatter\" and list manipulation so we should be able to deduce its type."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stack Effect Comments\n",
    "Joy function types will be represented by Forth-style stack effect comments.  I'm going to use numbers instead of names to keep track of the stack arguments.  (A little bit like [De Bruijn index](https://en.wikipedia.org/wiki/De_Bruijn_index), at least it reminds me of them):\n",
    "\n",
    "    pop (1 --)\n",
    "\n",
    "    swap (1 2 -- 2 1)\n",
    "\n",
    "    roll< (1 2 3 -- 2 3 1)\n",
    "\n",
    "These commands alter the stack but don't \"look at\" the values so these numbers represent an \"Any type\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `pop swap`\n",
    "\n",
    "    (1 --) (1 2 -- 2 1)\n",
    "    \n",
    "Here we encounter a complication. The argument numbers need to be made unique among both sides.   For this let's change `pop` to use 0:\n",
    "\n",
    "    (0 --) (1 2 -- 2 1)\n",
    "\n",
    "Following the second rule:\n",
    "    \n",
    "    (1 2 0 -- 2 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `pop∘swap roll<`\n",
    "\n",
    "    (1 2 0 -- 2 1) (1 2 3 -- 2 3 1)\n",
    "\n",
    "Let's re-label them:\n",
    "\n",
    "    (1a 2a 0a -- 2a 1a) (1b 2b 3b -- 2b 3b 1b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we follow the rules.\n",
    "\n",
    "We must unify `1a` and `3b`, and `2a` and `2b`, replacing the terms in the forms:\n",
    "\n",
    "    (1a 2a 0a -- 2a 1a) (1b 2b 3b -- 2b 3b 1b)\n",
    "                                                w/  {1a: 3b}\n",
    "    (3b 2a 0a -- 2a   ) (1b 2b    -- 2b 3b 1b)\n",
    "                                                w/  {2a: 2b}\n",
    "    (3b 2b 0a --      ) (1b       -- 2b 3b 1b)\n",
    "\n",
    "Here we must apply the second rule:\n",
    "\n",
    "       (3b 2b 0a --) (1b -- 2b 3b 1b)\n",
    "    -----------------------------------\n",
    "         (1b 3b 2b 0a -- 2b 3b 1b)\n",
    "\n",
    "Now we de-label the type, uh, labels:\n",
    "\n",
    "    (1b 3b 2b 0a -- 2b 3b 1b)\n",
    "\n",
    "    w/ {\n",
    "        1b: 1,\n",
    "        3b: 2,\n",
    "        2b: 3,\n",
    "        0a: 0,\n",
    "        }\n",
    "\n",
    "    (1 2 3 0 -- 3 2 1)\n",
    "\n",
    "And now we have the stack effect comment for `pop∘swap∘roll<`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compiling `pop∘swap∘roll<`\n",
    "The simplest way to \"compile\" this function would be something like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def poswrd(s, e, d):\n",
    "    return rolldown(*swap(*pop(s, e, d)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, internally this function would still be allocating tuples (stack cells) and doing other unnecesssary work.\n",
    "\n",
    "Looking ahead for a moment, from the stack effect comment:\n",
    "\n",
    "    (1 2 3 0 -- 3 2 1)\n",
    "\n",
    "We should be able to directly write out a Python function like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def poswrd(stack):\n",
    "    (_, (a, (b, (c, stack)))) = stack\n",
    "    return (c, (b, (a, stack)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This eliminates the internal work of the first version.  Because this function only rearranges the stack and doesn't do any actual processing on the stack items themselves all the information needed to implement it is in the stack effect comment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Functions on Stacks\n",
    "These are slightly tricky.\n",
    "\n",
    "    rest ( [1 ...] -- [...] )\n",
    "\n",
    "    cons ( 1 [...] -- [1 ...] )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `pop∘swap∘roll< rest`\n",
    "\n",
    "    (1 2 3 0 -- 3 2 1) ([1 ...] -- [...])\n",
    "\n",
    "Re-label (instead of adding left and right tags I'm just taking the next available index number for the right-side stack effect comment):\n",
    "\n",
    "    (1 2 3 0 -- 3 2 1) ([4 ...] -- [...])\n",
    "\n",
    "Unify and update:\n",
    "\n",
    "    (1       2 3 0 -- 3 2 1) ([4 ...] -- [...])\n",
    "                                                 w/ {1: [4 ...]}\n",
    "    ([4 ...] 2 3 0 -- 3 2  ) (        -- [...])\n",
    "\n",
    "Apply the first rule:\n",
    "\n",
    "       ([4 ...] 2 3 0 -- 3 2) (-- [...])\n",
    "    ---------------------------------------\n",
    "         ([4 ...] 2 3 0 -- 3 2 [...])\n",
    "\n",
    "And there we are."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `pop∘swap∘roll<∘rest rest`\n",
    "\n",
    "Let's do it again.\n",
    "\n",
    "    ([4 ...] 2 3 0 -- 3 2 [...]) ([1 ...] -- [...])\n",
    "\n",
    "Re-label (the tails of the lists on each side each get their own label):\n",
    "\n",
    "    ([4 .0.] 2 3 0 -- 3 2 [.0.]) ([5 .1.] -- [.1.])\n",
    "\n",
    "Unify and update (note the opening square brackets have been omited in the substitution dict, this is deliberate and I'll explain below):\n",
    "\n",
    "    ([4 .0.]   2 3 0 -- 3 2 [.0.]  ) ([5 .1.] -- [.1.])\n",
    "                                                        w/ { .0.] : 5 .1.] }\n",
    "    ([4 5 .1.] 2 3 0 -- 3 2 [5 .1.]) ([5 .1.] -- [.1.])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How do we find `.0.]` in `[4 .0.]` and replace it with `5 .1.]` getting the result `[4 5 .1.]`?  This might seem hard, but because the underlying structure of the Joy list is a cons-list in Python it's actually pretty easy.  I'll explain below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we unify and find our two terms are the same already: `[5 .1.]`:\n",
    "\n",
    "    ([4 5 .1.] 2 3 0 -- 3 2 [5 .1.]) ([5 .1.] -- [.1.])\n",
    "\n",
    "Giving us:\n",
    "\n",
    "    ([4 5 .1.] 2 3 0 -- 3 2) (-- [.1.])\n",
    "\n",
    "From here we apply the first rule and get:\n",
    "\n",
    "    ([4 5 .1.] 2 3 0 -- 3 2 [.1.])\n",
    "\n",
    "Cleaning up the labels:\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- 3 2 [...])\n",
    "\n",
    "This is the stack effect of `pop∘swap∘roll<∘rest∘rest`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `pop∘swap∘roll<∘rest∘rest cons`\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- 3 2 [...]) (1 [...] -- [1 ...])\n",
    "\n",
    "Re-label:\n",
    "\n",
    "    ([4 5 .1.] 2 3 1 -- 3 2 [.1.]) (6 [.2.] -- [6 .2.])\n",
    "\n",
    "Unify:\n",
    "\n",
    "    ([4 5 .1.] 2 3 1 -- 3 2 [.1.]) (6 [.2.] -- [6 .2.])\n",
    "                                                         w/ { .1.] : .2.] }\n",
    "    ([4 5 .2.] 2 3 1 -- 3 2      ) (6       -- [6 .2.])\n",
    "                                                         w/ {2: 6}\n",
    "    ([4 5 .2.] 6 3 1 -- 3        ) (        -- [6 .2.])\n",
    "\n",
    "First rule:\n",
    "\n",
    "    ([4 5 .2.] 6 3 1 -- 3 [6 .2.])\n",
    "\n",
    "Re-label:\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- 3 [2 ...])\n",
    "\n",
    "Done."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `pop∘swap∘roll<∘rest∘rest∘cons cons`\n",
    "One more time.\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- 3 [2 ...]) (1 [...] -- [1 ...])\n",
    "\n",
    "Re-label:\n",
    "\n",
    "    ([4 5 .1.] 2 3 1 -- 3 [2 .1.]) (6 [.2.] -- [6 .2.])\n",
    "\n",
    "Unify:\n",
    "\n",
    "    ([4 5 .1.] 2 3 1 -- 3 [2 .1.]) (6 [.2.] -- [6 .2.]  )\n",
    "                                                           w/ { .2.] : 2 .1.] }\n",
    "    ([4 5 .1.] 2 3 1 -- 3        ) (6       -- [6 2 .1.])\n",
    "                                                           w/ {3: 6}\n",
    "    ([4 5 .1.] 2 6 1 --          ) (        -- [6 2 .1.])\n",
    "\n",
    "First or second rule:\n",
    "\n",
    "    ([4 5 .1.] 2 6 1 -- [6 2 .1.])\n",
    "\n",
    "Clean up the labels:\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- [3 2 ...])\n",
    "\n",
    "And there you have it, the stack effect for `pop∘swap∘roll<∘rest∘rest∘cons∘cons`.\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- [3 2 ...])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From this stack effect comment it should be possible to construct the following Python code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def F(stack):\n",
    "    (_, (d, (c, ((a, (b, S0)), stack)))) = stack\n",
    "    return (d, (c, S0)), stack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part II: Implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Representing Stack Effect Comments in Python\n",
    "\n",
    "I'm going to use pairs of tuples of type descriptors, which will be integers or tuples of type descriptors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "roll_dn = (1, 2, 3), (2, 3, 1)\n",
    "\n",
    "pop = (1,), ()\n",
    "\n",
    "swap = (1, 2), (2, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `compose()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compose(f, g):\n",
    "\n",
    "    (f_in, f_out), (g_in, g_out) = f, g\n",
    "\n",
    "    # First rule.\n",
    "    #\n",
    "    #       (a -- b) (-- d)\n",
    "    #    ---------------------\n",
    "    #         (a -- b d)\n",
    "\n",
    "    if not g_in:\n",
    "\n",
    "        fg_in, fg_out = f_in, f_out + g_out\n",
    "\n",
    "    # Second rule.\n",
    "    #\n",
    "    #       (a --) (c -- d)\n",
    "    #    ---------------------\n",
    "    #         (c a -- d)\n",
    "\n",
    "    elif not f_out:\n",
    "\n",
    "        fg_in, fg_out = g_in + f_in, g_out\n",
    "\n",
    "    else: # Unify, update, recur.\n",
    "\n",
    "        fo, gi = f_out[-1], g_in[-1]\n",
    "\n",
    "        s = unify(gi, fo)\n",
    "\n",
    "        if s == False:  # s can also be the empty dict, which is ok.\n",
    "            raise TypeError('Cannot unify %r and %r.' % (fo, gi))\n",
    "\n",
    "        f_g = (f_in, f_out[:-1]), (g_in[:-1], g_out)\n",
    "\n",
    "        if s: f_g = update(s, f_g)\n",
    "\n",
    "        fg_in, fg_out = compose(*f_g)\n",
    "\n",
    "    return fg_in, fg_out"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `unify()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unify(u, v, s=None):\n",
    "    if s is None:\n",
    "        s = {}\n",
    "\n",
    "    if isinstance(u, int):\n",
    "        s[u] = v\n",
    "    elif isinstance(v, int):\n",
    "        s[v] = u\n",
    "    else:\n",
    "        s = False\n",
    "\n",
    "    return s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `update()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update(s, term):\n",
    "    if not isinstance(term, tuple):\n",
    "        return s.get(term, term)\n",
    "    return tuple(update(s, inner) for inner in term)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `relabel()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((1,), ()), ((1001, 1002), (1002, 1001)))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def relabel(left, right):\n",
    "    return left, _1000(right)\n",
    "\n",
    "def _1000(right):\n",
    "    if not isinstance(right, tuple):\n",
    "        return 1000 + right\n",
    "    return tuple(_1000(n) for n in right)\n",
    "\n",
    "relabel(pop, swap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `delabel()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((0,), ()), ((1, 2), (2, 1)))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def delabel(f):\n",
    "    s = {u: i for i, u in enumerate(sorted(_unique(f)))}\n",
    "    return update(s, f)\n",
    "\n",
    "def _unique(f, seen=None):\n",
    "    if seen is None:\n",
    "        seen = set()\n",
    "    if not isinstance(f, tuple):\n",
    "        seen.add(f)\n",
    "    else:\n",
    "        for inner in f:\n",
    "            _unique(inner, seen)\n",
    "    return seen\n",
    "\n",
    "delabel(relabel(pop, swap))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `C()`\n",
    "\n",
    "At last we put it all together in a function `C()` that accepts two stack effect comments and returns their composition (or raises and exception if they can't be composed due to type conflicts.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def C(f, g):\n",
    "    f, g = relabel(f, g)\n",
    "    fg = compose(f, g)\n",
    "    return delabel(fg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try it out."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1, 2, 0), (2, 1))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(pop, swap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3, 1, 2, 0), (2, 1, 3))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(C(pop, swap), roll_dn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2, 0, 1), (1, 0, 2))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(swap, roll_dn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3, 1, 2, 0), (2, 1, 3))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(pop, C(swap, roll_dn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3, 1, 2, 0), (2, 1, 3))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "poswrd = reduce(C, (pop, swap, roll_dn))\n",
    "poswrd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stack Functions\n",
    "Here's that trick to represent functions like `rest` and `cons` that manipulate stacks.  We use a cons-list of tuples and give the tails their own numbers.  Then everything above already works. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "rest = ((1, 2),), (2,)\n",
    "\n",
    "cons = (1, 2), ((1, 2),)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((3, 4), 1, 2, 0), (2, 1, 4))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(poswrd, rest)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compare this to the stack effect comment we wrote above:\n",
    "\n",
    "    ((  (3, 4), 1, 2, 0 ), ( 2, 1,   4  ))\n",
    "    (   [4 ...] 2  3  0  --  3  2  [...])\n",
    "\n",
    "The translation table, if you will, would be:\n",
    "\n",
    "    {\n",
    "    3: 4,\n",
    "    4: ...],\n",
    "    1: 2,\n",
    "    2: 3,\n",
    "    0: 0,\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((3, (4, 5)), 1, 2, 0), ((2, (1, 5)),))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = reduce(C, (pop, swap, roll_dn, rest, rest, cons, cons))\n",
    "\n",
    "F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compare with the stack effect comment and you can see it works fine:\n",
    "\n",
    "    ([4 5 ...] 2 3 1 -- [3 2 ...])\n",
    "      3 4  5   1 2 0     2 1  5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dealing with `cons` and `uncons`\n",
    "However, if we try to compose e.g. `cons` and `uncons` it won't work:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "uncons = ((1, 2),), (1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cannot unify (1, 2) and (1001, 1002).\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    C(cons, uncons)\n",
    "except Exception, e:\n",
    "    print e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `unify()` version 2\n",
    "The problem is that the `unify()` function as written doesn't handle the case when both terms are tuples.  We just have to add a clause to deal with this recursively:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unify(u, v, s=None):\n",
    "    if s is None:\n",
    "        s = {}\n",
    "    elif s:\n",
    "        u = update(s, u)\n",
    "        v = update(s, v)\n",
    "\n",
    "    if isinstance(u, int):\n",
    "        s[u] = v\n",
    "\n",
    "    elif isinstance(v, int):\n",
    "        s[v] = u\n",
    "\n",
    "    elif isinstance(u, tuple) and isinstance(v, tuple):\n",
    "\n",
    "        if len(u) != 2 or len(v) != 2:\n",
    "            # Not a type error, caller passed in a bad value.\n",
    "            raise ValueError(repr((u, v)))  # FIXME this message sucks.\n",
    "\n",
    "        (a, b), (c, d) = u, v\n",
    "        s = unify(a, c, s)\n",
    "        if s != False:\n",
    "            s = unify(b, d, s)\n",
    "    else:\n",
    "        s = False\n",
    "\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((0, 1), (0, 1))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(cons, uncons)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part III: Compiling Yin Functions\n",
    "Now consider the Python function we would like to derive:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def F_python(stack):\n",
    "    (_, (d, (c, ((a, (b, S0)), stack)))) = stack\n",
    "    return (d, (c, S0)), stack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And compare it to the input stack effect comment tuple we just computed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3, (4, 5)), 1, 2, 0)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The stack-de-structuring tuple has nearly the same form as our input stack effect comment tuple, just in the reverse order:\n",
    "\n",
    "    (_, (d, (c, ((a, (b, S0)), stack))))\n",
    "\n",
    "Remove the punctuation:\n",
    "\n",
    "     _   d   c   (a, (b, S0))\n",
    "\n",
    "Reverse the order and compare:\n",
    "\n",
    "     (a, (b, S0))   c   d   _\n",
    "    ((3, (4, 5 )),  1,  2,  0)\n",
    "\n",
    "Eh?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the return tuple "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2, (1, 5)),)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "is similar to the output stack effect comment tuple:\n",
    "\n",
    "    ((d, (c, S0)), stack)\n",
    "    ((2, (1, 5 )),      )\n",
    "\n",
    "This should make it pretty easy to write a Python function that accepts the stack effect comment tuples and returns a new Python function (either as a string of code or a function object ready to use) that performs the semantics of that Joy function (described by the stack effect.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python Identifiers\n",
    "We want to substitute Python identifiers for the integers.  I'm going to repurpose `joy.parser.Symbol` class for this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "from joy.parser import Symbol\n",
    "\n",
    "\n",
    "def _names_for():\n",
    "    I = iter(xrange(1000))\n",
    "    return lambda: Symbol('a%i' % next(I))\n",
    "\n",
    "\n",
    "def identifiers(term, s=None):\n",
    "    if s is None:\n",
    "        s = defaultdict(_names_for())\n",
    "    if isinstance(term, int):\n",
    "        return s[term]\n",
    "    return tuple(identifiers(inner, s) for inner in term)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `doc_from_stack_effect()`\n",
    "As a convenience I've implemented a function to convert the Python stack effect comment tuples to reasonable text format.  There are some details in how this code works that related to stuff later in the notebook, so you should skip it for now and read it later if you're interested."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def doc_from_stack_effect(inputs, outputs):\n",
    "    return '(%s--%s)' % (\n",
    "        ' '.join(map(_to_str, inputs + ('',))),\n",
    "        ' '.join(map(_to_str, ('',) + outputs))\n",
    "    )\n",
    "\n",
    "\n",
    "def _to_str(term):\n",
    "    if not isinstance(term, tuple):\n",
    "        try:\n",
    "            t = term.prefix == 's'\n",
    "        except AttributeError:\n",
    "            return str(term)\n",
    "        return '[.%i.]' % term.number if t else str(term)\n",
    "\n",
    "    a = []\n",
    "    while term and isinstance(term, tuple):\n",
    "        item, term = term\n",
    "        a.append(_to_str(item))\n",
    "\n",
    "    try:\n",
    "        n = term.number\n",
    "    except AttributeError:\n",
    "        n = term\n",
    "    else:\n",
    "        if term.prefix != 's':\n",
    "            raise ValueError('Stack label: %s' % (term,))\n",
    "\n",
    "    a.append('.%s.' % (n,))\n",
    "    return '[%s]' % ' '.join(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `compile_()`\n",
    "Now we can write a compiler function to emit Python source code.  (The underscore suffix distiguishes it from the built-in `compile()` function.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compile_(name, f, doc=None):\n",
    "    if doc is None:\n",
    "        doc = doc_from_stack_effect(*f)\n",
    "    inputs, outputs = identifiers(f)\n",
    "    i = o = Symbol('stack')\n",
    "    for term in inputs:\n",
    "        i = term, i\n",
    "    for term in outputs:\n",
    "        o = term, o\n",
    "    return '''def %s(stack):\n",
    "    \"\"\"%s\"\"\"\n",
    "    %s = stack\n",
    "    return %s''' % (name, doc, i, o)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here it is in action:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def F(stack):\n",
      "    \"\"\"([3 4 .5.] 1 2 0 -- [2 1 .5.])\"\"\"\n",
      "    (a5, (a4, (a3, ((a0, (a1, a2)), stack)))) = stack\n",
      "    return ((a4, (a3, a2)), stack)\n"
     ]
    }
   ],
   "source": [
    "source = compile_('F', F)\n",
    "\n",
    "print source"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compare:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def F_python(stack):\n",
    "    (_, (d, (c, ((a, (b, S0)), stack)))) = stack\n",
    "    return ((d, (c, S0)), stack)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next steps:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function F>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = {}\n",
    "\n",
    "eval(compile(source, '__main__', 'single'), {}, L)\n",
    "\n",
    "L['F']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_preamble import D, J, V\n",
    "from joy.library import SimpleFunctionWrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "D['F'] = SimpleFunctionWrapper(L['F'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 2 ...]\n"
     ]
    }
   ],
   "source": [
    "J('[4 5 ...] 2 3 1 F')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With this, we have a partial Joy compiler that works on the subset of Joy functions that manipulate stacks (both what I call \"stack chatter\" and the ones that manipulate stacks on the stack.)\n",
    "\n",
    "I'm probably going to modify the definition wrapper code to detect definitions that can be compiled by this partial compiler and do it automatically.  It might be a reasonable idea to detect sequences of compilable functions in definitions that have uncompilable functions in them and just compile those.  However, if your library is well-factored this might be less helpful."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compiling Library Functions\n",
    "We can use `compile_()` to generate many primitives in the library from their stack effect comments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def defs():\n",
    "\n",
    "    rolldown = (1, 2, 3), (2, 3, 1)\n",
    "\n",
    "    rollup = (1, 2, 3), (3, 1, 2)\n",
    "\n",
    "    pop = (1,), ()\n",
    "\n",
    "    swap = (1, 2), (2, 1)\n",
    "\n",
    "    rest = ((1, 2),), (2,)\n",
    "    \n",
    "    rrest = C(rest, rest)\n",
    "\n",
    "    cons = (1, 2), ((1, 2),)\n",
    "\n",
    "    uncons = ((1, 2),), (1, 2)\n",
    "    \n",
    "    swons = C(swap, cons)\n",
    "\n",
    "    return locals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "def cons(stack):\n",
      "    \"\"\"(1 2 -- [1 .2.])\"\"\"\n",
      "    (a1, (a0, stack)) = stack\n",
      "    return ((a0, a1), stack)\n",
      "\n",
      "\n",
      "def pop(stack):\n",
      "    \"\"\"(1 --)\"\"\"\n",
      "    (a0, stack) = stack\n",
      "    return stack\n",
      "\n",
      "\n",
      "def rest(stack):\n",
      "    \"\"\"([1 .2.] -- 2)\"\"\"\n",
      "    ((a0, a1), stack) = stack\n",
      "    return (a1, stack)\n",
      "\n",
      "\n",
      "def rolldown(stack):\n",
      "    \"\"\"(1 2 3 -- 2 3 1)\"\"\"\n",
      "    (a2, (a1, (a0, stack))) = stack\n",
      "    return (a0, (a2, (a1, stack)))\n",
      "\n",
      "\n",
      "def rollup(stack):\n",
      "    \"\"\"(1 2 3 -- 3 1 2)\"\"\"\n",
      "    (a2, (a1, (a0, stack))) = stack\n",
      "    return (a1, (a0, (a2, stack)))\n",
      "\n",
      "\n",
      "def rrest(stack):\n",
      "    \"\"\"([0 1 .2.] -- 2)\"\"\"\n",
      "    ((a0, (a1, a2)), stack) = stack\n",
      "    return (a2, stack)\n",
      "\n",
      "\n",
      "def swap(stack):\n",
      "    \"\"\"(1 2 -- 2 1)\"\"\"\n",
      "    (a1, (a0, stack)) = stack\n",
      "    return (a0, (a1, stack))\n",
      "\n",
      "\n",
      "def swons(stack):\n",
      "    \"\"\"(0 1 -- [1 .0.])\"\"\"\n",
      "    (a1, (a0, stack)) = stack\n",
      "    return ((a1, a0), stack)\n",
      "\n",
      "\n",
      "def uncons(stack):\n",
      "    \"\"\"([1 .2.] -- 1 2)\"\"\"\n",
      "    ((a0, a1), stack) = stack\n",
      "    return (a1, (a0, stack))\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for name, stack_effect_comment in sorted(defs().items()):\n",
    "    print\n",
    "    print compile_(name, stack_effect_comment)\n",
    "    print"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part IV: Types and Subtypes of Arguments\n",
    "So far we have dealt with types of functions, those dealing with simple stack manipulation.  Let's extend our machinery to deal with types of arguments."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### \"Number\" Type\n",
    "\n",
    "Consider the definition of `sqr`:\n",
    "\n",
    "    sqr == dup mul\n",
    "\n",
    "\n",
    "The `dup` function accepts one *anything* and returns two of that:\n",
    "\n",
    "    dup (1 -- 1 1)\n",
    "\n",
    "And `mul` accepts two \"numbers\" (we're ignoring ints vs. floats vs. complex, etc., for now) and returns just one:\n",
    "\n",
    "    mul (n n -- n)\n",
    "\n",
    "So we're composing:\n",
    "\n",
    "    (1 -- 1 1)∘(n n -- n)\n",
    "\n",
    "The rules say we unify 1 with `n`:\n",
    "\n",
    "       (1 -- 1 1)∘(n n -- n)\n",
    "    ---------------------------  w/  {1: n}\n",
    "       (1 -- 1  )∘(n   -- n)\n",
    "\n",
    "This involves detecting that \"Any type\" arguments can accept \"numbers\".  If we were composing these functions the other way round this is still the case:\n",
    "\n",
    "       (n n -- n)∘(1 -- 1 1)\n",
    "    ---------------------------  w/  {1: n}\n",
    "       (n n --  )∘(  -- n n) \n",
    "\n",
    "The important thing here is that the mapping is going the same way in both cases, from the \"any\" integer to the number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Distinguishing Numbers\n",
    "We should also mind that the number that `mul` produces is not (necessarily) the same as either of its inputs, which are not (necessarily) the same as each other:\n",
    "\n",
    "    mul (n2 n1 -- n3)\n",
    "\n",
    "\n",
    "       (1  -- 1  1)∘(n2 n1 -- n3)\n",
    "    --------------------------------  w/  {1: n2}\n",
    "       (n2 -- n2  )∘(n2    -- n3)\n",
    "\n",
    "\n",
    "       (n2 n1 -- n3)∘(1 -- 1  1 )\n",
    "    --------------------------------  w/  {1: n3}\n",
    "       (n2 n1 --   )∘(  -- n3 n3) \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Distinguishing Types\n",
    "So we need separate domains of \"any\" numbers and \"number\" numbers, and we need to be able to ask the order of these domains.  Now the notes on the right side of rule three make more sense, eh?\n",
    "\n",
    "       (a -- b t[i])∘(c u[j] -- d)   t <= u (t is subtype of u)\n",
    "    -------------------------------\n",
    "       (a -- b     )∘(c      -- d)   t[i] == t[k] == u[j]\n",
    "                                             ^\n",
    "\n",
    "       (a -- b t[i])∘(c u[j] -- d)   u <= t (u is subtype of t)\n",
    "    -------------------------------\n",
    "       (a -- b     )∘(c      -- d)   t[i] == u[k] == u[j]\n",
    "\n",
    "The indices `i`, `k`, and `j` are the number part of our labels and `t` and `u` are the domains.\n",
    "\n",
    "By creative use of Python's \"double underscore\" methods we can define a Python class hierarchy of Joy types and use the `issubclass()` method to establish domain ordering, as well as other handy behaviour that will make it fairly easy to reuse most of the code above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "class AnyJoyType(object):\n",
    "\n",
    "    prefix = 'a'\n",
    "\n",
    "    def __init__(self, number):\n",
    "        self.number = number\n",
    "\n",
    "    def __repr__(self):\n",
    "        return self.prefix + str(self.number)\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return (\n",
    "            isinstance(other, self.__class__)\n",
    "            and other.prefix == self.prefix\n",
    "            and other.number == self.number\n",
    "        )\n",
    "\n",
    "    def __ge__(self, other):\n",
    "        return issubclass(other.__class__, self.__class__)\n",
    "\n",
    "    def __add__(self, other):\n",
    "        return self.__class__(self.number + other)\n",
    "    __radd__ = __add__\n",
    "    \n",
    "    def __hash__(self):\n",
    "        return hash(repr(self))\n",
    "\n",
    "\n",
    "class NumberJoyType(AnyJoyType): prefix = 'n'\n",
    "class FloatJoyType(NumberJoyType): prefix = 'f'\n",
    "class IntJoyType(FloatJoyType): prefix = 'i'\n",
    "\n",
    "\n",
    "class StackJoyType(AnyJoyType):\n",
    "    prefix = 's'\n",
    "\n",
    "\n",
    "_R = range(10)\n",
    "A = map(AnyJoyType, _R)\n",
    "N = map(NumberJoyType, _R)\n",
    "S = map(StackJoyType, _R)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mess with it a little:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import permutations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"Any\" types can be specialized to numbers and stacks, but not vice versa:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a0 >= n0 -> True\n",
      "a0 >= s0 -> True\n",
      "n0 >= a0 -> False\n",
      "n0 >= s0 -> False\n",
      "s0 >= a0 -> False\n",
      "s0 >= n0 -> False\n"
     ]
    }
   ],
   "source": [
    "for a, b in permutations((A[0], N[0], S[0]), 2):\n",
    "    print a, '>=', b, '->', a >= b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our crude [Numerical Tower](https://en.wikipedia.org/wiki/Numerical_tower) of *numbers* > *floats* > *integers* works as well (but we're not going to use it yet):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a0 >= n0 -> True\n",
      "a0 >= f0 -> True\n",
      "a0 >= i0 -> True\n",
      "n0 >= a0 -> False\n",
      "n0 >= f0 -> True\n",
      "n0 >= i0 -> True\n",
      "f0 >= a0 -> False\n",
      "f0 >= n0 -> False\n",
      "f0 >= i0 -> True\n",
      "i0 >= a0 -> False\n",
      "i0 >= n0 -> False\n",
      "i0 >= f0 -> False\n"
     ]
    }
   ],
   "source": [
    "for a, b in permutations((A[0], N[0], FloatJoyType(0), IntJoyType(0)), 2):\n",
    "    print a, '>=', b, '->', a >= b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Typing `sqr`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "dup = (A[1],), (A[1], A[1])\n",
    "\n",
    "mul = (N[1], N[2]), (N[3],)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((a1,), (a1, a1))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((n1, n2), (n3,))"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mul"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Modifying the Inferencer\n",
    "Re-labeling still works fine:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((a1,), (a1, a1)), ((n1001, n1002), (n1003,)))"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = relabel(dup, mul)\n",
    "\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `delabel()` version 2\n",
    "The `delabel()` function needs an overhaul.  It now has to keep track of how many labels of each domain it has \"seen\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "\n",
    "def delabel(f, seen=None, c=None):\n",
    "    if seen is None:\n",
    "        assert c is None\n",
    "        seen, c = {}, Counter()\n",
    "\n",
    "    try:\n",
    "        return seen[f]\n",
    "    except KeyError:\n",
    "        pass\n",
    "\n",
    "    if not isinstance(f, tuple):\n",
    "        seen[f] = f.__class__(c[f.prefix] + 1)\n",
    "        c[f.prefix] += 1\n",
    "        return seen[f]\n",
    "\n",
    "    return tuple(delabel(inner, seen, c) for inner in f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((a1,), (a1, a1)), ((n1, n2), (n3,)))"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delabel(foo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `unify()` version 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unify(u, v, s=None):\n",
    "    if s is None:\n",
    "        s = {}\n",
    "    elif s:\n",
    "        u = update(s, u)\n",
    "        v = update(s, v)\n",
    "\n",
    "    if u == v:\n",
    "        return s\n",
    "\n",
    "    if isinstance(u, AnyJoyType) and isinstance(v, AnyJoyType):\n",
    "        if u >= v:\n",
    "            s[u] = v\n",
    "            return s\n",
    "        if v >= u:\n",
    "            s[v] = u\n",
    "            return s\n",
    "        raise TypeError('Cannot unify %r and %r.' % (u, v))\n",
    "\n",
    "    if isinstance(u, tuple) and isinstance(v, tuple):\n",
    "        if len(u) != len(v) != 2:\n",
    "            raise TypeError(repr((u, v)))\n",
    "        for uu, vv in zip(u, v):\n",
    "            s = unify(uu, vv, s)\n",
    "            if s == False: # (instead of a substitution dict.)\n",
    "                break\n",
    "        return s\n",
    " \n",
    "    if isinstance(v, tuple):\n",
    "        if not stacky(u):\n",
    "            raise TypeError('Cannot unify %r and %r.' % (u, v))\n",
    "        s[u] = v\n",
    "        return s\n",
    "\n",
    "    if isinstance(u, tuple):\n",
    "        if not stacky(v):\n",
    "            raise TypeError('Cannot unify %r and %r.' % (v, u))\n",
    "        s[v] = u\n",
    "        return s\n",
    "\n",
    "    return False\n",
    "\n",
    "\n",
    "def stacky(thing):\n",
    "    return thing.__class__ in {AnyJoyType, StackJoyType}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rewrite the stack effect comments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def defs():\n",
    "\n",
    "    rolldown = (A[1], A[2], A[3]), (A[2], A[3], A[1])\n",
    "\n",
    "    rollup = (A[1], A[2], A[3]), (A[3], A[1], A[2])\n",
    "\n",
    "    pop = (A[1],), ()\n",
    "\n",
    "    popop = (A[2], A[1],), ()\n",
    "\n",
    "    popd = (A[2], A[1],), (A[1],)\n",
    "\n",
    "    popdd = (A[3], A[2], A[1],), (A[2], A[1],)\n",
    "\n",
    "    swap = (A[1], A[2]), (A[2], A[1])\n",
    "\n",
    "    rest = ((A[1], S[1]),), (S[1],)\n",
    "\n",
    "    rrest = C(rest, rest)\n",
    "\n",
    "    cons = (A[1], S[1]), ((A[1], S[1]),)\n",
    "\n",
    "    ccons = C(cons, cons)\n",
    "\n",
    "    uncons = ((A[1], S[1]),), (A[1], S[1])\n",
    "\n",
    "    swons = C(swap, cons)\n",
    "\n",
    "    dup = (A[1],), (A[1], A[1])\n",
    "\n",
    "    dupd = (A[2], A[1]), (A[2], A[2], A[1])\n",
    "\n",
    "    mul = (N[1], N[2]), (N[3],)\n",
    "    \n",
    "    sqrt = C(dup, mul)\n",
    "\n",
    "    first = ((A[1], S[1]),), (A[1],)\n",
    "\n",
    "    second = C(rest, first)\n",
    "\n",
    "    third = C(rest, second)\n",
    "\n",
    "    tuck = (A[2], A[1]), (A[1], A[2], A[1])\n",
    "\n",
    "    over = (A[2], A[1]), (A[2], A[1], A[2])\n",
    "    \n",
    "    succ = pred = (N[1],), (N[2],)\n",
    "    \n",
    "    divmod_ = pm = (N[2], N[1]), (N[4], N[3])\n",
    "\n",
    "    return locals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "DEFS = defs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ccons = (a1 a2 [.1.] -- [a1 a2 .1.])\n",
      "cons = (a1 [.1.] -- [a1 .1.])\n",
      "divmod_ = (n2 n1 -- n4 n3)\n",
      "dup = (a1 -- a1 a1)\n",
      "dupd = (a2 a1 -- a2 a2 a1)\n",
      "first = ([a1 .1.] -- a1)\n",
      "mul = (n1 n2 -- n3)\n",
      "over = (a2 a1 -- a2 a1 a2)\n",
      "pm = (n2 n1 -- n4 n3)\n",
      "pop = (a1 --)\n",
      "popd = (a2 a1 -- a1)\n",
      "popdd = (a3 a2 a1 -- a2 a1)\n",
      "popop = (a2 a1 --)\n",
      "pred = (n1 -- n2)\n",
      "rest = ([a1 .1.] -- [.1.])\n",
      "rolldown = (a1 a2 a3 -- a2 a3 a1)\n",
      "rollup = (a1 a2 a3 -- a3 a1 a2)\n",
      "rrest = ([a1 a2 .1.] -- [.1.])\n",
      "second = ([a1 a2 .1.] -- a2)\n",
      "sqrt = (n1 -- n2)\n",
      "succ = (n1 -- n2)\n",
      "swap = (a1 a2 -- a2 a1)\n",
      "swons = ([.1.] a1 -- [a1 .1.])\n",
      "third = ([a1 a2 a3 .1.] -- a3)\n",
      "tuck = (a2 a1 -- a1 a2 a1)\n",
      "uncons = ([a1 .1.] -- a1 [.1.])\n"
     ]
    }
   ],
   "source": [
    "for name, stack_effect_comment in sorted(DEFS.items()):\n",
    "    print name, '=', doc_from_stack_effect(*stack_effect_comment)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "globals().update(DEFS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Compose `dup` and `mul`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((n1,), (n2,))"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(dup, mul)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Revisit the `F` function, works fine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((a1, (a2, s1)), a3, a4, a5), ((a4, (a3, s1)),))"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))\n",
    "F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([a1 a2 .1.] a3 a4 a5 -- [a4 a3 .1.])\n"
     ]
    }
   ],
   "source": [
    "print doc_from_stack_effect(*F)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some otherwise inefficient functions are no longer to be feared.  We can also get the effect of combinators in some limited cases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "def neato(*funcs):\n",
    "    print doc_from_stack_effect(*reduce(C, funcs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 a2 a3 -- a2 a1 a3)\n"
     ]
    }
   ],
   "source": [
    "# e.g. [swap] dip\n",
    "neato(rollup, swap, rolldown)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 a2 a3 a4 -- a3 a4)\n"
     ]
    }
   ],
   "source": [
    "# e.g. [popop] dipd\n",
    "neato(popdd, rolldown, pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 a2 a3 -- a3 a2 a1)\n"
     ]
    }
   ],
   "source": [
    "# Reverse the order of the top three items.\n",
    "neato(rollup, swap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `compile_()` version 2\n",
    "Because the type labels represent themselves as valid Python identifiers the `compile_()` function doesn't need to generate them anymore:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compile_(name, f, doc=None):\n",
    "    inputs, outputs = f\n",
    "    if doc is None:\n",
    "        doc = doc_from_stack_effect(inputs, outputs)\n",
    "    i = o = Symbol('stack')\n",
    "    for term in inputs:\n",
    "        i = term, i\n",
    "    for term in outputs:\n",
    "        o = term, o\n",
    "    return '''def %s(stack):\n",
    "    \"\"\"%s\"\"\"\n",
    "    %s = stack\n",
    "    return %s''' % (name, doc, i, o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def F(stack):\n",
      "    \"\"\"([a1 a2 .1.] a3 a4 a5 -- [a4 a3 .1.])\"\"\"\n",
      "    (a5, (a4, (a3, ((a1, (a2, s1)), stack)))) = stack\n",
      "    return ((a4, (a3, s1)), stack)\n"
     ]
    }
   ],
   "source": [
    "print compile_('F', F)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But it cannot magically create new functions that involve e.g. math and such.  Note that this is *not* a `sqr` function implementation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def sqr(stack):\n",
      "    \"\"\"(n1 -- n2)\"\"\"\n",
      "    (n1, stack) = stack\n",
      "    return (n2, stack)\n"
     ]
    }
   ],
   "source": [
    "print compile_('sqr', C(dup, mul))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Eventually I should come back around to this becuase it's not tooo difficult to exend this code to be able to compile e.g. `n2 = mul(n1, n1)` for `mul` with the right variable names and insert it in the right place.  It requires a little more support from the library functions, in that we need to know to call `mul()` the Python function for `mul` the Joy function, but since *most* of the math functions (at least) are already wrappers it should be straightforward.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `compilable()`\n",
    "The functions that *can* be compiled are the ones that have only `AnyJoyType` and `StackJoyType` labels in their stack effect comments.  We can write a function to check that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import imap\n",
    "\n",
    "\n",
    "def compilable(f):\n",
    "    return isinstance(f, tuple) and all(imap(compilable, f)) or stacky(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ccons = (a1 a2 [.1.] -- [a1 a2 .1.])\n",
      "cons = (a1 [.1.] -- [a1 .1.])\n",
      "dup = (a1 -- a1 a1)\n",
      "dupd = (a2 a1 -- a2 a2 a1)\n",
      "first = ([a1 .1.] -- a1)\n",
      "over = (a2 a1 -- a2 a1 a2)\n",
      "pop = (a1 --)\n",
      "popd = (a2 a1 -- a1)\n",
      "popdd = (a3 a2 a1 -- a2 a1)\n",
      "popop = (a2 a1 --)\n",
      "rest = ([a1 .1.] -- [.1.])\n",
      "rolldown = (a1 a2 a3 -- a2 a3 a1)\n",
      "rollup = (a1 a2 a3 -- a3 a1 a2)\n",
      "rrest = ([a1 a2 .1.] -- [.1.])\n",
      "second = ([a1 a2 .1.] -- a2)\n",
      "swap = (a1 a2 -- a2 a1)\n",
      "swons = ([.1.] a1 -- [a1 .1.])\n",
      "third = ([a1 a2 a3 .1.] -- a3)\n",
      "tuck = (a2 a1 -- a1 a2 a1)\n",
      "uncons = ([a1 .1.] -- a1 [.1.])\n"
     ]
    }
   ],
   "source": [
    "for name, stack_effect_comment in sorted(defs().items()):\n",
    "    if compilable(stack_effect_comment):\n",
    "        print name, '=', doc_from_stack_effect(*stack_effect_comment)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part V: Functions that use the Stack\n",
    "\n",
    "Consider the `stack` function which grabs the whole stack, quotes it, and puts it on itself:\n",
    "\n",
    "    stack (...     -- ... [...]        )\n",
    "    stack (... a   -- ... a [a ...]    )\n",
    "    stack (... b a -- ... b a [a b ...])\n",
    "\n",
    "We would like to represent this in Python somehow. \n",
    "To do this we use a simple, elegant trick.\n",
    "\n",
    "    stack         S   -- (         S,           S)\n",
    "    stack     (a, S)  -- (     (a, S),      (a, S))\n",
    "    stack (a, (b, S)) -- ( (a, (b, S)), (a, (b, S)))\n",
    "\n",
    "Instead of representing the stack effect comments as a single tuple (with N items in it) we use the same cons-list structure to hold the sequence and `unify()` the whole comments."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `stack∘uncons`\n",
    "Let's try composing `stack` and `uncons`.  We want this result:\n",
    "\n",
    "    stack∘uncons (... a -- ... a a [...])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The stack effects are:\n",
    "\n",
    "    stack = S -- (S, S)\n",
    "\n",
    "    uncons = ((a, Z), S) -- (Z, (a, S))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unifying:\n",
    "\n",
    "      S    -- (S, S) ∘ ((a, Z), S) -- (Z, (a,   S   ))\n",
    "                                                        w/ { S: (a, Z) }\n",
    "    (a, Z) --        ∘             -- (Z, (a, (a, Z)))\n",
    "\n",
    "So:\n",
    "\n",
    "    stack∘uncons == (a, Z) -- (Z, (a, (a, Z)))\n",
    "\n",
    "It works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `stack∘uncons∘uncons`\n",
    "Let's try `stack∘uncons∘uncons`:\n",
    "\n",
    "    (a, S     ) -- (S,      (a, (a, S     ))) ∘ ((b, Z),  S`             ) -- (Z, (b,   S`   ))\n",
    "    \n",
    "                                                                                    w/ { S: (b, Z) }\n",
    "                                                                                    \n",
    "    (a, (b, Z)) -- ((b, Z), (a, (a, (b, Z)))) ∘ ((b, Z),  S`             ) -- (Z, (b,   S`   ))\n",
    "    \n",
    "                                                                                    w/ { S`: (a, (a, (b, Z))) }\n",
    "                                                                                    \n",
    "    (a, (b, Z)) -- ((b, Z), (a, (a, (b, Z)))) ∘ ((b, Z), (a, (a, (b, Z)))) -- (Z, (b, (a, (a, (b, Z)))))\n",
    "\n",
    "    (a, (b, Z)) -- (Z, (b, (a, (a, (b, Z)))))\n",
    "\n",
    "It works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `compose()` version 2\n",
    "This function has to be modified to use the new datastructures and it is no longer recursive, instead recursion happens as part of unification.  Further, the first and second of Pöial's rules are now handled automatically by the unification algorithm.  (One easy way to see this is that now an empty stack effect comment is represented by a `StackJoyType` instance which is not \"falsey\" and so neither of the first two rules' `if` clauses will ever be `True`.  Later on I change the \"truthiness\" of `StackJoyType` to false to let e.g. `joy.utils.stack.concat` work with our stack effect comment cons-list tuples.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compose(f, g):\n",
    "    (f_in, f_out), (g_in, g_out) = f, g\n",
    "    s = unify(g_in, f_out)\n",
    "    if s == False:  # s can also be the empty dict, which is ok.\n",
    "        raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))\n",
    "    return update(s, (f_in, g_out))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I don't want to rewrite all the defs myself, so I'll write a little conversion function instead.  This is programmer's laziness."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sequence_to_stack(seq, stack=StackJoyType(23)):\n",
    "    for item in seq: stack = item, stack\n",
    "    return stack\n",
    "\n",
    "NEW_DEFS = {\n",
    "    name: (sequence_to_stack(i), sequence_to_stack(o))\n",
    "    for name, (i, o) in DEFS.iteritems()\n",
    "}\n",
    "NEW_DEFS['stack'] = S[0], (S[0], S[0])\n",
    "NEW_DEFS['swaack'] = (S[1], S[0]), (S[0], S[1])\n",
    "globals().update(NEW_DEFS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((a1, s1), (s1, (a1, (a1, s1))))"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(stack, uncons)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((a1, (a2, s1)), (s1, (a2, (a1, (a1, (a2, s1))))))"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reduce(C, (stack, uncons, uncons))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The display function should be changed too."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `doc_from_stack_effect()` version 2\n",
    "Clunky junk, but it will suffice for now."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "def doc_from_stack_effect(inputs, outputs):\n",
    "    switch = [False]  # Do we need to display the '...' for the rest of the main stack?\n",
    "    i, o = _f(inputs, switch), _f(outputs, switch)\n",
    "    if switch[0]:\n",
    "        i.append('...')\n",
    "        o.append('...')\n",
    "    return '(%s--%s)' % (\n",
    "        ' '.join(reversed([''] + i)),\n",
    "        ' '.join(reversed(o + [''])),\n",
    "    )\n",
    "\n",
    "\n",
    "def _f(term, switch):\n",
    "    a = []\n",
    "    while term and isinstance(term, tuple):\n",
    "        item, term = term\n",
    "        a.append(item)\n",
    "    assert isinstance(term, StackJoyType), repr(term)\n",
    "    a = [_to_str(i, term, switch) for i in a]\n",
    "    return a\n",
    "\n",
    "\n",
    "def _to_str(term, stack, switch):\n",
    "    if not isinstance(term, tuple):\n",
    "        if term == stack:\n",
    "            switch[0] = True\n",
    "            return '[...]'\n",
    "        return (\n",
    "            '[.%i.]' % term.number\n",
    "            if isinstance(term, StackJoyType)\n",
    "            else str(term)\n",
    "        )\n",
    "\n",
    "    a = []\n",
    "    while term and isinstance(term, tuple):\n",
    "        item, term = term\n",
    "        a.append(_to_str(item, stack, switch))\n",
    "    assert isinstance(term, StackJoyType), repr(term)\n",
    "    if term == stack:\n",
    "        switch[0] = True\n",
    "        end = '...'\n",
    "    else:\n",
    "        end = '.%i.' % term.number\n",
    "    a.append(end)\n",
    "    return '[%s]' % ' '.join(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ccons = (a1 a2 [.1.] -- [a1 a2 .1.])\n",
      "cons = (a1 [.1.] -- [a1 .1.])\n",
      "divmod_ = (n2 n1 -- n4 n3)\n",
      "dup = (a1 -- a1 a1)\n",
      "dupd = (a2 a1 -- a2 a2 a1)\n",
      "first = ([a1 .1.] -- a1)\n",
      "mul = (n1 n2 -- n3)\n",
      "over = (a2 a1 -- a2 a1 a2)\n",
      "pm = (n2 n1 -- n4 n3)\n",
      "pop = (a1 --)\n",
      "popd = (a2 a1 -- a1)\n",
      "popdd = (a3 a2 a1 -- a2 a1)\n",
      "popop = (a2 a1 --)\n",
      "pred = (n1 -- n2)\n",
      "rest = ([a1 .1.] -- [.1.])\n",
      "rolldown = (a1 a2 a3 -- a2 a3 a1)\n",
      "rollup = (a1 a2 a3 -- a3 a1 a2)\n",
      "rrest = ([a1 a2 .1.] -- [.1.])\n",
      "second = ([a1 a2 .1.] -- a2)\n",
      "sqrt = (n1 -- n2)\n",
      "stack = (... -- ... [...])\n",
      "succ = (n1 -- n2)\n",
      "swaack = ([.1.] -- [.0.])\n",
      "swap = (a1 a2 -- a2 a1)\n",
      "swons = ([.1.] a1 -- [a1 .1.])\n",
      "third = ([a1 a2 a3 .1.] -- a3)\n",
      "tuck = (a2 a1 -- a1 a2 a1)\n",
      "uncons = ([a1 .1.] -- a1 [.1.])\n"
     ]
    }
   ],
   "source": [
    "for name, stack_effect_comment in sorted(NEW_DEFS.items()):\n",
    "    print name, '=', doc_from_stack_effect(*stack_effect_comment)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(... -- ... [...])\n",
      "\n",
      "(... a1 -- ... a1 a1 [...])\n",
      "\n",
      "(... a2 a1 -- ... a2 a1 a1 a2 [...])\n",
      "\n",
      "(... a1 -- ... a1 [a1 ...])\n"
     ]
    }
   ],
   "source": [
    "print ; print doc_from_stack_effect(*stack)\n",
    "print ; print doc_from_stack_effect(*C(stack, uncons))\n",
    "print ; print doc_from_stack_effect(*reduce(C, (stack, uncons, uncons)))\n",
    "print ; print doc_from_stack_effect(*reduce(C, (stack, uncons, cons)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(... a2 a1 [.1.] -- ... [a2 a1 .1.] [[a2 a1 .1.] ...])\n"
     ]
    }
   ],
   "source": [
    "print doc_from_stack_effect(*C(ccons, stack))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((s1, (a1, (a2, s2))), (((a2, (a1, s1)), s2), ((a2, (a1, s1)), s2)))"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q = C(ccons, stack)\n",
    "\n",
    "Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `compile_()` version 3\n",
    "This makes the `compile_()` function pretty simple as the stack effect comments are now already in the form needed for the Python code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compile_(name, f, doc=None):\n",
    "    i, o = f\n",
    "    if doc is None:\n",
    "        doc = doc_from_stack_effect(i, o)\n",
    "    return '''def %s(stack):\n",
    "    \"\"\"%s\"\"\"\n",
    "    %s = stack\n",
    "    return %s''' % (name, doc, i, o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def Q(stack):\n",
      "    \"\"\"(... a2 a1 [.1.] -- ... [a2 a1 .1.] [[a2 a1 .1.] ...])\"\"\"\n",
      "    (s1, (a1, (a2, s2))) = stack\n",
      "    return (((a2, (a1, s1)), s2), ((a2, (a1, s1)), s2))\n"
     ]
    }
   ],
   "source": [
    "print compile_('Q', Q)"
   ]
  },
  {
   "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": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "unstack = (S[1], S[0]), S[1]\n",
    "enstacken = S[0], (S[0], S[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([.1.] --)\n"
     ]
    }
   ],
   "source": [
    "print doc_from_stack_effect(*unstack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-- [.0.])\n"
     ]
    }
   ],
   "source": [
    "print doc_from_stack_effect(*enstacken)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 [.1.] -- a1)\n"
     ]
    }
   ],
   "source": [
    "print doc_from_stack_effect(*C(cons, unstack))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 [.1.] -- [[a1 .1.] .2.])\n"
     ]
    }
   ],
   "source": [
    "print doc_from_stack_effect(*C(cons, enstacken))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((s1, (a1, s2)), (a1, s1))"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C(cons, unstack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part VI: Multiple Stack Effects\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "class IntJoyType(NumberJoyType): prefix = 'i'\n",
    "\n",
    "\n",
    "F = map(FloatJoyType, _R)\n",
    "I = map(IntJoyType, _R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "muls = [\n",
    "     ((I[2], (I[1], S[0])), (I[3], S[0])),\n",
    "     ((F[2], (I[1], S[0])), (F[3], S[0])),\n",
    "     ((I[2], (F[1], S[0])), (F[3], S[0])),\n",
    "     ((F[2], (F[1], S[0])), (F[3], S[0])),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(i1 i2 -- i3)\n",
      "(i1 f2 -- f3)\n",
      "(f1 i2 -- f3)\n",
      "(f1 f2 -- f3)\n"
     ]
    }
   ],
   "source": [
    "for f in muls:\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 -- a1 a1) (i1 i2 -- i3) (i1 -- i2)\n",
      "(a1 -- a1 a1) (f1 f2 -- f3) (f1 -- f2)\n"
     ]
    }
   ],
   "source": [
    "for f in muls:\n",
    "    try:\n",
    "        e = C(dup, f)\n",
    "    except TypeError:\n",
    "        continue\n",
    "    print doc_from_stack_effect(*dup), doc_from_stack_effect(*f), doc_from_stack_effect(*e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import product\n",
    "\n",
    "\n",
    "def meta_compose(F, G):\n",
    "    for f, g in product(F, G):\n",
    "        try:\n",
    "            yield C(f, g)\n",
    "        except TypeError:\n",
    "            pass\n",
    "\n",
    "\n",
    "def MC(F, G):\n",
    "    return sorted(set(meta_compose(F, G)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(n1 -- n2)\n"
     ]
    }
   ],
   "source": [
    "for f in MC([dup], [mul]):\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(f1 -- f2)\n",
      "(i1 -- i2)\n"
     ]
    }
   ],
   "source": [
    "for f in MC([dup], muls):\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Representing an Unbounded Sequence of Types\n",
    "\n",
    "We can borrow a trick from [Brzozowski's Derivatives of Regular Expressions](https://en.wikipedia.org/wiki/Brzozowski_derivative) to invent a new type of type variable, a \"sequence type\" (I think this is what they mean in the literature by that term...) or \"[Kleene Star](https://en.wikipedia.org/wiki/Kleene_star)\" type.  I'm going to represent it as a type letter and the asterix, so a sequence of zero or more `AnyJoyType` variables would be:\n",
    "\n",
    "    A*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `A*` works by splitting the universe into two alternate histories:\n",
    "\n",
    "    A* -> 0 | A A*\n",
    "\n",
    "The Kleene star variable disappears in one universe, and in the other it turns into an `AnyJoyType` variable followed by itself again.  We have to return all universes (represented by their substitution dicts, the \"unifiers\") that don't lead to type conflicts."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider unifying two stacks (the lowercase letters are any type variables of the kinds we have defined so far):\n",
    "\n",
    "    [a A* b .0.] U [c d .1.]\n",
    "                              w/ {c: a}\n",
    "    [  A* b .0.] U [  d .1.]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we have to split universes to unify `A*`.  In the first universe it disappears:\n",
    "\n",
    "    [b .0.] U [d .1.]\n",
    "                       w/ {d: b, .1.: .0.} \n",
    "         [] U []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While in the second it spawns an `A`, which we will label `e`:\n",
    "\n",
    "    [e A* b .0.] U [d .1.]\n",
    "                            w/ {d: e}\n",
    "    [  A* b .0.] U [  .1.]\n",
    "                            w/ {.1.: A* b .0.}\n",
    "    [  A* b .0.] U [  A* b .0.]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Giving us two unifiers:\n",
    "\n",
    "    {c: a,  d: b,  .1.:      .0.}\n",
    "    {c: a,  d: e,  .1.: A* b .0.}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "class KleeneStar(object):\n",
    "\n",
    "    kind = AnyJoyType\n",
    "\n",
    "    def __init__(self, number):\n",
    "        self.number = number\n",
    "        self.count = 0\n",
    "        self.prefix = repr(self)\n",
    "\n",
    "    def __repr__(self):\n",
    "        return '%s%i*' % (self.kind.prefix, self.number)\n",
    "\n",
    "    def another(self):\n",
    "        self.count += 1\n",
    "        return self.kind(10000 * self.number + self.count)\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return (\n",
    "            isinstance(other, self.__class__)\n",
    "            and other.number == self.number\n",
    "        )\n",
    "\n",
    "    def __ge__(self, other):\n",
    "        return self.kind >= other.kind\n",
    "\n",
    "    def __add__(self, other):\n",
    "        return self.__class__(self.number + other)\n",
    "    __radd__ = __add__\n",
    "    \n",
    "    def __hash__(self):\n",
    "        return hash(repr(self))\n",
    "\n",
    "class AnyStarJoyType(KleeneStar): kind = AnyJoyType\n",
    "class NumberStarJoyType(KleeneStar): kind = NumberJoyType\n",
    "#class FloatStarJoyType(KleeneStar): kind = FloatJoyType\n",
    "#class IntStarJoyType(KleeneStar): kind = IntJoyType\n",
    "class StackStarJoyType(KleeneStar): kind = StackJoyType\n",
    "\n",
    "\n",
    "As = map(AnyStarJoyType, _R)\n",
    "Ns = map(NumberStarJoyType, _R)\n",
    "Ss = map(StackStarJoyType, _R)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `unify()` version 4\n",
    "Can now return multiple results..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unify(u, v, s=None):\n",
    "    if s is None:\n",
    "        s = {}\n",
    "    elif s:\n",
    "        u = update(s, u)\n",
    "        v = update(s, v)\n",
    "\n",
    "    if u == v:\n",
    "        return s,\n",
    "\n",
    "    if isinstance(u, AnyJoyType) and isinstance(v, AnyJoyType):\n",
    "        if u >= v:\n",
    "            s[u] = v\n",
    "            return s,\n",
    "        if v >= u:\n",
    "            s[v] = u\n",
    "            return s,\n",
    "        raise TypeError('Cannot unify %r and %r.' % (u, v))\n",
    "\n",
    "    if isinstance(u, tuple) and isinstance(v, tuple):\n",
    "        if len(u) != len(v) != 2:\n",
    "            raise TypeError(repr((u, v)))\n",
    "            \n",
    "        a, b = v\n",
    "        if isinstance(a, KleeneStar):\n",
    "            # Two universes, in one the Kleene star disappears and unification\n",
    "            # continues without it...\n",
    "            s0 = unify(u, b)\n",
    "            \n",
    "            # In the other it spawns a new variable.\n",
    "            s1 = unify(u, (a.another(), v))\n",
    "            \n",
    "            t = s0 + s1\n",
    "            for sn in t:\n",
    "                sn.update(s)\n",
    "            return t\n",
    "\n",
    "        a, b = u\n",
    "        if isinstance(a, KleeneStar):\n",
    "            s0 = unify(v, b)\n",
    "            s1 = unify(v, (a.another(), u))\n",
    "            t = s0 + s1\n",
    "            for sn in t:\n",
    "                sn.update(s)\n",
    "            return t\n",
    "\n",
    "        ses = unify(u[0], v[0], s)\n",
    "        results = ()\n",
    "        for sn in ses:\n",
    "            results += unify(u[1], v[1], sn)\n",
    "        return results\n",
    " \n",
    "    if isinstance(v, tuple):\n",
    "        if not stacky(u):\n",
    "            raise TypeError('Cannot unify %r and %r.' % (u, v))\n",
    "        s[u] = v\n",
    "        return s,\n",
    "\n",
    "    if isinstance(u, tuple):\n",
    "        if not stacky(v):\n",
    "            raise TypeError('Cannot unify %r and %r.' % (v, u))\n",
    "        s[v] = u\n",
    "        return s,\n",
    "\n",
    "    return ()\n",
    "\n",
    "\n",
    "def stacky(thing):\n",
    "    return thing.__class__ in {AnyJoyType, StackJoyType}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(a1*, s1)"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = (As[1], S[1])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(a1, s2)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = (A[1], S[2])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{s1: (a1, s2)} -> (a1*, (a1, s2)) (a1, s2)\n",
      "{a1: a10001, s2: (a1*, s1)} -> (a1*, s1) (a10001, (a1*, s1))\n"
     ]
    }
   ],
   "source": [
    "for result in unify(b, a):\n",
    "    print result, '->', update(result, a), update(result, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{s1: (a1, s2)} -> (a1*, (a1, s2)) (a1, s2)\n",
      "{a1: a10002, s2: (a1*, s1)} -> (a1*, s1) (a10002, (a1*, s1))\n"
     ]
    }
   ],
   "source": [
    "for result in unify(a, b):\n",
    "    print result, '->', update(result, a), update(result, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "    (a1*, s1)       [a1*]       (a1, s2)        [a1]\n",
    "\n",
    "    (a1*, (a1, s2)) [a1* a1]    (a1, s2)        [a1]\n",
    "\n",
    "    (a1*, s1)       [a1*]       (a2, (a1*, s1)) [a2 a1*]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([n1* .1.] -- n0)\n"
     ]
    }
   ],
   "source": [
    "sum_ = ((Ns[1], S[1]), S[0]), (N[0], S[0])\n",
    "\n",
    "print doc_from_stack_effect(*sum_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-- [n1 n2 n3 .1.])\n"
     ]
    }
   ],
   "source": [
    "f = (N[1], (N[2], (N[3], S[1]))), S[0]\n",
    "\n",
    "print doc_from_stack_effect(S[0], f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{s1: (n1, (n2, (n3, s1)))} -> (n0, s0)\n",
      "{n1: n10001, s1: (n2, (n3, s1))} -> (n0, s0)\n",
      "{n1: n10001, s1: (n3, s1), n2: n10002} -> (n0, s0)\n",
      "{n1: n10001, s1: (n1*, s1), n3: n10003, n2: n10002} -> (n0, s0)\n"
     ]
    }
   ],
   "source": [
    "for result in unify(sum_[0], f):\n",
    "    print result, '->', update(result, sum_[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `compose()` version 3\n",
    "This function has to be modified to yield multiple results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compose(f, g):\n",
    "    (f_in, f_out), (g_in, g_out) = f, g\n",
    "    s = unify(g_in, f_out)\n",
    "    if not s:\n",
    "        raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))\n",
    "    for result in s:\n",
    "        yield update(result, (f_in, g_out))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "def meta_compose(F, G):\n",
    "    for f, g in product(F, G):\n",
    "        try:\n",
    "            for result in C(f, g):\n",
    "                yield result\n",
    "        except TypeError:\n",
    "            pass\n",
    "\n",
    "\n",
    "def C(f, g):\n",
    "    f, g = relabel(f, g)\n",
    "    for fg in compose(f, g):\n",
    "        yield delabel(fg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(f1 -- f2)\n",
      "(i1 -- i2)\n"
     ]
    }
   ],
   "source": [
    "for f in MC([dup], muls):\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([n1* .1.] -- [n1* .1.] n1)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "for f in MC([dup], [sum_]):\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 [.1.] -- n1)\n",
      "(n1 [n1* .1.] -- n2)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "for f in MC([cons], [sum_]):\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a1 [.1.] -- [a1 .1.]) ([n1 n1* .1.] -- n0) (n1 [n1* .1.] -- n2)\n"
     ]
    }
   ],
   "source": [
    "sum_ = (((N[1], (Ns[1], S[1])), S[0]), (N[0], S[0]))\n",
    "print doc_from_stack_effect(*cons),\n",
    "print doc_from_stack_effect(*sum_),\n",
    "\n",
    "for f in MC([cons], [sum_]):\n",
    "    print doc_from_stack_effect(*f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(a4, (a1*, (a3, s1)))"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = (A[4], (As[1], (A[3], S[1])))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(a1, (a2, s2))"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = (A[1], (A[2], S[2]))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{a1: a4, s2: s1, a2: a3}\n",
      "{a1: a4, s2: (a1*, (a3, s1)), a2: a10003}\n"
     ]
    }
   ],
   "source": [
    "for result in unify(b, a):\n",
    "    print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{s2: s1, a2: a3, a4: a1}\n",
      "{s2: (a1*, (a3, s1)), a2: a10004, a4: a1}\n"
     ]
    }
   ],
   "source": [
    "for result in unify(a, b):\n",
    "    print result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part VII: Typing Combinators\n",
    "\n",
    "In order to compute the stack effect of combinators you kinda have to have the quoted programs they expect available.  In the most general case, the `i` combinator, you can't say anything about its stack effect other than it expects one quote:\n",
    "\n",
    "    i (... [.1.] -- ... .1.)\n",
    "\n",
    "Or\n",
    "\n",
    "    i (... [A* .1.] -- ... A*)\n",
    "\n",
    "Consider the type of:\n",
    "\n",
    "    [cons] dip\n",
    "\n",
    "Obviously it would be:\n",
    "\n",
    "    (a1 [..1] a2 -- [a1 ..1] a2)\n",
    "\n",
    "`dip` itself could have:\n",
    "\n",
    "    (a1 [..1] -- ... then what?\n",
    "\n",
    "\n",
    "Without any information about the contents of the quote we can't say much about the result."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hybrid Inferencer/Interpreter\n",
    "I think there's a way forward.  If we convert our list (of terms we are composing) into a stack structure we can use it as a *Joy expression*, then we can treat the *output half* of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly.  We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack.  For combinators like `branch` that can have more than one stack effect we have to \"split universes\" again and return both."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Joy Types for Functions\n",
    "We need a type variable for Joy functions that can go in our expressions and be used by the hybrid inferencer/interpreter.  They have to store a name and a list of stack effects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FunctionJoyType(AnyJoyType):\n",
    "\n",
    "    def __init__(self, name, sec, number):\n",
    "        self.name = name\n",
    "        self.stack_effects = sec\n",
    "        self.number = number\n",
    "\n",
    "    def __add__(self, other):\n",
    "        return self\n",
    "    __radd__ = __add__\n",
    "\n",
    "    def __repr__(self):\n",
    "        return self.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Specialized for Simple Functions and Combinators\n",
    "For non-combinator functions the stack effects list contains stack effect comments (represented by pairs of cons-lists as described above.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "class SymbolJoyType(FunctionJoyType):\n",
    "    prefix = 'F'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For combinators the list contains Python functions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CombinatorJoyType(FunctionJoyType):\n",
    "\n",
    "    prefix = 'C'\n",
    "\n",
    "    def __init__(self, name, sec, number, expect=None):\n",
    "        super(CombinatorJoyType, self).__init__(name, sec, number)\n",
    "        self.expect = expect\n",
    "\n",
    "    def enter_guard(self, f):\n",
    "        if self.expect is None:\n",
    "            return f\n",
    "        g = self.expect, self.expect\n",
    "        new_f = list(compose(f, g, ()))\n",
    "        assert len(new_f) == 1, repr(new_f)\n",
    "        return new_f[0][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For simple combinators that have only one effect (like ``dip``) you only need one function and it can be the combinator itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "import joy.library\n",
    "\n",
    "dip = CombinatorJoyType('dip', [joy.library.dip], 23)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For combinators that can have more than one effect (like ``branch``) you have to write functions that each implement the action of one of the effects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "def branch_true(stack, expression, dictionary):\n",
    "    (then, (else_, (flag, stack))) = stack\n",
    "    return stack, concat(then, expression), dictionary\n",
    "\n",
    "def branch_false(stack, expression, dictionary):\n",
    "    (then, (else_, (flag, stack))) = stack\n",
    "    return stack, concat(else_, expression), dictionary\n",
    "\n",
    "branch = CombinatorJoyType('branch', [branch_true, branch_false], 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also provide an optional stack effect, input-side only, that will then be used as an identity function (that accepts and returns stacks that match the \"guard\" stack effect) which will be used to guard against type mismatches going into the evaluation of the combinator."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `infer()`\n",
    "With those in place, we can define a function that accepts a sequence of Joy type variables, including ones representing functions (not just values), and attempts to grind out all the possible stack effects of that expression.\n",
    "\n",
    "One tricky thing is that type variables *in the expression* have to be updated along with the stack effects after doing unification or we risk losing useful information.  This was a straightforward, if awkward, modification to the call structure of `meta_compose()` et. al."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "ID = S[0], S[0]  # Identity function.\n",
    "\n",
    "\n",
    "def infer(*expression):\n",
    "    return sorted(set(_infer(list_to_stack(expression))))\n",
    "\n",
    "\n",
    "def _infer(e, F=ID):\n",
    "    _log_it(e, F)\n",
    "    if not e:\n",
    "        return [F]\n",
    "\n",
    "    n, e = e\n",
    "\n",
    "    if isinstance(n, SymbolJoyType):\n",
    "        eFG = meta_compose([F], n.stack_effects, e)\n",
    "        res = flatten(_infer(e, Fn) for e, Fn in eFG)\n",
    "\n",
    "    elif isinstance(n, CombinatorJoyType):\n",
    "        fi, fo = n.enter_guard(F)\n",
    "        res = flatten(_interpret(f, fi, fo, e) for f in n.stack_effects)\n",
    "\n",
    "    elif isinstance(n, Symbol):\n",
    "        assert n not in FUNCTIONS, repr(n)\n",
    "        func = joy.library._dictionary[n]\n",
    "        res = _interpret(func, F[0], F[1], e)\n",
    "\n",
    "    else:\n",
    "        fi, fo = F\n",
    "        res = _infer(e, (fi, (n, fo)))\n",
    "\n",
    "    return res\n",
    "\n",
    "\n",
    "def _interpret(f, fi, fo, e):\n",
    "    new_fo, ee, _ = f(fo, e, {})\n",
    "    ee = update(FUNCTIONS, ee)  # Fix Symbols.\n",
    "    new_F = fi, new_fo\n",
    "    return _infer(ee, new_F)\n",
    "\n",
    "\n",
    "def _log_it(e, F):\n",
    "    _log.info(\n",
    "        u'%3i %s ∘ %s',\n",
    "        len(inspect_stack()),\n",
    "        doc_from_stack_effect(*F),\n",
    "        expression_to_string(e),\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Work in Progress\n",
    "And that brings us to current Work-In-Progress.  The mixed-mode inferencer/interpreter `infer()` function seems to work well.  There are details I should document, and the rest of the code in the \"polytypes\" module (FIXME link to its docs here!) should be explained...  There is cruft to convert the definitions in `DEFS` to the new `SymbolJoyType` objects, and some combinators.  Here is an example of output from the current code :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "1/0  # (Don't try to run this cell!  It's not going to work.  This is \"read only\" code heh..)\n",
    "\n",
    "logging.basicConfig(format='%(message)s', stream=sys.stdout, level=logging.INFO)\n",
    "\n",
    "globals().update(FUNCTIONS)\n",
    "\n",
    "h = infer((pred, s2), (mul, s3), (div, s4), (nullary, (bool, s5)), dipd, branch)\n",
    "\n",
    "print '-' * 40\n",
    "\n",
    "for fi, fo in h:\n",
    "    print doc_from_stack_effect(fi, fo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The numbers at the start of the lines are the current depth of the Python call stack.  They're followed by the current computed stack effect (initialized to `ID`) then the pending expression (the inference of the stack effect of which is the whole object of the current example.)\n",
    "\n",
    "In this example we are implementing (and inferring) `ifte` as `[nullary bool] dipd branch` which shows off a lot of the current implementation in action.\n",
    "\n",
    "      7 (--) ∘ [pred] [mul] [div] [nullary bool] dipd branch\n",
    "      8 (-- [pred ...2]) ∘ [mul] [div] [nullary bool] dipd branch\n",
    "      9 (-- [pred ...2] [mul ...3]) ∘ [div] [nullary bool] dipd branch\n",
    "     10 (-- [pred ...2] [mul ...3] [div ...4]) ∘ [nullary bool] dipd branch\n",
    "     11 (-- [pred ...2] [mul ...3] [div ...4] [nullary bool ...5]) ∘ dipd branch\n",
    "     15 (-- [pred ...5]) ∘ nullary bool [mul] [div] branch\n",
    "     19 (-- [pred ...2]) ∘ [stack] dinfrirst bool [mul] [div] branch\n",
    "     20 (-- [pred ...2] [stack ]) ∘ dinfrirst bool [mul] [div] branch\n",
    "     22 (-- [pred ...2] [stack ]) ∘ dip infra first bool [mul] [div] branch\n",
    "     26 (--) ∘ stack [pred] infra first bool [mul] [div] branch\n",
    "     29 (... -- ... [...]) ∘ [pred] infra first bool [mul] [div] branch\n",
    "     30 (... -- ... [...] [pred ...1]) ∘ infra first bool [mul] [div] branch\n",
    "     34 (--) ∘ pred s1 swaack first bool [mul] [div] branch\n",
    "     37 (n1 -- n2) ∘ [n1] swaack first bool [mul] [div] branch\n",
    "     38 (... n1 -- ... n2 [n1 ...]) ∘ swaack first bool [mul] [div] branch\n",
    "     41 (... n1 -- ... n1 [n2 ...]) ∘ first bool [mul] [div] branch\n",
    "     44 (n1 -- n1 n2) ∘ bool [mul] [div] branch\n",
    "     47 (n1 -- n1 b1) ∘ [mul] [div] branch\n",
    "     48 (n1 -- n1 b1 [mul ...1]) ∘ [div] branch\n",
    "     49 (n1 -- n1 b1 [mul ...1] [div ...2]) ∘ branch\n",
    "     53 (n1 -- n1) ∘ div\n",
    "     56 (f2 f1 -- f3) ∘ \n",
    "     56 (i1 f1 -- f2) ∘ \n",
    "     56 (f1 i1 -- f2) ∘ \n",
    "     56 (i2 i1 -- f1) ∘ \n",
    "     53 (n1 -- n1) ∘ mul\n",
    "     56 (f2 f1 -- f3) ∘ \n",
    "     56 (i1 f1 -- f2) ∘ \n",
    "     56 (f1 i1 -- f2) ∘ \n",
    "     56 (i2 i1 -- i3) ∘ \n",
    "    ----------------------------------------\n",
    "    (f2 f1 -- f3)\n",
    "    (i1 f1 -- f2)\n",
    "    (f1 i1 -- f2)\n",
    "    (i2 i1 -- f1)\n",
    "    (i2 i1 -- i3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "We built a simple type inferencer, and a kind of crude \"compiler\" for a subset of Joy functions.  Then we built a more powerful inferencer that actually does some evaluation and explores branching code paths"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Work remains to be done:\n",
    "\n",
    "- the rest of the library has to be covered\n",
    "- figure out how to deal with `loop` and `genrec`, etc..\n",
    "- extend the types to check values (see the appendix)\n",
    "- other kinds of \"higher order\" type variables, OR, AND, etc..\n",
    "- maybe rewrite in Prolog for great good?\n",
    "- definitions\n",
    "  - don't permit composition of functions that don't compose\n",
    "  - auto-compile compilable functions\n",
    "- Compiling more than just the Yin functions.\n",
    "- getting better visibility (than Python debugger.)\n",
    "- DOOOOCS!!!!  Lots of docs!\n",
    "  - docstrings all around\n",
    "  - improve this notebook (it kinda falls apart at the end narratively.  I went off and just started writing code to see if it would work.  It does, but now I have to come back and describe here what I did."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Appendix: Joy in the Logical Paradigm\n",
    "\n",
    "For *type checking* to work the type label classes have to be modified to let `T >= t` succeed, where e.g. `T` is `IntJoyType` and `t` is `int`.  If you do that you can take advantage of the *logical relational* nature of the stack effect comments to \"compute in reverse\" as it were.  There's a working demo of this at the end of the `polytypes` module.  But if you're interested in all that you should just use Prolog!\n",
    "\n",
    "Anyhow, type *checking* is a few easy steps away."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _ge(self, other):\n",
    "    return (issubclass(other.__class__, self.__class__)\n",
    "            or hasattr(self, 'accept')\n",
    "            and isinstance(other, self.accept))\n",
    "\n",
    "AnyJoyType.__ge__ = _ge\n",
    "AnyJoyType.accept = tuple, int, float, long, str, unicode, bool, Symbol\n",
    "StackJoyType.accept = tuple"
   ]
  }
 ],
 "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.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
