In the first method, the problem is #%app
https://docs.racket-lang.org/reference/application.html?q=%23%25app
It's hidden by the default configuration of the macro stepper. Try
changing the configuration to show it.

The problem is that the expansion is:

(substitute-term and or (and #t #f))

---> first item is a macro, so apply it

((substitute-term and or and) (substitute-term and or #t)
(substitute-term and or #f))

--> first item is not a macro (it's only something that calls a
macro), so add #%app

(#%app (substitute-term and or and) (substitute-term and or #t)
(substitute-term and or #f))

--> first item is a especial form, so analyze the subexpressions

(#%app *(substitute-term and or and)* (substitute-term and or #t)
(substitute-term and or #f))

--> first item in the second subexpression is a macro, so apply it

(#%app or (substitute-term and or #t) (substitute-term and or #f))

--> no surprises in the other subexpressiosn, I'll skip the repetitive steps

(#%app or #t #f)

--> now everything is expanded, so #%app want's to call the function
that is the first argument, but it's a macro instead of a function.
--> error



To fix it, I think it's necessary that substitute-term does more work,
and tries to replace the first expression directly instead of using
recursion.

(substitute-term and or (and #t #f))

---> first item is a macro, so apply it

(and (substitute-term and or #t) (substitute-term and or #f))

where in the first position, the or->and is replaced directly. I also
think this will be easier if you use syntax-case instead of case and
syntax-e.


 ************************

 The problem with the second method is in the line

 (datum->syntax stx (for-substitute-term (syntax->datum #'term-from)
(syntax->datum #'term-to) (syntax->datum #'body)))

This is unhygienic because in the substitution all the
marks/scopes/whatever are lost.

In this case, when #'body is n, the n is not a plain n, is an n that
has a scope that says that it's the argument of the function. When the
macro applies (syntax->datum #'body) then the result is the symbol 'n
without additional information, it's almost a like string of text.
Later when the macro calls (datum->syntax stx (...)) then the symbol
'n get the scopes from the syntax, that are probably the wrong scopes,
so the result is not the n that is the argument of the function but
another n, perhaps the toplevel n that should be defined in the
module.

This explanation is very very very confusing. I'm confused. I'm not
even sure it's 100% accurate, but the general idea is right. Anyway,
the takeaway is "Don't do it!!!!!!!!!!!!". Or to be more precise, only
try this after studding carefully about the hygiene system.

Gustavo





On Sun, Sep 11, 2016 at 12:54 AM, Vasily Rybakov <madbada...@gmail.com> wrote:
> Hi!
>
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to