I'm wondering why it isn't common for type systems to allow attributes representing properties of objects, and to allow variables to specify attributes in addition to regular types, and thus demand that certain properties hold for values assigned to them. This system would be a generalization of the @Nullable/@NotNull annotations that have become popular in Java, and an integration of them into the type system. It's such an obvious idea that I assume that the reason I haven't seen it in practice is that I'm ignorant and I'm hoping that someone can point out an example of it in the real world.
Note the type system of Haskell, where you'd specify something like:
Except that you'd run into trouble when implementing square, as in order to construct an instance of b, we must create a new data type (or retrofit an existing type) implementing Integral and Square. Java can do this as well:
But this doesn't really help because we need to create classes satisfying all possible combinations of attributes and functionality to convert between them.
For fun, I've thought up how I think such a type system would work and look. I'm imagining the notation E<@attr1, ..., @attrM>, where E is a regular type and each @attrI is an attribute, not a type parameter. Thus this feature doesn't work like templates in C++ or generics in Java - E knows nothing about the attributes or the number of attributes. We have the rule that E<@attr1, ..., @attrM> ≤ F<@attr'1, ..., @attr'N> iff E ≤ F and {@attr'1, ..., @attr'N} ⊆ {@attr1, ..., @attrM}. Or if we allow "sub-attributes", such as for numbers, @square extends @nonnegative, we would require that E ≤ F and there is a function f mapping {@attr'1, ..., @attr'N} to {@attr1, ..., @attrM} such that each f(@attr'I) ≤ @attr'I. So an int<@square> could be assigned to an int<@nonnegative>. Note that such a system breaks down for mutable objects, and thus there would need to be some language support for immutability or to guarantee that no mutation occurs.
For example:
@square(x * x) converts an int (more generally, an int<@attr1, ..., @attrM>) to an int<@square> (more generally, an int<@attr1, ..., @attrM, @square>). This notation suggests that a method is called, and in fact @square would be able validate its input to ensure that it is a valid square and that validation could be invoked at runtime:
In general there could be one or more validate methods with parameters of different types and an attribute can only be attached to a type that it can validate. On construction, the runtime validates the value against the attribute and any attributes that it extends. In some cases, validation could be slow, so perhaps it could be turned off, as with assertions. Alternately, certain attributes could "fake" validation by always returning true (or just rejecting obviously wrong values) and rely on the developer to assign attributes correctly.
Note that unfortunately, validation occurs at runtime, not compile time, meaning that some errors survive compilation. Nonetheless, the type system allows developers to express their intentions about what properties of objects hold and allows the compiler to ensure correctness assuming that the intentions are correct, which would be a significant step forward from having no such system.
A final missing piece, which I admit could ruin the whole thing, would be a way of marking which attributes a function preserves. For example, the square function would preserve @even and @odd. Without being able to specify preservation of attributes, passing a value through a function would strip off all attributes not explicitly assigned in the function. I'm not sure how such a system would work.
Note the type system of Haskell, where you'd specify something like:
Code:
square :: (Integral a, Integral b, Square b) => a -> b
sqrt :: (Integral a, Square a, Integral b) => a -> b
Code:
<N extends Integer & Square> int sqrt(N x);
For fun, I've thought up how I think such a type system would work and look. I'm imagining the notation E<@attr1, ..., @attrM>, where E is a regular type and each @attrI is an attribute, not a type parameter. Thus this feature doesn't work like templates in C++ or generics in Java - E knows nothing about the attributes or the number of attributes. We have the rule that E<@attr1, ..., @attrM> ≤ F<@attr'1, ..., @attr'N> iff E ≤ F and {@attr'1, ..., @attr'N} ⊆ {@attr1, ..., @attrM}. Or if we allow "sub-attributes", such as for numbers, @square extends @nonnegative, we would require that E ≤ F and there is a function f mapping {@attr'1, ..., @attr'N} to {@attr1, ..., @attrM} such that each f(@attr'I) ≤ @attr'I. So an int<@square> could be assigned to an int<@nonnegative>. Note that such a system breaks down for mutable objects, and thus there would need to be some language support for immutability or to guarantee that no mutation occurs.
For example:
Code:
int x = 3;
int<@square> xSquared = square(x);
int y = sqrt(xSquared); // y is an int because it's the square root of a square
int<@square> square(int x) {
if (x >= 0x10000 || x <= -0x10000)
throw new IllegalArgumentException(x + " is out of bounds");
return @square(x * x); // add the @square attribute - see below
}
int sqrt(int<@square> x) {
return (int)Math.sqrt(x);
}
@square(x * x) converts an int (more generally, an int<@attr1, ..., @attrM>) to an int<@square> (more generally, an int<@attr1, ..., @attrM, @square>). This notation suggests that a method is called, and in fact @square would be able validate its input to ensure that it is a valid square and that validation could be invoked at runtime:
Code:
typeAttribute @square extends @nonnegative {
boolean validate(int x) {
int sqrt = (int)Math.sqrt(x);
return x == sqrt * sqrt;
}
}
Note that unfortunately, validation occurs at runtime, not compile time, meaning that some errors survive compilation. Nonetheless, the type system allows developers to express their intentions about what properties of objects hold and allows the compiler to ensure correctness assuming that the intentions are correct, which would be a significant step forward from having no such system.
A final missing piece, which I admit could ruin the whole thing, would be a way of marking which attributes a function preserves. For example, the square function would preserve @even and @odd. Without being able to specify preservation of attributes, passing a value through a function would strip off all attributes not explicitly assigned in the function. I'm not sure how such a system would work.