From: Nick Mudge
Subject: What Does Lexical Mean?
Date: 
Message-ID: <1184053429.054344.110840@j4g2000prf.googlegroups.com>
I've been reading Practical Common Lisp (http://www.gigamonkeys.com/
book/) and (http://www.paulgraham.com/acl.html), and in these books
the authors refer to lexical scope, lexical variables, lexical
environment.

I've looked around on the Internet for a good definition of what
exactly "lexical" means in the context of programming, but I have not
found one, or at least it is not clear.

>From reading the book, and seeing the context, I think I can pick up
what the authors mean when they talk about lexical variable, etc., but
it is a little fuzzy because I'm not really sure what lexical in this
programming context exactly means. And I think the talk on lexical is
important enough to know and understand very well.

In the spirit of Guy Steele's essay, "Growing a Language" (http://
www.cs.virginia.edu/~evans/cs655/readings/steele.pdf) does anyone know
of a good definition of  lexical, or can define it well?

P.S. Since I'm here,  any other recommendations on great/good
references and books on Common Lisp?

From: Rob Warnock
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <GPSdnRMXrP4r1A7bnZ2dnUVZ_rGinZ2d@speakeasy.net>
Nick Mudge  <······@gmail.com> wrote:
+---------------
| I've looked around on the Internet for a good definition of what
| exactly "lexical" means in the context of programming, but I have not
| found one, or at least it is not clear.
+---------------

Well, the first & most basic thing is that the adjective "lexical"
literally means "of or relating to words" or more generally, to text.
E.g., "lexical analysis" is the process of reading a text and
breaking it up into its constituent "lexical tokens" (sometimes
called just "tokens") which then are handed to the "syntactic parser"
(or just "parser") for "syntax analysis". You can Google for those
terms yourself to find out more.

But from your question I suspect you're really asking about
"lexical variables" and "lexical scope", sometimes [as in the
following URL] also called "static scope", see:

    http://en.wikipedia.org/wiki/Scope_%28programming%29

especially the "History" paragraph.

You can also look up most of these terms in the CLHS Glossary:

    http://alu.org/HyperSpec/Body/sec_26-1.html>

The story begins with the notion of a "scope":

    http://alu.org/HyperSpec/Body/glo_s.html#scope
    scope n. the structural or textual region of code in which
    references to an object, a binding, an exit point, a tag,
    or an environment (usually by name) can occur.

Something is "lexically scoped" if you can determine all of the
places where that something is "in scope", that is, has a certain
value, simply by examining the source text -- you don't have to
actually run the program. In Common Lisp, it's defined this way:

    http://alu.org/HyperSpec/Body/glo_l.html#lexical_scope
    lexical scope n. scope that is limited to a spatial or textual
    region within the establishing form. ...

Said another way, with "dynamic binding" [see the CLHS], to know
what a (dynamic) variable is going to contain you have to know the
actual runtime (dynamic) history of execution up until the place
*and* exact time you're asking the question. But with lexical
scoping, you can know just by looking at the program text [lexicum].

Having absorbed that, you now probably have enough context to go
read this entire section of the CLHS [although skip or skim the
subsection on "Environment Objects" until later in your studies]:

    http://alu.org/HyperSpec/Body/sec_3-1-1.html
    3.1.1 Introduction to Environments

    A binding is ... [trimmed].

    An environment is ... [trimmed].

    Bindings in an environment are partitioned into namespaces.
    ...

      3.1.1.1 The Global Environment
      3.1.1.2 Dynamic Environments
      3.1.1.3 Lexical Environments
      3.1.1.4 Environment Objects


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: ········@gmail.com
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <1184081643.994955.320120@r34g2000hsd.googlegroups.com>
On Jul 10, 3:43 am, Nick Mudge <······@gmail.com> wrote:
> P.S. Since I'm here,  any other recommendations on great/good
> references and books on Common Lisp?

I think the Structure and Interpretation of Computer Programs does a
good job explaining lexical/dynamic references, but the examples are
Scheme, not Common Lisp.

http://mitpress.mit.edu/sicp/

 -jimbo
From: Kent M Pitman
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <u3azwl74c.fsf@nhplace.com>
Nick Mudge <······@gmail.com> writes:

> I've looked around on the Internet for a good definition of what
> exactly "lexical" means in the context of programming, but I have not
> found one, or at least it is not clear.

It refers to a set of lexemes, which in many languages are literally
textual, but in Common Lisp are structural.  That detail is not important,
but becomes important only when people misunderstand.  One can construct
CL programs that were never textual.  (list '+ 'x '2) creates a structure
(+ x 2) which was never in text anyway as such, yet has a lexical nature.

"lexical <anything>" is something described in relation to how these 
lexemes are laid out in statically observable fashion.  "dynamic <anything>"
is contrasted with this, and relates to how programs connect dynamically
as they are executed.

In

 (defun f () (g 2))
 (defun g (x) (h x))
 (defun h (x) (+ x 1))

h occurs lexically within g, and g occurs lexically within f.
h is used by f, but only dynamically, not lexically.
From: Matthias Buelow
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <5fh080F3d9j29U1@mid.dfncis.de>
Nick Mudge wrote:

> From reading the book, and seeing the context, I think I can pick up
> what the authors mean when they talk about lexical variable, etc., but
> it is a little fuzzy because I'm not really sure what lexical in this
> programming context exactly means. And I think the talk on lexical is
> important enough to know and understand very well.

Hmm, I'd put it this way: a lexical variable is a place that is visible
only within the lexical block where it is defined; a lexical block in
this context is anything that the read function returns.

This doesn't fully describe the semantics of lexical variables but
should portray the difference to "dynamic" (=special) variables, which
are visible "anywhere" since they're looked-up by name at runtime.
From: Daniel Leidisch
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <f6vvvm$6fl$02$1@news.t-online.com>
Thus spoke Nick Mudge <······@gmail.com>:
> [...] does anyone know of a good definition of  lexical, or can
> define it well?
 
http://www.flownet.com/ron/specials.pdf

helped me very much in understanding lexical vs. dynamic variables
and closures.


Regards,

dhl
From: John Lawrence Aspden
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <Xg6li.7850$oa7.3175@newsfe1-gui.ntli.net>
Wow, what an enlightening morning:

http://www.cs.virginia.edu/~evans/cs655/readings/steele.pdf
http://www.flownet.com/ron/specials.pdf

Thanks very much for these two references. Pure joy. I have the feeling of
understanding things I didn't previously realise I didn't understand.

Five hours well spent. Now, what was I supposed to be doing?

John.

-- 
Contractor in Cambridge UK -- http://www.aspden.com
From: Nick Mudge
Subject: Re: What Does Lexical Mean?
Date: 
Message-ID: <1184207640.451840.309210@g4g2000hsf.googlegroups.com>
On Jul 11, 8:01 am, John Lawrence Aspden <····@aspden.com> wrote:
> Wow, what an enlightening morning:
>
> http://www.cs.virginia.edu/~evans/cs655/readings/steele.pdfhttp://www.flownet.com/ron/specials.pdf
>
> Thanks very much for these two references. Pure joy. I have the feeling of
> understanding things I didn't previously realise I didn't understand.
>
> Five hours well spent. Now, what was I supposed to be doing?
>
> John.
>
> --
> Contractor in Cambridge UK --http://www.aspden.com

Yea, I know what you mean!

Thanks everyone for the really helpful data and answers.

Nick Mudge
http://nickmudge.info/