Essentials of Programming Languages Exercises
Preface
Usage
Usage  X
Exercise 0.1 [★]
We often use phrases like “some languages have property X.” For each such phrase, find one or more languages that have the property and one or more languages that do not have the property. Feel free to ferret out this information from any descriptive book on programming languages (say Scott (2005), Sebesta (2007), or Pratt & Zelkowitz (2001)).
Skipped.
1 Inductive Sets of Data
1.1 Recursively Specified Data
1.1.1 Inductive Specification
Exercise 1.1 [★]
Write inductive definitions of the following sets. Write each definition in all three styles (topdown, bottomup, and rules of inference). Using your rules, show the derivation of some sample elements of each set.
 {3n + 2  n ∈ N}
 {2n + 3m + 1  n, m ∈ N}
 {(n, 2n + 1)  n ∈ N}
 {(n, n^{2})  n ∈ N} Do not mention squaring in your rules. As a hint, remember the equation (n + 1)^{2} = n^{2} + 2n + 1.

{3n + 2  n ∈ N}

Topdown:
n ∈ S if
 n = 2, or
 n − 3 ∈ S

Bottomup:
S is the smallest set that satisfying the following two properties:
 2 ∈ S, and
 If n ∈ S, then n + 3 ∈ S

Rules of inference:
 \(\dfrac{}{2 ∈ S}\)
 \(\dfrac{n ∈ S}{n + 3 ∈ S}\)


{2n + 3m + 1  n, m ∈ N}

Topdown:
n ∈ S if
 n = 1, or
 n − 2 ∈ S, or
 n − 3 ∈ S

Bottomup:
S is the smallest set that satisfying the following two properties:
 1 ∈ S, and
 If n ∈ S, then n + 2 ∈ S, and
 If n ∈ S, then n + 3 ∈ S

Rules of inference:
 \(\dfrac{}{1 ∈ S}\)
 \(\dfrac{n ∈ S}{n + 2 ∈ S}\)
 \(\dfrac{n ∈ S}{n + 3 ∈ S}\)


{(n, 2n + 1)  n ∈ N}

Topdown:
(m, n) ∈ S if
 m = 0 and n = 1, or
 (m − 1, n − 2) ∈ S

Bottomup:
S is the smallest set that satisfying the following two properties:
 (0, 1) ∈ S, and
 If (m, n) ∈ S, then (m + 1, n + 2) ∈ S

Rules of inference:
 \(\dfrac{}{(0, 1) ∈ S}\)
 \(\dfrac{(m, n) ∈ S}{(m + 1, n + 2) ∈ S}\)


{(n, n^{2})  n ∈ N}

Topdown:
(m, n) ∈ S if
 m = 0 and n = 0, or
 (m − 1, n − 2m + 1) ∈ S

Bottomup:
S is the smallest set that satisfying the following two properties:
 (0, 0) ∈ S, and
 If (m, n) ∈ S, then (m + 1, n + 2m + 1) ∈ S

Rules of inference:
 \(\dfrac{}{(0, 0) ∈ S}\)
 \(\dfrac{(m, n) ∈ S}{(m + 1, n + 2m + 1) ∈ S}\)

Exercise 1.2 [★★]
What sets are defined by the following pairs of rules? Explain why.
 \((0, 1) ∈ S \quad \dfrac{(n, k) ∈ S}{(n + 1, k + 7) ∈ S}\)
 \((0, 1) ∈ S \quad \dfrac{(n, k) ∈ S}{(n + 1, 2k) ∈ S}\)
 \((0, 0, 1) ∈ S \quad \dfrac{(n, i, j) ∈ S}{(n + 1, j, i + j) ∈ S}\)
 [★★★] \((0, 1, 0) ∈ S \quad \dfrac{(n, i, j) ∈ S}{(n + 1, i + 2, i + j) ∈ S}\)

\((0, 1) ∈ S \quad \dfrac{(n, k) ∈ S}{(n + 1, k + 7) ∈ S}\)
{(n, 7n + 1)  n ∈ N}

\((0, 1) ∈ S \quad \dfrac{(n, k) ∈ S}{(n + 1, 2k) ∈ S}\)
{(n, 2^{n})  n ∈ N}

\((0, 0, 1) ∈ S \quad \dfrac{(n, i, j) ∈ S}{(n + 1, j, i + j) ∈ S}\)
{(n, f(n), f(n + 1))  n ∈ N, f(0) = 0, f(1) = 1, f(n + 2) = f(n) + f(n + 1)}

\((0, 1, 0) ∈ S \quad \dfrac{(n, i, j) ∈ S}{(n + 1, i + 2, i + j) ∈ S}\)
{(n, 2n + 1, n^{2})  n ∈ N}
Exercise 1.3 [★★★]
Find a set T of natural numbers such that 0 ∈ T, and whenever n ∈ T, then n + 3 ∈ T, but T ≠ S, where S is the set defined in definition 1.1.2.
Let T = N.
1.1.2 Defining Sets Using Grammars
Exercise 1.4 [★]
Write a derivation from ListofInt to
(7 . (3 . (14 . ())))
.
ListofInt
⇒ (
Int .
ListofInt)
⇒ (7 .
ListofInt)
⇒ (7 . (
Int .
ListofInt))
⇒ (7 . (3 .
ListofInt))
⇒ (7 . (3 . (
Int .
ListofInt)))
⇒ (7 . (3 . (14 .
ListofInt)))
⇒ (7 . (3 . (14 . ())))
1.1.3 Induction
Exercise 1.5 [★★]
Prove that if e ∈ LcExp, then there are the same number of left and right parentheses in e.
By induction on the structure of LcExp.
If e is of Identifier form, it has 0 left parenthesis and 0 right parenthesis, the hypothesis holds.
If e is of (lambda (
Identifier)
LcExp)
form, the Identifier has 0 parenthesis. By induction, LcExp has
the same number of left and right parentheses. Let the number be n, then e has n + 2 left parentheses and n + 2
right parentheses. The hypothesis holds.
If e is of (
LcExp LcExp)
form, let m be the number of left or right parentheses in the first LcExp, let
n be the number of left or right parentheses in the second LcExp, then e has m + n + 1 left parentheses and
m + n + 1 right parentheses. The hypothesis holds.
1.2 Deriving Recursive Programs
1.2.2 nthelement
Exercise 1.6 [★]
If we reversed the order of the tests in
nthelement
, what would go wrong?
car
may be applied to empty list.
Exercise 1.7 [★★]
The error message from
nthelement
is uninformative. Rewritenthelement
so that it produces a more informative error message, such as “(a b c)
does not have 8 elements.”
1.2.3 removefirst
Exercise 1.8 [★]
In the definition of removefirst, if the last line were replaced by
(removefirst s (cdr los))
, what function would the resulting procedure compute? Give the contract, including the usage statement, for the revised procedure.
removefirst : Sym × Listof(Sym) → Listof(Sym)
usage: (removefirst
s los)
returns a sub of list from los, starting from the symbol after the first
s. If los doesn’t contain s, an empty list is returned.
Exercise 1.9 [★★]
Define
remove
, which is likeremovefirst
, except that it removes all occurrences of a given symbol from a list of symbols, not just the first.
1.2.4 occursfree?
Exercise 1.10 [★]
We typically use “or” to mean “inclusive or”. What other meanings can “or” have?
Exclusive or.
1.2.5 subst
Exercise 1.11 [★]
In the last line of
substinsexp
, the recursion is on sexp and not a smaller substructure. Why is the recursion guaranteed to halt?
Because subst
recurs on smaller substructure. We can replace the call to substinsexp
with the body of
substinsexp
, then subst
becomes a normal recursive on a smaller substructure.
Exercise 1.12 [★]
Eliminate the one call to
substinsexp
insubst
by replacing it by its definition and simplifying the resulting procedure. The result will be a version ofsubst
that does not needsubstinsexp
. This technique is called inlining, and is used by optimizing compilers.
Exercise 1.13 [★★]
In our example, we began by eliminating the Kleene star in the grammar for Slist. Write
subst
following the original grammar by usingmap
.
1.3 Auxiliary Procedures and Context Arguments
1.3.0
Exercise 1.14 [★★]
Given the assumption 0 ≤ n < length(v), prove that
partialvectorsum
is correct.
Since 0 ≤ n < length(v), we know that length(v) is at least 1, so that v contains at least one element. We
prove partialvectorsum
is correct by induction over n.
Base case: if n equals to 0, (partialvectorsum
v n)
equals to (vectorref
v 0)
, which equals to
\(\sum_{i = 0}^0 v_i\), the claim holds.
Inductive case: if n ≠ 0, n (partialvectorsum
v n)
equals to
(add (vectorref
v n) (partialvectorsum
v (
n 1)))
, which equals to
\(v_n + \sum_{i = 0}^{n  1} v_i\), which equals to \(\sum_{i = 0}^n v_i\), the claim holds.
1.4 Exercises
1.4.0
Exercise 1.15 [★]
(duple n x)
returns a list containingn
copies ofx
.> > >
Exercise 1.16 [★]
(invert lst)
, where lst is a list of 2lists (lists of length two), returns a list with each 2list reversed.>
Exercise 1.17 [★]
(down lst)
wraps parentheses around each toplevel element oflst
.> > >
Exercise 1.18 [★]
(swapper s1 s2 slist)
returns a list the same as slist, but with all occurrences ofs1
replaced bys2
and all occurrences ofs2
replaced bys1
.> > >
Exercise 1.19 [★]
(listset lst n x)
returns a list likelst
, except that then
th element, using zerobased indexing, isx
.> >
Exercise 1.20 [★]
(countoccurrences s slist)
returns the number of occurrences ofs
inslist
.> 3 > 3 > 0
Exercise 1.21 [★★]
(product sos1 sos2)
, wheresos1
andsos2
are each a list of symbols without repetitions, returns a list of 2lists that represents the Cartesian product ofsos1
andsos2
. The 2lists may appear in any order.>
Exercise 1.22 [★★]
(filterin pred lst)
returns the list of those elements inlst
that satisfy the predicatepred
.> >
Exercise 1.23 [★★]
(listindex pred lst)
returns the 0based position of the first element oflst
that satisfies the predicatepred
. If no element oflst
satisfies the predicate, thenlistindex
returns#f
.> 1 > 0 > #f
Exercise 1.24 [★★]
(every? pred lst)
returns#f
if any element oflst
fails to satisfypred
, and returns#t
otherwise.> #f > #t
Exercise 1.25 [★★]
(exists? pred lst)
returns#t
if any element oflst
satisfiespred
, and returns#f
otherwise.> #t > #f
Exercise 1.26 [★★]
(up lst)
removes a pair of parentheses from each toplevel element oflst
. If a toplevel element is not a list, it is included in the result, as is. The value of(up (down lst))
is equivalent to lst, but(down (up lst))
is not necessarilylst
. (See exercise 1.17.)> >
Exercise 1.27 [★★]
(flatten slist)
returns a list of the symbols contained inslist
in the order in which they occur whenslist
is printed. Intuitively,flatten
removes all the inner parentheses from its argument.> > > >
Exercise 1.28 [★★]
(merge loi1 loi2)
, whereloi1
andloi2
are lists of integers that are sorted in ascending order, returns a sorted list of all the integers inloi1
andloi2
.> >
Exercise 1.29 [★★]
(sort loi)
returns a list of the elements ofloi
in ascending order.>
Exercise 1.30 [★★]
(sort/predicate pred loi)
returns a list of elements sorted by the predicate.> >
Exercise 1.31 [★]
Write the following procedures for calculating on a bintree (definition 1.1.7):
leaf
andinteriornode
, which build bintrees,leaf?
, which tests whether a bintree is a leaf, andlson
,rson
, andcontentsof
, which extract the components of a node.contentsof
should work on both leaves and interior nodes.
Exercise 1.32 [★]
Write a procedure
doubletree
that takes a bintree, as represented in definition 1.1.7, and produces another bintree like the original, but with all the integers in the leaves doubled.
Exercise 1.33 [★★]
Write a procedure
markleaveswithreddepth
that takes a bintree (definition 1.1.7), and produces a bintree of the same shape as the original, except that in the new tree, each leaf contains the number of nodes between it and the root that contain the symbolred
. For example, the expression
which is written using the procedures defined in exercise 1.31, should return the bintree
Exercise 1.34 [★★★]
Write a procedure
path
that takes an integern
and a binary search treebst
(page 10) that contains the integern
, and returns a list ofleft
s andright
s showing how to find the node containingn
. Ifn
is found at the root, it returns the empty list.>
Exercise 1.35 [★★★]
Write a procedure
numberleaves
that takes a bintree, and produces a bintree like the original, except the contents of the leaves are numbered starting from 0. For example,
should return
Exercise 1.36 [★★★]
Write a procedure
g
such thatnumberelements
from page 23 could be defined as
2 Data Abstraction
2.1 Specifying Data via Interfaces
2.1.0
Exercise 2.1 [★]
Implement the four required operations for bigits. Then use your implementation to calculate the factorial of 10. How does the execution time vary as this argument changes? How does the execution time vary as the base changes? Explain why.
When the argument of factorial
becomes larger, the execution time becomes longer. Obviously.
The execution time becomes shorter when the base becomes larger. I think that’s because fewer allocations are needed when the base becomes larger.
Exercise 2.2 [★★]
Analyze each of these proposed representations critically. To what extent do they succeed or fail in satisfying the specification of the data type?
 Unary representation. Too much space consumed.
 Scheme number representation. Not every language has native big integer support.
 Bignum representation. Not easy to implement.
Exercise 2.3 [★★]
Define a representation of all the integers (negative and nonnegative) as difftrees, where a difftree is a list defined by the grammar
Difftree ::=
(one)
(diff
Difftree Difftree)
The list
(one)
represents 1. If t_{1} represents n_{1} and t_{2} represents n_{2}, then(diff t1 t2)
is a representation of n_{1} − n_{2}.So both
(one)
and(diff (one) (diff (one) (one)))
are representations of 1;(diff (diff (one) (one)) (one))
is a representation of 1.
 Show that every number has infinitely many representations in this system.
 Turn this representation of the integers into an implementation by writing
zero
,iszero?
,successor
, andpredecessor
, as specified on page 32, except that now the negative integers are also represented. Your procedures should take as input any of the multiple legal representations of an integer in this scheme. For example, if yoursuccessor
procedure is given any of the infinitely many legal representations of 1, it should produce one of the legal representations of 2. It is permissible for different legal representations of 1 to yield different legal representations of 2. Write a procedure
difftreeplus
that does addition in this representation. Your procedure should be optimized for the difftree representation, and should do its work in a constant amount of time (independent of the size of its inputs). In particular, it should not be recursive.
 0 has infinitely many representations:
(diff (one) (one))
,(diff (diff (one) (one)) (diff (one) (one)))
, and so on. n can be represented as(diff
n 0)
, since 0 has infinitely many representations, n has infinitely many representations.
2.2 Representation Strategies for Data Types
2.2.1 The Environment Interface
Exercise 2.4 [★★]
Consider the data type of stacks of values, with an interface consisting of the procedures
emptystack
,push
,pop
,top
, andemptystack?
. Write a specification for these operations in the style of the example above. Which operations are constructors and which are observers?
(emptystack)
= ⌈∅⌉(push
⌈f⌉ v)
= ⌈g⌉, where g(0) = v, and g(n + 1) = f(n)(pop
⌈f⌉)
= g, where g(n) = f(n + 1)(top
⌈f⌉)
= ⌈f(0)⌉(emptystack?
⌈f⌉)
=#t
if f = ∅,#f
otherwise
Constructors: emptystack
, push
and pop
.
Observers: top
and emptystack?
.
2.2.2 Data Structure Representation
Exercise 2.5 [★]
We can use any data structure for representing environments, if we can distinguish empty environments from nonempty ones, and in which one can extract the pieces of a nonempty environment. Implement environments using a representation in which the empty environment is represented as the empty list, and in which
extendenv
builds an environment that looks like┌───┬───┐ │ ╷ │ ╶─┼─► savedenv └─┼─┴───┘ ▼ ┌───┬───┐ │ ╷ │ ╷ │ └─┼─┴─┼─┘ ┌───┘ └───┐ ▼ ▼ savedvar savedval
This is called an alist or associationlist representation.
Exercise 2.6 [★]
Invent at least three different representations of the environment interface and implement them.
Deferred.
Exercise 2.7 [★]
Rewrite
applyenv
in figure 2.1 to give a more informative error message.
Exercise 2.8 [★]
Add to the environment interface an observer called
emptyenv?
and implement it using the alist representation.
(emptyenv?
⌈f⌉)
= #t
if f = ∅, #f
otherwise.
Exercise 2.9 [★]
Add to the environment interface an observer called
hasbinding?
that takes an environment env and a variable s and tests to see if s has an associated value in env. Implement it using the alist representation.
(hasbinding?
⌈f⌉)
= #t
if f(var) = val for some var and val, #f
otherwise.
Exercise 2.10 [★]
Add to the environment interface a constructor
extendenv*
, and implement it using the alist representation. This constructor takes a list of variables, a list of values of the same length, and an environment, and is specified by
(extendenv* (
var_{1} … var_{k}) (
val_{1} … val_{k})
⌈f⌉)
= ⌈g⌉, where g(var) = val_{i} if var = var_{i} for some i such that 1 ≤ i ≤ k, f(var) otherwise.
Exercise 2.11 [★★]
A naïve implementation of
extendenv*
from the preceding exercise requires time proportional to k to run. It is possible to represent environments so thatextendenv*
requires only constant time: represent the empty environment by the empty list, and represent a nonempty environment by the data structure┌───┬───┐ │ ╷ │ ╶─┼─► savedenv └─┼─┴───┘ ▼ ┌───┬───┐ │ ╷ │ ╷ │ └─┼─┴─┼─┘ ┌───┘ └───┐ ▼ ▼ savedvars savedvals
Such an environment might look like
backbone │ ┌───┬───┐ ▼ ┌───┬───┐ ┌───┬───┐ │ ╷ │ ╶─┼──────────►│ ╷ │ ╶─┼──────────►│ ╷ │ ╶─┼──────────► rest of environment └─┼─┴───┘ └─┼─┴───┘ └─┼─┴───┘ ▼ ▼ ▼ ┌───┬───┐ ┌───┬───┐ ┌───┬───┐ │ ╷ │ ╷ │ │ ╷ │ ╷ │ │ ╷ │ ╷ │ └─┼─┴─┼─┘ └─┼─┴─┼─┘ └─┼─┴─┼─┘ ┌──┘ └──┐ ┌──┘ └──┐ ┌──┘ └──┐ ▼ ▼ ▼ ▼ ▼ ▼ (a b c) (11 12 13) (x z) (66 77) (x y) (88 99)
This is called the ribcage representation. The environment is represented as a list of pairs called ribs; each left rib is a list of variables and each right rib is the corresponding list of values.
Implement the environment interface, including
extendenv*
, in this representation.
2.2.3 Procedural Representation
Exercise 2.12 [★]
Implement the stack data type of exercise 2.4 using a procedural representation.
Exercise 2.13 [★★]
Extend the procedural representation to implement
emptyenv?
by representing the environment by a list of two procedures: one that returns the value associated with a variable, as before, and one that returns whether or not the environment is empty.
Exercise 2.14 [★★]
Extend the representation of the preceding exercise to include a third procedure that implements
hasbinding?
(see exercise 2.9).
2.3 Interfaces for Recursive Data Types
2.3.0
Exercise 2.15 [★]
Implement the lambdacalculus expression interface for the representation specified by the grammar above.
Exercise 2.16 [★]
Modify the implementation to use a representation in which there are no parentheses around the bound variable in a
lambda
expression.
Remaining implementations are the same as the ones in exercise 2.15.
Exercise 2.17 [★]
Invent at least two other representations of the data type of lambdacalculus expressions and implement them.
Skipped.
Exercise 2.18 [★]
We usually represent a sequence of values as a list. In this representation, it is easy to move from one element in a sequence to the next, but it is hard to move from one element to the preceding one without the help of context arguments. Implement nonempty bidirectional sequences of integers, as suggested by the grammar
NodeInSequence ::=
(
Int Listof(
Int)
Listof(
Int))
The first list of numbers is the elements of the sequence preceding the current one, in reverse order, and the second list is the elements of the sequence after the current one. For example,
(6 (5 4 3 2 1) (7 8 9))
represents the list(1 2 3 4 5 6 7 8 9)
, with the focus on the element 6.In this representation, implement the procedure
number>sequence
, which takes a number and produces a sequence consisting of exactly that number. Also implementcurrentelement
,movetoleft
,movetoright
,inserttoleft
,inserttoright
,atleftend?
, andatrightend?
.For example:
> > 6 > > > >
The procedure
movetoright
should fail if its argument is at the right end of the sequence, and the proceduremovetoleft
should fail if its argument is at the left end of the sequence.
Exercise 2.19 [★]
A binary tree with empty leaves and with interior nodes labeled with integers could be represented using the grammar
Bintree ::=
()
(
Int Bintree Bintree)
In this representation, implement the procedure
number>bintree
, which takes a number and produces a binary tree consisting of a single node containing that number. Also implementcurrentelement
,movetoleftson
,movetorightson
,atleaf?
,inserttoleft
, andinserttoright
. For example,> > > t1 > > 12 > #t >
Exercise 2.20 [★★★]
In the representation of binary trees in exercise 2.19 it is easy to move from a parent node to one of its sons, but it is impossible to move from a son to its parent without the help of context arguments. Extend the representation of lists in exercise 2.18 to represent nodes in a binary tree. As a hint, consider representing the portion of the tree above the current node by a reversed list, as in exercise 2.18.
In this representation, implement the procedures from exercise 2.19. Also implement
moveup
andatroot?
.
2.4 A Tool for Defining Recursive Data Types
2.4.0
Exercise 2.21 [★]
Implement the data type of environments, as in section 2.2.2, using
definedatatype
. Then includehasbinding?
of exercise 2.9.
Exercise 2.22 [★]
Using
definedatatype
, implement the stack data type of exercise 2.4.
Exercise 2.23 [★]
The definition of
lcexp
ignores the condition in definition 1.1.8 that says “Identifier is any symbol other thanlambda
.” Modify the definition of identifier? to capture this condition. As a hint, remember that any predicate can be used indefinedatatype
, even ones you define.
Exercise 2.24 [★]
Here is a definition of binary trees using
definedatatype
.
Implement a
bintreetolist
procedure for binary trees, so that(bintreetolist (interiornode 'a (leafnode 3) (leafnode 4)))
returns the list
Exercise 2.25 [★★]
Use
cases
to writemaxinterior
, which takes a binary tree of integers (as in the preceding exercise) with at least one interior node and returns the symbol associated with an interior node with a maximal leaf sum.> > > > foo > baz
The last invocation of
maxinterior
might also have returnedfoo
, since both thefoo
andbaz
nodes have a leaf sum of 5.
Exercise 2.26 [★★]
Here is another version of exercise 1.33. Consider a set of trees given by the following grammar:
Redbluetree ::= Redbluesubtree
Redbluesubtree ::=
(rednode
Redbluesubtree Redbluesubtree)
::=(bluenode
{Redbluesubtree}^{∗})
::=(leafnode Int)
Write an equivalent definition using
definedatatype
, and use the resulting interface to write a procedure that takes a tree and builds a tree of the same shape, except that each leaf node is replaced by a leaf node that contains the number of red nodes on the path between it and the root.
2.5 Abstract Syntax and Its Representation
2.5.0
Exercise 2.27 [★]
Draw the abstract syntax tree for the lambda calculus expressions
┌─────────┐
│ appexp │
└────┬────┘
┌──────┴───────┐
rator rand
┌─────┴──────┐ ┌────┴────┐
│ lambdaexp │ │ varexp │
└─────┬──────┘ └────┬────┘
┌────┴────┐ │
boundvar body var
┌─┴─┐ ┌────┴────┐ ┌─┴─┐
│ a │ │ appexp │ │ c │
└───┘ └────┬────┘ └───┘
┌─────┴──────┐
rator rand
┌────┴────┐ ┌────┴────┐
│ varexp │ │ varexp │
└────┬────┘ └────┬────┘
│ │
var var
┌─┴─┐ ┌─┴─┐
│ a │ │ b │
└───┘ └───┘
┌────────────┐
│ lambdaexp │
└─────┬──────┘
┌────┴─────┐
boundvar body
┌─┴─┐ ┌─────┴──────┐
│ x │ │ lambdaexp │
└───┘ └─────┬──────┘
┌────┴────┐
boundvar body
┌─┴─┐ ┌────┴────┐
│ y │ │ appexp │
└───┘ └────┬────┘
┌──────┴───────┐
rator rand
┌─────┴──────┐ ┌────┴────┐
│ lambdaexp │ │ varexp │
└─────┬──────┘ └────┬────┘
┌────┴────┐ │
boundvar body var
┌─┴─┐ ┌────┴────┐ ┌─┴─┐
│ x │ │ appexp │ │ x │
└───┘ └────┬────┘ └───┘
┌─────┴──────┐
rator rand
┌────┴────┐ ┌────┴────┐
│ varexp │ │ varexp │
└────┬────┘ └────┬────┘
│ │
var var
┌─┴─┐ ┌─┴─┐
│ x │ │ y │
└───┘ └───┘
Exercise 2.28 [★]
Write an unparser that converts the abstract syntax of an lcexp into a string that matches the second grammar in this section (page 52).
Exercise 2.29 [★]
Where a Kleene star or plus (page 7) is used in concrete syntax, it is most convenient to use a list of associated subtrees when constructing an abstract syntax tree. For example, if the grammar for lambdacalculus expressions had been
Lcexp ::= Identifier
varexp (var)
::=(lambda (
{Identifier}^{∗})
Lcexp)
lambdaexp (boundvars body)
::=(
Lcexp {Lcexp}^{∗})
appexp (rator rands)
then the predicate for the
boundvars
field could be(listof identifier?)
, and the predicate for therands
field could be(listof lcexp?)
. Write adefinedatatype
and a parser for this grammar that works in this way.
Exercise 2.30 [★★]
The procedure
parseexpression
as defined above is fragile: it does not detect several possible syntactic errors, such as(a b c)
, and aborts with inappropriate error messages for other expressions, such as(lambda)
. Modify it so that it is robust, accepting any sexp and issuing an appropriate error message if the sexp does not represent a lambdacalculus expression.
Exercise 2.31 [★★]
Sometimes it is useful to specify a concrete syntax as a sequence of symbols and integers, surrounded by parentheses. For example, one might define the set of prefix lists by
Prefixlist ::=
(
Prefixexp)
Prefixexp ::= Int
::=
Prefixexp Prefixexpso that
(  3 2  4  12 7)
is a legal prefix list. This is sometimes called Polish prefix notation, after its inventor, Jan Łukasiewicz. Write a parser to convert a prefixlist to the abstract syntax
so that the example above produces the same abstract syntax tree as the sequence of constructors
As a hint, consider writing a procedure that takes a list and produces a
prefixexp
and the list of leftover list elements.
3 Expressions
3.2 LET: A Simple Language
3.2.4 Specifying the Behavior of Expressions
Exercise 3.1 [★]
In figure 3.3, list all the places where we used the fact that ⌊⌈n⌉⌋ = n.
Skipped.
Exercise 3.2 [★★]
Give an expressed value val ∈ ExpVal for which ⌈⌊val⌋⌉ ≠ val.
Not sure, but maybe when val is constructed using a Bool?
3.2.8 Implementing the Specification of LET
Exercise 3.3 [★]
Why is subtraction a better choice than addition for our single arithmetic operation?
One reason I can think of, is that subtraction is not commutative, that is \(a  b\) may not equal to \(b  a\). If our implementation of subtraction is incorrect, we can discover the error quickly.
Exercise 3.4 [★]
Write out the derivation of figure 3.4 as a derivation tree in the style of the one on page 5.
\[ \dfrac{\dfrac{\dfrac{\dfrac{\texttt{(valueof «x» $ρ$)} = 33} {\texttt{(valueof «(x, 11)» $ρ$)} = 22}} {\texttt{(valueof «zero?((x, 11))» $ρ$)} = \texttt{(boolval #f)}}} {\texttt{(valueof «if zero?((x, 11)) then (y, 2) else (y, 4)» $ρ$)} = \texttt{(valueof «(y, 4)» $ρ$)}} \quad \dfrac{\texttt{(valueof «y» $ρ$)} = 22} {\texttt{(valueof «(y, 4)» $ρ$)} = 18}} {\texttt{(valueof «if zero?((x, 11)) then (y, 2) else (y, 4)» $ρ$)} = 18} \]
Exercise 3.5 [★]
Write out the derivation of figure 3.5 as a derivation tree in the style of the one on page 5.
Skipped.
Exercise 3.6 [★]
Extend the language by adding a new operator minus that takes one argument, n, and returns −n. For example, the value of
minus((minus(5), 9))
should be 14.
Solution is implemented here.
Exercise 3.7 [★]
Extend the language by adding operators for addition, multiplication, and integer quotient.
Solution is implemented here.
Exercise 3.8 [★]
Add a numeric equality predicate
equal?
and numeric order predicatesgreater?
andless?
to the set of operations in the defined language.
Solution is implemented here.
Exercise 3.9 [★★]
Add list processing operations to the language, including
cons
,car
,cdr
,null?
andemptylist
. A list should be able to contain any expressed value, including another list. Give the definitions of the expressed and denoted values of the language, as in section 3.2.2. For example,let x = 4 in cons
should return an expressed value that represents the list
(4 (3))
.
Solution is implemented here.
Exercise 3.10 [★★]
Add an operation
list
to the language. This operation should take any number of arguments, and return an expressed value containing the list of their values. For example,let x = 4 in list
should return an expressed value that represents the list
(4 3 1)
.
Solution is implemented here.
Exercise 3.11 [★]
In a real language, one might have many operators such as those in the preceding exercises. Rearrange the code in the interpreter so that it is easy to add new operators.
Solution is implemented here.
Exercise 3.12 [★]
Add to the defined language a facility that adds a
cond
expression. Use the grammarExpression ::=
cond
{Expression==>
Expression}^{∗}end
In this expression, the expressions on the lefthand sides of the
==>
’s are evaluated in order until one of them returns a true value. Then the value of the entire expression is the value of the corresponding righthand expression. If none of the tests succeeds, the expression should report an error.
Solution is implemented here.
Exercise 3.13 [★]
Change the values of the language so that integers are the only expressed values. Modify
if
so that the value 0 is treated as false and all other values are treated as true. Modify the predicates accordingly.
Solution is implemented here.
Exercise 3.14 [★★]
As an alternative to the preceding exercise, add a new nonterminal Boolexp of boolean expressions to the language. Change the production for conditional expressions to say
Expression ::=
if
Boolexpthen
Expressionelse
ExpressionWrite suitable productions for Boolexp and implement
valueofboolexp
. Where do the predicates of exercise 3.8 wind up in this organization?
I’ll deal with this one later.
Exercise 3.15 [★]
Extend the language by adding a new operation
Solution is implemented here.
Because print
cause a side effect while our specification framework does not have something to do this.
Exercise 3.16 [★★]
Extend the language so that a
let
declaration can declare an arbitrary number of variables, using the grammarExpression ::=
let
{Identifier=
Expression}^{∗}in
ExpressionAs in Scheme’s
let
, each of the righthand sides is evaluated in the current environment, and the body is evaluated with each new variable bound to the value of its associated righthand side. For example,let x = 30 in let x =  y =  in 
should evaluate to 1.
Solution is implemented here.
Exercise 3.17 [★★]
Extend the language with a
let*
expression that works like Scheme’slet*
, so thatlet x = 30 in let* x =  y =  in 
should evaluate to 2.
Solution is implemented here.
Exercise 3.18 [★★]
Add an expression to the defined language:
Expression ::=
unpack
{Identifier}^{∗}=
Expressionin
Expressionso that
unpack x y z = lst in ...
bindsx
,y
, andz
to the elements oflst
iflst
is a list of exactly three elements, and reports an error otherwise. For example, the value oflet u = 7 in unpack x y = cons in 
should be 4.
Solution is implemented here.
3.3 PROC: A Language with Procedures
3.3.2 Representing Procedures
Exercise 3.19 [★]
In many languages, procedures must be created and named at the same time. Modify the language of this section to have this property by replacing the
proc
expression with aletproc
expression.
Skipped.
Exercise 3.20 [★]
In PROC, procedures have only one argument, but one can get the effect of multiple argument procedures by using procedures that return other procedures. For example, one might write code like
let f = proc proc ... in
This trick is called Currying, and the procedure is said to be Curried. Write a Curried procedure that takes two arguments and returns their sum. You can write x + y in our language by writing
(
x, (0,
y))
.
proc 
proc
Exercise 3.21 [★★]
Extend the language of this section to include procedures with multiple arguments and calls with multiple operands, as suggested by the grammar
Expression ::=
proc (
{Identifier}^{∗(,)})
Expression
Expression ::=(
Expression {Expression}^{∗})
Solution is implemented here.
Exercise 3.22 [★★★]
The concrete syntax of this section uses different syntax for a builtin operation, such as difference, from a procedure call. Modify the concrete syntax so that the user of this language need not know which operations are builtin and which are defined procedures. This exercise may range from very easy to hard, depending on the parsing technology being used.
Solution is implemented here.
Exercise 3.23 [★★]
What is the value of the following PROC program?
let makemult = proc proc if zero? then 0 else  in let times4 = proc in
Use the tricks of this program to write a procedure for factorial in PROC. As a hint, remember that you can use Currying (exercise 3.20) to define a twoargument procedure
times
.
Value of given program is 12.
The procedure of factorial:
let maketimes = proc
proc
proc
if zero?
then 0
else 
in let times =
in let makefact = proc
proc
if zero?
then 1
else
in
Exercise 3.24 [★★]
Use the tricks of the program above to write the pair of mutually recursive procedures,
odd
andeven
, as in exercise 3.32.
odd
:
let false = zero?
in let true = zero?
in let makeeven = proc
proc
proc
if zero?
then true
else
in let makeodd = proc
proc
proc
if zero?
then false
else
in
even
:
let false = zero?
in let true = zero?
in let makeeven = proc
proc
proc
if zero?
then true
else
in let makeodd = proc
proc
proc
if zero?
then false
else
in
Exercise 3.25 [★]
The tricks of the previous exercises can be generalized to show that we can define any recursive procedure in PROC. Consider the following bit of code:
let makerec = proc let d = proc proc in proc in let maketimes4 = proc proc if zero? then 0 else  in let times4 = in
Show that it returns 12.
maketimes4
is a procedure that takes a times4
procedure and returns a times4
procedure. First we convert
maketimes4
to a procedure maker
that takes a maker
and returns a times4
procedure (assume we use f
to
represent maketimes4
):
proc let maker = proc
let recursiveproc =
in
in ...
But the code would not work because once we call (maker maker)
, it will first call (maker maker)
which will cause
infinite recursion. We will fix this by wrapping (maker maker)
inside another procedure:
proc let maker = proc
proc
let recursiveproc =
in
in ...
Now we get a maker
, we call the maker
with maker
, we will get a recursive version of f
:
proc let maker = proc
proc
let recursiveproc =
in
in
Let’s run the program:
let makerec = proc
let maker = proc
proc
let recursiveproc =
in
in
in let maketimes4 = proc
proc
if zero?
then 0
else 
in let times4 =
in
Yep, the result is also 12. Although it is a little different than the original one.
Exercise 3.26 [★★]
In our datastructure representation of procedures, we have kept the entire environment in the closure. But of course all we need are the bindings for the free variables. Modify the representation of procedures to retain only the free variables.
Here is a function that filters free variables in the environment:
Exercise 3.27 [★]
Add a new kind of procedure called a
traceproc
to the language. Atraceproc
works exactly like aproc
, except that it prints a trace message on entry and on exit.
Solution is implemented here.
Exercise 3.28 [★★]
Dynamic binding (or dynamic scoping) is an alternative design for procedures, in which the procedure body is evaluated in an environment obtained by extending the environment at the point of call. For example in
let a = 3 in let p = proc  a = 5 in 
the
a
in the procedure body would be bound to 5, not 3. Modify the language to use dynamic binding. Do this twice, once using a procedural representation for procedures, and once using a datastructure representation.
Solution is implemented here.
Only datastructure representation is implemented.
Exercise 3.29 [★★]
Unfortunately, programs that use dynamic binding may be exceptionally difficult to understand. For example, under lexical binding, consistently renaming the bound variables of a procedure can never change the behavior of a program: we can even remove all variables and replace them by their lexical addresses, as in section 3.6. But under dynamic binding, this transformation is unsafe.
For example, under dynamic binding, the procedure
proc (z) a
returns the value of the variablea
in its caller’s environment. Thus, the programlet a = 3 in let p = proc a in let f = proc in let a = 5 in
returns 5, since
a
’s value at the call site is 5. What iff
’s formal parameter werea
?
The result should be 2.
3.4 LETREC: A Language with Recursive Procedures
3.4.0
Exercise 3.30 [★]
What is the purpose of the call to
procval
on the nexttolast line ofapplyenv
?
When we are creating the desired recursive closure, we need an environment containing the closure, but we can not create
the environment directly because we need the closure in order to create the environment. So we delay the creation of
the closure in the environment so that we can create the environment without a closure. Then, when we need to use the
closure, we create it by calling procval
.
Exercise 3.31 [★]
Extend the language above to allow the declaration of a recursive procedure of possibly many arguments, as in exercise 3.21.
Solution is implemented here.
Exercise 3.32 [★★]
Extend the language above to allow the declaration of any number of mutually recursive unary procedures, for example:
letrec even= if zero? then 1 else odd = if zero? then 0 else in
Solution is implemented here.
Exercise 3.33 [★★]
Extend the language above to allow the declaration of any number of mutually recursive procedures, each of possibly many arguments, as in exercise 3.21.
Solution is implemented here.
Exercise 3.34 [★★★]
Implement
extendenvrec
in the procedural representation of environments from section 2.2.3.
Skipped.
Exercise 3.35 [★]
The representations we have seen so far are inefficient, because they build a new closure every time the procedure is retrieved. But the closure is the same every time. We can build the closures only once, by putting the value in a vector of length 1 and building an explicit circular structure, like
TODO: Add this figure later.
Here’s the code to build this data structure.
Complete the implementation of this representation by modifying the definitions of the environment data type and
applyenv
accordingly. Be sure thatapplyenv
always returns an expressed value.
Solution is implemented here.
Exercise 3.36 [★★]
Extend this implementation to handle the language from exercise 3.32.
Solution is implemented here.
Exercise 3.37 [★]
With dynamic binding (exercise 3.28), recursive procedures may be bound by
let
; no special mechanism is necessary for recursion. This is of historical interest; in the early years of programming language design other approaches to recursion, such as those discussed in section 3.4, were not widely understood. To demonstrate recursion via dynamic binding, test the programlet fact = proc add1 in let fact = proc if zero? then 1 else * in
using both lexical and dynamic binding. Write the mutually recursive procedures
even
andodd
as in section 3.4 in the defined language with dynamic binding.
Skipped.
3.7 Implementing Lexical Addressing
3.7.2 The Nameless Interpreter
Exercise 3.38 [★]
Extend the lexical address translator and interpreter to handle
cond
from exercise 3.12.
Solution is implemented here.
Exercise 3.39 [★]
Extend the lexical address translator and interpreter to handle
pack
andunpack
from exercise 3.18.
Solution is implemented here.
Exercise 3.40 [★★]
Extend the lexical address translator and interpreter to handle
letrec
. Do this by modifying the context argument totranslationof
so that it keeps track of not only the name of each bound variable, but also whether it was bound byletrec
or not. For a reference to a variable that was bound by aletrec
, generate a new kind of reference, called anamelessletrecvarexp
. You can then continue to use the nameless environment representation above, and the interpreter can do the right thing with anamelessletrecvarexp
.
Solution is implemented here.
Exercise 3.41 [★★]
Modify the lexical address translator and interpreter to handle
let
expressions, procedures, and procedure calls with multiple arguments, as in exercise 3.21. Do this using a nameless version of the ribcage representation of environments (exercise 2.11). For this representation, the lexical address will consist of two nonnegative integers: the lexical depth, to indicate the number of contours crossed, as before; and a position, to indicate the position of the variable in the declaration.
Solution is implemented here.
Exercise 3.42 [★★★]
Modify the lexical address translator and interpreter to use the trimmed representation of procedures from exercise 3.26. For this, you will need to translate the body of the procedure not
(extendsenv
var senv)
, but in a new static environment that tells exactly where each variable will be kept in the trimmed representation.
Solution is implemented here.
Exercise 3.43 [★★★]
The translator can do more than just keep track of the names of variables. For example, consider the program
let x = 3 in let f = proc  in
Here we can tell statically that at the procedure call,
f
will be bound to a procedure whose body is(y,x)
, wherex
has the same value that it had at the procedurecreation site. Therefore we could avoid looking upf
in the environment entirely. Extend the translator to keep track of “known procedures” and generate code that avoids an environment lookup at the call of such a procedure.
Solution is implemented here.
Exercise 3.44 [★★★]
In the preceding example, the only use of
f
is as a known procedure. Therefore the procedure built by the expression proc(y) (y,x)
is never used. Modify the translator so that such a procedure is never constructed.
Solution is implemented here.
4 State
4.2 EXPLICITREFS: A Language with Explicit References
4.2.0
Exercise 4.1 [★]
What would have happened had the program been instead
let g = proc let counter = newref in in let a = in let b = in 
The result would been 0. Because counter
rebinds to a new location that has the value 0 every time g is called, the
final value that referenced by counter
will be the same.
4.2.1 StorePassing Specifications
Exercise 4.2 [★]
Write down the specification for a
zero?exp
.
\[ \dfrac{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} = (val_1, σ_1)} {\texttt{(valueof (zero?exp $exp_1$) $ρ$ $σ_0$)} = \cases{(\texttt{(boolval #t)}, σ_1) &if $\texttt{(expval>num $val_1$)} = 0$ \\ (\texttt{(boolval #f)}, σ_1) &if $\texttt{(expval>num $val_1$)} ≠ 0$}} \]
Exercise 4.3 [★]
Write down the specification for a
callexp
.
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (val_1, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val_2, σ_2)}} {\texttt{(valueof (callexp $exp_1$ $exp_2$) $ρ$ $σ_0$)} = \texttt{(applyprocedure $val_1$ $val_2$ $σ_2$)}} \]
Exercise 4.4 [★★]
Write down the specification for a
begin
expression.Expression ::=
begin
Expression {;
Expression}^{∗}end
A
begin
expression may contain one or more subexpressions separated by semicolons. These are evaluated in order and the value of the last is returned.
\[ \dfrac{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} = (val_1, σ_1)} {\eqalign{\texttt{(valueof (beginexp $exp_1$ '()) $ρ$ $σ_0$)} &= (val_1, σ_1) \\ \texttt{(valueof (beginexp $exp_1$ (cons $exp_2$ $exps$)) $ρ$ $σ_0$)} &= \texttt{(valueof (beginexp $exp_2$ $exps$) $ρ$ $σ_1$)}}} \]
Exercise 4.5 [★★]
Write down the specification for
list
(exercise 3.10).
\[ \texttt{(valueof (listexp '()))} = \texttt{(emptylist)} \]
\[ \dfrac{\eqalign{ \texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (val_1, σ_1) \\ \texttt{(valueof (listexp $exps$) $ρ$ $σ_1$)} &= (val_2, σ_2)}} {\texttt{(valueof (listexp (cons $exp_1$ $exps$)))} = (\texttt{(pairval $val_1$ $val_2$)}, σ_2)} \]
4.2.2 Specifying Operations on Explicit References
Exercise 4.6 [★]
Modify the rule given above so that a
setrefexp
returns the value of the righthand side.
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (l, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val, σ_2)}} {\texttt{(valueof (setrefexp $exp_1$ $exp_2$ $ρ$ $σ_0$))} = (val, [l=val]σ_2)} \]
Exercise 4.7 [★]
Modify the rule given above so that a
setrefexp
returns the old contents of the location.
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (l, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val, σ_2)}} {\texttt{(valueof (setrefexp $exp_1$ $exp_2$ $ρ$ $σ_0$))} = (σ_0(l), [l=val]σ_2)} \]
4.2.3 Implementation
Exercise 4.8 [★]
Show exactly where in our implementation of the store these operations take linear time rather than constant time.
In newref
, length
and append
take linear time, so newref
takes linear time.
In deref
, listref
take linear time, so deref
takes linear time.
In setref!
, setrefinner
loops through the store, which takes linear time, so setref!
takes linear time.
Exercise 4.9 [★]
Implement the store in constant time by representing it as a Scheme vector. What is lost by using this representation?
Note that newref
still takes linear time. It is possible to implement the store that allocates locations in constant
time on average by preallocating more locations in advance, but it is a little complicated, so I’ll just choose the easy
way to implement the store.
As for the disadvantages of using a Scheme vector to implement the store, may be sharing values between stores becomes more difficult.
Exercise 4.10 [★]
Implement the
begin
expression as specified in exercise 4.4.
The reference implementation already implemented the begin
expression, so I’ll just skip this one.
Exercise 4.11 [★]
Implement
list
from exercise 4.5.
Solution is implemented here.
Exercise 4.12 [★★★]
Our understanding of the store, as expressed in this interpreter, depends on the meaning of effects in Scheme. In particular, it depends on us knowing when these effects take place in a Scheme program. We can avoid this dependency by writing an interpreter that more closely mimics the specification. In this interpreter,
valueof
would return both a value and a store, just as in the specification. A fragment of this interpreter appears in figure 4.6. We call this a storepassing interpreter. Extend this interpreter to cover all of the language EXPLICITREFS.Every procedure that might modify the store returns not just its usual value but also a new store. These are packaged in a data type called
answer
. Complete this definition ofvalueof
.
Solution is implemented here.
Also, what is applystore
in the reference implementation?
Exercise 4.13 [★★★]
Extend the interpreter of the preceding exercise to have procedures of multiple arguments.
Solution is implemented here.
4.3 IMPLICITREFS: A Language with Implicit References
4.3.1 Specification
Exercise 4.14 [★]
Write the rule for
let
.
\[ \dfrac{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} = (val_1, σ_1)} {\texttt{(valueof (letexp $var$ $exp_1$ $body$) $ρ$ $σ_0$)} = \texttt{(valueof $body$ $[var = l]ρ$ $[l = val_1]σ_1$)}} \]
4.3.2 The Implementation
Exercise 4.15 [★]
In figure 4.8, why are variables in the environment bound to plain integers rather than expressed values, as in figure 4.5?
Because we know for sure that the denoted values will all be references, so plain integers are sufficient to represent the location info we need.
Exercise 4.16 [★]
Now that variables are mutable, we can build recursive procedures by assignment. For example
letrec times4= if zero? then 0 else  in
can be replaced by
let times4 = 0 in
Trace this by hand and verify that this translation works.
First we allocate a new location for the number 0, then we bind times4
to the location. After we setting times4
to
the procedure, the location pointed by times4
contains the procedure closure. In the enclosed environment of the
procedure, times4
also points to the procedure so the procedure can call itself recursively.
Exercise 4.17 [★★]
Write the rules for and implement multiargument procedures and
let
expressions.
\[ \eqalign{ &\texttt{(applyprocedure (procedure (list $var_1$ $var_2$ $…$ $var_n$) $body$ $ρ$) (list $val_1$ $val_2$ $…$ $val_n$) $σ$)} \\ = &\texttt{(valueof $body$ $[var_n = l_n]…[var_2 = l_2][var_1 = l_1]ρ$ $[l_n = val_n]…[l_2 = val_2][l_1 = val_1]σ$)}} \]
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (val_1, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val_2, σ_2) \\ &… \\ \texttt{(valueof $exp_n$ $ρ$ $σ_{n  1}$)} &= (val_n, σ_n)}} {\eqalign{ &\texttt{(valueof (letexp (list $var_1$ $var_2$ $…$ $var_n$) (list $exp_1$ $exp_2$ $…$ $exp_n$) $body$) $ρ$ $σ_0$)} \\ = &\texttt{(valueof $body$ $[var_n = l_n]…[var_2 = l_2][var_1 = l_1]ρ$ $[l_n = val_n]…[l_2 = val_2][l_1 = val_1]σ_n$)}}} \]
Exercise 4.18 [★★]
Write the rule for and implement multiprocedure
letrec
expressions.
\[ \eqalign{ &\texttt{(valueof (letrecexp (list $var_1$ $var_2$ $…$ $var_n$) (list $bvars_1$ $bvars_2$ $…$ $bvars_n$) (list $pbody_1$ $pbody_2$ $…$ $pbody_n$) $letrecbody$) $ρ$ $σ$)} \\ = &\texttt{(let ([letrecenv $[var_n=l_n]…[var_2=l_2][var_1=l_1]ρ$])} \\ &\quad \texttt{(valueof $letrecbody$ letrecenv $[l_n = \texttt{(procedure $bvars_n$ $pbody_n$ letrecenv)}] … [l_2 = \texttt{(procedure $bvars_2$ $pbody_2$ letrecenv)}] [l_1 = \texttt{(procedure $bvars_1$ $pbody_1$ letrecenv)}]σ$))}} \]
Exercise 4.19 [★★]
Modify the implementation of multiprocedure
letrec
so that each closure is built only once, and only one location is allocated for it. This is like exercise 3.35.
Solution is implemented here.
Exercise 4.20 [★★]
In the language of this section, all variables are mutable, as they are in Scheme. Another alternative is to allow both mutable and immutable variable bindings:
 ExpVal = Int + Bool + Proc
 DenVal = Ref(ExpVal) + ExpVal
Variable assignment should work only when the variable to be assigned to has a mutable binding. Dereferencing occurs implicitly when the denoted value is a reference.
Modify the language of this section so that
let
introduces immutable variables, as before, but mutable variables are introduced by aletmutable
expression, with syntax given byExpression ::=
letmutable
Identifier=
Expressionin
Expression
Solution is implemented here.
Exercise 4.21 [★★]
We suggested earlier the use of assignment to make a program more modular by allowing one procedure to communicate information to a distant procedure without requiring intermediate procedures to be aware of it. Very often such an assignment should only be temporary, lasting for the execution of a procedure call. Add to the language a facility for dynamic assignment (also called fluid binding) to accomplish this. Use the production
Expression ::=
setdynamic
Identifier=
Expressionduring
Expression
setdynamicexp (
var exp_{1} body)
The effect of the
setdynamic
expression is to assign temporarily the value of exp_{1} to var, evaluate body, reassign var to its original value, and return the value of body. The variable var must already be bound. For example, inlet x = 11 in let p = proc  in 
the value of
x
, which is free in procedurep
, is 17 in the call(p 22)
, but is reset to 11 in the call(p 13)
, so the value of the expression is 5 − 2 = 3.
Solution is implemented here.
Exercise 4.22 [★★]
So far our languages have been expressionoriented: the primary syntactic category of interest has been expressions and we have primarily been interested in their values. Extend the language to model the simple statementoriented language whose specification is sketched below. Be sure to Follow the Grammar by writing separate procedures to handle programs, statements, and expressions.
Values As in IMPLICITREFS.
Syntax Use the following syntax:
Program ::= Statement
Statement ::= Identifier
=
Expression
::=
::={
{Statement}^{∗(;)}}
::=if
Expression Statement Statement
::=while
Expression Statement
::=var
{Identifier}^{∗(,)};
StatementThe nonterminal Expression refers to the language of expressions of IMPLICITREFS, perhaps with some extensions.
Semantics A program is a statement. A statement does not return a value, but acts by modifying the store and by printing.
Assignment statements work in the usual way. A print statement evaluates its actual parameter and prints the result. The
if
statement works in the usual way. A block statement, defined in the last production for Statement, binds each of the declared variables to an uninitialized reference and then executes the body of the block. The scope of these bindings is the body.Write the specification for statements using assertions like
\[ \texttt{(resultof $stmt$ $ρ$ $σ_0$)} = σ_1 \]
Examples Here are some examples.
7 12 3 4 3 12
Example 3 illustrates the scoping of the block statement.
Example 4 illustrates the interaction between statements and expressions. A procedure value is created and stored in the variable
f
. In the last line, this procedure is applied to the actual parameters 4 andx
; sincex
is bound to a reference, it is dereferenced to obtain 3.
Solution is implemented here.
Specification for statements:
\[ \dfrac{\texttt{(valueof $exp$ $ρ$ $σ_0$)} = (val, σ_1)} {\texttt{(resultof (assignstatement $var$ $exp$) $ρ$ $σ_0$)} = [ρ(var) = val]σ_1} \]
\[ \dfrac{\texttt{(valueof $exp$ $ρ$ $σ_0$)} = (val, σ_1)} {\texttt{(resultof (printstatement $exp$) $ρ$ $σ_0$)} = σ_1} \]
\[ \dfrac{\eqalign{ \texttt{(resultof $stmt_1$ $ρ$ $σ_0$)} &= σ_1 \\ \texttt{(resultof $stmt_2$ $ρ$ $σ_1$)} &= σ_2 \\ &… \\ \texttt{(resultof $stmt_n$ $ρ$ $σ_{n  1}$)} &= σ_n}} {\texttt{(resultof (bracestatement (list $stmt_1$ $stmt_1$ $…$ $stmt_n$)) $ρ$ $σ_0$)} = σ_n} \]
\[ \dfrac{\texttt{(valueof $exp$ $ρ$ $σ_0$)} = (val, σ_1)} {\texttt{(resultof (ifstatement $exp$ $stmt_1$ $stmt_2$) $ρ$ $σ_0$)} = \cases{\texttt{(resultof $stmt_1$ $ρ$ $σ_1$)} &if $\texttt{(expval>bool $val$)} = \texttt{#t}$ \\ \texttt{(resultof $stmt_2$ $ρ$ $σ_1$)} &if $\texttt{(expval>bool $val$)} = \texttt{#f}$}} \]
\[ \dfrac{\eqalign{ \texttt{(valueof $exp$ $ρ$ $σ_0$)} &= (val, σ_1) \\ \texttt{(resultof $stmt$ $ρ$ $σ_1$)} &= σ_2}} {\eqalign{ &\texttt{(resultof (whilestatement $exp$ $stmt$) $ρ$ $σ_2$)} \\ = &\cases{\texttt{(resultof (whilestatement $exp$ $stmt$) $ρ$ $σ_2$)} &if $\texttt{(expval>bool $val$)} = \texttt{#t}$ \\ σ_1 &if $\texttt{(expval>bool $val$)} = \texttt{#f}$}}} \]
\[ \eqalign{ &\texttt{(resultof (blockstatement (list $var_1$ $var_2$ $…$ $var_n$) $stmt$) $ρ$ $σ_0$)} \\ = &\texttt{(resultof $stmt$ $[var_n = l_n]…[var_2 = l_2][var_1 = l_1]ρ$ $[l_n = undefined]…[l_2 = undefined][l_1 = undefined]σ_0$)}} \]
Exercise 4.23 [★]
Add to the language of exercise 4.22
read
statements of the formread
var. This statement reads a nonnegative integer from the input and stores it in the given variable.
Solution is implemented here.
Exercise 4.24 [★]
A
dowhile
statement is like awhile
statement, except that the test is performed after the execution of the body. Adddowhile
statements to the language of exercise 4.22.
Solution is implemented here.
Exercise 4.25 [★]
Extend the block statement of the language of exercise 4.22 to allow variables to be initialized. In your solution, does the scope of a variable include the initializer for variables declared later in the same block statement?
Solution is implemented here.
No, the scope of a variable does not include the initializer for variables declared later in the same block statement.
Exercise 4.26 [★★★]
Extend the solution to the preceding exercise so that procedures declared in a single block are mutually recursive. Consider restricting the language so that the variable declarations in a block are followed by the procedure declarations.
Solution is implemented here.
Exercise 4.27 [★★]
Extend the language of the preceding exercise to include subroutines. In our usage a subroutine is like a procedure, except that it does not return a value and its body is a statement, rather than an expression. Also, add subroutine calls as a new kind of statement and extend the syntax of blocks so that they may be used to declare both procedures and subroutines. How does this affect the denoted and expressed values? What happens if a procedure is referenced in a subroutine call, or vice versa?
Solution is implemented here.
Denoted values does not change, expressed values now contains a subval
variant.
Error will happen if procedure is referenced in a subroutine call, or vice versa.
4.4 MUTABLEPAIRS: A Language with Mutable Pairs
4.4.2 Another Representation ofMutable Pairs
Exercise 4.28 [★★]
Write down the specification rules for the five mutablepair operations.
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (val_1, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val_2, σ_2)}} {\texttt{(valueof (newpairexp $exp_1$ $exp_2$) $ρ$ $σ_0$)} = (\texttt{(mutpairval (apair $l_1$ $l_2$))}, [l_2 = val_2][l_1 = val_1]σ_2)} \]
\[ \dfrac{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} = (\texttt{(mutpairval (apair $l_1$ $l_2$))}, σ_1)} {\texttt{(valueof (leftexp $exp_1$) $ρ$ $σ_0$)} = (σ_1(l_1), σ_1)} \]
\[ \dfrac{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} = (\texttt{(mutpairval (apair $l_1$ $l_2$))}, σ_1)} {\texttt{(valueof (rightexp $exp_1$) $ρ$ $σ_0$)} = (σ_1(l_2), σ_1)} \]
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (\texttt{(mutpairval (apair $l_1$ $l_2$))}, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val_2, σ_2)}} {\texttt{(valueof (setleftexp $exp_1$ $exp_2$) $ρ$ $σ_0$)} = (\texttt{(numval 82)}, [l_1 = val_2]σ_2)} \]
\[ \dfrac{\eqalign{\texttt{(valueof $exp_1$ $ρ$ $σ_0$)} &= (\texttt{(mutpairval (apair $l_1$ $l_2$))}, σ_1) \\ \texttt{(valueof $exp_2$ $ρ$ $σ_1$)} &= (val_2, σ_2)}} {\texttt{(valueof (setrightexp $exp_1$ $exp_2$) $ρ$ $σ_0$)} = (\texttt{(numval 83)}, [l_2 = val_2]σ_2)} \]
Exercise 4.29 [★★]
Add arrays to this language. Introduce new operators
newarray
,arrayref
, andarrayset
that create, dereference, and update arrays. This leads to
 ArrVal = (Ref(ExpVal))^{∗}
 ExpVal = Int + Bool + Proc + ArrVal
 DenVal = Ref(ExpVal)
Since the locations in an array are consecutive, use a representation like the second representation above. What should be the result of the following program?
let a = newarray p = proc let v = arrayref in arrayset in
Here
newarray(2,99)
is intended to build an array of size 2, with each location in the array containing 99.begin
expressions are defined in exercise 4.4. Make the array indices zerobased, so an array of size 2 has indices 0 and 1.
Solution is implemented here.
The result of that program should be 2.
Exercise 4.30 [★★]
Add to the language of exercise 4.29 a procedure
arraylength
, which returns the size of an array. Your procedure should work in constant time. Make sure thatarrayref
andarrayset
check to make sure that their indices are within the length of the array.
Solution is implemented here.
4.5 ParameterPassing Variations
4.5.1 CALLBYREFERENCE
Exercise 4.31 [★]
Write out the specification rules for CALLBYREFERENCE.
Skipped.
Exercise 4.32 [★]
Extend the language CALLBYREFERENCE to have procedures of multiple arguments.
Solution is implemented here.
Exercise 4.33 [★★]
Extend the language CALLBYREFERENCE to support callbyvalue procedures as well.
Solution is implemented here.
Exercise 4.34 [★]
Add a callbyreference version of
let
, calledletref
, to the language. Write the specification and implement it.
Solution is implemented here.
Exercise 4.35 [★★]
We can get some of the benefits of callbyreference without leaving the callbyvalue framework. Extend the language IMPLICITREFS by adding a new expression
Expression ::=
ref
Identifier
refexp (var)
This differs from the language EXPLICITREFS, since references are only of variables. This allows us to write familiar programs such as
swap
within our callbyvalue language. What should be the value of this expression?let a = 3 in let b = 4 in let swap = proc proc let temp = deref in in
Here we have used a version of
let
with multiple declarations (exercise 3.16). What are the expressed and denoted values of this language?
Solution is implemented here.
Here we have used a version of
let
with multiple declarations (exercise 3.16).
No, you have not.
The value of the given expression should be 1.
Expressed values now contains reference values, and denoted values still only contains references.
Exercise 4.36 [★]
Most languages support arrays, in which case array references are generally treated like variable references under callbyreference. If an operand is an array reference, then the location referred to, rather than its contents, is passed to the called procedure. This allows, for example, a swap procedure to be used in commonly occurring situations in which the values in two array elements are to be exchanged. Add array operators like those of exercise 4.29 to the callbyreference language of this section, and extend
valueofoperand
to handle this case, so that, for example, a procedure application like
will work as expected. What should happen in the case of
?
Solution is implemented here.
((swap arrayref(a, arrayref(a, i))) arrayref(a, j))
will swap the element indexed by the value of arrayref(a, i)
with the element indexed by j
.
Exercise 4.37 [★★]
Callbyvalueresult is a variation on callbyreference. In callbyvalueresult, the actual parameter must be a variable. When a parameter is passed, the formal parameter is bound to a new reference initialized to the value of the actual parameter, just as in callbyvalue. The procedure body is then executed normally. When the procedure body returns, however, the value in the new reference is copied back into the reference denoted by the actual parameter. This may be more efficient than callbyreference because it can improve memory locality. Implement callbyvalueresult and write a program that produces different answers using callbyvalueresult and callbyreference.
Solution is implemented here.
This program produces 4 using callbyvalueresult while it produces 3 using callbyreference.
let f = proc
in let x = 5
in
4.5.2 Lazy Evaluation: CALLBYNAME and CALLBYNEED
Exercise 4.38 [★]
The example below shows a variation of exercise 3.25 that works under callbyneed. Does the original program in exercise 3.25 work under callbyneed? What happens if the program below is run under callbyvalue? Why?
let makerec = proc let d = proc in in let maketimes4 = proc proc if zero? then 0 else  in let times4 = in
Yes, the original program in exercise 3.25 works under callbyneed.
And the program above will loop infinitely under callbyvalue, because in line 3, (d d)
calls d
with itself, and
when d
is called, it calls its argument x
with x
, where x
is d
itself. So (d d)
leads to another call to
(d d)
which leads to infinite loop.
Exercise 4.39 [★]
In the absence of effects, callbyname and callbyneed always give the same answer. Construct an example in which callbyname and callbyneed give different answers.
let x = 0
in let f = proc
in
The program above should produce 1 in callbyneed and 2 in callbyname.
Exercise 4.40 [★]
Modify
valueofoperand
so that it avoids making thunks for constants and procedures.
Solution is implemented here.
Exercise 4.41 [★★]
Write out the specification rules for callbyname and callbyneed.
Skipped.
Exercise 4.42 [★★]
Add a lazy
let
to the callbyneed interpreter.
Solution is implemented here.
5 ContinuationPassing Interpreters
5.1 A ContinuationPassing Interpreter
5.1.0
Exercise 5.1 [★]
Implement this data type of continuations using the procedural representation.
Solution is implemented here.
Exercise 5.2 [★]
Implement this data type of continuations using a datastructure representation.
Solution is implemented here.
Exercise 5.3 [★]
Add
let2
to this interpreter. Alet2
expression is like alet
expression, except that it defines exactly two variables.
Solution is implemented here.
Exercise 5.4 [★]
Add
let3
to this interpreter. Alet3
expression is like alet
expression, except that it defines exactly three variables.
Solution is implemented here.
Exercise 5.5 [★]
Add lists to the language, as in exercise 3.9.
Solution is implemented here.
Exercise 5.6 [★★]
Add a
list
expression to the language, as in exercise 3.10. As a hint, consider adding two new continuationbuilders, one for evaluating the first element of the list and one for evaluating the rest of the list.
Solution is implemented here.
Exercise 5.7 [★★]
Add multideclaration
let
(exercise 3.16) to this interpreter.
Solution is implemented here.
Exercise 5.8 [★★]
Add multiargument procedures (exercise 3.21) to this interpreter.
Solution is implemented here.
Exercise 5.9 [★★]
Modify this interpreter to implement the IMPLICITREFS language. As a hint, consider including a new continuationbuilder
(setrhscont env var cont)
.
Solution is implemented here.
Exercise 5.10 [★★]
Modify the solution to the previous exercise so that the environment is not kept in the continuation.
Not all environments can be removed from continuations. For example, I cannot think of a way to remove the environment in the continuation of the bound expression.
Solution is implemented here.
Exercise 5.11 [★★]
Add the
begin
expression of exercise 4.4 to the continuationpassing interpreter. Be sure that no call tovalueof
orvalueofrands
occurs in a position that would build control context.
Solution is implemented here.
Exercise 5.12 [★]
Instrument the interpreter of figures 5.4–5.6 to produce output similar to that of the calculation on page 150.
Skipped.
Exercise 5.13 [★]
Translate the definitions of
fact
andfactiter
into the LETREC language. You may add a multiplication operator to the language. Then, using the instrumented interpreter of the previous exercise, compute(fact 4)
and(factiter 4)
. Compare them to the calculations at the beginning of this chapter. Find(* 4 (* 3 (* 2 (fact 1))))
in the trace of(fact 4)
. What is the continuation ofapplyprocedure/k
for this call of(fact 1)
?
The implementation of fact
is:
letrec fact= if zero?
then 1
else *
in fact
The implementation of factiter
is:
letrec factiteracc = proc
if zero?
then a
else
in proc
The continuation of applyprocedure/k
for call of (fact 1)
is:
Exercise 5.14 [★]
The instrumentation of the preceding exercise produces voluminous output. Modify the instrumentation to track instead only the size of the largest continuation used during the calculation. We measure the size of a continuation by the number of continuationbuilders employed in its construction, so the size of the largest continuation in the calculation on page 150 is 3. Then calculate the values of
fact
andfactiter
applied to several operands. Confirm that the size of the largest continuation used byfact
grows linearly with its argument, but the size of the largest continuation used byfactiter
is a constant.
Skipped.
Exercise 5.15 [★]
Our continuation data type contains just the single constant,
endcont
, and all the other continuationbuilders have a single continuation argument. Implement continuations by representing them as lists, where(endcont)
is represented by the empty list, and each other continuation is represented by a nonempty list whose car contains a distinctive data structure (called frame or activation record) and whose cdr contains the embedded continuation. Observe that the interpreter treats these lists like a stack (of frames).
Solution is implemented here.
Exercise 5.16 [★★]
Extend the continuationpassing interpreter to the language of exercise 4.22. Pass a continuation argument to
resultof
, and make sure that no call toresultof
occurs in a position that grows a control context. Since a statement does not return a value, distinguish between ordinary continuations and continuations for statements; the latter are usually called command continuations. The interface should include a procedureapplycommandcont
that takes a command continuation and invokes it. Implement command continuations both as data structures and as zeroargument procedures.
Solution is implemented here.
5.2 A Trampolined Interpreter
5.2.0
Exercise 5.17 [★]
Modify the trampolined interpreter to wrap
(lambda () ...)
around each call (there’s only one) toapplyprocedure/k
. Does this modification require changing the contracts?
Solution is implemented here.
No, this modification does not require changing the contracts.
Exercise 5.18 [★]
The trampoline systemin figure 5.7 uses a procedural representation of a Bounce. Replace this by a data structure representation.
Solution is implemented here.
Exercise 5.19 [★]
Instead of placing the
(lambda () ...)
around the body ofapplyprocedure/k
, place it around the body ofapplycont
. Modify the contracts to match this change. Does the definition of Bounce need to change? Then replace the procedural representation of Bounce with a datastructure representation, as in exercise 5.18.
Solution is implemented here.
The definition of Bounce need not to change.
Exercise 5.20 [★]
In exercise 5.19, the last bounce before
trampoline
returns a FinalAnswer is always something like(applycont (endcont)
val)
, whereval
is someExpVal
. Optimize your representation of bounces in exercise 5.19 to take advantage of this fact.
Skipped.
Exercise 5.21 [★★]
Implement a trampolining interpreter in an ordinary procedural language. Use a data structure representation of the snapshots as in exercise 5.18, and replace the recursive call to
trampoline
in its own body by an ordinarywhile
or other looping construct.
Skipped.
Exercise 5.22 [★★★]
One could also attempt to transcribe the environmentpassing interpreters of chapter 3 in an ordinary procedural language. Such a transcription would fail in all but the simplest cases, for the same reasons as suggested above. Can the technique of trampolining be used in this situation as well?
I don’t think trampolining can be used in that situation. In the case of continuationpassing interpreters, every call
to valueof
is a tail call, so we can simulate the process using a loop. But in the case of environmentpassing
interpreters, not every call to valueof
is a tail call, so we cannot convert the process using a loop. The
trampolining method will fail in such case.
5.3 An Imperative Interpreter
5.3.0
Exercise 5.23 [★]
What happens if you remove the “goto” line in one of the branches of this interpreter? Exactly how does the interpreter fail?
The interpreter will stop running at the position where the “goto” line should be.
Exercise 5.24 [★]
Devise examples to illustrate each of the complications mentioned above.
Skipped.
Exercise 5.25 [★★]
Registerize the interpreter for multiargument procedures (exercise 3.21).
Solution is implemented here.
Exercise 5.26 [★]
Convert this interpreter to a trampoline by replacing each call to
applyprocedure/k
with(set! pc applyprocedure/k)
and using a driver that looks like
Solution is implemented here.
Exercise 5.27 [★]
Invent a language feature for which setting the
cont
variable last requires a temporary variable.
Skipped.
Exercise 5.28 [★]
Instrument this interpreter as in exercise 5.12. Since continuations are represented the same way, reuse that code. Verify that the imperative interpreter of this section generates exactly the same traces as the interpreter in exercise 5.12.
Skipped.
Exercise 5.29 [★]
Apply the transformation of this section to
factiter
(page 139).
Exercise 5.30 [★★]
Modify the interpreter of this section so that procedures rely on dynamic binding, as in exercise 3.28. As a hint, consider transforming the interpreter of exercise 3.28 as we did in this chapter; it will differ from the interpreter of this section only for those portions of the original interpreter that are different. Instrument the interpreter as in exercise 5.28. Observe that just as there is only one continuation in the state, there is only one environment that is pushed and popped, and furthermore, it is pushed and popped in parallel with the continuation. We can conclude that dynamic bindings have dynamic extent: that is, a binding to a formal parameter lasts exactly until that procedure returns. This is different from lexical bindings, which can persist indefinitely if they wind up in a closure.
Skipped.
Exercise 5.31 [★]
Eliminate the remaining
let
expressions in this code by using additional global registers.
Skipped.
Exercise 5.32 [★★]
Improve your solution to the preceding exercise by minimizing the number of global registers used. You can get away with fewer than 5. You may use no data structures other than those already used by the interpreter.
Skipped.
Exercise 5.33 [★★]
Translate the interpreter of this section into an imperative language. Do this twice: once using zeroargument procedure calls in the host language, and once replacing each zeroargument procedure call by a
goto
. How do these alternatives perform as the computation gets longer?
Skipped.
Exercise 5.34 [★★]
As noted on page 157, most imperative languages make it difficult to do this translation, because they use the stack for all procedure calls, even tail calls. Furthermore, for large interpreters, the pieces of code linked by
goto
’s may be too large for some compilers to handle. Translate the interpreter of this section into an imperative language, circumventing this difficulty by using the technique of trampolining, as in exercise 5.26.
Skipped.
5.4 Exceptions
5.4.0
Exercise 5.35 [★★]
This implementation is inefficient, because when an exception is raised,
applyhandler
must search linearly through the continuation to find a handler. Avoid this search by making thetrycont
continuation available directly in each continuation.
Solution is implemented here.
Exercise 5.36 [★]
An alternative design that also avoids the linear search in
applyhandler
is to use two continuations, a normal continuation and an exception continuation. Achieve this goal by modifying the interpreter of this section to take two continuations instead of one.
Solution is implemented here.
Exercise 5.37 [★]
Modify the defined language to raise an exception when a procedure is called with the wrong number of arguments.
Solution is implemented here.
Exercise 5.38 [★]
Modify the defined language to add a division expression. Raise an exception on division by zero.
Solution is implemented here.
Exercise 5.39 [★★]
So far, an exception handler can propagate the exception by reraising it, or it can return a value that becomes the value of the
try
expression. One might instead design the language to allow the computation to resume from the point at which the exception was raised. Modify the interpreter of this section to accomplish this by running the body of the handler with the continuation from the point at which theraise
was invoked.
Solution is implemented here.
Exercise 5.40 [★★★]
Give the exception handlers in the defined language the ability to either return or resume. Do this by passing the continuation from the
raise
exception as a second argument. This may require adding continuations as a new kind of expressed value. Devise suitable syntax for invoking a continuation on a value.
Solution is implemented here.
Exercise 5.41 [★★★]
We have shown how to implement exceptions using a datastructure representation of continuations. We can’t immediately apply the recipe of section 2.2.3 to get a procedural representation, because we now have two observers:
applyhandler
andapplycont
. Implement the continuations of this section as a pair of procedures: a oneargument procedure representing the action of the continuation underapplycont
, and a zeroargument procedure representing its action underapplyhandler
.
Solution is implemented here.
I had to use a oneargument procedure to represent the action under applyhandler
instead of a zeroargument
procedure.
Exercise 5.42 [★★]
The preceding exercise captures the continuation only when an exception is raised. Add to the language the ability to capture a continuation anywhere by adding the form
letcc
Identifierin
Expression with the specification
(valueof/k (letcc
var body)
ρ cont)
= (valueof/k
body(extendenv
var cont ρ)
cont)
Such a captured continuation may be invoked with
throw
: the expressionthrow
Expressionto
Expression evaluates the two subexpressions. The second expression should return a continuation, which is applied to the value of the first expression. The current continuation of the throw expression is ignored.
Solution is implemented here.
Exercise 5.43 [★★]
Modify
letcc
as defined in the preceding exercise so that the captured continuation becomes a new kind of procedure, so instead of writingthrow
exp_{1}to
exp_{2}, one would write(
exp_{2} exp_{1})
.
Solution is implemented here.
Exercise 5.44 [★★]
An alternative to
letcc
andthrow
of the preceding exercises is to add a single procedure to the language. This procedure, which in Scheme is calledcallwithcurrentcontinuation
, takes a oneargument procedure,p
, and passes top
a procedure that when invoked with one argument, passes that argument to the current continuation,cont
. We could definecallwithcurrentcontinuation
in terms ofletcc
andthrow
as follows:let callwithcurrentcontinuation = proc letcc cont in in ...
Add
callwithcurrentcontinuation
to the language. Then write a translator that takes the language withletcc
andthrow
and translates it into the language withoutletcc
andthrow
, but withcallwithcurrentcontinuation
.
Solution is implemented here.
To translate a language with letcc
and throw
into the language without letcc
and throw
, just do the following:
 Translate
letcc
varin
body intocallcc(proc (
var)
body)
;  Translate
throw
exp_{1}to
exp_{2} into(
exp_{2} exp_{1})
.
5.5 Threads
5.5.0
Exercise 5.45 [★]
Add to the language of this section a construct called
yield
. Whenever a thread executes ayield
, it is placed on the ready queue, and the thread at the head of the ready queue is run. When the thread is resumed, it should appear as if the call to yield had returned the number 99.
Solution is implemented here. It is copied from the reference implementation.
Exercise 5.46 [★★]
In the system of exercise 5.45, a thread may be placed on the ready queue either because its time slot has been exhausted or because it chose to yield. In the latter case, it will be restarted with a full time slice. Modify the system so that the ready queue keeps track of the remaining time slice (if any) of each thread, and restarts the thread only with the time it has remaining.
Solution is implemented here.
Exercise 5.47 [★]
What happens if we are left with two subthreads, each waiting for a mutex held by the other subthread?
The two subthreads will deadlock and never be executed.
Exercise 5.48 [★]
We have used a procedural representation of threads. Replace this by a datastructure representation.
Solution is implemented here.
Exercise 5.49 [★]
Do exercise 5.15 (continuations as a stack of frames) for THREADS.
Solution is implemented here.
Exercise 5.50 [★★]
Registerize the interpreter of this section. What is the set of mutually tailrecursive procedures that must be registerized?
Solution is implemented here.
Procedures that must be registerized are applycont
, applyprocedure
, applyunop
, signalmutex
, valueof/k
and
waitformutex
.
Exercise 5.51 [★★★]
We would like to be able to organize our program so that the consumer in figure 5.17 doesn’t have to busywait. Instead, it should be able to put itself to sleep and be awakened when the producer has put a value in the buffer. Either write a program with mutexes to do this, or implement a synchronization operator that makes this possible.
let buffer = 0
in let mut = mutex(
in let producer = proc
letrec wait1 = if zero?
then
else
in
in let consumer = proc
in
Exercise 5.52 [★★★]
Write a program using mutexes that will be like the program in figure 5.21, except that the main thread waits for all three of the subthreads to terminate, and then returns the value of
x
.
let x = 0
in let mut = mutex(
in let incr_x = proc
let mut1 = mutex(
in
in let mut1 =
in let mut2 =
in let mut3 =
in
Exercise 5.53 [★★★]
Modify the thread package to include thread identifiers. Each new thread is associated with a fresh thread identifier. When the child thread is spawned, it is passed its thread identifier as a value, rather than the arbitrary value 28 used in this section. The child’s number is also returned to the parent as the value of the
spawn
expression. Instrument the interpreter to trace the creation of thread identifiers. Check to see that the ready queue contains at most one thread for each thread identifier. How can a child thread know its parent’s identifier? What should be done about the thread identifier of the original program?
Solution is implemented here.
We can pass both the parent’s thread number and the child’s thread number to the child.
Exercise 5.54 [★★]
Add to the interpreter of exercise 5.53 a
kill
facility. Thekill
construct, when given a thread number, finds the corresponding thread on the ready queue or any of the waiting queues and removes it. In addition,kill
should return a true value if the target thread is found and false if the thread number is not found on any queue.
Solution is implemented here.
Exercise 5.55 [★★]
Add to the interpreter of exercise 5.53 an interthread communication facility, in which each thread can send a value to another thread using its thread identifier. A thread can receive messages when it chooses, blocking if no message has been sent to it.
Solution is implemented here.
Exercise 5.56 [★★]
Modify the interpreter of exercise 5.55 so that rather than sharing a store, each thread has its own store. In such a language, mutexes can almost always be avoided. Rewrite the example of this section in this language, without using mutexes.
Skipped.
Exercise 5.57 [★★★]
There are lots of different synchronization mechanisms in your favorite OS book. Pick three and implement them in this framework.
Skipped.
Exercise 5.58 [definitely
★] Go off with your friends and have some pizza, but make sure only one person at a time grabs a piece!
Skipped.
6 ContinuationPassing Style
6.1 Writing Programs in ContinuationPassing Style
6.1.0
Exercise 6.1 [★]
Consider figure 6.2 without
(set! pc fact/k)
in the definition offact/k
and without(set! pc applycont)
in the definition ofapplycont
. Why does the program still work?
Because when fact/k
is called, the value of pc
must be fact/k
, so we don’t need to set pc
to fact/k
in order
to continue computation. Same for applycont
.
Exercise 6.2 [★]
Prove by induction on n that for any g,
(fib/k
n g)
=(
g(fib
n))
.
Base case: if n < 2, (fib/k
n g)
= (
g 1)
= (
g (fib
n))
.
Inductive case: if n ≥ 2,
(fib/k
n g)
= (fib/k (
n 1) (lambda (val1) (fib/k (
n 2) (lambda (val2) (
g (+ val1 val2))))))
= ((lambda (val1) (fib/k (
n 2) (lambda (val2) (
g (+ val1 val2))))) (fib (
n 1)))
(by induction)
= (fib/k (
n 2) (lambda (val2) (
g (+ (fib (
n 1)) val2))))
= ((lambda (val2) (
g (+ (fib (
n 1)) val2))) (fib (
n 2)))
(by induction)
= (
g (+ (fib (
n 1)) (fib (
n 2))))
= (
g (fib
n))
Exercise 6.3 [★]
Rewrite each of the following Scheme expressions in continuationpassing style. Assume that any unknown functions have also been rewritten in CPS.
(lambda (x y) (p (+ 8 x) (q y)))
(lambda (x y u v) (+ 1 (f (g x y) (+ u v))))
(+ 1 (f (g x y) (+ u (h v))))
(zero? (if a (p x) (p y)))
(zero? (if (f a) (p x) (p y)))
(let ((x (let ((y 8)) (p y)))) x)
(let ((x (if a (p x) (p y)))) x)
Exercise 6.4 [★★]
Rewrite each of the following procedures in continuationpassing style. For each procedure, do this first using a datastructure representation of continuations, then with a procedural representation, and then with the inlined procedural representation. Last, write the registerized version. For each of these four versions, test to see that your implementation is tailrecursive by defining
endcont
by=
as we did in chapter 5.
removefirst
(section 1.2.3).listsum
(section 1.3).occursfree?
(section 1.2.4).subst
(section 1.2.5).
Solution is implemented here.
Exercise 6.5 [★]
When we rewrite an expression in CPS, we choose an evaluation order for the procedure calls in the expression. Rewrite each of the preceding examples in CPS so that all the procedure calls are evaluated from right to left.
Skipped.
Exercise 6.6 [★]
How many different evaluation orders are possible for the procedure calls in
(lambda (x y) (+ (f (g x)) (h (j y))))
? For each evaluation order, write a CPS expression that calls the procedures in that order.
There are six different evaluation orders.
Exercise 6.7 [★★]
Write out the procedural and the inlined representations for the interpreter in figures 5.4, 5.5, and 5.6.
Solution is implemented here and here.
Exercise 6.8 [★★★]
Rewrite the interpreter of section 5.4 using a procedural and inlined representation. This is challenging because we effectively have two observers,
applycont
andapplyhandler
. As a hint, consider modifying the recipe on page 6.1 so that we add to each procedure two extra arguments, one representing the behavior of the continuation underapplycont
and one representing its behavior underapplyhandler
.
Solution is implemented here and here.
Exercise 6.9 [★]
What property of multiplication makes this program optimization possible?
The associative property.
Exercise 6.10 [★]
For
listsum
, formulate a succinct representation of the continuations, like the one forfact/k
above.
6.2 Tail Form
6.2.0
Exercise 6.11 [★]
Complete the interpreter of figure 6.6 by writing
valueofsimpleexp
.
Solution is implemented here. This is copied from the reference implementation.
Exercise 6.12 [★]
Determine whether each of the following expressions is simple.
((f (x,1)),1)
(f ((x,y),1))
if zero?(x) then (x,y) else ((x,y),1)
let x = proc (y) (y x) in (x,3)
let f = proc (x) x in (f 3)
((f (x,1)),1)
is not simple because(f (x,1))
is a procedure call.(f ((x,y),1))
is not simple because(f ((x,y),1))
is a procedure call.if zero?(x) then (x,y) else ((x,y),1)
is simple.let x = proc (y) (y x) in (x,3)
is simple. although(y x)
is procedural call, but it is in a procedure body so that’s OK.let f = proc (x) x in (f 3)
is not simple because(f 3)
is a procedure call.
Exercise 6.13 [★]
Translate each of these expressions in CPSIN into continuationpassing style using the CPS recipe on page 200 above. Test your transformed programs by running them using the interpreter of figure 6.6. Be sure that the original and transformed versions give the same answer on each input.
removeall
.letrec removeall= if null? then emptylist else if number? then if equal? then else cons else cons
occursin?
.letrec occursin? = if null? then 0 else if number? then if equal? then 1 else else if then 1 else
remfirst
. This usesoccursin?
from the preceding example.letrec remfirst= letrec loop = if null? then emptylist else if number? then if equal? then cdr else cons else if then cons else cons in
depth
.letrec depth= if null? then 1 else if number? then else if less? then else add1
depthwithlet
.letrec depth= if null? then 1 else if number? then else let dfirst = add1 drest = in if less? then drest else dfirst
map
.letrec map= if null? then emptylist else cons square = * in
fnlrgtn
. This procedure takes an nlist, like an slist (page 9), but with numbers instead of symbols, and a numbern
and returns the first number in the list (in lefttoright order) that is greater thann
. Once the result is found, no further elements in the list are examined. For example,finds 7.
every
. This procedure takes a predicate and a list and returns a true value if and only if the predicate holds for each list element.letrec every= if null? then 1 else if then else 0 in
Skipped.
Exercise 6.14 [★]
Complete the interpreter of figure 6.6 by supplying definitions for
valueofprogram
andapplycont
.
Solution is implemented here. This is copied from the reference implementation.
Exercise 6.15 [★]
Observe that in the interpreter of the preceding exercise, there is only one possible value for
cont
. Use this observation to remove thecont
argument entirely.
Solution is implemented here.
Exercise 6.16 [★]
Registerize the interpreter of figure 6.6.
Solution is implemented here.
Exercise 6.17 [★]
Trampoline the interpreter of figure 6.6.
Solution is implemented here.
Exercise 6.18 [★★]
Modify the grammar of CPSOUT so that a simple
diffexp
orzero?exp
can have only a constant or variable as an argument. Thus in the resulting languagevalueofsimpleexp
can be made nonrecursive.
Solution is implemented here.
Exercise 6.19 [★★]
Write a Scheme procedure
tailform?
that takes the syntax tree of a program in CPSIN, expressed in the grammar of figure 6.3, and determines whether the same string would be in tail form according to the grammar of figure 6.5.
Solution is implemented here.
6.3 Converting to ContinuationPassing Style
6.3.0
Exercise 6.20 [★]
Our procedure
cpsofexps
causes subexpressions to be evaluated from left to right. Modifycpsofexps
so that subexpressions are evaluated from right to left.
Solution is implemented here.
Exercise 6.21 [★]
Modify
cpsofcallexp
so that the operands are evaluated from left to right, followed by the operator.
Solution is implemented here.
Exercise 6.22 [★]
Sometimes, when we generate
(
K simp)
, K is already aprocexp
. So instead of generating
we could generate
let var1 = simp in ...
This leads to CPS code with the property that it never contains a subexpression of the form
unless that subexpression was in the original expression.
Modify
makesendtocont
to generate this better code. When does the new rule apply?
Solution is implemented here.
The new rule applies when transform a expression on a operand position.
Exercise 6.23 [★★]
Observe that our rule for
if
makes two copies of the continuation K, so in a nestedif
the size of the transformed program can grow exponentially. Run an example to confirm this observation. Then show how this may be avoided by changing the transformation to bind a fresh variable toK
.
Solution is implemented here.
Exercise 6.24 [★★]
Add lists to the language (exercise 3.10). Remember that the arguments to a list are not in tail position.
Solution is implemented here.
Exercise 6.25 [★★]
Extend CPSIN so that a
let
expression can declare an arbitrary number of variables (exercise 3.16).
Solution is implemented here.
Exercise 6.26 [★★]
A continuation variable introduced by
cpsofexps
will only occur once in the continuation. Modifymakesendtocont
so that instead of generatinglet var1 = simp1 in T
as in exercise 6.22, it generates T[simp_{1}/var_{1}], where the notation E_{1}[E_{2}/var] means expression E_{1} with every free occurrence of the variable var replaced by E_{2}.
Solution is implemented here.
Exercise 6.27 [★★]
As it stands,
cpsofletexp
will generate a uselesslet
expression. (Why?) Modify this procedure so that the continuation variable is the same as the let variable. Then if exp_{1} is nonsimple,
(cpsofexp <<let
var_{1}=
exp_{1}in
exp_{2}>>
K)
= (cpsofexp
exp_{1}<<proc (
var_{1}) (cpsofexp
exp_{2} K)>>
Solution is implemented here.
Exercise 6.28 [★]
Food for thought: imagine a CPS transformer for Scheme programs, and imagine that you apply it to the first interpreter from chapter 3. What would the result look like?
It would look like a continuationpassing interpreter.
Exercise 6.29 [★★]
Consider this variant of
cpsofexps
.
Why is this variant of cpsofexp more efficient than the one in figure 6.8?
Because this variant only scan exps
once without looking back, the original one will scan exps
multiple times if
there are multiple nonsimple expressions in exps
.
Exercise 6.30 [★★]
A call to
cpsofexps
with a list of expressions of length one can be simplified as follows:
(cpsofexps (list
exp)
builder)
=(cpsofexp/ctx
exp(lambda (simp) (
builder(list simp))))
where
cpsofexp/ctx : InpExp × (SimpleExp → TfExp) → TfExp
Thus, we can simplify occurrences of
(cpsofexps (list ...))
, since the number of arguments to list is known. Therefore the definition of, for example,cpsofdiffexp
could be defined withcpsofexp/ctx
instead of withcpsofexps
.
For the use of
cpsofexps
incpsofcallexp
, we can usecpsofexp/ctx
on therator
, but we still needcpsofexps
for the rands. Remove all other occurrences ofcpsofexps
from the translator.
Solution is implemented here.
Exercise 6.31 [★★★]
Write a translator that takes the output of
cpsofprogram
and produces an equivalent program in which all the continuations are represented by data structures, as in chapter 5. Represent data structures like those constructed usingdefinedatatype
as lists. Since our language does not have symbols, you can use an integer tag in the car position to distinguish the variants of a data type.
Skipped.
Exercise 6.32 [★★★]
Write a translator like the one in exercise 6.31, except that it represents all procedures by data structures.
Skipped.
Exercise 6.33 [★★★]
Write a translator that takes the output from exercise 6.32 and converts it to a register program like the one in figure 6.1.
Skipped.
Exercise 6.34 [★★]
When we convert a program to CPS, we do more than produce a program in which the control contexts become explicit. We also choose the exact order in which the operations are done, and choose names for each intermediate result. The latter is called sequentialization. If we don’t care about obtaining iterative behavior, we can sequentialize a program by converting it to Anormal form or ANF. Here’s an example of a program in ANF.
Whereas a program in CPS sequentializes computation by passing continuations that name intermediate results, a program in ANF sequentializes computation by using
let
expressions that name all of the intermediate results.Retarget
cpsofexp
so that it generates programs in ANF instead of CPS. (For conditional expressions occurring in nontail position, use the ideas in exercise 6.23.) Then, show that applying the revisedcpsofexp
to, e.g., the definition offib
yields the definition offib/anf
. Finally, show that given an input program which is already in ANF, your translator produces the same program except for the names of bound variables.
Solution is implemented here.
Exercise 6.35 [★]
Verify on a few examples that if the optimization of exercise 6.27 is installed, CPStransforming the output of your ANF transformer (exercise 6.34) on a program yields the same result as CPStransforming the program.
Skipped.
6.4 Modeling Computational Effects
6.4.0
Exercise 6.36 [★★]
Add a
begin
expression (exercise 4.4) to CPSIN. You should not need to add anything to CPSOUT.
Solution is implemented here.
Exercise 6.37 [★★★]
Add implicit references (section 4.3) to CPSIN. Use the same version of CPSOUT, with explicit references, and make sure your translator inserts allocation and dereference where necessary. As a hint, recall that in the presence of implicit references, a
varexp
is no longer simple, since it reads from the store.
Solution is implemented here.
Exercise 6.38 [★★★]
If a variable never appears on the lefthand side of a
set
expression, then it is immutable, and could be treated as simple. Extend your solution to the preceding exercise so that all such variables are treated as simple.
Solution is implemented here.
Exercise 6.39 [★]
Implement
letcc
andthrow
in the CPS translator.
Solution is implemented here.
Exercise 6.40 [★★]
Implement
try/catch
andthrow
from section 5.4 by adding them to the CPS translator. You should not need to add anything to CPSOUT. Instead, modifycpsofexp
to take two continuations: a success continuation and an error continuation.
Solution is implemented here.
7 Types
7.1 Values and Their Types
7.1.0
Exercise 7.1 [★]
Below is a list of closed expressions. Consider the value of each expression. For each value, what type or types does it have? Some of the values may have no type that is describable in our type language.
proc 
proc  proc
proc x
proc proc
proc
proc
proc if x then 88 else 99
proc if x then y else 99 proc
proc if then else  proc proc proc
proc if then  else proc proc
proc let d = proc proc in proc
proc 
int > int
proc  proc
(
t> int) > (
t> int)
proc x
t
>
tproc proc
(
t_{1}>
t_{2}) > (
t_{1}>
t_{2})
proc
(int >
t) >
tproc
Not describable
proc if x then 88 else 99
bool > int
proc if x then y else 99 proc
bool > (int > int)
Type error
Type error
proc if then else  proc proc proc
(
t> int) > ((int > int) > ((int > bool) > (
t> int)))
proc if then  else proc proc
int > ((int > bool) > (((int > bool) > int) > int))
proc let d = proc proc in proc
((
t_{1}>
t_{2}) > (
t_{1}>
t_{2})) > (
t_{1}>
t_{2})
(Not sure about this one)
Exercise 7.2 [★★]
Are there any expressed values that have exactly two types according to definition 7.1.1?
No, I don’t think so.
Exercise 7.3 [★★]
For the language LETREC, is it decidable whether an expressed value val is of type t?
Not sure about this one.
7.2 Assigning a Type to an Expression
7.2.0
Exercise 7.4 [★]
Using the rules of this section, write derivations, like the one on page 5, that assign types for
proc (x) x
andproc (x) proc (y) (x y)
. Use the rules to assign at least two types for each of these terms. Do the values of these expressions have the same types?
\[ \dfrac{\texttt{(typeof «x» $[\texttt{x}=t]tenv$)} = t} {\texttt{(typeof «proc (x) x» $tenv$)} = \texttt{($t$ > $t$)}} \]
\[ \dfrac{\dfrac{\dfrac{\eqalign{\texttt{(typeof «x» $[\texttt{y}=t_1][\texttt{x}=\texttt{($t_1$ > $t_2$)}]tenv$)} &= \texttt{($t_1$ > $t_2$)} \\ \texttt{(typeof «y» $[\texttt{y}=t_1][\texttt{x}=\texttt{($t_1$ > $t_2$)}]tenv$)} &= t_1}} {\texttt{(typeof «(x y)» $[\texttt{y}=t_1][\texttt{x}=\texttt{($t_1$ > $t_2$)}]tenv$)} = t_2}} {\texttt{(typeof «proc (y) (x y)» $[\texttt{x}=\texttt{($t_1$ > $t_2$)}]tenv$)} = \texttt{($t_1$ > $t_2$)}}} {\texttt{(typeof «proc (x) proc (y) (x y)» $tenv$)} = \texttt{($t_1$ > $t_2$) > ($t_1$ > $t_2$)}} \]
The values of these expressions do not necessarily have the same types. According to the actual type of t, the result type may be different.
CHECKED: A TypeChecked Language
7.3.1 The Checker
Exercise 7.5 [★★]
Extend the checker to handle multiple
let
declarations, multiargument procedures, and multipleletrec
declarations. You will need to add types of the form(
t_{1}*
t_{2}* ... *
t_{n}>
t)
to handle multiargument procedures.
Solution is implemented here.
Exercise 7.6 [★]
Extend the checker to handle assignments (section 4.3).
Solution is implemented here.
Exercise 7.7 [★]
Change the code for checking an
ifexp
so that if the test expression is not a boolean, the other expressions are not checked. Give an expression for which the new version of the checker behaves differently from the old version.
Solution is implemented here.
This expression behaves differently in the new version of checker:
if 1 then  else 2
Exercise 7.8 [★★]
Add
pairof
types to the language. Say that a value is of typepairof
t_{1}*
t_{2} if and only if it is a pair consisting of a value of type t_{1} and a value of type t_{2}. Add to the language the following productions:Type ::=
pairof
Type*
Type
pairtype (ty1 ty2)
Expression ::=
newpair (
Expression,
Expression)
pairexp (exp1 exp2)
Expression ::=
unpair
Identifier Identifier=
Expression
in
Expression
unpairexp (var1 var2 exp body)
A
pair
expression creates a pair; anunpair
expression (like exercise 3.18) binds its two variables to the two parts of the expression; the scope of these variables isbody
. The typing rules forpair
andunpair
are:\[ \dfrac{\eqalign{\texttt{(typeof $e_1$ $tenv$)} &= t_1 \\ \texttt{(typeof $e_1$ $tenv$)} &= t_2}} {\texttt{(typeof (pairexp $e_1$ $e_2$) $tenv$)} = \texttt{pairof $t_1$ * $t_2$}} \]
\[ \dfrac{\eqalign{ \texttt{(typeof $e_{pair}$ $tenv$)} &= \texttt{pairof $t_1$ $t_2$} \\ \texttt{(typeof $e_{body}$ $[var_1=t_1][var_2=t_2]tenv$)} &= t_{body}}} {\texttt{(typeof (unpairexp $var_1$ $var_2$ $e_1$ $e_{body}$) $tenv$)} = t_{body}} \]
Extend CHECKED to implement these rules. In
typetoexternalform
, produce the list(pairof
t_{1} t_{2})
for a pair type.
Solution is implemented here.
Exercise 7.9 [★★]
Add
listof
types to the language, with operations similar to those of exercise 3.9. A value is of typelistof
t if and only if it is a list and all of its elements are of type t. Extend the language with the productionsType ::=
listof
Type
listtype (ty)
Expression ::=
list (
Expression {,
Expression}^{∗})
listexp (exp1 exps)
Expression ::=
cons (
Expression,
Expression)
consexp (exp1 exp2)
Expression ::=
null? (
Expression)
nullexp (exp1)
Expression ::=
emptylist [
Type]
emptylistexp (ty)
with types given by the following four rules:
\[ \dfrac{\eqalign{\texttt{(typeof $e_1$ $tenv$)} &= t \\ \texttt{(typeof $e_2$ $tenv$)} &= t \\ &⋮ \\ \texttt{(typeof $e_n$ $tenv$)} &= t}} {\texttt{(typeof (listexp $e_1$ ($e_2$ $…$ $e_n$)) $tenv$)} = \texttt{listof $t$}} \]
\[ \dfrac{\eqalign{\texttt{(typeof $e_1$ $tenv$)} &= t \\ \texttt{(typeof $e_2$ $tenv$)} &= \texttt{listof $t$}}} {\texttt{(typeof «cons($e_1$, $e_2$)» $tenv$)} = \texttt{listof $t$}} \]
\[ \dfrac{\texttt{(typeof $e_1$ $tenv$)} = \texttt{listof $t$}} {\texttt{(typeof «null?($e_1$)» $tenv$)} = \texttt{bool}} \]
\[ \texttt{(typeof «emptylist[$t$]» $tenv$)} = \texttt{listof $t$} \]
Although
cons
is similar topair
, it has a very different typing rule.Write similar rules for
car
andcdr
, and extend the checker to handle these as well as the other expressions. Use a trick similar to the one in exercise 7.8 to avoid conflict withproctypeexp
. These rules should guarantee thatcar
andcdr
are applied to lists, but they should not guarantee that the lists be nonempty. Why would it be unreasonable for the rules to guarantee that the lists be nonempty? Why is the type parameter inemptylist
necessary?
Solution is implemented here.
Because a list may be dynamic generated, we have no way to know the length of the list statically, so we can not guarantee a list is nonempty.
Because our implementation must know the exact type of every expression. If we omit the type parameter, we couldn’t
determine the type the expression emptylist
.
Exercise 7.10 [★★]
Extend the checker to handle EXPLICITREFS. You will need to do the following:
 Add to the type system the types
refto
t, where t is any type. This is the type of references to locations containing a value of type t. Thus, if e is of type t,(newref
e)
is of typerefto
t. Add to the type system the type
void
. This is the type of the value returned bysetref
. You can’t apply any operation to a value of typevoid
, so it doesn’t matter what valuesetref
returns. This is an example of types serving as an informationhiding mechanism. Write down typing rules for
newref
,deref
, andsetref
. Implement these rules in the checker.
Solution is implemented here.
Exercise 7.11 [★★]
Extend the checker to handle MUTABLEPAIRS.
Solution is implemented here.
7.4 INFERRED: A Language with Type Inference
7.4.0
Exercise 7.12 [★]
Using the methods in this section, derive types for each of the expressions in exercise 7.1, or determine that no such type exists. As in the other examples of this section, assume there is a
?
attached to each bound variable.
Turns out most of my solutions are correct, except for the last one. I think the last one
proc let d = proc
proc
in proc
is a fixedpoint combinator, and should have the type
((
t_{1} >
t_{2}) > (
t_{1} >
t_{2})) > (
t_{1} >
t_{2})
.
But due to our limited type system, we couldn’t assign a concrete type to d
or x
, so we failed to infer the who
program.
In fact, the following program in Typed Racket type checks.
#lang typed/racket/base
Exercise 7.13 [★]
Write down a rule for doing type inference for
let
expressions. Using your rule, derive types for each of the following expressions, or determine that no such type exists.
let x = 4 in (x 3)
let f = proc (z) z in proc (x) ((f x), 1)
let p = zero?(1) in if p then 88 else 99
let p = proc (z) z in if p then 88 else 99
\[ \begin{alignat}{2} \texttt{(letexp $var$ $e_1$ $body$)} &: & t_{var} &= t_{e_1} \\ & & t_{body} &= t_\texttt{(letexp $var$ $e_1$ $body$)} \end{alignat} \]

let x = 4 in (x 3)
Type error.

let f = proc (z) z in proc (x) ((f x), 1)
int > int

let p = zero?(1) in if p then 88 else 99
int

let p = proc (z) z in if p then 88 else 99
Type error.
Exercise 7.14 [★]
What is wrong with this expression?
letrec ? even= proc if zero? then 1 else in letrec ? odd = if zero? then 0 else in
The parameter x
of odd
should be of type int
.
Exercise 7.15 [★★]
Write down a rule for doing type inference for a
letrec
expression. Your rule should handle multiple declarations in aletrec
. Using your rule, derive types for each of the following expressions, or determine that no such type exists:
letrec ? f = if zero? then 0 else  in f
letrec ? even = if zero? then 1 else ? odd = if zero? then 0 else in
letrec ? even = proc if zero? then 1 else in letrec ? odd = if zero? then 0 else in
\[ \begin{alignat}{2} \texttt{(letrecexp $pnames$ $bvars$ $pbodies$ $letrecbody$)} &: & t_{pname_1} &= \texttt{($t_{bvar_1}$ > $t_{pbody_1}$)} \\ & & t_{pname_2} &= \texttt{($t_{bvar_2}$ > $t_{pbody_2}$)} \\ & & &⋮ \\ & & t_{pname_n} &= \texttt{($t_{bvar_n}$ > $t_{pbody_n}$)} \\ & & t_{letrecbody} &= t_\texttt{(letrecexp $pnames$ $bvars$ $pbodies$ $letrecbody$)} \end{alignat} \]
letrec ? f = if zero? then 0 else  in f
(int > int)
letrec ? even = if zero? then 1 else ? odd = if zero? then 0 else in
int
letrec ? even = proc if zero? then 1 else in letrec ? odd = if zero? then 0 else in
int
Exercise 7.16 [★★★]
Modify the grammar of INFERRED so that missing types are simply omitted, rather than marked with
?
.
Solution is implemented here.
7.4.1 Substitutions
Exercise 7.17 [★★]
In our representation,
extendsubst
may do a lot of work if σ is large. Implement an alternate representation in whichextendsubst
is implemented as