all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Understanding macroexp.el
@ 2015-11-18 10:32 Michael Heerdegen
  2015-11-18 11:37 ` Andreas Schwab
  2015-11-18 13:40 ` Stefan Monnier
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Heerdegen @ 2015-11-18 10:32 UTC (permalink / raw)
  To: Emacs Development; +Cc: Stefan Monnier

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

Hello,

yesterday I tried to understand the helpers in macroexp.el.  I have a
question:

Would it make sense to install the following:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: macroexp.patch --]
[-- Type: text/x-diff, Size: 740 bytes --]

*** /tmp/ediff139941AX	2015-11-18 11:12:07.788012282 +0100
--- /home/micha/software/emacs/lisp/emacs-lisp/macroexp.el	2015-11-18 10:47:41.041352909 +0100
***************
*** 331,337 ****
    (cond
     ((null bindings) exp)
     ((eq 'let* (car-safe exp)) `(let* (,@bindings ,@(cadr exp)) ,@(cddr exp)))
!    (t `(let* ,bindings ,exp))))
  
  (defun macroexp-if (test then else)
    "Return an expression equivalent to `(if ,TEST ,THEN ,ELSE)."
--- 331,337 ----
    (cond
     ((null bindings) exp)
     ((eq 'let* (car-safe exp)) `(let* (,@bindings ,@(cadr exp)) ,@(cddr exp)))
!    (t `(let* ,bindings ,@(macroexp-unprogn exp)))))
  
  (defun macroexp-if (test then else)
    "Return an expression equivalent to `(if ,TEST ,THEN ,ELSE)."

[-- Attachment #3: Type: text/plain, Size: 479 bytes --]


Here is an example where it matters:

(defmacro test (exp)
  (macroexp-let2 nil res exp
    `(progn
       (f ,res)
       (g ,res))))

(note that the `progn' is mandatory here.)  Without the patch:

(macroexpand '(test (+ 1 2)))
==>
(let*
    ((#:res #1=(+ 1 2)))
  (progn
    (f #1#)
    (g #1#)))


After patch:

(macroexpand '(test (+ 1 2)))
==>
(let*
    ((#:res #1=(+ 1 2)))
  (f #1#)
  (g #1#))


(And is it currently allowed to push this to master?)


Thanks,

Michael.

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

* Re: Understanding macroexp.el
  2015-11-18 10:32 Understanding macroexp.el Michael Heerdegen
@ 2015-11-18 11:37 ` Andreas Schwab
  2015-11-18 12:03   ` Michael Heerdegen
  2015-11-18 13:40 ` Stefan Monnier
  1 sibling, 1 reply; 5+ messages in thread
From: Andreas Schwab @ 2015-11-18 11:37 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Stefan Monnier, Emacs Development

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Here is an example where it matters:
>
> (defmacro test (exp)
>   (macroexp-let2 nil res exp
>     `(progn
>        (f ,res)
>        (g ,res))))
>
> (note that the `progn' is mandatory here.)  Without the patch:
>
> (macroexpand '(test (+ 1 2)))
> ==>
> (let*
>     ((#:res #1=(+ 1 2)))
>   (progn
>     (f #1#)
>     (g #1#)))
>
>
> After patch:
>
> (macroexpand '(test (+ 1 2)))
> ==>
> (let*
>     ((#:res #1=(+ 1 2)))
>   (f #1#)
>   (g #1#))

For byte-compiled code there should be no difference between the two
expansions.

Andreas.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."



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

* Re: Understanding macroexp.el
  2015-11-18 11:37 ` Andreas Schwab
@ 2015-11-18 12:03   ` Michael Heerdegen
  2015-11-18 13:36     ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Heerdegen @ 2015-11-18 12:03 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Stefan Monnier, Emacs Development

Andreas Schwab <schwab@suse.de> writes:

> For byte-compiled code there should be no difference between the two
> expansions.

What's the use of `macroexp-unprogn' then at all?  Or does it make a
difference in other cases?


Michael.



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

* Re: Understanding macroexp.el
  2015-11-18 12:03   ` Michael Heerdegen
@ 2015-11-18 13:36     ` Stefan Monnier
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2015-11-18 13:36 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Andreas Schwab, Emacs Development

>> For byte-compiled code there should be no difference between the two
>> expansions.
> What's the use of `macroexp-unprogn' then at all?

It's when you care to generate slightly cleaner code it's easy to do so.
It usually doesn't really matter much.

> Or does it make a difference in other cases?

I can't remember exactly what motivated me to introduce
macroexp-unprogn, but I don't think it was a strong need.
I can retroactively come up with scenarios where it could make
a difference, but they all seem reasonably infrequent.
E.g. (+ 1 2) will be optimized to 3, but (+ 1 (progn 2)) won't.


        Stefan



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

* Re: Understanding macroexp.el
  2015-11-18 10:32 Understanding macroexp.el Michael Heerdegen
  2015-11-18 11:37 ` Andreas Schwab
@ 2015-11-18 13:40 ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2015-11-18 13:40 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Emacs Development

> Would it make sense to install the following:
>
> *** /tmp/ediff139941AX	2015-11-18 11:12:07.788012282 +0100
> --- /home/micha/software/emacs/lisp/emacs-lisp/macroexp.el	2015-11-18 10:47:41.041352909 +0100
> ***************
> *** 331,337 ****
>     (cond
>      ((null bindings) exp)
>      ((eq 'let* (car-safe exp)) `(let* (,@bindings ,@(cadr exp)) ,@(cddr exp)))
> !    (t `(let* ,bindings ,exp))))
>
>   (defun macroexp-if (test then else)
>     "Return an expression equivalent to `(if ,TEST ,THEN ,ELSE)."
> --- 331,337 ----
>     (cond
>      ((null bindings) exp)
>      ((eq 'let* (car-safe exp)) `(let* (,@bindings ,@(cadr exp)) ,@(cddr exp)))
> !    (t `(let* ,bindings ,@(macroexp-unprogn exp)))))
>
>   (defun macroexp-if (test then else)
>     "Return an expression equivalent to `(if ,TEST ,THEN ,ELSE)."

That'd be fine, I think, yes.  It probably won't make much difference,
if any, tho.


        Stefan



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

end of thread, other threads:[~2015-11-18 13:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-18 10:32 Understanding macroexp.el Michael Heerdegen
2015-11-18 11:37 ` Andreas Schwab
2015-11-18 12:03   ` Michael Heerdegen
2015-11-18 13:36     ` Stefan Monnier
2015-11-18 13:40 ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.