Control Flow Analysis and Logic Programming Section and ..., Schemes and Mind Maps of Programming Languages

Harvard School of Engineering and Applied Sciences — CS 152: Programming Languages. Control Flow Analysis and Logic Programming.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 05/11/2023

ekassh
ekassh 🇺🇸

4.7

(23)

272 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages
Control Flow Analysis and Logic Programming
Section and Practice Problems
Monday April 27, 2015
1 Control Flow Analysis
Consider the following lambda calculus program.
(λf. (f76) + (f77)) (λa. a)
(a) Add labels to the program. That is, make it an expression in the labeled lambda calculus of Lecture
22, where every label is unique.
(b) Let ebe your labeled lambda calculus program. Write out C[[e]]e, i.e., the set of constraints for the
program e. (Hint, you should have 20 constraints in total. In particular, for each of the 3 applications,
you should have 4 constraints, 2 for each of the lambda terms in the program.)
(c) Find Cand r, the smallest functions that satisfy the constraints you generated in the question above.
(d) Check that your functions Cand rmake sense. That is, if an expression labeled lcan evaluate to an
expression labeled l0, do you have l0C(l)?
(e) Consider adding the expression (let x=e1in e2)lto the language. Define C[[(let x=e1in e2)l]]e. Try
rewriting the program above using one or more let expressions, and make sure that the constraints
you generate for the modified program produce the same solution Cand r.
2 Logic Programming
(a) Consider the following Prolog program (where [] is a constant representing the empty list, [t]is short-
hand for cons(t, []) and [t1, t2|t3]is shorthand for cons(t1,cons(t2, t3)).
foo([],[]).
foo([X],[X]).
foo([X, Y |S],[Y, X|T]) :– foo(S, T )
For each of the following queries, compute the substitutions that Prolog will generate, if any. (Note
that there is a difference between an empty substitution, and no substitution.) If the query evaluation
will not terminate, explain why.
foo([a,b], X ).
foo([a,b,c], X ).
foo([a,b],[a,b])
foo(X, [a])
foo(X, Y ).
pf2

Partial preview of the text

Download Control Flow Analysis and Logic Programming Section and ... and more Schemes and Mind Maps Programming Languages in PDF only on Docsity!

Harvard School of Engineering and Applied Sciences — CS 152: Programming Languages

Control Flow Analysis and Logic Programming

Section and Practice Problems

Monday April 27, 2015

1 Control Flow Analysis

Consider the following lambda calculus program.

(λf. (f 76) + (f 77)) (λa. a)

(a) Add labels to the program. That is, make it an expression in the labeled lambda calculus of Lecture 22, where every label is unique.

(b) Let e be your labeled lambda calculus program. Write out C[[e]]e, i.e., the set of constraints for the program e. (Hint, you should have 20 constraints in total. In particular, for each of the 3 applications, you should have 4 constraints, 2 for each of the lambda terms in the program.)

(c) Find C∗^ and r∗, the smallest functions that satisfy the constraints you generated in the question above.

(d) Check that your functions C∗^ and r∗^ make sense. That is, if an expression labeled l can evaluate to an expression labeled l′, do you have l′^ ∈ C∗(l)?

(e) Consider adding the expression (let x = e 1 in e 2 )l^ to the language. Define C[[(let x = e 1 in e 2 )l]]e. Try rewriting the program above using one or more let expressions, and make sure that the constraints you generate for the modified program produce the same solution C∗^ and r∗.

2 Logic Programming

(a) Consider the following Prolog program (where [] is a constant representing the empty list, [t] is short- hand for cons(t, []) and [t 1 , t 2 |t 3 ] is shorthand for cons(t 1 , cons(t 2 , t 3 )).

foo([], []). foo([X], [X]). foo([X, Y |S], [Y, X|T ]) :– foo(S, T )

For each of the following queries, compute the substitutions that Prolog will generate, if any. (Note that there is a difference between an empty substitution, and no substitution.) If the query evaluation will not terminate, explain why.

  • foo([a, b], X).
  • foo([a, b, c], X).
  • foo([a, b], [a, b])
  • foo(X, [a])
  • foo(X, Y ).

Control Flow Analysis and Logic Programming Section and Practice Problems

(b) Consider the following Datalog program.

bar(a, b, c). bar(X, Y, Z) :– bar(Y, X, Z). bar(X, Y, Z) :– bar(Z, Y, X), quux(X, Z). quux(b, c). quux(c, d). quux(X, Y ) :– quux(Y, X). quux(X, Z) :– quux(X, Y ), quux(Y, Z).

Find all solutions to the query bar(X, Y, Z).

Page 2 of 2