Docsity

Lambda Calculus - Problem Set 4 | COM S 342, Assignments of Computer Science

Toggle sidebar
bg1
Com S 342 Fall2005 Dr. Markus Lumpe
Problem Set 4: Lambda-Calculus
This homework is due October 4. No extensions will be given!
Problem 1
Consider the following BNF specification:
<LambdaExp> ::= <Identifier>
|
(lambda (<Identifier>) <LambdaExp>)
|
(<LambdaExp> <LambdaExp>)
Define the procedure (free-names
e
) that returns the set (i.e., a list) of free names of
the lambda-calculus expression
e
. The set of free names, written fv(
e
), is defined as
follows:
fv( x ) = { x }
fv( (lambda (x) e) ) = fv( e ) \ { x }
fv( (e1 e2) ) = fv( e1 ) ∪ fv( e2 )
In order to solve this problem, use the procedures that you have defined in problem set
2 and 3.
Solution:
(define free-names
(lambda (e)
(cond
((is-variable? e)
(list e))
((is-abstraction? e)
(difference (free-names (caddr e)) (cadr e)))
((is-application? e)
(union (free-names (car e)) (free-names (cadr e))))
)
)
) 15
Problem 2
Define the procedure (contains-beta-redex?
e
) that returns #t if the lambda–
calculus expression
e
contains a beta-redex. Otherwise, the procedure returns #f. A
beta-redex is an application (e1 e2) where e1 is an abstraction, that is, it has the form
(lambda (x) e1). Note, a beta-redex may occur underneath a lambda-abstraction, that
1
pf3
pf4

Partial preview of the text

Download Lambda Calculus - Problem Set 4 | COM S 342 and more Assignments Computer Science in PDF only on Docsity!

Problem Set 4: Lambda-Calculus

This homework is due October 4. No extensions will be given!

Problem 1

Consider the following BNF specification:

::= | (lambda ( ) ) | ( )

Define the procedure (free-namese) that returns the set (i.e., a list) of free names of

the lambda-calculus expression e. The set of free names, written fv(e) , is defined as

follows:

fv( x ) = { x } fv( (lambda (x) e) ) = fv( e ) \ { x }

fv( (e 1 e 2 ) ) = fv( e 1 ) ∪ fv( e2 )

In order to solve this problem, use the procedures that you have defined in problem set 2 and 3.

Solution: (define free-names (lambda (e) (cond ((is-variable? e) (list e)) ((is-abstraction? e) (difference (free-names (caddr e)) (cadr e))) ((is-application? e) (union (free-names (car e)) (free-names (cadr e)))) ) ) ) 15

Problem 2

Define the procedure (contains-beta-redex? e) that returns #t if the lambda–

calculus expression e contains a beta-redex. Otherwise, the procedure returns #f. A

beta-redex is an application (e 1 e 2 ) where e 1 is an abstraction, that is, it has the form (lambda (x) e 1 ). Note, a beta-redex may occur underneath a lambda-abstraction, that

is, the sub-expression e 1 may contain a beta-redex. Also, in case of a lambda-calculus application (e 1 e 2 ) either e 1 or e 2 may contain a beta-redex.

In order to solve this problem, use the procedures that have been defined in problem set 2.

Solution: define contains-beta-redex? (lambda (e) (cond ((is-variable? e) #f) ((is-application? e) (or (is-abstraction? (car e)) (contains-beta-redex? (car e)) (contains-beta-redex? (cadr e)) )) ((is-abstraction? e) (contains-beta-redex? (caddr e))) ) ) 20 )

Problem 3

Define the procedure (fresh-namesym los) that returns a fresh name derived from

sym that does not occur in los. Use the string “%” to build a unique symbol and the

Scheme procedures symbol->string , string-append , and string->symbol. Example,

**> (fresh-name 'x '(x y z)) x%

(fresh-name 'x '(x% x%% y z)) x (fresh-name 'x '(x x% x%% y z)) x%%%**

Solution: (define fresh-name (lambda (sym los) (if (member sym los) (fresh-name (string->symbol (string-append (symbol->string sym) "%")) los) sym ) ) ) 15

(caadr e1) (union (free-names e2) (free-names (caddr e1))))))

(list (car e1) (list new-name) (substitution (substitution (caddr e1) (caadr e1) new-name) x e2)) ; rule 6 ) (list (car e1) (cadr e1) (substitution (caddr e1) x e2)) ) ) ----- ) 40 ) ) )

Total: 15 + 15 + 20 + 40 = 90

Submission deadline: Thursday, October 4, 2004, 2:10 p.m.

Submission procedure: on paper in class and electronically using the turnin- hw4 script, which is located in ~cs342/public/bin. Please use the printout of the submission confirmation email as cover page and check the problems that you have solved.

In order to submit your homework solutions, go (using your CS UNIX account) into the directory that contains your solution (i.e., Scheme source files and all related files). In that directory run the command “~cs342/public/bin/turnin-hw4”. After a successful submission, your will receive a confirmation email. Before the due date, you can resubmit your solutions as often as you like.