How to set procedure name in macros

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to set procedure name in macros

Duncan Mak
Hello all,

Happy new year!

In my DEFINE-FOO macro and I noticed that I can't control the name of
the lambda in the syntax expansion.

Here's a test case -

(define-syntax define-a
  (lambda (stx)
    (syntax-case stx ()
      ((_ name) #`(define name (lambda () #f))))))

(define-syntax define-bar
  (lambda (stx)
    (syntax-case stx ()
      ((_ name) #`(define name (lambda () name: 'bar #f))))))

(define-syntax define-c
  (lambda (stx)
    (syntax-case stx ()
      ((_ name) #`(define name (letrec ((foo (lambda () name: name
#f))) foo))))))

(define-syntax define-d
  (lambda (stx)
    (syntax-case stx ()
      ((_ name) #`(define name (letrec ((foo (lambda () #f)))
(set-procedure-property! foo 'name name) foo))))))

(define-a a)
(format #t "Should say a: ~A~%" (procedure-property a 'name))
(define-bar b)
(format #t "Should say bar: ~A~%" (procedure-property b 'name))
(define-c c)
(format #t "Should say c: ~A~%" (procedure-property c 'name))
(define-d d)
(format #t "Should say d: ~A~%" (procedure-property d 'name))

The output looks like this:

Should say a: a
Should say bar: b
Should say c: foo
Should say d: foo

I'm particularly interested in the C or D cases.

Thanks!


--
Duncan.
Reply | Threaded
Open this post in threaded view
|

Re: How to set procedure name in macros

Per Bothner


On 01/01/2017 02:59 PM, Duncan Mak wrote:
> Hello all,
>
> Happy new year!
>
> In my DEFINE-FOO macro and I noticed that I can't control the name of
> the lambda in the syntax expansion.

This works:

(define-syntax define-d
   (lambda (stx)
     (syntax-case stx ()
       ((_ n) #`(define n (letrec ((foo (lambda () #f)))
                                  (set-procedure-property! foo 'name 'n)
                                  foo))))))

First, you needs to quite the property value.
Secondly, you should not use the same identifier for the syntax parameter
as the name" keyword.

I'm not clear why the following doesn't work - I'm looking into it.

(define-syntax define-c
   (lambda (stx)
     (syntax-case stx ()
       ((_ n) #`(define n (letrec ((foo (lambda () name: 'n
                                                #f)))
                            foo))))))

--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: How to set procedure name in macros

Duncan Mak
Hello Per,

I'm running the Kawa 2.2 release jars and define-d didn't work for me.
It still says 'foo'.

Perhaps it's a fix that hasn't made it to a released build?

Looking forward to Kawa 2.3 ;-)



Duncan.

On Sun, Jan 1, 2017 at 7:37 PM, Per Bothner <[hidden email]> wrote:

>
>
> On 01/01/2017 02:59 PM, Duncan Mak wrote:
>>
>> Hello all,
>>
>> Happy new year!
>>
>> In my DEFINE-FOO macro and I noticed that I can't control the name of
>> the lambda in the syntax expansion.
>
>
> This works:
>
> (define-syntax define-d
>   (lambda (stx)
>     (syntax-case stx ()
>       ((_ n) #`(define n (letrec ((foo (lambda () #f)))
>                                  (set-procedure-property! foo 'name 'n)
>                                  foo))))))
>
> First, you needs to quite the property value.
> Secondly, you should not use the same identifier for the syntax parameter
> as the name" keyword.
>
> I'm not clear why the following doesn't work - I'm looking into it.
>
> (define-syntax define-c
>   (lambda (stx)
>     (syntax-case stx ()
>       ((_ n) #`(define n (letrec ((foo (lambda () name: 'n
>                                                #f)))
>                            foo))))))
>
> --
>         --Per Bothner
> [hidden email]   http://per.bothner.com/



--
Duncan.
Reply | Threaded
Open this post in threaded view
|

Re: How to set procedure name in macros

Duncan Mak
Oh sorry, I sent prematurely, define-d does work in Kawa 2.2.

Thanks!

On Sun, Jan 1, 2017 at 9:49 PM, Duncan Mak <[hidden email]> wrote:

> Hello Per,
>
> I'm running the Kawa 2.2 release jars and define-d didn't work for me.
> It still says 'foo'.
>
> Perhaps it's a fix that hasn't made it to a released build?
>
> Looking forward to Kawa 2.3 ;-)
>
>
>
> Duncan.
>
> On Sun, Jan 1, 2017 at 7:37 PM, Per Bothner <[hidden email]> wrote:
>>
>>
>> On 01/01/2017 02:59 PM, Duncan Mak wrote:
>>>
>>> Hello all,
>>>
>>> Happy new year!
>>>
>>> In my DEFINE-FOO macro and I noticed that I can't control the name of
>>> the lambda in the syntax expansion.
>>
>>
>> This works:
>>
>> (define-syntax define-d
>>   (lambda (stx)
>>     (syntax-case stx ()
>>       ((_ n) #`(define n (letrec ((foo (lambda () #f)))
>>                                  (set-procedure-property! foo 'name 'n)
>>                                  foo))))))
>>
>> First, you needs to quite the property value.
>> Secondly, you should not use the same identifier for the syntax parameter
>> as the name" keyword.
>>
>> I'm not clear why the following doesn't work - I'm looking into it.
>>
>> (define-syntax define-c
>>   (lambda (stx)
>>     (syntax-case stx ()
>>       ((_ n) #`(define n (letrec ((foo (lambda () name: 'n
>>                                                #f)))
>>                            foo))))))
>>
>> --
>>         --Per Bothner
>> [hidden email]   http://per.bothner.com/
>
>
>
> --
> Duncan.



--
Duncan.
Reply | Threaded
Open this post in threaded view
|

Re: How to set procedure name in macros

Per Bothner
In reply to this post by Duncan Mak
I checked in a fix for the following.
I also checked in a testcase based on yours in testsuite/reflect-fname.scm.  Thanks!

On 01/01/2017 02:59 PM, Duncan Mak wrote:

> Hello all,
>
> Happy new year!
>
> In my DEFINE-FOO macro and I noticed that I can't control the name of
> the lambda in the syntax expansion.
>
> Here's a test case -
>
> (define-syntax define-a
>   (lambda (stx)
>     (syntax-case stx ()
>       ((_ name) #`(define name (lambda () #f))))))
>
> (define-syntax define-bar
>   (lambda (stx)
>     (syntax-case stx ()
>       ((_ name) #`(define name (lambda () name: 'bar #f))))))
>
> (define-syntax define-c
>   (lambda (stx)
>     (syntax-case stx ()
>       ((_ name) #`(define name (letrec ((foo (lambda () name: name
> #f))) foo))))))
>
> (define-syntax define-d
>   (lambda (stx)
>     (syntax-case stx ()
>       ((_ name) #`(define name (letrec ((foo (lambda () #f)))
> (set-procedure-property! foo 'name name) foo))))))
>
> (define-a a)
> (format #t "Should say a: ~A~%" (procedure-property a 'name))
> (define-bar b)
> (format #t "Should say bar: ~A~%" (procedure-property b 'name))
> (define-c c)
> (format #t "Should say c: ~A~%" (procedure-property c 'name))
> (define-d d)
> (format #t "Should say d: ~A~%" (procedure-property d 'name))
>
> The output looks like this:
>
> Should say a: a
> Should say bar: b
> Should say c: foo
> Should say d: foo
>
> I'm particularly interested in the C or D cases.
>
> Thanks!
>
>

--
        --Per Bothner
[hidden email]   http://per.bothner.com/