{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f7ecdba7",
   "metadata": {},
   "source": [
    "# BigInts in Joy\n",
    "\n",
    "Part of the puzzle is implementing \"bigints\", unbounded integers, by means of Oberon RISC signed 32-bit ints and their operations.\n",
    "\n",
    "We can model bigints as a pair of a Boolean value for the sign and a list of integers for the digits, to keep things simple let the bool be the first item on a list followed by zero or more int digits.  The Least Signifigant digit is at the top or head of the list.  Our *base* for the digits is:\n",
    "\n",
    "$$2^{31}$$\n",
    "\n",
    "Our digits are 0..2147483647 (our \"nine\".)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "08a49b81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2147483648"
     ]
    }
   ],
   "source": [
    "1 31 lshift "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5511a7ed",
   "metadata": {},
   "source": [
    "We can `inscribe` a constant function `base` to keep this value handy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "628aa432",
   "metadata": {},
   "outputs": [],
   "source": [
    "unit "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b52e0112",
   "metadata": {},
   "outputs": [],
   "source": [
    "[base] swoncat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b34d58ef",
   "metadata": {},
   "outputs": [],
   "source": [
    " inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc03dac4",
   "metadata": {},
   "source": [
    "This also permits a kind of parameterization.  E.g. let's say we wanted to use base 10 for our digits, maybe during debugging.  All that requires is to rebind the symbol `base` to 10."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d053ebb8",
   "metadata": {},
   "source": [
    "We could define a Boolean predicate that returns `true` for integers that are valid as digits and `false` otherwise:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "35476eac",
   "metadata": {},
   "outputs": [],
   "source": [
    "[valid_digit [0 >] [base <] &&] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "02a48806",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32 true 1232147483648 false"
     ]
    }
   ],
   "source": [
    "32 valid_digit 1232147483648 valid_digit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "03a8fe65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e9d9ec9",
   "metadata": {},
   "source": [
    "## Converting to and from Python Integers\n",
    "Because we are working with Python Joy right now we can convert ints to bigints and vice versa.  THis will be helpful to check our work.  Later we can deal with converting to and from strings (which this Joy doesn't have anyway, so it's probably fine to defer.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfed1f25",
   "metadata": {},
   "source": [
    "To get the sign bool we can just use `!-` (\"not negative\"), to get the list of digits we repeatedly `divmod` the number by our `base`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3fc98ccd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1797196498 5748904729"
     ]
    }
   ],
   "source": [
    "12345678901234567890 base divmod swap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b838c4cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1797196498 1453937433 2"
     ]
    }
   ],
   "source": [
    "base divmod swap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "42c9d92d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1797196498 1453937433 2 0"
     ]
    }
   ],
   "source": [
    "base divmod swap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73bfa634",
   "metadata": {},
   "source": [
    "We keep it up until we get to zero.  This suggests a `while` loop:\n",
    "\n",
    "    [0 >=] [base divmod swap] while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "faaac9d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1312754386 1501085485 57659106 105448366 58"
     ]
    }
   ],
   "source": [
    "clear 1234567890123456789012345678901234567890\n",
    "\n",
    "[0 >] [base divmod swap] while pop"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01fa7493",
   "metadata": {},
   "source": [
    "But we want these numbers in a list.  The naive way using `infra` generates them in the reverse order of what we would like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2a613f36",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[58 105448366 57659106 1501085485 1312754386]"
     ]
    }
   ],
   "source": [
    "clear [1234567890123456789012345678901234567890]\n",
    "\n",
    "[ [0 >] [base divmod swap] while pop ]\n",
    "\n",
    "infra"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0054d0f7",
   "metadata": {},
   "source": [
    "We could just reverse the list, but it's more efficient to build the result list in the order we want, LSB to MSB:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e97b149d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "clear 1234567890123456789012345678901234567890\n",
    "\n",
    "[0 <=]\n",
    "[pop []]\n",
    "[base divmod swap]\n",
    "[i cons]\n",
    "genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f7cf20d",
   "metadata": {},
   "source": [
    "#### Representing Zero\n",
    "\n",
    "This will return the empty list for zero:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "67bb934b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "0 [0 <=] [pop []] [base divmod swap] [i cons] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bb42882",
   "metadata": {},
   "source": [
    "I think this is better than returning `[0]` because that amounts to a single leading zero.\n",
    "\n",
    "    [bool]   is \"0\"\n",
    "    [bool 0] is \"00\"\n",
    "\n",
    "Eh?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f18b370",
   "metadata": {},
   "source": [
    "#### `digitalize`\n",
    "\n",
    "Let's `inscribe` this function under the name `digitalize`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "227271a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[digitalize [0 <=] [pop []] [base divmod swap] [i cons] genrec] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cfbb371",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b6f77ac6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "1234567890123456789012345678901234567890 digitalize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bf8ba9e",
   "metadata": {},
   "source": [
    "Putting it all together we have `!-` for the sign and `abs digitalize` for the digits, followed by `cons`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "246c2a58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "clear 1234567890123456789012345678901234567890\n",
    "\n",
    "[!-] [abs digitalize] cleave cons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a571e98a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c89e6054",
   "metadata": {},
   "source": [
    "#### `to-bigint`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5a72ce30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[to-bigint [!-] [abs digitalize] cleave cons] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1370319c",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "59514dcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "clear 1234567890123456789012345678901234567890 to-bigint"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5741b48c",
   "metadata": {},
   "source": [
    "With negative numbers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f3c19364",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[false 1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "clear -1234567890123456789012345678901234567890 to-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "998512c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b44d9d47",
   "metadata": {},
   "source": [
    "### Converting from bigint to Python ints\n",
    "\n",
    "To convert a bigint into a Python integer we need to keep a \"power\" parameter on the stack, setting it up and discarding it at the end, as well as an accumulator value starting at zero:\n",
    "\n",
    "    prep == rest 1 0 rolldown\n",
    "\n",
    "    [true 3 2 1] rest 1 0 rolldown\n",
    "    1 0 [3 2 1]\n",
    "\n",
    "We will deal with the sign bit later."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b7d0d1f",
   "metadata": {},
   "source": [
    "So the problem is to derive:\n",
    "\n",
    "       1 0 [digits...] [F] step\n",
    "    -------------------------\n",
    "           result\n",
    "\n",
    "Where `F` is:\n",
    "\n",
    "              power acc digit F\n",
    "    ---------------------------------------\n",
    "       (power*base) (acc + (power*digit)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fd5f38f",
   "metadata": {},
   "source": [
    "Now this is an interesting function.  The first thing I noticed is that it has two results that can be computed independently, suggesting a form like:\n",
    "\n",
    "    F == [G] [H] clop\n",
    "\n",
    "Then I noticed that `power *` is a sub-function of both `G` and `H`, but let's not overthink it, eh?\n",
    "\n",
    "    G == popop base *\n",
    "    H == rolldown * +\n",
    "\n",
    "    F == [G] [H] clop popdd\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bdd5ceab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      1 0 23 • popop base *\n",
      "           1 • base *\n",
      "           1 • 2147483648 *\n",
      "1 2147483648 • *\n",
      "1 2147483648 • mul\n",
      "  2147483648 • \n",
      "\n",
      "2147483648"
     ]
    }
   ],
   "source": [
    "clear 1 0 23 [popop base *] trace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "244aa4e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 0 23 • rolldown * +\n",
      "0 23 1 • * +\n",
      "0 23 1 • mul +\n",
      "  0 23 • +\n",
      "  0 23 • add\n",
      "    23 • \n",
      "\n",
      "23"
     ]
    }
   ],
   "source": [
    "clear 1 0 23 [rolldown * +] trace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b0b1afb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2147483648 23"
     ]
    }
   ],
   "source": [
    "clear 1 0 23\n",
    "\n",
    "[popop base *]\n",
    "[rolldown * +]\n",
    "clop\n",
    "popdd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "818f51bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7497680b",
   "metadata": {},
   "source": [
    "#### `prep` and `from-bigint'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d15747ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[prep rest 1 0 rolldown] inscribe\n",
    "[from-bigint' [F] step popd] inscribe\n",
    "[F [G] [H] clop popdd] inscribe\n",
    "[G popop base *] inscribe\n",
    "[H rolldown * +] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c77d58c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "1 0 [1312754386 1501085485 57659106 105448366 58]\n",
    "\n",
    "from-bigint'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b2b076c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "to-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "778ecfb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 0 [1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "prep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "682ed5e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "from-bigint'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5bb0973a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "to-bigint prep from-bigint'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd9f5fe1",
   "metadata": {},
   "source": [
    "What about that sign bit?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4736f46a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "neg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8615243e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "to-bigint prep from-bigint'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f85b4be1",
   "metadata": {},
   "source": [
    "That's no good, we lose the sign.  Time to deal with that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e2f8d6cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 1312754386 1501085485 57659106 105448366 58]"
     ]
    }
   ],
   "source": [
    "to-bigint"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1c3e7eb",
   "metadata": {},
   "source": [
    "We want to get the sign bit and the Python int,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e09c99c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true 1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "[first] [prep from-bigint'] cleave"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5236f004",
   "metadata": {},
   "source": [
    "and then use the sign bit to negate the Python int if the bigint was negative:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cc990e1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "swap [] [neg] branch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfc29643",
   "metadata": {},
   "source": [
    "This gives:\n",
    "\n",
    "    foo == [first] [prep from-bigint'] cleave\n",
    "    bar == swap [] [neg] branch\n",
    "    from-bigint == foo bar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "511b6bac",
   "metadata": {},
   "source": [
    "(I just realize that if you pre-swap the two quoted programs in ``foo`` then you can omit ``swap`` from ``bar``.)\n",
    "\n",
    "    foo == [prep from-bigint'] [first] cleave\n",
    "    bar == [] [neg] branch\n",
    "    from-bigint == foo bar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bffedec",
   "metadata": {},
   "source": [
    "#### `from-bigint`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "25591061",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[foo [prep from-bigint'] [first] cleave] inscribe\n",
    "[bar [neg] [] branch] inscribe\n",
    "[from-bigint foo bar] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b897a3bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "1234567890123456789012345678901234567890 to-bigint from-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d1efc1ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1234567890123456789012345678901234567890"
     ]
    }
   ],
   "source": [
    "neg to-bigint from-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1ad99efa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a592c8e",
   "metadata": {},
   "source": [
    "## Addition of Like Signs `add-digits`\n",
    "\n",
    "Let's figure out how to add two lists of digits (we will assume that the signs are the same.)  We need to put an inital `false` value for a carry flag, and then there's a `genrec`.\n",
    "\n",
    "    initial-carry == false rollup\n",
    "      add-digits' ≡ [P] [THEN] [R0] [R1] genrec\n",
    "      \n",
    "       add-digits ≡ initial-carry add-digits'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "632dc05c",
   "metadata": {},
   "source": [
    "### The predicate\n",
    "\n",
    "I think we're going to want a recursive function (duh!?) but it's not quite a standard *hylomorphism* for (at least) two reasons:\n",
    "\n",
    "- We're tearing down two lists simultaneously.\n",
    "- They might not be the same length.\n",
    "\n",
    "There are two base cases: two empty lists or one empty list, the recursive branch is taken only if both lists are non-empty.\n",
    "\n",
    "    bool [a ...] [b ...] P"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b0df08e",
   "metadata": {},
   "source": [
    "The first thing to do is convert them to Booleans.  Let's make a little truth table to work with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f33ca7b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[a] [b]] [[a] []] [[] [b]] [[] []]]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[\n",
    "[[a] [b]]\n",
    "[[a] []]\n",
    "[[] [b]]\n",
    "[[] []]\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d404aabf",
   "metadata": {},
   "source": [
    "Then we can `map` our predicate over this list to be sure ti does what we want:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b2d4f3b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[true true] [true false] [false true] [false false]]"
     ]
    }
   ],
   "source": [
    "[[[bool] ii] infra] map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00049aff",
   "metadata": {},
   "source": [
    "We want to *and* the bools and invert the result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d87fa971",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[false] [true] [true] [true]]"
     ]
    }
   ],
   "source": [
    "clear [[[a] [b]] [[a] []] [[] [b]] [[] []]] \n",
    "\n",
    "[[[bool] ii & not] infra] map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ce10b89",
   "metadata": {},
   "source": [
    "So the predicate function we want here is:\n",
    "\n",
    "    P == [bool] ii & not"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fc5095b",
   "metadata": {},
   "source": [
    "### The base cases\n",
    "On the non-recursive branch of the `genrec` we have to decide between three cases, but because addition is commutative we can lump together the first two:\n",
    "\n",
    "    bool [] [b ...] THEN\n",
    "    bool [a ...] [] THEN\n",
    "    \n",
    "    bool [] [] THEN\n",
    "\n",
    "So we have an `ifte` expression:\n",
    "\n",
    "    THEN ≡ [P'] [THEN'] [ELSE] ifte\n",
    "\n",
    "Let's define the predicate:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "491c0846",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[true] [true] [false]]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[\n",
    "[[a] []]\n",
    "[[] [b]]\n",
    "[[] []]\n",
    "]\n",
    "\n",
    "[[[bool] ii |] infra] map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f2bdc8d",
   "metadata": {},
   "source": [
    "    P' ≡ [bool] ii |\n",
    "\n",
    "So `THEN'` deals with one number (list of digits) being longer than the other, while the `ELSE` branch deals with the case of both lists being empty."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa51fd49",
   "metadata": {},
   "source": [
    "#### One list empty\n",
    "In the cases where one of the two lists (but not both) is empty:\n",
    "\n",
    "    carry [a ...] [] THEN'\n",
    "    carry [] [b ...] THEN'\n",
    "\n",
    "We first get rid of the empty list:\n",
    "\n",
    "    ditch-empty-list ≡ [bool] [popd] [pop] ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "bd51792f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[ditch-empty-list [bool] [popd] [pop] ifte] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71697765",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "fbf134e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]"
     ]
    }
   ],
   "source": [
    "[1][] ditch-empty-list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "90db3359",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[][1] ditch-empty-list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b624991",
   "metadata": {},
   "source": [
    "    THEN' ≡ ditch-empty-list THEN''\n",
    "\n",
    "Now we have:\n",
    "\n",
    "    carry [n ...] THEN''\n",
    "\n",
    "This is `add-carry-to-digits`..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4d9e00f",
   "metadata": {},
   "source": [
    "#### But first  `add-with-carry`\n",
    "We will want some function `F` that accepts a bool and two ints and leaves behind a new int and a new Boolean carry flag:\n",
    "\n",
    "            carry0 a b F\n",
    "    --------------------------\n",
    "         (a+b+carry0) carry\n",
    "\n",
    "(I find it interesting that this function accepts the carry from below the int args but returns it above the result.  Hmm...)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "223cee8e",
   "metadata": {},
   "source": [
    "#### `bool-to-int`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "4fce2757",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[bool-to-int [0] [1] branch] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f8a18ff",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "e86b7b90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1"
     ]
    }
   ],
   "source": [
    "false bool-to-int\n",
    "true bool-to-int"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "007ba148",
   "metadata": {},
   "source": [
    "We can use this function to convert the carry flag to an integer and then add it to the sum of the two digits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "cd70e18e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false 1 2 [bool-to-int] dipd + +"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a27ff9f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true 1 2 [bool-to-int] dipd + +"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89b971c9",
   "metadata": {},
   "source": [
    "So the first part of `F` is `[bool-to-int] dipd + +` to get the total, then we need to do\n",
    "\n",
    "`base mod` to get the new digit and `base >=` to get the new carry flag:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "74a59ab5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 false"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "4 base [mod] [>=] clop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "3a8f5078",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 true"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "base 100 +\n",
    "\n",
    "base [mod] [>=] clop"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b7eb5d7",
   "metadata": {},
   "source": [
    "Put it all together and we have:\n",
    "\n",
    "    _add-with-carry0 ≡ [bool-to-int] dipd + +\n",
    "    _add-with-carry1 ≡ base [mod] [>=] clop\n",
    "\n",
    "    add-with-carry ≡ _add-with-carry0 _add-with-carry1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2b3a39e",
   "metadata": {},
   "source": [
    "#### `add-with-carry`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c05bcd8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[_add-with-carry0 [bool-to-int] dipd + +] inscribe\n",
    "[_add-with-carry1 base [mod] [>=] clop] inscribe\n",
    "[add-with-carry _add-with-carry0 _add-with-carry1] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56369ede",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "03649114",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 true"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false base 100 add-with-carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "9dca0fbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "101 true"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true base 100 add-with-carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "c8f01d7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "102 false"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false 2 100 add-with-carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a1e77990",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "103 false"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true 2 100 add-with-carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b46a62ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44429ac5",
   "metadata": {},
   "source": [
    "### Now back to `add-carry-to-digits`\n",
    "\n",
    "This should be a very simple recursive function.\n",
    "\n",
    "    add-carry-to-digits ≡ [_actd_P] [_actd_THEN] [_actd_R0] [_actd_R1] genrec\n",
    "\n",
    "    carry [n ...] add-carry-to-digits\n",
    "    carry [n ...] [_actd_P] [_actd_THEN] [_actd_R0] [_actd_R1] genrec\n",
    "\n",
    "The predicate is the carry flag itself inverted (but when we recur we will need to check if the list is non-empty because it may eventually be empty):\n",
    "\n",
    "    _actd_P ≡ pop not\n",
    "\n",
    "The base case simply discards the carry flag:\n",
    "\n",
    "    _actd_THEN ≡ popd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6895e78b",
   "metadata": {},
   "source": [
    "That leaves the recursive branch:\n",
    "\n",
    "    true [n ...] R0 [add-carry-to-digits] R1\n",
    "\n",
    "-or-\n",
    "\n",
    "    true [] R0 [add-carry-to-digits] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2279fc8",
   "metadata": {},
   "source": [
    "We know that the Boolean value is `true`.  We also know that the list will be non-empty, but only on the first iteration of the `genrec`.  It may be that the list is empty on a later iteration.\n",
    "\n",
    "The `R0` function should check the list.\n",
    "\n",
    "    _actd_R0 ≡ [bool] [_actd_R0.then] [_actd_R0.else] ifte"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5df77def",
   "metadata": {},
   "source": [
    "If it's empty...  (omitting the \"\\_actd_\" prefix for clarity)\n",
    "\n",
    "       true [] R0.else [add-carry-to-digits] R1\n",
    "    ----------------------------------------------\n",
    "        1 false [] [add-carry-to-digits] i cons"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c49b3c6",
   "metadata": {},
   "source": [
    "Note that this implies:\n",
    "\n",
    "    R1 == i cons\n",
    "\n",
    "We have `1 false []` (rather than some other arrangement) to be compatible (same types and order) with the result of the other branch, which we now derive."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5898c6a",
   "metadata": {},
   "source": [
    "If the list of digits isn't empty...\n",
    "\n",
    "                true [a ...] R0.then [add-carry-to-digits] i cons\n",
    "    ----------------------------------------------------------------\n",
    "       true 0 a add-with-carry [...] [add-carry-to-digits] i cons\n",
    "    ----------------------------------------------------------------\n",
    "                   (a+1) carry [...] [add-carry-to-digits] i cons"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13513fab",
   "metadata": {},
   "source": [
    "There we go.\n",
    "\n",
    "    _actd_R0.else ≡ popd 1 false rolldown\n",
    "\n",
    "    _actd_R0.then ≡ 0 swap uncons [add-with-carry] dip\n",
    "\n",
    "    _actd_R1 ≡ i cons"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32967775",
   "metadata": {},
   "source": [
    "#### `add-carry-to-digits`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c8f1aa44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[add-carry-to-digits [_actd_P] [_actd_THEN] [_actd_R0] [_actd_R1] genrec] inscribe\n",
    "[_actd_P pop not] inscribe\n",
    "[_actd_THEN popd] inscribe\n",
    "[_actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte] inscribe\n",
    "[_actd_R0.else popd 1 false rolldown] inscribe\n",
    "[_actd_R0.then 0 swap uncons [add-with-carry] dip] inscribe\n",
    "[_actd_R1 i cons] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dccff48c",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "1fa115d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false [3 2 1] add-carry-to-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "79d9c526",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [] add-carry-to-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "bb030214",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [3 2 1] add-carry-to-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "5e0a05af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true [2147483647 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true base -- [2 1] cons "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "7c730d6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 3 1]"
     ]
    }
   ],
   "source": [
    "add-carry-to-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "33bc080f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf558731",
   "metadata": {},
   "source": [
    "So that handles the cases where one of the two lists (but not both) is empty.\n",
    "\n",
    "#### Both lists empty\n",
    "If both lists are empty we discard one list and check the carry to determine our result as decribed above:\n",
    "\n",
    "    ELSE == pop swap [] [1 swons] branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "1ac813da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[carry [] [1 swons] branch] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5f77e9c",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "5e5ddf74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [] [] pop swap carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "c57d1fb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false [] [] pop swap carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "59b1338c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7687e503",
   "metadata": {},
   "source": [
    "The story so far...\n",
    "\n",
    "    add-digits == initial-carry add-digits'\n",
    "    \n",
    "    add-digits' == [P] [THEN] [R0] [R1] genrec\n",
    "\n",
    "    initial-carry == false rollup\n",
    "\n",
    "    P == [bool] ii & not\n",
    "\n",
    "    THEN == [P'] [THEN'] [ELSE] ifte\n",
    "\n",
    "    P' == [bool] ii |\n",
    "\n",
    "    THEN' == ditch-empty-list add-carry-to-digits\n",
    "    \n",
    "    carry == [] [1 swons] branch\n",
    "\n",
    "    ELSE == pop swap carry\n",
    "\n",
    "We just need to knock out those recursive functions `R0` and `R1` and we're done."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa6dc5c6",
   "metadata": {},
   "source": [
    "### And recur...\n",
    "\n",
    "    bool [a ...] [b ...] R0 [add-digits'] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b2a650",
   "metadata": {},
   "source": [
    "First we will want to `uncons` the digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "78dec757",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false 1 4 [2 3] [5 6]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false [1 2 3] [4 5 6] [uncons] ii swapd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435069f2",
   "metadata": {},
   "source": [
    "#### `uncons-two`\n",
    "We could call this `uncons-two`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "5b4bd2fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 4 [2 3] [5 6]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[uncons-two [uncons] ii swapd] inscribe\n",
    "\n",
    "[1 2 3] [4 5 6] uncons-two"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "f284ea19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b49246f",
   "metadata": {},
   "source": [
    "    bool a b [...] [...] R0' [add-digits'] R1\n",
    "\n",
    "It's at this point that we'll want to employ the `add-with-carry` function:\n",
    "\n",
    "    bool a b [...] [...] [add-with-carry] dipd R0'' [add-digits'] R1\n",
    "\n",
    "    bool a b add-with-carry [...] [...] R0'' [add-digits'] R1\n",
    "\n",
    "    (a+b) bool [...] [...] R0'' [add-digits'] R1\n",
    "\n",
    "If we postulate a `cons` in our `R1` function...\n",
    "\n",
    "    (a+b) bool [...] [...] R0'' [add-digits'] i cons\n",
    "\n",
    "Then it seems like we're done?  `R0''` is nothing?\n",
    "\n",
    "    R0 ≡ uncons-two [add-with-carry] dipd\n",
    "    \n",
    "    R1 ≡ i cons\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25d9bf30",
   "metadata": {},
   "source": [
    "#### `add-digits`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "8adffacb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[add-digits initial-carry add-digits'] inscribe\n",
    "[add-digits' [P] [THEN] [R0] [R1] genrec] inscribe\n",
    "[initial-carry false rollup] inscribe\n",
    "[P [bool] ii & not] inscribe\n",
    "[THEN [P'] [THEN'] [ELSE] ifte] inscribe\n",
    "[P' [bool] ii |] inscribe\n",
    "[THEN' ditch-empty-list add-carry-to-digits] inscribe\n",
    "[ELSE pop swap [] [1 swons] branch] inscribe\n",
    "[R0 uncons-two [add-with-carry] dipd] inscribe\n",
    "[R1 i cons] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "5b2e5738",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 2 1] [1 1 1]"
     ]
    }
   ],
   "source": [
    "[3 2 1] [1 1 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "cd7ac0b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 3 2]"
     ]
    }
   ],
   "source": [
    "add-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "50f2e62c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 3 2] [2147483647]"
     ]
    }
   ],
   "source": [
    "base -- unit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "54ca630f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 2]"
     ]
    }
   ],
   "source": [
    "add-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "27bb4638",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 2] [2147483647 2147483647 2147483647]"
     ]
    }
   ],
   "source": [
    "base -- dup dup unit ccons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "4d2d446a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 2 1]"
     ]
    }
   ],
   "source": [
    "add-digits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0159b2e3",
   "metadata": {},
   "source": [
    "243 + 999 = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "c187ffff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1242"
     ]
    }
   ],
   "source": [
    "clear 243 999 +"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c142268c",
   "metadata": {},
   "source": [
    "## `add-bigints`\n",
    "\n",
    "There is one more thing we have to do to use this: we have to deal with the signs.\n",
    "\n",
    "    add-bigints \n",
    "    [[first] ii xor not]                 # are they the same sign?\n",
    "    [[uncons] dip rest add-digits cons]  # add the digits and set the sign.\n",
    "    [neg-bigint sub-bigints]             # adding unlikes is actually subtraction.\n",
    "    ifte\n",
    "\n",
    "But we haven't implemented `neg-bigint` or `sub-bigints` yet..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "f2987cd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 123] [true 456]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "123 to-bigint 456 to-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "393623c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "[first] ii xor not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "20ce6f9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 123] [true 456]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "123 to-bigint 456 to-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "b355996b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true [123] [true 456]"
     ]
    }
   ],
   "source": [
    "[uncons] dip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "3d4a27b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true [123] [456]"
     ]
    }
   ],
   "source": [
    "rest "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "ac5990b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true [579]"
     ]
    }
   ],
   "source": [
    "add-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "11b3384e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 579]"
     ]
    }
   ],
   "source": [
    " cons"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab08817b",
   "metadata": {},
   "source": [
    "#### `add-bigints`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "e75d711e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 579]"
     ]
    }
   ],
   "source": [
    "[same-sign [first] ii xor not] inscribe\n",
    "[add-like-bigints [uncons] dip rest add-digits cons] inscribe\n",
    "[add-bigints [same-sign] [add-like-bigints] [1 0 /] ifte] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "e69471ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 579] [true 2147483647]"
     ]
    }
   ],
   "source": [
    "base -- to-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "29963839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 578 1]"
     ]
    }
   ],
   "source": [
    "add-bigints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "dac1c858",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21102239",
   "metadata": {},
   "source": [
    "## Subtraction of Like Signs `sub-digits`\n",
    "\n",
    "Subtraction is similar to addition in that it's a simple recursive algorithm that works digit-by-digit.  It has the same four cases as well, we can reuse `P` and `P'`.\n",
    "\n",
    "    initial-carry == false rollup\n",
    "      sub-digits' == [P] [sub.THEN] [sub.R0] [sub.R1] genrec\n",
    "       sub-digits == initial-carry add-digits'\n",
    "         sub.THEN == [P'] [sub.THEN'] [sub.ELSE] ifte"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccfd7c6d",
   "metadata": {},
   "source": [
    "### Refactoring For The Win\n",
    "\n",
    "\n",
    "We noted above that the algorithm for subtraction is similar to that for addition.  Maybe we can reuse *more* than just `P` and `P'`?  In fact, I think we could refactor (prematurely, two cases is one too few) something like this?\n",
    "\n",
    "                 [sub.THEN'] [sub.ELSE] [sub.R0] [sub.R1] foo\n",
    "    ---------------------------------------------------------------------\n",
    "       [P] [[P'] [sub.THEN'] [sub.ELSE] ifte] [sub.R0] [sub.R1] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16e993be",
   "metadata": {},
   "source": [
    "or just\n",
    "\n",
    "                 [THEN] [ELSE] [R0] [R1] foo\n",
    "    ----------------------------------------------------\n",
    "       [P] [[P'] [THEN] [ELSE] ifte] [R0] [R1] genrec\n",
    "\n",
    "eh?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5008dc0",
   "metadata": {},
   "source": [
    "`foo` is something like:\n",
    "\n",
    "    F == [ifte] ccons [P'] swons\n",
    "    G == [F] dipdd\n",
    "\n",
    "    [THEN] [ELSE] [R0] [R1] [F] dipdd foo'\n",
    "    [THEN] [ELSE] F [R0] [R1] foo'\n",
    "    [THEN] [ELSE] [ifte] ccons [P'] swons [R0] [R1] foo'\n",
    "    [[THEN] [ELSE] ifte] [P'] swons [R0] [R1] foo'\n",
    "    [[P'] [THEN] [ELSE] ifte] [R0] [R1] foo'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db8ee8e6",
   "metadata": {},
   "source": [
    "That leaves `[P]`...\n",
    "\n",
    "    F == [ifte] ccons [P'] swons [P] swap\n",
    "    G == [F] dipdd\n",
    "\n",
    "    [THEN] [ELSE] [ifte] ccons [P'] swons [P] swap [R0] [R1] foo'\n",
    "    [[THEN] [ELSE] ifte] [P'] swons [P] swap [R0] [R1] foo'\n",
    "    [[P'] [THEN] [ELSE] ifte] [P] swap [R0] [R1] foo'\n",
    "    [P] [[P'] [THEN] [ELSE] ifte] [R0] [R1] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ef96e1b",
   "metadata": {},
   "source": [
    "Ergo:\n",
    "\n",
    "      F == [ifte] ccons [P'] swons [P] swap\n",
    "    foo == [F] dipdd genrec\n",
    "    combine-two-lists == [i cons] foo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30fa6616",
   "metadata": {},
   "source": [
    "-and-\n",
    "\n",
    "    add-digits' == [one-empty-list]\n",
    "                   [both-empty]\n",
    "                   [both-full]\n",
    "                   combine-two-lists\n",
    "\n",
    "    one-empty-list == ditch-empty-list add-carry-to-digits\n",
    "    both-empty == pop swap carry\n",
    "    both-full == uncons-two [add-with-carry] dipd\n",
    "\n",
    "This illustrates how refactoring creates denser yet more readable code."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "184f6b2e",
   "metadata": {},
   "source": [
    "But this doesn't go quite far enough, I think.\n",
    "\n",
    "    R0 == uncons-two [add-with-carry] dipd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7598e48c",
   "metadata": {},
   "source": [
    "I think `R0` will pretty much always do:\n",
    "\n",
    "    uncons-two [combine-two-values] dipd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b721354e",
   "metadata": {},
   "source": [
    "And so it should be refactored further to something like:\n",
    "\n",
    "             [F] R0\n",
    "    -------------------------\n",
    "       uncons-two [F] dipd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07804405",
   "metadata": {},
   "source": [
    "And then `add-digits'` becomes just:\n",
    "\n",
    "\n",
    "    add-digits' == [one-empty-list]\n",
    "                   [both-empty]\n",
    "                   [add-with-carry]\n",
    "                   combine-two-lists"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dfff37b",
   "metadata": {},
   "source": [
    "If we factor `ditch-empty-list` out of `one-empty-list`, and `pop` from `both-empty`:\n",
    "\n",
    "    add-digits' == [add-carry-to-digits]\n",
    "                   [swap carry]\n",
    "                   [add-with-carry]\n",
    "                   combine-two-lists\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa6e7586",
   "metadata": {},
   "source": [
    "Let's figure out the new form.\n",
    "\n",
    "\n",
    "       [ONE-EMPTY] [BOTH-EMPTY] [COMBINE-VALUES] foo\n",
    "    ---------------------------------------------------\n",
    "       [P]\n",
    "       [\n",
    "           [P']\n",
    "           [ditch-empty-list ONE-EMPTY]\n",
    "           [pop BOTH-EMPTY]\n",
    "           ifte\n",
    "       ]\n",
    "       [uncons-two [COMBINE-VALUES] dipd]\n",
    "       [i cons] genrec\n",
    "\n",
    "eh?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a5e4669",
   "metadata": {},
   "source": [
    "Let's not over think it.\n",
    "\n",
    "       [ONE-EMPTY] [ditch-empty-list] swoncat [BOTH-EMPTY] [pop] swoncat [COMBINE-VALUES] \n",
    "       [ditch-empty-list ONE-EMPTY] [pop BOTH-EMPTY] [COMBINE-VALUES]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d7aad63",
   "metadata": {},
   "source": [
    "With:\n",
    "\n",
    "       [C] [A] [B] sandwich\n",
    "    --------------------------\n",
    "            [A [C] B]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "55f64814",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[sandwich swap [cons] dip swoncat] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "04e2d0d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[B] [A] [C]"
     ]
    }
   ],
   "source": [
    "clear [B] [A] [C]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "a28d9323",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[A [B] C]"
     ]
    }
   ],
   "source": [
    "sandwich"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "004b7aca",
   "metadata": {},
   "source": [
    "So to get from \n",
    "\n",
    "    [A] [B] [C]\n",
    "\n",
    "to:\n",
    "\n",
    "    [ditch-empty-list A] [pop B] [uncons-two [C] dipd]\n",
    "\n",
    "we use:\n",
    "\n",
    "    [[[ditch-empty-list] swoncat] dip [pop] swoncat] dip [uncons-two] [dipd] sandwich\n",
    "\n",
    "It's gnarly, but simple:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "bc16275c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[_foo0.0 [[ditch-empty-list] swoncat] dip] inscribe\n",
    "[_foo0.1 [pop] swoncat] inscribe\n",
    "[_foo0.3 [_foo0.0 _foo0.1] dip] inscribe\n",
    "[_foo0.4 [uncons-two] [dipd] sandwich] inscribe\n",
    "[_foo0 _foo0.3 _foo0.4] inscribe\n",
    "\n",
    "[_foo1 [\n",
    "  [ifte] ccons\n",
    "  [P'] swons \n",
    "  [P] swap \n",
    "  ] dip\n",
    "] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "c517da7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ditch-empty-list A] [pop B] [uncons-two [C] dipd]"
     ]
    }
   ],
   "source": [
    "[A] [B] [C] _foo0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "3f482224",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[P] [[P'] [ditch-empty-list A] [pop B] ifte] [uncons-two [C] dipd]"
     ]
    }
   ],
   "source": [
    "_foo1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "4b90e664",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[P] [[P'] [ditch-empty-list add-carry-to-digits] [pop swap carry] ifte] [uncons-two [add-with-carry] dipd]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[add-carry-to-digits]\n",
    "[swap carry]\n",
    "[add-with-carry]\n",
    "_foo0 _foo1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "173bf5b7",
   "metadata": {},
   "source": [
    "Compare the above with what we wanted:\n",
    "\n",
    "    [P]\n",
    "    [\n",
    "       [P']\n",
    "       [ditch-empty-list ONE-EMPTY]\n",
    "       [pop BOTH-EMPTY]\n",
    "       ifte\n",
    "    ]\n",
    "    [uncons-two [COMBINE-VALUES] dipd]\n",
    "\n",
    "Allwe need to do is add:\n",
    "\n",
    "    [i cons] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "ff88e2c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false [3 2 1] [6 5 4] [P] [[P'] [ditch-empty-list add-carry-to-digits] [pop swap carry] ifte] [uncons-two [add-with-carry] dipd]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[3 2 1] [6 5 4] initial-carry\n",
    "\n",
    "[add-carry-to-digits]\n",
    "[swap carry]\n",
    "[add-with-carry]\n",
    "_foo0 _foo1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "941a4426",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 7 5]"
     ]
    }
   ],
   "source": [
    "[i cons] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "917600dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[build-two-list-combiner _foo0 _foo1 [i cons]] inscribe\n",
    "[combine-two-lists [add-carry-to-digits] [swap carry] [add-with-carry] build-two-list-combiner] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "21f6b0e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false [3 2 1] [6 5 4] [P] [[P'] [ditch-empty-list add-carry-to-digits] [pop swap carry] ifte] [uncons-two [add-with-carry] dipd] [i cons]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[3 2 1] [6 5 4] initial-carry\n",
    "combine-two-lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "4a8819fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 7 5]"
     ]
    }
   ],
   "source": [
    "genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "5d451c1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 7 5]"
     ]
    }
   ],
   "source": [
    "[base 10] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "28fefe8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 6 5 4 3 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "123456 to-bigint\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "fa37c40c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c45ce348",
   "metadata": {},
   "source": [
    "So that's nice.\n",
    "\n",
    "In order to avoid the overhead of rebuilding the whole thing each time we could pre-compute the function and store it in the dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "850a1421",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[P] [[P'] [ditch-empty-list add-carry-to-digits] [pop swap carry] ifte] [uncons-two [add-with-carry] dipd] [i cons]"
     ]
    }
   ],
   "source": [
    "[add-carry-to-digits]\n",
    "[swap carry]\n",
    "[add-with-carry]\n",
    "build-two-list-combiner"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "930acbc8",
   "metadata": {},
   "source": [
    "Now grab the definition, add the `genrec` and symbol (name) and inscribe it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "4b6005fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[add-digits' [P] [[P'] [ditch-empty-list add-carry-to-digits] [pop swap carry] ifte] [uncons-two [add-with-carry] dipd] [i cons] genrec]"
     ]
    }
   ],
   "source": [
    "[genrec] ccons ccons [add-digits'] swoncat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "c616033e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    " inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985e3f81",
   "metadata": {},
   "source": [
    "Try it out..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "1a18ddd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 5 3]"
     ]
    }
   ],
   "source": [
    "false [3 2 1] [4 3 2] add-digits'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "3c639487",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false [7 5 3] [9]"
     ]
    }
   ],
   "source": [
    "false swap base -- unit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "c9222cab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 6 3]"
     ]
    }
   ],
   "source": [
    "add-digits'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "f3bc8d28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82b7bf9a",
   "metadata": {},
   "source": [
    "#### Demonstrate `add-bigints`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "b4a71b70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 4 3 2 1] [true 9 9 9]"
     ]
    }
   ],
   "source": [
    "1234 999 [to-bigint] ii "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "6aba84ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 3 3 2 2]"
     ]
    }
   ],
   "source": [
    "add-bigints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "d5dc4dce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2233"
     ]
    }
   ],
   "source": [
    "from-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "2e9ab2c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2233 2233"
     ]
    }
   ],
   "source": [
    "1234 999 +"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "080d81a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13336d40",
   "metadata": {},
   "source": [
    "### Subtracting\n",
    "\n",
    "Okay, we're almost ready to implement subtraction, but there's a wrinkle!  When we subtract a smaller (absolute) value from a larger (absolute) value there's no problem:\n",
    "\n",
    "    10 - 5 = 5\n",
    "\n",
    "But I don't know the algorithm to subtract a larger number from a smaller one:\n",
    "\n",
    "    5 - 10 = ???\n",
    "\n",
    "The answer is -5, of course, but what's the algorithm?  How to make the computer figure that out?  We make use of the simple algebraic identity:\n",
    "\n",
    "    a - b = -(b - a)\n",
    "\n",
    "So if we want to subtract a larger number `a` from a smaller one `b` we can instead subtract the smaller from the larger and invert the sign:\n",
    "\n",
    "    5 - 10 = -(10 - 5)\n",
    "\n",
    "To do this we need a function `gt-digits` that will tell us which of two digit lists represents the larger integer."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "003c5dad",
   "metadata": {},
   "source": [
    "#### `gt-digits`\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec4ea273",
   "metadata": {},
   "source": [
    "I just realized I don't have a list length function yet!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "9c50bd24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[length [pop ++] step_zero] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "4c1dbabd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[] length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "ef683ade",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[this is a list] length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "ff52920a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3] [4 5] 3 2"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[1 2 3] [4 5] over over [length] app2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "be946f53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5] [1 2 3]"
     ]
    }
   ],
   "source": [
    "[swap][6][7]cmp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4bd496a",
   "metadata": {},
   "source": [
    "what about a function that iterates through two lists until one or the other ends, or they end at the same time (same length) and we walk back through comparing the digits?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "056e3072",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "02d4e38e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "[1 2 3] [4 5 6] [bool] ii &"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "5683eb84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4] [2 5] [3 6]]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[1 2 3] [4 5 6]\n",
    "[[bool] ii | not]\n",
    "[pop]\n",
    "[uncons-two]\n",
    "[i [unit cons] dip cons]\n",
    "genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "a4e1f728",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4] [2 5] [3 6]]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[1 2 3] [4 5 6]\n",
    "[[bool] ii | not]\n",
    "[pop]\n",
    "[uncons-two]\n",
    "[i [unit cons] dip cons]\n",
    "genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "dc7c151a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb580b17",
   "metadata": {},
   "source": [
    "So I guess that's `zip`?\n",
    "\n",
    "But we want something a little different.\n",
    "\n",
    "It's a weird function: compare lengths, if they are the same length then compare contents pairwise from the end.\n",
    "\n",
    "if the first list is empty and the second list isn't then the whole function should return false\n",
    "\n",
    "if the first list is non-empty and the second list is empty then the whole function should return true\n",
    "\n",
    "if both lists are non-empty we uncons some digits for later comparison?  Where to put them?  Leave them on the stack?  What about short-circuits?\n",
    "\n",
    "if both lists are empty we start comparing uncons'd pairs until we find an un-equal pair or run out of pairs.\n",
    "\n",
    "if we run out of pairs before we find an unequal pair then the function returns true (the numbers are identical, we should try to shortcut the actual subtraction here, but let's just get it working first, eh?)\n",
    "\n",
    "if we find an unequal pair we return a>b and discard the rest of the pairs.  Or maybe this all happens in some sort of `infra first` situation?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4c0376a",
   "metadata": {},
   "source": [
    "So the predicate will be `[bool] ii & not`, if one list is longer than the other we are done.\n",
    "We postulate a third list to contain the pairs:\n",
    "\n",
    "    [] [3 2 1] [4 5 6] [P] [BASE] [R0] [R1] genrec\n",
    "\n",
    "The recursive branch seems simpler to figure out:\n",
    "\n",
    "    [] [3 2 1] [4 5 6] R0 [F] R1\n",
    "\n",
    "    uncons-two [unit cons swons] dipd [F] i\n",
    "\n",
    "    [] [3 2 1] [4 5 6] [P] [BASE] [uncons-two [unit cons swons] dipd] tailrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "50c2669a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    " [xR1 uncons-two [unit cons swons] dipd] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "1bbf3465",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] [3 2 1] [4 5 6]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2 1] [4 5 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "5a9347f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 6] [2 5] [3 4]] [] []"
     ]
    }
   ],
   "source": [
    "xR1 xR1 xR1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "69b18847",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[xP [bool] ii & not] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "0b29370d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4] [3 5]] [1] []"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2 1] [5 4] [xP] [] [xR1] tailrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "32ce148e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 5] [3 4]] [] [1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2] [4 5 1] [xP] [] [xR1] tailrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "4efc8f76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 3] [2 4] [3 5]] [] []"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2 1] [5 4 3] [xP] [] [xR1] tailrec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7bcbb67",
   "metadata": {},
   "source": [
    "Now comes the tricky part, that base case:\n",
    "\n",
    "we have three lists.  The first is a possibly-empty list of pairs to compare.\n",
    "\n",
    "The second two are the tails of the original lists.\n",
    "\n",
    "If the top list is non-empty then the second list must be empty so the whole function should return true\n",
    "\n",
    "If the top list is empty and the second list isn't then the whole function should return false\n",
    "\n",
    "If both lists are empty we start comparing uncons'd pairs until we find an un-equal pair or run out of pairs.\n",
    "\n",
    "    [bool]  # if the first list is non-empty\n",
    "    [popop pop true]\n",
    "    [\n",
    "        [pop bool]  # the second list is non-empty (the first list is empty)\n",
    "        [popop pop false]\n",
    "        [\n",
    "            # both lists are empty\n",
    "            popop\n",
    "            compare-pairs\n",
    "        ]\n",
    "        ifte\n",
    "    ]\n",
    "    ifte\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "7d973f7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[][][1]\n",
    "\n",
    "[bool]\n",
    "[popop pop true]\n",
    "[\n",
    "    [pop bool]\n",
    "    [popop pop false]\n",
    "    [popop 23 swons]\n",
    "    ifte\n",
    "]\n",
    "ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "9239e0c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[][1][]\n",
    "\n",
    "[bool]\n",
    "[popop pop true]\n",
    "[\n",
    "    [pop bool]\n",
    "    [popop pop false]\n",
    "    [popop 23 swons]\n",
    "    ifte\n",
    "]\n",
    "ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "0c58a6e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[1][][]\n",
    "\n",
    "[bool]\n",
    "[popop pop true]\n",
    "[\n",
    "    [pop bool]\n",
    "    [popop pop false]\n",
    "    [popop 23 swons]\n",
    "    ifte\n",
    "]\n",
    "ifte"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97c4afb5",
   "metadata": {},
   "source": [
    "#### `compare-pairs`\n",
    "\n",
    "This should be a pretty simple recursive function\n",
    "\n",
    "    [P] [THEN] [R0] [R1] genrec\n",
    "\n",
    "If the list is empty we return `false`\n",
    "\n",
    "    P == bool not\n",
    "    THEN == pop false\n",
    "\n",
    "On the recursive branch we have an `ifte` expression:\n",
    "\n",
    "                pairs R0 [compare-pairs] R1\n",
    "    ---------------------------------------------------\n",
    "       pairs [P.rec] [THEN.rec] [compare-pairs] ifte\n",
    "\n",
    "We must compare the pair from the top of the list:\n",
    "\n",
    "    P.rec == first [>] infrst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "7f302157",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[[1 3] [2 4] [3 5]] first [>] infrst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "d25ef02e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true true true]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[[1 3] [2 4] [3 5]] [[>] infrst] map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6eb7683",
   "metadata": {},
   "source": [
    "    THEN.rec == pop true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "06e0ffcb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[compare-pairs\n",
    "  [bool not]\n",
    "  [pop false]\n",
    "  [\n",
    "    [first [>] infrst]\n",
    "    [pop true]\n",
    "  ]\n",
    "  [ifte]\n",
    "  genrec\n",
    "] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "b2428f49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 3] [2 4] [3 5]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "4ef5f503",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 3] [3 3] [3 5]] compare-pairs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d665b767",
   "metadata": {},
   "source": [
    "Whoops!  I forgot to remove the already-checked pair from the list of pairs!  (Later on I discover that the logic is inverted here: `>=` not `<` d'oh!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "7f060f83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[compare-pairs\n",
    "  [bool not]\n",
    "  [pop false]\n",
    "  [\n",
    "    [first [>=] infrst]\n",
    "    [pop true]\n",
    "  ]\n",
    "  [[rest] swoncat ifte]\n",
    "  genrec\n",
    "] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f80c5ba9",
   "metadata": {},
   "source": [
    "This is clunky and inefficient but it works."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "8d74a801",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 0] [2 2] [3 3]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "0104d2b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 1] [2 2] [3 3]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "4728d141",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 2] [2 2] [3 3]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "bfb5e62c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "7983881c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 1] [2 1] [3 3]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "677757c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 1] [2 2] [3 3]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "5691f813",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [[1 1] [2 3] [3 3]] compare-pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c1030f9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "b95fe984",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear\n",
    "[[1 1] [2 1] [3 3]] [] []\n",
    "\n",
    "[bool]\n",
    "[popop pop true]\n",
    "[\n",
    "    [pop bool]\n",
    "    [popop pop false]\n",
    "    [popop compare-pairs]\n",
    "    ifte\n",
    "]\n",
    "ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "b5ddac15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "[BASE\n",
    "  [bool]\n",
    "  [popop pop true]\n",
    "  [\n",
    "    [pop bool]\n",
    "    [popop pop false]\n",
    "    [popop compare-pairs]\n",
    "    ifte\n",
    "  ]\n",
    "  ifte\n",
    "] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "d4077376",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] [3 2 1] [4 5 6]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2 1] [4 5 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "ac840996",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "[xP] [BASE] [xR1] tailrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "e7c3b141",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] [4 5 6] [3 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2 1] [4 5 6] swap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "95c7308d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false"
     ]
    }
   ],
   "source": [
    "[xP] [BASE] [xR1] tailrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "5e43ba80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] [3 2 1] [3 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "[] [3 2 1] dup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "20070426",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "[xP] [BASE] [xR1] tailrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "c97b0df9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "980a1bbf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[gt-bigint <<{} [xP] [BASE] [xR1] tailrec] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "4f429421",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [3 2 1] [4 5 6] gt-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "5a6419ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false"
     ]
    }
   ],
   "source": [
    "clear [3 2 1] [4 5 6] swap gt-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "cd586291",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true"
     ]
    }
   ],
   "source": [
    "clear [3 2 1] dup gt-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3cb18d6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "35fb22d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5 6] [3 2 1]"
     ]
    }
   ],
   "source": [
    "clear [3 2 1] [4 5 6] [gt-bigint] [swap] [] ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "36d74e5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5 6] [3 2 1]"
     ]
    }
   ],
   "source": [
    "clear [4 5 6] [3 2 1] [gt-bigint] [swap] [] ifte"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1ebc16c",
   "metadata": {},
   "source": [
    "And so it goes.\n",
    "\n",
    "Now we can subtract, we just have to remember to invert the sign bit if we swap the digit lists.\n",
    "\n",
    "Maybe something like:\n",
    "\n",
    "    check-gt == [gt-bigint] [swap true rollup] [false rollup] ifte\n",
    "\n",
    "To keep the decision around as a Boolean flag?  We can `xor` it with the sign bit?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "dd645337",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[check-gt [gt-bigint] [swap [not] dipd] [] ifte] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "935552d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false [4 5 6] [3 2 1]"
     ]
    }
   ],
   "source": [
    "false [4 5 6] [3 2 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "05100765",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false [4 5 6] [3 2 1]"
     ]
    }
   ],
   "source": [
    "check-gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "7ac873d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ef0f655",
   "metadata": {},
   "source": [
    "### Subtraction, at last...\n",
    "\n",
    "So now that we can compare digit lists to see if one is larger than the other we can subtract (inverting the sign if necessary) much like we did addition:\n",
    "\n",
    "    sub-bigints == [same-sign] [sub-like-bigints] [1 0 /] ifte\n",
    "\n",
    "    sub-like-bigints == [uncons] dip rest   sub-digits cons\n",
    "                                          ^\n",
    "                                          |\n",
    "\n",
    "At this point we would have the sign bit then the two digit lists.\n",
    "\n",
    "    sign [c b a] [z y x]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "173aa582",
   "metadata": {},
   "source": [
    "We want to use `check-gt` here:\n",
    "\n",
    "    sign [c b a] [z y x] check-gt\n",
    "    sign swapped? [c b a] [z y x] check-gt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "242e863f",
   "metadata": {},
   "source": [
    "It seems we should just flip the sign bit if we swap, eh?\n",
    "\n",
    "    check-gt == [gt-bigint] [swap [not] dipd] [] ifte"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37ed39c6",
   "metadata": {},
   "source": [
    "Now we subtract the digits:\n",
    "\n",
    "    sign [c b a] [z y x] sub-digits cons"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97693e57",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    sub-like-bigints == [uncons] dip rest check-gt sub-digits cons\n",
    "\n",
    "    sub-digits == initial-carry sub-digits'\n",
    "\n",
    "    sub-digits' ==\n",
    "        [sub-carry-from-digits]\n",
    "        [swap sub-carry]\n",
    "        [sub-with-carry]\n",
    "        build-two-list-combiner\n",
    "        genrec\n",
    "\n",
    "We just need to define the pieces."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "844210e4",
   "metadata": {},
   "source": [
    "#### `sub-with-carry`\n",
    "\n",
    "We know we will never be subtracting a larger (absolute) number from a smaller (absolute) number (they might be equal) so the carry flag will never be true *at the end of a digit list subtraction.*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c10e256",
   "metadata": {},
   "source": [
    "       carry a b sub-with-carry\n",
    "    ------------------------------\n",
    "         (a-b-carry)  new-carry\n",
    "\n",
    "    _sub-with-carry0 ≡ [bool-to-int] dipd - -\n",
    "    _sub-with-carry1 ≡ [base + base mod] [0 <] clop\n",
    "\n",
    "    sub-with-carry ≡ _sub-with-carry0 _sub-with-carry1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "c360cc7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[_sub-with-carry0 rolldown bool-to-int [-] ii] inscribe\n",
    "[_sub-with-carry1 [base + base mod] [0 <] cleave] inscribe\n",
    "[sub-with-carry _sub-with-carry0 _sub-with-carry1] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "3a84adff",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false 3 9"
     ]
    }
   ],
   "source": [
    "clear false 3 base --"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "0dcdc968",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 true"
     ]
    }
   ],
   "source": [
    "sub-with-carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "2d60492d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0fbfaf",
   "metadata": {},
   "source": [
    "#### `sub-carry-from-digits`\n",
    "\n",
    "Should be easy to make modeled on `add-carry-to-digits`, another very simple recursive function.  The predicate, base case, and `R1` are the same:\n",
    "\n",
    "    carry [n ...] sub-carry-from-digits\n",
    "    carry [n ...] [pop not] [popd] [_scfd_R0] [i cons] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc4ff7c6",
   "metadata": {},
   "source": [
    "That leaves the recursive branch:\n",
    "\n",
    "    true [n ...] _scfd_R0 [sub-carry-from-digits] i cons\n",
    "\n",
    "-or-\n",
    "\n",
    "    true [] _scfd_R0 [sub-carry-from-digits] i cons\n",
    "\n",
    "**Except** that this should should never happen when subtracting, because we already made sure that we're only ever subtracting a number less than or equal to the, uh, number we are subtracting from (TODO rewrite this trainwreck of a sentence).\n",
    "\n",
    "             true [a ...] _scfd_R0 [sub-carry-from-digits] i cons\n",
    "    ----------------------------------------------------------------\n",
    "     true 0 a add-with-carry [...] [sub-carry-from-digits] i cons\n",
    "    ------------------------------------------------------------------\n",
    "                 (a+1) carry [...] [sub-carry-from-digits] i cons"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "984f1bf4",
   "metadata": {},
   "source": [
    "    \n",
    "    true [a ...] _scfd_R0\n",
    "    true [a ...] 0 swap uncons [sub-with-carry] dip\n",
    "    true 0 [a ...] uncons [sub-with-carry] dip\n",
    "    true 0 a [...] [sub-with-carry] dip\n",
    "    true 0 a sub-with-carry [...]\n",
    "\n",
    "    _scfd_R0 == 0 swap uncons [sub-with-carry] dip"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "845abba7",
   "metadata": {},
   "source": [
    "But there's a problem!  This winds up subtracting `a` from 0 rather than the other way around:\n",
    "\n",
    "    _scfd_R0 == uncons 0 swap [sub-with-carry] dip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "20c8ad40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[sub-carry-from-digits\n",
    "  [pop not]\n",
    "  [popd]\n",
    "  [_scfd_R0]\n",
    "  [i cons]\n",
    "  genrec\n",
    "] inscribe\n",
    "[_scfd_R0 uncons 0 swap [sub-with-carry] dip] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77e427f9",
   "metadata": {},
   "source": [
    "Try it out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "92201303",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "false [3 2 1] sub-carry-from-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "1a1d7cdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 0]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [0 1] sub-carry-from-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "39ce9c6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 2 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [3 2 1] sub-carry-from-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "a93e916e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 9 0]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [0 0 1] sub-carry-from-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "c2273ff3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c609313",
   "metadata": {},
   "source": [
    "But what about those leading zeroes?\n",
    "\n",
    "We could use a version of `cons` that refuses to put 0 onto an empty list?\n",
    "\n",
    "    cons-but-not-leading-zeroes == [[bool] ii | not] [popd] [cons] ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "d8c7f7a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[cons-but-not-leading-zeroes [[bool] ii | not] [popd] [cons] ifte] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "875aaafa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[sub-carry-from-digits\n",
    "  [pop not]\n",
    "  [popd]\n",
    "  [_scfd_R0]\n",
    "  [i cons-but-not-leading-zeroes]\n",
    "  genrec\n",
    "] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "bd2447b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[_scfd_R0 uncons 0 swap [sub-with-carry] dip] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "id": "ffaf1fcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 9]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "\n",
    "true [0 0 1] sub-carry-from-digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "id": "8a2b2165",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73180c15",
   "metadata": {},
   "source": [
    "#### `sub-carry`\n",
    "\n",
    "    sub-carry == pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "c22775e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[sub-like-bigints [uncons] dip rest check-gt sub-digits cons] inscribe\n",
    "[sub-digits initial-carry sub-digits'] inscribe\n",
    "[sub-digits'\n",
    "    [sub-carry-from-digits]\n",
    "    [swap pop]\n",
    "    [sub-with-carry]\n",
    "    build-two-list-combiner\n",
    "    genrec\n",
    "] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "ac670030",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true [3 2 1] [6 5 4]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "true [3 2 1] [6 5 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "c19ff340",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false false [6 5 4] [3 2 1]"
     ]
    }
   ],
   "source": [
    "check-gt initial-carry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "8fbba815",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "false [3 3 3]"
     ]
    }
   ],
   "source": [
    "sub-digits'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "3b05693f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 5 4 3 2 1] [true 9 0 1]"
     ]
    }
   ],
   "source": [
    "clear\n",
    "12345 to-bigint 109 to-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "fc69485b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[true 6 3 2 2 1]"
     ]
    }
   ],
   "source": [
    "sub-like-bigints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "c992d25a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12236"
     ]
    }
   ],
   "source": [
    "from-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "id": "90abab0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64b84d78",
   "metadata": {},
   "source": [
    "#### `neg-bigint`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "391d87fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "[neg-bigint [not] infra] inscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "a7cce3f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123"
     ]
    }
   ],
   "source": [
    "123 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "edb25ebd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-123"
     ]
    }
   ],
   "source": [
    "to-bigint neg-bigint from-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "06e2f309",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123"
     ]
    }
   ],
   "source": [
    "to-bigint neg-bigint from-bigint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "dd47bdae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": []
    }
   ],
   "source": [
    "clear\n",
    "[sub-bigints [same-sign] [sub-like-bigints] [neg-bigint add-like-bigints] ifte] inscribe\n",
    "[add-bigints [same-sign] [add-like-bigints] [neg-bigint sub-like-bigints] ifte] inscribe"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de5f0dd7",
   "metadata": {},
   "source": [
    "## Multiplication\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89d38381",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f2b707c1",
   "metadata": {},
   "source": [
    "## Appendix: Source Code\n",
    "    clear\n",
    "    [base 2147483648]\n",
    "    [ditch-empty-list [bool] [popd] [pop] ifte]\n",
    "    [bool-to-int [0] [1] branch]\n",
    "    [uncons-two [uncons] ii swapd]\n",
    "    [sandwich swap [cons] dip swoncat]\n",
    "\n",
    "    [digitalize [0 <=] [pop []] [base divmod swap] [i cons] genrec]\n",
    "    [to-bigint [!-] [abs digitalize] cleave cons]\n",
    "\n",
    "    [prep rest 1 0 rolldown]\n",
    "    [from-bigint' [next-digit] step popd]\n",
    "    [next-digit [increase-power] [accumulate-digit] clop popdd]\n",
    "    [increase-power popop base *]\n",
    "    [accumulate-digit rolldown * +]\n",
    "\n",
    "    [sign-int [first] [prep from-bigint'] cleave]\n",
    "    [neg-if-necessary swap [neg] [] branch]\n",
    "    [from-bigint sign-int neg-if-necessary]\n",
    "\n",
    "    [add-with-carry _add-with-carry0 _add-with-carry1]\n",
    "    [_add-with-carry0 [bool-to-int] dipd + +]\n",
    "    [_add-with-carry1 base [mod] [>=] clop]\n",
    "\n",
    "    [add-carry-to-digits [pop not] [popd] [_actd_R0] [i cons] genrec]\n",
    "    [_actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte]\n",
    "    [_actd_R0.else popd 1 false rolldown]\n",
    "    [_actd_R0.then 0 swap uncons [add-with-carry] dip]\n",
    "\n",
    "    [add-digits initial-carry add-digits']\n",
    "    [initial-carry false rollup]\n",
    "\n",
    "    [add-digits' [P] [THEN] [R0] [R1] genrec]\n",
    "    [P [bool] ii & not]\n",
    "    [THEN [P'] [THEN'] [ELSE] ifte]\n",
    "    [R0 uncons-two [add-with-carry] dipd]\n",
    "    [R1 i cons]\n",
    "    [P' [bool] ii |]\n",
    "    [THEN' ditch-empty-list add-carry-to-digits]\n",
    "    [ELSE pop swap [] [1 swons] branch]\n",
    "\n",
    "    [same-sign [first] ii xor not]\n",
    "    [add-like-bigints [uncons] dip rest add-digits cons]\n",
    "    [add-bigints [same-sign] [add-like-bigints] [neg-bigint sub-like-bigints] ifte]\n",
    "\n",
    "    [build-two-list-combiner _btlc0 _btlc1 [i cons]]\n",
    "    [_btlc0.0 [[ditch-empty-list] swoncat] dip]\n",
    "    [_btlc0.1 [pop] swoncat]\n",
    "    [_btlc0.3 [_btlc0.0 _btlc0.1] dip]\n",
    "    [_btlc0.4 [uncons-two] [dipd] sandwich]\n",
    "    [_btlc0 _btlc0.3 _btlc0.4]\n",
    "    [_btlc1 [[ifte] ccons [P'] swons [P] swap] dip]\n",
    "\n",
    "    [carry [] [1 swons] branch]\n",
    "\n",
    "    [compare-pairs [bool not] [pop false] [[first [>=] infrst] [pop true]] [[rest] swoncat ifte] genrec]\n",
    "    [xR1 uncons-two [unit cons swons] dipd]\n",
    "    [xP [bool] ii & not]\n",
    "    [BASE [bool] [popop pop true] [[pop bool] [popop pop false] [popop compare-pairs] ifte] ifte]\n",
    "    [gt-bigint <<{} [xP] [BASE] [xR1] tailrec]\n",
    "    [check-gt [gt-bigint] [swap [not] dipd] [] ifte]\n",
    "\n",
    "    [sub-carry pop]\n",
    "\n",
    "    [sub-carry-from-digits [pop not] [popd] [_scfd_R0] [i cons-but-not-leading-zeroes] genrec] inscribe\n",
    "    [_scfd_R0 uncons 0 swap [sub-with-carry] dip] inscribe\n",
    "    [cons-but-not-leading-zeroes [P'] [cons] [popd] ifte]\n",
    "\n",
    "    [sub-with-carry _sub-with-carry0 _sub-with-carry1]\n",
    "    [_sub-with-carry0 rolldown bool-to-int [-] ii]\n",
    "    [_sub-with-carry1 [base + base mod] [0 <] cleave]\n",
    "\n",
    "    [sub-like-bigints [uncons] dip rest check-gt sub-digits cons]\n",
    "    [sub-digits initial-carry sub-digits']\n",
    "\n",
    "    enstacken [inscribe] step\n",
    "\n",
    "    [add-carry-to-digits]\n",
    "    [swap carry]\n",
    "    [add-with-carry]\n",
    "    build-two-list-combiner\n",
    "    [genrec] ccons ccons\n",
    "    [add-digits'] swoncat\n",
    "    inscribe\n",
    "\n",
    "    [sub-carry-from-digits]\n",
    "    [swap sub-carry]\n",
    "    [sub-with-carry]\n",
    "    build-two-list-combiner\n",
    "    [genrec] ccons ccons\n",
    "    [sub-digits'] swoncat\n",
    "    inscribe\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55944c2a",
   "metadata": {},
   "source": [
    "### notes\n",
    "\n",
    "So far I have three formats for Joy source:\n",
    "\n",
    "- `def.txt` is a list of definitions (UTF-8), one per line, with no special marks.\n",
    "- `foo ≡ bar baz...` lines in the `joy.py` embedded definition text, because why not?  (Sometimes I use `==` instead of `≡` mostly because some tools can't handle the Unicode glyph.  Like converting this notebook to PDF via LaTeX just omitted them.)\n",
    "- `[name body] inscribe` Joy source code that literally defines new words in the dictionary at runtime.  A text of those commands can be fed to the interpreter to customize it without any special processing (like the other two formats require.)\n",
    "\n",
    "So far I prefer the `def.txt` style but that makes it tricky to embed them automatically into the `joy.py` file.\n",
    "\n",
    "#### Refactoring\n",
    "\n",
    "We have `i cons` but that's pretty tight already, eh?\n",
    "\n",
    "However, `[i cons] genrec` is an interesting combinator.  It's almost `tailrec` with that `i` combinator for the recursion, but then `cons` means it's a list-builder (an *anamorphism* if you go for that sort of thing.)\n",
    "\n",
    "    simple-list-builder == [i cons] genrec\n",
    "\n",
    "And maybe:\n",
    "\n",
    "    boolii == [bool] ii\n",
    "\n",
    "       both? == boolii &\n",
    "     one-of? == boolii |\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Joypy",
   "language": "",
   "name": "thun"
  },
  "language_info": {
   "file_extension": ".joy",
   "mimetype": "text/plain",
   "name": "Joy"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
