guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: with-syntax return error in Guile, not in Kawa or Racket


From: Jean Abou Samra
Subject: Re: with-syntax return error in Guile, not in Kawa or Racket
Date: Fri, 10 May 2024 17:24:13 +0200
User-agent: Evolution 3.52.1 (3.52.1-1.fc40)

> but there should be something different with my macro and code because if i
> put:
> 
> (define-syntax ←
>   (syntax-rules ()
>     ((← . args)
>      (<- . args))))
> 
> in my code i get again the warnings at compilation:
> 
> ;;; /Users/mattei/library-FunctProg/guile/logiki+.scm:2526:19: warning:
> possibly unbound variable `lin'
> ;;; /Users/mattei/library-FunctProg/guile/logiki+.scm:2546:18: warning:
> possibly unbound variable `col'
> ;;; /Users/mattei/library-FunctProg/guile/logiki+.scm:2554:13: warning:
> possibly unbound variable `lin-pos-epi'
> 
> and the error at runtime:
> scheme@(guile-user)> (logic-test)
> test 1
> (or (and (not a) (not b) (not c) (not d)) (and (not a) (not b) (not c) d) (and
> (not a) (not b) c (not d)) (and (not a) b (not c) d) (and (not a) b c (not d))
> (and (not a) b c d) (and a (not b) (not c) (not d)) (and a (not b) (not c) d)
> (and a (not b) c (not d)) (and c (not d))) = ice-9/boot-9.scm:1685:16: In
> procedure raise-exception:
> Unbound variable: lin
> 
> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> scheme@(guile-user) [1]> 
> 
> so strictly talking it is not the same thing as calling ← defined like this:
> 
> (define-syntax ←
>   (syntax-rules ()
>     ((← . args)
>      (<- . args))))
> 
> and not the same as calling ← defined like this:
> […]
> even if the version above of ← is the same as <- ,i just replaced at any place
> <- by ←
> 
> i have no explains. I suppose there is something hard too understand with the
> macro syntax system.


I'd need a reproducible example to debug this. I have no explanation
other than that there must be a trivial mistake somewhere in your
code or testing procedure (happens to everyone).


> i even tried this :
> 
> (define-syntax ← 
>    (syntax-rules ()
>      ((_ ( ) expr) (<- ( ) expr))
>      ((_ (var) expr) (<- (var) expr))
>      ((_ (brket-applynext container index ...) expr) (<- (brket-applynext
> container index ...) expr))
>      ((_ var expr) (<- var expr))
>      ((_ var var1 ... expr) (<- var var1 ... expr))))
> 
> to better respect the pattern of <- it fails too when call from ←


Believe me, this is a red herring.


> yes frightening... the variable just disappeared...

I wouldn't call it frightening. It's completely expected, and documented.

The hygiene algorithm is not that simple, but the intuitive high-level
view of what happens here is that during the expansion of

(let ((lin 5))
  (display (mac lin)))

the built-in let macro creates a kind of "access token" for the variable
lin, and marks the body (display (mac lin)) with that token so that it
has the variable lin in scope. This is so that forms introduced by macros
(like mac here) which happen to also use the name lin will not interfere.
Now, when you do the syntax->datum, you strip away all that information,
and with (datum->syntax #f …), you convert the symbols back to syntax,
but with zero scope information (as documented in the Guile manual).
That means they can only refer to top-level variables. So the lookup
of the local variable lin fails, but it works if lin is a global variable
(because global variables are dynamic in Scheme, cf. module-set! and all
that, unlike local variables which are static).


> in Guile the 2 examples works:
> 
> scheme@(guile-user)> (define-syntax mac
>   (lambda (sintax)
>     (syntax-case sintax ()
>       ((mac arg)
>        (datum->syntax #f (syntax->datum #'arg))))))
> scheme@(guile-user)> (define lin 6)
> scheme@(guile-user)> (display (mac lin))
> 6scheme@(guile-user)> (let ((lin 5))
>   (display (mac lin)))
> 6scheme@(guile-user)> 


Well, here, the second example runs but display 6, not 5, i.e. it looks
up the global variable defined earlier, not the local one.


Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]