Proposal for an additional type system
The types in most programming languages are only technical types. If a variable has a type, you may know that it is of type integer or double, but you don't know if it's a length or an area. The types required are semantic types. stypes: length area time speed scalar allowed expressions: length = length length + length : length time = time time + time : time scalar * length : length scalar * time : time sqrt(area) : length speed = speed length / time : speed foo : time bar : length baz : length bar = 23 baz = 42.3 foo = 17.4 speed'variable = (bar + baz) / foo baz = length'5 bar = length'foo Python's dynamic type checking remains. These type names are all user defined. A type name with a tick before an expression means that this expression has the semantic type of the type name. The function names in the allowed expressions statement don't need to be names of existing functions.
On Sat, Feb 1, 2020 at 10:06 AM <guenter-hofer@aon.at> wrote:
The types in most programming languages are only technical types. If a variable has a type, you may know that it is of type integer or double, but you don't know if it's a length or an area. The types required are semantic types.
stypes: length area time speed scalar
allowed expressions: length = length length + length : length time = time time + time : time scalar * length : length scalar * time : time sqrt(area) : length speed = speed length / time : speed
This doesn't need a dedicated type system. Those are actual types, representing units - though you've omitted the actual units. Create a system of classes with actual units, for instance: class Meter(int): ... class Second(int): ... Define the operations that are valid on each of these, and what they return. Define the valid comparisons (for instance, 3 meters is not less than 4 seconds, and that doesn't make sense). Python's normal type system is quite powerful, and can handle this smoothly. (Once you've had a shot at building this yourself, poke around on PyPI and see what already exists.) ChrisA
On Jan 31, 2020, at 15:05, guenter-hofer@aon.at wrote:
The types in most programming languages are only technical types. If a variable has a type, you may know that it is of type integer or double, but you don't know if it's a length or an area. The types required are semantic types.
stypes: length area time speed scalar
allowed expressions: length = length length + length : length time = time time + time : time scalar * length : length scalar * time : time sqrt(area) : length speed = speed length / time : speed
Some of these already exist in the stdlib. For example, datetime.timedelta can be multiplied by a scalar, added to a timedelta, or added to a time, but can’t be multiplied by a timedelta or a time, or added to a scalar. The others could be defined—and in fact there are multiple libraries on PyPI with different designs that do exactly that in different ways. If you wanted to add one of those PyPI libraries to the stdlib, or maybe just add the “framework” part of it that people can use to create new quantity types themselves, that might be a reasonable proposal, but you’d need to look them all over and make the case for why one design is the right one to standardize on and why it needs to be standardized in the first place, not just suggest that we should have something underspecified.
participants (3)
-
Andrew Barnert
-
Chris Angelico
-
guenter-hofer@aon.at