comp.lang.ada
 help / color / mirror / Atom feed
From: jayessay <nospam@foo.com>
Subject: Re: OT: What was the first programming language to use 'generics'?...
Date: 19 Aug 2005 11:42:07 -0400
Date: 2005-08-19T11:42:07-04:00	[thread overview]
Message-ID: <m33bp69bn4.fsf@rigel.goldenthreadtech.com> (raw)
In-Reply-To: wccu0hnj5au.fsf@shell01.TheWorld.com

Robert A Duff <bobduff@shell01.TheWorld.com> writes:

> David Trudgett <wpower@zeta.org.au.nospamplease>  writes:
> 
> > "Martin Dowie" <martin.dowie@baesystems.com> writes:
> > 
> > > ...and were they called 'generics'?
> > >
> > > Not Ada but I just know /someone/ here will know this! ;-)
> > >
> > >
> > 
> > Probably (given I'm not a language historian) Lisp code macros were
> > the first, except for a couple of provisos: (a) Lisp never had the
> > strong static typing that Ada has, so Lisp macros weren't invented to
> > solve the same problem as Ada generics; and (b) Lisp macros are more
> > general than Ada generics in the sense that they allow essentially
> > arbitrary code (not text) generation (every Ada programmer's
> > nightmare? ;-)).
> 
> An important difference between Ada's generics and most macro systems

Apropos to the actual thread title, I don't think Lisp macros are much
of anything like "generics" as the term is typically used, because
while they can act as templates for simple cases, that is a small part
of what they do.


> is: what should names in the template refer to.

The distinctions here are harder to apply in the Lisp case, but it may
be interesting to look a bit more.


> If it refers to a formal parameter in the template, it refers to the
> actual parameter in the instance.

This is the easier of the two.  The most typical case in Lisp, would
adhere to this as well:

(defmacro hi (a)
  `(format nil "Hello there object ~A" ,a))

The backquote comma form is a shorthand for creating a template, so
this very simple example is indeed a code template.  The backquote
says to copy things verbatim except for "escaped" things (which the
comma does).

A call could be

(hi 1) ==> "Hello there object 1"

The expansion is just what you would think:

(FORMAT NIL "Hello there object ~A" 1)

(hi 'A) ==> "Hello there object A"

Expansion: (FORMAT NIL "Hello there object ~A" 'A)


However, this typical _use_ of parameters is not required or even what
you want to do in many cases.  Those cases are where the parameter(s)
may control the expansion process.  For example,

(defmacro foo (key)
  (let ((type-of-key (type-of key)))
    (if (eq key :one)
	`(identity '(One is the first counting number))
      `(identity '(the type of the KEY argument is ,type-of-key)))))

(foo :one) ==> (one is the first counting number)

Expansion: (identity '(one is the first counting number))

(foo :two) ==> (the type of the key argument is symbol)

Expansion: (identity '(the type of the key argument is symbol))


As you can see, the KEY parameter does not show up in either the
output (as its value) or the expansion code (as itself).


>  If a name in the template refers to something local to the
> template, it refers to the copy of that thing in the instance.

If you look at the foo macro and its expansion a bit, you can also see
there are things ("names") in the "template" (expander code) which do
not show up at all in the "instance" (expansion).  For example, the
LET, IF, etc are all gone. There are other things which do behave in
this fashion, for example, IDENTITY.

> If it refers to neither, what should it refer to?  To something
> visible at the place of the template, or something visible at the
> place of the instance?  The Ada answer is: at the place of the
> template.  The usual macro answer is: at the place of the instance.

In the case of Lisp, the answer is it refers to _expander code_ and it
doesn't exist (let alone being visible) at either noted place.  It
exists in the macro function:

(symbol-function 'foo) ==> #<macro FOO @ #x775b97f2>


It's this last bit, that a Lisp macro is really a function (like any
other, except that it runs at a special time), able to make use of any
resources available in the base language, 3rd party libs, your own
code and the macros lexical and expansion time environment, that makes
Lisp macros so completely different from templates, parameterized
code/types, generics, and other "macros".


> Some Lisp dialects have "hygienic macros", which are a lot more like Ada
> generics in this regard.

You're probably thinking of Scheme (syntax-case) or maybe Dylan here.
I'd say they are somewhat more like generics/templates.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com



  parent reply	other threads:[~2005-08-19 15:42 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-18  9:18 OT: What was the first programming language to use 'generics'? Martin Dowie
2005-08-18 11:29 ` Jean-Pierre Rosen
2005-08-19 16:25   ` Charles Lindsey
2005-08-18 21:11 ` David Trudgett
2005-08-18 21:36   ` Robert A Duff
2005-08-18 23:43     ` David Trudgett
2005-08-19  2:13       ` OT: What was the first programming language to use Larry Kilgallen
2005-08-19  9:44         ` David Trudgett
2005-08-19 14:22         ` jayessay
2005-08-19 15:08       ` OT: What was the first programming language to use 'generics'? Robert A Duff
2005-08-19 18:09         ` jayessay
2005-08-19 15:42     ` jayessay [this message]
2005-08-19  2:47 ` Jeffrey R. Carter
2005-08-21 17:33 ` adaworks
     [not found]   ` <odeig19vmplnbt67s3s148eb4mqrk9vujd@4ax.com>
2005-08-23  7:03     ` adaworks
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox