Skip to content

The standard library today

This page lists every module of Pia's standard library as it is built, in lib/Pia. Each module is written in Pia and proved: what its contracts promise holds for every input, so a caller proves its own code from those contracts alone. The standard library and its core is the design the library follows, and The C++23 standard library in Pia maps each C++ header to the module that covers it.

A program uses a module through an interface it names, import Pia.Core.Order.Compare (getting started, ADR 0039). In Bazel, each module is a pia_library target in lib/Pia/BUILD.bazel.

tools/docs/check.py fails a pull request that adds a module without a line here.

Core

The vocabulary types, concepts and algorithms that everything else is built from (ADR 0040).

Module What it gives
Pia.Core.Order The concepts Eq and Ord with their laws, and min, max, clamp, swap, exchange and lexicographical_compare over them: C++'s <compare> and <concepts>
Pia.Core.Option option t, None or Some of a t, and the functions over it: C++'s <optional>
Pia.Core.Result What C++23's std::expected has on top of the language's own result t e
Pia.Core.Int gcd, midpoint, abs, div and saturating arithmetic for u8 to i64, with C++'s undefined cases ruled out by preconditions
Pia.Core.Bit The rest of C++23's <bit>, beside the builtins clz, ctz, popcount, bswap, rotl and rotr
Pia.Core.Hash The concept Hash, an Eq whose hash agrees with it, its instances, and @derive(hash)
Pia.Core.Ratio C++'s <ratio>, as metaprograms over exact integers
Pia.Core.Ascii C++'s <cctype> for ASCII bytes, each classifier exact
Pia.Core.Utf8 UTF-8 decoding, encoding and validity, with both round trips proved
Pia.Core.Charconv from_chars and to_chars for integers in bases 2 to 36, with the round trip proved
Pia.Core.Digits The facts about digit strings that formatting and parsing proofs need, as lemmas
Pia.Fn Laws of function arguments, such as strict_weak_order and sorted, that a higher-order function states as preconditions (ADR 0044)
Pia.Time C++'s <chrono> durations, as types a metaprogram makes over a ratio

Algorithms

C++'s <algorithm> and <numeric> on slices. Each takes its comparison or operation as a function value and is proved once, for every element type and every such function (ADR 0043).

Module What it gives
Pia.Core.Algo.Search find, count, contains, equal, mismatch, search, min_element, is_sorted and the rest of the non-modifying algorithms
Pia.Core.Algo.BinarySearch lower_bound, upper_bound, equal_range and binary_search
Pia.Core.Algo.Modify fill, copy, copy_backward, swap_ranges, reverse, replace, shift_left and shift_right
Pia.Core.Algo.Remove remove, whose contract says where each kept element goes
Pia.Core.Algo.Numeric iota, accumulate, reduce, inner_product, the scans and adjacent_difference
Pia.Core.Algo.Permutation The multiset spec functions that sorting states its results with
Pia.Core.Algo.Sort insertion_sort: sorted, and a permutation of its input
Pia.Core.Algo.Heap The heap operations, and sort as a heap sort
Pia.Core.Algo.PartialSort partial_sort and nth_element
Pia.Core.Algo.Partition is_partitioned, partition_point and partition
Pia.Core.Algo.Merge merge of two sorted ranges
Pia.Core.Algo.Set includes, set_intersection and set_difference on sorted ranges
Pia.Core.Algo.SetUnion set_union
Pia.Core.Algo.SetSymmetric set_symmetric_difference

Memory

Allocators, boxes and vectors, with no garbage collector and every allocation fallible (ADR 0026, ADR 0041, ADR 0042).

Module What it gives
Pia.Mem.Alloc oom, the one way an allocation fails; the concept Allocator a t r, an allocator of ts lent from the region r; and the bump allocator bump t r
Pia.Mem.Box box t r, an owning box of one t, whose fullness is spec-only state, so a use after release is a failed goal; its type parameter is @unsized, so it may be a concept, and box Shape holds any type that meets Shape (ADR 0046)
Pia.Mem.Vec vec t a r, a growable vector that holds its allocator, C++'s std::vector with every growth fallible

Text and data formats

Module What it gives
Pia.Text.Json.Spec RFC 8259's token grammar, as spec functions over bytes, which the rest of the JSON modules are proved against (ADR 0045)
Pia.Text.Json.Lex A JSON tokenizer over a byte slice that never reads outside its input and always ends
Pia.Text.Json.Write A JSON writer into a caller's buffer, whose room is its precondition, so nothing fails at run time
Pia.Text.Json.Read The token readers that derived decoders are built from
Pia.Text.Json.Derive @derive(json): the metaprograms that write a JSON codec for a type
Pia.Text.Json.Dom A JSON document as a recursive sum type over spans of its input, copying no text (ADR 0047)
Pia.Text.Json.Shape Checks that a JSON DOM has the shape of a type, and builds that type from it, with @derive(json_dom) (JSON)
Pia.Format.Safetensors The safetensors container that model weights come in, read from a file's bytes and proved total and safe on every input (ADR 0067)
Pia.Llm.Tokenizer llama2.c's byte-pair tokenizer, read from its tokenizer.bin and proved (ADR 0067)

Compression and images

The codecs are proved total and safe on every input, and allocate nothing: the caller lends every buffer (ADR 0054).

Module What it gives
Pia.Image.Bits LSB-first bit readers and writers over a byte slice, as GIF and deflate read them
Pia.Image.Raster An image's dimensions and the indices of its samples
Pia.Image.Gif.Spec What a GIF means, as spec functions: the LZW dictionary of GIF89a
Pia.Image.Gif.Lzw GIF's LZW decoder, proved against the spec's dictionary
Pia.Image.Gif.Ref GIF89a's LZW reading of a whole code stream, as a trace spec (lzw_ok), which Lzw's decoder is proved against
Pia.Image.Gif.LzwEncode GIF's LZW encoder
Pia.Image.Gif.Decode The GIF container, proved to accept exactly what the spec's block grammar accepts
Pia.Image.Gif.Encode Animated GIF encoding
Pia.Image.Gif.Quantise An RGB raster to a palette of at most 256 colours and an index raster
Pia.Compress.Huffman Canonical Huffman codes as deflate defines them, with the completeness check and decoding
Pia.Compress.Inflate.Spec The parts of a deflate block, as spec functions
Pia.Compress.Inflate Deflate decoding into a caller's buffer, after zlib's puff.c
Pia.Compress.Zlib The zlib format, with adler32 proved equal to RFC 1950's definition

The system and the GPU

Module What it gives
Pia.Sys.Posix The contracts of the POSIX functions Pia calls, as one interface that every target meets (ADR 0057)
Pia.Sys.Fs Files over Pia.Sys.Posix, written once for both targets: a @linear file that must be closed exactly once, open_read, create, read_some, write_all and close, each failing with a decoded errno
Pia.Sys.Gen.arm64_macos The bindings for arm64 macOS, generated from the system headers by tools/bindings/gen.py, which CI checks are current
Pia.Sys.Gen.x86_64_linux The same for x86_64 Linux
Pia.Gpu Devices, buffers and kernels on the GPU through a Metal host shim, which --trust-report lists as trusted (ADR 0068)

A spread marked @place(gpu) runs on the GPU without calling Pia.Gpu itself: the GPU demo shows one.