Conceptadvanced
Refinement types
Also called refinement type system.
Types that carry a logical predicate on their values, such as the integers greater than zero, checked with a decision procedure.
A refinement type is an ordinary type together with a logical predicate on its values, such as , the integers greater than zero. Subtyping between refinement types is logical implication, and the checker proves each implication with a decision procedure, usually an SMT solver. The predicates come from a fixed logic that the solver decides, and a refined type always erases to an ordinary type. This is the difference from dependent types, where any term can appear in a type and equality is checked by computation. Liquid types is a method to infer refinement types, and this page is about the type system and its checking. The name also covers an older idea, Freeman and Pfenning's refinements of a datatype into subsorts, such as non-empty lists; this page uses the predicate form, which is the common one today.
In plain words
A normal type says "this is an integer". A refinement type can say "this is an integer that is not zero" or "this is an index that is smaller than the length of that array". You write such facts on the inputs and outputs of functions. The checker then makes sure that every call passes values that meet them. To do this, it collects what it knows at each point, for example that an if test was false, and asks a solver whether those facts imply the one that is needed. If the solver says yes, the call is safe. If it says no, the checker reports an error, often with an example value that breaks the rule. The checker stays automatic only because the facts are written in a logic that the solver can always decide.
The idea
Syntax
The base types are , and predicates are formulas of a logic over program variables and the special variable , which stands for the value itself. A typical is quantifier-free linear integer arithmetic with uninterpreted functions, such as . Types are
The function type is dependent: the result type may mention the argument . A plain is . Programs are in A-normal form, so every argument of a call is a variable or a constant. Then substituting an argument into a predicate keeps the predicate in .
Typing and subtyping rules
The context holds variable bindings and guards, the conditions of the if tests on the path to the current point. Its meaning is the conjunction of for each binding and of each guard.
Gamma(x) = {v : B | p}
-------------------------------- T-Const ----------------------------------- T-Var
Gamma |- n : {v : int | v = n} Gamma |- x : {v : B | p and v = x}
Gamma, x : t1 |- e : t2 Gamma |- e : (x : t1 -> t2) Gamma |- y : t1
--------------------------------- T-Abs ------------------------------------------------- T-App
Gamma |- fun x -> e : x : t1 -> t2 Gamma |- e y : t2[y/x]
Gamma |- y : bool Gamma, y |- e1 : t Gamma, not y |- e2 : t
----------------------------------------------------------------- T-If
Gamma |- if y then e1 else e2 : t
Gamma |- e : t1 Gamma |- t1 <: t2 t2 is well formed in Gamma
----------------------------------------------------------------- T-Sub
Gamma |- e : t2
[[Gamma]] and p1 implies p2 is valid in L
------------------------------------------ S-Base
Gamma |- {v : B | p1} <: {v : B | p2}
Gamma |- s1 <: t1 Gamma, x : s1 |- t2 <: s2
----------------------------------------------- S-Fun
Gamma |- (x : t1 -> t2) <: (x : s1 -> s2)
T-Var gives a variable the most precise type, "equal to ". T-If adds the test to the context of each branch, which makes the checker path-sensitive. S-Base is the only rule that consults the logic. S-Fun is the usual rule of subtyping, contravariant in the argument.
Semantics and soundness
The semantics is the call-by-value semantics of the underlying language, with primitives that get stuck on bad input: and an array read out of bounds have no step. The primitives get types that rule this out:
A refinement type denotes a set of values, . Soundness is progress and preservation. Preservation uses a substitution lemma for dependent function types: if and , then . Progress uses that the logic is sound for its model: if S-Base proved for the second argument of , then the value is not 0, so the call steps. A program that passes the checker never gets stuck on a primitive, so it never divides by zero and never reads outside an array.
Checking and its decidability
With every function annotated, checking is syntax-directed when T-Sub is used only at fixed places (call arguments, function bodies against their annotation, and the two branches of an if against a common type). Each use of S-Base produces one verification condition , whose free variables are read as universally quantified. A formula is valid when its negation is unsatisfiable, so each condition is one query to an SMT solver. This is verification condition generation driven by types.
Theorem. Checking annotated programs is decidable if and only if validity of quantifier-free formulas of is decidable.
Proof. If validity is decidable, the checker makes one pass over the program and produces finitely many conditions. A-normal form keeps each of them in . It decides each one. Conversely, let be any quantifier-free formula of over integer variables. The program fun x1 -> ... -> fun xk -> true checks against if and only if is valid, since the only condition is with unconstrained in the context, and does not occur in . So a checker for every program would decide validity in .
Linear integer arithmetic (Presburger arithmetic), equality with uninterpreted functions, the theory of arrays and their combination by Nelson-Oppen are decidable, which is why refinement languages use them. Integer arithmetic with multiplication of variables is not: validity of such formulas includes Hilbert's tenth problem, which is undecidable. A refinement checker that allows nonlinear predicates is therefore incomplete: the solver may answer "unknown" or time out, and the checker then rejects the program. Quantified predicates have the same problem.
Inference is a separate problem. Refinements for local variables can be read off by T-Var and T-If, but the refinement of a recursive function or a loop is an invariant, and finding one is as hard as loop invariant synthesis. Liquid types restricts the search to conjunctions of given qualifiers, which makes inference decidable.
Examples
Safe division. Check fun a -> if a = 0 then 0 else div 100 a. In the else branch, the guard is in . T-Var gives a the type , and S-Base must show it is a subtype of :
This is valid. Without the test, the condition is , and the solver gives the counterexample .
Absolute value. Check fun x -> if x < 0 then 0 - x else x against . The two branches give the conditions
Both are valid in linear integer arithmetic.
Array bounds. Let , with uninterpreted. In
let rec sum (a : arr) (i : {v : int | 0 <= v}) (acc : int) : int =
if i >= len a then acc else sum a (i + 1) (acc + get a i)
the call of get needs , and the recursive call needs . Both are valid, so the bound check in get can be removed. This is the use that Xi and Pfenning (1998) measured.
Where the logic stops. The predicate with both and variables is outside linear arithmetic. A checker over linear arithmetic either rejects it or treats as an uninterpreted function, which loses facts such as unless they are added as axioms.
Where it is used
- Liquid Haskell checks refinement types for Haskell with an SMT solver (Vazou et al. 2014). Under lazy evaluation, a binding may never be evaluated, so a false refinement on a diverging binding could prove anything. Liquid Haskell checks termination to keep subtyping sound.
- F* combines dependent types with refinements and sends its conditions to Z3. Flux adds refinement types to Rust, using the ownership information that borrow checking gives.
- Dependent ML and ATS use integer indices with linear constraints (Xi and Pfenning 1998, 1999), the same idea in the language of indexed types.
- Datasort refinements (Freeman and Pfenning 1991) refine a datatype into subsorts with a finite lattice, so their inference is a finite fixed point.
- Run-time contracts. Ada range subtypes and contract systems state the same predicates but check them when the program runs (design by contract). A refinement checker proves them before.
References
- Tim Freeman and Frank Pfenning, Refinement types for ML, ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 1991), pages 268 to 277, refinements of ML datatypes into subsorts, intersection types for functions, and the inference of refinements by abstract interpretation over a finite lattice
- Hongwei Xi and Frank Pfenning, Eliminating array bound checking through dependent types, ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 1998), pages 249 to 257, types indexed by integer expressions with linear constraints, checked by a constraint solver, and the removal of array bound checks
- Patrick M. Rondon, Ming Kawaguchi and Ranjit Jhala, Liquid types, ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2008), pages 159 to 169, the refinement type rules with dependent function types, subtyping by implication in a decidable logic, and the path-sensitive environment
- Niki Vazou, Eric L. Seidel, Ranjit Jhala, Dimitrios Vytiniotis and Simon Peyton Jones, Refinement types for Haskell, ACM SIGPLAN International Conference on Functional Programming (ICFP 2014), pages 269 to 282, refinement types under lazy evaluation, and the use of termination to keep subtyping sound
- Ranjit Jhala and Niki Vazou, Refinement types, a tutorial, Foundations and Trends in Programming Languages 6(3-4), 2021, pages 159 to 317, the step-by-step development of a refinement type checker, the verification conditions it generates, and their decidability
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 1VRD55 Edit this page History Report an error Markdown