[Python-Dev] type categories

Oren Tirosh oren-py-d@hishome.net
Tue, 27 Aug 2002 00:13:02 +0300

On Mon, Aug 26, 2002 at 04:33:53PM -0400, Andrew Koenig wrote:
> Oren> Form-based categories may be used to convey semantic categories:
> Oren> just add a dummy method or member to serve as a marker. It can
> Oren> force an interface with an otherwise identical form to be
> Oren> intentionally incompatible to help you detect semantic
> Oren> categorization errors.
> Remember that one thing I consider important is the ability to claim
> that classes written by others belong to a category defined by me.  I do not
> want to have to modify those classes in order to do so.

How about union(int, long, float, has_marker("TotallyOrdered")) ?

This basically means "I know that int, long and float are totally ordered
and I'm willing to take your word for it if you claim that your type is 
also totally ordered".

If the set of types that match a predicate is cached it should be at least 
as efficient as any other form of runtime interface checking.

> Oren> The opposite is not true: semantic categories cannot be used to
> Oren> enforce well-formedness. You can mark a class as implementing
> Oren> the "TotallyOrdered" interface when it doesn't even have a
> Oren> comparison method.
> Yes.  But semantic categories are useful anyway.

Sure they are, but if form-based categories can be used to define semantic
categories but not the other way around makes a point in favor of using
form-based categories as as the basic form for categories implemented by 
the language.

Inheritance of implementation also inherits the form (methods and call
signatures). If you don't go out of your way to modify it a subclass will 
usually also be a subcategory so this should be pretty transparent most of 
the time.

Form-based categories are a tool for making claims about code: "under 
condition X the method Y should not raise NameError or TypeError". If you 
want, you also use this tool to make semantic claims about your data types.
With compile-time type inference these claims can be upgraded to the level 
of formal proofs.