unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* `pp-eval-expression' behaves strangely under lexical binding
@ 2016-04-17 13:43 Marcin Borkowski
  2016-04-17 14:33 ` Marcin Borkowski
  0 siblings, 1 reply; 6+ messages in thread
From: Marcin Borkowski @ 2016-04-17 13:43 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

put this in an empty buffer, call `normal-mode' (so that
`lexical-binding' is set to t) and evaluate:

--8<---------------cut here---------------start------------->8---
;;; -*- lexical-binding: t; -*-
(setq spam 'eggs)
(let ((spam 'bacon))
  (pp-eval-expression 'spam)
  (message "%s" spam))
--8<---------------cut here---------------end--------------->8---

Then look at *Messages*: "eggs" is printed, then "bacon".

I wanted to use `pp-eval-expression' when debugging (basically as
a pretty-printing variant of `message'), and this behavior hit me.  Why
does this behave in this way, and is it a feature or a bug?

Best

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: `pp-eval-expression' behaves strangely under lexical binding
  2016-04-17 13:43 `pp-eval-expression' behaves strangely under lexical binding Marcin Borkowski
@ 2016-04-17 14:33 ` Marcin Borkowski
  2016-04-17 16:15   ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Marcin Borkowski @ 2016-04-17 14:33 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list


On 2016-04-17, at 13:43, Marcin Borkowski <mbork@mbork.pl> wrote:

> Hi all,
>
> put this in an empty buffer, call `normal-mode' (so that
> `lexical-binding' is set to t) and evaluate:
>
> --8<---------------cut here---------------start------------->8---
> ;;; -*- lexical-binding: t; -*-
> (setq spam 'eggs)
> (let ((spam 'bacon))
>   (pp-eval-expression 'spam)
>   (message "%s" spam))
> --8<---------------cut here---------------end--------------->8---
>
> Then look at *Messages*: "eggs" is printed, then "bacon".
>
> I wanted to use `pp-eval-expression' when debugging (basically as
> a pretty-printing variant of `message'), and this behavior hit me.  Why
> does this behave in this way, and is it a feature or a bug?

OK, never mind: `pp-eval-expression' is defined in Lisp, so it cannot
work any other way.

But isn't my question still valid for `symbol-value'?

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: `pp-eval-expression' behaves strangely under lexical binding
  2016-04-17 14:33 ` Marcin Borkowski
@ 2016-04-17 16:15   ` Stefan Monnier
  2016-04-17 17:42     ` Marcin Borkowski
  2016-04-18 15:07     ` Andy Moreton
  0 siblings, 2 replies; 6+ messages in thread
From: Stefan Monnier @ 2016-04-17 16:15 UTC (permalink / raw)
  To: help-gnu-emacs

> But isn't my question still valid for `symbol-value'?

No, whether it's implemented in C or in Lisp is irrelevant.  As soon as
you put a symbol into a `quote`, it can't be a reference to a lexical
variable any more because the compiler will treat it as data and data
cannot refer to lexical variables, only to other data.

Dynamically scoped variables are data, on the other hand (they're just
a particular field of a symbol object, which is a kind of "struct").


        Stefan




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

* Re: `pp-eval-expression' behaves strangely under lexical binding
  2016-04-17 16:15   ` Stefan Monnier
@ 2016-04-17 17:42     ` Marcin Borkowski
  2016-04-18 15:07     ` Andy Moreton
  1 sibling, 0 replies; 6+ messages in thread
From: Marcin Borkowski @ 2016-04-17 17:42 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2016-04-17, at 16:15, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> But isn't my question still valid for `symbol-value'?
>
> No, whether it's implemented in C or in Lisp is irrelevant.  As soon as
> you put a symbol into a `quote`, it can't be a reference to a lexical
> variable any more because the compiler will treat it as data and data
> cannot refer to lexical variables, only to other data.
>
> Dynamically scoped variables are data, on the other hand (they're just
> a particular field of a symbol object, which is a kind of "struct").

Stefan, thanks for your answer.  I'm not entirely sure I understand it,
but I'm going to give it some thinking, and if it is still unclear, I'll
get back here.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: `pp-eval-expression' behaves strangely under lexical binding
  2016-04-17 16:15   ` Stefan Monnier
  2016-04-17 17:42     ` Marcin Borkowski
@ 2016-04-18 15:07     ` Andy Moreton
  2016-04-18 15:58       ` Stefan Monnier
  1 sibling, 1 reply; 6+ messages in thread
From: Andy Moreton @ 2016-04-18 15:07 UTC (permalink / raw)
  To: help-gnu-emacs

On Sun 17 Apr 2016, Stefan Monnier wrote:

>> But isn't my question still valid for `symbol-value'?
>
> No, whether it's implemented in C or in Lisp is irrelevant.  As soon as
> you put a symbol into a `quote`, it can't be a reference to a lexical
> variable any more because the compiler will treat it as data and data
> cannot refer to lexical variables, only to other data.
>
> Dynamically scoped variables are data, on the other hand (they're just
> a particular field of a symbol object, which is a kind of "struct").

Hi Stefan,

This short description is very helpful. Could you add some similar wording
to the elisp manual to help clarify usage of lexical binding ?

Thanks,

    AndyM




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

* Re: `pp-eval-expression' behaves strangely under lexical binding
  2016-04-18 15:07     ` Andy Moreton
@ 2016-04-18 15:58       ` Stefan Monnier
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2016-04-18 15:58 UTC (permalink / raw)
  To: help-gnu-emacs

>>> But isn't my question still valid for `symbol-value'?
>> No, whether it's implemented in C or in Lisp is irrelevant.  As soon as
>> you put a symbol into a `quote`, it can't be a reference to a lexical
>> variable any more because the compiler will treat it as data and data
>> cannot refer to lexical variables, only to other data.
>> Dynamically scoped variables are data, on the other hand (they're just
>> a particular field of a symbol object, which is a kind of "struct").
> This short description is very helpful. Could you add some similar wording
> to the elisp manual to help clarify usage of lexical binding ?

I don't really see how/where to put it in the manual, sadly.
If you give me a sample patch, I promise I'll try to improve it ;-)


        Stefan


PS: Another way to look at those issues is from the "α-renaming" point
of view.  A lexical variable's name should have no impact on the code,
in the sense that the compiler should be able to rename it or get rid of
the name (and indeed Emacs's Elisp compiler gets rid of the lexical
variables's names).  So the compiler can take

    (let ((a 1))
      (foo 'a (+ a 1)))

and treat it as if you had written

    (let ((b 1))
      (foo 'a (+ b 1)))

or even

    (let ((<noname> 1))
      (foo 'a (+ <the-last-var-defined> 1)))

But clearly, it can treat it as if you had written

    (let ((b 1))
      (foo 'b (+ b 1)))

[ Unless maybe the compiler is being told that `foo` takes
  a variable-identifier as first argument.  ]

and even less

    (let ((<noname> 1))
      (foo <reference-to-the-last-var-defined> (+ <the-last-var-defined> 1)))

since `foo` now wouldn't even receive a *symbol* any more but some other
kind of object (lexical variables, at run-time, aren't referenced via
symbols at all).


        Stefan




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

end of thread, other threads:[~2016-04-18 15:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-17 13:43 `pp-eval-expression' behaves strangely under lexical binding Marcin Borkowski
2016-04-17 14:33 ` Marcin Borkowski
2016-04-17 16:15   ` Stefan Monnier
2016-04-17 17:42     ` Marcin Borkowski
2016-04-18 15:07     ` Andy Moreton
2016-04-18 15:58       ` Stefan Monnier

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