From: Dave Goel <deego3@gmail.com>
To: Michael Heerdegen <michael_heerdegen@web.de>,
Dave Goel <deego3@gmail.com>
Cc: "Gerd Möllmann" <gerd.moellmann@gmail.com>, 66940@debbugs.gnu.org
Subject: bug#66940: Dynamic scoping is all weird now?
Date: Mon, 6 Nov 2023 01:57:05 -0500 [thread overview]
Message-ID: <CAOCW0Dgznbj9wH_rSZAB+XSgiUBds+0VdoVBU=rwuW+yapUhzg@mail.gmail.com> (raw)
In-Reply-To: <87ttpzhcmd.fsf@web.de>
[-- Attachment #1: Type: text/plain, Size: 3181 bytes --]
Michael,
Many thanks for that excellent explanation.
One more question -
With this, first, let's simplify our example as follows.
Let's eval this at the start:
(setq lexical-binding nil)
Eval it, so we are operating in the old world of dynamic scoping.
(let ((ii 1))
(defmacro mac ()
`(print ,ii)
)
(mac))
As you said, with lexical-binding nil, the ii = 1 had no effect above. It
will use the ii from the runtime environment.
So, let's try this -
(setq ii 3)
(let ((ii 4))
(mac))
It evals to 3, not 4. Per our understanding of dynamic scoping, we would
have expected it use the innermost ii? Why does it use the global ii?
On Sun, Nov 5, 2023 at 8:52 PM Michael Heerdegen <michael_heerdegen@web.de>
wrote:
> Dave Goel <deego3@gmail.com> writes:
>
> > (progn
> > (setq lexical-binding nil)
> >
> > (dotimes (ii 10)
> > (defmacro mac ()
> > `(message "%S" ,ii)
> > )
> > (let
> > ((old_ii ii))
> > (setq ii 33)
> > (mac)
> > (setq ii old_ii)
> > )))
>
> This code has two problems:
>
> (1) Setting `lexical-binding' in the middle of an evaluation does not
> work as you think. Either you eval en expression (like the above) using
> lexical binding or dynamical binding. If you change `lexical-binding'
> in the middle you might get unexpected behavior. Please set the binding
> mode only in the file header, not in the code. In the extremely rare
> cases where you really must evaluate an expression using the other
> binding mode use `eval' with an appropriate second argument.
>
> (2) Macro expansion does not work as you think. Most of the time macros
> are expanded _once_ in the complete expression and then the result is
> evaluated. When you redefine a macro in the middle of evaluating code
> using it, most of the time this will not have an effect because the
> macro had already been expanded in the following code.
>
>
> > You eval this code once. It works.
> > You eval this again. It works.
> > The third time, though, it lands you in the debugger. The very same
> > code. Why the third time? And, why the debugger? ii is well set every
> > time it is used.
>
> It's not surprising.
>
> The first time you start evaluating the expression using lexical
> binding. Because your code sets lexical-binding to nil, the second time
> you actually use dynamical binding completely.
>
> The third run fails because when `mac' is expanded a variable `ii' is
> undefined on top-level, so macroexpansion fails.
>
> The second run is different: because the first run had used
> lexical-binding (more or less completely, since you started the
> interpreter using lexical-binding mode), the macroexpander of `mac' is
> actually a closure that inherited the value of `ii' from the last
> iteration of the first run. Because of that the second run succeeds.
>
>
> The second run redefines `mac' so that its reference to `ii' now means
> the dynamical global variable, which doesn't exist.
>
> So, I think everything indeed perfectly works as expected here.
>
>
> Michael.
>
[-- Attachment #2: Type: text/html, Size: 4004 bytes --]
next prev parent reply other threads:[~2023-11-06 6:57 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-05 4:06 bug#66940: Dynamic scoping is all weird now? Dave Goel
2023-11-05 4:09 ` Dave Goel
2023-11-05 6:04 ` Gerd Möllmann
2023-11-05 6:58 ` Dave Goel
2023-11-05 7:25 ` Gerd Möllmann
2023-11-05 8:12 ` Dave Goel
2023-11-05 8:39 ` Gerd Möllmann
2023-11-05 19:07 ` Dave Goel
2023-11-05 20:21 ` Dave Goel
2023-11-06 1:52 ` Michael Heerdegen
2023-11-06 6:25 ` Gerd Möllmann
2023-11-06 16:49 ` Drew Adams
2023-11-06 6:57 ` Dave Goel [this message]
2023-11-07 5:59 ` Michael Heerdegen
2023-11-08 2:49 ` Dave Goel
2023-11-08 3:08 ` Michael Heerdegen
2023-11-08 3:21 ` Dave Goel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAOCW0Dgznbj9wH_rSZAB+XSgiUBds+0VdoVBU=rwuW+yapUhzg@mail.gmail.com' \
--to=deego3@gmail.com \
--cc=66940@debbugs.gnu.org \
--cc=gerd.moellmann@gmail.com \
--cc=michael_heerdegen@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
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).