{
 "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
}
