• R/O
  • HTTP
  • SSH
  • HTTPS

Thun: Commit

Interpreter and library.


Commit MetaInfo

Revision062b01da55e8349521f2d2cdbea3b103ff6ad82b (tree)
Zeit2022-10-16 01:52:58
AutorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

That seems to work: addition and subtraction.

Ändern Zusammenfassung

Diff

--- a/docs/notebooks/BigInts.ipynb
+++ b/docs/notebooks/BigInts.ipynb
@@ -5078,39 +5078,245 @@
50785078 ]
50795079 },
50805080 {
5081+ "cell_type": "code",
5082+ "execution_count": 211,
5083+ "id": "3d29ed60",
5084+ "metadata": {},
5085+ "outputs": [
5086+ {
5087+ "name": "stdout",
5088+ "output_type": "stream",
5089+ "text": []
5090+ }
5091+ ],
5092+ "source": [
5093+ "clear"
5094+ ]
5095+ },
5096+ {
5097+ "cell_type": "markdown",
5098+ "id": "cb2fe4bb",
5099+ "metadata": {},
5100+ "source": [
5101+ "#### `neg-bigint`"
5102+ ]
5103+ },
5104+ {
5105+ "cell_type": "code",
5106+ "execution_count": 212,
5107+ "id": "47d10c16",
5108+ "metadata": {},
5109+ "outputs": [
5110+ {
5111+ "name": "stdout",
5112+ "output_type": "stream",
5113+ "text": []
5114+ }
5115+ ],
5116+ "source": [
5117+ "[neg-bigint [not] infra] inscribe"
5118+ ]
5119+ },
5120+ {
5121+ "cell_type": "code",
5122+ "execution_count": null,
5123+ "id": "781a1ed2",
5124+ "metadata": {},
5125+ "outputs": [],
5126+ "source": [
5127+ "123 "
5128+ ]
5129+ },
5130+ {
5131+ "cell_type": "code",
5132+ "execution_count": 213,
5133+ "id": "e2bf4564",
5134+ "metadata": {},
5135+ "outputs": [
5136+ {
5137+ "name": "stdout",
5138+ "output_type": "stream",
5139+ "text": [
5140+ "-123"
5141+ ]
5142+ }
5143+ ],
5144+ "source": [
5145+ "to-bigint neg-bigint from-bigint"
5146+ ]
5147+ },
5148+ {
5149+ "cell_type": "code",
5150+ "execution_count": 214,
5151+ "id": "9b4c3ea7",
5152+ "metadata": {},
5153+ "outputs": [
5154+ {
5155+ "name": "stdout",
5156+ "output_type": "stream",
5157+ "text": [
5158+ "123"
5159+ ]
5160+ }
5161+ ],
5162+ "source": [
5163+ "to-bigint neg-bigint from-bigint"
5164+ ]
5165+ },
5166+ {
5167+ "cell_type": "code",
5168+ "execution_count": null,
5169+ "id": "7c4edc9a",
5170+ "metadata": {},
5171+ "outputs": [],
5172+ "source": []
5173+ },
5174+ {
5175+ "cell_type": "code",
5176+ "execution_count": null,
5177+ "id": "8d467543",
5178+ "metadata": {},
5179+ "outputs": [],
5180+ "source": []
5181+ },
5182+ {
5183+ "cell_type": "code",
5184+ "execution_count": null,
5185+ "id": "5722d274",
5186+ "metadata": {},
5187+ "outputs": [],
5188+ "source": []
5189+ },
5190+ {
5191+ "cell_type": "code",
5192+ "execution_count": null,
5193+ "id": "d08a5ea1",
5194+ "metadata": {},
5195+ "outputs": [],
5196+ "source": []
5197+ },
5198+ {
5199+ "cell_type": "code",
5200+ "execution_count": null,
5201+ "id": "4e18e84a",
5202+ "metadata": {},
5203+ "outputs": [],
5204+ "source": []
5205+ },
5206+ {
5207+ "cell_type": "code",
5208+ "execution_count": null,
5209+ "id": "b72d58e6",
5210+ "metadata": {},
5211+ "outputs": [],
5212+ "source": []
5213+ },
5214+ {
5215+ "cell_type": "code",
5216+ "execution_count": null,
5217+ "id": "f134ab23",
5218+ "metadata": {},
5219+ "outputs": [],
5220+ "source": []
5221+ },
5222+ {
50815223 "cell_type": "markdown",
50825224 "id": "f2b707c1",
50835225 "metadata": {},
50845226 "source": [
50855227 "## Appendix: Source Code\n",
5228+ " clear\n",
5229+ " [base 2147483648]\n",
5230+ " [ditch-empty-list [bool] [popd] [pop] ifte]\n",
5231+ " [bool-to-int [0] [1] branch]\n",
5232+ " [uncons-two [uncons] ii swapd]\n",
5233+ " [sandwich swap [cons] dip swoncat]\n",
5234+ "\n",
5235+ " [digitalize [0 <=] [pop []] [base divmod swap] [i cons] genrec]\n",
5236+ " [to-bigint [!-] [abs digitalize] cleave cons]\n",
5237+ "\n",
5238+ " [prep rest 1 0 rolldown]\n",
5239+ " [from-bigint' [next-digit] step popd]\n",
5240+ " [next-digit [increase-power] [accumulate-digit] clop popdd]\n",
5241+ " [increase-power popop base *]\n",
5242+ " [accumulate-digit rolldown * +]\n",
5243+ "\n",
5244+ " [sign-int [first] [prep from-bigint'] cleave]\n",
5245+ " [neg-if-necessary swap [neg] [] branch]\n",
5246+ " [from-bigint sign-int neg-if-necessary]\n",
5247+ "\n",
5248+ " [add-with-carry _add-with-carry0 _add-with-carry1]\n",
5249+ " [_add-with-carry0 [bool-to-int] dipd + +]\n",
5250+ " [_add-with-carry1 base [mod] [>=] clop]\n",
5251+ "\n",
5252+ " [add-carry-to-digits [pop not] [popd] [_actd_R0] [i cons] genrec]\n",
5253+ " [_actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte]\n",
5254+ " [_actd_R0.else popd 1 false rolldown]\n",
5255+ " [_actd_R0.then 0 swap uncons [add-with-carry] dip]\n",
5256+ "\n",
5257+ " [add-digits initial-carry add-digits']\n",
5258+ " [initial-carry false rollup]\n",
5259+ "\n",
5260+ " [add-digits' [P] [THEN] [R0] [R1] genrec]\n",
5261+ " [P [bool] ii & not]\n",
5262+ " [THEN [P'] [THEN'] [ELSE] ifte]\n",
5263+ " [R0 uncons-two [add-with-carry] dipd]\n",
5264+ " [R1 i cons]\n",
5265+ " [P' [bool] ii |]\n",
5266+ " [THEN' ditch-empty-list add-carry-to-digits]\n",
5267+ " [ELSE pop swap [] [1 swons] branch]\n",
5268+ "\n",
5269+ " [same-sign [first] ii xor not]\n",
5270+ " [add-like-bigints [uncons] dip rest add-digits cons]\n",
5271+ " [add-bigints [same-sign] [add-like-bigints] [neg-bigint sub-like-bigints] ifte]\n",
5272+ "\n",
5273+ " [build-two-list-combiner _btlc0 _btlc1 [i cons]]\n",
5274+ " [_btlc0.0 [[ditch-empty-list] swoncat] dip]\n",
5275+ " [_btlc0.1 [pop] swoncat]\n",
5276+ " [_btlc0.3 [_btlc0.0 _btlc0.1] dip]\n",
5277+ " [_btlc0.4 [uncons-two] [dipd] sandwich]\n",
5278+ " [_btlc0 _btlc0.3 _btlc0.4]\n",
5279+ " [_btlc1 [[ifte] ccons [P'] swons [P] swap] dip]\n",
5280+ "\n",
5281+ " [carry [] [1 swons] branch]\n",
5282+ "\n",
5283+ " [compare-pairs [bool not] [pop false] [[first [>=] infrst] [pop true]] [[rest] swoncat ifte] genrec]\n",
5284+ " [xR1 uncons-two [unit cons swons] dipd]\n",
5285+ " [xP [bool] ii & not]\n",
5286+ " [BASE [bool] [popop pop true] [[pop bool] [popop pop false] [popop compare-pairs] ifte] ifte]\n",
5287+ " [gt-bigint <<{} [xP] [BASE] [xR1] tailrec]\n",
5288+ " [check-gt [gt-bigint] [swap [not] dipd] [] ifte]\n",
5289+ "\n",
5290+ " [sub-carry pop]\n",
5291+ "\n",
5292+ " [sub-carry-from-digits [pop not] [popd] [_scfd_R0] [i cons-but-not-leading-zeroes] genrec] inscribe\n",
5293+ " [_scfd_R0 uncons 0 swap [sub-with-carry] dip] inscribe\n",
5294+ " [cons-but-not-leading-zeroes [P'] [cons] [popd] ifte]\n",
5295+ "\n",
5296+ " [sub-with-carry _sub-with-carry0 _sub-with-carry1]\n",
5297+ " [_sub-with-carry0 rolldown bool-to-int [-] ii]\n",
5298+ " [_sub-with-carry1 [base + base mod] [0 <] cleave]\n",
5299+ "\n",
5300+ " [sub-like-bigints [uncons] dip rest check-gt sub-digits cons]\n",
5301+ " [sub-digits initial-carry sub-digits']\n",
5302+ "\n",
5303+ " enstacken [inscribe] step\n",
5304+ "\n",
5305+ " [add-carry-to-digits]\n",
5306+ " [swap carry]\n",
5307+ " [add-with-carry]\n",
5308+ " build-two-list-combiner\n",
5309+ " [genrec] ccons ccons\n",
5310+ " [add-digits'] swoncat\n",
5311+ " inscribe\n",
50865312 "\n",
5087- " base 2147483648\n",
5088- " ditch-empty-list [bool] [popd] [pop] ifte\n",
5089- " bool-to-int [0] [1] branch\n",
5090- " uncons-two [uncons] ii swapd\n",
5091- "\n",
5092- " add-with-carry _a0 _a1\n",
5093- " _a0 [bool-to-int] dipd + +\n",
5094- " _a1 base [mod] [>=] clop\n",
5095- "\n",
5096- " add-carry-to-digits [pop not] [popd] [_actd_R0] [i cons] genrec\n",
5097- " _actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte\n",
5098- " _actd_R0.else popd 1 false rolldown\n",
5099- " _actd_R0.then 0 swap uncons [add-with-carry] dip\n",
5100- "\n",
5101- " add-digits initial-carry add-digits'\n",
5102- " initial-carry false rollup\n",
5103- " add-digits' [P] [THEN] [R0] [i cons] genrec\n",
5104- " P [bool] ii & not\n",
5105- " THEN [P'] [THEN'] [ELSE] ifte\n",
5106- " R0 uncons-two [add-with-carry] dipd\n",
5107- " P' [bool] ii |\n",
5108- " THEN' ditch-empty-list add-carry-to-digits\n",
5109- " ELSE pop swap [] [1 swons] branch\n",
5110- "\n",
5111- " same-sign [first] ii xor not\n",
5112- " add-like-bigints [uncons] dip rest add-digits cons\n",
5113- " add-bigints [same-sign] [add-like-bigints] [1 0 /] ifte"
5313+ " [sub-carry-from-digits]\n",
5314+ " [swap sub-carry]\n",
5315+ " [sub-with-carry]\n",
5316+ " build-two-list-combiner\n",
5317+ " [genrec] ccons ccons\n",
5318+ " [sub-digits'] swoncat\n",
5319+ " inscribe\n"
51145320 ]
51155321 },
51165322 {
@@ -5122,7 +5328,7 @@
51225328 "\n",
51235329 "So far I have three formats for Joy source:\n",
51245330 "\n",
5125- "- `def.txt` is a list of definitions (UTF-8), one per line, with no special marks (like above.)\n",
5331+ "- `def.txt` is a list of definitions (UTF-8), one per line, with no special marks.\n",
51265332 "- `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",
51275333 "- `[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",
51285334 "\n",
--- a/implementations/bigints.joy
+++ b/implementations/bigints.joy
@@ -18,6 +18,8 @@ clear
1818 [neg-if-necessary swap [neg] [] branch]
1919 [from-bigint sign-int neg-if-necessary]
2020
21+[neg-bigint [not] infra]
22+
2123 [add-with-carry _add-with-carry0 _add-with-carry1]
2224 [_add-with-carry0 [bool-to-int] dipd + +]
2325 [_add-with-carry1 base [mod] [>=] clop]
@@ -41,9 +43,9 @@ clear
4143
4244 [same-sign [first] ii xor not]
4345 [add-like-bigints [uncons] dip rest add-digits cons]
44-[add-bigints [same-sign] [add-like-bigints] [1 0 /] ifte]
46+[add-bigints [same-sign] [add-like-bigints] [neg-bigint sub-like-bigints] ifte]
4547
46-[build-two-list-combiner _btlc0 _btlc1 [i cons]]
48+[build-two-list-combiner _btlc0 _btlc1 [[i cons] genrec] ccons cons]
4749 [_btlc0.0 [[ditch-empty-list] swoncat] dip]
4850 [_btlc0.1 [pop] swoncat]
4951 [_btlc0.3 [_btlc0.0 _btlc0.1] dip]
@@ -73,13 +75,14 @@ clear
7375 [sub-like-bigints [uncons] dip rest check-gt sub-digits cons]
7476 [sub-digits initial-carry sub-digits']
7577
78+[sub-bigints [same-sign] [sub-like-bigints] [neg-bigint add-like-bigints] ifte]
79+
7680 enstacken [inscribe] step
7781
7882 [add-carry-to-digits]
7983 [swap carry]
8084 [add-with-carry]
8185 build-two-list-combiner
82-[genrec] ccons ccons
8386 [add-digits'] swoncat
8487 inscribe
8588
@@ -87,8 +90,5 @@ inscribe
8790 [swap sub-carry]
8891 [sub-with-carry]
8992 build-two-list-combiner
90-[genrec] ccons ccons
9193 [sub-digits'] swoncat
9294 inscribe
93-
94-
Show on old repository browser