Skip to content

Commit

Permalink
deploy: 915885e
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoss committed Jan 9, 2025
1 parent 652adfc commit 70caf76
Show file tree
Hide file tree
Showing 26 changed files with 209 additions and 140 deletions.
191 changes: 99 additions & 92 deletions _sources/more_programs/arithmetic.ipynb
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true,
"authorship_tag": "ABX9TyPhPjEe24JYXQSUVGBFiwOH",
"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"
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/lmoss/onesharp/blob/main/arithmetic.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "mx5eAo_aRljE"
},
"source": [
"## Programs for Arithmetic\n",
"## Programs for arithmetic\n",
"\n",
"The main point of working with 1# is that it gives an explicit treatment of some of the most important results of the theory of computation. This was the reason the language was formulated and developed. Nobody ever intended 1# to be used very widely.\n",
"The main point of working with ```1#``` is that it gives an explicit treatment of some of the most important results of the theory of computation. This was the reason the language was formulated and developed. Nobody ever intended 1# to be used very widely.\n",
"\n",
"At the same time, once we have 1# or some other programming language, it will be of some interest to check that we really can use it to do computation. This lesson is mainly a verification that you can use 1# to send a rocket to the moon. That is, it hints that one could theoretically use 1# to do any numerical computations that could be done with any computer. So this backs up the claim that our very simple language 1# is computationally complete.\n",
"At the same time, once we have ```1#``` or some other programming language, it will be of some interest to check that we really can use it to do computation. This lesson is mainly a verification that you can use 1# to send a rocket to the moon. That is, it hints that one could theoretically use 1# to do any numerical computations that could be done with any computer. So this backs up the claim that our very simple language 1# is computationally complete.\n",
"\n",
"For the most part, this lesson is optional. The specific results on arithmetic are not going to be used later. The definitions of primitive recursive function and computable function are important in computability theory. But if you already know them from other sources and are willing to believe that 1# is a Turing-complete formalism, then you should feel free to work through this lesson quickly, or even to skip it altogether. .\n",
"For the most part, this lesson is optional. The specific results on arithmetic are not going to be used later. The definitions of primitive recursive function and computable function are important in computability theory. But if you already know them from other sources and are willing to believe that ```1#``` is a Turing-complete formalism, then you should feel free to work through this lesson quickly, or even to skip it altogether. .\n",
"\n"
],
"metadata": {
"id": "mx5eAo_aRljE"
}
]
},
{
"cell_type": "code",
Expand All @@ -58,6 +42,9 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "eLajSKX_R2Gz"
},
"source": [
"# bb representation\n",
"\n",
Expand Down Expand Up @@ -103,13 +90,13 @@
"</td>\n",
"</table>\n",
"<br>\n"
],
"metadata": {
"id": "eLajSKX_R2Gz"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GJYkLkUGSmrQ"
},
"source": [
"We can't use the binary representation directly, since we don't have a symbol 0 around. So we change the 0s to #s. And after that, we turn the number around, getting the backwards binary representation. Here are the first 20 numbers again, in this form:\n",
"\n",
Expand Down Expand Up @@ -148,26 +135,28 @@
"</TABLE> \n",
"</td>\n",
"</table>\n"
],
"metadata": {
"id": "GJYkLkUGSmrQ"
}
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "1Vm2_hSaSv_x"
},
"source": [
"### Removing superfluous #s\n",
"### Removing superfluous ```#```s\n",
"\n",
"One touchy point about bb representations is that all bb representations should either be the word # or else end in a 1. Here is a program that strips off superfluous #s at the end of a word in R1. It uses the first three registers It also converts an empty R1 to a #.\n",
"One touchy point about bb representations is that all bb representations should either be the word # or else end in a ```1```. Here is a program that strips off superfluous ```#```s at the end of a word in R1. It uses the first three registers It also converts an empty R1 to a ```#```.\n",
"\n",
"```1##### 1111111111 11111111111 11111111111 11111111111 1111### 111### 11## 11### 11# 1##### 1111111111 1111111111### 1111111111### 11##### 1### 1111### 111## 11## 11111111#### 111# 11## 1111111111 1#### 11##### 1### 1111### 111## 11# 1111111111 1111111#### 111# 11# 1111111111 1111111111#### 11##### 1111111111### 1111111111### 111##### 111111### 111### 1## 1111#### 1# 111111#### 1111111111 11111111111 11111111111 11111#### 1111111111 1### 111# 111##### 111111### 111### 1## 1111#### 1# 111111#### 11### 1##```\n"
],
"metadata": {
"id": "1Vm2_hSaSv_x"
}
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "ytRP-YfJS4g6"
},
"source": [
"\n",
"\n",
Expand Down Expand Up @@ -206,46 +195,48 @@
"</TABLE> \n",
"\n",
"\n",
"Here is how the program works: as long as a 1 is seen in R1, a # is put in R2; the program then loops to the top. On encountering the first #, the program goes to the blue part: it adds a 1 in R2 and then copies the rest of R1 to the end of R2. If the original input is empty, a 1 is put in R2. And after all of this, R2 is moved back to R1. The full code is shown below.\n",
"Here is how the program works: as long as a ```1``` is seen in R1, a ```#``` is put in R2; the program then loops to the top. On encountering the first ```#```, the program goes to the blue part: it adds a ```1``` in R2 and then copies the rest of R1 to the end of R2. If the original input is empty, a ```1``` is put in R2. And after all of this, R2 is moved back to R1. The full code is shown below.\n",
"\n",
"1##### 1111111111 111### 1111111111### 11# 1##### 111111### 111### 11## 1111#### 11# 111111#### 1111### 11## 1111111111 111#### 11# 11##### 111111### 111### 1## 1111#### 1# 111111####\n",
"\n",
"\n",
"\n",
"\n"
],
"metadata": {
"id": "ytRP-YfJS4g6"
}
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "rQGxHtJEVEdy"
},
"source": [
"###Comparison\n",
"### Comparison\n",
"\n",
"Here is a program compare which takes words in R1 and R2 and decides if they are symbol-for-symbol the same. If so, it outputs a 1 in R1; if not, it leaves R1 empty.\n",
"Here is a program compare which takes words in R1 and R2 and decides if they are symbol-for-symbol the same. If so, it outputs a ```1``` in R1; if not, it leaves R1 empty.\n",
"\n",
"1##### 111111### 111111111### 11##### 1111111111 1### 1111111111### 111111#### 11##### 1111111111 11111### 111111### 11111### 11##### 111### 1111111111 111#### 1### 1##### 111### 11#### 111#### 11##### 1111### 11#### 111#### 1#\n",
"\n",
"The original contents of R1 and R2 are destroyed in the process.\n"
],
"metadata": {
"id": "rQGxHtJEVEdy"
}
]
},
{
"cell_type": "markdown",
"source": [
"<img src=\"https://github.com/lmoss/onesharp/blob/main/pianotrumpet.jpg?raw=1\" width=\"200\" height=\"160\">"
],
"metadata": {
"id": "BnRthH3zU7rR"
}
},
"source": [
"<img src=\"https://github.com/lmoss/onesharp/blob/main/pianotrumpet.jpg?raw=1\" width=\"200\" height=\"160\">"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "87sh9zNERS52"
},
"source": [
"#Addition\n",
"# Addition\n",
"\n",
"Here is a program add which adds numbers. Given m in R1 and n in R2 (represented in bb), the program computes the sum m + n (again, represented in bb) and puts it in R1. At the end, all other registers are empty (and in particular the original inputs m and n are lost).\n",
"\n",
Expand All @@ -256,24 +247,25 @@
"\n",
"\n",
"\n"
],
"metadata": {
"id": "87sh9zNERS52"
}
]
},
{
"cell_type": "markdown",
"source": [
"<img src=\"https://github.com/lmoss/onesharp/blob/main/harp.jpg?raw=1\" width=\"200\" height=\"160\">"
],
"metadata": {
"id": "yijcynmaU3Ko"
}
},
"source": [
"<img src=\"https://github.com/lmoss/onesharp/blob/main/harp.jpg?raw=1\" width=\"200\" height=\"160\">"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "ZjTIbR-QTLFN"
},
"source": [
"#Multiplication\n",
"# Multiplication\n",
"\n",
"Having seen a program for addition, we next look at multiplication.\n",
"\n",
Expand Down Expand Up @@ -386,63 +378,78 @@
"1##### 111### 11#### 111#### 11##### 111### 11#### 111#### 111##### 111### 11#### 111#### 1111##### 111111### 111### 1## 1111#### 1# 111111####\n",
"\n",
"The parts of the program that are shaded red are special to the multiplication algorithm. The point is that if we wish to write a program for something similar, we could keep the outside \"shell\" and just modify the red lines above. Your first exercise below asks you to do just that.\n"
],
"metadata": {
"id": "ZjTIbR-QTLFN"
}
]
},
{
"cell_type": "markdown",
"source": [
"<img src=\"https://github.com/lmoss/onesharp/blob/main/drum.jpg?raw=1\" width=\"200\" height=\"160\">"
],
"metadata": {
"id": "Luszku-QUrPm"
}
},
"source": [
"<img src=\"https://github.com/lmoss/onesharp/blob/main/drum.jpg?raw=1\" width=\"200\" height=\"160\">"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "ghMwz69oUbcL"
},
"source": [
"```{exercise}\n",
"Modify the multiplication program to compute exponentiation. This would be a program exp with the following property: when started with the bb representation of m in R1 and n in R2, the program eventually halts with the bb representation of mn in R1.\n",
"Modify the multiplication program to compute exponentiation. This would be a program ```exp``` with the following property: when started with the bb representation of m in R1 and n in R2, the program eventually halts with the bb representation of mn in R1.\n",
"```"
],
"metadata": {
"id": "ghMwz69oUbcL"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "S1Ikp4jAUayy"
},
"source": [
"```{exercise}\n",
"In our work on multiplication, we assumed addition and worked with it to get multiplication. Now forget about our program for addition, and using only the program ```succ``` for the successor function, construct a program for addition.\n",
"```"
],
"metadata": {
"id": "S1Ikp4jAUayy"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wJj8FgM6Ub9e"
},
"source": [
"```{exercise}\n",
"Write a program which, when started with n in bb in R1, gives n! in R1. [Here n! is the factorial function. Remember that 0! = 1 and (n+1)! = (n+1) x n!.]\n",
"```\n"
],
"metadata": {
"id": "wJj8FgM6Ub9e"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_V4TPS-_Un-v"
},
"source": [
"```{exercise}\n",
"Write two programs which convert back and forth from unary to binary representation.\n",
"```"
],
"metadata": {
"id": "_V4TPS-_Un-v"
}
]
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyPhPjEe24JYXQSUVGBFiwOH",
"include_colab_link": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
]
},
"nbformat": 4,
"nbformat_minor": 0
}
4 changes: 3 additions & 1 deletion genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="more_programs/sanity.html">Tools to help write programs</a></li>

<li class="toctree-l1"><a class="reference internal" href="more_programs/arithmetic.html">Programs for Arithmetic</a></li>
<li class="toctree-l1"><a class="reference internal" href="more_programs/arithmetic.html">Programs for arithmetic</a></li>




<li class="toctree-l1"><a class="reference internal" href="more_programs/primitive_recursion.html">Primitive Recursion</a></li>
Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="more_programs/sanity.html">Tools to help write programs</a></li>

<li class="toctree-l1"><a class="reference internal" href="more_programs/arithmetic.html">Programs for Arithmetic</a></li>
<li class="toctree-l1"><a class="reference internal" href="more_programs/arithmetic.html">Programs for arithmetic</a></li>




<li class="toctree-l1"><a class="reference internal" href="more_programs/primitive_recursion.html">Primitive Recursion</a></li>
Expand Down
4 changes: 3 additions & 1 deletion introOneSharp/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../more_programs/sanity.html">Tools to help write programs</a></li>

<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for Arithmetic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for arithmetic</a></li>




<li class="toctree-l1"><a class="reference internal" href="../more_programs/primitive_recursion.html">Primitive Recursion</a></li>
Expand Down
4 changes: 3 additions & 1 deletion introOneSharp/haltDef.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../more_programs/sanity.html">Tools to help write programs</a></li>

<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for Arithmetic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for arithmetic</a></li>




<li class="toctree-l1"><a class="reference internal" href="../more_programs/primitive_recursion.html">Primitive Recursion</a></li>
Expand Down
4 changes: 3 additions & 1 deletion introOneSharp/instructions.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../more_programs/sanity.html">Tools to help write programs</a></li>

<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for Arithmetic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for arithmetic</a></li>




<li class="toctree-l1"><a class="reference internal" href="../more_programs/primitive_recursion.html">Primitive Recursion</a></li>
Expand Down
4 changes: 3 additions & 1 deletion introOneSharp/move_copy_write.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="../more_programs/sanity.html">Tools to help write programs</a></li>

<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for Arithmetic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../more_programs/arithmetic.html">Programs for arithmetic</a></li>




<li class="toctree-l1"><a class="reference internal" href="../more_programs/primitive_recursion.html">Primitive Recursion</a></li>
Expand Down
Loading

0 comments on commit 70caf76

Please sign in to comment.