Geometry: Point and Rect records with computed properties
Demonstrates small value-records, deriving fields from nested records, and float tolerance in assertions.
A Soma program from the records part of the verified corpus: 46 lines, cells Geometry, GeometryTest. It passes soma check with soma 3.2.4.
How it is built
The cell Geometry declares 5 signals in its face: dist(a: Map, b: Map) -> Float, area(r: Map) -> Int, center(r: Map) -> Map, contains_pt(r: Map, p: Map) -> Bool and unit_square() -> Map. Handlers: dist (line 14), area (line 20), center (line 24), contains_pt (line 28) and unit_square (line 32).
The file also holds 1 test cell (GeometryTest) with 5 assertions, run by soma test.
The source
geometry.cell
// Geometry: Point and Rect records with computed properties. Demonstrates
// small value-records, deriving fields from nested records, and float
// tolerance in assertions.
cell Geometry {
face {
signal dist(a: Map, b: Map) -> Float
signal area(r: Map) -> Int
signal center(r: Map) -> Map
signal contains_pt(r: Map, p: Map) -> Bool
signal unit_square() -> Map
}
on dist(a: Map, b: Map) {
let dx = a.x - b.x
let dy = a.y - b.y
return sqrt(dx * dx + dy * dy)
}
on area(r: Map) {
return (r.hi.x - r.lo.x) * (r.hi.y - r.lo.y)
}
on center(r: Map) {
return Point { x: (r.lo.x + r.hi.x) / 2, y: (r.lo.y + r.hi.y) / 2 }
}
on contains_pt(r: Map, p: Map) {
return p.x >= r.lo.x && p.x <= r.hi.x && p.y >= r.lo.y && p.y <= r.hi.y
}
on unit_square() {
return Rect { lo: Point { x: 0, y: 0 }, hi: Point { x: 1, y: 1 } }
}
}
cell test GeometryTest {
rules {
assert abs(dist(Point { x: 0, y: 0 }, Point { x: 3, y: 4 }) - 5.0) < 0.0001
assert area(Rect { lo: Point { x: 0, y: 0 }, hi: Point { x: 4, y: 3 } }) == 12
assert center(unit_square()).x == 0.5
assert contains_pt(unit_square(), Point { x: 0.5, y: 0.5 }) == true
assert contains_pt(unit_square(), Point { x: 2, y: 0 }) == false
}
}What the toolchain says
$ soma check geometry.cell
✓ All checks passed.$ soma test geometry.cell
test GeometryTest ...
✓ assert abs(dist(Point { x: 0, y: 0 }, Point { x: 3, y: 4 }) - 5.0) < 0.0001
✓ assert area(Rect { lo: Point { x: 0, y: 0 }, hi: Point { x: 4, y: 3 } }) == 12
✓ assert center(unit_square()).x == 0.5
✓ assert contains_pt(unit_square(), Point { x: 0.5, y: 0.5 }) == true
✓ assert contains_pt(unit_square(), Point { x: 2, y: 0 }) == false
5 tests: 5 passed, 0 failed$ soma verify geometry.cell
Verifying geometry.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)