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.
Linear a
Section titled “Linear a”type Linear a = Linear aWraps 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) incase conn of Linear fd -> do send fd "hello" close fd endendOption a
Section titled “Option a”type Option a = Some a | NoneAn 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)) incase safeDiv 10 2 of Some v -> v None -> 0end # => 5Result a e
Section titled “Result a e”type Result a e = Ok a | Err eThe 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") incase toPort 8080 of Ok p -> p Err _ -> 0end # => 8080Iterator a
Section titled “Iterator a”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 infoldl (\acc _ -> acc + 1) 0 [line for line = readLines "data.txt"]# => number of lines in the fileFunctions
Section titled “Functions”| 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 infoldl (\acc x -> acc + x) 0 [1, 2, 3, 4] # => 10Std\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
Section titled “identity”identity x = xReturns its argument unchanged. Useful as a default transformation for higher-order functions.
identity 42 # => 42identity "yona" # => "yona"const x _ = xReturns 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 # => 0flip f a b = f b aReverses the argument order of a two-argument function.
flip (\a b -> a - b) 2 10 # => 8compose
Section titled “compose”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 # => 12Other always-available definitions
Section titled “Other always-available definitions”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).
Its foundational traits are Eq, Ord, Hash, Show, Array, Closeable,
Sized, Iterable, Foldable, Semigroup, Monoid, From, TryFrom,
Parse, Send, and Shareable. Operators use Eq and Ord statically;
collection traits lift over their element types; conversion traits select the
complete source/target instance; and the method-free concurrency markers are
proved at compile time and erased. See Traits for laws,
standard instances, conversion witnesses, and marker restrictions, plus
Std\File and Std\Types for the functions that
use the support types.