How to Use the Simulators for Assignment 4
The simulator programs let you run programs in tiny Python (aka
Rubber Boa), the counter program language, and FRACTRAN. You can
also run and test lambda calculus programs using standard features
in Python.
Download the simulator programs from the website, as well as the
sample programs. For Tiny Python and lambda calculus, there is
no simulator program, but there are .py files implementing a large
number of basic functions in these two languages. There is
also a `cheat-checker' for Tiny Python, to make sure that you
do not use any Python statements that are not part of the tiny
subset.You should save these all in the same directory on your
computer.
Start IDLE. Open up any of the Python programs in your
directory and select 'Run Module' from the Run menu.
Then at the prompt in the Python shell, type
from cheatcheck import *
from countermachine import *
from fractran import *
The tiny Python functions that I've give you to start off with are
in a file called recursive_function_land.py.
To cheat-check what's already there, type
cheat_check('recursive_function_land.py')
You will get a message that it is okay to proceed. You can
then check out some of the function that are written for you, by
typing, say,
add(14,27)
iszero(12)
iszero(0)
pred(6)
pred(0)
Any tiny Python functions you write for homework should be added to
the file recursive_function_land.py, and you
should cheat-check before submitting anything. If you already
know Python, it is hard to avoid writing forbidden code!
Programs for the counter machine are prepared as in the examples add.cp
and divmod2.cp. You have to use single
lower-case letters for the names of counters, but you can use any
combination of letters and digits for labels.
To run the first, with arguments 14 and 27, just type
runcp('add.cp',14,27)
If you want to see what instruction is being executed at every step,
type
runcp('add.cp',14,27, verbose=True)
This gives the line numbers of instructions, starting at 0, and not
the labels. The output at the end shows the values of the counters
a,b,c,..,z when the program halts.
A FRACTRAN program is just a list of fractions, one per line.
You need to include the fraction bar / in each line, even if the
value is an integer (i.e., write 5/1 in place of 5). To run the
example division program on the input 7, for example, type
runfractran('divmod2.fract', 7)
To run one of the example addition programs on inputs 5 and
7, type
runfractran('add1.fract', 5,7)
The program will automatically do the encoding of the input for
you: That is, 7 will be encoded as 27, and 5,7 as 2537.
The result printed at the end is the prime factorization of the
value at which the program halts. So, for example, add2.fract
computes the sum of x and y as 5x+y, so you will see the
answer to adding 5 and 7 appear as {5:12}. Once again, there is a 'verbose'
option that will print out all the steps, and show you which
fraction was used as the multiplier at each step.
For the lambda calculus problems, just add your new functions to the
list that is already in lambdaland.py. There are also some
tips in the comments, as well as examples, on how to test if your
code is doing the right thing.