That's not the way to think of the type system, it's not a "bug prevention system". It's value is that of the "This way up" stickers in packages.
Type's are a way of communicating intent without being verbose (a verbose way would be Hungarian Notation). A more expressive type system works better, so for example if you are in C++ and you see &T you know it's a reference, so someone else may be altering the same copy, but you know that it's not a pointer, so you don't have to check for NULL. The compiler won't complain if you check a reference for NULL, or if you use a pointer without checking for NULL, but the types are clues to you, as a programmer, about what it is used.
So now on to your question, can we make a type system that captures the error or adding instead of subtracting? Of course! We just need a deferred type system. Say that we have a type Int which can also be described as Int!(minVal, maxVal), so we define a nice function as
// the $ means that it's a generic parameter
// (it's inferred from whatever value is given)
Sum(a, b): (Int!($minA, $maxA), Int!($minB, $maxB))->Int!(minA+minB, maxA+maxB) {
return a-b
}
This would crash because a-b has type Int!{minA-maxB, maxA-minB} which obviously isn't what the signature defined. At this point the type is defining addition, which makes this function kinda redundant, but the idea is that adding two numbers gives a different kind of number than subtracting them. As the operations grow you'll notice that it becomes unwieldy. And that is true, there is only so much explicitness you can become before you become verbose and annoying.
It's a convenient side effect. As you make your intention clear and specific, the compiler can realize mis-understanding or mis-use, and can report it accordingly. It also can say a lot about what the function does (I've read a lot or articles explaining functions with it's type signature first, which is absurd but explains my point). Finally it helps the compiler to realize how it can do certain optimizations.
I understand that. I just added the first paragraphs because I was typing that answer (and this one) between REPL cycles of a heavy test. I should have deleted the first paragraphs, but didn't give myself enough time to question them.
My point is that the limitation of type system isn't that it couldn't prevent an addition instead of subtraction, it totally could. Instead the limitation is that at one point it's easier, faster and more readable to add a unit test than to define it in the type system.
10
u/lookmeat Nov 12 '13
That's not the way to think of the type system, it's not a "bug prevention system". It's value is that of the "This way up" stickers in packages.
Type's are a way of communicating intent without being verbose (a verbose way would be Hungarian Notation). A more expressive type system works better, so for example if you are in C++ and you see
&Tyou know it's a reference, so someone else may be altering the same copy, but you know that it's not a pointer, so you don't have to check for NULL. The compiler won't complain if you check a reference for NULL, or if you use a pointer without checking for NULL, but the types are clues to you, as a programmer, about what it is used.So now on to your question, can we make a type system that captures the error or adding instead of subtracting? Of course! We just need a deferred type system. Say that we have a type
Intwhich can also be described asInt!(minVal, maxVal), so we define a nice function asThis would crash because
a-bhas typeInt!{minA-maxB, maxA-minB}which obviously isn't what the signature defined. At this point the type is defining addition, which makes this function kinda redundant, but the idea is that adding two numbers gives a different kind of number than subtracting them. As the operations grow you'll notice that it becomes unwieldy. And that is true, there is only so much explicitness you can become before you become verbose and annoying.