{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preamble\n",
    "\n",
    "First, import what we need."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joy.joy import run\n",
    "from joy.library import initialize\n",
    "from joy.utils.stack import stack_to_string\n",
    "from joy.utils.pretty_print import TracePrinter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a dictionary, an initial stack, and two helper functions to run Joy code and print results for us."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "D = initialize()\n",
    "S = ()\n",
    "\n",
    "\n",
    "def J(text):\n",
    "    print(stack_to_string(run(text, S, D)[0]))\n",
    "\n",
    "\n",
    "def V(text):\n",
    "    tp = TracePrinter()\n",
    "    run(text, S, D, tp.viewer)\n",
    "    tp.print_()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run some simple programs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "41\n"
     ]
    }
   ],
   "source": [
    "J('23 18 +')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "J('45 30 gcd')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### With Viewer\n",
    "\n",
    "A `viewer` records each step of the evaluation of a Joy program.  The `TracePrinter` has a facility for printing out a trace of the evaluation, one line per step.  Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression (\"continuation\") on the right.  I find these traces beautiful, like a kind of art."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      • 23 18 +\n",
      "   23 • 18 +\n",
      "23 18 • +\n",
      "   41 • \n"
     ]
    }
   ],
   "source": [
    "V('23 18 +')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  • 45 30 gcd\n",
      "                               45 • 30 gcd\n",
      "                            45 30 • gcd\n",
      "                            45 30 • 1 [tuck modulus dup 0 >] loop pop\n",
      "                          45 30 1 • [tuck modulus dup 0 >] loop pop\n",
      "   45 30 1 [tuck modulus dup 0 >] • loop pop\n",
      "                            45 30 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         30 45 30 • modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                            30 15 • dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         30 15 15 • 0 > [tuck modulus dup 0 >] loop pop\n",
      "                       30 15 15 0 • > [tuck modulus dup 0 >] loop pop\n",
      "                       30 15 True • [tuck modulus dup 0 >] loop pop\n",
      "30 15 True [tuck modulus dup 0 >] • loop pop\n",
      "                            30 15 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         15 30 15 • modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                             15 0 • dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                           15 0 0 • 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         15 0 0 0 • > [tuck modulus dup 0 >] loop pop\n",
      "                       15 0 False • [tuck modulus dup 0 >] loop pop\n",
      "15 0 False [tuck modulus dup 0 >] • loop pop\n",
      "                             15 0 • pop\n",
      "                               15 • \n"
     ]
    }
   ],
   "source": [
    "V('45 30 gcd')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's a longer trace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  • 96 27 gcd\n",
      "                               96 • 27 gcd\n",
      "                            96 27 • gcd\n",
      "                            96 27 • 1 [tuck modulus dup 0 >] loop pop\n",
      "                          96 27 1 • [tuck modulus dup 0 >] loop pop\n",
      "   96 27 1 [tuck modulus dup 0 >] • loop pop\n",
      "                            96 27 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         27 96 27 • modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                            27 15 • dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         27 15 15 • 0 > [tuck modulus dup 0 >] loop pop\n",
      "                       27 15 15 0 • > [tuck modulus dup 0 >] loop pop\n",
      "                       27 15 True • [tuck modulus dup 0 >] loop pop\n",
      "27 15 True [tuck modulus dup 0 >] • loop pop\n",
      "                            27 15 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         15 27 15 • modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                            15 12 • dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         15 12 12 • 0 > [tuck modulus dup 0 >] loop pop\n",
      "                       15 12 12 0 • > [tuck modulus dup 0 >] loop pop\n",
      "                       15 12 True • [tuck modulus dup 0 >] loop pop\n",
      "15 12 True [tuck modulus dup 0 >] • loop pop\n",
      "                            15 12 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         12 15 12 • modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                             12 3 • dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                           12 3 3 • 0 > [tuck modulus dup 0 >] loop pop\n",
      "                         12 3 3 0 • > [tuck modulus dup 0 >] loop pop\n",
      "                        12 3 True • [tuck modulus dup 0 >] loop pop\n",
      " 12 3 True [tuck modulus dup 0 >] • loop pop\n",
      "                             12 3 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                           3 12 3 • modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                              3 0 • dup 0 > [tuck modulus dup 0 >] loop pop\n",
      "                            3 0 0 • 0 > [tuck modulus dup 0 >] loop pop\n",
      "                          3 0 0 0 • > [tuck modulus dup 0 >] loop pop\n",
      "                        3 0 False • [tuck modulus dup 0 >] loop pop\n",
      " 3 0 False [tuck modulus dup 0 >] • loop pop\n",
      "                              3 0 • pop\n",
      "                                3 • \n"
     ]
    }
   ],
   "source": [
    "V('96 27 gcd')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
