Zebra

Version 0.9.0 · Windows, Linux, macOS

A readable language that keeps its promises.

Zebra reads like Python and checks like Eiffel. Contracts, nil tracking and exhaustive pattern matching are part of the language, and the compiler that enforces them is written in Zebra. Programs compile to native executables.

Install Read the book

account.zbr
class Account
    var balance: int = 0

    invariant
        balance >= 0

    def deposit(amount: int)
        require
            amount > 0
        ensure
            balance == old balance + amount
        balance += amount

    def withdraw(amount: int)
        require
            amount <= balance
        balance -= amount

def main()
    var acct = Account()
    acct.deposit(100)
    acct.withdraw(30)
    print("balance: ${acct.balance}")
    acct.withdraw(500)
    print("not reached")
$ zebra account.zbr
balance: 70
panic: require failed at account.zbr:16: in 'withdraw'

The broken promise is named by file, line and method. Contracts stay on in release builds; --turbo is the one switch that removes them.

A short tour

Every program on this page was compiled by a script and its output compared with what is shown here. The compiler's own progress lines, which it prints on stderr, are left out.

Nil is part of the type

A str? may be nil; a str may not. Reading through a value that might be nil is a compile error that tells you the three ways out.

def findUser(id: int): str?
    if id == 1
        return "ada"
    return nil

def main()
    var name = findUser(2)
    print(name.len)
nil_bad.zbr:8:11: error: 'name' may be nil here, so `.len` cannot be read from it -- check it first (if x != nil ...), unwrap it (x!), or give a default (x orelse "")

After a check, the compiler knows:

def findUser(id: int): str?
    if id == 1
        return "ada"
    return nil

def main()
    var name = findUser(1)
    if name != nil
        print("hello, ${name}")
    print(findUser(2) orelse "nobody")
hello, ada
nobody

Unions you cannot forget a case of

A branch over a union must handle every variant or say else. Add a variant later and each branch that misses it stops compiling.

struct Rect
    var w: float
    var h: float

union Shape
    circle: float
    rect: Rect
    point

def area(s: Shape): float
    branch s
        on Shape.circle as r   return 3.14159 * r * r
        on Shape.rect as b     return b.w * b.h
        on Shape.point         return 0.0

def main()
    var shapes = [Shape.circle(1.0), Shape.rect(Rect(w: 2.0, h: 3.0)), Shape.point()]
    for s in shapes
        print(area(s))
3.14159
6.0
0.0

Errors you can see at the call

A function that can fail says throws. The caller either passes the error on with ? or handles it with catch; there is no third, silent option.

def parsePort(s: str): int throws
    var n = s.tryInt()
    if n == nil
        raise "not a number: ${s}"
    if n < 1 or n > 65535
        raise "port out of range: ${n}"
    return n

def main()
    print(parsePort("8080")?)
    var fallback = parsePort("99999") catch 80
    print(fallback)
    var p = parsePort("http") catch |e| e.message.len
    print(p)
8080
80
18

Leave the ? off and the program does not compile:

def half(n: int): int throws
    if n % 2 != 0
        raise "odd: ${n}"
    return n / 2

def main()
    var h = half(10)
    print(h)
errors_bad.zbr:7: error: throws call needs '?' (auto error-propagation was removed; add '?')

Lists, lambdas and loops

Collections carry the usual functional methods. A lambda is def(x) = expr, and for i, x in xs gives the index first.

def main()
    var words = ["zebra", "okapi", "quagga", "giraffe"]
    var long: List(str) = words.filter(def(w) = w.len > 5)
    var loud: List(str) = long.map(def(w) = w.upper())
    print(loud.join(", "))
    for i, w in words
        print("${i}: ${w}")
QUAGGA, GIRAFFE
0: zebra
1: okapi
2: quagga
3: giraffe

Tests beside the code

Any function whose name starts with test_ is a test, in the same file as the code it tests. zebra test runs them. The third test below is wrong on purpose, to show what a failure prints: both sides of the comparison.

def slug(title: str): str
    return title.trim().lower().replace(" ", "-")

def test_lowercases()
    assert slug("Hello World") == "hello-world"

def test_trims()
    assert slug("  zebra  ") == "zebra"

def test_keeps_digits()
    assert slug("Rev 2") == "rev2"
$ zebra test slug.zbr
RUN: test_lowercases
PASS: test_lowercases
RUN: test_trims
PASS: test_trims
RUN: test_keeps_digits
FAIL: test_keeps_digits: assert failed at slug.zbr:11: left == right -- left: "rev-2", right: "rev2"

2 passed, 1 failed

The run exits 1 when any test fails, so it can gate a build. A test can be tagged and run on its own, and zebra test --coverage writes line coverage for the run.

What is in the box

Everything below is part of the 0.9.0 release. Each item is in the language reference, and the names are taken from the compiler's own list of what it accepts.

Checks

  • Contracts: require, ensure, invariant, old
  • Nil in the type: T?, orelse, !
  • Unions and an exhaustive branch
  • Errors in the signature: throws, ?, catch
  • Constrained types: type Port = int where ...

Language

  • Classes, and structs as value types with except updates
  • Interfaces and mixins, in place of inheritance
  • Generics, with constraints
  • Lambdas and closures
  • Generators, with yield
  • Modules (use) and namespaces

Library

  • Files, paths, JSON, CSV, regular expressions
  • HTTP client and server, WebSockets, TCP and UDP
  • SQLite, built in
  • Threads, channels and a thread pool
  • Dates, hashing, cryptography, compression
  • Desktop GUIs, through libui-ng

Tools

  • zebra test, with line coverage
  • zebra fmt, zebra repl
  • zebra build, with build files written in Zebra
  • zebra debug and a language server, for editors
  • An allocation report: --census
  • Calling C: extern

The compiler explains itself

Zebra looks enough like Python that Python habits come along. Where a habit does not carry over, the error says what to write instead.

def main()
    var c = true
    var x = 1 if c else 2
    print(x)
py_ternary.zbr:3:15: error: there is no `a if c else b` in Zebra: the conditional expression is the call form `if(c, a, b)` (QUICKSTART §1.5)
def main()
    var xs = [1, 2, 3]
    print(xs.map(x => x * 2))
py_arrow.zbr:3:20: error: there is no `x => expr` arrow lambda in Zebra: write `def(x) = expr` (QUICKSTART §1.5)
def main()
    print(-7 / 2)
    print(-7 % 2)
-3
-1

One habit that does not raise an error: integer division truncates toward zero, as in C, not toward negative infinity as in Python.

Zebra for Python programmers puts Python programs and their Zebra versions side by side, both checked to print the same thing.

Desktop apps in one file

GUI programs use the Model-View-Update shape: a model, a union of messages, a pure update, and a view that draws the model. Widgets are native, through libui-ng.

GUI support is the newest part of Zebra. The test suite builds GUI programs and runs them headless; it cannot click them, so rendering is checked by hand.

struct Counter
    var count: int

union Msg
    inc
    dec
    reset

def init(): Counter
    return Counter(count: 0)

def update(model: Counter, msg: Msg): Counter
    branch msg
        on Msg.inc   return Counter(count: model.count + 1)
        on Msg.dec   return Counter(count: model.count - 1)
        on Msg.reset return Counter(count: 0)

def view(g: Gui, model: Counter)
    g.text("Count: " + model.count.toString())
    g.separator()
    g.button("+", Msg.inc)
    g.sameLine()
    g.button("-", Msg.dec)
    g.sameLine()
    g.button("Reset", Msg.reset)

def main()
    Gui.run("Counter", 400, 200, init, update, view)

Status, plainly

Release
0.9.0, published November 2026. Zebra is before 1.0: the language can still change, and each change is listed in the changelog.
Platforms
Prebuilt for Windows x86-64, Linux x86-64 and macOS on Apple silicon.
How it compiles
To native executables, today through Zig. The Zig toolchain ships inside the release, so there is nothing else to install. A second backend, through V, is in progress.
Testing
The 0.9.0 release passed the project's full daily test run: 60 gates, among them 1,058 smoke tests, 466 programs whose printed output must match a recorded baseline, and the compiler rebuilding itself byte-for-byte.
Known bugs
Public, in BUGS.md.
License
MIT, as is the bundled Zig toolchain.

Install

In PowerShell:

irm https://raw.githubusercontent.com/torial/zebra-language/main/install/install.ps1 | iex

Or download an archive from the releases page and see the install notes. Later, zebra up updates an installed release.

Your first program

def main()
    print("hello, zebra")
zebra hello.zbr
hello, zebra

zebra hello.zbr compiles and runs, printing a few progress lines of its own first. zebra -c hello.zbr checks the program without building it.