University of Virginia Computer Science CS150: Computer Science, Fall 2005 |
It will likely contain questions on:
1. For Problem Set 6, Question 4, two common answers were given:
Answer A:Explain which answer is better and why.(define (make-professor name) (let ((super (make-lecturer name))) (lambda (message) (if (eq? message 'profess) (lambda (self message) (ask self 'say (list "It is intuitively obvious that")) (ask self 'lecture message)) (get-method super message)))))Answer B:(define (make-professor name) (let ((super (make-lecturer name))) (lambda (message) (if (eq? message 'profess) (lambda (self message) (ask self 'say (list "It is intuitively obvious that")) (ask self 'say message) (ask self 'say (list "you should be taking notes"))) (get-method super message)))))
2. Consider the following definitions taken from the PS6 code (without modification except for removing some methods that are not needed for this question):
(define (make-object name) (lambda (message) (case message ((class) (lambda (self) 'object)) ((name) (lambda (self) name)) (else no-method)))) (define (make-physical-object name) (let ((super (make-object name)) (location null-place)) ; location set when we install (lambda (message) ; Normal actions (case message ((class) (lambda (self) 'physical-object)) ((location) (lambda (self) location)) (else (get-method super message)))))) (define (make-mobile-object name) (let ((super (make-physical-object name))) (lambda (message) (case message ((class) (lambda (self) 'mobile-object)) (else (get-method super message)))))) (define (make-person name) (let ((super (make-mobile-object name)) (possessions '()) (restlessness 0.0)) (lambda (message) (case message ((class) (lambda (self) 'person)) (else (get-method super message))))))Provide the simplest possible expression that could have been used in the definition of mystery to produce the given environment diagram shown here:
3. How many frames would be created when (make-mobile-object "bike") is evaluated? (Explain why each frame is generated. Recall that let is syntactic sugar for ((lambda (...) )).)
(From Spring 2003 Exam 2)
Consider the classes defined below: sail-boat is a subclass of boat which is a subclass of object.
(define (ask object message . args) (apply (get-method object message) object args)) (define (get-method object message) (object message)) (define make-object (lambda (name) (lambda (message) (case message ((class) (lambda (self) 'object)) ((object?) (lambda (self) #t)) ((name) (lambda (self) name)) (else (error "No method")))))) (define make-boat (lambda (name) (let ((super (make-object name)) (floating #t)) (lambda (message) (case message ((class) (lambda (self) 'boat)) ((boat?) (lambda (self) #t)) ((is-floating?) (lambda (self) floating)) ((sink) (lambda (self) (set! floating #f))) (else (get-method super message))))))) (define make-sail-boat (lambda (name) (let ((super (make-boat name)) (sailing #f)) (lambda (message) (case message ((class) (lambda (self) 'sail-boat)) ((raise-sail) (lambda (self) (set! sailing #t))) ((lower-sail) (lambda (self) (set! sailing #f))) (else (get-method super message)))))))1. The following environment diagram shows the state after evaluating (define discovery (make-sail-boat "discovery")). The procedure associated with the name discovery is missing its environment pointer. Add the missing environment pointer to the diagram.
2. There are three frames in the diagram (labeled F1, F2 and F3) that contain a place named name with the value "discovery". If we evaluate (ask discovery 'name) in the global environment, in which frame with the value of name be found?
Cy D. Fect defines a procedure reverse! that uses mutation to reverse a list:
Consider evaluating:(define (next-to-last-pair lst) (if (null? lst) (error "Null list!") (if (null? (cdr lst)) (error "Single length list!") (if (null? (cdr (cdr lst))) lst (next-to-last-pair (cdr lst)))))) (define (reverse! lst) (if (or (null? lst) (eq? 1 (length lst))) #f ;; Nothing to do for empty or length 1 list (value doesn't matter) (let* ((nexttolast (next-to-last-pair lst)) (lastpair (cdr nexttolast)) (first (car lst))) ;;; Point 0 (set-cdr! nexttolast null) ;;; Point 1 (reverse! (cdr lst)) (set-cdr! nexttolast lastpair) (set-car! lst (car lastpair)) ;;; Point 2 (set-car! lastpair first))))
> (define ilist (list 1 2 3))
> (reverse! ilist)
> ilist
(3 2 1)
At Point 0 on the first call to reverse! the environment looks like:
The E1 environment is created by the application (reverse! ilist). The E2 environment is created by the let inside reverse! (which desugars to an application of a lambda). (The let* actually desugars into more than one lambda, and more than one environment, but for this question we combine them into one environment.)
4. Show what the environment looks like at Point 1 (after evaluating (set-cdr! nexttolast null)). To make your drawing easier, here is a template drawing. You can answer this question by only adding to this template.
5. Show what the environment looks like at Point 2. You can answer this question by only adding to this template.
5, 6 and 7. (Counts as 3 questions) How would a theoretician order the problems below from least difficult to most difficult? Justify your answer by explaining how much work each problem involves using Θ notation (or explaining why the problem is undecidable). Be careful to fully describe what any variables you use in your answer mean, and any assumptions you need to make.
Ali G ProblemInput: Two long d-digit numbers (mostly nines)Chess Problem
Output: The product of the two input numbersInput: A chess position (a list of pieces on a 64 square board)Genome Assembly ProblemOutput: If there is winning strategy for white (moves first), output true (that is, a way of moving for white that guarantees a win no matter what the adversary does). Otherwise, output false.
Input: A set of genome reads (sequences of base pairs)Procedure Equivalence ProblemOutput: The smallest possible genome that includes each read.
Input: Two procedures, P and Q, each taking one input that can be any value. You may assume both procedures are guaranteed to always terminate.Output: true is P is functionally equivalent to Q; false otherwise. Two procedures are functionally equivalent if for every input the produce the same output.
8-9. (counts as 2 questions) How would a pragmatist order the same problems? If the amount of work is different for the pragmatist, explain clearly why. You should assume your pragmatist cares about getting a reasonably good answer to the problem today. State any assumptions you make about what the actual input size is, and how good and answer is "good enough".
Input: A description of a Turing Machine and input tape.5. Is the repeats-fsm-state problem defined below decidable or undecidable? For credit, your answer must include a convincing argument to support your answer.Output: If executing the Turing Machine on the given input would ever repeat a machine configuration (the state of the finite state machine and the contents of the tape), output true. Otherwise, output false. That is, the output should be true if and only if executing the Turing Machine on the given input would encounter the same exact machine configuration more than once.
Input: A description of a Turing Machine and input tape.Output: If executing the Turing Machine on the given input would ever repeat a finite state machine state, output true. Otherwise, output false. That is, the output should be true if and only if executing the Turing Machine on the given input would enter the same finite state machine state more than once.
Is the Greater than Zero Problem described below decidable?
Input: A program P and input n
Output: If evaluating P applied to n would produce a value greater than 0, true. If evaluating P applied to n would produce a value that is not a number, or is a number less than or equal to 0, or would not terminate, output is false.
In class, we defined if, T and F using Lambda Calculus as:
if ≡ λ pca . p c aSuppose we instead defined if as:
T ≡ λ xy . x
F ≡ λ xy . y
8. Show how T and F would need to be defined to make the new definition of if work correctly.
7. Given T, F and if as defined in class:
T ≡ λ x (λ y . x)a. Define a Lambda Calculus term that behaves like not. For example, if (not T) M N should reduce to N and if (not F) M N should reduce to M.
F ≡ λ x (λ y . y)
if ≡ λ pca . pca
b. Show that your definition of not works by showing the steps to reduce if (not T) M N to normal form (where M and N represent and Lambda Calculus term in normal form).
Consider the preSQL language described below:
Commands ::= Command ; CommandsPreSQL is similar to SQL except:
Commands ::= Command
Command ::= CreateTableCommand | InsertCommand | CountCommand | DeleteCommand
CreateTable ::= CREATE TABLE Name
Create a new table named Name with no entries.
InsertCommand ::= INSERT INTO Table Value
Insert a value (TRUE or FALSE) as the last element in table Table.
Value ::= TRUE
Value ::= FALSE
CountCommand ::= COUNT Table
Evaluates to the number of entries in Table.
DeleteCommand ::= DELETE FROM Table
Remove the first (earliest inserted) element from the table Table.
9. Is PreSQL a Universal Language? Use convincing arguments to support your answer.> CREATE TABLE tab
> INSERT INTO tab TRUE
> INSERT INTO tab FALSE
> COUNT tab
2
> DELETE FROM tab
> COUNT tab
1
PreSQLsql adds one additional command to PreSQL:
Command ::= UNTILEMPTY Table { Commands }UNTILEMPTY will check if Table is empty (that is, it has zero entries). If it is empty, it does nothing. Otherwise, it will execute the Commands and repeat the UNTILEMPTY command. It will keep repeating the Commands until the Table is empty.
For example, UNTILEMPTY tab { DELETE FROM tab } would delete all entries from that table tab.
10. Show how you could do addition using PreSQLsql. Your addition code should assume the input is in the form of two tables, tableA and tableB. After addition, a new table named output should have the result. Provide code that could be used for the missing code below that would correctly add any two natural numbers (your code may modify the input tables):
|
Phine Knight suggests the modelling computation using a Phine Machine consisting of a list of numbered instructions (the Instructions), a pointer to the current instructions (the InstructionPointer), and a store that associates name and values (the Store).
A program executes by executing the instruction numbered by the InstructionPointer, and then increasing the InstructionPointer by 1. This continues unless the Instruction is HALT.
Instructions can do one of four things:
( <1: X := 0 2: X := X + 1 3: Y := X - 1 4: HALT >, 1, { } )The model will start by executing instruction number 1, which puts <X, 0> in the store and advances the Instruction Pointer to 2. After this, the state is:
( <1: X := 0 2: X := X + 1 3: Y := X - 1 4: HALT >, 2, { <X, 0> } )After the next two steps, the state will be:
( <1: X := 0 2: X := X + 1 3: Y := X - 1 4: HALT>, 4, { <X, 1>, <Y, 0> } )8. Write a BNF grammar that could be used to describe the state of a Phine Machine. A good answer will describe the smaller language possible that includes strings for describing all possible Phine Machine states.
9. Is this Phine Machine model of computation equivalent to a Turing Machine? Argue convincingly why it is or isn't.
10. Condy Shonal suggests adding one new instruction to Knight's model:
Instruction ::= IF Name = 0 GOTO NumberIf the value of Name in the Store is 0, then this instruction sets the InstructionPointer to Number. If it is not, then this instruction does nothing and advances the InstructionPointer by one.
For example,
( <1: IF A = 0 GOTO 5 2: B := B + 1 3: A := A - 1 4: GOTO 1 5: HALT>, 1, { <A, n>, <B, m> } )is a program that will halt with B having the value n + m.
Condy claims her new model of computation is as powerful as a Turing Machine, but Fine Knight does not believe her. Write an informal but convincing argument that this model is as powerful as a Turing Machine.