[racket] Define-type language form, and macros

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Sun Sep 11 14:06:43 EDT 2011

On Sun, Sep 11, 2011 at 1:44 PM, Jeremy Kun <kun.jeremy at gmail.com> wrote:
> Is there an existing Racket analogue for the "define-type" language form
> from the PLAI text?

You can pull out specific things from a language or module by using:

   (require (only-in some-module
                          language-feature-1 language-feature-2 ...))


In particular, it looks like you want to pull some of the features of
the PLAI language

    http://docs.racket-lang.org/plai/plai-scheme.html

into vanilla racket.  Yup, that should work!


For example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket

(require (only-in plai
                  define-type
                  type-case))

(define-type expr
  [num (val integer?)]
  [sum (lhs expr?)
       (rhs expr?)])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



> Or
> perhaps, is there a way for me to look at the original syntax definitions so
> I can puzzle ovdrer how it's done?

The PLAI language is written in Racket itself, so you're always
welcome to look at how it's implemented.  I think it starts around
here:

    https://github.com/plt/racket/tree/master/collects/plai

You'll see in https://github.com/plt/racket/blob/master/collects/plai/main.rkt
that the implementation is made of two modules, one that implements
the datatype stuff, and the other the testing harness.  The datatype
library itself (https://github.com/plt/racket/blob/master/collects/plai/datatype.rkt)
looks... well.. substantial... :)  Might take a while to understand
how it all fits together.  We can talk about it more if you'd like.



[cutting your macro code]

> I get an interesting error along the lines of
>
> define-values: illegal use (not at top-level) in: (define-values...
>
> I'm guessing it has something to do with the rule that a define-syntax isn't
> allowed to change the surrounding lexical scope

Yeah; there are certain syntactic contexts

    [see: http://docs.racket-lang.org/reference/syntax-model.html#(part._expand-context-model)
for gory details]

where you're not allowed to have a define-syntax.  Expression position
is one of them.  The arguments to values are in expression context.
You probably want to use 'begin' here instead, unless I'm
misunderstanding your code?




> On another note, is there a nice way for me to print out the literal
> expansion performed by define-syntax?

Have you tried the macro debugger tool in DrRacket yet?  The macro
debugger at the DrRacket toolbar lets you step through macro
expansion.

There are also tools in:

    http://docs.racket-lang.org/macro-debugger/index.html

that may help.


Posted on the users mailing list.