I'm learning Racket and I stumpbled into a couple of problems with macros.
I tried to make macros that implements recursive lambda, but not the classic one (that uses letre), but the one that uses Y combinator.
So it should work like this:
(recursion fact (n)
(if (zero? n)
1
(* n (fact (sub1 n)))))
transforms into
((lambda (x) (x x))
(lambda (fact)
(lambda (n)
(if (zero? n) 1 (* n ((fact fact) (sub1 n)))))))
which produces recursive anonymous function to compute factorial.
So I wrote this macros:
(define-syntax recursion
(syntax-rules ()
[(_ label (args ...) body ...)
((lambda (x) (x x))
(lambda (label)
(lambda (args ...)
(substitute-term label (label label) body) ...)))]))
(substitute-term) macros is helper macros to substitute one piece of code with another, here its fist version:
(define-syntax (substitute-term stx)
(syntax-case stx ()
[(_ term-from term-to body)
(cond
[(null? (syntax-e #'body)) #'(void)]
[(list? (syntax-e #'body)) #`#,(map (lambda (x) (append (syntax-e #'(substitute-term term-from term-to)) (if (list? x) x (list x)))) (syntax-e #'body))]
[else (if (equal? (syntax-e #'body) (syntax-e #'term-from)) #'term-to #'body)])]))
>(substitute-term - + (- 1 2))
3
This works. But
>(substitute-term and or (and #t #f))
or: bad syntax in: or
Macro stepper shows that it expands into
(or (substitute-term and or #t) (substitute-term and or #f))
And after this step is "bad syntax" error. I couldn't figure why is this and how to fix it. It raises "bad syntax" errors with all special forms for some reason. Can somebody explain to me -- why? And how to fix it?
Then I tried rewrite (substitute-term) macro:
(define-syntax (substitute-term-2 stx)
(syntax-case stx ()
[(substitute-term term-from term-to body)
(datum->syntax stx (for-substitute-term (syntax->datum #'term-from) (syntax->datum #'term-to) (syntax->datum #'body)))]))
It uses helper function (define-for-syntax) which do all the work:
(define-for-syntax (for-substitute-term term-from term-to expr)
(cond
[(null? expr) (void)]
[(list? expr) (map (lambda (x) (apply for-substitute-term (list term-from term-to x))) expr)]
[else (if (equal? expr term-from) term-to expr)]))
>(substitute-term-2 and or (and #t #f))
#t
Hurray! But if I use it in my (recursion) macro:
(define-syntax recursion-2
(syntax-rules ()
[(_ label (args ...) body ...)
((lambda (x) (x x))
(lambda (label)
(lambda (args ...)
(substitute-term-2 label (label label) body) ...)))]))
>((recursion-2 fact (n)
(if (zero? n)
1
(* n (fact (sub1 n))))) 5)
n: unbound identifier in module
context...:
other binding...: in: n
Although macro stepper shows that it expands into
((lambda (x) (x x))
(lambda (fact)
(lambda (n)
(substitute-term-2
fact
(fact fact)
(if (zero? n) 1 (* n (fact (sub1 n))))))))
Which if entered in interaction area works as intended. I understand that binding for n is lost when I invoke (substitute-term-2) macro on body. But I couldn't find in documentation -- why? And how to fix it?
I would be grateful, if somebody explained to me what's wrong with my first and my second attempts and how to fix them. Thanks!
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks for your answer, it was very helpful.
So i rewrote my (substitute-term) macro like this:
(define-syntax (substitute-term stx)
(syntax-case stx ()
[(_ term-from term-to (body0 body ...))
#`#,(append (if (equal? (syntax-e #'body0) (syntax-e #'term-from))
(list #'term-to)
(list #'body0))
#'((substitute-term term-from term-to body) ...))]
[(_ term-from term-to body)
(if (equal? (syntax-e #'body)
(syntax-e #'term-from))
#'term-to
#'body)]))
The (syntax-e) parts is kept because (equal? #'body #'term-from) won't yield #t.
Now it works:
>(substitute-term and or (and #t #f))
#t
After a little bit tinkering I returned to (case) form, so it can substitute not only single terms, but whole expressions:
(define-syntax (substitute-syntax stx)
(syntax-case stx ()
[(_ term-from term-to body)
(cond
[(equal? (syntax->datum #'body) (syntax->datum #'term-from)) #'term-to]
[(list? (syntax->datum #'body)) #`#,(append (if (equal? (syntax->datum (car (syntax-e #'body)))
(syntax->datum #'term-from))
(list #'term-to)
(list #`#,(car (syntax-e #'body))))
(map (lambda (x)
(append (syntax-e #'(substitute-syntax term-from term-to))
(list x)))
(cdr (syntax-e #'body))))]
[else #'body])]))
>(substitute-syntax (and #t #f) or ((and #t #f) #f #t))
Expands into (or #t #f)
#t
And my final version of recursion looks like this:
(define-syntax recursion
(syntax-rules ()
[(_ (args ...) body ...)
((lambda (x) (x x))
(lambda (generated-label)
(lambda (args ...)
(substitute-syntax recursion (generated-label generated-label) body) ...)))]
[(_ label (args ...) body ...)
((lambda (x) (x x))
(lambda (label)
(lambda (args ...)
(substitute-syntax label (label label) body) ...)))]))
So it can be used without label, if there are no nested recursions:
>((recursion (n)
(if (zero? n)
1
(* n (recursion (sub1 n))))) 5)
5
Now it works, as intended.
Also thanks to Jens and Sam -- your examples is valuable, I learned from them (for example, how I can use (let) to bind variable that was defined before -- so it keeps previous binding in the definitions part of (let) but uses new binding in the body part of (let)).
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
When using a macro, always think: "can I do the same without a macro?"
You use the simple auto-applicator (lambda (x) (x x)).
I suggest to use an applicative-order Y-combinator, such as:
(define Y (λ (m) ((λ (f) (f f)) (λ (g) (m (λ (n) ((g g) n)))))))
Now: ((Y (lambda (!) (lambda (n) (if (zero? n) 1 (* n (! (sub1 n))))))) 4) -> 24
There are many more ways to write an applicative-order Y-combinator.
Now is the moment to think about a sugaring macro:
#lang racket
; Let Y work for functions of more than one argument:
(define Y (λ (m) ((λ (f) (f f)) (λ (g) (m (λ args (apply (g g) args)))))))
((Y (lambda (!) (lambda (n) (if (zero? n) 1 (* n (! (sub1 n))))))) 4)
; The sugar:
(define-syntax-rule
(recursive-lambda (name arg ...) body ...)
(Y (lambda (name) (lambda (arg ...) body ...))))
((recursive-lambda (! n) (if (zero? n) 1 (* n (! (sub1 n))))) 4)
; Or in tail recursive style
; (I am not sure it is realy tail recursive under Y,
; but using debug I don't see a growing stack)
((recursive-lambda (! n m) (if (zero? n) m (! (sub1 n) (* n m)))) 4 1)
Jos
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.