This is the mail archive of the
guile@cygnus.com
mailing list for the Guile project.
Polymorphism, genericity, etc.
- To: jostobfe@linux.zrz.TU-Berlin.DE
- Subject: Polymorphism, genericity, etc.
- From: Michael Vanier <mvanier@bbb.caltech.edu>
- Date: Sat, 24 Jul 1999 16:24:12 -0700 (PDT)
- Cc: mstachow@alum.mit.edu, silovic@zesoi.fer.hr, chris@tech.com.au, guile@sourceware.cygnus.com
Polymorphism and genericity must be the two most-abused words in computer
science. Here is a short list of the (totally conflicting) ways that these
words are used in different language contexts, as I understand it, as well
as their potential relevance to scheme, also as I understand it.
Abbreviations:
FL: strongly typed functional languages like ML, Haskell, etc.
(although ML also has imperative features)
OO: strongly-typed object-oriented languages like C++, Java, Eiffel
CLOS: Common Lisp Object System
Polymorphism 1 (FL): a polymorphic type system can include types that are
parametrized on other types. E.g. in ML, ('a list) means (list
composed of objects that are all of type 'a). See Genericity 1 (OO).
This is not relevant to scheme as scheme is not strongly-typed.
Polymorphism 2 (OO): When a variable referencing an object of a particular
class A or an object of a subclass of A receives a message representing
a method call, it calls the method that corresponds to the method
definition for the object's class (class A's method if the object is of
class A, class B's method if the object is of class B and B is a
subclass of A, etc. etc.). See Genericity 2 (CLOS). This kind of
polymorphism is necessary even for dynamically-typed languages such as
scheme, at least if you want to have an object system.
Genericity 1 (OO): This usually refers to (almost) the same thing as FL
polymorphic types, that is, types (usually container classes) that can
be parametrized on other types. Eiffel and Ada use the word "generic"
here whereas C++ uses the word "template". So in Eiffel,
"list[integer]" is a list of integers; in C++ it's "list<int>". Again,
this is irrelevant to scheme since scheme is dynamically-typed; any
list is a "list of anything". Contrast Genericity 2 (CLOS).
Genericity 2 (CLOS): A "generic function" in CLOS is one that dispatches on
the types of its arguments. This includes, but is not limited to, the
single-argument dispatch of Polymorphism 2, as well as more complex
dispatches based on the types of any argument or combination of
arguments (multiple dispatch). For this to work, the system must know
what the types of the arguments are (at run-time at least). Such a
system can be implemented in scheme. How this relates to
subtype-supertype relationships in cases of multiple dispatch is not
clear to me.
To make things worse, some languages (like C++ and java) also include
function overloading and operator overloading to achieve some of the same
things CLOS achieves through multiple dispatch, and it all gets very
complex.
When Bertrand Meyer speaks of genericity, he *always* means Genericity 1,
*not* CLOS-type generic functions, which are a totally different beast.
His comments about genericity and inheritance effectively amount to this:
if you have inheritance you can fake genericity (by having an abstract
"list" class, for instance, and then subclassing it to form an
"integer_list" class), but the reverse is not true. However, Meyer
strongly advocates having BOTH inheritance (with Polymorphism 2) AND
genericity, because faking genericity with inheritance is clumsy and
tedious. For example, in Java (which has inheritance (polymorphism 2) but no
generics (genericity 1)), you get around the lack of a parametrized list
class not by writing a new subclass for each type (which would be
type-safe), but by using the "list of any object" class, which requires you
to check types at run-time.
Confused yet? Join the crowd :-)
Mike
--------------------------------------------------------------
Mike Vanier mvanier@bbb.caltech.edu
Department of Computation and Neural Systems, Caltech 216-76
GNU/Linux: We can't lose; we're on a mission from God.
- References:
- Re: records
- From: Jost Boekemeier <jostobfe@linux.zrz.TU-Berlin.DE>