Contents

Every Haze file is a module. There is no separate module declaration and no package manifest. The compiler loads the entry file, follows its use declarations, parses each dependency, and merges the result into one checked program. Understanding the file-to-module mapping is therefore the whole of the module system.

Files and Module Names#

The entry module is named after the entry file's stem. A dependency module is named by its path relative to the entry file's directory, with the .hz extension removed. A file named core.hz inside a directory takes the directory's name instead.

File Module name when loaded from the entry directory
main.hz main (the entry module)
orders.hz orders
lib/pair_mod.hz lib/pair_mod
std/seq/core.hz std/seq
a/b/c/deeper.hz a/b/c/deeper

The module name is used for symbol qualification and for diagnostics. The entry module has no prefix; every symbol from another module is internally prefixed with the module name, which is why two modules can define the same private helper without a collision.

Import Syntax#

A use declaration names a module path. With braces it imports specific public names. Without braces it imports every public name as a wildcard. There is no aliasing and no renaming.

use std/seq {map, filter, fold}
use std/env {args}
use orders

An explicit import makes the selected names available unqualified. A wildcard import makes all public names available, but an explicit name takes precedence if both forms are present. When two wildcard imports export the same name, the first matching module in declaration order wins; avoid that situation by importing explicitly.

A module path uses / separators. A qualified access after the module is loaded uses ::, as in seq::fold(...). Because module paths may contain several segments, explicit imports are usually more readable than qualified access for anything beyond a single-segment module name.

Relative Imports#

A module path may start with . or ... The path is resolved relative to the importing file, not the process working directory and not the entry file.

use ./local_helper {helper}
use ../shared/types {Config}
use ../../modules/some_module {root_value}

The conventional directory module is core.hz. Thus use . resolves to ./core.hz in the importing file's directory, and use .. resolves to ../core.hz. A path such as use ../deep resolves to ../deep.hz or ../deep/core.hz.

File Lookup#

For a module path p and an importing file in directory d, the compiler tries two locations in order. The first is d/p.hz. The second is d/p/core.hz. The first existing regular file wins.

<importing directory>/<module path>.hz
<importing directory>/<module path>/core.hz

This rule applies to relative imports as well. A directory module is not special beyond the core.hz convention; it is simply a path whose final component is a directory containing a file named core.hz.

Visibility#

Declarations are private by default. pub makes a top-level function, type, constant, contract, or trait visible to other modules. Trait methods and implementation methods may also be marked pub. An impl block itself is not public; the trait and the target type determine whether the implementation is usable elsewhere.

const INTERNAL_LIMIT i32 = 100

pub type Order {
    id    i32
    qty   i32
    price i32
}

pub pure fn total(order Order) i32 => order.qty * order.price

Importing a private name is an error. Qualified access to a private name is also an error. A wildcard import silently skips private names; an explicit import of a private name reports that the name is private to its module.

The Standard Library#

The standard library lives under std/ in the hazelib directory next to the compiler. An import whose path begins with std/ is first looked up relative to the importing file, then under the standard library root. This means a local module can shadow a standard module, which is useful for testing but should be done deliberately.

use std/seq {map, filter, fold}
use std/env {args}
use std/fs {read_file, write_file}

The guide documents the core language rather than the standard library. The standard modules follow the same visibility and import rules as user modules; consult their source for the available names.

The Entry Point and Arguments#

The entry module must define main. main takes no parameters. Process arguments are available through the environment module as a List[str]; the runtime initializes them before the entry function runs.

use std/env {args}

fn main() {
    for argument in args() {
        print(argument)
    }
}

The first element of args() is the program path, matching the usual C convention. Invalid UTF-8 arguments are omitted rather than replaced.

Tests and Modules#

A test "name" { ... } block is attached to the module that contains it. A normal compile ignores test blocks. The hz test command treats each file it is given as an entry module, so tests in a directory are run file by file. Tests in imported modules are not run when the importer is compiled.

test "total multiplies quantity by price" {
    const order = Order { id = 1, qty = 3, price = 10 }
    assert total(order) == 30
}

The Tests chapter covers the runner and the rules for assert.

Cycles#

Module loading is breadth-first and memoized by module name. A cycle such as A importing B and B importing A does not produce a special diagnostic; the second request for an already loaded module is skipped, and resolution runs over the complete module table. Declarations are still checked in their own module, so a cycle is not a way to bypass visibility or type rules. Avoid cycles when a simpler dependency direction is available; they make the module graph harder to reason about.

Guidelines for Module Boundaries#

A module should have a single reason to change. Export the smallest surface that callers need, keep helpers private, and let contracts define the obligations of the exported functions. When two modules need the same type, put the type in a dedicated module and import it from both rather than defining two structurally similar types. Types are nominal, so two records with identical fields are still different types and cannot be substituted for one another.