Some notebooks.

This commit is contained in:
Simon Forman 2023-02-01 14:54:07 -08:00
parent 28fa767593
commit d2048a3846
3 changed files with 630 additions and 1284 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,190 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ff8ca725",
"metadata": {},
"source": [
"In the Thun dialect of Joy there are only four kinds of things:\n",
"\n",
"- Integers (positive and negative, unbounded)\n",
"- Boolean values \"true\" and \"false\"\n",
"- Lists of things enclosed by square brackets (\"\\[\" and \"\\]\").\n",
"- Symbols (names) which denote functions.\n",
"\n",
"When the interpreter encounters a literal value (an integer, Boolean, or list) it is put onto the **stack**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78e3e3fe",
"metadata": {},
"outputs": [],
"source": [
"1 2 3 true false [4 5 true []] "
]
},
{
"cell_type": "markdown",
"id": "68c241e5",
"metadata": {},
"source": [
"When symbols are encountered they are looked up in the **dictionary** and the function they denote is evaluated:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68f0292a",
"metadata": {},
"outputs": [],
"source": [
"cons"
]
},
{
"cell_type": "markdown",
"id": "9b0205ff",
"metadata": {},
"source": [
"There is a special function `trace` that will print a *trace* of the evalutation of an expression step-by-step. (It's like the `i` combinator with side-effects):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9638d25",
"metadata": {},
"outputs": [],
"source": [
"[cons] trace"
]
},
{
"cell_type": "markdown",
"id": "a0fc74d7",
"metadata": {},
"source": [
"Let's see some other functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18dfa724",
"metadata": {},
"outputs": [],
"source": [
"[pop unit ccons]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b324b9e",
"metadata": {},
"outputs": [],
"source": [
"trace"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "656740eb",
"metadata": {},
"outputs": [],
"source": [
"sum"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10e61981",
"metadata": {},
"outputs": [],
"source": [
"clear"
]
},
{
"cell_type": "markdown",
"id": "524c3507",
"metadata": {},
"source": [
"Control flow is achieved by special functions called **combinators** that accept quoted expressions on the stack and prefix them in various ways to the pending expression. The four core combinators are:\n",
"\n",
"* `branch` - do one thing or another based on a condition.\n",
"* `loop` - do one thing over again (or not.)\n",
"* sequence - do one thing then another (not a combinator, this is the default operation.)\n",
"* `fork` (do things independently of each other, parallel.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d331ecca",
"metadata": {},
"outputs": [],
"source": [
"23"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c68bc68d",
"metadata": {},
"outputs": [],
"source": [
"dup 0 < [] [0 swap -]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84a7fc4a",
"metadata": {},
"outputs": [],
"source": [
"[branch] trace"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa9fc0c9",
"metadata": {},
"outputs": [],
"source": [
"neg"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80708666",
"metadata": {},
"outputs": [],
"source": [
"[dup 0 < [] [0 swap -] branch] trace"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Joypy",
"language": "",
"name": "thun"
},
"language_info": {
"file_extension": ".joy",
"mimetype": "text/plain",
"name": "Joy"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -0,0 +1,62 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6ac9e0d5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6"
]
}
],
"source": [
"6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1bac5df8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24"
]
}
],
"source": [
"4 *"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e65a67a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Joypy",
"language": "",
"name": "thun"
},
"language_info": {
"file_extension": ".joy",
"mimetype": "text/plain",
"name": "Joy"
}
},
"nbformat": 4,
"nbformat_minor": 5
}