{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyMnlai2G8/WeEZPg+0i2iAb", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Three simple programs\n" ], "metadata": { "id": "30SjDScX1Du5" } }, { "cell_type": "markdown", "source": [ "We have seen the syntax of commands of 1# in \n", "[a previous notebook](syntax-guide). Now we present some of the simplest programs in the language, programs which we will see again and again in what follows.\n", "\n", "\n" ], "metadata": { "id": "2Do-w0oT6zny" } }, { "cell_type": "markdown", "source": [ "## Move\n", "\n", "A good way to learn about the different commands is to examine simple programs. Among these 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", "You can try the program out by (a) moving the interprer down, using the appropriate commands in the notebook; (b) entering ```move_2_1``` as the program and some random words in R1 and R2 and then running ```move_2_1``` on those inputs.\n", "\n", "Since it is hard to understand a program of 1#, we have tools to help. First, we can *parse* the program. Parsing means dividing the program into instructions." ], "metadata": { "id": "mtraRus17Ygp" } }, { "cell_type": "markdown", "source": [ "The program move_2_1 is a loop, and we can further add to the explanations in the chart." ], "metadata": { "id": "yuwmZeA87gTk" } }, { "cell_type": "markdown", "source": [ "Even better, we can get an parse with glosses, as follows:" ], "metadata": { "id": "WSmD6lOS7fdr" } }, { "cell_type": "code", "source": [ "#@title\n", "j = [['11#####', 'cases on R2', ],\n", " ['111111###', \"register 2 is empty: go forward 6 to instruction 8 (we're done)\"],\n", " ['111###', 'first symbol is a 1: go forward 3 to instruction 6 (to the tan section)'],\n", " ['1##', 'first symbol is a #: add # to R1'],\n", " ['1111####', 'go backward 4 to instruction 1 (to the top)'],\n", " ['1#', 'add 1 to R1'],\n", " ['111111####', 'go backward 6 to instruction 1 (to the top)']\n", "]\n", " \n", "df = pd.DataFrame(j,columns=[\"instruction\", 'explanation'])\n", "df.index = np.arange(1, len(df) + 1)\n", "df.style.set_properties(**{'border': '1.3px solid green',\n", " 'color': 'magenta'})\n", "n = len(df.columns)\n", "df.style.set_properties(**{'text-align': 'left'})\n", "#df.style.apply(lambda x: [\"background-color: red\"]*n if x['instruction']== 'Reading' else [\"background-color: white\"]*n, axis = 1)\n", "#df.style.apply(lambda x: [\"background-color: #B0E0E6\"]*n if x['instruction'] in ['1##','1111####'] elif [\"background-color: #D4B48C\"]*n if x['instruction'] in ['1#','111111####'] else [\"background-color: #FFFFCC\"]*n, axis = 1)\n", "df.style.apply(lambda x: [\"background-color: #B0E0E6\"]*n if x['instruction'] in ['1##','1111####'] else [\"background-color: #FFFFCC\"]*n, axis = 1)\n", "df.style.apply(lambda x: \n", " [\"background-color: #B0E0E6\"]*n if x['instruction'] in ['1##','1111####'] \n", " else [\"background-color: #D4B48C\"]*n if x['instruction'] in ['1#','111111####']\n", " else [\"background-color: #FFFFCC\"]*n, axis = 1)\n" ], "metadata": { "id": "W8JtWwdD7Kmy" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "\n", "\n", "If R2 is empty, it goes to line 8. Since the program itself only has 7 lines, this means that we have transferred *out* of the program. We say that the program *halts* at that point.\n", "\n", "If the first symbol of R2 is a 1, then the second instruction after the case instruction at the top transfers us down to line 6. This part of the program would then add a 1 to R1 and return to the very beginning of the program.\n", "\n", "If the first symbol of R2 is a #, then we delete that # and go three steps forward, to line 4. This part of the program would then add a # to R1 and return to the very beginning of the program.\n", "\n", "The point is that by going around loop repeatedly, we transfer the contents of R2 symbol-by-symbol to R1.\n", "Similarly, whenever m and n are different numbers, we can build a program ```move_m_n```. This program would write the contents of Rm onto the end of Rn, emptying Rm in the process.\n", "\n", "\n", "\n" ], "metadata": { "id": "gBSBWLVZ7pkj" } }, { "cell_type": "markdown", "source": [ "## Modifying our simple loop\n", "\n", "Suppose we want to modify ```move_2_1``` to get ```move_3_4```, a program which would copy the contents of R3 onto the end of R4 (and empty R4) in the process.\n", "Here is a way to do this which shows off some command-line tools that are part of the working environment of this course." ], "metadata": { "id": "N0YwW7Fk7zIA" } }, { "cell_type": "code", "source": [ "parse(move_2_1)" ], "metadata": { "id": "DJCdDJew1I-i" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "pre_program = ['111#####', '111111###', '111###', '1111##', '1111####', '1111#', '111111####']" ], "metadata": { "id": "SbKYTUWM7-mp" } }, { "cell_type": "code", "source": [ "unparse(pre_program)" ], "metadata": { "id": "4GtMpbBG8DJR" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "We can check this out by entering it into the interpreter. We could either copy the output line (without the quotes), and go up to the top of this notebook. Alternatively, we could move the interpreter down to here using an up-arrow command that you will need to find." ], "metadata": { "id": "vf5BQNtK8F88" } }, { "cell_type": "markdown", "source": [ "### Exercise 5\n", "\n", "Write a program which takes the contents of R1 and adds them to the ends of *both* R2 and R3.\n" ], "metadata": { "id": "nXot9ZVc8KJt" } }, { "cell_type": "markdown", "source": [ "### Exercise 6\n", "\n", "Write a program that clears out R1, leaving it empty.\n" ], "metadata": { "id": "rV-jrnJC8NfQ" } }, { "cell_type": "markdown", "source": [ "### Exercise 7\n", "\n", "Write a program that clears R3 and then swaps the contents of R1 and R2 (using the now-empty R3).\n" ], "metadata": { "id": "6eI-A53p8Q2F" } }, { "cell_type": "markdown", "source": [ "### Exercise 8 \n", "\n", "Write a program p that adds a 1 to the *beginning* of R1, assuming that R2 is empty. (For example, if R1 has ```##1``` to start, then running p would result in R1 having ```1##1```.) Of course, your program may use R2!" ], "metadata": { "id": "JXdWau8S8VLb" } }, { "cell_type": "code", "source": [], "metadata": { "id": "q_aEWF3y1CnB" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "TwUW0fhV1Ba5" }, "outputs": [], "source": [] } ] }