Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Standard Library Reference

This page documents all functions available in the milang prelude, C builtins, and operators. Functions marked “extensible” can be extended for user-defined types via open function chaining.

Types

Bool = {True; False}
List = {Nil; Cons head tail}
Maybe = {Nothing; Just val}

-- Sized numeric type aliases (Int', UInt', Float' are primitive constructors)
Int = Int' 0        -- arbitrary-precision signed integer
UInt = UInt' 0      -- arbitrary-precision unsigned integer
Float = Float' 64   -- 64-bit floating-point
Byte = UInt' 8      -- unsigned 8-bit integer

Extensible Functions

These functions are designed to be extended via open function chaining for user-defined types.

FunctionSignatureDescription
truthya : NumBoolean coercion. Falsy: 0, 0.0, "", False, Nil. Truthy: everything else. Used by if, guards, not, &&, ||.
toStringa : StrString conversion. Handles True, False, Nil symbolically; delegates to _toString for primitives (int, float, string).
eqa : a : NumEquality. Default falls through to structural ==. Used by contains.

Core Functions

FunctionSignatureDescription
ida : aIdentity function.
consta : b : aReturns first argument, ignores second.
flip(a : b : c) : b : a : cFlips the first two arguments of a function.
nota : NumLogical negation via truthy.

List Functions

These functions work on List values. Functions marked with † also work on Maybe via additive type annotations.

FunctionSignatureDescription
nullList : NumReturns 1 if list is Nil (or Nothing), 0 otherwise.
headList : MaybeFirst element wrapped in Maybe (Nothing if empty).
tailList : MaybeTail wrapped in Maybe (Nothing if empty).
defaulta : List : aExtract value or return a default. For Maybe: inner value or default. For List: first element or default. Like Haskell’s fromMaybe.
fold(a : b : a) : a : List : aLeft fold over a list (or extracts Maybe value with default).
map(a : b) : List : ListApply function to each element (or to Just value).
filter(a : Num) : List : ListKeep elements where predicate is truthy (or filter Just value).
concatList : List : ListConcatenate two lists (or combine two Maybes, preferring first Just).
flatMap(a : List) : List : ListMap then flatten (or chain Maybe computations).
pushList : a : ListAppend element to end of list.
atList : Num : MaybeGet element at index (zero-based); returns Nothing if out of bounds. at' takes index first.
sumList : NumSum of numeric list.
productList : NumProduct of numeric list.
any(a : Num) : List : Num1 if predicate is truthy for any element (or for Just value).
all(a : Num) : List : Num1 if predicate is truthy for all elements (or for Just value).
containsList : a : Num1 if list contains element (via eq).
rangeNum : Num : ListInteger range [start, end).
zipList : List : ListPair corresponding elements into 2-element lists.
lastList : MaybeLast element wrapped in Maybe (Nothing if empty).
initList : MaybeAll elements except the last wrapped in Maybe (Nothing if empty).
reverseList : ListReverse a list.
takeNum : List : ListFirst n elements.
dropNum : List : ListDrop first n elements.
enumerateList : ListPair each element with its index: [[0, a], [1, b], ...].
joinStr : List : StrJoin string list with separator.
lena : NumLength of a string, list, or Maybe (1 for Just, 0 for Nothing).

Numeric Functions

FunctionSignatureDescription
absNum : NumAbsolute value.
negNum : NumNegation (0 - x).
minNum : Num : NumMinimum of two numbers.
maxNum : Num : NumMaximum of two numbers.

String Builtins

String operations provided by the C runtime:

FunctionSignatureDescription
strlenStr : NumLength of a string.
charAtStr : Num : MaybeCharacter at index; returns Just a single-char string when index is valid, or Nothing when out of range.
indexOfStr : Str : NumIndex of first occurrence of substring (-1 if not found).
sliceStr : Num : Num : StrSubstring from start index to end index.
splitStr : Str : ListSplit string by delimiter.
trimStr : StrRemove leading/trailing whitespace.
toUpperStr : StrConvert to uppercase.
toLowerStr : StrConvert to lowercase.
replaceStr : Str : Str : StrReplace all occurrences: replace old new str.

Type Conversion Builtins

FunctionSignatureDescription
toStringa : StrConvert to string (extensible — see above).
toInta : MaybeConvert to integer; returns Just on success (parsing or conversion), Nothing on failure.
toFloata : MaybeConvert to float; returns Just on success, Nothing on failure.

Record Introspection Builtins

Functions for inspecting and modifying record structure at runtime:

FunctionSignatureDescription
tagRecord : StrConstructor tag name (e.g., tag (Just 1) -> "Just").
fieldsRecord : ListList of field values; returns [] for non-record values.
fieldNamesRecord : ListList of field names; returns [] for non-record values.
getFieldRecord : Str : MaybeDynamic field access by name; returns Just value if present, Nothing otherwise.
setFieldRecord : Str : a : RecordReturn copy with field updated; on non-record values returns the original value unchanged.

Metaprogramming

FunctionSignatureDescription
valuesList : ListBlock-to-list collector. When used with =>, collects each line of the block as a list element. Used by the FFI annotation DSL.

Monads

Milang provides a lightweight monad system using quote/splice. The bind function sequences computations that may fail or produce multiple results:

FunctionSignatureDescription
bindm : (a : m) : mMonadic bind. Dispatches to list_bind or maybe_bind based on the input type.
list_bindList : (a : List) : ListFlatMap for lists.
maybe_bindMaybe : (a : Maybe) : MaybeChain Maybe computations; short-circuits on Nothing.
-- Maybe monad: short-circuits on Nothing
result = bind (Just 5) \x ->
  bind (Just (x + 1)) \y ->
    Just (x + y)
-- result = Just 11

-- List monad: cartesian product
pairs = bind [1, 2] \x ->
  bind [10, 20] \y ->
    [x + y]
-- pairs = [11, 21, 12, 22]

Memory Management

FunctionSignatureDescription
gc_managePointer : Native : ManagedWrap an FFI pointer with a finalizer for automatic GC cleanup. See C FFI — Memory Management.

Operators

OperatorSignatureDescription
|>a : (a : b) : bPipe forward: x |> f = f x.
>>(a : b) : (b : c) : a : cForward composition.
<<(b : c) : (a : b) : a : cBackward composition.
<-Record : Record : RecordRecord merge: base <- overlay.
=>(a : b) : a : bBlock argument: f => body passes indented block as last argument.
&&a : a : NumShort-circuit logical AND (via truthy).
||a : a : NumShort-circuit logical OR (via truthy).
:a : List : ListCons (prepend element to list).
+ - * / % **Num : Num : NumArithmetic (+ also concatenates strings; ** takes an integer exponent).
== /= < > <= >=a : a : NumComparison (structural equality for records).

Maybe examples

-- Maybe-returning stdlib usage
p1 = toInt "123"
p2 = toInt "abc"
p3 = toFloat "3.14"
r = {a = 1}

show mi = mi -> Just {val} = toString val; Nothing = "Nothing"

main world =
  world.io.println (show p1)
  world.io.println (show p2)
  world.io.println (toString p3)
  world.io.println (show (getField r "a"))
  world.io.println (show (getField r "b"))
123
Nothing
Just(3.14)
1
Nothing

Use default to extract a value with a fallback:

main world =
  world.io.println (toString (default 0 (Just 42)))
  world.io.println (toString (default 0 Nothing))
  world.io.println (toString (default 99 [1, 2, 3]))
  world.io.println (toString (default 99 []))
42
0
1
99