soma.cell

Rot13 — self-inverse letter rotation

Rotates each letter by 13, preserving case; applying it twice restores the original. Demonstrates two alphabet lookups (lower/upper), index_of for the source position, and the involution property in a test. Run: soma run rot13.cell --signal rot13 "Hello, World"

A Soma program from the text part of the verified corpus: 45 lines, cells Rot13, Rot13Test. It passes soma check with soma 3.2.4.

How it is built

The cell Rot13 declares 1 signal in its face: rot13(text: String) -> String. Handlers: rot13 (line 16).

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

Generated from soma describe on this file.

The source

rot13.cell

// ============================================================================
// ROT13 — self-inverse letter rotation
// ============================================================================
//
// Rotates each letter by 13, preserving case; applying it twice restores the
// original. Demonstrates two alphabet lookups (lower/upper), index_of for the
// source position, and the involution property in a test.
//
// Run: soma run rot13.cell --signal rot13 "Hello, World"

cell Rot13 {
    face {
        signal rot13(text: String) -> String
    }

    on rot13(text: String) {
        let lower = "abcdefghijklmnopqrstuvwxyz"
        let upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        let out = ""
        for i in range(0, len(text)) {
            let c = text[i]
            let lp = index_of(lower, c)
            let up = index_of(upper, c)
            if lp >= 0 {
                out = out + lower[(lp + 13) % 26]
            } else if up >= 0 {
                out = out + upper[(up + 13) % 26]
            } else {
                out = out + c
            }
        }
        return out
    }
}

cell test Rot13Test {
    rules {
        assert rot13("hello") == "uryyb"
        assert rot13("Hello, World") == "Uryyb, Jbeyq"
        assert rot13(rot13("Self inverse 123!")) == "Self inverse 123!"
        assert rot13("ABC") == "NOP"
        assert rot13("123") == "123"
    }
}

What the toolchain says

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

$ soma check rot13.cell
warning: `out` grows by concatenation inside a loop — each step copies the whole value, so the loop is quadratic (8 MB took 32 s, a list + join() 30 ms): push the pieces to a List and `join(parts, "")` once
  --> rot13.cell:25:17
   |
25 |                 out = out + lower[(lp + 13) % 26]
   |                 ^
✓ 1 warning(s), no errors.
$ soma test rot13.cell
test Rot13Test ...
  ✓ assert rot13("hello") == "uryyb"
  ✓ assert rot13("Hello, World") == "Uryyb, Jbeyq"
  ✓ assert rot13(rot13("Self inverse 123!")) == "Self inverse 123!"
  ✓ assert rot13("ABC") == "NOP"
  ✓ assert rot13("123") == "123"

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

index_of, len, range

Source file: /corpus/text/rot13.cell · examples/corpus/text/rot13.cell