That woulda been cool when I was going though that book. When you
finish you might want to try to get a link from Pierce's web site,
and/or inclusion in his download directory for the book, as that's
probably most likely place I would have seen it.<br>
<br>
Corey<br>
<br><br><div><span class="gmail_quote">On 1/5/06, <b class="gmail_sendername">David Van Horn</b> <<a href="mailto:dvanhorn@cs.brandeis.edu">dvanhorn@cs.brandeis.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Danny Yoo wrote:<br>> I've been reading Pierce's excellent "Types and Programming Languages"<br>> textbook, and thought it might be fun to try to use PLT Scheme instead of<br>> OCaml.<br><br>It is fun---I've done this sort of thing over and over and over again.
<br><br>> <a href="http://hkn.eecs.berkeley.edu/~dyoo/tmp/tapl-plt.txt">http://hkn.eecs.berkeley.edu/~dyoo/tmp/tapl-plt.txt</a><br><br>Just a couple comments on the code:<br><br>> (**********************************)
<br>> let rec isval t = match t with<br>> TmTrue -> true<br>> | TmFalse -> true<br>> | t when isnumericalval t -> true<br>> | _ -> false<br>> (**********************************)<br>
><br>><br>> Here's our equivalent in PLT Scheme:<br>><br>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>> (define (isval t)<br>> (match t<br>> ((struct True ()) #t)<br>> ((struct False ()) #t)
<br>> (t (=> fail) (if (isnumericalval t) #t (fail)))<br>> (else #f)))<br>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>><br>> It's slightly uglier than in OCaml --- the third clause uses the
<br>> enhanced "=>" form to make sure pattern matching can continue when<br>> isnumericalval fails --- but still, it isn't bad. (And of course we<br>> could easily simplify the function so it doesn't need it, but it's
<br>> good to know about "=>", since eval1 uses the "when" form quite a<br>> bit.)<br><br>You might use the `?' pattern in plt-match.ss to correspond to `when'<br>guards in OCaml. Eg:<br><br>
(define (isval t)<br> (match t<br> ((struct True ()) #t)<br> ((struct False ()) #t)<br> ((? isnumericalval) #t)<br> (else #f)))<br><br>Another thing to note is that in OCaml, isval is of type t -> bool, but
<br>your Scheme predicates are Any -> bool, which isn't bad in itself, but<br>if you're applying isval to non-terms, chances are it's a bug. I use<br>contracts to catch those things.<br><br>As for concrete syntax, I almost never write parsers when I'm
<br>experimenting with languages implemented in Scheme. I just cook up an<br>S-expression concrete syntax and write functions parse : S-expr -> t and<br>unparse : t -> S-expr. (Of course, learning the parser tools is a fine
<br>thing.) There are examples of this in the pattern matcher documentation<br>if I recall correctly.<br><br>David<br><br>_________________________________________________<br> For list-related administrative tasks:<br>
<a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br></blockquote></div><br><br clear="all"><br>-- <br>((lambda (y) (y y)) (lambda (y) (y y)))