Ordinary type systems can tell you a variable is an Int, but they cannot tell you whether that Int is ever negative when it shouldn't be. Refinement types close that gap: a type like says "an integer and it must be positive," and the compiler enforces the predicate automatically using an SMT solver (a constraint-reasoning engine).
The idea is deceptively simple. Instead of writing a defensive if (n <= 0) throw ... everywhere and hoping no branch was forgotten, you write the condition once, in the type. Every assignment to that variable becomes a proof obligation — the compiler asks the SMT solver "can this value possibly violate the predicate?" and rejects the program if the answer is yes.
This is not just tidier code. It is a fundamentally different relationship between types and logic: types become first-class specifications, and checking them is mechanical theorem-proving at the scale of a whole codebase.
Comments
Loading comments...