Related articles |
---|
Pronouns in programming language? vii@altern.org (John Fremlin) (2000-02-27) |
Re: Pronouns in programming language? schairer@dai.ed.ac.uk (Axel Schairer) (2000-02-28) |
Re: Pronouns in programming language? rweaver@ix.netcom.com (2000-02-28) |
Re: Pronouns in programming language? pwagle@my-deja.com (2000-02-28) |
Re: Pronouns in programming language? jjones@cs.uiuc.edu (2000-02-28) |
Re: Pronouns in programming language? mal@bewoner.dma.be (Lieven Marchand) (2000-02-28) |
Re: Pronouns in programming language? hamish.a@virgin.net (Hamish Atkinson) (2000-02-28) |
Re: Pronouns in programming language? rkrayhawk@aol.com (2000-02-28) |
[14 later articles] |
From: | Axel Schairer <schairer@dai.ed.ac.uk> |
Newsgroups: | comp.compilers |
Date: | 28 Feb 2000 02:56:33 -0500 |
Organization: | Dept of Artificial Intelligence, Edinburgh University, Scotland |
References: | 00-02-149 |
Keywords: | design |
[Sorry, the post got longer than I thought it would. Also not the
whole of it might be entirely on-topic for c.c.]
[Language design has always been within c.c's purview so long as it's
at least within hailing distance of topics that affect the way you'd
build a compiler. -John]
John Fremlin <vii@altern.org> writes:
> Has any programming language/compiler implemented pronoun like
> constructs?
Our Moderator wrote:
> [I can think of some examples. Lisp LET (typically implemented as an
> anonymous procedure) permits you to assign names to expressions for very
> limited scopes.
That would be
(let ((it very-long-variable-name))
(cond ((equal it key1) ...)
((equal it key2) ...)
...))
but you'd probably rather use a case form for this in Common Lisp.
Also, the Common Lisp macro system can be used to implement anaphora.
There are, as far as I can remember, discussions of this kind of thing
in Peter Norvig: _Paradigms of AI Programming -- Case Studies in
Common Lisp_, and also in Paul Graham: _On Lisp_.
As an example, you can write a macro for `anaphoric if' such that the
then and else clauses can refer to the value of the condition by the
variable `it'. (Something like this is in Graham's book.)
(defmacro if-a (condition then-clause &optional (else-clause nil))
`(let ((it ,condition))
(if it
,then-clause
,else-clause)))
This is expanded at compile time so that your compiler, instead of,
(if-a (lookup-entry key database)
(process it)
(add-new-entry-to-database-and-process-that))
really compiles
(let ((it (lookup-entry key database)))
(if it (process it) (add-new-entry-to-database-and-process-that)))
(This is actually useful in Lisp because everything but NIL is
interpreted as true, and according to a common convention you would
probably specify LOOKUP-ENTRY to either return the entry it found, or
NIL if there was no entry. So in the then-clause, IT would have a
meaningful value.)
> Would it be a good idea? I don't think that it'd be too difficult to
> implement in my project . . .
Sometimes this can be a nice feature, however there are problems with
it. If you nest such constructs you'll end up shadowing one IT by
another. So you might run the risk that some seemingly unproblematic
editing step might break your code. If you change the snippet on the
left to the one on the right
(if-a condition1 | (if-a condition1
| (if-a condition2
| (g it)
(f it) | (f it)) ; <=== Bug?
(...)) | (...))
you now apply F to the value of CONDITION2, whereas you applied it to
the value of CONDITION1 before the change. Therefore, some people
prefer to explicitly specify the name for the anaphoric reference, as
in
(if-a my-it condition
(... my-it ...)
(...))
or some such. This can also be done in CL's macro system.
In the examples you give in your post it does, to me, not seem to be
worth the trouble because just using abbreviatinons for `variables on
the left hand side of equations in the condition of if-expressions'
seems to be too ad hoc, and generalising to arbitrary expression would
mean you have to decide and specify how often the expressions you
abbreviate are evaluated. Also what does `it' refer to if you don't
have a test for equality as the condition?
In summary, it could be nice to have, but you have to balance the
advantages and disadvantages.
Hope this helps,
Axel
Return to the
comp.compilers page.
Search the
comp.compilers archives again.