Skip to content

Prelude

The Prelude is a Yona module that is automatically loaded for all programs. Its types and functions are available everywhere without an explicit import. This page is the complete reference for the core Prelude surface; utility functions over these types (such as map and unwrapOr for options) live in the standard library — see Std\Option and Std\Result.

Type Constructors Purpose
Linear a Linear a Resource wrapper; must be consumed exactly once
Option a Some a, None Optional value
Result a e Ok a, Err e Success or failure
Iterator a Iterator (() -> Option a) Pull-based streaming iterator

All constructors are first-class functions and can be used in pattern matching.

type Linear a = Linear a

Wraps a resource (file handle, socket, process handle) whose lifecycle is tracked by the linearity checker. A Linear value must be consumed exactly once, by pattern matching. Using it after consumption, consuming it in only one branch of a conditional, or letting it go out of scope unconsumed are compile-time errors (see error codes E0600–E0602).

let conn = Linear (tcpConnect host port) in
case conn of
Linear fd -> do
send fd "hello"
close fd
end
end
type Option a = Some a | None

An optional value: Some x when a value is present, None when it is absent. Functions that may not produce a result return Option instead of a sentinel value.

let safeDiv = (\a b -> if b == 0 then None else Some (a / b)) in
case safeDiv 10 2 of
Some v -> v
None -> 0
end # => 5
type Result a e = Ok a | Err e

The outcome of a computation that can fail: Ok value on success, Err error on failure. The error type e is often a symbol or a string.

let toPort = (\n -> if n > 0 && n < 65536 then Ok n else Err "out of range") in
case toPort 8080 of
Ok p -> p
Err _ -> 0
end # => 8080
type Iterator a = Iterator (() -> Option a)

A pull-based iterator: it wraps a function that returns Some element on each call and None once exhausted. Streaming producers in the standard library (readLines, chars, split) return Iterator so large inputs are processed in O(1) memory. Generators consume iterators as sources, which is the idiomatic way to feed one into a fold:

import readLines from Std\File, foldl from Std\List in
foldl (\acc _ -> acc + 1) 0 [line for line = readLines "data.txt"]
# => number of lines in the file
Function Signature Semantics
identity a -> a Returns its argument unchanged
const a -> b -> a Ignores its second argument
flip (a -> b -> c) -> b -> a -> c Swaps a function’s two arguments
compose (b -> c) -> (a -> b) -> a -> c Applies g, then f

These four combinators are the complete prelude function surface. Collection folds and transformations (foldl, foldr, map, filter, …) are not prelude functions — import them from Std\List:

import foldl from Std\List in
foldl (\acc x -> acc + x) 0 [1, 2, 3, 4] # => 10

Std\List.foldl is tail-recursive, which the compiler turns into a loop — it never grows the stack. foldr recurses to the right and is not tail-recursive; prefer foldl for aggregation over long sequences.

identity x = x

Returns its argument unchanged. Useful as a default transformation for higher-order functions.

identity 42 # => 42
identity "yona" # => "yona"
const x _ = x

Returns its first argument and ignores the second. Partially applied, const x is a function that returns x for any input.

const 1 99 # => 1
let always0 = const 0 in always0 5 # => 0
flip f a b = f b a

Reverses the argument order of a two-argument function.

flip (\a b -> a - b) 2 10 # => 8
compose f g x = f (g x)

Function composition: applies g first, then f to the result.

compose (\x -> x * 2) (\x -> x + 1) 5 # => 12

The Prelude also defines file-I/O and reflection support types — FileHandle, FileMode (Read | Write | ReadWrite | Append), Whence (SeekSet | SeekCur | SeekEnd), and Type (returned by typeOf) — as well as the built-in traits Show, Eq, Ord, Hash, Array, and Closeable with instances for the primitive types. See Std\File and Std\Types for the functions that use them, and the language specification for trait semantics.