Contents

A Haze program is a module with a main function. Save the following as hello.hz:

fn main() {
    print("hello, haze")
}

Compile and run it:

hz -run hello.hz

The compiler produces an executable next to the source. hz hello.hz -o hello names the output explicitly; running ./hello produces the same line.

Functions#

Parameters are written as name followed by type. The return type follows the parameter list. A function without a return type returns void.

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

fn main() {
    print(add(2, 3))
}

An expression-bodied function uses =>. A block-bodied function uses braces and returns with return or by leaving a final expression as the result.

fn absolute(n i32) i32 {
    if n < 0 {
        return -n
    }
    return n
}

Contracts#

A precondition is introduced with : pre. A postcondition with : post name => binds the returned value to name.

fn half(n i32) i32
    : pre n % 2 == 0
    : post r => r * 2 == n
=> n / 2

fn main() {
    print(half(10))
}

The compiler evaluates the precondition for half(10) and accepts it. half(7) produces a compile-time error pointing at the call and the failing contract. The program is rejected before code generation.

error: precondition failed at compile time

A precondition becomes a runtime check when the argument is not known at compile time. -release removes runtime contract checks but does not disable compile-time evaluation.

Bindings#

const declares a binding that is not reassigned; var declares one that is. The type annotation is optional when the initializer determines the type.

fn describe(n i32) str {
    const parity = if n % 2 == 0 { "even" } else { "odd" }
    return parity
}

An if with both branches is an expression. An if without else is a statement and produces no value. Conditions must be bool.

Option and Result#

Option[T] has the constructors Some(value) and None. Result[T, E] has Success(value) and Error(reason).

fn safe_div(a i32, b i32) Result[i32, str] {
    if b == 0 {
        return Error("division by zero")
    }
    return Success(a / b)
}

fn main() {
    match safe_div(10, 2) {
        Success(value) => print(value)
        Error(reason) => print(reason)
    }
}

? propagates a Result error to the enclosing function when that function returns a compatible Result.

fn ratio(a i32, b i32) Result[i32, str] {
    const quotient = safe_div(a, b)?
    return Success(quotient * 2)
}

A Complete Program#

use std/seq {filter, map}

type Score {
    name  str
    value i32
}

pure fn passing(score Score) bool => score.value >= 50

contract Passing(score Score) {
    pre passing(score)
}

fn curve(score Score) Score
    : Passing(score)
    : post r => r.value >= score.value
=> Score { name = score.name, value = score.value + 5 }

fn main() {
    const scores = [
        Score { name = "Ada", value = 72 },
        Score { name = "Grace", value = 48 },
    ]

    const names = scores
        .filter(passing)
        .map(fn(score) => score.name)

    for name in names {
        print(name)
    }

    print(curve(scores[0]).value)
}

curve(scores[0]) is accepted because scores[0] is a constant record whose value is 72, so passing folds to true at compile time. If the argument were a runtime value, the precondition would become a runtime check, and the caller would have to know that the value satisfies it or handle the failure.