unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#14203: Manual: 'my-or'; 'let' inside macros
@ 2013-04-14 16:33 Nikita Karetnikov
  2013-04-14 17:53 ` Mark H Weaver
  0 siblings, 1 reply; 2+ messages in thread
From: Nikita Karetnikov @ 2013-04-14 16:33 UTC (permalink / raw)
  To: 14203

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

I think this example [1,2]:

(define-syntax my-or
  (syntax-rules ()
    ((my-or)
     #t)
    ((my-or exp)
     exp)
    ((my-or exp rest ...)
     (let ((t exp))
       (if exp
           exp
           (my-or rest ...))))))

should look like this:

(define-syntax my-or
  (syntax-rules ()
    ((my-or)
     #t)
    ((my-or exp)
     exp)
    ((my-or exp rest ...)
     (let ((t exp))
       (if t                    ; <-
           t
           (my-or rest ...))))))

Otherwise, what's the rationale behind 'let'?

AFAICT, it's described here [3], but Guile is not affected, right?  So
the following works as well:

(define-syntax my-or
  (syntax-rules ()
    ((my-or)
     #t)
    ((my-or exp)
     exp)
    ((my-or exp rest ...)
     (if exp
         exp
         (my-or rest ...)))))

Note that 'my-or' is used in several places (e.g., [4]) and it's
necessary to change them all.  Also, there are other macros that use
'let' (e.g., 'cond1').

[1] https://gnu.org/software/guile/manual/guile.html#Defining-Macros
[2] https://gnu.org/software/guile/manual/guile.html#Hygiene
[3] http://stackoverflow.com/a/3215238
[4] https://gnu.org/software/guile/manual/guile.html#Syntax-Case

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

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

* bug#14203: Manual: 'my-or'; 'let' inside macros
  2013-04-14 16:33 bug#14203: Manual: 'my-or'; 'let' inside macros Nikita Karetnikov
@ 2013-04-14 17:53 ` Mark H Weaver
  0 siblings, 0 replies; 2+ messages in thread
From: Mark H Weaver @ 2013-04-14 17:53 UTC (permalink / raw)
  To: Nikita Karetnikov; +Cc: 14203-done

Nikita Karetnikov <nikita@karetnikov.org> writes:

> I think this example [1,2]:
>
> (define-syntax my-or
>   (syntax-rules ()
>     ((my-or)
>      #t)
>     ((my-or exp)
>      exp)
>     ((my-or exp rest ...)
>      (let ((t exp))
>        (if exp
>            exp
>            (my-or rest ...))))))
>
> should look like this:
>
> (define-syntax my-or
>   (syntax-rules ()
>     ((my-or)
>      #t)
>     ((my-or exp)
>      exp)
>     ((my-or exp rest ...)
>      (let ((t exp))
>        (if t                    ; <-
>            t
>            (my-or rest ...))))))

Indeed, thanks!  I've pushed this fix to the stable-2.0 branch, and am
closing this bug.

Answers to your other questions follow.

> AFAICT, it's described here [3], but Guile is not affected, right?
> [3] http://stackoverflow.com/a/3215238

That post gives an example that looks superficially similar, but is
actually entirely different:

  (define remove!
    (let ((null? null?)
          (cdr cdr)
          (eq? eq?))
      (lambda ... function that uses null?, cdr, eq? ...)

Indeed, this is not necessary in Guile due to its module system.

> So the following works as well:
>
> (define-syntax my-or
>   (syntax-rules ()
>     ((my-or)
>      #t)
>     ((my-or exp)
>      exp)
>     ((my-or exp rest ...)
>      (if exp
>          exp
>          (my-or rest ...)))))

The above definition has a problem: it would result in 'exp' being
evaluated more than once, unless it returns false.

For example, if you used your proposed definition above,
(my-or (read) 5) would expand to:

  (if (read)
      (read)
      5)

Which would obviously not do what you expect from 'or'.  Instead, we
want:

  (let ((t (read)))
    (if t
        t
        5))

> Note that 'my-or' is used in several places (e.g., [4]) and it's
> necessary to change them all.
> [4] https://gnu.org/software/guile/manual/guile.html#Syntax-Case

Unless I'm mistaken, the Syntax-case section uses 'my-or', but does not
define it, so I don't think anything needs to be fixed there.  Right?

     Thanks,
       Mark





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

end of thread, other threads:[~2013-04-14 17:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-14 16:33 bug#14203: Manual: 'my-or'; 'let' inside macros Nikita Karetnikov
2013-04-14 17:53 ` Mark H Weaver

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