Skip to content

Commit bebd82d

Browse files
committed
50003: Removed register machine code from notes
1 parent 3bb8db7 commit bebd82d

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed
Binary file not shown.

50003 - Models of Computation/register_machines/code/register_machine_encoding.py

+46-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Dec = namedtuple('Dec', 'reg label1 label2')
77
Halt = namedtuple('Halt', '')
88

9-
'''
9+
print(r'''
1010
__ ________ _ _____ ____ __ __ ______
1111
\ \ / / ____| | / ____/ __ \| \/ | ____|
1212
\ \ /\ / /| |__ | | | | | | | | \ / | |__
@@ -27,7 +27,7 @@
2727
Halt()
2828
2929
To ensure your latex will compile, make sure you have commands for, these are
30-
available on my github (Oliver Killane) (Imperial-Computing-Year-2-Notes):
30+
available in the notes repository:
3131
3232
% register machine helper commands:
3333
\newcommand{\instrlabel}[1]{\text{\textcolor{teal}{$L_{#1}$}}}
@@ -38,7 +38,7 @@
3838
\newcommand{\halt}{\text{\textcolor{red}{\textbf{HALT}}}}
3939
4040
To see examples, go to the end of this file.
41-
'''
41+
''')
4242

4343
# for encoding numbers as <a,b>
4444
def encode_large(x: int, y: int) -> int:
@@ -72,18 +72,22 @@ def encode_large_list_helper(lst: List[int], step: int) -> Tuple[int, int]:
7272
if (step == 0):
7373
print(r"\begin{center}\begin{tabular}{r l l}")
7474
if len(lst) == 0:
75-
print(f"{step} &" + rf"$ {buffer} 0$ & (No more numbers in the list, can unwrap recursion) \\")
75+
print(f"{step} &" + rf"$ {buffer} 0$ & (No more numbers in the list, " +
76+
"can unwrap recursion) \\")
7677
return (0, step)
7778
else:
7879

79-
print(rf"{step} & $ {buffer} \langle \langle {lst[0]}, \ulcorner {lst[1:]} \urcorner \rangle \rangle $ & (Take next element {lst[0]}, and encode the rest {lst[1:]}) \\")
80+
print(rf"{step} & $ {buffer} \langle \langle {lst[0]}, \ulcorner " +
81+
rf"{lst[1:]} \urcorner \rangle \rangle $ & (Take next element " +
82+
rf" {lst[0]}, and encode the rest {lst[1:]}) \\")
8083

8184
(b, step2) = encode_large_list_helper(lst[1:], step + 1)
8285
c = encode_large(lst[0], b)
8386

8487
step2 += 1
8588

86-
print(f"{step2} & $ {buffer} \langle \langle {lst[0]}, {b} \\rangle \\rangle = {c} $ & (Can now encode) \\\\")
89+
print(f"{step2} & $ {buffer} \langle \langle {lst[0]}, {b} \\rangle "+
90+
f"\\rangle = {c} $ & (Can now encode) \\\\")
8791

8892
if (step == 0):
8993
print(r"\end{tabular}\end{center}")
@@ -102,7 +106,8 @@ def decode_large_list_helper(n : int, prev : List[int], step : int = 0) -> List[
102106
else:
103107
(a,b) = decode_large(n)
104108
prev.append(a)
105-
print(rf"{step} & ${n} = \langle \langle {a}, {b} \rangle \rangle \ \ $&$ {prev}$ & (Decode into two integers) \\ ")
109+
print(rf"{step} & ${n} = \langle \langle {a}, {b} \rangle \rangle \ \ " +
110+
rf"$&$ {prev}$ & (Decode into two integers) \\ ")
106111

107112
next = decode_large_list_helper(b, prev, step + 1)
108113

@@ -115,13 +120,17 @@ def decode_large_list_helper(n : int, prev : List[int], step : int = 0) -> List[
115120
# R+(i) -> L(j)
116121
def encode_inc(instr: Inc) -> int:
117122
encode = encode_large(2 * instr.reg, instr.label)
118-
print(rf"$\ulcorner \inc{{{instr.reg}}}{{{instr.label}}} \urcorner = \langle \langle 2 \times {instr.reg}, {instr.label} \rangle \rangle = {encode}$")
123+
print(rf"$\ulcorner \inc{{{instr.reg}}}{{{instr.label}}} \urcorner = " +
124+
rf"\langle \langle 2 \times {instr.reg}, {instr.label} \rangle " +
125+
rf"\rangle = {encode}$")
119126
return encode
120127

121128
# R-(i) -> L(j), L(k)
122129
def encode_dec(instr: Dec) -> int:
123-
encode: int = encode_large(2 * instr.reg + 1, encode_small(instr.label1 ,instr.label2))
124-
print(rf"$\ulcorner \dec {{{instr.reg}}}{{{instr.label1}}}{{{instr.label2}}} \urcorner = \langle \langle 2 \times {instr.reg} + 1, \langle {instr.label1}, {instr.label2} \rangle \rangle \rangle = {encode}$")
130+
encode: int = encode_large(2 * instr.reg + 1, encode_small(instr.label1, instr.label2))
131+
print(rf"$\ulcorner \dec {{{instr.reg}}}{{{instr.label1}}}{{{instr.label2}}}" +
132+
rf" \urcorner = \langle \langle 2 \times {instr.reg} + 1, \langle " +
133+
rf"{instr.label1}, {instr.label2} \rangle \rangle \rangle = {encode}$")
125134
return encode
126135

127136
# Halt
@@ -201,11 +210,14 @@ def program_str(prog) -> str:
201210
def program_run(prog, instr_no : int, registers : List[int])-> Tuple[int, List[int]]:
202211
# step instruction label R0 R1 R2 ... (info)
203212
print(rf"\begin{{center}}\begin{{tabular}}{{l l l c" + " c" * len(registers) + " }")
204-
print(r"\textbf{Step} & \textbf{Instruction} & \instrlabel{{i}} &" + " & ".join([rf"$\reglabel{{{n}}}$" for n in range(0, len(registers))]) + r" & \textbf{Description}\\")
213+
print(r"\textbf{Step} & \textbf{Instruction} & \instrlabel{{i}} &" +
214+
" & ".join([rf"$\reglabel{{{n}}}$" for n in range(0, len(registers))]) +
215+
r" & \textbf{Description}\\")
205216
print(r"\hline")
206217
step = 0
207218
while True:
208-
step_str = rf"{step} & ${instr_to_str(prog[instr_no])}$ & ${instr_no}$ & " + "&".join([f"${n}$" for n in registers]) + "&"
219+
step_str = (rf"{step} & ${instr_to_str(prog[instr_no])}$ & ${instr_no}$ & " +
220+
"&".join([f"${n}$" for n in registers]) + "&")
209221
instr = prog[instr_no]
210222
if type(instr) == Inc:
211223
if (instr.reg >= len(registers)):
@@ -229,14 +241,18 @@ def program_run(prog, instr_no : int, registers : List[int])-> Tuple[int, List[i
229241
else:
230242
registers[instr.reg] -= 1
231243
instr_no = instr.label1
232-
print(step_str + rf"(Subtract 1 from register {instr.reg} and jump to instruction {instr.label1})\\")
244+
print(step_str +
245+
rf"(Subtract 1 from register {instr.reg} and "+
246+
rf"jump to instruction {instr.label1})\\")
233247
else:
234248
if instr.label2 >= len(prog):
235249
print(step_str + rf"(label {instr.label2} is does not exist)\\")
236250
break
237251
else:
238252
instr_no = instr.label2
239-
print(step_str + rf"(Register {instr.reg} is zero, jump to instruction {instr.label2})\\")
253+
print(step_str +
254+
rf"(Register {instr.reg} is zero, jump to " +
255+
rf"instruction {instr.label2})\\")
240256
else:
241257
print(step_str + rf"(Halt!)\\")
242258
break
@@ -288,22 +304,23 @@ def examples():
288304
Inc(0,0)
289305
], 0, [0,7])
290306

291-
encode_program_to_list([
292-
Inc(1,1),
293-
Inc(0,2),
294-
Inc(0,3),
295-
Inc(0,4),
296-
])
307+
# encode_program_to_list([
308+
# Inc(1,1),
309+
# Inc(0,2),
310+
# Inc(0,3),
311+
# Inc(0,4),
312+
# ])
297313

298-
encode_program_to_int([
299-
Dec(1,2,1),
300-
Halt(),
301-
Dec(1,3,4),
302-
Dec(1,5,4),
303-
Halt(),
304-
Inc(0,0)
305-
])
314+
# encode_program_to_int([
315+
# Dec(1,2,1),
316+
# Halt(),
317+
# Dec(1,3,4),
318+
# Dec(1,5,4),
319+
# Halt(),
320+
# Inc(0,0)
321+
# ])
306322

307-
decode_program((2 ** 46) * 20483)
323+
# decode_program((2 ** 46) * 20483)
308324

309325
# examples()
326+
# test()

50003 - Models of Computation/register_machines/register_machines.tex

+8-7
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,15 @@ \subsection{Programs}
316316
\vdots & \vdots \\
317317
\instr{n}{instruction_n}
318318
\end{matrix*} \right) \urcorner = \ulcorner [\ulcorner instruction_0 \urcorner, \dots, \ulcorner instruction_n \urcorner] \urcorner\]
319-
\section{Tools}
320-
In order to simplify checking workings, a basic python script for running, encoding and decoding register machines is provided (also available in the notes repository).
321-
\\
322-
\\ It is designed to be used in the python shell, to allow for easy manipulation, storing, etc of register machines, encoding/decoding results.
323-
\\
324-
\\ It also produces latex to show step-by-step workings for calculations.
325-
\inputminted{python}{register_machines/code/register_machine_encoding.py}
326319

320+
\begin{sidenotebox}{Tools}
321+
In order to simplify checking workings, a basic python script for running, encoding and decoding register machines is provided (also available in the notes repository).
322+
\begin{itemize}
323+
\item It is designed to be used in the python shell, to allow for easy manipulation, storing, etc of register machines, encoding/decoding results.
324+
\item It also produces latex to show step-by-step workings for calculations.
325+
\end{itemize}
326+
Have a go at making your own register machine encode/decode and simulation in your language of choice!
327+
\end{sidenotebox}
327328
\section{Gadgets}
328329
\begin{definitionbox}{Register Machine Gadget}
329330
A gadget is a partial register machine graph, used as components in more complex programs, that can be composed into larger register machines or gadgets.

0 commit comments

Comments
 (0)