soma.cell

Isolation precautions — a contagious patient cannot be discharged to a

shared bay, and clearance requires two negative tests Hospital-acquired infection spreads when an isolation breach goes unnoticed: a patient colonized with a multidrug-resistant organism (MRSA, C. diff, or a novel respiratory virus) is moved out of contact/airborne precautions before they are cleared, seeding the next ward. The control is a strict status: isolated patients stay isolated until an infection- prevention clearance — typically TWO negative tests — is documented. Here the patient's infection-control status is a state machine. A patient under `isolated` can only reach `cleared` through `pending_clearance` after the required negatives — there is no isolated -> cleared shortcut, and `cleared` is the only state from which a normal-ward transfer is allowed. A bounded counter records consecutive negatives; one positive resets it, so "two negatives" can't be faked by a single lucky swab.

A Soma program from the medical part of the verified corpus: 155 lines, cells Isolation, IsolationTests. It passes soma check with soma 3.2.4.

How it is built

The cell Isolation declares 8 signals in its face: admit_isolated(patient: String) -> Map, test_result(patient: String, negative: Bool) -> Map, request_clearance(patient: String) -> Map, clear(patient: String) -> Map, transfer_to_ward(patient: String) -> Map, negatives(patient: String) -> Int, iso_state(patient: String) -> String and demo() -> String; keeps 1 memory slot: neg_streak (Map<String, Int>, persistent and consistent) under the invariant neg_streak >= 0 && neg_streak <= 2. Its state machine precautions starts in isolated and allows 5 transitions: isolated -> pending_clearance, pending_clearance -> cleared, pending_clearance -> isolated, cleared -> on_ward and on_ward -> discharged_safe. discharged_safe is terminal. Handlers: admit_isolated (line 56), test_result (line 64), request_clearance (line 78), clear (line 89), transfer_to_ward (line 99), negatives (line 107), iso_state (line 108) and demo (line 110).

The file also holds 1 test cell (IsolationTests) with 15 assertions, run by soma test.

Generated from soma describe on this file.

The source

isolation.cell

// ============================================================================
// ISOLATION PRECAUTIONS — a contagious patient cannot be discharged to a
// shared bay, and clearance requires two negative tests
// ============================================================================
//
// Hospital-acquired infection spreads when an isolation breach goes
// unnoticed: a patient colonized with a multidrug-resistant organism (MRSA,
// C. diff, or a novel respiratory virus) is moved out of contact/airborne
// precautions before they are cleared, seeding the next ward. The control
// is a strict status: isolated patients stay isolated until an infection-
// prevention clearance — typically TWO negative tests — is documented.
//
// Here the patient's infection-control status is a state machine. A patient
// under `isolated` can only reach `cleared` through `pending_clearance`
// after the required negatives — there is no isolated -> cleared shortcut,
// and `cleared` is the only state from which a normal-ward transfer is
// allowed. A bounded counter records consecutive negatives; one positive
// resets it, so "two negatives" can't be faked by a single lucky swab.
//
//   soma verify examples/corpus/medical/isolation.cell
//   soma test   examples/corpus/medical/isolation.cell
//   soma run    examples/corpus/medical/isolation.cell demo
// ============================================================================

cell Isolation {
    face {
        signal admit_isolated(patient: String) -> Map
        signal test_result(patient: String, negative: Bool) -> Map
        signal request_clearance(patient: String) -> Map
        signal clear(patient: String) -> Map
        signal transfer_to_ward(patient: String) -> Map
        signal negatives(patient: String) -> Int
        signal iso_state(patient: String) -> String
        signal demo() -> String
    }

    memory {
        // Consecutive negative tests. Two are required to clear; a single
        // positive resets to 0. Bounded — you cannot fabricate a tally.
        neg_streak: Map<String, Int> [persistent, consistent]
        invariant neg_streak >= 0 && neg_streak <= 2
    }

    // A patient cannot jump from `isolated` to `cleared`. Clearance goes
    // through `pending_clearance`, and only `cleared` allows a normal-ward
    // transfer. `discharged_safe` is terminal.
    state precautions {
        initial: isolated
        isolated          -> pending_clearance   // two negatives documented
        pending_clearance -> cleared             // infection prevention sign-off
        pending_clearance -> isolated            // a positive reset
        cleared           -> on_ward             // safe to move to a shared bay
        on_ward           -> discharged_safe     // terminal
    }

    on admit_isolated(patient: String) {
        neg_streak.set(patient, 0)
        return map("ok", true, "state", get_status(patient))
    }

    // Record a test. Two consecutive negatives qualify for clearance; any
    // positive resets the streak and (if pending) sends the patient back to
    // isolation.
    on test_result(patient: String, negative: Bool) {
        let cur = neg_streak.get(patient) ?? 0
        if negative {
            let next = min(cur + 1, 2)
            neg_streak.set(patient, next)
            return map("ok", true, "negatives", next)
        }
        neg_streak.set(patient, 0)
        let st = get_status(patient)
        if st == "pending_clearance" { transition(patient, "isolated") }
        return map("ok", true, "negatives", 0, "reset", true)
    }

    // Move toward clearance — only with the two negatives on record.
    on request_clearance(patient: String) {
        let streak = neg_streak.get(patient) ?? 0
        if streak < 2 {
            return map("ok", false, "reason",
                "needs 2 negatives, has {streak} — clearance blocked")
        }
        let r = try { transition(patient, "pending_clearance") }
        if r.error != () { return map("ok", false, "reason", "{r.error}") }
        return map("ok", true, "state", get_status(patient))
    }

    on clear(patient: String) {
        let r = try { transition(patient, "cleared") }
        if r.error != () {
            return map("ok", false, "reason", "cannot clear: {r.error}")
        }
        return map("ok", true, "state", get_status(patient))
    }

    // Transfer to a shared ward is allowed ONLY from cleared — a still-
    // isolated patient cannot reach a normal bay.
    on transfer_to_ward(patient: String) {
        let r = try { transition(patient, "on_ward") }
        if r.error != () {
            return map("ok", false, "reason", "isolation breach blocked: {r.error}")
        }
        return map("ok", true, "state", get_status(patient))
    }

    on negatives(patient: String) { return neg_streak.get(patient) ?? 0 }
    on iso_state(patient: String) { return get_status(patient) }

    on demo() {
        admit_isolated("pt-1")
        let breach = transfer_to_ward("pt-1")   // still isolated — REFUSED
        test_result("pt-1", true)
        test_result("pt-1", false)              // positive resets the streak
        test_result("pt-1", true)
        test_result("pt-1", true)               // two in a row now
        request_clearance("pt-1")
        clear("pt-1")
        let ok = transfer_to_ward("pt-1")
        print("early transfer: {breach}")
        print("after clearance: {ok}")
        return "demo done"
    }
}

cell test IsolationTests {
    rules {
        assert admit_isolated("a").ok == true

        // a still-isolated patient cannot be moved to a shared ward
        assert transfer_to_ward("a").ok == false
        assert_fails transition("a", "on_ward")
        assert_fails transition("a", "cleared")

        // one negative is not enough
        assert test_result("a", true).negatives == 1
        assert request_clearance("a").ok == false

        // a positive resets the streak — no faking two with one swab
        assert test_result("a", false).negatives == 0
        assert test_result("a", true).negatives == 1
        assert test_result("a", true).negatives == 2

        // now clearance, sign-off, and a safe transfer
        assert request_clearance("a").ok == true
        assert clear("a").ok == true
        assert transfer_to_ward("a").ok == true
        assert iso_state("a") == "on_ward"

        // the streak counter cannot be inflated past the requirement
        assert_fails neg_streak.set("a", 3)
        assert_fails neg_streak.set("a", -1)
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check isolation.cell
✓ All checks passed.
$ soma test isolation.cell
test IsolationTests ...
  ✓ assert admit_isolated("a").ok == true
  ✓ assert transfer_to_ward("a").ok == false
  ✓ assert_fails transition("a", "on_ward") — raised invalid transition: isolated → on_ward. Current state: 'isolated'. Valid targets: [pending_clearance]
  ✓ assert_fails transition("a", "cleared") — raised invalid transition: isolated → cleared. Current state: 'isolated'. Valid targets: [pending_clearance]
  ✓ assert test_result("a", true).negatives == 1
  ✓ assert request_clearance("a").ok == false
  ✓ assert test_result("a", false).negatives == 0
  ✓ assert test_result("a", true).negatives == 1
  ✓ assert test_result("a", true).negatives == 2
  ✓ assert request_clearance("a").ok == true
  ✓ assert clear("a").ok == true
  ✓ assert transfer_to_ward("a").ok == true
  ✓ assert iso_state("a") == "on_ward"
  ✓ assert_fails neg_streak.set("a", 3) — raised memory invariant violated on 'neg_streak': neg_streak >= 0 && neg_streak <= 2 — rejected write of 3 (key "a"); the slot is unchanged
  ✓ assert_fails neg_streak.set("a", -1) — raised memory invariant violated on 'neg_streak': neg_streak >= 0 && neg_streak <= 2 — rejected write of -1 (key "a"); the slot is unchanged

15 tests: 15 passed, 0 failed
$ soma verify isolation.cell
Verifying isolation.cell...

State machine 'precautions': 5 states, initial 'isolated'
  States: [cleared, discharged_safe, isolated, on_ward, pending_clearance]

  ✓ 5 states, 5 transitions
  ✓ all states reachable from 'isolated'
  ✓ terminal states: [discharged_safe]
  ✓ no deadlocks
  ✓ liveness: every state can eventually reach a terminal state
  ✓ think-isolated: CTL safety properties hold regardless of LLM output (4 handlers, 4 literal transitions, 0 dynamic)
  ✓ termination: all 8 handlers structurally terminate
  ⚠ refinement: declared transition `on_ward → discharged_safe` is never reached by any handler — spec may be aspirational or stale
  ✓ refinement: handler `test_result` ⟶ {pending_clearance → isolated [if st == "pending_clearance"]}
  ✓ refinement: handler `request_clearance` ⟶ {pending_clearance}
  ✓ refinement: handler `clear` ⟶ {cleared}
  ✓ refinement: handler `transfer_to_ward` ⟶ {on_ward}
  · transition sources: `request_clearance`, `clear`, `transfer_to_ward` call transition(id, target) without declaring the source — the runtime refuses a move the graph forbids, but the proof does not see where the handler starts; `transition(id, from, to)` names the edge (checked by `soma check`, refused at run time from any other state)

Isolation/invariants:

  ✓ invariant neg_streak >= 0 && neg_streak <= 2 — writer 'admit_isolated' proven (writes 0)
  ✓ invariant neg_streak >= 0 && neg_streak <= 2 — writer 'test_result' proven by induction (writes next)
  ✓ invariant neg_streak >= 0 && neg_streak <= 2 — writer 'test_result' proven (writes 0)

14 passed, 1 warnings, 0 failures

  Temporal properties for 'precautions':
  ✓ deadlock_free — no deadlocks in any reachable state
Temporal: 1 passed, 0 failed
VERIFY OK
$ soma run isolation.cell demo
early transfer: {"ok": false, "reason": "isolation breach blocked: invalid transition: isolated → on_ward. Current state: 'isolated'. Valid targets: [pending_clearance]"}
after clearance: {"ok": true, "state": "on_ward"}
demo done

Builtins used

get_status, map, min, print, transition

Source file: /corpus/medical/isolation.cell · examples/corpus/medical/isolation.cell