Skip to main content

PCD Standard Library

The BRIK-64 standard library is written 100% in PCD. There is no Rust, no C, no FFI. Every stdlib function compiles through the same pipeline as user code, producing the same verified ELF, BIR, or WASM output. This is the proof of concept: a language whose standard library is written in itself, formally verified by the TCE, and compiled to native code by the self-hosting compiler.
All stdlib modules are located in the stdlib/ directory relative to your project root. Import them with import "stdlib/module.pcd";.

Module Overview

ModuleFileFunctions
mathstdlib/math.pcdabs, min, max, clamp, pow, sqrt, floor, ceil, round, sign, gcd, lcm
stringstdlib/string.pcdlen, upper, lower, trim, split, join, find, contains, starts_with, ends_with, replace, format, to_int, from_int, char_at, substr
arraystdlib/array.pcdlen, push, pop, get, set, sort, reverse, map, filter, reduce, contains, find_index, slice, concat, zip, flatten
iostdlib/io.pcdread_file, write_file, read_line, write_line, exists, read_bytes, write_bytes
fmtstdlib/fmt.pcdformat, to_string, pad_left, pad_right, truncate, repeat, center
jsonstdlib/json.pcdparse, stringify, get, set, keys, values, has, merge

math

import "stdlib/math.pcd";

abs(n: i64) → i64

Returns the absolute value of n.
let x = abs(-42);    // 42
let y = abs(7);      // 7

min(a: i64, b: i64) → i64

Returns the smaller of two values.
let m = min(10, 3);   // 3

max(a: i64, b: i64) → i64

Returns the larger of two values.
let m = max(10, 3);   // 10

clamp(val: i64, lo: i64, hi: i64) → i64

Clamps val to the range [lo, hi].
let c = clamp(150, 0, 100);   // 100
let d = clamp(-5, 0, 100);    // 0
let e = clamp(50, 0, 100);    // 50

pow(base: i64, exp: i64) → i64

Returns base raised to the power exp. Uses fast exponentiation (O(log exp)).
let p = pow(2, 10);   // 1024
let q = pow(3, 0);    // 1

sqrt(n: i64) → i64

Integer square root (floor). Returns the largest integer k such that k*k ≤ n.
let s = sqrt(16);   // 4
let t = sqrt(17);   // 4
let u = sqrt(25);   // 5

floor(n: i64) → i64

Identity for integers. Provided for type-consistent APIs.

ceil(n: i64) → i64

Identity for integers. Provided for type-consistent APIs.

sign(n: i64) → i64

Returns -1, 0, or 1 depending on the sign of n.
let s1 = sign(-100);   // -1
let s2 = sign(0);      // 0
let s3 = sign(42);     // 1

gcd(a: i64, b: i64) → i64

Greatest common divisor via Euclidean algorithm.
let g = gcd(48, 18);   // 6

lcm(a: i64, b: i64) → i64

Least common multiple.
let l = lcm(4, 6);   // 12

string

import "stdlib/string.pcd";

len(s: string) → i64

Returns the number of characters in s.
let n = len("hello");   // 5

upper(s: string) → string

Converts all lowercase letters to uppercase.
let u = upper("hello");   // "HELLO"

lower(s: string) → string

Converts all uppercase letters to lowercase.
let l = lower("WORLD");   // "world"

trim(s: string) → string

Removes leading and trailing whitespace.
let t = trim("  hello  ");   // "hello"

split(s: string, delim: string) → array

Splits s on delim, returns an array of substrings.
let parts = split("a,b,c", ",");   // ["a", "b", "c"]
let lines = split(text, "\n");

join(arr: array, sep: string) → string

Joins an array of strings with sep.
let s = join(["a", "b", "c"], "-");   // "a-b-c"
let csv = join(fields, ",");

find(s: string, sub: string) → i64

Returns the index of the first occurrence of sub in s, or -1 if not found.
let i = find("hello world", "world");   // 6
let j = find("hello", "xyz");           // -1

contains(s: string, sub: string) → bool

Returns true if s contains sub.
let ok = contains("hello world", "world");   // true

starts_with(s: string, prefix: string) → bool

let ok = starts_with("hello", "hel");   // true

ends_with(s: string, suffix: string) → bool

let ok = ends_with("hello.pcd", ".pcd");   // true

replace(s: string, from: string, to: string) → string

Replaces all occurrences of from with to.
let r = replace("foo foo foo", "foo", "bar");   // "bar bar bar"

format(template: string, args: array) → string

Replaces {} placeholders with array elements in order.
let msg = format("Hello, {}! You are {} years old.", ["Alice", 30]);
// "Hello, Alice! You are 30 years old."

to_int(s: string) → i64

Parses a decimal integer string. Returns 0 for invalid input.
let n = to_int("42");    // 42
let m = to_int("-7");    // -7
let bad = to_int("abc"); // 0

from_int(n: i64) → string

Converts an integer to its decimal string representation.
let s = from_int(42);    // "42"
let t = from_int(-100);  // "-100"

char_at(s: string, i: i64) → string

Returns the character at index i as a single-character string.
let ch = char_at("hello", 1);   // "e"

substr(s: string, start: i64, length: i64) → string

Returns a substring of s starting at start with the given length.
let sub = substr("hello world", 6, 5);   // "world"

array

import "stdlib/array.pcd";

len(arr: array) → i64

let n = len([1, 2, 3]);   // 3

push(arr: array, elem) → array

Returns a new array with elem appended.
let arr2 = push([1, 2], 3);   // [1, 2, 3]

pop(arr: array) → (array, value)

Returns a tuple of (new_array, last_element).
let (arr2, last) = pop([1, 2, 3]);   // ([1, 2], 3)

get(arr: array, i: i64) → value

Returns element at index i. Equivalent to arr[i].
let x = get(arr, 2);

set(arr: array, i: i64, val) → array

Returns a new array with element at index i replaced by val.
let arr2 = set([1, 2, 3], 1, 99);   // [1, 99, 3]

sort(arr: array) → array

Returns a new array sorted in ascending order (integers or strings).
let sorted = sort([3, 1, 4, 1, 5, 9]);   // [1, 1, 3, 4, 5, 9]

reverse(arr: array) → array

Returns a new array with elements in reverse order.
let r = reverse([1, 2, 3]);   // [3, 2, 1]

map(arr: array, f: closure) → array

Applies f to each element, returns new array of results.
let doubled = map([1, 2, 3], fn(x) { x * 2 });   // [2, 4, 6]

filter(arr: array, pred: closure) → array

Returns new array of elements for which pred returns true.
let evens = filter([1, 2, 3, 4, 5], fn(x) { x % 2 == 0 });   // [2, 4]

reduce(arr: array, initial, f: closure) → value

Folds arr from left with accumulator starting at initial.
let sum = reduce([1, 2, 3, 4, 5], 0, fn(acc, x) { acc + x });   // 15
let product = reduce([1, 2, 3, 4], 1, fn(acc, x) { acc * x });  // 24

contains(arr: array, elem) → bool

Returns true if elem is in arr.
let ok = contains([1, 2, 3], 2);   // true

find_index(arr: array, elem) → i64

Returns the index of the first occurrence of elem, or -1.
let i = find_index(["a", "b", "c"], "b");   // 1

slice(arr: array, start: i64, end: i64) → array

Returns elements from start (inclusive) to end (exclusive).
let s = slice([0, 1, 2, 3, 4], 1, 4);   // [1, 2, 3]

concat(a: array, b: array) → array

Concatenates two arrays.
let c = concat([1, 2], [3, 4]);   // [1, 2, 3, 4]

zip(a: array, b: array) → array

Pairs elements: returns array of tuples.
let z = zip([1, 2, 3], ["a", "b", "c"]);   // [(1, "a"), (2, "b"), (3, "c")]

flatten(arr: array) → array

Flattens one level of nesting.
let f = flatten([[1, 2], [3, 4], [5]]);   // [1, 2, 3, 4, 5]

io

import "stdlib/io.pcd";

read_file(path: string) → string

Reads the entire file at path as a UTF-8 string.
let source = read_file("input.pcd");

write_file(path: string, content: string)

Writes content to path, creating or overwriting the file.
write_file("output.bir", bir_text);

read_line() → string

Reads a single line from stdin (without trailing newline).
let line = read_line();

write_line(s: string)

Writes s followed by a newline to stdout.
write_line("Processing complete.");

exists(path: string) → bool

Returns true if the file at path exists.
if (exists("config.pcd")) {
    let cfg = read_file("config.pcd");
}

read_bytes(path: string) → array

Reads file as array of byte values (i64 in 0–255 range).
let bytes = read_bytes("binary.dat");
let first_byte = bytes[0];

write_bytes(path: string, bytes: array)

Writes array of byte values to file.
write_bytes("output.bin", [0x7f, 0x45, 0x4c, 0x46]);

fmt

import "stdlib/fmt.pcd";

format(template: string, args: array) → string

String interpolation with {} placeholders. Each {} is replaced by the next element of args, converted to string:
let s = format("x={}, y={}", [10, 20]);    // "x=10, y=20"
let msg = format("Hello, {}!", ["world"]); // "Hello, world!"

to_string(val) → string

Converts any value to its string representation.
let s1 = to_string(42);         // "42"
let s2 = to_string(true);       // "true"
let s3 = to_string([1, 2, 3]);  // "[1, 2, 3]"

pad_left(s: string, width: i64, ch: string) → string

Pads s on the left to reach width characters, using ch as padding character.
let p = pad_left("42", 5, "0");    // "00042"
let q = pad_left("hi", 6, " ");    // "    hi"

pad_right(s: string, width: i64, ch: string) → string

Pads s on the right.
let p = pad_right("hi", 6, ".");   // "hi...."

truncate(s: string, max_len: i64) → string

Truncates s to max_len characters.
let t = truncate("hello world", 5);   // "hello"

repeat(s: string, n: i64) → string

Repeats s exactly n times.
let r = repeat("ab", 3);   // "ababab"

center(s: string, width: i64, ch: string) → string

Centers s within width characters, padding with ch.
let c = center("hi", 8, "-");   // "---hi---"

json

import "stdlib/json.pcd";
The JSON module represents JSON values as PCD values: JSON objects are arrays of (key, value) tuple pairs, JSON arrays are PCD arrays, and JSON primitives map to their PCD equivalents.

parse(s: string) → value

Parses a JSON string into a PCD value.
let obj = parse("{\"name\": \"Alice\", \"age\": 30}");

stringify(val) → string

Serializes a PCD value to a JSON string.
let s = stringify(["a", "b", 1, true]);   // "[\"a\",\"b\",1,true]"

get(obj, key: string) → value

Gets a field from a JSON object by key.
let name = get(obj, "name");   // "Alice"

set(obj, key: string, val) → value

Returns a new object with the field set.
let obj2 = set(obj, "age", 31);

keys(obj) → array

Returns an array of the object’s keys.
let ks = keys(obj);   // ["name", "age"]

values(obj) → array

Returns an array of the object’s values.
let vs = values(obj);   // ["Alice", 30]

has(obj, key: string) → bool

Returns true if obj has the given key.
let ok = has(obj, "email");   // false

merge(a, b) → value

Merges two JSON objects. Fields in b override fields in a.
let merged = merge(defaults, overrides);

Using Multiple Stdlib Modules

import "stdlib/math.pcd";
import "stdlib/string.pcd";
import "stdlib/array.pcd";
import "stdlib/fmt.pcd";

PC process_data {
    let raw = MC_56.READ("data.txt");
    let lines = split(raw, "\n");
    let n = len(lines);

    let total = 0;
    loop(n) as i {
        let line = lines[i];
        let trimmed = trim(line);
        if (len(trimmed) > 0) {
            let val = to_int(trimmed);
            let total = total + val;
        }
    }

    let avg = total / n;
    let msg = format("Sum={}, Count={}, Avg={}", [total, n, avg]);
    OUTPUT msg;
}
Importing multiple stdlib modules is idiomatic PCD. The planner resolves all imports before type-checking, so there are no forward-reference issues between modules.