Path: utzoo!attcan!uunet!mcvax!ukc!etive!aiva!jeff From: jeff@aiva.ed.ac.uk (Jeff Dalton) Newsgroups: comp.lang.lisp Subject: Re: What's the value of lexical scoping? Keywords: Scope, Binding, Locals, Break, Future of LISP Message-ID: <478@aiva.ed.ac.uk> Date: 4 Jul 88 19:16:23 GMT References: <24508@ucbvax.BERKELEY.EDU> <515@dcl-csvax.comp.lancs.ac.uk> <452@aiva.ed.ac.uk> <519@dcl-csvax.comp.lancs.ac.uk> Reply-To: jeff@uk.ac.ed.aiva (Jeff Dalton) Organization: Dept. of AI, Univ. of Edinburgh, UK Lines: 42 In article <519@dcl-csvax.comp.lancs.ac.uk> simon@comp.lancs.ac.uk (Simon Brooke) writes: >Things are bubbling! good. Let's look at some of the arguments that have >been advanced. Firstly, the objection to dynamic binding that's most >commonly advanced: namely, that you fall down holes when you confuse >locals with globals. [...] If, in a lexically scoped lisp, you refer >to a global variable thinking it's a local, or vice-versa, you'll still >fall down a hole. Lexical scoping does not obviate the need for good >software engineering practice - namely, in this case a *naming scheme*. There are many cases where both lexical and dynamic binding produce the same result. If you stick to these cases, problems with dynamic binding will, of course, not appear. The problem with dynamic binding is not so much that (incorrect) references to (supposedly) local variables might refer to a global instead -- that is clearly a problem in any language that has global varibales -- but that there is no way to have a local variable whose value is not visible everywhere. One cannot determine by local inspection what references to a variable exist: any function called might refer to it. *All* variables are globally visible, not just the ones meant to be global. A naming scheme can handle this problem, but bugs are much harder to localize when it breaks down. Suppose F has a local M, G has a local N, F calls G, and the author of G mistakenly typed M in one place instead of N. Neither N nor M were meant to be global, so a naming scheme for globals would have helped. A naming scheme that forbid local variables names such as N and M would not be acceptable. And by "local" here I should really say something like "dynamic variables meant to have only local (i.e. lexically valid) references". In most code, dynamic scope is needed in a minority of cases and all other cases that turn out to refer to the dynamic binding of a variable will be bugs. This suggests that some explicit step be required to get dynamic scope and that lexical scope be the default. And so it is a Good Thing that Common Lisp (and the varieties of Scheme that provide dynamic scope) require such explicit steps and a Bad Thing when a Lisp provides only dynamic variables. Jeff Dalton, JANET: J.Dalton@uk.ac.ed AI Applications Institute, ARPA: J.Dalton%uk.ac.ed@nss.cs.ucl.ac.uk Edinburgh University. UUCP: ...!ukc!ed.ac.uk!J.Dalton