unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* define-syntax
@ 2008-12-09 16:51 Pach Roman (DGS-EC/ESG3)
  2008-12-09 20:20 ` define-syntax Neil Jerram
  0 siblings, 1 reply; 9+ messages in thread
From: Pach Roman (DGS-EC/ESG3) @ 2008-12-09 16:51 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 779 bytes --]

I've performed following two short tests.

test 1:
-------

(use-syntax (ice-9 syncase))

(define-syntax my-macro-1
  (syntax-rules ()
    ((_ par1 par2 par3)
     (begin
	   (string-concatenate (list par1 par2 par3))
	   ))))

(define (dummy)
  (my-macro-1 "a" "b" "c"))

(format #t "dummy => ~s\n" (procedure-source dummy))

result:
dummy => (lambda () (string-concatenate (list "a" "b" "c")))

test 2:
-------
(define-macro (my-macro-2 par1 par2 par3)
  (string-concatenate (list par1 par2 par3)))

(define (dummy)
  (my-macro-2 "a" "b" "c")
  )
(dummy)
(format #t "dummy => ~s\n" (procedure-source dummy))

result:
dummy => (lambda () "abc")


It seems to me the define-syntax is broken in version "1.8.2"
or I've done something wrong.

[-- Attachment #2: Type: text/html, Size: 3011 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2008-12-09 16:51 define-syntax Pach Roman (DGS-EC/ESG3)
@ 2008-12-09 20:20 ` Neil Jerram
  2008-12-09 20:52   ` AW: define-syntax Pach Roman (DGS-EC/ESG3)
  0 siblings, 1 reply; 9+ messages in thread
From: Neil Jerram @ 2008-12-09 20:20 UTC (permalink / raw)
  To: Pach Roman (DGS-EC/ESG3); +Cc: guile-devel

2008/12/9 Pach Roman (DGS-EC/ESG3) <Roman.Pach@de.bosch.com>:
> I've performed following two short tests.
>
> test 1:
> -------
>
> (use-syntax (ice-9 syncase))
>
> (define-syntax my-macro-1
>   (syntax-rules ()
>     ((_ par1 par2 par3)
>      (begin
>            (string-concatenate (list par1 par2 par3))
>            ))))
>
> (define (dummy)
>   (my-macro-1 "a" "b" "c"))
>
> (format #t "dummy => ~s\n" (procedure-source dummy))
>
> result:
> dummy => (lambda () (string-concatenate (list "a" "b" "c")))
>
> test 2:
> -------
> (define-macro (my-macro-2 par1 par2 par3)
>   (string-concatenate (list par1 par2 par3)))
>
> (define (dummy)
>   (my-macro-2 "a" "b" "c")
>   )
> (dummy)
> (format #t "dummy => ~s\n" (procedure-source dummy))
>
> result:
> dummy => (lambda () "abc")
>
> It seems to me the define-syntax is broken in version "1.8.2"
> or I've done something wrong.

These examples both look good to me.  Which one do you think is wrong?

It may help if I point out that define-macro usage usually involves
backquoting.  The define-macro version of your define-syntax example
would normally be:

 (define-macro (my-macro-2 par1 par2 par3)
   `(string-concatenate (list ,par1 ,par2 ,par3)))

Regards,
       Neil




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2008-12-09 20:52   ` AW: define-syntax Pach Roman (DGS-EC/ESG3)
@ 2008-12-09 21:17     ` Neil Jerram
  0 siblings, 0 replies; 9+ messages in thread
From: Neil Jerram @ 2008-12-09 21:17 UTC (permalink / raw)
  To: Pach Roman (DGS-EC/ESG3); +Cc: guile-devel

2008/12/9 Pach Roman (DGS-EC/ESG3) <Roman.Pach@de.bosch.com>:
>
> take look at the results
>
> (1) dummy => (lambda () (string-concatenate (list "a" "b" "c")))
> (2) dummy => (lambda () "abc")
>
> in the case of define-syntax (1) the string "abc" will be computed at
> the run time,
> the macro version (2) hat produced the "abc" already by reading the
> macro definition.

Agreed.

> I've expected both of the be computed during the reading of scheme
> source.

That's not what define-syntax is supposed to do, as I understand it.
If you think we've got this wrong, can you point to something specific
in R5RS?

Regards,
      Neil




^ permalink raw reply	[flat|nested] 9+ messages in thread

* define-syntax
@ 2010-06-15 20:48 Ludovic Courtès
  2010-06-15 22:06 ` define-syntax Andy Wingo
  2010-06-16  7:35 ` define-syntax Marco Maggi
  0 siblings, 2 replies; 9+ messages in thread
From: Ludovic Courtès @ 2010-06-15 20:48 UTC (permalink / raw)
  To: guile-devel

Hello,

From R6RS Section 10:

  define-syntax form  The expander expands and evaluates the
    right-hand-side expression and binds the keyword to the resulting
    transformer.

Thus I think the following should work:

  (define-syntax +
    (let ((plus +))  ;; `+' should resolve to whatever `+' is bound to
                     ;; before this definition
      (lambda (stx)
        (syntax-case stx ()
          ((_ args ...)
           (apply plus (map syntax->datum #'(args ...))))))))

But it fails when expanding the macro:

  scheme@(guile-user)> (+ 2 3)
  Throw to key `wrong-type-arg':
  ERROR: In procedure vm-debug-engine:
  ERROR: Wrong type to apply: #<syntax-transformer +>

Thoughts?

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2010-06-15 20:48 define-syntax Ludovic Courtès
@ 2010-06-15 22:06 ` Andy Wingo
  2010-06-15 22:38   ` define-syntax Ludovic Courtès
  2010-06-16  7:35 ` define-syntax Marco Maggi
  1 sibling, 1 reply; 9+ messages in thread
From: Andy Wingo @ 2010-06-15 22:06 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

Hi,

On Tue 15 Jun 2010 22:48, ludo@gnu.org (Ludovic Courtès) writes:

> From R6RS Section 10:
>
>   define-syntax form  The expander expands and evaluates the
>     right-hand-side expression and binds the keyword to the resulting
>     transformer.
>
> Thus I think the following should work:
>

An interesting issue, and an interesting example. The problem boils down
to eval-when.

$ cat > foo.scm
(define-syntax +
  (let ((plus +))  ;; `+' should resolve to whatever `+' is bound to
                   ;; before this definition
    (lambda (stx)
      (syntax-case stx ()
        ((_ args ...)
         (apply plus (map syntax->datum #'(args ...))))))))
^D
$ guile -l foo.scm -c '(begin (display (+ 1 2 3)) (newline))'
6

Indeed + does resolve to whatever + was bound to before the definition;
it's just that when you define the + macro it usually defines at
compile-time too! By compiling ahead of time and exiting we leave + in
its pristine state. See also the discussion of
eval-syntax-expanders-when in psyntax.scm or in
http://www.scheme.com/csug8/system.html#./system:s78.

Cheers,

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2010-06-15 22:06 ` define-syntax Andy Wingo
@ 2010-06-15 22:38   ` Ludovic Courtès
  0 siblings, 0 replies; 9+ messages in thread
From: Ludovic Courtès @ 2010-06-15 22:38 UTC (permalink / raw)
  To: guile-devel

Hey!

Andy Wingo <wingo@pobox.com> writes:

> Indeed + does resolve to whatever + was bound to before the definition;
> it's just that when you define the + macro it usually defines at
> compile-time too! By compiling ahead of time and exiting we leave + in
> its pristine state. See also the discussion of
> eval-syntax-expanders-when in psyntax.scm or in
> http://www.scheme.com/csug8/system.html#./system:s78.

Ooh, interesting.  Thanks for the explanation!

(I eventually found out about the problem in my constant folding macro,
where bits of ‘eval-when’ in the right place did the trick.)

Ludo’.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2010-06-15 20:48 define-syntax Ludovic Courtès
  2010-06-15 22:06 ` define-syntax Andy Wingo
@ 2010-06-16  7:35 ` Marco Maggi
  2010-06-18  8:39   ` define-syntax Andy Wingo
  1 sibling, 1 reply; 9+ messages in thread
From: Marco Maggi @ 2010-06-16  7:35 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

"Ludovic Courtès" wrote:
>  (define-syntax +
>    (let ((plus +))

I am assuming you are starting the program with:

(import (rnrs))

or you are importing at level 1 a library which exports "+";
if this is  the case, and you want  R6RS compatibility, IMHO
this should fail because  you are redefining the binding for
"+";  many R6RS  implementations agree  with this  (with the
exception of Ypsilon which has hygiene problems and must not
be taken as model).

  Notice that  bindings from  "(rnrs)" are imported  at both
levels 0 and 1[1]:

    For  the libraries  defined in  the library  report, the
    export  level   is  0  for  nearly   all  bindings.  The
    exceptions are syntax-rules, identifier-syntax, ..., and
    _ from  the (rnrs base (6)) library,  which are exported
    with  level 1, set!  from the  (rnrs base  (6)) library,
    which is exported with levels  0 and 1, and all bindings
    from  the  composite  (rnrs  (6)) library  (see  library
    chapter on “Composite library”), which are exported with
    levels 0 and 1.

  As  a side  note: the  existence  of the  binding for  the
keyword can be recorded by  SYNTAX in the right-hand side of
a DEFINE-SYNTAX, so  that the binding itself is  can be used
to refer to the context of the definition:

  (import (rnrs))

  (define ciao 123)

  (define-syntax this
    (lambda (stx)
      (syntax-case stx ()
        ((_)
         (datum->syntax #'this 'ciao)))))

  (write (this))
  (newline)

and also:

  (import (rnrs))

  (define ciao 123)

  (define-syntax this
    (let ((ctx #'this))
      (lambda (stx)
        (syntax-case stx ()
          ((_)
           (datum->syntax ctx 'ciao))))))

  (write (this))
  (newline)

for this kind  of things, I suggest taking  the behaviour of
Larceny as model: if it works with it, it will probably work
with all the other R6RS implementations.

HTH

[1] <http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-10.html#node_sec_7.2>
-- 
Marco Maggi



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2010-06-16  7:35 ` define-syntax Marco Maggi
@ 2010-06-18  8:39   ` Andy Wingo
  2010-06-18 14:41     ` define-syntax Marco Maggi
  0 siblings, 1 reply; 9+ messages in thread
From: Andy Wingo @ 2010-06-18  8:39 UTC (permalink / raw)
  To: Marco Maggi; +Cc: Ludovic Courtès, guile-devel

On Wed 16 Jun 2010 09:35, Marco Maggi <marco.maggi-ipsu@poste.it> writes:

> "Ludovic Courtès" wrote:
>>  (define-syntax +
>>    (let ((plus +))
>
> I am assuming you are starting the program with:
>
> (import (rnrs))

Probably not a good assumption to make on this list :)

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: define-syntax
  2010-06-18  8:39   ` define-syntax Andy Wingo
@ 2010-06-18 14:41     ` Marco Maggi
  0 siblings, 0 replies; 9+ messages in thread
From: Marco Maggi @ 2010-06-18 14:41 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-devel

"Andy Wingo" wrote:
> Probably not a good assumption to make on this list :)

The message from the OP started with:

> From R6RS Section 10:
>
>  define-syntax form  The expander expands and evaluates the
>    right-hand-side expression and binds the keyword to the resulting
>    transformer.
-- 
Marco Maggi



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-06-18 14:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-15 20:48 define-syntax Ludovic Courtès
2010-06-15 22:06 ` define-syntax Andy Wingo
2010-06-15 22:38   ` define-syntax Ludovic Courtès
2010-06-16  7:35 ` define-syntax Marco Maggi
2010-06-18  8:39   ` define-syntax Andy Wingo
2010-06-18 14:41     ` define-syntax Marco Maggi
  -- strict thread matches above, loose matches on Subject: below --
2008-12-09 16:51 define-syntax Pach Roman (DGS-EC/ESG3)
2008-12-09 20:20 ` define-syntax Neil Jerram
2008-12-09 20:52   ` AW: define-syntax Pach Roman (DGS-EC/ESG3)
2008-12-09 21:17     ` define-syntax Neil Jerram

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).