Region Decomposition Basics
Region decompositions are declared using the [@@decomp ...] attribute on a function definition. The primary entry point is top, which decomposes the entire input space into disjoint regions.
The [@@decomp top ()] attribute
let f x = if x = 1 || x = 2 then x + 1 else x - 1
[@@decomp top ()]This decomposes the entire input space of f into disjoint regions, each with constraints and an invariant.
Example
let classify age =
if age < 0 then "invalid"
else if age < 13 then "child"
else if age < 18 then "teenager"
else if age < 65 then "adult"
else "senior"
[@@decomp top ()]This produces five regions:
| Constraints | Invariant |
|---|---|
age <= -1 | "invalid" |
age >= 0 && age <= 12 | "child" |
age >= 13 && age <= 17 | "teenager" |
age >= 18 && age <= 64 | "adult" |
age >= 65 | "senior" |
Optional parameters
~assuming: side-conditions
Restrict the decomposition to inputs satisfying a given predicate. This yields all unique regions of behavior under the assumption that the side-condition holds. Use [%id ...] to reference the assumption function:
let asm x = x > 1
let g x = if x > 10 then x * 2 else x + 5
[@@decomp top ~assuming:[%id asm] ()]This decomposes g only over inputs where asm holds (i.e., x > 1), potentially producing fewer regions. Side-conditions and basis are orthogonal: you can use them independently or together.
~basis: abstraction boundaries
Every decomposition is performed with respect to a basis: a collection of function symbols treated as atomic concepts. Basis functions' definitions are not expanded and do not contribute case-splits during decomposition, but they are interpreted when deciding feasibility of purported regions. This means infeasible regions involving basis functions are still correctly pruned.
let g x = if f x > x then -1 else 1
[@@decomp top ~basis:[ [%id f] ] ()]The decomposition of g will treat f as an opaque function, producing regions in terms of f x rather than expanding f's definition. You can later merge f's own decomposition in using the << operator (see Composition).
~ctx_simp: contextual simplification
Enable contextual simplification (value propagation and rewriting) during decomposition:
let h x y = if x > 0 then x + y else y - x
[@@decomp top ~ctx_simp:true ()]~prune: prune infeasible regions
Check each region's constraints for feasibility. Regions proved unsatisfiable are removed, and each region proved feasible gets a witness input: a concrete assignment satisfying its constraints.
let f x = if x > 0 && x < 0 then 1 else 2
[@@decomp top ~prune:true ()]The feasibility check can fail, for example on a step limit or a timeout. A region whose check fails is kept with status Feasibility check failed and no witness; it may or may not be empty.
~rule_specs: rewrite and forward-chaining rules
~rule_specs passes rewrite and forward-chaining rules that are applied while the decomposition is computed. Most of the time they only change how constraints and invariants are written, into a form that prune or ~assuming can decide more easily. They can also change the regions themselves: a rule that simplifies a branch condition to a constant removes the case split on that branch.
The usual way to get a rule is to prove a theorem and mark it with a rule attribute:
[@@rw]makes it a rewrite rule, used to simplify terms inside constraints and invariants.[@@fc]makes it a forward-chaining rule, adding derived facts to a region's context so that feasibility checks and further simplification can use them.
A theorem is only used by a decomposition when it is passed with ~rule_specs:
let h (xs : int list) (ys : int list) : int =
if List.length (xs @ ys) > 3 then 1 else 0
theorem len_app (xs : int list) (ys : int list) =
List.length (xs @ ys) = List.length xs + List.length ys
[@@by auto] [@@rw]
let d_plain = h
[@@decomp top ()]
let d_rw = h
[@@decomp top ~rule_specs:[ [%id len_app] ] ()]| Region | Constraint with top () | Constraint with top ~rule_specs:[ [%id len_app] ] () | Invariant |
|---|---|---|---|
| 1 | List.length (xs @ ys) <= 3 | List.length xs + List.length ys <= 3 | 0 |
| 2 | List.length (xs @ ys) >= 4 | List.length xs + List.length ys >= 4 | 1 |
The regions are the same, but the rule has rewritten List.length (xs @ ys) into a sum the solver can reason about. That matters when a later ~assuming, prune or merge needs to decide whether a constraint is feasible, or when you want the region constraints in a simpler form. Rules are most useful when the decomposed function calls recursive library functions (List.length, List.mem, folds) and you know an identity that exposes the structure you care about.
A forward-chaining rule is declared and passed in the same way, with [@@fc] instead of [@@rw]. It adds facts to a region's context rather than rewriting terms, which is what a feasibility check needs. Here prune on its own cannot decide whether List.length xs < 0 is satisfiable, so it keeps that region with status Feasibility check failed; with the rule in context it proves the region empty and drops it:
let k (xs : int list) : int =
if List.length xs < 0 then 1 else 0
theorem len_nonneg (xs : int list) =
List.length xs >= 0
[@@by auto] [@@fc]
let d_k_prune = k
[@@decomp top ~prune:true ()]
let d_k_prune_fc = k
[@@decomp top ~prune:true ~rule_specs:[ [%id len_nonneg] ] ()]| Decomposition | Constraints | Invariant | Witness |
|---|---|---|---|
d_k_prune | List.length xs <= -1 | 1 | None (pruning stopped at the 100-step limit) |
d_k_prune | List.length xs >= 0 | 0 | xs = [] |
d_k_prune_fc | List.length xs >= 0 | 0 | xs = [] |
Note that [@@rw] or [@@fc] on a theorem also makes it a rule for later proofs in the file, not only for the decompositions it is passed to.
Unproved rules with rule_spec
When a fact cannot be proved, or you only want to assume it for the purpose of a decomposition, declare it with the rule_spec keyword instead of theorem. It takes the same rule attributes and is passed the same way:
rule_spec len_app_rs (xs : int list) (ys : int list) =
List.length (xs @ ys) = List.length xs + List.length ys
[@@rw]
let d_rs = h
[@@decomp top ~rule_specs:[ [%id len_app_rs] ] ()]A `rule_spec` is not proved, unlike a theorem
A rule_spec is never proved. It is used only by the decompositions you pass
it to, not by theorem or verify, and ImandraX accepts a false one without
complaint. A false rule spec can silently remove real behaviour from the
decomposition. Prefer a proved theorem, and use rule_spec only for facts
you are willing to assume.
Understanding the output
Each region in the decomposition contains:
- Constraints (
cs): a conjunction of conditions on the input variables that define the region - Invariant (
inv): a symbolic expression describing the function's output in terms of the input variables
The regions satisfy:
- Local correctness: for each region, if its constraints hold for an input, then the function's value equals its invariant
- Coverage: the disjunction of all region constraints is a tautology (modulo background theories and any
~assumingside-condition). Every possible input belongs to at least one region - Feasibility: with pruning on (
~prune:trueor|>> prune), regions proved empty are removed and regions proved feasible carry a witness: a concrete input satisfying its constraints, usable as a test case. Regions whose feasibility check fails are kept without a witness (see~pruneabove). Without pruning, regions are not checked and may be empty - Semantically meaningful: each region corresponds to a distinct computational path through the function
Decompositions are first-class values: they can be printed, composed, refined, and serialized.
Recursive functions
Region decomposition works with recursive functions too. ImandraX unrolls the recursion and analyzes the resulting regions:
let rec gcd a b =
if b = 0 then a
else gcd b (a mod b)
[@@decomp top ()]For recursive functions, the decomposition captures the distinct patterns of recursion depth and base-case behavior.