racketplai

Must a colon separate an id and a type? Is a type with a question mark no longer acceptable?


Scheme (Racket) newbie here.

I am reading this book: Programming Languages Application and Interpretation by Shriram Krishnamurthi.

I installed the plai-typed package.

The book has this define-type on page 6:

(define-type AE?
  [num (n number?)]
  [add (lhs AE?)(rhs AE?)]
  [sub (lhs AE?)(rhs AE?)])

When I enter that in Racket, it complains that there must be colon between n and number?, and the type must be number not number?

I made the changes to define-type:

(define-type AE
  [num (n : number)]
  [add (lhs : AE)(rhs : AE)]
  [sub (lhs : AE)(rhs : AE)])

Racket seems to like this.

So, is every example in the book incorrect?

Is there a way to get Racket to accept the first version of define-type? Or, do I need to modify every example in the book?


Solution

  • You are reading the first edition of the book, which requires the package plai rather than plai-typed.

    The second edition of the book (https://cs.brown.edu/courses/cs173/2012/book/book.pdf) uses plai-typed, which is the package that you installed.

    So you can either switch the book and continue using plai-typed, or switch the package and continue using the book.

    One major difference between plai and plai-typed is that the latter has static type checking. So if you prefer static type checking, you might want to switch the book.