Home / Docs / Diagnostics
U2016: Opaque predicate called outside checked
A call of an opaque predicate appears somewhere other than the predicate of checked or the body of another opaque predicate.
Why Una rejects it
The solver knows an opaque predicate only as an uninterpreted symbol. So a value obtains an opaque fact only through checked, which counts against the module's escape budget and is listed in the build report, or through a capability operation's ensures (spec/03 §4, spec/06 §6, ADR 0042).
An opaque predicate is therefore called only as the predicate of checked and in the bodies of other opaque predicates. A call anywhere else is U2016:
- in code;
- in a
requiresorensuresclause of a function; - in a
reflectbody; - in an example.
The where clause of a kind and the clauses of a capability operation name the predicate without establishing its fact, so they may use it.
How to fix it
Two fixes keep the escape budget.
- In code, when it needs only the test and not the fact, test with a function, not a predicate, that has the same body: an
ifon it establishes nothing for the solver. This works only when the body calls no other opaque predicate. The function's result type must not be a kind whosewherenames the predicate, whose fact the solver could not prove. - When the predicate's body is in the refinement language and does not call itself, declare it
predicate reflect, as the fixed example does. The solver then knows its definition, and a guard proves the fact. A body outside the refinement language is U2015. If the predicate appears in a capability operation's clauses, or in a kind of an operation's signature, those contracts are assumptions, and withreflectthey give their callers the predicate's whole definition: verify each such operation against a handler.
Otherwise, checked(value, p(it)) else fail <Variant> establishes the fact at run time. It is the last repair, marked as an escape (ADR 0095): it counts against the module's escape budget and is listed in the build report.
The repair at a glance
module demo
pub kind SlugError = | Malformed
- predicate opaque valid_slug(t: Text) -> Bool = t.len >= 3
+ predicate reflect valid_slug(t: Text) -> Bool = t.len >= 3
pub kind Slug = Text where valid_slug(it)
pub parse_slug(raw: Text) -> Slug
fails SlugError
=
if valid_slug(raw) then raw else fail MalformedFailing example
module demo
pub kind SlugError = | Malformed
predicate opaque valid_slug(t: Text) -> Bool = t.len >= 3
pub kind Slug = Text where valid_slug(it)
pub parse_slug(raw: Text) -> Slug
fails SlugError
=
if valid_slug(raw) then raw else fail MalformedFixed example
module demo
pub kind SlugError = | Malformed
predicate reflect valid_slug(t: Text) -> Bool = t.len >= 3
pub kind Slug = Text where valid_slug(it)
pub parse_slug(raw: Text) -> Slug
fails SlugError
=
if valid_slug(raw) then raw else fail Malformed