Contents

A Haze program is a module with a main function. The smallest useful program prints a line and exits. Save the following as hello.hz.

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

Compile and run it with the -run flag, or compile it and execute the result separately.

hz -run hello.hz

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

Functions and Return Types#

A function declaration names its parameters with types after the names. The return type follows the parameter list. A function whose return type is omitted returns void.

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

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

An expression-bodied function uses => and returns the expression. 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
}

The First Contract#

The point of Haze is that a function can state what it requires and what it guarantees. A precondition is introduced with : pre, and a postcondition with : post name =>. The name after post is bound to the returned value inside the postcondition.

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 the constant call half(10) and accepts it. Changing the call to half(7) produces a compile-time error that points at the call and at the failing contract. The error is not a runtime exception; the program is rejected before code generation.

error: precondition failed at compile time

The same precondition becomes a runtime check when the argument is not known at compile time. A function can therefore be called with a value read from a file, and the check still protects the body. The -release flag removes runtime contract checks, but it does not disable compile-time evaluation.

Values and Decisions#

Local bindings use const for a binding that will not be reassigned and var for one that will. 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 an else is a statement and produces no value. Conditions must be bool; there is no implicit truthiness.

Missing and Failing Values#

Haze does not use null for absence. Option[T] has the constructors Some(value) and None. Result[T, E] has Success(value) and Error(reason). Both are matched or tested; neither can be unwrapped silently.

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)
    }
}

The ? operator propagates a Result error to the enclosing function when the function returns a compatible Result. The enclosing function must make the failure part of its own signature, so the possibility of failure is never hidden.

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

A Small Complete Program#

The following program combines a record, a contract, a list pipeline, and a match. It is deliberately small, but every construct in it scales to larger code.

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)
}

The call to curve(scores[0]) is accepted because the postcondition of the filter pipeline is not needed here: 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 compiler would require the caller to know that the value satisfies it or to handle the failure explicitly.

Where the Language Goes Next#

The Language Tour shows how records, sum types, generics, traits, and contracts fit together in one program. The Design by Contract chapter is the conceptual centre of the guide, and Writing Idiomatic Haze Code explains the style that the rest of the language is designed to support.