Homework 5: Continuations

COMP 105, Fall 2019

Due Wed, Oct 16 @ 11:59pm

This homework will give you practice using continuations, which are discussed in Section 2.10 of the textbook. As with the last two assignments, you will write code in μScheme. You will write a continuation-based backtracking solver for boolean formulae, expanding on the solver discussed in class and in Section 2.10.

These questions are meant to guide you through the readings that will help you complete the assignment. Keep your answers brief and simple.

  1. Revisit the definition of values in figure 2.1 on page 99. Next, study the definition of the predefined function all? in section 2.8.3, which starts on page 135. Like many higher-order functions, all? has a subtle contract: it is permissible to call (all? p? xs) if and only if there exists a set A such that following are true:
    • Parameter p? is a function that accepts one argument, which must be a member of A. Then p? returns a Boolean.
    • Parameter xs is in LIST(A). That is, xs is a list of values, and every element of xs is a member of A.
    In other words, xs must be a list of values, each of which may be passed to p?. With this contract in mind, answer these questions:
    1. Write a value that is never permissible as the second, xs argument to all?, no matter what p? is.
    2. Write a value made with cons that is never permissible as the second, xs argument to all?, no matter what p? is.
    3. Write a value that may or may not be permissible as the second, xs argument to all?, depending on what p? is. Your value should be permissible if p? is number?, but impermissible if p? is prime?.
  2. Study the description of function list-of? below, and also the hints. Now, for each value of xs that you listed in the previous question, answer whether it is permissible to pass that value to list-of? along with the predicate prime?, and if so, what result list-of? should return. Assume that we're using a version of prime? that accepts any value and returns #f for non-numbers.
    1. Permissible? If so, what does list-of? return?
    2. Permissible? If so, what does list-of? return?
    3. Permissible? If so, what does list-of? return?
  3. Section 2.12.3, which starts on page 159, describes the semantics of the true-definition forms. Use the semantics to answer two questions about the following sequence of definitions:
    (val f (lambda () y))
    (val y 10)
    (f)
    Given evaluating lambda in an environment ρ creates a closure using that same ρ, if the definitions above are evaluated in an environment in which y ∈ domρ, then what is the result of the call to f? Pick A, B, or C.
    1. It returns 10.
    2. An error is raised: Run-time error: name y not found.
    3. It returns whatever value y had before the definitions were evaluated.
    If the definitions above are evaluated in an environment in which y ∉ domρ, what is the result of the call to f? Pick either A or B.
    1. It returns 10.
    2. An error is raised: Run-time error: name y not found.
  4. Read the definition of eval-formula below, including the definition of the environment used by eval-formula. What is the result of evaluating the following Boolean formulas in an environment where x is #t, y is #f, and z is #f? (For each formula, answer #t, “true”, #f, or “false.”)
    1. x, which in μScheme is constructed by 'x
    2. ¬x, which in μScheme is constructed by (make-not 'x)
    3. ¬y ∧ x, which in μScheme is constructed by (make-and (list2 (make-not 'y) 'x))
    4. ¬x ∨ y ∨ z, which in μScheme is constructed by (make-or (list3 (make-not 'x) 'y 'z))
    5. Formula (make-not (make-or (list1 'z))), which has a tricky make-or applied to a list of length 1, and so can’t be written using infix notation
  5. Read about the Boolean-satisfaction problem for CNF formulas, in section 2.10.1, which starts on page 145. The rules for satisfaction are the same for all Boolean formulas, even those that are not in CNF.For example:
    • x‌ ∨ y ∨ z, which in μScheme is constructed by (make-or '(x y z)), is solved by '((x #t))
    • x ∧ y ∧ z, which in μScheme is constructed by (make-and '(x y z)), is solved by '((x #t) (y #t) (z #t))
    • x ∧ ¬x, which in μScheme is constructed by (make-and (list2 'x (make-not 'x))), has no solution
    For each of the following Boolean formulas, if there is an assignment to x, y, and z that satisfies the formula, write the words “is solved by” and a satisfying assignment. Incomplete assignments are OK. If there is no satisfying assignment, write the words “has no solution.”:
    1. (x ∨ ¬x) ∧ y, which in μScheme is constructed by (make-and (list2 (make-or (list2 'x (make-not 'x))) 'y))
    2. (x ∨ ¬x) ∧ ¬x, which in μScheme is constructed by (make-and (list2 (make-or (list2 'x (make-not 'x))) (make-not 'x))),
    3. (x ∨ y ∨ z) ∧ (¬x ∧ x) ∧ (x ∨ ¬y ∨ ¬z), which in μScheme is constructed by
      (make-and 
          (list3 (make-or (list3 'x 'y 'z)) 
                 (make-and (list2 (make-not 'x) 'x))
                 (make-or (list3 'x (make-not 'y) (make-not 'z))))))

Your SAT solver will represent boolean formulae the same way as in the textbook: A formula is either a symbol such as 'x, representing a variable, or a record constructed using the following definitions:

(record not [arg])
(record or  [args])
(record and [args])