Refinement Type Systems
Refinement types strengthen ordinary types with logical predicates. Subtyping becomes implication, and a restricted predicate language makes inference tractable.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 3
- Reading time
- 1 min
In this article3 chapters
Subtyping asks for entailment
A refined type \(\{v:T\mid p(v)\}\) contains values of base type \(T\) satisfying a predicate. To use it where a weaker predicate \(q\) is required, the type checker proves
\[ \Gamma\land p(v)\quad\Longrightarrow\quad q(v). \]
Here the context supplies facts from earlier bindings and branch conditions. A solver proves the implication by checking that its antecedent together with the negated conclusion is unsatisfiable. The arithmetic theory in the solver must match the program semantics. In particular, an unbounded-integer proof does not automatically justify modular machine arithmetic.
Logical subtyping separates the base value from the predicate that restricts it. The matching SMT theory determines what an implication check means for operations such as integer addition.
An array bound follows from two facts
Suppose the context contains \(0\le i\) and \(n\le |a|\). In the true branch of a test \(i<n\), transitivity yields \(0\le i<|a|\), the refinement expected by a checked array lookup. In the false branch the lower and upper facts alone do not produce the strict upper bound. The type rule identifies which branch supplies the missing fact. It can also make the array length an index tied to a returned value.
Finite qualifiers turn inference into solving
Liquid types restrict unknown refinements to combinations of a finite set of templates. Program typing generates constrained implications whose predicates can be approximated by removing incompatible qualifiers. The chosen templates limit what the inference can express. Stronger invariants may require new qualifiers or explicit annotations. The connection to abstract interpretation is a finite space of candidate facts, while automated theorem proving discharges implications. Richer dependent types can express specifications beyond the solver fragment at the cost of requiring more explicit proof.[1][2]