[Python-Dev] type categories
Andrew Koenig
ark@research.att.com
Mon, 26 Aug 2002 16:33:53 -0400 (EDT)
Oren> On Mon, Aug 26, 2002 at 03:51:13PM -0400, Andrew Koenig wrote:
Oren> Can you give a more concrete example of what could a cartesian
Oren> product of type predicates actually stand for in Python?
>> Consider my TotallyOrdered suggestion from before. I would like to
>> have a way of saying that for any two types T1 and T2 (where T1
>> might equal T2) chosen from the set {int, long, float}, < imposes a
>> total ordering on values of those types.
>> Come to think of it, that's not really a Cartesian product.
>> Rather, it's a claim about the members of the set
>> union(int,union(long, float)).
Oren> Isn't it easier to just spell it union(int, long, float)?
Yes but I have a cold today so I'm not thinking clearly.
Oren> Your example helped me make the distinction between two very
Oren> types of type categories:
Oren> 1. Type categories based on form: presence of methods, call signatures, etc.
Oren> 2. Type categories based on semantics.
Oren> Semantic categories only live within a single form category. A
Oren> method call cannot possibly be semantically correct if it isn't
Oren> well-formed: it will cause a runtime error. But a method call
Oren> that is well-formed may or may not be semantically correct.
Yes.
Oren> A language *can* verify well-formedness. It cannot verify
Oren> semantical correctness but it can provide tools to help
Oren> developers communicate their semantic expectations.
Yes.
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.
So, for example, if I want to say that int is TotallyOrdered, I do not
want to have to modify the definition of int to do so.
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.
Oren> A similar case can happen when using inheritance for
Oren> categorization: a subclass may modify the call signatures,
Oren> making the class form-incompatible but it still retains its
Oren> ancestry which may be interpreted in some cases as a marker of a
Oren> semantic category.
Right. And several people have noted that it can be desirable for
subclasses sometimes not to be members of all of their base classes'
categories.