Haze is a design by contract, multi-paradigm programming language, aiming to deliver performance, memory safety, and minimal-cost abstractions. This enables developers to write safe and maintainable code, with contract-enforced correctness. All contracts are checked at compile time, moreover, the contracts are composable.

Memory is managed via a highly optimized Immix collector, which is thread-safe, therefore no manual memory management is required.

Programs compile to statically linked binaries by default through the use of its UQBE backend. UQBE is a modernized fork of QBE, capable of operating on Windows in addition to POSIX platforms.

Haze's license is GPL-3.0-only.

UQBE's license is MIT, as per the original.

Contract composition

contract IsEven(n i32) {
    pre n % 2 == 0
}

contract SomeContract(n i32) : IsEven(n) {
    post r => r > 0
}

fn some_function(n i32) : SomeContract(n) {
    print(n)
}

fn main() {
    some_function(10)
}

Pure functions, contracts, and recursion

pure fn gcd(a i32, b i32) i32
    : pre a > 0
    : pre b >= 0
    : post r => a % r == 0 && b % r == 0
    => if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }

fn main() {
    print(gcd(48, 18))
}

Composable pipelines

use seq {range, map, filter, fold}

fn add(a i32, b i32) i32 => a + b

fn main() {
    const total = range(1, 11)
        .filter(fn(n) => n % 2 == 0)
        .map(fn(n) => n * n)
        .fold(0, add)

    print(total)
}

Functions as values

pure fn compose(f fn(i32) i32, g fn(i32) i32, x i32) i32
    : post r => r == f(g(x))
    => f(g(x))

fn adder(n i32) fn(i32) i32
    => fn(x i32) i32 => x + n

fn main() {
    const inc = fn(x i32) i32 => x + 1
    const square = fn(x i32) i32 => x * x
    const add_ten = adder(10)

    print(compose(inc, square, 5))
    print(add_ten(32))
}

Patterns over data

type Shape =
    | Circle(radius i32)
    | Rect(width i32, height i32)

pure fn area(s Shape) i32 => match s {
    Circle(r) => r * r * 3
    Rect(w, h) => w * h
}

fn main() {
    const shape = Rect(8, 6)
    print(area(shape))
}

Compile-time proof

pure fn factorial(n i32) i32
    : pre n >= 0
    : pre n <= 12
    : post r => r >= 1
    => if n <= 1 {
        1
    } else {
        n * factorial(n - 1)
    }

const answer i32 = factorial(12)

fn main() {
    print(answer)
}