soma.cell

E0018: determinism_violation

E0018 determinism_violation — a [deterministic] handler uses a nondeterministic builtin.

A compile-time error of soma check, the checker of the Soma programming language (soma explain E0018).

The rule

A handler marked [deterministic] must give the same result for the same arguments, so soma check refuses one that calls a nondeterministic builtin such as random(). The annotation is a promise callers and tests rely on. Move the random or time-dependent value out of the handler and pass it in as a parameter, or remove the annotation if the handler is not meant to be deterministic.

A program that triggers it

wrong.cell

cell Pricing {
    on quote(base_cents: Int, qty: Int) [deterministic] {
        let jitter = round(random() * 5)
        return base_cents * qty + jitter
    }
}

What soma check prints:

$ soma check wrong.cell
error: handler 'quote' is [deterministic] but uses 'random' — reads a non-deterministic source
  --> wrong.cell:2:5
  |
2 |     on quote(base_cents: Int, qty: Int) [deterministic] {
  |     ^
  = code: E0018
✗ 1 error(s), 0 warning(s).

The fix field of soma check --json: handler 'quote' is [deterministic] but uses 'random' — reads a non-deterministic source

Lines 2–3 are rewritten.

wrong.cell → right.cell

--- wrong.cell
+++ right.cell
@@ -1,5 +1,4 @@
 cell Pricing {
-    on quote(base_cents: Int, qty: Int) [deterministic] {
-        let jitter = round(random() * 5)
+    on quote(base_cents: Int, qty: Int, jitter: Int) [deterministic] {
         return base_cents * qty + jitter
     }

With the fix, soma check accepts the program:

$ soma check right.cell
✓ All checks passed.