Refiners

Refiners transform a region decomposition after it has been computed. They are applied with the |>> operator inside a [@@decomp ...] attribute:

top () |>> prune |>> combine

The available refiners are:

RefinerWhat it does
pruneRemove regions proved empty and attach a witness to regions proved feasible
combineJoin regions that have the same invariant
enumerate eSplit 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 > -3

Plain 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:

ConstraintsInvariantNote
x <= -61
x >= -5 && x <= 30
x >= 4 && x <= 81
x >= 91
x >= 12 && x <= 82Unsatisfiable

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]
ConstraintsInvariantWitness
x <= -61x = -6
x >= -5 && x <= 30x = 0
x >= 4 && x <= 81x = 4
x >= 91x = 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]
ConstraintsInvariantNote
x <= -6 || (x >= 4 && x <= 8) || x >= 91
x >= -5 && x <= 30
x >= 12 && x <= 82Unsatisfiable

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 regionChild addsInvariantInputs in the child
x <= -6f x = 11all of the parent
x >= -5 && x <= 3f x = 00-4 to 3
x >= -5 && x <= 3f x = 10-5
x >= 4 && x <= 8f x = 014
x >= 4 && x <= 8f x = 115 to 8
x >= 9f x = 119 to 12
x >= 9f x = 2113 and up
x >= 12 && x <= 8none2none: 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, f is applied to g's x, not to the x + 1 that g passes 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, enumerate tags every region with the values of any function of the same inputs.
  • Splits by value, not by region. Regions of e that 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: a bool or bool option result is fine, an unbounded int or int option result is not. Enumerating by pos gives each region a pos x child, a not (pos x) child, or both.
  • enumerate needs its argument. A bare top () |>> enumerate is 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] ]]
  • Additive takes the union: a region's children are the children from enumerating by each function separately, so children of one region overlap.
  • Multiplicative takes 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:

StyleChildren
Additivef x = 0; f x = 1; pos x; not (pos x)
Multiplicativef 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:

ConstraintsInvariantWitness
x <= -6 || (x >= 4 && x <= 8) || x >= 91x = 4
x >= -5 && x <= 30x = 0