{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyP/f3oLHI9gbYupphFqO3wd",
"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": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# Universal Programs\n",
"\n",
"This lesson will teach you about universal programs:\n",
"these are programs that take $\\mathtt{1\\#}$ programs as inputs and run\n",
"them. \n",
" \n"
],
"metadata": {
"id": "5ju1mn7Tur1V"
}
},
{
"cell_type": "markdown",
"source": [
"To start, enter the following cell:"
],
"metadata": {
"id": "HQKACjesvE90"
}
},
{
"cell_type": "code",
"source": [
"#@title\n",
"!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 *"
],
"metadata": {
"cellView": "form",
"id": "F2axy4PUvHFm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"u = universal\n",
"u"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 571
},
"id": "4X9YC9eTu3M1",
"outputId": "974ba216-d1b7-4ba7-d1c7-6bb04e92426a"
},
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'1#####1###11###11####111111#1#####111###111111###1111111###11111#####1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111###1###1####111#111111111####111##1#####1111###11###11111###111#11111#####1111111111111111111111111111111111111111111111###111111111###111##1#####1111###11###11111###111#11111#####1111111111111111111111111111111111111###111111111###111##1#####1111###11###11111###111#11111#####1111111111111111111111111111111111111111111111###111111111###111##1#####1111###11###11111###111#11111#####111111111111111111111111111111111111111111111111111111###111111111###111##1#####1111###11###11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111###111#11111#####1111111111###1###111#####111111###111###1111##1111####1111#111111####11111111111111111111111111111111111111111111111111111111111111####111#####1###11###1111###11111#1111#111111####11111##1111##111#####1111111###1111###1111##11111##11111####1111#1111111####111111111111111111111111111111111111111111111111111111111111111111111111111###111#####1###11###11111###11111#1111#111111#1111111####1111##111#####111111###111###1111##1111####1111#111111####11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111####111#####1###11###1111###1111#11111#111111####11111#11111#1111##111#####111111###111###1111##1111####1111#111111####11111#####11111111###11###1###111111#####111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111###1###1###11111111####111111#####1111###111#11111#1111####111#####111###111111#111####1#####111111###111###111##1111####111#111111####1111#####111111###111###1##1111####1#111111####111#####111111###111###1##1111####1#111111####111111111111111111111111111111111111111111111111111111111####11#####111###11111###111111111111###11##11##111111####111#11#####1###111###111##11###111#11111111111111####111##11#####11111111111111111111111111111111111111111111111111111111111111111111###1###111##11111#####1###1111111111111111111111####11111#####1111111111111111111111111111###111111111111111111111111111###11111#####111###11###111111111111111111111111111111111111111111111###11#####111111111111111111###111111111###111#11#####1###1###111#111##111##111111111111111111111111111111111111111111111111111111111###111#11#####1###111###111##11###111#111111111111111111####111#111#1111111111111111111111111111111111111111111111###11#####111111111111111111###111111111###111#11#####1###1###111##111##111##11111111111111111111111111111111111###111#11#####1###111###111##11###111#111111111111111111####111#111##111111111111111111111111###11111#####1###1###11111#####1###1###11#####11111111111111111111111###111###111##1111111111111###11#####1###11111###1###11111#111111#111111###11111#11111#111111#111111#1###11#####111111###111###111##1111####111#111111####111#####111111###111###11##1111####11#111111####1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111####11#####1###1###11#####1###1###11#####11111111111111###11###11111111###11#####1###111###1#11111111####1##1111111111####11#####111###11####111####111111#####11###11####1111#####111###11####111####'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 2
}
]
},
{
"cell_type": "markdown",
"source": [
"````{prf:definition}\n",
":label: my-definition\n",
"\n",
"A word $u$ is *universal* if\n",
"\n",
"$$\\phifn_u(x) \\simeq \\phifn_x(\\ )\n",
"$$\n",
"for all words $x$.\n",
"\n",
"This means that if running $x$ on all empty registers\n",
"eventually gives some output in R1 (and all other registers empty),\n",
"then that same output would be the result of running $u$ with $p$\n",
"in R1 and all other registers empty.\n",
"And in the other direction, if running $x$ with\n",
"all empty registers gives some output, say $y$,\n",
" in R1 (and all registers \n",
"empty\n",
"at the end), then we would get the same output $y$ in R1 \n",
"when we run $u$ with $x$ in R1 and all other registers empty.\n",
"````\n",
"\n",
"\n"
],
"metadata": {
"id": "FgONLO1KvnKn"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"Here is a specific example of this. Try running u with 1# 1## in\n",
"R1. The result is 1#, and this is what happens when we run 1# 1## on all\n",
"empty registers. So the universal program u takes 1# 1## and\n",
"simulates a run of that program.
For another example,\n",
" suppose we start u with self in R1 \n",
"(and all other registers empty). You can check for yourself that\n",
"we eventually get self in R1. \n",
"(Note: the computation might take several minutes.) \n",
"This confirms the basic\n",
"property because running self with all registers empty\n",
"gives self. \n",
"
\n",
"
\n",
"We can even run u on programs that contain u itself.\n",
"For this, we return to our first example, concerning the word 1# 1##.\n",
"As we know, \n",
"
\n",
"Recall also that \n",
"
\n",
"It follows that \n",
"
\n",
"And from this we conclude that \n",
"
\n", "This is something you can try for yourself. \n", "\n" ], "metadata": { "id": "TQkSinvCvOPp" } }, { "cell_type": "markdown", "source": [ "\n", "### The plan for this lesson\n", "\n", "
\n",
"The goal of this lesson is for you to write a universal program\n",
"yourself. The next sections guide you, using a series of exercises.\n",
"
\n",
"
\n", "We hasten to make two remarks: first of all, it would be more\n", "instructive for you to forget about the rest of this lesson \n", "entirely and to just write your own universal program. But this\n", " most readers would probably appreciate the hints and will find\n", "enough of a challenge in the construction of a long working program.\n", "Second, the sections to come do not guide you to write a program thatt\n", "is exactly like u above. The u \n", "above was designed to be as short as possible.\n", "In fact, we leave you with a challenge: write a universal\n", "program with the smallest number of instructions. The one above has \n", "300." ], "metadata": { "id": "ff5wd-MKyn9C" } }, { "cell_type": "markdown", "source": [ "### Simplification\n", "\n", "We are going to simplify the requirements in order to make\n", "it easier to write a universal program. So we'll outline how\n", "to write a program that is close to a universal program,\n", "but not quite a universal program.\n", "
\n", "We'll write a program $u$ that acts like a universal\n", "program, but only for programs which use only R1, R2, and R3.\n", "In more detail:\n", " \n", "Let $p$ be any\n", "program of $\\mathtt{1\\#}$ which only uses R1, R2, and R3.\n", "Let $x$ be any word on our alphabet $\\{\\mathtt{1},\\mathtt{\\#}\\}$,\n", "including possibly the empty word. Then the following are equivalent: \n", "\n", "(A')\n", "If $p$ is started with all empty registers, then eventually we halt\n", "with $x$ in R1. \n", "\n", "(B') If $u$ is started with $p$ in R1 and with the empty word \n", "in all other\n", "registers, then eventually we halt with $x$ in R1. \n", "\n", "Once again, the work of this section sketches a construction of \n", "such a program $u$. You will need to do much of the work yourself\n", "in actually getting the program from the sketch. And even when we\n", "have $u$, it will not be quite what we want for this lesson overall,\n", "because $u$ will only behave right on programs which use the first\n", "three registers. \n", "Later on, we'll come\n", "back and drop this simplification to get a program which is truly \n", "universal.\n", "\n", "At this point, we are ready to sketch a method for you to\n", "write a universal program subject to this simplification.\n", "\n", "We are going to use registers as follows:\n", "
R1: the input program p |
R2: an instruction number 1n\n", " | \n", "
R3: the nth instruction of p \n", " |
R4: the contents of R1 |
R5: the contents of R2 |
R6: the contents of R3 |
\n",
"That is, we are going to simulate the \n",
"computation of a register machine on a program $p$ by\n",
"using six registers. Our universal program $u$ is \n",
"what does the simulation, and it works step-by-step. That is,\n",
"$u$ mimics what a register machine would really do.\n",
"The only difference is that one step of a real register machine\n",
"would be done by *many* steps of our universal program.\n",
"\n",
"\n",
"The registers above hold what is sometimes called a snapshot\n",
"of a register machine run. \n",
"This means that they hold all of the relevant information about a register\n",
"machine computation at one particular point in time: they tell what program is\n",
"running, which instruction would be run next and what number instruction it is,\n",
"and also the contents of all of the registers that the program is using.\n",
"Now a real register machine goes from snapshot to snapshot in one step,\n",
"but the universal machine that we are building will take many steps to\n",
"go from one snapshot to the next. \n",
"
\n", "Our universal program is composed of a few sub-programs.\n", "We shall illustrate the ideas behind several sub-programs by making a\n", "large chart. This chart shows just one example\n", "of how a universal program could work, when we take\n", "$p$\n", "to be the prorgram
\n", "Now to make life easier, we'll write out this program and indicate\n", "the instruction numbers above them:\n", "
1 | \n", "2 | \n", "3 | \n", "4 | \n", "5 | \n", "6 | \n", "7 | \n", "8 | \n", "9 | \n", "
1## | \n", "1##### | \n", "111111111### | \n", "11111### | \n", "11# | \n", "11## | \n", "11## | \n", "111111#### | \n", "11# | \n", "
10 | \n", "11 | \n", "12 | \n", "13 | \n", "14 | \n", "15 | \n", "16 | \n", "17 | \n", "18 | \n", "
11## | \n", "111111111#### | \n", "11##### | \n", "111111### | \n", "111### | \n", "1## | \n", "1111#### | \n", "1# | \n", "111111#### | \n", "
\n",
"What we are going to show is tables of what the various\n",
"registers show after different parts of a run of a universal program\n",
"u on this program $\\mathtt{1\\#\\#}$ + write.\n",
"We hasten to add that the universal program exhibited above\n",
"does not conform to the tables. (This is because\n",
"registers work differently in it. Also, the program\n",
"above is designed to work even without our simplifying\n",
"assumption.)\n",
"
\n",
"Here are the tables; we'll have some words below about them.\n",
"
\n",
" \n",
"
\n",
" \n",
" R1: the input program p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" \n",
" R2: an instruction number 1n\n",
" \n",
" \n",
" 1 \n",
" 1 \n",
" 11 \n",
" 11 \n",
" 11111 \n",
" 11111 \n",
" 16 \n",
" 16 \n",
" 17 \n",
" 17 \n",
" 18 \n",
" 18 \n",
" \n",
" R3: the nth instruction of p \n",
" \n",
" \n",
" 1## \n",
" \n",
" 1##### \n",
" \n",
" 11# \n",
" \n",
" 11## \n",
" \n",
" 11## \n",
" \n",
" 16 #### \n",
" \n",
" R4: the contents of R1 \n",
" \n",
" \n",
" \n",
" # \n",
" # \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" R5: the contents of R2 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 1 \n",
" \t1 \n",
" 1# \n",
" \t1# \n",
" 1## \n",
" 1## \n",
" \n",
"R6: the contents of R3 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"
\n",
"continuing\n",
"
\n",
" \n",
"
\n",
" \n",
" R1: the input program p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" \n",
" R2: an instr number 1n\n",
" \n",
" 11 \n",
" 11 \n",
" 111 \n",
" 111 \n",
" 112 \n",
" 112 \n",
" 114 \n",
" 114 \n",
" 117 \n",
" 117 \n",
" 118 \n",
" 118 \n",
" \n",
" R3: the nth instruction of p \n",
" \n",
" 1##### \n",
" \n",
" 19 ### \n",
" \n",
" 11##### \n",
" \n",
" 111### \n",
" \n",
" 1# \n",
" \n",
" 16 #### \n",
" \n",
" R4: the contents of R1 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 1 \n",
" 1 \n",
" \n",
" R5: the contents of R2 \n",
" 1## \n",
" 1## \n",
" 1## \n",
" 1## \n",
" 1## \n",
" 1## \n",
" 1## \n",
" \t1## \n",
" ## \n",
" \t## \n",
" ## \n",
" ## \n",
" \n",
"R6: the contents of R3 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"
\n",
"and going further in the move2,1 part\n",
"at the end of write\n",
" \n",
"
\n",
" \n",
" R1: the input program p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" \n",
" R2: an instruction number 1n\n",
" \n",
" 112 \n",
" 112 \n",
" 115 \n",
" 115 \n",
" 116 \n",
" 116 \n",
" 112 \n",
" 112 \n",
" 115 \n",
" 115 \n",
" 116 \n",
" 116 \n",
" \n",
" R3: the nth instruction of p \n",
" \n",
" 11##### \n",
" \n",
" 1## \n",
" \n",
" 14#### \n",
" \n",
" 11##### \n",
" \n",
" 1## \n",
" \n",
" 14#### \n",
" \n",
" R4: the contents of R1 \n",
" 1 \n",
" 1 \n",
" 1 \n",
" 1 \n",
" 1# \n",
" 1# \n",
" 1# \n",
" 1# \n",
" 1# \n",
" 1# \n",
" 1## \n",
" 1## \n",
" \n",
" R5: the contents of R2 \n",
" ## \n",
" ## \n",
" # \n",
" # \n",
" # \n",
" # \n",
" # \n",
" \t# \n",
" \n",
" \t \n",
" \n",
" \n",
" \n",
"R6: the contents of R3 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"
\n",
"
\n",
"and concluding\n",
"
\n",
" \n",
"
\n",
" \n",
" R1: the input program p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" p \n",
" 1## \n",
" \n",
" R2: an instruction number 1n\n",
" \n",
" 112 \n",
" 112 \n",
" 113 \n",
" 113 \n",
" 119 \n",
" \n",
" \n",
" R3: the nth instruction of p \n",
" \n",
" 11##### \n",
" \n",
" 16 ### \n",
" \n",
" \n",
" \n",
" R4: the contents of R1 \n",
" 1## \n",
" 1## \n",
" 1## \n",
" 1## \n",
" 1## \n",
" \n",
" \n",
" R5: the contents of R2 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"\n",
" \n",
"R6: the contents of R3 \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"
\n",
"As you can see, there are four different colors of the columns.\n",
"The colors indicate the sub-programs of u.\n",
"The columns themselves indicate the register contents when one\n",
"of the sub-programs begins.\n",
"
\n",
"The first color shown is orange.\n",
"This is the the easiest to explain. The orange sub-program is 11#.\n",
"So it adds a 1 to R2, indicating that at the\n",
"very beginning of the execution of\n",
"the input program, the instruction to look at first is instruction 1.\n",
"
\n",
"The beige columns alternate with the green ones.\n",
"The purpose of the beige program is to take the input program\n",
"(in R1) and the next instruction number (R2), and to\n",
"look up the appropriate \n",
"instruction and put it into R3. This is similar to a program\n",
"which we saw earlier on. But we need to be sure that the contents\n",
"of R1 and R2 are not destroyed at the end. (So you will probably need\n",
"to use registers beyond R6 for all of this.) The way to understand\n",
"the beige columns is that they tell the contents of the various registers\n",
"at the beginning of the operation of the \"beige\n",
"sub-program\". You will need to write that sub-program.\n",
"It is a fairly straightforward modification of a program which you\n",
"already have seen, the one that gives the nthe instruction of\n",
"an input program. But there are some differences: you need to be\n",
"sure that the output goes in R3, that the input is preserved, and that\n",
"if the contents of R2 is one more than the length of the input program in R1,\n",
"then the program \"knows\" about this and transfers over to the yellow sub-program\n",
"shown at the end.\n",
"
\n",
"The main action of the steps is done in the green columns;\n",
"more precisely, \n",
"the columns shown are the beginnings of the\n",
"\"green\" sub-program, and then the results of that sub-program\n",
"are the beige columns which follow.\n",
"The exact action depends on what kind of instruction is in R3 in \n",
"the given green column. For example, if R3 has 11#, then\n",
"in the next beige column we add a # to R5 (not R2: it is R5\n",
"that models what is in R2 of the real machine when we run \n",
"program). We also must add 1 to R2 in simulating the execution\n",
"of 11#, since after 11#, we go to the very next instruction.\n",
"This is how the green columns work when we execute an instruction\n",
"11#. The other cases are left for you to think about.\n",
"Note also that each time the green sub-program runs, R3 is emptied \n",
"at the end.\n",
"
\n",
"The last column color is the yellow one at the very end.\n",
"Before the yellow one starts up, the beige program\n",
"has determined (in some sense) that \n",
" our program $p$ has 18 instructions and we have \n",
" come to a point where we ask for instruction 19. Thus, \n",
" the input program has halted. We therefore want to\n",
" take the contents of R1 as run by the input program\n",
" (this is the word in R4), and move this to R1.\n",
" Also, we would like to clear out the contents of\n",
" whichever registers are not empty. We do this so that \n",
" the program we are writing will itself come to a good halt\n",
" on its input.\n",
"
\n",
"
\n",
"It might seem silly to have R1 unchanged throughout.\n",
"But this isn't really what is happening at all.\n",
"In the course of the beige subprograms, parts of R1 are copied\n",
"to other registers, and the copied back. So at the \n",
"end of the beige sub-programs, we have the original\n",
"program $p$ back in R1. \n",
"
\n", "Similarly, it might seem weird to have R6 completely unused\n", "in running a universal program on $p$. This is due\n", "to the fact that our program $p$ only uses R1 and R2.\n", "If we worked with an example program that used R3, then R6\n", "would not have remained empty in the tables.\n", "\n", "\n", "\n", "\n" ], "metadata": { "id": "rDOKl29NwPvA" } }, { "cell_type": "markdown", "source": [ "## Coding unboundedly many registers into one\n", "\n", "The simplified form of the universal program that we just saw could be modified to work on programs with any fixed finite number of registers. But we want one universal programs that works on *all* programs, without a pre-established bound. For this, we need a new idea. \n", "\n", "We now call upon a technique introduced in a separate notebook in this course, the technique of [two-by-two encoding](two_by_two.ipynb). That notebook shows how we can add an *end of register marker* to R1, and the work with a simple encoding of symbols by pairs. We took the end of register marker to be ```##```, and we encoded ```1``` by ```11``` and ```#``` by ```1#```. \n", "\n", "All of that work was for R1 alone. But we could do the same work for all of the other registers. \n", "\n", "Following this, the idea is to simply run all of the encoded registers into one. \n", "\n" ], "metadata": { "id": "Vq9XIPJ-3JVP" } }, { "cell_type": "markdown", "source": [ "For an example of what we mean, suppose that we were dealing with the following snapshot:\n", "\n", "
R1: 11# | \n", "R2: | \n", "R3: ## | \n", "R4: | \n", "R5: | \n", "R6: 1 | \n", "R7: # | \n", "R8: ##1 | \n", "
Contents | \n", "R1: 11# | \n", "R2: | \n", "R3: ## | \n", "R4: | \n", "R5: | \n", "R6: 1 | \n", "R7: # | \n", "R8: ##1 | \n", "
Encoded as | \n", "11111### | \n", "## | \n", "R3:## | \n", "## | \n", "## | \n", "11## | \n", "1# ## | \n", "1#1#11## | \n", "
\n",
"
\n",
"
\n", "Here is what each group needs to do:\n", "
\n",
"The way I have described things makes the green sub-programs\n",
"a little harder than they have to be. That is, if we have an instruction in \n",
"R3, say 111###, the first thing to do would seem to be to work with it\n",
"in order to decide which kind of instruction it is. (In this case, \n",
"the instruction is\n",
"a transfer instruction, of course.) Now you certainly can write the \n",
"green sub-program to start out by parsing the instruction in R3\n",
"and then working on a case-by-case basis. However, this is not\n",
"the only way to do things. If you are careful with the\n",
"beige program, the one that gives the nth instruction in the\n",
"input program, then the parsing could be done automatically.\n",
"What I mean here is that the beige program could be written in \n",
"such a way that it incorporates the parsing as it goes.\n",
"
\n",
"
\n",
"If you are successful in getting a beige program that does what\n",
"we want, then this would be much more work\n",
"than all of the rest combined. \n"
],
"metadata": {
"id": "hFWesdv5xu3j"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"### A remark on the beige and green sub-programs\n",
"\n",
"The way I have described things makes the green sub-programs\n",
"a little harder than they have to be. That is, if we have an instruction in \n",
"R3, say 111###, the first thing to do would seem to be to work with it\n",
"in order to decide which kind of instruction it is. (In this case, \n",
"the instruction is\n",
"a transfer instruction, of course.) Now you certainly can write the \n",
"green sub-program to start out by parsing the instruction in R3\n",
"and then working on a case-by-case basis. However, this is not\n",
"the only way to do things. If you are careful with the\n",
"beige program, the one that gives the nth instruction in the\n",
"input program, then the parsing could be done automatically.\n",
"What I mean here is that the beige program could be written in \n",
"such a way that it incorporates the parsing as it goes.\n",
"
\n",
"
\n",
"If you are successful in getting a beige program that does what\n",
"we want, then this would be much more work\n",
"than all of the rest combined. "
],
"metadata": {
"id": "b_zZ3TYc0yJd"
}
},
{
"cell_type": "markdown",
"source": [
"The group overall will need to turn in a program along with an explanation of\n",
"the various parts. You should say clearly what the parts do, and also give some\n",
"examples of how they run. If you have made flowcharts, it would be good\n",
"to hand those in as well. \n",
"\n",
"You should test your overall program. It would be good to run\n",
"your program on 1#, on diag, and on 1#1## + u.\n",
"\n",
" At various points, you will be tempted to worry about what would happen\n",
"if the input program does not halt. (For example, what should your\n",
"program do if the input program is just 1111#### ?)\n",
"Strictly speaking, the universal program should itself diverge on this input.\n",
"But for our purposes, it's fine to ignore this point.\n",
"You need not worry about this at all, only about the case\n",
"when the input program really does halt. \n",
"\n",
"Above, we worked with the simplifying assumption that\n",
"the input program only uses R1, R2, and R3. You should get a program\n",
"*u* that woks on all input programs. \n",
"\n",
"Your final draft should say who did what.\n",
"\n",
"It is important to write well. Your goal should be to write something that could be understood by\n",
"someone else in the class. \n"
],
"metadata": {
"id": "V7o1O9esyQM6"
}
},
{
"cell_type": "markdown",
"source": [
""
],
"metadata": {
"id": "EPtY2M-hzToK"
}
},
{
"cell_type": "markdown",
"source": [
"## Implementing add# instructions\n",
"\n",
"To see how some of this would work, we want to discuss how add1 instructions like 1##, 11##, etc. are implemented.\n",
"\n",
"When we execute such a instruction, we'll assume as above that R4 contains the encoded contents of all the registers. We'll also assume that R5 contains a number in unary, call it m. We want a sub-program that will update R4 by adding 1ยง# onto the end of the mth encoded register. Here is such a program:\n",
"\n",
"11111##### 1### 1### 1### 1111##### 1111111111111111### 1111111### 111111## 1111##### 1### 1### 111111## 111111111111### 111111# 1111##### 1### 111### 111111## 11111111111111#### 111111# 1111111111111111#### 111111## 111111## 1### 11111##### 111111111### 1### 111111##### 111111111111111111111111#### 111### 1111111## 1111#### 1111111# 111111#### 111111##### 111111### 11111111### 111111##### 1### 1### 1### 1111111# 1111111## 111111111### 1111111# 111111##### 1### 111### 1111111## 111111111111111#### 1111111# 11111111111111111#### 1111111## 1111111## 1111 ##### 111111### 111### 1111111## 1111#### 1111111# 111111#### 1111111 ##### 111111### 111### 1111## 1111#### 1111# 111111####\n",
"\n",
"You should try it out to make sure that it works right. Some good test cases would include the case when R4 is empty, as it would be in the beginning of a run.\n",
"\n",
"So far, we have described how add# instructions would be implemented in a universal program. All of the same work goes for add1 instructions, of course, with just a few changes.\n",
"\n",
"It would take a bit more work to handle the case instructions, but again, this is pretty similar.\n"
],
"metadata": {
"id": "Sp_zvxk7z4RK"
}
},
{
"cell_type": "markdown",
"source": [
"# Universal programs of more than one argument"
],
"metadata": {
"id": "E-EX6Y880HB1"
}
},
{
"cell_type": "markdown",
"source": [
"```{exercise}\n",
"What happens if we run the universal program $u$ on the self-writing program $\\self$?\n",
"\n",
"Incidentally, running $u$ on $\\self$ takes 22,376,608 steps, whereas running $\\self$ on empty registers takes 3,322.\n",
"```\n"
],
"metadata": {
"id": "qJo6UhZO1r14"
}
},
{
"cell_type": "markdown",
"source": [
"```{exercise}\n",
"Write a program ```trade``` which looks like it trades places with its input in R1. (Of course, a program cannot literally trade places with its input. But here is what we mean: running trade with p in R1 and all other registers empty does the same thing as running p with trade in R1 and all other registers empty.)\n",
"\n",
"[Hint: You will need a universal program, and also an application of the Recursion Theorem.]\n",
"```"
],
"metadata": {
"id": "btoU-e9g1rti"
}
},
{
"cell_type": "markdown",
"source": [
"```{exercise}\n",
"Write a program ```self1``` which writes itself to R1 but only uses R1 itself (no other registers). \n",
"This exercise requires a trick, since there is no program like ```diag``` which only uses one register.\n",
"\n",
"[Hint: you will need the technique of coding several registers into R1.]\n",
"```"
],
"metadata": {
"id": "aDG-w5LO1ri3"
}
}
]
}