[plt-scheme] macro expension order

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Mon Jul 9 19:21:28 EDT 2007

Hi all,

I have the following program;

  (define-syntax NONE (syntax-id-rules () (NONE 'NONE)))
  (define-syntax SOME (syntax-rules () ((SOME g57951) (list 'SOME g57951))))
  (define expt
    (match-lambda*
      ((list NONE n) (expt (SOME 2) n))
      ((list (list 'SOME b) 0) 1)
      ((list (list 'SOME b) n) (if (= (mod n 2) 0) (expt (SOME (* b b)) 
(/ n 2)) (* b (expt (SOME b) (- n 1)))))))

It is automatically translated from the following SML program:

datatype 'a option = NONE | SOME of 'a

fun expt (NONE, n) = expt (SOME 2, n)
   | expt (SOME b, 0) = 1
   | expt (SOME b, n) =
     if n mod 2 = 0 then
        expt (SOME (b*b), n div 2)
     else
        b * expt (SOME b, n-1)

Obviously, here I'm hoping that the syntax-id-rules of NONE could be 
expanded before syntax-case of match-lambda*. But it doesn't. As the 
code shows, I already did some hack so that the "SOME b" is translated 
to (list 'SOME b) instead of (SOME b) in the pattern. But it is 
impossible to do the same trick for "NONE": it is indistinguishable from 
a variable binding by syntax. In other words, the way ML patterns are 
translated could not be changed, but the way ML datatypes are translated 
can be changed.

So here I would like to ask: what is the correct way to "define" 
something that will apply before the match library see the pattern? Or, 
maybe I can change the default order of macro expansion (although this 
may not be a good idea)?

Thanks in advance.

Sincerely,
Chongkai


Posted on the users mailing list.