[plt-scheme] Re: Wrote some notes on using PLT Scheme for TAPL stuff

From: David Van Horn (dvanhorn at cs.brandeis.edu)
Date: Thu Jan 5 10:49:27 EST 2006

Danny Yoo wrote:
> I've been reading Pierce's excellent "Types and Programming Languages"
> textbook, and thought it might be fun to try to use PLT Scheme instead of
> OCaml.

It is fun---I've done this sort of thing over and over and over again.

>     http://hkn.eecs.berkeley.edu/~dyoo/tmp/tapl-plt.txt

Just a couple comments on the code:

> (**********************************)
> let rec isval t = match t with
>     TmTrue -> true
>   | TmFalse -> true
>   | t when isnumericalval t -> true
>   | _ -> false
> (**********************************)
> Here's our equivalent in PLT Scheme:
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define (isval t)
>   (match t
>     ((struct True ()) #t)
>     ((struct False ()) #t)
>     (t (=> fail) (if (isnumericalval t) #t (fail)))
>     (else #f)))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> It's slightly uglier than in OCaml --- the third clause uses the
> enhanced "=>" form to make sure pattern matching can continue when
> isnumericalval fails --- but still, it isn't bad.  (And of course we
> could easily simplify the function so it doesn't need it, but it's
> good to know about "=>", since eval1 uses the "when" form quite a
> bit.)

You might use the `?' pattern in plt-match.ss to correspond to `when'
guards in OCaml.  Eg:

(define (isval t)
   (match t
     ((struct True ()) #t)
     ((struct False ()) #t)
     ((? isnumericalval) #t)
     (else #f)))

Another thing to note is that in OCaml, isval is of type t -> bool, but
your Scheme predicates are Any -> bool, which isn't bad in itself, but
if you're applying isval to non-terms, chances are it's a bug.  I use
contracts to catch those things.

As for concrete syntax, I almost never write parsers when I'm
experimenting with languages implemented in Scheme.  I just cook up an
S-expression concrete syntax and write functions parse : S-expr -> t and
unparse : t -> S-expr.  (Of course, learning the parser tools is a fine
thing.)  There are examples of this in the pattern matcher documentation
if I recall correctly.


Posted on the users mailing list.