Home
Schedule
Software
Mailing List

Typed Machine

Due: Tuesday, February 4, 00:00AM

Chapter 2.2 of CPDT presents a typed language of arithmetic and boolean expressions. Your task is to extend the language to support pairs. Like the previous assignment, the trick is getting the definitions right. Once you do, the proofs of correctness should remain completely unchanged.

You’ll need to make the following changes:

  1. Add a new type constructor for pairs to type,

  2. Add a new binary operation to create new pairs,

  3. Add a new class of unary operators to return the left and right projections of pairs,

  4. Extend the definition of equality to allow equality testing on pairs, and

  5. Augment the compiler and machine language as needed.

Hints

Semantics of Pairs

Have pairs in your language denote pairs in Coq, which are typically written as x * y. However, this is just notation for (prod _ _ x y), which is defined in Init.Datatypes. If you want to explicitly supply the type arguments to prod, you can write @prod A B x y. For example, @prod nat bool 10 true is the same as (10, true). This is particularly useful if you want to partially apply prod to its type arguments.

Coq’s Type Inference and Error Messages

You may get some unhelpful error messages when hacking. For example, in the following definition, I haven’t filled out the case for making pairs:

Definition tbinopDenote (arg1 arg2 res : type) (b : tbinop arg1 arg2 res)
      : typeDenote arg1 -> typeDenote arg2 -> typeDenote res :=
  match b with
    | TPlus => plus
    | TTimes => mult
    | TEq t => typEq t
    | TLt => leb
  end.

But, Coq reports a useless error:

The term "plus" has type "nat -> nat -> nat"
 while it is expected to have type
  "typeDenote ?42 -> typeDenote ?43 -> typeDenote ?44".

If you instead type the following:

Definition tbinopDenote (arg1 arg2 res : type) (b : tbinop arg1 arg2 res)
  : typeDenote arg1 -> typeDenote arg2 -> typeDenote res :=
  match b in tbinop arg1 arg2 res 
    return typeDenote arg1 -> typeDenote arg2 -> typeDenote res with
    | TPlus => plus
    | TTimes => mult
    | TEq t => typEq t
    | TLt => leb
  end.

Coq reports a much more helpful error:

Error: Non exhaustive pattern-matching: no clause found for pattern
TMakePair _ _

The in ... return ... expression is part of the full syntax of dependent pattern matching. When you elide it, Coq can often infer it correctly. In this example, if you miss a case, Coq’s inference fails us and we get an awful error message. We will study this in detail in a few weeks.

When in doubt, add more type annotations.