soma.cell

Typed record literals and runtime type tags

Demonstrates is_a/is_type on record literals, type_of, and dispatching on a record's _type field rather than a sum type — records carry their own type name.

A Soma program from the records part of the verified corpus: 46 lines, cells Shapes, ShapesTest. It passes soma check with soma 3.2.4.

How it is built

The cell Shapes declares 5 signals in its face: circle(r: Int) -> Map, square(s: Int) -> Map, kind(shape: Map) -> String, area(shape: Map) -> Int and is_circle(shape: Map) -> Bool. Handlers: circle (line 14), square (line 15), kind (line 17), area (line 24) and is_circle (line 30).

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

Generated from soma describe on this file.

The source

shapes_typed.cell

// Typed record literals and runtime type tags. Demonstrates is_a/is_type
// on record literals, type_of, and dispatching on a record's _type field
// rather than a sum type — records carry their own type name.

cell Shapes {
    face {
        signal circle(r: Int) -> Map
        signal square(s: Int) -> Map
        signal kind(shape: Map) -> String
        signal area(shape: Map) -> Int
        signal is_circle(shape: Map) -> Bool
    }

    on circle(r: Int)  { return Circle { r: r } }
    on square(s: Int)  { return Square { side: s } }

    on kind(shape: Map) {
        if is_a(shape, "Circle") { return "circle" }
        if is_a(shape, "Square") { return "square" }
        return "unknown"
    }

    // area uses the record tag to choose a formula (pi*r^2 truncated for circle)
    on area(shape: Map) {
        if is_a(shape, "Circle") { return floor(3.14159 * shape.r * shape.r) }
        if is_a(shape, "Square") { return shape.side * shape.side }
        return 0
    }

    on is_circle(shape: Map) {
        return is_type(shape, "Circle")
    }
}

cell test ShapesTest {
    rules {
        assert kind(circle(2)) == "circle"
        assert kind(square(3)) == "square"
        assert area(circle(2)) == 12
        assert area(square(4)) == 16
        assert is_circle(circle(1)) == true
        assert is_circle(square(1)) == false
        assert type_of(circle(1)) == "Map"
    }
}

What the toolchain says

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

$ soma check shapes_typed.cell
✓ All checks passed.
$ soma test shapes_typed.cell
test ShapesTest ...
  ✓ assert kind(circle(2)) == "circle"
  ✓ assert kind(square(3)) == "square"
  ✓ assert area(circle(2)) == 12
  ✓ assert area(square(4)) == 16
  ✓ assert is_circle(circle(1)) == true
  ✓ assert is_circle(square(1)) == false
  ✓ assert type_of(circle(1)) == "Map"

7 tests: 7 passed, 0 failed
$ soma verify shapes_typed.cell
Verifying shapes_typed.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)

Builtins used

floor, is_a, is_type, type_of

Source file: /corpus/records/shapes_typed.cell · examples/corpus/records/shapes_typed.cell