{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "8C7LMBIh5Hmr"
},
"source": [
"# Basic programs\n",
"\n",
"We have seen the syntax of ```1#``` instructions \n",
"[in a previous notebook](instructions.ipynb).\n",
"We turn to the simplest programs in the language."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZUEj49fx7gDq"
},
"outputs": [],
"source": [
"!python -m pip install -U setuptools\n",
"!python -m pip install -U git+https://github.com/lmoss/onesharp.git@main\n",
"from onesharp.interpreter.interpreter import *"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Q1NfvunUI9ui"
},
"source": [
"## move_2_1\n",
"\n",
"The first is a program called ```move_2_1```. It is designed to move the contents of R2 onto the end of R1, emptying out R2 in the process. Written out in full it is \n",
"\n",
"```11#####111111###111###1##1111####1#111111####```\n",
"\n",
"We have defined ```move_2_1``` to be this program, and so you can run it as shown below.\n",
"\n",
"You can try the program by entering the following line. In it\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "JnrcdeAl_Kmm",
"outputId": "3f228e38-08a4-4272-c30d-0d1a03639eab"
},
"outputs": [
{
"data": {
"text/plain": [
"'11#####1##11111'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"onesharp(move_2_1, ['11#','####1##11111'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZEn2WieG_RJY"
},
"source": [
"The words in the brackets are the contents of registers 1 and 2 when we start. Again, our program moved the contents of R2 onto the end of R1, emptying R2. You should want to change the opening contents of R1 and R2 in the cell above before going on."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "0YHJpDX7_pDf"
},
"source": [
"(content:parsing)=\n",
"It is hard to understand a program of ```1#```, but we have tools to help. First, we can *parse* the program. Parsing means dividing the program into instructions."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vc_brPCuG_qG",
"outputId": "a8c37f98-ab93-451e-ae5b-29dc38ef68db"
},
"outputs": [
{
"data": {
"text/plain": [
"['11#####', '111111###', '111###', '1##', '1111####', '1#', '111111####']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"parse(move_2_1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ezY3wZ1BHCq_"
},
"source": [
"Even better, we can get an parse with glosses, as follows:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iv_DHKDpHK1A"
},
"source": [
"The program ```move_2_1``` is a loop, and we can further add to the explanations in the chart.\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 269
},
"id": "r0RbyVhEHLNl",
"outputId": "a7cdbc5d-2467-4373-f008-083cdbdbbfa1"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n", " | instruction | \n", "explanation | \n", "
---|---|---|
1 | \n", "11##### | \n", "cases on R2 | \n", "
2 | \n", "111111### | \n", "register 2 is empty: go forward 6 to instruction 8 (we're done) | \n", "
3 | \n", "111### | \n", "first symbol is a 1: go forward 3 to instruction 6 (to the tan section) | \n", "
4 | \n", "1## | \n", "first symbol is a #: add # to R1 | \n", "
5 | \n", "1111#### | \n", "go backward 4 to instruction 1 (to the top) | \n", "
6 | \n", "1# | \n", "add 1 to R1 | \n", "
7 | \n", "111111#### | \n", "go backward 6 to instruction 1 (to the top) | \n", "
\n", "Here is the idea behind copying Rm to Rn. We use\n", "an auxilliary register, say Rp. We move Rm into Rn and Rp\n", "at the same time, and then be move Rp back to Rm.\n", "Of course, when we do this, we should have Rp empty to start\n", "with. \n", "
\n", "Here is our program when m = 1, n = 2, p = 3:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "2_qToGIXEpts" }, "outputs": [], "source": [ "copy_1_2_3" ] }, { "cell_type": "markdown", "metadata": { "id": "5hyC35yaEKLl" }, "source": [ "## write" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "UDDObujTBXCl" }, "source": [ "We construct with \n", "a program $write$ with \n", "the following properties:\n", "\n", "1. When $write$ is started with $x$ in R1 \n", "and R2 empty, we eventually halt with a\n", "word $y$\n", "in R1 and all other registers empty.\n", "\n", "2. Then $y$ is a program, and running\n", "$y$ with R1 empty results in $x$ back in R1 and all\n", "other registers empty." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "In the notation from [a little later](functions), we have\n", "\n", "$$\n", "[\\]\\!](\\ ) = x\n", "$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "t6q0JLmoBjf3", "outputId": "cdcc65f6-2ef1-4794-c1a1-65e648c73b8f" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'1#####111111111###11111###11#11##11##111111####11#11##111111111####11#####111111###111###1##1111####1#111111####'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "write" ] }, { "cell_type": "markdown", "metadata": { "id": "KRZFyRqsBmnY" }, "source": [ "Here is the explicated parse:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "sz9wLnNOBo7h" }, "outputs": [], "source": [ "parse_explain(write)" ] }, { "cell_type": "markdown", "metadata": { "id": "VAUQmglUB1D5" }, "source": [ "Even more informatively, here is a table:\n", "\n", "
1##### | Cases on R1 |
111111111### | Go forward 9:\n", " to move2,1 part |
11111### | Go forward 5:\n", "to the brown part | \n", "
11# | Add 1 to R2: 1## adds # to R1 |
11## | Add # to R2 |
11## | Add # to R2 |
111111#### | Go backward 6 (to\n", "the top) |
11# | Add 1 to R2: 1# adds 1 to R1 |
11## | Add # to R2 |
111111111#### | Go backward 9\n", "(to the top) | \n", "
move2,1 | from earlier in this notebook |