PS2: Antics with Semantics
 Due:
 Update: all parts now due at the end of Mon. Feb 13
 Notes:
 Unlike PS1, this pset contains a solo problem (Problem 1). You can start the solo problem right away.
 You can do Problems 2 and 3 (and possibly start 4 and 5) based on knowledge you already have as of Fri. Feb. 03.
 You can collaborate on these nonsolo problems with your classmates following the usual Gilligan’s Island rule.
 The problems needn’t be done in order. Feel free to jump around.
 Submission:
 In the yourFullName CS251 Spring 2016 Folder that you created for PS1, create a Google Doc named yourFullName CS251 PS2.
 For all problems, include all answers (including Racket code and bigstep/smallstep derivations) in your PS2 google doc. Please format your derivations in Problems 1 and 4 and definitions from Problem 5 so that they’re easy to read. Format all derivations and code using a fixedwidth font (Courier New or Consolas). You use a small font size if that helps.
 For Problem 5 (Racket Recursion), also drop a copy of your
yourAccountNameps2functions.rkt
in your~/cs251/drop/ps02
drop folder on cs.wellesley.edu.
1. Conjunction Junction (Solo Problem) (25 points)
This is a solo problem. This means you must complete it entirely on your own without help from any other person and without consulting resources other than course materials or online documentation. You may ask Lyn for clarification, but not for help.
In this problem, we will consider Racket’s (and E1 E2)
and (or E1 E2)
logical operator constructs. (In Racket, and
and or
can have any number of subexpressions, but we’ll keep things simple by focusing on the common case with two subexpressions.)
1a. Bigstep Semantics (6 points)
In bigstep semantics, here are the evaluation rules for and
and or
:
 E1 ↓ #f
 [and false]
(and E1 E2) ↓ #f
 E1 ↓ V1
 E2 ↓ V2
 [and nonfalse] (where V1 is not #f)
(and E1 E2) ↓ V2
 E1 ↓ #f
 E2 ↓ V2
 [or false]
(or E1 E2) ↓ V2
 E1 ↓ V1
 [or nonfalse] (where V1 is not #f)
(or E1 E2) ↓ V1
Note that there are situations where and
and or
can return a value based solely on the value of the expression E1
without evaluating the expression E2
. Because of this, they are called shortcircuit operators.
Using the above rules together with the other bigstep rules you know, give complete bigstep evaluation derivations for the following two expressions. You may use either the conclusionabovesubderivation format or conclusionbelowsubderivation format, as described in PS1 Problem 5. Be sure to label each rule application with the rule name in square brackets.
(or (and (< 1 2) (> 3 4)) (and (= 5 6) (/ 7 0)))
(and (or (= 8 9) (* 10 11)) (or (+ 12 13) ( 14 #f)))
1b. Smallstep Semantics (4 points)
In smallstep semantics, here are the reduction rules for and
and or
:
(and #f E2) ⇒ #f [and false]
(and V1 E2) ⇒ E2 [and nonfalse] (where V1 is not #f)
(or #f E2) ⇒ E2 [or false]
(or V1 E2) ⇒ V1 [or nonfalse] (where V1 is not #f)
In all four of these rules, note that the first operand must be evaluated to a value before any evaluation begins on the second operand.
Using these smallstep reduction rules with other smallstep rules you know, give complete smallstep evaluation derivations for the same two expressions evaluated in 1a. Be sure to (1) indicate the redex in each step with curly braces and (2) label the result of each rule application with the rule name in square brackets.
1c. Desugaring (5 points)
Rather than giving bigstep or smallstep reduction rules to define the semantics of and
and or
, we can instead provide a way to translate these constructs to the if
expression we already know. This approach is known as desuguaring. In this case, (and E1 E2)
desugars to (i.e., translates to) (if E1 E2 #f)
and (or E1 E2)
desugars to (if E1 E1 E2)
. The key advantage of desugaring is that new constructs can be explained in terms of old ones rather than requiring new rules.
 Assume that
E1 ⇒* #f
.
Show that
(if E1 E2 #f) ⇒* #f
, and so(if E1 E2 #f)
acts like(and E1 E2)
in this case 
Show that
(if E1 E1 E2) ⇒* E2
, and so(if E1 E1 E2)
acts like(or E1 E2)
in this case

 Assume that
E1 ⇒* V1
, where V1 is not false.
Show that
(if E1 E2 #f) ⇒* E2
, and so(if E1 E2 #f)
acts like(and E1 E2)
in this case 
Show that
(if E1 E1 E2) ⇒* V1
, and so(if E1 E1 E2)
acts like(or E1 E2)
in this case

 The desugaring of
(or E1 E2)
to(if E1 E1 E2)
has a downside. What is it? (Later We will see how to fix this downside).
1d. Alternative logical operators (4 points)
We can imagine alternative logical operators conj
and disj
that are similar to and
and or
respectively, but have different smallstep semantics rules:
(conj #f V2) ⇒ #f [conj false]
(conj #t V2) ⇒ V2 [conj true]
(disj #f V2) ⇒ V2 [disj false]
(disj #t V2) ⇒ #t [disj true]
Note that in all four rules, both the first and second operands must be values in order for the reduction to take place.
Using these alternative smallstep reduction rules with other smallstep rules you know, give complete smallstep evaluation derivations for the following variant of the examples from parts 1a and 1b:
(disj (conj (< 1 2) (> 3 4)) (conj (= 5 6) (/ 7 0)))
(conj (disj (= 8 9) (* 10 11)) (disj (+ 12 13) ( 14 #f)))
1e. Comparisons to logical operators in other languages (6 points)

Which of Racket’s
and
/or
or the alternativedisj
/conj
operators behaves more like Python’sand
/or
infix operators? Refer to Python documentation and use sample expressions to justify your answer. 
In what way(s) are Racket’s
and
/or
like Java’s&&
/
? In what way(s) are they different? Refer to Java documentation and use sample expressions to justify your answer. 
In what way(s) are the alternative
disj
/conj
like Java’s&&
/
? In what way(s) are they different? Refer to Java documentation and use sample expressions to justify your answer.
2. The Evils of Two Lesses (14 points)
Consider the exact expression (3 < 4 < 2)
. (Do not remove the parens or any spaces, add any extra parens, or move around any symbols.) This expression is handled differently in different programming languages. For each of the following four programming languages, explain how this expression is handled in that language.

If the expression returns a value, specify that value and explain why that value is returned and how you know this fact. Give evidence from a sample program in that language that is has the value you claim.

If the expression causes an error, explain the nature of the error (is it a syntax error? type error? something else?) and how you know this fact. Again, give evidence from a sample program in that language that is has the error you claim.
 Java (3 points)
 JavaScript (4 points)
 Python (4 points)
 Racket (3 points)
3. Truthiness (20 points)
)
In the context of a conditional test, a value that chooses the then arm of the conditional is called truthy and a value that chooses the else arm of the conditional is called falsey (sometimes spelled falsy). We will use the term neither to refer to a value that cannot be used as a conditional test (causing a compiletime or runtime error).
We have seen that in Racket, the #f
value is falsey and every value that is not #f
is truthy; Racket has no neither values. This problem asks you to explore truthiness in other languages.
Below are four programming languages and links to their language specification documents. Based on information in the documents, determine which values are truthy, which are falsey, and which (if any) are neither. In your answers, explicitly cite all the section of the reference manuals that you used to determine your answer.

Python (4 points): https://docs.python.org/2/reference/

Java (5 points): https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf

JavaScript (a.k.a. EcmaScript) (5 points): http://www.ecmainternational.org/publications/files/ECMAST/Ecma262.pdf

C (6 points): http://www.openstd.org/JTC1/SC22/WG14/www/docs/n1570.pdf
Notes:

Hint: begin by searching for the terms if statement or conditional in the documents.

One of the purposes of this problem is to introduce you to language specification documents. These are the authoritative sources for answering any questions about the languages, even nitpicky ones. Although they can be somewhat dense, you should get into the habit of consulting them. For reasons you will understand after doing this problem, people who write/consult/cite such language specifications are sometimes called language lawyers.

In the EcmaScript specification, notions like ReturnIfAbrupt and CompletionRecord are used to model the dynamic semantics of exceptions and can be ignored for the purposes of this problem.

The C specification for the semantics of if statments uses the terminolgy “compares equal to 0”. Although it’s not obvious, this terminology is defined in Section 6.5.9 Equality operators on p. 96.

The correct answer for C is particularly challenging to track down. Consider the following in your answer:
 How are pointers treated by C? Are there any pointers treated as falsey?
 How are arrays treated by C?
4. Sum Fun (25 points)
In the Mon. Feb 06 class, we will define the following recursive factorial function:
(define fact
(lambda (n)
(if (= n 0)
1
(* n (fact ( n 1))))))
We can use the smallstep semantics introduced in lecture on Tue. Sep 13 to explain the evaluation of (fact 4)
. To simplify things, let’s introduce the abbreviation λ_fact for the follow lambda
expression:
(lambda (n) (if (= n 0) 1 (* n (fact ( n 1)))))
Then here is the smallstep evaluation derivation for (fact 4)
relative to an implicit environment that binds the name fact
to the expression λ_fact
:
({fact} 4)
⇒ {(λ_fact 4)}
⇒ (if {(= 4 0)} 1 (* 4 (fact ( 4 1))))
⇒ {(if #f 1 (* 4 (fact ( 4 1))))}
⇒ (* 4 ({fact} ( 4 1)))
⇒ (* 4 (λ_fact {( 4 1)}))
⇒ (* 4 {(λ_fact 3)})
⇒ (* 4 (if {(= 3 0)} 1 (* 3 (fact ( 3 1)))))
⇒ (* 4 {(if #f 1 (* 3 (fact ( 3 1))))})
⇒ (* 4 (* 3 ({fact} ( 3 1))))
⇒ (* 4 (* 3 (λ_fact {( 3 1)})))
⇒ (* 4 (* 3 {(λ_fact 2)}))
⇒ (* 4 (* 3 (if {(= 2 0)} 1 (* 2 (fact ( 2 1))))))
⇒ (* 4 (* 3 {(if #f 1 (* 2 (fact ( 2 1))))}))
⇒ (* 4 (* 3 (* 2 ({fact} ( 2 1)))))
⇒ (* 4 (* 3 (* 2 (λ_fact {( 2 1)}))))
⇒ (* 4 (* 3 (* 2 {(λ_fact 1)})))
⇒ (* 4 (* 3 (* 2 (if {(= 1 0)} 1 (* 1 (fact ( 1 1)))))))
⇒ (* 4 (* 3 (* 2 {(if #f 1 (* 1 (fact ( 1 1))))})))
⇒ (* 4 (* 3 (* 2 (* 1 ({fact} ( 1 1))))))
⇒ (* 4 (* 3 (* 2 (* 1 (λ_fact {( 1 1)})))))
⇒ (* 4 (* 3 (* 2 (* 1 {(λ_fact 0)}))))
⇒ (* 4 (* 3 (* 2 (* 1 (if {(= 0 0)} 1 (* 0 (fact ( 0 1))))))))
⇒ (* 4 (* 3 (* 2 (* 1 {(if #t 1 (* 0 (fact ( 0 1))))}))))
⇒ (* 4 (* 3 (* 2 {(* 1 1)})))
⇒ (* 4 (* 3 {(* 2 1)}))
⇒ (* 4 {(* 3 2)})
⇒ {(* 4 6)}
⇒ 24
To highlight the essential steps of such an evaluation, we will often use the notation E1 ⇒* E2
to mean that expression E1
rewrites to expression E2
by some number (possibly zero) of ⇒ steps. Below, we use this notation to omit all lines except for the ones involving (1) calls to λ_fact
on argument values or (2) calculation of the final result. Here’s an example of an abbreviated evaluation derivation for the above example:
({fact} 4)
⇒ {(λ_fact 4)}
⇒* (* 4 {(λ_fact 3)})
⇒* (* 4 (* 3 {(λ_fact 2)}))
⇒* (* 4 (* 3 (* 2 {(λ_fact 1)})))
⇒* (* 4 (* 3 (* 2 (* 1 {(λ_fact 0)}))))
⇒* (* 4 (* 3 (* 2 {(* 1 1)})))
⇒ (* 4 (* 3 {(* 2 1)}))
⇒ (* 4 {(* 3 2)})
⇒ {(* 4 6)}
⇒ 24
As another example, consider a recursive definition of a function for calculating the nth Fibonacci number:
(define fib
(lambda (n)
(if (<= n 1)
n
(+ (fib ( n 1)) (fib ( n 2))))))
Suppose λ_fib is an abbreviation for the following lambda
expression:
(lambda (n)
(if (<= n 1)
n
(+ (fib ( n 1)) (fib ( n 2))))))
Then here is an abbreviated evaluation derivation for (fib 4)
relative to an implicit environment that binds the name fib
to the expression λ_fib
:
({fib} 4)
⇒ {(λ_fib 4)}
⇒* (+ {(λ_fib 3)} (fib ( 4 2)))
⇒* (+ (+ {(λ_fib 2)} (fib ( 3 2))) (fib ( 4 2)))
⇒* (+ (+ (+ {(λ_fib 1)} (fib ( 2 2))) (fib ( 3 2))) (fib ( 4 2)))
⇒* (+ (+ (+ 1 {(λ_fib 0)}) (fib ( 3 2))) (fib ( 4 2)))
⇒* (+ (+ {(+ 1 0)} (fib ( 3 2))) (fib ( 4 2)))
⇒* (+ (+ 1 {(λ_fib 1)}) (fib ( 4 2)))
⇒* (+ {(+ 1 1)} (fib ( 4 2)))
⇒* (+ 2 {(λ_fib 2)})
⇒* (+ 2 (+ {(λ_fib 1)} (fib ( 2 2))))
⇒* (+ 2 (+ 1 {(λ_fib 0)}))
⇒* (+ 2 {(+ 1 0)})
⇒ {(+ 2 1)}
⇒ 3
Note that (λ_fib 4) ⇒* (+ (λ_fib 3) (fib ( 4 2)))
and not (λ_fib 4) ⇒* (+ (λ_fib 3) (λ_fib 2))
. Why? Because the smallstep evaluation of (+ E1 E2)
must fully evaluate E1
to a value V1
before any evaluation is performed on E2
. So the expression (fib ( 4 2))
is not simplified in any way until (λ_fib 3)
evaluates to 2
.
Also note that ⇒* (+ (+ 1 0) (fib ( 3 2))) ⇒ (+ 1 (fib ( 3 2)))
and not (+ (+ 1 0) (fib ( 3 2))) ⇒ (+ (+ 1 0) (λ_fib 1))
. The addition redex (+ 1 0)
must be evaluated to 1
before any work is done reducing (fib ( 3 2))
.
In this problem you are asked to reason about and create such abbreviated derivations.
a. sumbetween
(5 points)
Here is a definition of a sumbetween
function that returns the sum of all the integers between its two integer arguments (inclusive):
(define sumbetween
(lambda (lo hi)
(if (> lo hi)
0
(+ lo (sumbetween (+ lo 1) hi)))))
Using the abbreviated smallstep derivation notation shown above for (fact 4)
, show an abbreviated evaluation derivation for (sumbetween 3 7)
that shows the key steps in this derivation. Use the notation λ_sb as an abbreviation for the lambda
expression
(lambda (lo hi)
(if (> lo hi)
0
(+ lo (sumbetween (+ lo 1) hi))))
Your derivation should only show lines in which λ_sb
is called on values and lines involving the calculation of +
in (+ lo ...)
, but not any lines that involve if
, >
, or the calculation of +
in (+ lo 1)
.
b. Stack depth for sumbetween
(3 points)
Although the smallstep evaluation model does not have any explicit notion of stack frames, operations like *
in the recursive fact
definition and +
in the recursive sumbetween
definition correspond to pending operations that are remembered to be performed when control returns from the stack frame for a particular function invocation in a model based on stack frames. In the (fact 4)
example, the fact that the maximal sequence of nested multiplications, (* 4 (* 3 (* 2 (* 1 1))))
, has four multiplications corresponds to a nesting of five stack frames (one for each call of fact
on arguments 4
down to 0
).
In the case of fact
, we will call the maximal number of pending multiplications the stack depth. So (fact 4)
has a stack depth of 4, and (fact 100)
would have a stack depth of 100. So the stack depth of (fact n)
grows linearly in n
.
Let’s define the stack depth for sumbetween
to be the maximal number of nested pending addition operations in the smallstep evaluation derivation.

What is the stack depth for
(sumbetween 3 7)
? 
What is the stack depth for
(sumbetween 1 128)
? 
How does the stack depth for
(sumbetween 1 n)
grow withn
?
c. sumbetweenhalves
(8 points)
Here is a definition of a sumbetweenhalves
function that also returns the sum of all the integers between its two integer arguments (inclusive), but does so in a different way from sumbetween
:
(define sumbetweenhalves
(lambda (lo hi)
(if (> lo hi)
0
(if (= lo hi)
lo
(+ (sumbetweenhalves lo (quotient (+ lo hi) 2))
(sumbetweenhalves (+ 1 (quotient (+ lo hi) 2)) hi))))))
Show an abbreviated evaluation derivation for (sumbetweenhalves 3 7)
that shows the key steps in this derivation. Use the notation λ_sbh as an abbreviation for the lambda
expression
(lambda (lo hi)
(if (> lo hi)
0
(if (= lo hi)
lo
(+ (sumbetweenhalves lo (quotient (+ lo hi) 2))
(sumbetweenhalves (+ 1 (quotient (+ lo hi) 2)) hi))))))
Your derivation should be similar to the (fib 4)
example given above. Note that somes lines will have a mixture of calls to λ_sbh
on values and calls to sumbetweenhalves
on unevaluated expressions. See the (fib 4)
example for an explanation of this. For example, your derivation should begin like this:
(λ_sbh 3 7)
⇒* (+ (λ_sbh 3 5)
(sumbetweenhalves (+ 1 (quotient (+ 3 7) 2)) 7))
⇒* (+ (+ (λ_sbh 3 4)
(sumbetweenhalves (+ 1 (quotient (+ 3 5) 2)) 5))
(sumbetweenhalves (+ 1 (quotient (+ 3 7) 2)) 7))
d. Stack depth for sumbetweenhalves
(4 points)
Define the stack depth for a call to sumbetweenhalves
as the maximal number of nested pending +
operations from (+ (sumbetweenhalves ...) (sumbetweenhalves ...))
.

What is the stack depth for
(sumbetweenhalves 3 7)
? 
What is the stack depth for
(sumbetweenhalves 1 128)
? 
How does the stack depth for
(sumbetweenhalves 1 n)
grow withn
? 
Does
sumbetweenhalves
offer any benefit oversumbetween
as a way to calculate the sum of integers in a range?
e. sumbetweeniter
(3 points)
Now we consider one more way to calculate the sum of integers in a given range. The function sumbetweeniter
defined below also sums numbers in a given range using the helper function sumbetweentail
.
(define sumbetweeniter
(lambda (lo hi)
(sumbetweentail lo hi 0)))
(define sumbetweentail
(lambda (lo hi sumsofar)
(if (> lo hi)
sumsofar
(sumbetweentail (+ lo 1) hi (+ lo sumsofar)))))
Show an abbreviated evaluation derivation for (sumbetweeniter 3 7)
that shows the key steps in this derivation. Use the notation λ_sbi as an abbreviation for the lambda
expression
(lambda (lo hi)
(sumbetweentail lo hi 0)))
and the notation λ_sbt as an abbreviation for the lambda
expression
(lambda (lo hi sumsofar)
(if (> lo hi)
sumsofar
(sumbetweentail (+ lo 1) hi (+ lo sumsofar)))))
In your derivation, show only lines in which λ_sbi or λ_sbt are called on values.
f. Benefits of sumbetweeniter
/sumbetweentail
(2 points)
Do sumbetweeniter
/sumbetweentail
offer any benefit(s) over sumbetween
and sumbetweenhalves
? Explain.
Note: sumbetweentail
is an example of a socalled tailrecursive function, which we will study in a few weeks. Racket has no loop constructs, but they are not necessary because it is is possible to write all iterations (loops) in Racket using tail recursion.
5. Racket Recursion (16 points)
Although you’ve written recursive function definitions before in other courses, recursion is particularly important in CS251 for three reasons:

The list data structures in Racket and Standard ML are recursively defined, and so are naturally processed with recursion. (You will get lots of practice with these starting later in the week of Feb 06.)

Neither Racket nor Standard ML has looping constructs, so what you would express via loops in other languages must be expressed via recursion in these languages. (As suggested in Problem 4e above, a particular kind of recursion known as tail recursion can express anything expressible with loops in other languages and then some.)

Later in the semester we will study metaprograms – i.s., programs that manipulate other programs. Metaprograms typically process the abstract syntax tree (AST) structure of the program being manipulated. Such tree processing is most naturally expressed using recursion. Indeed, you’ve already seen that bigstep evaluation semantics is treerecursive in nature.
For each of the following two Racket function specifications, write and test a recursive function that satisfies that specification. In all of your definitions, you should use the following recursive problem solving strategy:

For which argument(s) is the function so simple that the answer can be returned immediately? This is the base case.

For the other case(s) (known as the recursive case(s)), use divide/conquer/glue:

divide: make one or more subproblems that are smaller instances of the given problem;

conquer: assume that the recursive function you’re defining simply works and returns the correct answer on all of the smaller problems.

glue: combine the result(s) of the recursive function call(s) with information in the original problem to create the correct result for the whole problem.

The fact
, fib
, sumbetween
and sumbetweenhalves
functions shown above are all instances of this strategy, and we will see many examples of recursion with list arguments in the coming week. (The sumbetweeniter
function is not an instance of this strategy because it introduces a helper function that changes the structure of the problem into an iteration.)
Notes:

For this problem, you should use Dr. Racket to create a single file named
yourAccountNameps2functions.rkt
that contains all the functions (including helper functions) that you define for this problem.  In your definitions, unless otherwise instructed, you should not introduce any recursive helper functions. (But you can define nonrecursive helper functions).
 If the following error message pops up during the testing of one of your functions, it mostly likely means that you have an infinite recursion that doesn’t reach its base case and runs out of memory due to a stack depth that cannot fit into available memory.

(8 points) Define a function
sumsquaresofintsdivisibleby
that takes three integer arguments (divisor
,lo
, andhi
) and returns the sum of the squares of all the integers betweenlo
andhi
(inclusive) that are evenly divisible bydivisor
.> (sumsquaresofintsdivisibleby 2 1 8) 120 ; = 2^2 + 4^2 + 6^2 + 64^2 > (sumsquaresofintsdivisibleby 3 1 10) 126 ; = 3^2 + 6^2 + 9^2 > (sumsquaresofintsdivisibleby 5 1 10) 125 ; = 5^2 + 10^2 > (sumsquaresofintsdivisibleby 7 1 10) 49 ; = 7^2 > (sumsquaresofintsdivisibleby 11 1 10) 0 ; no multiples of 11 between 1 and 10 > (sumsquaresofintsdivisibleby 11 7 15) 121 ; = 11^2 > (sumsquaresofintsdivisibleby 2 10 8) 0 ; the range "from 10 up to 8" is empty
Use the following helper function in this problem:
(define divisibleby? (lambda (num divisor) (= (remainder num divisor) 0)))

(8 points) A Hamming number is any positive integer expressible as 2^{i}⋅3^{j}⋅5^{k}. E.g., the Hamming numbers between 1 and 100 are:
1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 45 48 50 54 60 64 72 75 80 81 90 96 100
Define a function
hamming?
that takes a single numeric argument (including 0, negatives, and nonintegers), and returns#t
if the argument is a Hamming number and#f
if it is not. Your function need not work on arguments that are not numbers.> (hamming? 30) #t > (hamming? 31) #f > (hamming? 31) #f > (hamming? 3.141) #f > (hamming? 0) #f > (filter hamming? (range 100 101)) ; list all integers from 100 up to ; (but not including) 101 ; for which hamming? is true '(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 45 48 50 54 60 64 72 75 80 81 90 96 100)
Notes:
 Use
integer?
to test if a value is an integer.  Use
(and E1 E2 ... En)
to determine if all of the expressionsE1
,E2
, …,En
are true.  Use
(or E1 E2 ... En)
to determine if at least one the expressionsE1
,E2
, …,En
is true.  You needn’t use any
if
expressions in your definition. All you need areand
andor
. (But you can useif
if you want to.)  The
divisibleby?
helper function from Problem 5a is useful here as well.
 Use