Not quite as smooth as I'd hoped.

This commit is contained in:
Simon Forman
2022-10-14 20:36:50 -07:00
parent 6ade65ef0c
commit 0b4b79f884
3 changed files with 399 additions and 417 deletions
+365 -93
View File
@@ -2437,7 +2437,7 @@
{
"cell_type": "code",
"execution_count": 95,
"id": "e9dc6852",
"id": "d8832fda",
"metadata": {},
"outputs": [
{
@@ -2560,7 +2560,7 @@
},
{
"cell_type": "markdown",
"id": "dbfebd04",
"id": "fdc6fc09",
"metadata": {},
"source": [
"But this doesn't go quite far enough, I think.\n",
@@ -2570,7 +2570,7 @@
},
{
"cell_type": "markdown",
"id": "d4d65bf2",
"id": "94249f95",
"metadata": {},
"source": [
"I think `R0` will pretty much always do:\n",
@@ -2580,7 +2580,7 @@
},
{
"cell_type": "markdown",
"id": "35ff6c75",
"id": "ecc190e3",
"metadata": {},
"source": [
"And so it should be refactored further to something like:\n",
@@ -2619,7 +2619,7 @@
},
{
"cell_type": "markdown",
"id": "0f3be915",
"id": "c9fd7e21",
"metadata": {},
"source": [
"Let's figure out the new form.\n",
@@ -2642,7 +2642,7 @@
},
{
"cell_type": "markdown",
"id": "7d4ee083",
"id": "64118784",
"metadata": {},
"source": [
"Let's not over think it.\n",
@@ -2653,7 +2653,7 @@
},
{
"cell_type": "markdown",
"id": "b5c20b1c",
"id": "90310271",
"metadata": {},
"source": [
"With:\n",
@@ -2717,7 +2717,7 @@
},
{
"cell_type": "markdown",
"id": "06c700d6",
"id": "e8433d46",
"metadata": {},
"source": [
"So to get from \n",
@@ -2738,7 +2738,7 @@
{
"cell_type": "code",
"execution_count": 103,
"id": "063b3760",
"id": "77e6c03a",
"metadata": {},
"outputs": [
{
@@ -2766,7 +2766,7 @@
{
"cell_type": "code",
"execution_count": 104,
"id": "151f9f3d",
"id": "1119f1e6",
"metadata": {},
"outputs": [
{
@@ -2802,7 +2802,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "fba574be",
"id": "02bfc247",
"metadata": {},
"outputs": [],
"source": []
@@ -2831,7 +2831,7 @@
},
{
"cell_type": "markdown",
"id": "c9d4432e",
"id": "089fc71c",
"metadata": {},
"source": [
"Compare the above with what we wanted:\n",
@@ -2853,7 +2853,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "77e1ba72",
"id": "929366cc",
"metadata": {},
"outputs": [],
"source": []
@@ -2861,7 +2861,7 @@
{
"cell_type": "code",
"execution_count": 107,
"id": "0c11e3a8",
"id": "a3b7e48a",
"metadata": {},
"outputs": [
{
@@ -2886,7 +2886,7 @@
{
"cell_type": "code",
"execution_count": 108,
"id": "301c4fb0",
"id": "313ce113",
"metadata": {
"scrolled": true
},
@@ -2906,7 +2906,7 @@
{
"cell_type": "code",
"execution_count": 110,
"id": "115d4c56",
"id": "aca55d10",
"metadata": {},
"outputs": [
{
@@ -2923,7 +2923,7 @@
},
{
"cell_type": "markdown",
"id": "5e7c24c4",
"id": "92a471fa",
"metadata": {},
"source": [
" combine-two-lists == build-two-list-combiner [i cons] genrec\n",
@@ -2933,7 +2933,7 @@
{
"cell_type": "code",
"execution_count": 111,
"id": "810427a9",
"id": "0fa2607d",
"metadata": {},
"outputs": [
{
@@ -2954,7 +2954,7 @@
{
"cell_type": "code",
"execution_count": 112,
"id": "0ba7548f",
"id": "026095a5",
"metadata": {},
"outputs": [
{
@@ -2972,7 +2972,7 @@
{
"cell_type": "code",
"execution_count": 113,
"id": "a94d2e37",
"id": "15974737",
"metadata": {},
"outputs": [
{
@@ -2990,7 +2990,7 @@
{
"cell_type": "code",
"execution_count": 114,
"id": "7787e86b",
"id": "525bc382",
"metadata": {},
"outputs": [
{
@@ -3080,7 +3080,7 @@
{
"cell_type": "code",
"execution_count": 119,
"id": "67e321e7",
"id": "52bb5921",
"metadata": {},
"outputs": [
{
@@ -3191,7 +3191,7 @@
},
{
"cell_type": "markdown",
"id": "2cec47e6",
"id": "c85d6fe6",
"metadata": {},
"source": [
"#### Demonstrate `add-bigints`"
@@ -3200,7 +3200,7 @@
{
"cell_type": "code",
"execution_count": 125,
"id": "6168ebb7",
"id": "3da6266b",
"metadata": {},
"outputs": [
{
@@ -3218,7 +3218,7 @@
{
"cell_type": "code",
"execution_count": 126,
"id": "b8fee2d5",
"id": "7d38dbaa",
"metadata": {},
"outputs": [
{
@@ -3236,7 +3236,7 @@
{
"cell_type": "code",
"execution_count": 127,
"id": "9dc66c3b",
"id": "e65f7ff9",
"metadata": {},
"outputs": [
{
@@ -3254,7 +3254,7 @@
{
"cell_type": "code",
"execution_count": 128,
"id": "82e85105",
"id": "5d385f35",
"metadata": {},
"outputs": [
{
@@ -3272,7 +3272,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "87862546",
"id": "fb7da91a",
"metadata": {},
"outputs": [],
"source": []
@@ -3280,7 +3280,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "465edd01",
"id": "679e9df6",
"metadata": {},
"outputs": [],
"source": []
@@ -3288,7 +3288,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "6a5453b3",
"id": "d4d2850a",
"metadata": {},
"outputs": [],
"source": []
@@ -3296,7 +3296,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "6c1b83b4",
"id": "c4da9da4",
"metadata": {},
"outputs": [],
"source": []
@@ -3304,7 +3304,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5559f087",
"id": "55a37610",
"metadata": {},
"outputs": [],
"source": []
@@ -3312,7 +3312,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "76f5198f",
"id": "c637a7ea",
"metadata": {},
"outputs": [],
"source": []
@@ -3320,7 +3320,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9e8c8cba",
"id": "b56cf709",
"metadata": {},
"outputs": [],
"source": []
@@ -4546,9 +4546,25 @@
"check-gt"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "187c0381",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
}
],
"source": [
"clear"
]
},
{
"cell_type": "markdown",
"id": "97693e57",
"id": "1a8a8196",
"metadata": {},
"source": [
"### Subtraction, at last...\n",
@@ -4563,26 +4579,51 @@
"\n",
"At this point we would have the sign bit then the two digit lists.\n",
"\n",
" sign [c b a] [z y x]\n",
"\n",
" sign [c b a] [z y x]"
]
},
{
"cell_type": "markdown",
"id": "9751fd65",
"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\n",
"\n",
" sign swapped? [c b a] [z y x] check-gt"
]
},
{
"cell_type": "markdown",
"id": "df2ae839",
"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\n",
"\n",
" check-gt == [gt-bigint] [swap [not] dipd] [] ifte"
]
},
{
"cell_type": "markdown",
"id": "c775c3a4",
"metadata": {},
"source": [
"Now we subtract the digits:\n",
"\n",
" sign [c b a] [z y x] sub-digits cons\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",
@@ -4590,54 +4631,22 @@
" build-two-list-combiner\n",
" [i cons] genrec\n",
"\n",
"We just need to define the pieces.\n"
"We just need to define the pieces."
]
},
{
"cell_type": "markdown",
"id": "7c0fbfaf",
"id": "32eeaf38",
"metadata": {},
"source": [
" sub-carry-from-digits\n",
"\n",
"Should be easy to make modeled on\n",
"\n",
" add-carry-to-digits\n",
"\n",
"I'll do it in the morning, and if my logic is correct and I haven't bungled typing it in, the whole thing should just work! OMG? Tomorrow..."
]
},
{
"cell_type": "code",
"execution_count": 195,
"id": "b6c2e9ee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24 true"
]
}
],
"source": [
"[sub-carry-from-digits popd] inscribe"
]
},
{
"cell_type": "markdown",
"id": "a0067046",
"metadata": {},
"source": [
" sub-carry == pop\n",
"#### `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": "49387109",
"id": "3b47df3b",
"metadata": {},
"source": [
" carry a b sub-with-carry\n",
@@ -4652,16 +4661,14 @@
},
{
"cell_type": "code",
"execution_count": 182,
"id": "0237bb03",
"execution_count": 135,
"id": "880dc3da",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-10"
]
"text": []
}
],
"source": [
@@ -4672,8 +4679,8 @@
},
{
"cell_type": "code",
"execution_count": 193,
"id": "c8de9645",
"execution_count": 139,
"id": "bae53ad4",
"metadata": {
"scrolled": true
},
@@ -4682,25 +4689,25 @@
"name": "stdout",
"output_type": "stream",
"text": [
"false 23 2147483647"
"false 3 9"
]
}
],
"source": [
"clear false 23 base --"
"clear false 3 base --"
]
},
{
"cell_type": "code",
"execution_count": 194,
"id": "aa66a6ae",
"execution_count": 140,
"id": "f54d93bc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24 true"
"4 true"
]
}
],
@@ -4710,8 +4717,8 @@
},
{
"cell_type": "code",
"execution_count": 229,
"id": "3f75130f",
"execution_count": 141,
"id": "740034d8",
"metadata": {},
"outputs": [
{
@@ -4724,6 +4731,271 @@
"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": "d29e6193",
"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": "d31868ab",
"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": "5c4404a8",
"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": 147,
"id": "be093900",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3 2 1]"
]
}
],
"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": "253538a4",
"metadata": {},
"source": [
"Try it out:"
]
},
{
"cell_type": "code",
"execution_count": 148,
"id": "ce9f01e2",
"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": 160,
"id": "2120af39",
"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": 161,
"id": "55fc1f59",
"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": 162,
"id": "1db3eaeb",
"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": 163,
"id": "ededec7b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
}
],
"source": [
"clear"
]
},
{
"cell_type": "markdown",
"id": "ffc08bef",
"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": 165,
"id": "fe557598",
"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": null,
"id": "f7275cd6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "740a5c97",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ea8ba1c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 164,
"id": "b6c2e9ee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
}
],
"source": [
"[sub-carry-from-digits popd] inscribe"
]
},
{
"cell_type": "markdown",
"id": "32cef119",
"metadata": {},
"source": [
" sub-carry == pop"
]
},
{
"cell_type": "code",
"execution_count": 230,