diff --git a/docs/notebooks/BigInts.ipynb b/docs/notebooks/BigInts.ipynb index 7633eb9..c353e3a 100644 --- a/docs/notebooks/BigInts.ipynb +++ b/docs/notebooks/BigInts.ipynb @@ -2,16 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 51, + "execution_count": 1, "id": "73967878", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", - "text": [ - "[0 3 1]" - ] + "text": [] } ], "source": [ @@ -21,16 +19,14 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 2, "id": "b34d58ef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", - "text": [ - "[0 3 1]" - ] + "text": [] } ], "source": [ @@ -49,36 +45,16 @@ "This lets us use (Oberon RISC) 32-bit signed ints to store our digits." ] }, - { - "cell_type": "markdown", - "id": "3f77ce91", - "metadata": {}, - "source": [ - "## Take Two\n", - "\n", - "This really got away from me the first time. I think the big mistake ai made was including the middle branch in the recursive branch.\n" - ] - }, { "cell_type": "code", - "execution_count": null, - "id": "b2845a8d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 53, + "execution_count": 3, "id": "35476eac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", - "text": [ - "[0 3 1]" - ] + "text": [] } ], "source": [ @@ -87,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 4, "id": "02a48806", "metadata": {}, "outputs": [ @@ -106,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 5, "id": "1d276daf", "metadata": {}, "outputs": [ @@ -126,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 6, "id": "2454e662", "metadata": {}, "outputs": [ @@ -155,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 7, "id": "8a60b54b", "metadata": {}, "outputs": [ @@ -195,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 8, "id": "3fc98ccd", "metadata": {}, "outputs": [ @@ -215,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 9, "id": "b838c4cb", "metadata": {}, "outputs": [ @@ -241,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 10, "id": "faaac9d6", "metadata": {}, "outputs": [ @@ -262,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 11, "id": "2a613f36", "metadata": {}, "outputs": [ @@ -284,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 12, "id": "e97b149d", "metadata": {}, "outputs": [ @@ -305,7 +281,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 13, "id": "de4fe588", "metadata": {}, "outputs": [ @@ -323,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 14, "id": "67bb934b", "metadata": {}, "outputs": [ @@ -343,8 +319,8 @@ }, { "cell_type": "code", - "execution_count": 65, - "id": "3d7ef5a7", + "execution_count": 15, + "id": "227271a9", "metadata": {}, "outputs": [ { @@ -361,7 +337,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 16, "id": "b6f77ac6", "metadata": {}, "outputs": [ @@ -389,7 +365,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 17, "id": "246c2a58", "metadata": {}, "outputs": [ @@ -409,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 18, "id": "5a72ce30", "metadata": {}, "outputs": [ @@ -427,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 19, "id": "59514dcd", "metadata": {}, "outputs": [ @@ -447,7 +423,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 20, "id": "f3c19364", "metadata": {}, "outputs": [ @@ -467,8 +443,8 @@ }, { "cell_type": "code", - "execution_count": 71, - "id": "00daba0b", + "execution_count": 21, + "id": "998512c6", "metadata": {}, "outputs": [ { @@ -483,7 +459,7 @@ }, { "cell_type": "markdown", - "id": "8157b5e7", + "id": "0a592c8e", "metadata": {}, "source": [ "## Addition of Like Signs `add-digits`\n", @@ -498,7 +474,7 @@ }, { "cell_type": "markdown", - "id": "fc9b97e4", + "id": "7b0df08e", "metadata": {}, "source": [ "### The predicate\n", @@ -517,8 +493,8 @@ }, { "cell_type": "code", - "execution_count": 72, - "id": "77eea97f", + "execution_count": 22, + "id": "b2d4f3b2", "metadata": {}, "outputs": [ { @@ -543,8 +519,8 @@ }, { "cell_type": "code", - "execution_count": 73, - "id": "b3d917b2", + "execution_count": 23, + "id": "d87fa971", "metadata": {}, "outputs": [ { @@ -569,7 +545,7 @@ }, { "cell_type": "markdown", - "id": "c37e6dc8", + "id": "6ce10b89", "metadata": {}, "source": [ " P == [bool] ii & not" @@ -577,7 +553,7 @@ }, { "cell_type": "markdown", - "id": "2f69bbd1", + "id": "3fc5095b", "metadata": {}, "source": [ "### The base cases\n", @@ -592,8 +568,8 @@ }, { "cell_type": "code", - "execution_count": 74, - "id": "4a36d545", + "execution_count": 24, + "id": "491c0846", "metadata": {}, "outputs": [ { @@ -617,7 +593,7 @@ }, { "cell_type": "markdown", - "id": "518f5c0d", + "id": "9f2bdc8d", "metadata": {}, "source": [ " P' ≡ [bool] ii |\n", @@ -627,7 +603,7 @@ }, { "cell_type": "markdown", - "id": "87db88e8", + "id": "fa51fd49", "metadata": {}, "source": [ "#### One list empty\n", @@ -643,8 +619,8 @@ }, { "cell_type": "code", - "execution_count": 75, - "id": "3af0bfc7", + "execution_count": 25, + "id": "bd51792f", "metadata": {}, "outputs": [ { @@ -660,8 +636,8 @@ }, { "cell_type": "code", - "execution_count": 76, - "id": "85030079", + "execution_count": 26, + "id": "fbf134e2", "metadata": {}, "outputs": [ { @@ -678,8 +654,8 @@ }, { "cell_type": "code", - "execution_count": 77, - "id": "e4aafcaa", + "execution_count": 27, + "id": "90db3359", "metadata": {}, "outputs": [ { @@ -697,7 +673,7 @@ }, { "cell_type": "markdown", - "id": "76a60ef3", + "id": "3b624991", "metadata": {}, "source": [ " THEN' ≡ ditch-empty-list THEN''\n", @@ -711,7 +687,7 @@ }, { "cell_type": "markdown", - "id": "b84cccef", + "id": "c4d9e00f", "metadata": {}, "source": [ "#### But first `add-with-carry`\n", @@ -726,8 +702,8 @@ }, { "cell_type": "code", - "execution_count": 78, - "id": "3333e900", + "execution_count": 28, + "id": "e86b7b90", "metadata": {}, "outputs": [ { @@ -749,8 +725,8 @@ }, { "cell_type": "code", - "execution_count": 79, - "id": "22a7c867", + "execution_count": 29, + "id": "cd70e18e", "metadata": {}, "outputs": [ { @@ -769,8 +745,8 @@ }, { "cell_type": "code", - "execution_count": 80, - "id": "75713ea2", + "execution_count": 30, + "id": "a27ff9f0", "metadata": {}, "outputs": [ { @@ -789,7 +765,7 @@ }, { "cell_type": "markdown", - "id": "a9417aac", + "id": "89b971c9", "metadata": {}, "source": [ "So the first part of `F` is `[bool_to_int] dipd + +` to get the total, then we need to" @@ -797,8 +773,8 @@ }, { "cell_type": "code", - "execution_count": 81, - "id": "b4f1000f", + "execution_count": 31, + "id": "74a59ab5", "metadata": {}, "outputs": [ { @@ -817,8 +793,8 @@ }, { "cell_type": "code", - "execution_count": 82, - "id": "ffbdf325", + "execution_count": 32, + "id": "3a8f5078", "metadata": {}, "outputs": [ { @@ -839,7 +815,7 @@ }, { "cell_type": "markdown", - "id": "f09689c3", + "id": "2b7eb5d7", "metadata": {}, "source": [ "Put it all together and we have:\n", @@ -852,8 +828,8 @@ }, { "cell_type": "code", - "execution_count": 83, - "id": "b8161efb", + "execution_count": 33, + "id": "c05bcd8e", "metadata": {}, "outputs": [ { @@ -871,8 +847,8 @@ }, { "cell_type": "code", - "execution_count": 84, - "id": "0f082af7", + "execution_count": 34, + "id": "03649114", "metadata": {}, "outputs": [ { @@ -891,8 +867,8 @@ }, { "cell_type": "code", - "execution_count": 85, - "id": "a0d656bf", + "execution_count": 35, + "id": "9dca0fbc", "metadata": {}, "outputs": [ { @@ -911,8 +887,8 @@ }, { "cell_type": "code", - "execution_count": 86, - "id": "fb7065be", + "execution_count": 36, + "id": "c8f01d7d", "metadata": {}, "outputs": [ { @@ -931,8 +907,8 @@ }, { "cell_type": "code", - "execution_count": 87, - "id": "9b91363f", + "execution_count": 37, + "id": "a1e77990", "metadata": {}, "outputs": [ { @@ -951,8 +927,8 @@ }, { "cell_type": "code", - "execution_count": 89, - "id": "5596f574", + "execution_count": 38, + "id": "b46a62ba", "metadata": {}, "outputs": [ { @@ -967,7 +943,7 @@ }, { "cell_type": "markdown", - "id": "861e6896", + "id": "44429ac5", "metadata": {}, "source": [ "### Now back to `add-carry-to-digits`\n", @@ -990,7 +966,7 @@ }, { "cell_type": "markdown", - "id": "26ba75fb", + "id": "6895e78b", "metadata": {}, "source": [ "That leaves the recursive branch:\n", @@ -1004,7 +980,7 @@ }, { "cell_type": "markdown", - "id": "4d66332b", + "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", @@ -1016,7 +992,7 @@ }, { "cell_type": "markdown", - "id": "4ed0fd33", + "id": "379d88db", "metadata": {}, "source": [ "If it's empty...\n", @@ -1030,7 +1006,7 @@ }, { "cell_type": "markdown", - "id": "e82f8749", + "id": "9f1e5b30", "metadata": {}, "source": [ "If it's not empty...\n", @@ -1044,7 +1020,7 @@ }, { "cell_type": "markdown", - "id": "07f1bc97", + "id": "d5898c6a", "metadata": {}, "source": [ "Ah ha!? How to get that `(a+1)` back into the list!?\n", @@ -1066,7 +1042,7 @@ }, { "cell_type": "markdown", - "id": "9bfc7127", + "id": "13513fab", "metadata": {}, "source": [ "There we go.\n", @@ -1080,8 +1056,8 @@ }, { "cell_type": "code", - "execution_count": 90, - "id": "6bd105a9", + "execution_count": 39, + "id": "c8f1aa44", "metadata": {}, "outputs": [ { @@ -1102,8 +1078,8 @@ }, { "cell_type": "code", - "execution_count": 91, - "id": "39244b26", + "execution_count": 40, + "id": "1fa115d9", "metadata": {}, "outputs": [ { @@ -1122,8 +1098,8 @@ }, { "cell_type": "code", - "execution_count": 92, - "id": "0a921fd7", + "execution_count": 41, + "id": "79d9c526", "metadata": {}, "outputs": [ { @@ -1142,8 +1118,8 @@ }, { "cell_type": "code", - "execution_count": 93, - "id": "94359309", + "execution_count": 42, + "id": "bb030214", "metadata": {}, "outputs": [ { @@ -1162,8 +1138,8 @@ }, { "cell_type": "code", - "execution_count": 94, - "id": "d5d4de95", + "execution_count": 43, + "id": "5e0a05af", "metadata": {}, "outputs": [ { @@ -1182,8 +1158,8 @@ }, { "cell_type": "code", - "execution_count": 95, - "id": "a34b4771", + "execution_count": 44, + "id": "7c730d6d", "metadata": {}, "outputs": [ { @@ -1200,7 +1176,7 @@ }, { "cell_type": "markdown", - "id": "181cdfd4", + "id": "cf558731", "metadata": {}, "source": [ "So that handles the cases where one of the two lists (but not both) is empty.\n", @@ -1213,7 +1189,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 45, "id": "5e5ddf74", "metadata": {}, "outputs": [ @@ -1233,7 +1209,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 46, "id": "c57d1fb2", "metadata": {}, "outputs": [ @@ -1253,7 +1229,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 47, "id": "59b1338c", "metadata": {}, "outputs": [ @@ -1269,7 +1245,7 @@ }, { "cell_type": "markdown", - "id": "c4454f63", + "id": "7687e503", "metadata": {}, "source": [ "The story so far...\n", @@ -1295,7 +1271,7 @@ }, { "cell_type": "markdown", - "id": "54632eea", + "id": "fa6dc5c6", "metadata": {}, "source": [ "### And recur...\n", @@ -1305,7 +1281,7 @@ }, { "cell_type": "markdown", - "id": "995bd8d3", + "id": "53b2a650", "metadata": {}, "source": [ "First we will want to `uncons` the digits" @@ -1313,8 +1289,8 @@ }, { "cell_type": "code", - "execution_count": 99, - "id": "02c19af8", + "execution_count": 48, + "id": "78dec757", "metadata": {}, "outputs": [ { @@ -1333,7 +1309,7 @@ }, { "cell_type": "markdown", - "id": "9557420b", + "id": "435069f2", "metadata": {}, "source": [ "#### `uncons-two`\n", @@ -1342,8 +1318,8 @@ }, { "cell_type": "code", - "execution_count": 100, - "id": "5d671aa4", + "execution_count": 49, + "id": "5b4bd2fe", "metadata": {}, "outputs": [ { @@ -1364,8 +1340,8 @@ }, { "cell_type": "code", - "execution_count": 102, - "id": "80eab7d7", + "execution_count": 50, + "id": "f284ea19", "metadata": {}, "outputs": [ { @@ -1380,7 +1356,7 @@ }, { "cell_type": "markdown", - "id": "a47c67cf", + "id": "2b49246f", "metadata": {}, "source": [ " bool a b [...] [...] R0' [add-digits'] R1\n", @@ -1406,8 +1382,8 @@ }, { "cell_type": "code", - "execution_count": 103, - "id": "a16c95cf", + "execution_count": 51, + "id": "8adffacb", "metadata": {}, "outputs": [ { @@ -1426,13 +1402,13 @@ "[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\n" + "[R1 i cons] inscribe" ] }, { "cell_type": "code", - "execution_count": 104, - "id": "66176a8f", + "execution_count": 52, + "id": "5b2e5738", "metadata": {}, "outputs": [ { @@ -1449,8 +1425,8 @@ }, { "cell_type": "code", - "execution_count": 105, - "id": "e0272194", + "execution_count": 53, + "id": "cd7ac0b9", "metadata": {}, "outputs": [ { @@ -1467,8 +1443,8 @@ }, { "cell_type": "code", - "execution_count": 106, - "id": "bec3b32d", + "execution_count": 54, + "id": "50f2e62c", "metadata": {}, "outputs": [ { @@ -1485,8 +1461,8 @@ }, { "cell_type": "code", - "execution_count": 107, - "id": "6d8b0054", + "execution_count": 55, + "id": "54ca630f", "metadata": {}, "outputs": [ { @@ -1503,8 +1479,8 @@ }, { "cell_type": "code", - "execution_count": 108, - "id": "c842f875", + "execution_count": 56, + "id": "27bb4638", "metadata": {}, "outputs": [ { @@ -1521,8 +1497,8 @@ }, { "cell_type": "code", - "execution_count": 109, - "id": "80a240b0", + "execution_count": 57, + "id": "4d2d446a", "metadata": {}, "outputs": [ { @@ -1539,7 +1515,7 @@ }, { "cell_type": "markdown", - "id": "558e0a6d", + "id": "0159b2e3", "metadata": {}, "source": [ "243 + 999 = " @@ -1547,8 +1523,8 @@ }, { "cell_type": "code", - "execution_count": 110, - "id": "31aa6611", + "execution_count": 58, + "id": "c187ffff", "metadata": {}, "outputs": [ { @@ -1565,1805 +1541,11 @@ }, { "cell_type": "markdown", - "id": "db2de9c5", + "id": "c142268c", "metadata": {}, "source": [ "Bitchin'" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a46d9942", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4fe39366", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ef9db281", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "834ee60c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "20a2809e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b74e8780", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b2765992", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c96c51a5", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "50665e75", - "metadata": {}, - "source": [ - "### Predicate\n", - "so the predicate for the whole thing would be" - ] - }, - { - "cell_type": "code", - "execution_count": 121, - "id": "187aa6ec", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear\n", - "\n", - "[_add_p [bool not] ii &] inscribe" - ] - }, - { - "cell_type": "code", - "execution_count": 123, - "id": "ee3d2316", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "true" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[][]_add_p" - ] - }, - { - "cell_type": "code", - "execution_count": 124, - "id": "7e8c4942", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[1][]_add_p" - ] - }, - { - "cell_type": "code", - "execution_count": 125, - "id": "8b41766c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[][1]_add_p" - ] - }, - { - "cell_type": "code", - "execution_count": 126, - "id": "1bacc617", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[1][1]_add_p" - ] - }, - { - "cell_type": "markdown", - "id": "978b5cac", - "metadata": {}, - "source": [ - "### Recursive Branch\n", - "So (ignoring the source of the initial carry bit for now) we have:\n", - "\n", - " _add_p == [bool not] ii &\n", - " _add_then == pop swap [] [1 swons] branch\n", - "\n", - " add_digits == [_add_p] [_add_then] [R0] [R1] genrec\n", - "\n", - "What should the recursive branch do?\n", - "\n", - "It will be some `ifte` expression:\n", - "\n", - " R0 [add_digits] R1 == [P] [THEN] [ELSE] ifte\n", - "\n", - "Ideally, we can arrange to neatly bracket the `[add_digits]` quote, but otherwise we can build up the `ifte` quotes in the usual way with `cons` and `concat`." - ] - }, - { - "cell_type": "markdown", - "id": "7246fe3d", - "metadata": {}, - "source": [ - "#### Is One List Empty?\n", - "The context will be a Boolean carry bit and two lists, one of which (but not both!) may be empty.\n", - "\n", - " carry [a ...] [b ...] R0 [add_digits] R1\n", - "\n", - " carry [] [b ...] R0 [add_digits] R1\n", - "\n", - " carry [a ...] [] R0 [add_digits] R1\n", - "\n", - "So I think the first thing to do is detect and handle the (possibly) empty lists?" - ] - }, - { - "cell_type": "markdown", - "id": "d54e0860", - "metadata": {}, - "source": [ - "#### predicate" - ] - }, - { - "cell_type": "code", - "execution_count": 92, - "id": "728f7b72", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "true" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[1] [1] [bool] ii &" - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "id": "3561502a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[1] [] [bool] ii &" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "id": "2c395fcc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false" - ] - } - ], - "source": [ - "clear\n", - "\n", - "[] [1] [bool] ii &" - ] - }, - { - "cell_type": "markdown", - "id": "13993d1f", - "metadata": {}, - "source": [ - "So our `ifte` predicate is `[bool] ii &`, what's the rest look like?\n", - "\n", - " [[bool] ii &] ... [add_digits] ... ifte\n", - "\n", - "We will need `add_digits` on the `true` branch to deal with the rest of the lists, but not (I think) on the false branch?\n", - "\n", - " [[bool] ii &] [... [add_digits] ...] [ELSE] ifte" - ] - }, - { - "cell_type": "code", - "execution_count": 130, - "id": "9caf15b4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "[_add_rec_pred [bool] ii &] inscribe" - ] - }, - { - "cell_type": "markdown", - "id": "9445eb3c", - "metadata": {}, - "source": [ - "Plugging in `add-with-carry` we get:" - ] - }, - { - "cell_type": "code", - "execution_count": 118, - "id": "7e745018", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 false [2 3] [5 6]" - ] - } - ], - "source": [ - "clear\n", - "\n", - "false [1 2 3] [4 5 6] uncons-two [add-with-carry] dipd" - ] - }, - { - "cell_type": "markdown", - "id": "121ff3c0", - "metadata": {}, - "source": [ - "### Recur\n", - "\n", - "It is at this point that we would recur:\n", - "\n", - " 5 false [2 3] [5 6] [add_digits] i ...\n", - " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n", - "\n", - "This part runs and becomes some list (possibly empty) of result digits (the base case consumes the carry flag and one list):\n", - "\n", - " 5 [7 9]\n", - "\n", - "So our conclusion must be `cons`:\n", - "\n", - " 5 [7 9] cons\n", - " [5 7 9]\n", - "\n", - "And our whole branch function is:\n", - "\n", - " THEN == uncons-two [add-with-carry] dipd [add_digits] i cons\n", - "\n", - " [_add_rec_pred] [THEN] [ELSE] ifte\n", - "\n", - "It's a little tricky having that recursive call being nested like that:\n", - "\n", - " THEN0.1 == [add-with-carry] dipd\n", - " THEN0 == uncons-two THEN0.1\n", - " THEN1 == i cons\n", - "\n", - " [_add_rec_pred] [THEN0 [add_digits] THEN1] [ELSE] ifte\n", - "\n", - "But we can make it work." - ] - }, - { - "cell_type": "markdown", - "id": "cd576704", - "metadata": {}, - "source": [ - "#### Interlude: Nested or Unnested?\n", - "\n", - " F == [P] [THEN] [R0] [R1] genrec\n", - " F == [P] [THEN] [R0 [F] R1] ifte\n", - "\n", - "Our recursive branch will take the form of an `ifte` expression that incorporates the function `[F]` into one of its branches:\n", - "\n", - " F == [P] [THEN] [[P0] [THEN0 [F] THEN1] [ELSE] ifte] ifte\n", - "\n", - " F == [P] [THEN] [[P0] [THEN0]] [F] [[THEN1] cons concat [ELSE] ifte] ifte\n", - "\n", - "\n", - "\n", - " [P0] [THEN0] [F] [THEN1] cons concat [ELSE] ifte\n", - " ------------------------------------------------------\n", - " [P0] [THEN0] [[F] THEN1] concat [ELSE] ifte\n", - " ------------------------------------------------------\n", - " [P0] [THEN0 [F] THEN1] [ELSE] ifte\n", - " ------------------------------------------------------\n", - " [P0] [THEN0 [F] THEN1] [ELSE] ifte\n", - "\n", - "It's awkward but it works." - ] - }, - { - "cell_type": "code", - "execution_count": 112, - "id": "4fcf1d97", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear" - ] - }, - { - "cell_type": "markdown", - "id": "aa76c2fc", - "metadata": {}, - "source": [ - "#### add-carry-to-digits\n", - "So now we have `carry` and one list of digits. (We also know that we have at least one digit in the list at this point but we likely won't need that information.)\n", - "\n", - " carry [a ...] add-carry-to-digits\n", - "\n", - "I think once we define `add-carry-to-digits` we are pretty much done? Let's find out." - ] - }, - { - "cell_type": "markdown", - "id": "828699dc", - "metadata": {}, - "source": [ - "##### If `carry` is `false` we just return the digits list, eh?\n", - "\n", - " false [a ...] swap [] [THEN] branch\n", - " -----------------------------------------\n", - " [a ...]" - ] - }, - { - "cell_type": "markdown", - "id": "2a386fe2", - "metadata": {}, - "source": [ - "##### If `true` we want to maybe reuse `add-with-carry`?\n", - "\n", - "\n", - " true [a ...] swap [THEN] [] branch\n", - " ----------------------------------------\n", - " [a ...] THEN\n", - " --------------------------------------\n", - " false 1 a add-with-carry [...] G\n", - " --------------------------------------\n", - " (1+a) carry [...] G\n", - " ------------------------------------------------\n", - " (1+a) carry [...] add-carry-to-digits cons\n", - " ------------------------------------------------\n", - " (1+a) [...] cons\n", - " ------------------------------------------------\n", - " [(1+a) ...]\n", - "\n", - "So `add-carry-to-digits` is a recursive function (not a `branch` after all.)" - ] - }, - { - "cell_type": "markdown", - "id": "08a149fa", - "metadata": {}, - "source": [ - "Omitting `ditch-empty-list` for the moment:\n", - "\n", - " THEN == [false 1] dip uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - "\n", - " [a ...] THEN\n", - " [a ...] [false 1] dip uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - " false 1 [a ...] uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - " false 1 a [...] [add-with-carry] dip [add-carry-to-digits] i cons\n", - " false 1 a add-with-carry [...] [add-carry-to-digits] i cons\n", - " (1+a) false [...] [add-carry-to-digits] i cons\n", - " (1+a) false [...] add-carry-to-digits cons\n", - " (1+a) [...] cons\n", - " [(1+a) ...]\n", - "\n", - "QED" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "id": "9f4ebfd9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear" - ] - }, - { - "cell_type": "markdown", - "id": "afd8074b", - "metadata": {}, - "source": [ - "#### add-carry-to-digits, recursively\n", - "\n", - " carry [...] add-carry-to-digits\n", - "\n", - " carry [...] [P] [THEN] [R0] [R1] genrec\n", - "\n", - "So:\n", - "\n", - " P == swap not\n", - " \n", - "-or-\n", - "\n", - " P == pop not\n", - " \n", - "and\n", - "\n", - " THEN == popd\n", - "\n", - "That leaves:\n", - "\n", - " true [...] R0 [add-carry-to-digits] R1\n", - "\n", - "I am tempted to leave the transformation from the above form to\n", - "\n", - " true [...] popd [false 1] dip uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - "\n", - "as the proverbial \"exercise for the reader\" but it's kinda trivial:\n", - "\n", - " R0 == popd [false 1] dip uncons [add-with-carry] dip\n", - " R1 == i cons\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "id": "0c095d53", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 false [2 3]" - ] - } - ], - "source": [ - "true [1 2 3] popd [false 1] dip uncons [add-with-carry] dip" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "id": "e793bd13", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 [2 3]" - ] - } - ], - "source": [ - "popd" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "id": "e5d3966a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2 2 3]" - ] - } - ], - "source": [ - "cons" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "id": "c8aec12d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear\n", - "\n", - "[add-carry-to-digits [pop not] [popd] [popd [false 1] dip uncons [add-with-carry] dip] [i cons] genrec] inscribe" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "id": "d290d896", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2 2 3]" - ] - } - ], - "source": [ - "true [1 2 3] add-carry-to-digits" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "id": "10cb7a1f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1 2 3]" - ] - } - ], - "source": [ - "clear\n", - "\n", - "false [1 2 3] add-carry-to-digits" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "id": "89298766", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "true [2147483647 2147483647 2 3]" - ] - } - ], - "source": [ - "clear\n", - "\n", - "true base -- dup [2 3] ccons" - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "id": "af73db1b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0 0 3 3]" - ] - } - ], - "source": [ - "add-carry-to-digits" - ] - }, - { - "cell_type": "markdown", - "id": "69f59f45", - "metadata": {}, - "source": [ - "E.g. 3299 + 1 = 3300" - ] - }, - { - "cell_type": "code", - "execution_count": 90, - "id": "00fa293e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false [2147483647 2147483647 2 3]" - ] - } - ], - "source": [ - "clear\n", - "\n", - "false base -- dup [2 3] ccons" - ] - }, - { - "cell_type": "code", - "execution_count": 91, - "id": "227a28f1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2147483647 2147483647 2 3]" - ] - } - ], - "source": [ - "add-carry-to-digits" - ] - }, - { - "cell_type": "markdown", - "id": "50d06f33", - "metadata": {}, - "source": [ - "So that seems like it works." - ] - }, - { - "cell_type": "code", - "execution_count": 119, - "id": "7e735e04", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear" - ] - }, - { - "cell_type": "markdown", - "id": "b55599cb", - "metadata": {}, - "source": [ - "## Our Story So Far...\n", - " _add_p == [bool not] ii &\n", - " _add_then == pop swap [] [1 swons] branch\n", - " _add_rec_pred == [bool] ii &\n", - "\n", - " add_digits == [_add_p] [_add_then] [R0] [R1] genrec" - ] - }, - { - "cell_type": "markdown", - "id": "f515f498", - "metadata": {}, - "source": [ - "Where:\n", - "\n", - " R0 [add_digits] R1\n", - " -----------------------------------------------------\n", - " [_add_rec_pred] [ THEN ] [ELSE] ifte\n", - " -----------------------------------------------------\n", - " [_add_rec_pred] [THEN0 [add_digits] THEN1] [ELSE] ifte\n", - "\n", - " THEN0 == uncons-two [add-with-carry] dipd\n", - "\n", - " THEN1 == i cons\n", - "\n", - " ELSE == ditch-empty-list add-carry-to-digits" - ] - }, - { - "cell_type": "code", - "execution_count": 131, - "id": "348e377d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "[THEN0 uncons-two [add-with-carry] dipd] inscribe\n", - "[THEN1 i cons] inscribe\n", - "[ELSE ditch-empty-list add-carry-to-digits] inscribe" - ] - }, - { - "cell_type": "markdown", - "id": "c3b2768f", - "metadata": {}, - "source": [ - "Recalling:\n", - "\n", - " [P0] [THEN0] [F] [THEN1] cons concat [ELSE] ifte\n", - " ------------------------------------------------------\n", - " [P0] [THEN0] [[F] THEN1] concat [ELSE] ifte\n", - " ------------------------------------------------------\n", - " [P0] [THEN0 [F] THEN1] [ELSE] ifte\n", - " ------------------------------------------------------\n", - " [P0] [THEN0 [F] THEN1] [ELSE] ifte" - ] - }, - { - "cell_type": "markdown", - "id": "2d58b809", - "metadata": {}, - "source": [ - "Ergo:\n", - "\n", - " R0 == [_add_rec_pred] [THEN0]\n", - " R1 == [THEN1] cons concat [ELSE] ifte\n", - "\n", - " add_digits == [_add_p] [_add_then] [R0] [R1] genrec\n" - ] - }, - { - "cell_type": "code", - "execution_count": 132, - "id": "2219756c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "[_add_R0 [_add_rec_pred] [THEN0]] inscribe\n", - "[_add_R1 [THEN1] cons concat [ELSE] ifte] inscribe" - ] - }, - { - "cell_type": "code", - "execution_count": 133, - "id": "80866347", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "[add_digits [_add_p] [_add_then] [_add_R0] [_add_R1] genrec] inscribe" - ] - }, - { - "cell_type": "code", - "execution_count": 134, - "id": "51314100", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false [3 2 1] [8]" - ] - } - ], - "source": [ - "false [3 2 1] [8] " - ] - }, - { - "cell_type": "code", - "execution_count": 135, - "id": "63a589cb", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[11 2 1]" - ] - } - ], - "source": [ - "add_digits" - ] - }, - { - "cell_type": "code", - "execution_count": 136, - "id": "b5c5b50b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2147483647 11 2 1]" - ] - } - ], - "source": [ - "base -- swons" - ] - }, - { - "cell_type": "code", - "execution_count": 137, - "id": "1c81b4bc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false [2147483647 11 2 1] [23]" - ] - } - ], - "source": [ - "false swap [23]" - ] - }, - { - "cell_type": "code", - "execution_count": 138, - "id": "cdd845ef", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[22 12 2 1]" - ] - } - ], - "source": [ - "add_digits" - ] - }, - { - "cell_type": "code", - "execution_count": 142, - "id": "583a2390", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2147483647] [1]" - ] - } - ], - "source": [ - "clear base -- unit [1]" - ] - }, - { - "cell_type": "markdown", - "id": "defedd4b", - "metadata": {}, - "source": [ - "When i tried it it failed and inspection revealed that I had forgotten to set the initial carry value." - ] - }, - { - "cell_type": "code", - "execution_count": 144, - "id": "532967be", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false [2147483647] [1]" - ] - } - ], - "source": [ - "clear false base -- unit [1]" - ] - }, - { - "cell_type": "code", - "execution_count": 145, - "id": "e15c8b33", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0 1]" - ] - } - ], - "source": [ - "add_digits" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "84c6c958", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 148, - "id": "ca5c95af", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "false [2147483647 2147483647 2147483647 2147483647] [1]" - ] - } - ], - "source": [ - "clear false base -- unit dup concat dup concat [1]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3a6960ea", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "8c32715b", - "metadata": {}, - "source": [ - "#### I made a mistake in add-carry-to-digits\n", - "\n", - "The recursive branch:\n", - "\n", - " true [...] R0 [add-carry-to-digits] R1\n", - "\n", - "Must also deal with the case where the list is empty!\n", - "\n", - " true [] R0 [add-carry-to-digits] R1\n", - "\n", - "The `popd` is okay, but then we need a branch:\n", - "\n", - " R0 == popd ? [1 swons] [R0.t] branch\n", - " R0.t == [false 1] dip uncons [add-with-carry] dip\n", - "\n", - " R1 == i cons\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1b4b1302", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "cab5271f", - "metadata": {}, - "source": [ - "#### add-carry-to-digits, recursively, TAKE TWO\n", - "\n", - " carry [...] add-carry-to-digits\n", - "\n", - " carry [...] [P] [THEN] [R0] [R1] genrec\n", - "\n", - " P == pop not\n", - " THEN == popd\n", - "\n", - "That leaves one of:\n", - "\n", - " true [...] R0 [add-carry-to-digits] R1\n", - "\n", - "-or-\n", - "\n", - " true [] R0 [add-carry-to-digits] R1\n", - "\n", - "It would be fun to just set `R1` to `ifte`\n", - "\n", - " true [] R0 [add-carry-to-digits] ifte\n", - "\n", - "Which means that `R0` must be a predicate and the \"then\" branch.\n", - "\n", - " true [] [bool not] [THEN] [add-carry-to-digits] ifte\n", - "\n", - "And I think `THEN` would just be:\n", - "\n", - " THEN == popd 1 swons\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 162, - "id": "955aed77", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear\n", - "[R0.t [false 1] dip uncons [add-with-carry] dip] inscribe\n", - "[add-carry-to-digits\n", - " [pop not]\n", - " [popd]\n", - " [[bool not] [popd 1 swons]]\n", - " [ifte]\n", - " genrec\n", - "] inscribe" - ] - }, - { - "cell_type": "markdown", - "id": "1ee5b941", - "metadata": {}, - "source": [ - "This is still broken because the " - ] - }, - { - "cell_type": "code", - "execution_count": 160, - "id": "8ca1f509", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "true [2147483647 2147483647 2147483647 2147483647]" - ] - } - ], - "source": [ - "clear true base -- unit dup concat dup concat" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "77e2e26f", - "metadata": {}, - "outputs": [], - "source": [ - "add-carry-to-digits" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "07a528bd", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "97ed01aa", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dee054a6", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f92eebb0", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "36e683fd", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "91ffeec1", - "metadata": {}, - "source": [ - "#### add-carry-to-digits\n", - "\n", - "So `add-carry-to-digits` is a recursive function:\n", - "\n", - " carry [a ...] add-carry-to-digits\n", - "\n", - " carry [a ...] [P] [THEN] [R0] [R1] genrec\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dc5f3cba", - "metadata": {}, - "outputs": [], - "source": [ - "[pop not] [popd] [[bool not] [1 cons swap]] [ifte] genrec" - ] - }, - { - "cell_type": "markdown", - "id": "6201b301", - "metadata": {}, - "source": [ - "##### If `carry` is `false` we just return the digits list, eh?\n", - "\n", - " false [a ...] [pop not] [popd] [R0] [R1] genrec\n", - " -----------------------------------------------------\n", - " [a ...]" - ] - }, - { - "cell_type": "markdown", - "id": "2ffbb77c", - "metadata": {}, - "source": [ - "##### If `true` we want to maybe reuse `add-with-carry`?\n", - "\n", - " true [a ...] R0 [add-carry-to-digits] R1\n", - "\n", - "-or-\n", - "\n", - " true [] R0 [add-carry-to-digits] R1\n", - "\n", - "Um...\n", - "\n", - " true [] [bool not] [1 cons popd] [add-carry-to-digits] ifte\n", - "\n", - "\n", - " true [] [bool not] [1 cons popd] [add-carry-to-digits] ifte\n", - " true [] 1 cons popd\n", - " true [1] popd\n", - " [1]\n", - "\n", - "-or-\n", - "\n", - " true [a ...] [bool not] [1 cons swap] [F add-carry-to-digits] itfe\n", - " true [a ...] F add-carry-to-digits\n", - "\n", - "\n", - " true [a ...] F add-carry-to-digits\n", - " ---------------------------------------------------------\n", - " true 0 a add-with-carry [...] add-carry-to-digits\n", - "\n", - " \n", - " F == 0 swap uncons [add-with-carry] dip\n", - "\n", - "\n", - " true [a ...] [bool not] [1 cons swap] [add-carry-to-digits] [F] swoncat itfe\n", - "\n", - "\n", - "\n", - " true [a ...] [bool not] [1 cons swap] [add-carry-to-digits] [0 swap uncons [add-with-carry] dip] swoncat itfe\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 162, - "id": "0ee18517", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear\n", - "[R0.t [false 1] dip uncons [add-with-carry] dip] inscribe\n", - "[add-carry-to-digits\n", - " [pop not]\n", - " [popd]\n", - " [[bool not] [popd 1 swons]]\n", - " [ifte]\n", - " genrec\n", - "] inscribe" - ] - }, - { - "cell_type": "markdown", - "id": "965cf4df", - "metadata": {}, - "source": [ - "Omitting `ditch-empty-list` for the moment:\n", - "\n", - " THEN == [false 1] dip uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - "\n", - " [a ...] THEN\n", - " [a ...] [false 1] dip uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - " false 1 [a ...] uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - " false 1 a [...] [add-with-carry] dip [add-carry-to-digits] i cons\n", - " false 1 a add-with-carry [...] [add-carry-to-digits] i cons\n", - " (1+a) false [...] [add-carry-to-digits] i cons\n", - " (1+a) false [...] add-carry-to-digits cons\n", - " (1+a) [...] cons\n", - " [(1+a) ...]\n", - "\n", - "QED" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "id": "1787863d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear" - ] - }, - { - "cell_type": "markdown", - "id": "77eeace6", - "metadata": {}, - "source": [ - "#### add-carry-to-digits, recursively\n", - "\n", - " carry [...] add-carry-to-digits\n", - "\n", - " carry [...] [P] [THEN] [R0] [R1] genrec\n", - "\n", - "So:\n", - "\n", - " P == swap not\n", - " \n", - "-or-\n", - "\n", - " P == pop not\n", - " \n", - "and\n", - "\n", - " THEN == popd\n", - "\n", - "That leaves:\n", - "\n", - " true [...] R0 [add-carry-to-digits] R1\n", - "\n", - "I am tempted to leave the transformation from the above form to\n", - "\n", - " true [...] popd [false 1] dip uncons [add-with-carry] dip [add-carry-to-digits] i cons\n", - "\n", - "as the proverbial \"exercise for the reader\" but it's kinda trivial:\n", - "\n", - " R0 == popd [false 1] dip uncons [add-with-carry] dip\n", - " R1 == i cons\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2a37982c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0c1ad90d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a184d47c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0343594a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e13c8b2e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e921d72a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2727bab9", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3373be97", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "342e9982", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7f655e89", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "611f40ff", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bada9caa", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fb66962d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6764903e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b4b10bfe", - "metadata": {}, - "outputs": [], - "source": [ - "[base 2147483648] inscribe\n", - "[valid_digit [0 >] [base <] &&] inscribe\n", - "[_add_p [bool not] ii &] inscribe\n", - "[_add_then pop swap [] [1 swons] branch] inscribe\n", - "[_add_rec_pred [bool] ii &] inscribe\n", - "[bool_to_int [0] [1] branch] inscribe\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\n", - "[uncons-two [uncons] ii swapd] inscribe\n", - "[ditch-empty-list [bool] [popd] [pop] ifte] inscribe\n", - "[add-carry-to-digits [pop not] [popd] [popd [false 1] dip uncons [add-with-carry] dip] [i cons] genrec] inscribe\n", - "[THEN0 uncons-two [add-with-carry] dipd] inscribe\n", - "[THEN1 i cons] inscribe\n", - "[ELSE ditch-empty-list add-carry-to-digits] inscribe\n", - "[_add_R0 [_add_rec_pred] [THEN0]] inscribe\n", - "[_add_R1 [THEN1] cons concat [ELSE] ifte] inscribe\n", - "[add_digits [_add_p] [_add_then] [_add_R0] [_add_R1] genrec] inscribe\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "11c66f4e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "60cda31e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0852a374", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c2c3f8f5", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "eb4d2dac", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6c7f705e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8e69a883", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "551b52de", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 55, - "id": "3fb6905c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1] 2 false [2 3]" - ] - } - ], - "source": [ - "[isnt_bigint\n", - "[first isnt_bool]\n", - "[rest [isnt_int] map]\n", - "cleave cons\n", - "] inscribe" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "id": "39b3d4bc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1] 2 false [2 3] [[!] [!-] [!=] [%] [&] [&&] [*] [+] [++] [-] [--] [/] [<] [<<] [<<{}] [<=] [<>] [<{}] [=] [>] [>=] [>>] [?] [abs] [add] [add-with-carry] [all] [anamorphism] [and] [any] [app1] [app2] [app3] [appN] [at] [b] [base] [binary] [bool] [bool_to_int] [branch] [ccccons] [ccons] [choice] [clear] [cleave] [clop] [cmp] [codi] [codireco] [concat] [cond] [cons] [digitalize] [dinfrirst] [dip] [dipd] [dipdd] [disenstacken] [ditch-empty-list] [div] [divmod] [down_to_zero] [drop] [dup] [dupd] [dupdd] [dupdip] [dupdipd] [enstacken] [eq] [first] [first_two] [flatten] [floordiv] [fork] [fourth] [gcd] [gcd2] [ge] [genrec] [getitem] [grabN] [grba] [gt] [help] [i] [ifte] [ii] [infra] [infrst] [inscribe] [isnt_bigint] [isnt_bool] [isnt_int] [isnt_stack] [le] [loop] [lshift] [lt] [make_generator] [manual] [map] [max] [min] [mod] [mul] [ne] [neg] [not] [nulco] [nullary] [of] [or] [over] [pam] [pm] [pop] [popd] [popdd] [popop] [popopd] [popopdd] [popopop] [pow] [pred] [primrec] [product] [quit] [quoted] [range] [range_to_zero] [reco] [remove] [rest] [reverse] [roll<] [roll>] [rolldown] [rollup] [rrest] [rshift] [run] [second] [select] [sharing] [shift] [shunt] [size] [sort] [spiral_next] [split_at] [split_list] [sqr] [stack] [stackd] [step] [step_zero] [stuncons] [stununcons] [sub] [succ] [sum] [swaack] [swap] [swapd] [swoncat] [swons] [tailrec] [take] [ternary] [third] [times] [to_bigint] [trace] [tuck] [unary] [uncons] [unique] [unit] [unquoted] [unswons] [valid_digit] [warranty] [while] [words] [x] [xor] [zip] [|] [||] [•]]" - ] - } - ], - "source": [ - "words" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "id": "4c5edbfa", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [] - } - ], - "source": [ - "clear" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "id": "1e201000", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2147483648" - ] - } - ], - "source": [ - "1 31 <<" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b310e739", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 59, - "id": "fc53f72d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "12 3" - ] - } - ], - "source": [ - "clear\n", - "\n", - "123 10 divmod" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "id": "2cb7c88f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "12 0 3" - ] - } - ], - "source": [ - "base divmod" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e7c36361", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 61, - "id": "005ac08e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "==== Help on divmod ====\n", - "\n", - "Similarly to pm (\"Plus or minus\") this function computes\n", - "both the\n", - "::\n", - "\n", - " a b divmod\n", - " ---------------------\n", - " a b div a b mod\n", - " ---------------------\n", - " q r\n", - "\n", - "Where: q * b + r == a\n", - "\n", - "---- end ( divmod )\n", - "\n", - "\n", - "12 0 3" - ] - } - ], - "source": [ - "[divmod] help" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6ebcf6d8", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {