Refiners
Refiners transform a region decomposition after it has been computed. They are applied with the |>> operator inside a [@@decomp ...] attribute:
top () |>> prune |>> combineThe available refiners are:
| Refiner | What it does |
|---|---|
prune | Remove regions proved empty and attach a witness to regions proved feasible |
combine | Join regions that have the same invariant |
enumerate e | Split each region by the values of another function |
enumerate_all ~style [e1; e2; …] | Like enumerate, for several functions at once |
Merging the decomposition of a callee into its caller is done with the << and <|< operators rather than with |>>; see Composition.
Running example
The examples on this page decompose g, which calls f. pos is used where a second function with g's parameters is needed.
let f (x : int) : int =
if x > 12 then 2 else if x > 4 then 1 else if x < -4 then 1 else 0
let g (x : int) : int = if x > 8 then 1 else f (x + 1)
let pos (x : int) : bool = x > -3Plain top () on g gives five regions. One of them, x >= 12 && x <= 8, has contradictory constraints, but it is kept because pruning is off by default:
| Constraints | Invariant | Note |
|---|---|---|
x <= -6 | 1 | |
x >= -5 && x <= 3 | 0 | |
x >= 4 && x <= 8 | 1 | |
x >= 9 | 1 | |
x >= 12 && x <= 8 | 2 | Unsatisfiable |
prune
Check each region for feasibility: remove the regions found infeasible, and attach a witness input (a model) to the regions found feasible:
let d_prune = g
[@@decomp top () |>> prune]| Constraints | Invariant | Witness |
|---|---|---|
x <= -6 | 1 | x = -6 |
x >= -5 && x <= 3 | 0 | x = 0 |
x >= 4 && x <= 8 | 1 | x = 4 |
x >= 9 | 1 | x = 9 |
top ~prune:true () gives the same result when top is the whole expression. In a composed expression the two differ: ~prune:true prunes the top result before the other operators run, so regions emptied by a later step are kept. Put |>> prune last when you want a clean result.
Witnesses after composition
prune attaches a witness to each region it finds feasible. Regions that a
later step changes or merges need new witnesses, which may require another
prune. Some refiners prune as part of their work; enumerate is one.
combine
Join all regions that have the same invariant into one region, whose constraint is the disjunction of theirs:
let d_combine = g
[@@decomp top () |>> combine]| Constraints | Invariant | Note |
|---|---|---|
x <= -6 || (x >= 4 && x <= 8) || x >= 9 | 1 | |
x >= -5 && x <= 3 | 0 | |
x >= 12 && x <= 8 | 2 | Unsatisfiable |
Use it when you care about distinct outputs rather than distinct paths: one region per behavior. ~| m is shorthand for m |>> combine.
enumerate
enumerate e takes a decomposition e of another function and, for every region, adds one child region per value that function can return inside the region:
let d_enum = g
[@@decomp top () |>> enumerate (top () [%id f])]Each child is its parent's constraints plus one f x = v constraint:
| Parent region | Child adds | Invariant | Inputs in the child |
|---|---|---|---|
x <= -6 | f x = 1 | 1 | all of the parent |
x >= -5 && x <= 3 | f x = 0 | 0 | -4 to 3 |
x >= -5 && x <= 3 | f x = 1 | 0 | -5 |
x >= 4 && x <= 8 | f x = 0 | 1 | 4 |
x >= 4 && x <= 8 | f x = 1 | 1 | 5 to 8 |
x >= 9 | f x = 1 | 1 | 9 to 12 |
x >= 9 | f x = 2 | 1 | 13 and up |
x >= 12 && x <= 8 | none | 2 | none: no input satisfies the parent |
The parent regions are kept alongside their children, so the result has 12 regions: the 5 parents and the 7 children. Some things to know:
- Same signature. The enumerated function must take the same number of parameters as the decomposed function, with the same types in the same order (names may differ), and it is applied to them directly. Above,
fis applied tog'sx, not to thex + 1thatgpasses to it. A mismatch is reported when the decomposition runs. - It does not need to appear in the decomposed function. Unlike merge, which substitutes a callee at its call site,
enumeratetags every region with the values of any function of the same inputs. - Splits by value, not by region. Regions of
ethat return the same value share one child, and only feasible children are kept. The enumerated function must take finitely many values in every region, otherwise enumeration does not terminate: aboolorbool optionresult is fine, an unboundedintorint optionresult is not. Enumerating byposgives each region apos xchild, anot (pos x)child, or both. enumerateneeds its argument. A baretop () |>> enumerateis a type error.
The typical use is to enumerate by a classifier, to get test cases that cover every output class within every path.
enumerate_all
enumerate_all enumerates by several functions at once. The ~style argument is required:
let d_add = g
[@@decomp top () |>> enumerate_all ~style:Additive [ top () [%id f]; top () [%id pos] ]]
let d_mul = g
[@@decomp top () |>> enumerate_all ~style:Multiplicative [ top () [%id f]; top () [%id pos] ]]Additivetakes the union: a region's children are the children from enumerating by each function separately, so children of one region overlap.Multiplicativetakes the product: each child fixes the values of all the functions at once, and only feasible combinations are kept, so children of one region are disjoint.
For the region x >= -5 && x <= 3, the children add these constraints:
| Style | Children |
|---|---|
Additive | f x = 0; f x = 1; pos x; not (pos x) |
Multiplicative | f x = 0 && pos x; f x = 1 && not (pos x); f x = 0 && not (pos x) |
f x = 1 && pos x is infeasible in this region, so Multiplicative drops it. enumerate e is the same as enumerate_all [e]; with a single function the two styles agree, and list order does not matter.
Chaining refiners
Refiners chain left to right:
let d_chain = g
[@@decomp top () |>> prune |>> combine]This first removes the empty region, then joins the three regions that return 1:
| Constraints | Invariant | Witness |
|---|---|---|
x <= -6 || (x >= 4 && x <= 8) || x >= 9 | 1 | x = 4 |
x >= -5 && x <= 3 | 0 | x = 0 |