Why Haze
The design pressures behind Haze and the principles that shape idiomatic code.
Contents
Haze exists because most programs describe their most important rules in three incompatible places: comments that drift, validation code that is easy to skip, and tests that can only cover the cases the author imagined. The language starts from a different premise. A function's obligations are part of its signature, a type's valid states are part of its declaration, and a failure is a value the caller must handle. Those ideas are usually called design by contract, refinement typing, and errors as values. Haze makes them the ordinary way to write code.
The Three Commitments#
The language is organized around three commitments, and nearly every feature serves one of them.
| Commitment | What it means in practice | Language support |
|---|---|---|
| Contracts are executable | Preconditions, postconditions, and invariants are written beside the code they constrain and are checked where the compiler can, at runtime where it cannot, generally this is of no consequence, though, as failure values are represented through a lightweight Result[T, E] |
pre, post, named contracts, type invariants, loop invariants |
| Invalid states are unrepresentable | A value either satisfies its type or cannot be built; a missing value is Option, a failed operation is Result |
refinement types, sum types, Option, Result, checked construction |
| Memory is not the programmer's problem | Allocation and reclamation are automatic and precise, without a borrow checker ceremony | tracing collector, managed references, immutable-first data modelling |
A fourth, softer commitment runs through the syntax: functional composition should be available without giving up imperative control flow. Haze has for, while-style loops, mutation, and early return, and it also has first-class functions, method-call syntax, pattern matching, and collection pipelines. Idiomatic Haze uses each where it is clearest rather than treating one paradigm as a team.
Contracts Before Convenience#
A contract is not a comment and not a test. It is a boolean expression over the function's parameters and result, written in the same expression language as the body. The compiler evaluates contracts with constant arguments, rejects violations it can prove, and emits runtime checks for the rest. That means a contract has three distinct lives: it documents intent, it participates in compile-time verification, and it guards the boundary at runtime.
The practical consequence is that a public function should tell the truth about its domain. A function that divides must say that the divisor is nonzero. A function that indexes a list must say that the index is in range. A function that returns a sorted list should say so in its postcondition. When those statements are part of the signature, callers can rely on them, and the compiler can use them to reject impossible calls before the program runs.
Failure Is a Value#
Haze has no unwrap, no exceptions, and no implicit panic path for expected failures. An operation that can fail returns Result[T, E]; an operation that can be absent returns Option[T]. The caller recovers the value with match or is, and the type checker keeps the failure case visible. This is more verbose than an exception in the smallest examples and dramatically less surprising in the largest ones. It also composes with contracts: a checked construction either produces a valid value or returns an error to the caller, so a broken invariant never becomes a corrupt object.
Memory Safety Without a Borrow Checker#
Haze uses a precise tracing Immix collector. Managed values such as lists, records, sum values, closures, and byte buffers are allocated on the Haze heap and reclaimed when they become unreachable. The compiler emits root maps at every collection point, so the collector does not scan registers or guess. The language does not require manual free, reference counting, or lifetime annotations. The design goal is that ordinary application code never talks about memory management unless it is deliberately crossing into C.
Design Principles#
The following principles explain most of the language's choices and most of the advice in the idiomatic chapter.
| Principle | Consequence |
|---|---|
| Prefer total functions | Model missing and failing cases in the return type instead of panicking or returning sentinel values |
| Prefer narrow types | Use refinement types and sum types to make invalid values impossible to construct |
| Prefer explicit obligations | Write preconditions and postconditions for behaviour that callers depend on |
| Prefer immutable data flow | Build new values rather than mutating shared ones; mutate only local accumulators |
| Prefer composition | Use small pure functions, higher-order functions, and pipelines before reaching for control-flow machinery |
| Prefer Haze over FFI | Cross into C only for capabilities the language does not provide, and wrap the boundary in checked Haze APIs |
| Prefer evidence over assertion | Use tests for behaviour and contracts for invariants; do not duplicate one as the other |