soma.cell

Playlist as an ordered list of Track records

Demonstrates building, reordering, and summarizing a list of records, plus sort_by/pluck over record fields and total-duration aggregation.

A Soma program from the records part of the verified corpus: 50 lines, cells Playlist, PlaylistTest. It passes soma check with soma 3.2.4.

How it is built

The cell Playlist declares 5 signals in its face: tracks() -> List, total_seconds() -> Int, by_artist(artist: String) -> List, longest() -> Map and titles_sorted() -> List. Handlers: tracks (line 14), total_seconds (line 23), by_artist (line 27), longest (line 31) and titles_sorted (line 35).

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

Generated from soma describe on this file.

The source

playlist.cell

// Playlist as an ordered list of Track records. Demonstrates building,
// reordering, and summarizing a list of records, plus sort_by/pluck over
// record fields and total-duration aggregation.

cell Playlist {
    face {
        signal tracks() -> List
        signal total_seconds() -> Int
        signal by_artist(artist: String) -> List
        signal longest() -> Map
        signal titles_sorted() -> List
    }

    on tracks() {
        return list(
            Track { title: "Aurora",  artist: "Nim",  secs: 210 },
            Track { title: "Boreal",  artist: "Nim",  secs: 185 },
            Track { title: "Cinder",  artist: "Vox",  secs: 240 },
            Track { title: "Dune",    artist: "Vox",  secs: 175 }
        )
    }

    on total_seconds() {
        return sum(tracks() |> pluck("secs"))
    }

    on by_artist(artist: String) {
        return tracks() |> filter(t => t.artist == artist) |> pluck("title")
    }

    on longest() {
        return tracks() |> max_by("secs")
    }

    on titles_sorted() {
        return tracks() |> sort_by("secs", "desc") |> pluck("title")
    }
}

cell test PlaylistTest {
    rules {
        assert total_seconds() == 810
        assert len(by_artist("Nim")) == 2
        assert by_artist("Nim")[0] == "Aurora"
        assert longest().title == "Cinder"
        assert titles_sorted()[0] == "Cinder"
        assert titles_sorted()[3] == "Dune"
    }
}

What the toolchain says

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

$ soma check playlist.cell
✓ All checks passed.
$ soma test playlist.cell
test PlaylistTest ...
  ✓ assert total_seconds() == 810
  ✓ assert len(by_artist("Nim")) == 2
  ✓ assert by_artist("Nim")[0] == "Aurora"
  ✓ assert longest().title == "Cinder"
  ✓ assert titles_sorted()[0] == "Cinder"
  ✓ assert titles_sorted()[3] == "Dune"

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

filter, len, list, max_by, pluck, sort_by, sum

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