Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

Control Flow

Halyard provides several ways to control program flow. Unlike imperative languages, all control flow constructs in Halyard are expressions that return values.

If Expressions

The if-then-else expression evaluates to one of two values:

halyard
let x = 5

let result =
    if x > 0 then
        "positive"
    else
        "non-positive"

result

Both branches must have the same type. This is enforced at compile time — a type mismatch between the then and else branches produces a BranchTypeMismatch error:

halyard
let condition = True

-- Valid: both branches return Int
if condition then
    1
else
    0

Invalid example (branches have different types — caught at compile time):

halyard
if False then
    1
else
    "zero"  -- Error: type mismatch

Multi-line If

halyard
let condition = True

if condition then
    "yes"
else
    "no"

Multi-way Conditionals

Chain conditions with else if:

halyard
let score = 85

let grade =
    if score >= 90 then
        "A"
    else if score >= 80 then
        "B"
    else if score >= 70 then
        "C"
    else if score >= 60 then
        "D"
    else
        "F"

grade  -- "B"

Case Expressions

Pattern match on values with case:

halyard
enum Color = Red | Green | Blue

let color = Color::Green

let description =
    case color of
        Color::Red -> "The color of fire"
        Color::Green -> "The color of nature"
        Color::Blue -> "The color of sky"

description

Qualified Enum Patterns

Patterns support the same qualified :: syntax used in expressions:

halyard
enum Color = Red | Green | Blue

let color = Color::Green

case color of
    Color::Red -> "fire"
    Color::Green -> "nature"
    Color::Blue -> "sky"

Matching Multiple Patterns

Use | to match multiple patterns (or-patterns):

halyard
enum Day
    = Monday
    | Tuesday
    | Wednesday
    | Thursday
    | Friday
    | Saturday
    | Sunday

let day = Day::Saturday

let isWeekend =
    case day of
        Day::Saturday | Day::Sunday -> True
        _ -> False

isWeekend  -- True

Matching with Guards

Add conditions to patterns using if:

halyard
let number = -5

case number of
    n if n < 0 -> "negative"
    n if n == 0 -> "zero"
    n if n > 0 -> "positive"
    _ -> "unknown"

Matching Nested Structures

Pattern match deeply nested data:

halyard
let data = (3, 4)

case data of
    (a, b) -> a + b

Maybe Handling

Handle optional values with case:

halyard
let maybeUser = Just "Alice"

let displayName =
    case maybeUser of
        Just name -> name
        Nothing -> "Anonymous"

displayName

The Maybe type is defined as:

halyard
enum Maybe a = Just a | Nothing

Result Handling

Handle success/failure with Result:

halyard
import String

let parseResult = Ok 42

case parseResult of
    Ok n -> "Parsed: " ++ String.fromInt n
    Err msg -> "Error: " ++ msg

The Result type is defined as:

halyard
enum Result a e = Ok a | Err e

List Pattern Matching

Destructure lists in case expressions:

halyard
let numbers = [1, 2, 3]

case numbers of
    [] -> "empty list"
    [x] -> "single element"
    [x, y] -> "two elements"
    x :: xs -> "multiple elements"

Processing Lists Recursively

halyard
fn sum : [Int] -> Int
fn sum list =
    case list of
        [] -> 0
        x :: xs -> x + sum xs

sum [1, 2, 3, 4, 5]  -- 15

Multiple Head Elements

halyard
let list = [1, 2, 3, 4]

case list of
    a :: b :: rest -> a + b
    x :: xs -> x
    [] -> 0

Exhaustiveness Checking

The compiler ensures all cases are handled — for enums, lists, tuples, records and literal domains alike:

halyard
enum Color = Red | Green | Blue

let color = Color::Blue

case color of
    Color::Red -> "red"
    Color::Green -> "green"
    Color::Blue -> "blue"

Use _ for catch-all patterns:

halyard
enum Color = Red | Green | Blue

let color = Color::Green

case color of
    Color::Red -> "primary"
    _ -> "other"

Guard Exhaustiveness

A guarded arm never counts as coverage. For Int/Char, interval analysis proves a complete partition without a catch-all; otherwise the matrix reports what is missing as a NonExhaustiveMatch:

halyard
let x = 0

case x of
    n if n > 0 -> "positive"
    n if n < 0 -> "negative"
    _ -> "zero"

Best Practices

  1. Prefer case over if for matching on types
  2. Handle all cases explicitly when possible
  3. Use guards for complex conditions, but add an unguarded catch-all unless the guards provably partition an Int/Char scrutinee
  4. Keep pattern matches shallow — deep nesting is hard to read
  5. Put specific patterns first, catch-all patterns last

Next Steps

Learn about collections to work with lists, tuples, and records.