[plt-scheme] macro expension order
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.
One way to solve this problem is to have the translation program
remember those ML datatypes and there scope. But the datatype definition
can be but put in another ML structure/signature, which is not easily
accessible to the translation program. So here I'm trying to somehow
just put the knowledge into Scheme environment and have the program tell
the difference between "NONE" and "n" by checking the environment.
Chongkai
Robby Findler wrote:
> Chongkai: Jens Axel is making the reasonable (indeed wise!) suggestion
> that you represent ML values with structs, not lists. This will be
> more efficient and will make it easier to avoid bugs. All in all, a
> good idea.
>
> Robby
>
> On 7/9/07, Chongkai Zhu <czhu at cs.utah.edu> wrote:
>> The problem is: to _compile_ SML code into PLT Scheme, not to manually
>> translate ML.
>>
>> According to SML syntax, in (list NONE n), there is no obvious
>> difference bewetween NONE and n: they are both normal symbol.
>>
>> Chongkai
>>
>> Jens Axel Søgaard wrote:
>> > Chongkai Zhu skrev:
>> >> 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)
>> >
>> >
>> > Why not represent NONE and SOME as structures?
>> >
>> > (define-struct NONE ())
>> > (define-struct SOME (b))
>> >
>> > (require (lib "plt-match.ss"))
>> >
>> > (define expt
>> > (match-lambda*
>> > [(list (struct NONE ()))
>> > (expt (make-SOME 2) n)]
>> > [(list (struct SOME (b)) 0)
>> > 1]
>> > [(list (struct SOME (b)) n)
>> > (if (= (modulo n 2) 0)
>> > (expt (make-SOME (* b b)) (quotient n 2))
>> > (* b (expt (make-SOME b) (- n 1))))]))
>> >
>> > (expt (make-SOME 2) 0) ; => 1
>> > (expt (make-SOME 2) 1) ; => 2
>> > (expt (make-SOME 2) 2) ; => 4
>> > (expt (make-SOME 2) 3) ; => 8
>> >
>> >
>> _________________________________________________
>> For list-related administrative tasks:
>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>