Types and Programming Languages Exercises

Preface

2 Mathematical Preliminaries

2.2 Ordered Sets

2.2.6

Exercise [★★ ↛]: Suppose we are given a relation R on a set S. Define the relation R′ as follows:

R′ = R ∪ {(s, s) | s ∈ S}.

That is, R′ contains all the pairs in R plus all pairs of the form (s, s). Show that R′ is the reflexive closure of R.

We need to show that:

2.2.7

Exercise [★★, ↛]: Here is a more constructive definition of the transitive closure of a relation R. First, we define the following sequence of sets of pairs:

R0 = R Ri+1 = Ri ∪ { (s,u) ∣ for some t,  (s,t) ∈ Ri  and  (t,u) ∈ Ri }

That is, we construct each Ri + 1 by adding to Ri all the pairs that can be obtained by “one step of transitivity” from pairs already in Ri. Finally, define the relation R+ as the union of all the Ri:

R+ = ⋃ i Ri

Show that this R+ is really the transitive closure of R—i.e., that it satisfies the conditions given in Definition 2.2.5.

We need to show that:

2.2.8

Exercise [★★, ↛]: Suppose R is a binary relation on a set S and P is a predicate on S that is preserved by R. Show that P is also preserved by R∗.

We need to show that: For any s ∈ S and t ∈ S: (s, t) ∈ R∗ and s ∈ P implies t ∈ P.

Since P is preserved by R, we have: For any s ∈ S and t ∈ S: (s, t) ∈ R and s ∈ P implies t ∈ P.

First, we prove that for all i, P is preserved by Ri that is defined in exercise 2.2.7. This can be done by induction:

Since R∗ is the union of all Ri, P is also preserved by R∗.

3 Untyped Arithmetic Expressions

3.2 Syntax

3.2.4

Exercise [★★]: How many elements does S3 have?

By definition:

So we have:

3.2.5

Exercise [★★]: Show that the sets Si are cumulative—that is, that for each i we have Si ⊆ Si + 1.

Proof by induction:

3.3 Induction on Terms

3.3.4

Theorem [Principles of induction on terms]: Suppose P is a predicate on terms.

  • Induction on depth:
    • If, for each term s,
      • given P(r) for all r such that depth(r) < depth(s)
        we can show P(s),
    • then P(s) holds for all s.
  • Induction on size:
    • If, for each term s,
      • given P(r) for all r such that size(r) < size(s)
        we can show P(s),
    • then P(s) holds for all s.
  • Structural induction:
    • If, for each term s,
      • given P(r) for all immediate subterms r of s
        we can show P(s),
    • then P(s) holds for all s.

Proof: Exercise (★★).

3.5 Evaluation

3.5.5

Exercise [★]: Spell out the induction principle used in the preceding proof, in the style of Theorem 3.3.4.

3.5.10

Exercise [★]: Rephrase Definition 3.5.9 as a set of inference rules.

t→t′ t→∗t′

t→∗t

t→∗t′ t′→∗t″ t→∗t″

3.5.13

Exercise [Recommended, ★★]:

  1. Suppose we add a new rule
    if true then t 2 else t 3 → t 3 (E-Funny1)
    to the ones in Figure 3-1. Which of the above theorems (3.5.4, 3.5.7, 3.5.8, 3.5.11, and 3.5.12) remain valid?
  2. Suppose instead that we add this rule:
    t 2 → t 2 ′ if t 1 then t 2 else t 3 → if t 1 then t 2 ′ else t 3 (E-Funny2)
    Now which of the above theorems remain valid? Do any of the proofs need to change?
    • 3.5.4: If t → t′ and t → t″, then t′ = t″.
      • Invalid.
    • 3.5.7: Every value is in normal form.
      • Valid.
    • 3.5.8: If t is in normal form, then t is a value.
      • Valid.
    • 3.5.11: If t →∗ u and t →∗ u′, where u and u′ are both normal forms, then u = u′.
      • Invalid.
    • 3.5.12: For every term t there is some normal form t′ such that t →∗ t′.
      • Valid.
    • 3.5.4: If t → t′ and t → t″, then t′ = t″.
      • Invalid.
    • 3.5.7: Every value is in normal form.
      • Valid.
    • 3.5.8: If t is in normal form, then t is a value.
      • Valid.
    • 3.5.11: If t →∗ u and t →∗ u′, where u and u′ are both normal forms, then u = u′.
      • Valid.
    • 3.5.12: For every term t there is some normal form t′ such that t →∗ t′.
      • Valid.

List of common unicode characters:

Code PointNameAliasCharacter
U+2032PRIMEminutes, feet′
U+2033DOUBLE PRIMEseconds, inches″
U+219BRIGHTWARDS ARROW WITH STROKE↛
U+2205EMPTY SETnull set∅
U+2208ELEMENT OF∈
U+2209NOT AN ELEMENT OF∉
U+2217ASTERISK OPERATOR∗
U+2223DIVIDESsuch that, APL stile∣
U+222AUNIONcup∪
U+2286SUBSET OF OR EQUAL TO⊆
U+22C3N-ARY UNIONz notation generalised union⋃
U+2605BLACK STAR★