[racket] Define-type language form, and macros

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Sun Sep 11 17:09:05 EDT 2011

The main other interesting things going on are validating that all the
variants are from the same type, every variant is covered, all the
fields are there, etc.

Jay

On Sun, Sep 11, 2011 at 1:30 PM, Jeremy Kun <kun.jeremy at gmail.com> wrote:
> So it looks like there are a lot more complicated things going on in
> define-type and type-case than I naively tried to implement myself. It's
> probably safer to just do the nice (require (only-in)) thing and not
> reinvent a worse version of the wheel. Although I did get my particular
> define-syntax to work as I wanted it to, I'm guessing it could be used in
> more interesting ways than I anticipated, and cause things to break horribly
> in ways I don't want.
> Thanks for the tips!
>
> Jeremy
>
>
> On Sun, Sep 11, 2011 at 1:06 PM, Danny Yoo <dyoo at cs.wpi.edu> wrote:
>>
>> 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.
>
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93



Posted on the users mailing list.