[plt-scheme] macro expension order

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Jul 9 21:41:49 EDT 2007

At Mon, 09 Jul 2007 20:17:49 -0500, Chongkai Zhu wrote:
> The problem is not where stucts or lists or whatever, but telling the 
> difference between a datatype (NONE here) or an identifier (say, n here) 
> in ML source code (the pattern part), and then express the difference in 
> Scheme program.
> I can easily translate
> datatype 'a option = NONE | SOME of 'a
> into
>    (define-struct NONE ())
>    (define-struct SOME (b))
> but then if I see
> fun expt (NONE, n) = expt (SOME 2, n)
> I need (some knowledge) to tell here NONE is not the same thing as the 
> "n" next to it.

Ah -- your goal is much clearer now. 

I think you probably want to expand `id' in a pattern to something like
`(ML-pattern-id id)'. Then define `ML-pattern-id' as a match expander
that checks the binding of `id'; if it's bound as a struct type, then
expand to a suitable structure pattern, otherwise expand to just `id'.

Or something like that. The key point is that you can pick what an
identifier means in a match pattern by wrapping it with a form that you
control (as a match expander).


Posted on the users mailing list.