Com S 342 --- Principles of Programming Languages EXERCISE 10: TRANSFORMING PROCEDURAL REPRESENTATIONS TO ABSTRACT SYNTAX TREE REPRESENTATIONS (File $Date: 2004/03/11 17:59:26 $) The purpose of this exercise is for you to learn about the transformation of procedural representations to abstract syntax tree representations. As with all exercises, this is to be done individually. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs342/syllabus.shtml). As with all exercises, you have two choices for doing the work. You can either: - complete it as specified or - write down questions or problems that you had in trying to complete it. If you write down questions or problems you have, these should be detailed enough so that we can tell that you have read the materials and thought about them. (Don't just write: "I didn't understand how to do it". Instead, say what you tried and what you didn't understand.) During the class where this exercise is discussed, you should ask about these difficulties, and clear them up. Don't be shy; there will be other people with the same problem, and everyone can learn by discussing these issues. And you'll most likely see similar things on the homework, so it's best to understand them now. 1. [transform idea] Read section 2.3 of "Essentials of Programming Languages" (2nd ed., 2001) by Friedman, Wand, and Haynes. (You may need to read other parts of chapter 2 if you haven't already done that.) Consider the following procedures (from $PUB/lib/seq-as-proc.scm) (define seq-repeat (lambda (num) (lambda (n) num))) (define seq-generator (lambda (f) (lambda (n) (f n)))) a. Both of these procedures, when called return a procedure. The first returns (lambda (n) num), and the second returns (lambda (n) (f n)). What are the free variables in each of these returned procedures? Consider the following procedure: (define seq-nth (lambda (s n) (s n))) b. What are the results of the following expressions? (seq-nth (seq-repeat 1) 0) (seq-nth (seq-repeat 1) 3940213425412) (seq-nth (seq-generator (lambda (i) i)) 0) (seq-nth (seq-generator (lambda (i) i)) 342) (seq-nth (seq-generator (lambda (i) (/ 1 (+ i 1)))) 1) (seq-nth (seq-generator (lambda (i) (/ 1 (+ i 1)))) 2) (seq-nth (seq-generator (lambda (i) (/ 1 (+ i 1)))) 3) c. What information has to be kept in the closure formed when scheme evaluates an expression of the form (seq-repeat n), where n is a number? d. What information has to be kept in the closure formed when scheme evaluates an expression of the form (seq-generator rule), where rule is a procedure? 2. Declare a variant record, using define-datatype, that has two variants: repeated-seq and generated-seq. Each should have a field whose name is the name of the corresponding free variable discussed in 1(a), and each field should have a type that is the type of the information used in parts 1(c) and 1(d). WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above. ADDITIONAL THOUGHTS If you have time, try to define a version of seq-nth that works on the variant record that is the answer to part 2. ADDITIONAL READINGS See William Cook's paper, "Object-Oriented Programming Versus Abstract Data Types" in Lecture Notes in Computer Science, volume 489, pages 151-178, 1991. This is in the library or you can read it at the following URL: http://www.cs.utexas.edu/users/wcook/papers/OOPvsADT/CookOOPvsADT90.pdf