On this page
Modules
Modules help organize code into logical units and control visibility.
File-Level Modules
A file-level module declaration must appear at the start of the file. The module name is derived from the filename:
module exposing createUser, getName
fn createUser : String -> Int -> { name: String, age: Int }
fn createUser name age = { name = name, age = age }
fn getName : { name: String, age: Int } -> String
fn getName user = user.name
A file-level body is not indented. module exposing ... with no name says
"this whole file is the module", so its declarations are the file's own top
level and sit flush against the left margin. Indenting them is an error:
-- A file-level module body must NOT be indented
module exposing fm
fn fm : Int -> Int
fn fm n = n + 1
Compiled
The fix is one of two things, and the error says both: unindent the body, or
give the module a name — module MyModule exposing ... — which makes it an
inline block, and an inline block's body is indented. The two spellings mean
different things, so the layout is how you say which you meant.
A module file is type-checked like any other file. Its function bodies get the same inference and the same diagnostics the file you run gets, and an error inside one names the module file and the position in it, not the file that imported it.
A comment — with or without a blank line after it — annotates the declaration below it and does not end the module body. Everything after it is still a member:
module exposing celsiusToFahrenheit, fahrenheitToCelsius
-- Water freezes at 0 degrees Celsius, 32 Fahrenheit.
fn celsiusToFahrenheit : Float -> Float
fn celsiusToFahrenheit c = c * 1.8 + 32.0
-- And the other way round.
fn fahrenheitToCelsius : Float -> Float
fn fahrenheitToCelsius f = (f - 32.0) / 1.8
Exposing
Control what the module exports:
-- Expose specific items
module exposing functionA, functionB, TypeA
-- Expose all
module exposing ..
-- Expose nothing (private module)
module exposing ()
Named Inline Modules
Named inline modules must have their content indented:
module CustomMath exposing add, multiply
fn add : Int -> Int -> Int
fn add x y =
x + y
fn multiply : Int -> Int -> Int
fn multiply x y =
x * y
CustomMath.add 3 4
-- 7
Compiled
Important: Non-indented content after an inline module declaration is an error:
-- Error: a NAMED module's body must be indented. The message is the parser's,
-- and it names the symptom (no member parsed) rather than the indentation.
module CustomMath exposing add
fn add x y = x + y -- Wrong: not indented
Compiled
Correct:
module CustomMath exposing add
fn add : Int -> Int -> Int
fn add x y =
x + y
CustomMath.add 5 3
-- 8
Compiled
What a Module Body May Contain
A module body holds the same kinds of declaration a file does: functions,
enums, type aliases, imports — and effects, trait definitions and
trait ... for ... impls:
-- A module body holds effects, traits and impls, not only functions
module Shapes exposing area
effect Audit
note : String -> Unit
trait Showy a
disp : a -> String
trait Showy for Int
fn disp x = "an int"
fn area : Int -> String
fn area n = disp n
Shapes.area 3
Compiled
Two things follow from these being real declarations rather than a nested dialect:
- An impl in a module body is an impl for the program. It is found by
dispatch wherever the module is part of the compilation, and it reserves its
trait's method names for a top-level
fnexactly as one written at a file's top level does — including in the files that import the module. The rule, with therunand import cases, is in Functions → "Trait method names are reserved". - An effect declared in a module body belongs to that module. Its members
may perform it and handle it; a caller cannot handle it, because an
exposinglist names functions and types and there is no way to expose an effect. Declare the effect at the file's top level if callers need to handle it — see Effects → "Where ahandlemay appear".
Recursive Functions in Modules
Functions defined inside modules can call themselves recursively — and each other, in any order: a module's members are visible throughout its body, exactly as a file's top-level functions are visible throughout the file.
module CustomMath exposing factorial, fibonacci
fn factorial : Int -> Int
fn factorial n =
if n <= 1 then
1
else
n * factorial (n - 1)
fn fibonacci : Int -> Int
fn fibonacci n =
if n <= 1 then
n
else
fibonacci (n - 1) + fibonacci (n - 2)
CustomMath.factorial 5
-- 120
Compiled
module CustomMath exposing fibonacci
fn fibonacci : Int -> Int
fn fibonacci n =
if n <= 1 then
n
else
fibonacci (n - 1) + fibonacci (n - 2)
CustomMath.fibonacci 10
-- 55
Compiled
Module Access
Access module members with dot notation:
module CustomMath exposing add, multiply
fn add : Int -> Int -> Int
fn add x y =
x + y
fn multiply : Int -> Int -> Int
fn multiply x y =
x * y
CustomMath.add 1 2
-- 3
Compiled
module CustomMath exposing multiply
fn multiply : Int -> Int -> Int
fn multiply x y =
x * y
CustomMath.multiply 3 4
-- 12
Compiled
Imports
Import modules to use their exports:
import Html
import Html.Attributes
Importing Standard Library Modules
Halyard provides built-in modules that can be imported directly:
import Json
import List
import Maybe
import Result
import String
List.map (|x : Int| x * 2) [1, 2, 3]
Json.encode { name = "Alice", age = 30 }
Ok 5 |> Result.map (|x : Int| x + 1)
Compiled
Or import specific functions directly:
import List exposing map, filter
map (|x : Int| x + 1) [1, 2, 3]
Compiled
See the Standard Library for all available modules and functions.
Import with Alias
Create shorter aliases for modules using the as keyword:
import Html.Attributes as Attr
Aliases provide convenient shorthand throughout your code:
import List as L
-- Modules and import aliases
import Math as M
M.abs (L.length [1, 2, 3])
Compiled
Aliases work with the exposing clause:
import List as L exposing length
length [1, 2]
Compiled
Note: Aliases replace the original module name — after import Dir as D, only D is accessible; writing Dir.fn or Dir::Enum::Variant is a compile error. This applies to all access forms: M.fn, M::Enum::Variant, and M::Enum::Pattern. Aliases must be uppercase and are inherited by child scopes.
Import with Exposing
Import specific items directly:
import List exposing map
-- Using built-in list functions directly
[1, 2, 3] |> map (|x| x * 2) -- [2, 4, 6]
Compiled
User-Defined File Modules
When you import a module that isn't part of the standard library, Halyard looks for a matching .hly file in the modules/ directory relative to your entry file. Module names use PascalCase but filenames use snake_case:
A halyard.toml is required. The manifest is what makes a directory a project, and module
discovery applies only inside one — a main.hly sitting beside a modules/ folder with no
manifest above it resolves no modules at all, in the compiler and in the editor alike. This
is the same bargain Cargo, Elm and cabal make: the manifest marks the root, and the root is
what paths are relative to. If an import of your own module stops resolving, check that
halyard.toml exists above it.
my-project/
├── halyard.toml
├── src/
│ └── main.hly
└── modules/
├── greeter.hly -- import Greeter
├── data_utils.hly -- import DataUtils
└── data/
├── list.hly -- import Data.List
└── nested/
└── deep.hly -- import Data.Nested.Deep
Defining a File Module
Each module file needs a module declaration with an exposing clause:
module exposing double, square
fn double : Int -> Int
fn double x = x * 2
fn square : Int -> Int
fn square x = x * x
Importing File Modules
Import file modules the same way you import stdlib modules — all import forms work:
-- Qualified access
import Math
Math.double 5 -- 10
-- With alias
import Math as M
M.square 3 -- 9
-- Exposing specific functions
import Math exposing double
double 5 -- 10
-- Exposing everything
import Math exposing ..
square 4 -- 16
Nested Modules
Dots in the module name map to directory separators:
-- Imports modules/data/list.hly
import Data.List
Data.List.head [1, 2, 3]
-- Imports modules/data/nested/deep.hly
import Data.Nested.Deep
Modules Importing Other Modules
File modules can import both other user modules and stdlib modules:
module exposing greeting
import String
fn greeting : String -> String
fn greeting name =
let parts = ["Hello", name]
String.join ", " parts
Symbol Resolution
When you import a module, Halyard resolves symbol names through a specific order:
- Current scope — look for local definitions
- Parent scopes — walk up the scope chain
- Imported modules — check module exports
- Standard library — built-in modules like
List,Math
Module Exports
A module's exposing clause defines what symbols it exports:
module Utils exposing double, Triple, Point
fn double: Int -> Int
fn double x = x * 2
enum Triple = Triple { first : Int, second : Int, third : Int }
type Point = { x: Int, y: Int }
double— Function exportedTriple— Enum exported (all constructors are always included)Point— Newtype exported
Import Resolution
Import statements determine which symbols are brought into scope:
-- Qualified: module name required
import List
List.length [1, 2] -- 2
-- Exposing: symbols directly accessible
import List exposing length, map
length [1, 2] -- 2
-- Alias: shorter qualified name
import List as L
L.length [1, 2] -- 2
Name Resolution
PascalCase module segments are converted to snake_case filenames:
| Module name | File path |
|---|---|
Greeter | modules/greeter.hly |
DataUtils | modules/data_utils.hly |
Data.List | modules/data/list.hly |
Data.Nested.Deep | modules/data/nested/deep.hly |
Circular Dependencies
Halyard detects circular imports at compile time:
-- modules/cycle_a.hly
module exposing a
import CycleB -- Error: circular dependency
let a = CycleB.b
One Path, One Module
A module's identity is its path relative to the project root, and halyard.toml is what
defines the project. So one path names one module, and declaring it twice is an error
wherever the two declarations are:
module Shapes exposing area -- one declaration of `Shapes`
fn area : Float -> Float
fn area r = r * r
module Shapes exposing perimeter -- Error: `Shapes` is declared more than once
fn perimeter : Float -> Float
fn perimeter r = r + r
That covers three cases which used to differ:
- Twice in one file, as above —
Module 'Shapes' is declared more than once in this unit. - Twice in one project, in two different files, or in a file and a
runchild of it. A project is one namespace, run children included. - An inline module named like a module file. Declaring
module Helperwhilemodules/helper.hlyexists is still rejected —Module 'Helper' is declared inline and is also the module file '...'— because a module file owns its path by sitting at it.
Shadowing is not declaring, and stays legal. An inline module Math that shadows the
standard library's, or shadows a module you imported, declares the name once — there is no
second declaration to clash with:
module Math exposing double
fn double : Int -> Int
fn double x =
x * 2
Math.double 5
Compiled
Outside a project there is no root, so each file is on its own. Two .hly files in a
directory with no halyard.toml above them may each declare module Helpers: they are two
modules, not one declared twice. The editor says so too — it reports the clash only for
files that share a project.
Example: Complete Module
import Math
module Geometry exposing Shape, area, perimeter
enum Shape
= Circle Float
| Rectangle Float Float
| Triangle Float Float Float
fn area : Shape -> Float
fn area shape =
case shape of
Circle r -> 3.14159 * r * r
Rectangle w h -> w * h
Triangle a b c ->
let s = (a + b + c) / 2.0
let sq = s * (s - a) * (s - b) * (s - c)
case Math.sqrt sq of
Just v -> v
Nothing -> 0.0
fn perimeter : Shape -> Float
fn perimeter shape =
case shape of
Circle r -> 2.0 * 3.14159 * r
Rectangle w h -> 2.0 * (w + h)
Triangle a b c -> a + b + c
import Geometry exposing Shape, area
let circle = Shape::Circle 5.0
area circle
Compiled
module Geometry exposing perimeter
fn perimeter : Float -> Float
fn perimeter r =
2.0 * 3.14159 * r
Geometry.perimeter 5.0
-- 31.4159
Compiled
Exporting and Importing Enums
Enums defined in a module can be exported and used in importing code:
module Shapes exposing Shape, area
enum Shape
= Circle Float
| Square Float
fn area : Shape -> Float
fn area shape =
case shape of
Circle r -> 3.14 * r * r
Square s -> s * s
import Shapes exposing Shape, area
let c = Shape::Circle 2.0
area c
Compiled
Include the type name in the exposing list to make the enum and its constructors available to importers. Both exposing .. and specific exposing Type, fn syntax work.
Shadowing Standard Library Modules
User-defined modules can shadow stdlib module names if the stdlib version is not imported:
module Math exposing double
fn double : Int -> Int
fn double x =
x * 2
Math.double 5
Compiled
If you import a stdlib module (List, String, Math, IO, Http, Json, Jsonl, DataFrame, Result, Maybe), you cannot define a module with the same name.
File Composition with Tasks
For composing a program from multiple files at compile time, Halyard provides tasks. While modules define reusable namespaces with exports, tasks let you run another file's code and receive its exposed values as a record:
let x = 5
let { result } = run "compute.hly" { x }
result -- computed by compute.hly
The target file uses a task declaration to define its interface:
-- compute.hly
task expecting { x : Int } exposing { result : Int }
let result = x * 2
See the tasks guide for the full details on passing variables and record destructuring.
Best Practices
- One concept per module — keep modules focused
- Use descriptive names —
User.AuthenticationnotUA - Minimize exports — only expose what's needed
- Prefer qualified access — clearer where things come from
- Keep module content indented for inline modules
Next Steps
- Learn about tasks for composing programs from multiple files
- Learn about error handling to understand Halyard's helpful error messages