CS20a
Homework #6
November 14, 2002
Due November 21, 2002
Exercise 1.
Partial recursive functions
Show that every
while
program over
N
is equivalent to a
while
program with at most 1
while
loop.
Exercise 2.
Primitive recursive functions
Show that a function is primitive recursive iff it is computed by a Turing Machine with a prim
itive recursive time bound.
For this problem, assume a Turing Machine computes a function
Σ
*
→ {
0
,
1
}
, where the argu
ment is the input string, and the result is 1 iff the Turing machine accepts.
A Turing Machine is timebounded by a primitive recursive function
f
:
N
→
N
iff for any input
string
x
, the Turing Machine accepts or rejects within
f (

x

)
moves.
Exercise 3.
Laboratory
For this lab, we will build an evaluator for the
λ
calculus with numbers and conditionals. Here
is the syntax of the language:
e
::
=
i
(numbers)

x
(variables)

e
1
e
2
(application)

λx.e
(functions)

e
1
binop
e
2
(binary arithmetic)

if
e
1
then
e
2
else
e
3
(conditional)

let
v
=
e
1
in
e
2
(let definition)

letrec
f v
=
e
1
in
e
2
(recursive function definition)
binop
::
=
+    * 
/
=≤
<
≥≥
≠
Evaluation is defined using
evaluation rules
. If
e
↓
v
we say the program
e
evaluates to
the
value
v
, where a value is a number or a function.
The exact specification of evaluation is defined
through evaluation rules.
If we were using substitution (like we did in class), the evaluation
semantics would look something like this.
i
↓
i
number
e
1
↓
i
1
e
2
↓
i
2
e
1
binop
e
2
↓
(i
1
binop
i
2
)
binop
e
1
↓
λx.e
3
e
2
↓
v
2
e
3
[e
2
/x]
↓
v
3
e
1
e
2
↓
v
3
app
λx.e
↓
λx.e
fun
e
1
↓
0
e
2
↓
v
2
if
e
1
then
e
2
else
e
3
↓
v
2
iftrue
e
1
↓
0
e
3
↓
e
3
↓
v
3
if
e
1
then
e
2
else
e
3
↓
v
4
iffalse
The problem with this semantics is that it is really inefficient because the usual definition of
substitution
e
1
[e
2
/v]
requires copying the entire expression
e
1
.
An alternative definition that is much more widely used is to define a variable
environment
that
keeps track of values for variables. When a variable is evaluated, the value is retrieved from
the environment. We’ll use the notation
σ
for an environment. The environment supports two
operations: we get the value of a variable
v
with the operation
σ[v]
; and we can set the value
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 cs
 Recursion, Primitive recursive function, Integer Integer Integer

Click to edit the document details