unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Problem with define-macro from compiled file (Guile 1.9)
@ 2011-01-06 18:18 Patrick Bernaud
  2011-01-06 23:58 ` Ludovic Courtès
  2011-01-07  8:53 ` Hans Aberg
  0 siblings, 2 replies; 6+ messages in thread
From: Patrick Bernaud @ 2011-01-06 18:18 UTC (permalink / raw)
  To: guile-user

Hello,

I have a case where a macro is defined in a file that is then loaded
by another which makes use of the macro. And it produces a 'wrong type
to apply' error from the VM.

(Works fine with GUILE_AUTO_COMPILE=0 and compiled files removed).

For example, with the 'when' macro from the manual:

-%<---- when.scm ----%<-
(define-macro (when cond exp . rest)
  `(if ,cond
       (begin ,exp . ,rest)))
-%<---- when.scm ----%<-

-%<---- test.scm ----%<-
(load "when.scm")
(when #t (display "Launching missiles!\n"))
-%<---- test.scm ----%<-

I get:

$ guile -s test.scm
Launching missiles!
Backtrace:
In ice-9/boot-9.scm:
 170: 9 [catch #t #<catch-closure 8ebb2c0> ...]
In unknown file:
   ?: 8 [catch-closure]
In ice-9/boot-9.scm:
  62: 7 [call-with-prompt prompt0 ...]
In ice-9/eval.scm:
 389: 6 [eval # #]
In ice-9/boot-9.scm:
1863: 5 [save-module-excursion #<procedure 8f0dc90 at ice-9/boot-9.scm:1877:3 ()>]
1171: 4 [load "test.scm" #f]
In unknown file:
   ?: 3 [load-compiled/vm "/home/pat/.cache/guile/ccache/2.0-0.T-LE-4/home/pat/devel/guile/test.scm.go"]
In test.scm:
   2: 2 [#<procedure 8e835a0 ()>]
In ice-9/boot-9.scm:
 115: 1 [#<procedure 8e354d8 at ice-9/boot-9.scm:110:6 (thrown-k . args)> wrong-type-arg ...]
In unknown file:
   ?: 0 [catch-closure wrong-type-arg #f ...]

ERROR: In procedure catch-closure:
ERROR: Wrong type to apply: #<syntax-transformer when>
$

Am I doing wrong something wrong?

Regards,


Patrick



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

* Re: Problem with define-macro from compiled file (Guile 1.9)
  2011-01-06 18:18 Problem with define-macro from compiled file (Guile 1.9) Patrick Bernaud
@ 2011-01-06 23:58 ` Ludovic Courtès
  2011-01-07  8:53 ` Hans Aberg
  1 sibling, 0 replies; 6+ messages in thread
From: Ludovic Courtès @ 2011-01-06 23:58 UTC (permalink / raw)
  To: guile-user

Hi,

Patrick Bernaud <patrickb@chez.com> writes:

> -%<---- when.scm ----%<-
> (define-macro (when cond exp . rest)
>   `(if ,cond
>        (begin ,exp . ,rest)))
> -%<---- when.scm ----%<-
>
> -%<---- test.scm ----%<-
> (load "when.scm")
> (when #t (display "Launching missiles!\n"))
> -%<---- test.scm ----%<-

[...]

> ERROR: In procedure catch-closure:
> ERROR: Wrong type to apply: #<syntax-transformer when>

This is because ‘when’ wasn’t expanded in ‘test.scm’, because its
definition wasn’t visible at the time ‘test.scm’ was compiled (remember
that ‘load’ is something that happens at run time.)

Thus the compiler assumed that ‘when’ is a global variable, leading to
the error above (compiling ‘test.scm’ with ‘guile-tools compile
-Wunbound-variable’ should have given a warning.)

The solution is to get rid of ‘load’: either use ‘(include "when.scm")’,
or turn ‘when.scm’ into a module and use it in ‘test.scm’.

Thanks,
Ludo’.




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

* Re: Problem with define-macro from compiled file (Guile 1.9)
  2011-01-06 18:18 Problem with define-macro from compiled file (Guile 1.9) Patrick Bernaud
  2011-01-06 23:58 ` Ludovic Courtès
@ 2011-01-07  8:53 ` Hans Aberg
  2011-01-07 23:22   ` Andreas Rottmann
  1 sibling, 1 reply; 6+ messages in thread
From: Hans Aberg @ 2011-01-07  8:53 UTC (permalink / raw)
  To: guile-user

On 6 Jan 2011, at 19:18, Patrick Bernaud wrote:

> I have a case where a macro is defined in a file that is then loaded
> by another which makes use of the macro. And it produces a 'wrong type
> to apply' error from the VM.
>
> (Works fine with GUILE_AUTO_COMPILE=0 and compiled files removed).
>
> For example, with the 'when' macro from the manual:
>
> -%<---- when.scm ----%<-
> (define-macro (when cond exp . rest)
> `(if ,cond
>      (begin ,exp . ,rest)))
> -%<---- when.scm ----%<-
>
> -%<---- test.scm ----%<-
> (load "when.scm")
> (when #t (display "Launching missiles!\n"))
> -%<---- test.scm ----%<-

If you are writing your own macro, I find 'define-syntax' easier:

(use-syntax (ice-9 syncase))

(define-syntax when
  (syntax-rules ()
    ((when cond x ...)
      (if cond (begin x ...)))
))

(when #t (display "Launching missiles!\n"))





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

* Re: Problem with define-macro from compiled file (Guile 1.9)
  2011-01-07  8:53 ` Hans Aberg
@ 2011-01-07 23:22   ` Andreas Rottmann
  2011-01-08  9:46     ` Hans Aberg
  2011-01-08 12:00     ` Predicate of define-syntax Hans Aberg
  0 siblings, 2 replies; 6+ messages in thread
From: Andreas Rottmann @ 2011-01-07 23:22 UTC (permalink / raw)
  To: Hans Aberg; +Cc: guile-user

Hans Aberg <haberg-1@telia.com> writes:

> On 6 Jan 2011, at 19:18, Patrick Bernaud wrote:
>
>> I have a case where a macro is defined in a file that is then loaded
>> by another which makes use of the macro. And it produces a 'wrong type
>> to apply' error from the VM.
>>
>> (Works fine with GUILE_AUTO_COMPILE=0 and compiled files removed).
>>
>> For example, with the 'when' macro from the manual:
>>
>> -%<---- when.scm ----%<-
>> (define-macro (when cond exp . rest)
>> `(if ,cond
>>      (begin ,exp . ,rest)))
>> -%<---- when.scm ----%<-
>>
>> -%<---- test.scm ----%<-
>> (load "when.scm")
>> (when #t (display "Launching missiles!\n"))
>> -%<---- test.scm ----%<-
>
> If you are writing your own macro, I find 'define-syntax' easier:
>
> [...]

It's not only easier, it also not inherently broken (as `define-macro'
is).  See <http://en.wikipedia.org/wiki/Hygienic_macro> for a discussion
of the hygiene issue.

For example, the following invocation of `when' will break when
implemented with `define-macro' as above:

(let ((begin #f))
  (when #t
    (display "Launching missiles!\n")))

Regards, Rotty
-- 
Andreas Rottmann -- <http://rotty.yi.org/>



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

* Re: Problem with define-macro from compiled file (Guile 1.9)
  2011-01-07 23:22   ` Andreas Rottmann
@ 2011-01-08  9:46     ` Hans Aberg
  2011-01-08 12:00     ` Predicate of define-syntax Hans Aberg
  1 sibling, 0 replies; 6+ messages in thread
From: Hans Aberg @ 2011-01-08  9:46 UTC (permalink / raw)
  To: Andreas Rottmann; +Cc: Patrick Bernaud, guile-user

On 8 Jan 2011, at 00:22, Andreas Rottmann wrote:

>> If you are writing your own macro, I find 'define-syntax' easier:
>
> It's not only easier, it also not inherently broken (as `define-macro'
> is).  See <http://en.wikipedia.org/wiki/Hygienic_macro> for a  
> discussion
> of the hygiene issue.

This is a good discussion. I need introducing local variables in the  
macro definition, which 'define-macro' resolves. So thanks!




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

* Predicate of define-syntax
  2011-01-07 23:22   ` Andreas Rottmann
  2011-01-08  9:46     ` Hans Aberg
@ 2011-01-08 12:00     ` Hans Aberg
  1 sibling, 0 replies; 6+ messages in thread
From: Hans Aberg @ 2011-01-08 12:00 UTC (permalink / raw)
  To: Andreas Rottmann; +Cc: guile-user

On 8 Jan 2011, at 00:22, Andreas Rottmann wrote:

>> If you are writing your own macro, I find 'define-syntax' easier:
>
> It's not only easier, it also not inherently broken (as `define-macro'
> is).  See <http://en.wikipedia.org/wiki/Hygienic_macro> for a  
> discussion
> of the hygiene issue.

It seems that one cannot check that it is a macro, though. For some  
reason, they do not self-evaluate.


(macro? cond)
   ~> #t


(define-syntax def
   (syntax-rules ()
     ((def x ...)
       (define x ...))
))

(macro? def)
   ~> error





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

end of thread, other threads:[~2011-01-08 12:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-06 18:18 Problem with define-macro from compiled file (Guile 1.9) Patrick Bernaud
2011-01-06 23:58 ` Ludovic Courtès
2011-01-07  8:53 ` Hans Aberg
2011-01-07 23:22   ` Andreas Rottmann
2011-01-08  9:46     ` Hans Aberg
2011-01-08 12:00     ` Predicate of define-syntax Hans Aberg

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).