[Openmcl-devel] Applying sequence functions to a new type
Gary Byers
gb at clozure.com
Tue Mar 1 07:58:49 PST 2005
On Tue, 1 Mar 2005 rm at fabula.de wrote:
> On Tue, Mar 01, 2005 at 05:52:41AM -0700, Gary Byers wrote:
> >
> > On Mon, 28 Feb 2005, Andrew P. Lentvorski, Jr. wrote:
> >
> > > This is probably a really stupid question, but how do I define the
> > > sequence functions so that they apply to a new class?
> >
> > You basically can't reliably subclass BUILT-IN-CLASSes like SEQUENCE
> > (or FLOAT or FIXNUM) in CLOS.
>
> Is this a limitation of CLOS or an artefact of the implementation of BUILT-
> IN-CLASSes?
Well, BUILT-IN-CLASSes are supposed to be limited; see the dictionary
entry for BUILT-IN-CLASS in CLHS.
>
> > The standard sequence functions are not necessarily GENERIC-FUNCTIONs
> > (they aren't in OpenMCL or in any other implementation I'm aware of),
> > so you can't extend their behavior by adding methods on them.
>
> But that at least is open to the implementation - it _would_ be possible
> to have the sequence functions as GENERIC-FUNCTIONs.
Yes. The implementation could define LENGTH (for example) via a few
methods:
(defmethod length ((x t))
(error 'type-error :datum x :expected-type 'sequence))
(defmethod length ((x list))
(let* ((ll (list-lenth x)))
(or ll (error "~s is a circular list." x))))
(defmethod length ((x vector))
(implementation::%vector-length x))
The Gray streams protocol basically does this ("genericizes" a lot
of standard stream functions), effectively making STREAM an abstract
class: a STREAM is basically anything for which the applicable
STREAMP method returns true (likewise INPUT-STREAM/OUTPUT-STREAM, etc.)
The built-in STREAM class may or may not be involved in this at
all (it's certainly possible do define STREAMP methods that return
true for argumenets that don't have that class in their CPL.)
That's certainly useful and not too controversial; the compiler's
not likely to open-code stream functions, and any programs or persons
that try to reason about program behavior would probably find
abstract streams no easier or harder to reason about than concrete
ones. (For all that I know, there's someone somewhere who's upset
that Gray streams make programs harder to reason about and would
rather that they didn't exist, but I haven't heard this expresssed.)
I think that many people could agree that it could be useful to genericize
sequence functions (or CL arithmetic, or ...), but at least as many
people would -like- those domains to stay closed. (That was certainly
true 15 or 20 years ago, when CLOS was being designed; it's not clear
that CLOS would have been approved in that time frame if it insisted
that all CL functions were generic and that built-in classes could
be subclassed.)
More information about the Openmcl-devel
mailing list