From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Robert Thorpe Newsgroups: gmane.emacs.help Subject: Re: Real-life examples of lexical binding in Emacs Lisp Date: Wed, 17 Jun 2015 23:07:33 +0100 Message-ID: <87oakedm56.fsf@robertthorpeconsulting.com> References: <871thaulbv.fsf@debian.uxu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1434578892 30615 80.91.229.3 (17 Jun 2015 22:08:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Jun 2015 22:08:12 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Emanuel Berg Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jun 18 00:07:59 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z5LV0-0003KR-0j for geh-help-gnu-emacs@m.gmane.org; Thu, 18 Jun 2015 00:07:58 +0200 Original-Received: from localhost ([::1]:49480 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5LUz-0004oN-9h for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jun 2015 18:07:57 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47057) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5LUh-0004oA-LF for help-gnu-emacs@gnu.org; Wed, 17 Jun 2015 18:07:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z5LUe-0001cO-Fo for help-gnu-emacs@gnu.org; Wed, 17 Jun 2015 18:07:39 -0400 Original-Received: from outbound-smtp04.blacknight.com ([81.17.249.35]:58648) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5LUe-0001bW-9j for help-gnu-emacs@gnu.org; Wed, 17 Jun 2015 18:07:36 -0400 Original-Received: from mail.blacknight.com (pemlinmail01.blacknight.ie [81.17.254.10]) by outbound-smtp04.blacknight.com (Postfix) with ESMTPS id 1BBA898F9B for ; Wed, 17 Jun 2015 22:07:35 +0000 (UTC) Original-Received: (qmail 8340 invoked from network); 17 Jun 2015 22:07:34 -0000 Original-Received: from unknown (HELO RTLaptop) (rt@robertthorpeconsulting.com@[109.79.181.203]) by 81.17.254.9 with ESMTPSA (DHE-RSA-AES128-SHA encrypted, authenticated); 17 Jun 2015 22:07:34 -0000 In-Reply-To: <871thaulbv.fsf@debian.uxu> (message from Emanuel Berg on Wed, 17 Jun 2015 22:33:08 +0200) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 81.17.249.35 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105023 Archived-At: Emanuel Berg writes: > I take it "lexical" refers to you can make it out by > looking at the code. Yes. > "Dynamic" refers to it depends on the code and the > program state in execution. Yes. > I agree those terms are confusing. To me, it sounds > like they refer to call-by-value vs. > call-by-reference, which isn't so. > > I'd call it "normal scope" vs. > "stacked scope", perhaps. Both cases are usually implemented using stacks. Thinking about this helps understanding. In the lexical scope case we can think of one huge stack. Each entry in the stack can contain many elements. When execution enters a "let" form a new entry is created on the stack and the variables declared there are stored in that entry. They "shadow" other variables with the same name - they take precedence over them. When execution exits the let form that stack entry is deleted. This is usually how lexical scope is implement too. For dynamic scope we can think of each variable name as being associated with it's own stack. When execution reaches a let form a new value is pushed onto the stack for the relevant name. When the let form exits it's deleted from the top of the stack. Code always uses the top-of-stack value, so earlier let forms are shadowed by later ones. This is one way of implementing dynamic scope too, it's "shallow binding". I think it's how Elisp implements it. Dynamic scope was an accident of how Lisp was first written. See: http://www-formal.stanford.edu/jmc/history/lisp/node4.html The question of "extent" rather than "scope" brings up more problems. So does "deep" vs "shallow" binding with dynamic scope. BR, Robert Thorpe