Address book: Contact records stored in a Map slot, round-tripped through storage, and queried
Demonstrates record literals, dot-access, storing records in [persistent] map slots, and self-verifying tests.
A Soma program from the records part of the verified corpus: 49 lines, cells AddressBook, AddressBookTest. It passes soma check with soma 3.2.4.
How it is built
The cell AddressBook declares 4 signals in its face: add(name: String, email: String, city: String) -> String, get(name: String) -> Map, city_of(name: String) -> String and count() -> Int; keeps 1 memory slot: contacts (Map<String, Map>, persistent and consistent). Handlers: add (line 17), get (line 23), city_of (line 29) and count (line 34).
The file also holds 1 test cell (AddressBookTest) with 6 assertions, run by soma test.
The source
address_book.cell
// Address book: Contact records stored in a Map slot, round-tripped
// through storage, and queried. Demonstrates record literals, dot-access,
// storing records in [persistent] map slots, and self-verifying tests.
cell AddressBook {
face {
signal add(name: String, email: String, city: String) -> String
signal get(name: String) -> Map
signal city_of(name: String) -> String
signal count() -> Int
}
memory {
contacts: Map<String, Map> [persistent, consistent]
}
on add(name: String, email: String, city: String) {
let c = Contact { name: name, email: email, city: city }
contacts.set(name, c) // storage auto-serializes the record
return name
}
on get(name: String) {
let raw = contacts.get(name)
if raw == () { return map("error", "not found") }
return raw // comes back as a structured record
}
on city_of(name: String) {
let c = get(name)
return c.city ?? "unknown"
}
on count() {
return contacts.len
}
}
cell test AddressBookTest {
rules {
assert add("Ada", "[email protected]", "London") == "Ada"
assert add("Linus", "[email protected]", "Helsinki") == "Linus"
assert get("Ada").email == "[email protected]"
assert city_of("Linus") == "Helsinki"
assert city_of("Ghost") == "unknown"
assert count() == 2
}
}What the toolchain says
$ soma check address_book.cell
✓ All checks passed.$ soma test address_book.cell
test AddressBookTest ...
✓ assert add("Ada", "[email protected]", "London") == "Ada"
✓ assert add("Linus", "[email protected]", "Helsinki") == "Linus"
✓ assert get("Ada").email == "[email protected]"
✓ assert city_of("Linus") == "Helsinki"
✓ assert city_of("Ghost") == "unknown"
✓ assert count() == 2
6 tests: 6 passed, 0 failed$ soma verify address_book.cell
Verifying address_book.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)