Home / Docs / Diagnostics

U2016: Opaque predicate called outside checked

Release v0.4.0

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:

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.

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

diff
  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 Malformed

Failing example

una
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 Malformed

Fixed example

una
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