40. The C++23 standard library, mapped onto Pia and proved¶
Date: 2026-11-22
Status: Accepted (Dean, 2026-11-22)
Context¶
Dean asked for the C++23 standard library in Pia, now that Pia has generics, concepts, slices, result with ?, defer and Drop, @linear, borrows, with_arena and a proved generic vec over a bump allocator. ADR 0023 already decided that the library is written in Pia over a minimal core, and docs/stdlib.md lays it out in layers (Pia.Core, Pia.Sys, Pia.Mem, Pia.Io, Pia.Math, Pia.Par). What it does not say is how much of what a C++ programmer expects from std Pia provides, under which names, and in what order.
C++23 has about a hundred headers. Some translate directly (<bit>, <algorithm>, <charconv>). Some exist because of a C++ mechanism that Pia replaces (<exception>, <iterator>, <memory>'s std::allocator, <type_traits>). Some need things Pia does not have yet (floats, atomics, function values). And some are out of scope for reasons of principle (<locale>, wide characters, <any>).
A verified library also has to say more than C++'s does. The standard states preconditions in prose ("the behaviour is undefined if [first, last) is not sorted"), and results as "Effects:" and "Returns:" clauses. In Pia, each of those is a contract the compiler proves, so the library cannot be a transliteration: every function needs a specification precise enough to prove, which often needs a spec function the standard never names (sorted, count, permutation).
Prior art: Rust's core, alloc and std give the same capabilities as C++'s library under different idioms (slices for iterator pairs, Result for exceptions, traits for concepts). SPARK's formal containers and Ada's Ada.Containers show contracts written against a mathematical model. Dafny's and Verus's standard libraries (vstd) show which specifications are practical to prove. Zig's std shows a library where every allocating function takes an allocator.
Decision¶
1. Capability parity, not API parity¶
Pia provides what the C++23 library lets a program do, in Pia's idioms, not C++'s names and signatures. A C++ programmer finds each std entity through docs/cpp-std.md, a header-by-header map that says, for each header, which Pia module provides it, whether it is direct, replaced by a Pia idiom, deferred (and on what), or out of scope (and why). The map is updated by every library PR, so it is also the status page.
Each library function's doc comment names the C++ entity it corresponds to, as (** C++: std::lower_bound *), so the language server, search and the map agree.
Names follow Pia's style: snake case, single words where they are clear (find, count, fill, rotate), the C++ name where it is the established term (lower_bound, popcount, gcd, iota), and no std:: prefixes, _t suffixes or _if variants, whose forms are overloads instead (see section 6).
2. C++'s prose becomes proved contracts¶
- Every precondition is a
requires. What is undefined behaviour in C++ is a proof goal at the call in Pia: an unsorted range passed tolower_bound, an index past the end, apopon an empty heap, a shift by the width or more,midpointof nothing,gcdoverflowing. None of these has a run-time check, because none can happen in a proved program. - Every "Returns:", "Effects:" and "Postconditions:" clause is an
ensures, written with spec functions in the module (sorted,count,permutation,is_heap,prefix).sortpromises a sorted permutation of its input,lower_boundthe first index whose element is not less than the key, andto_charsa string thatfrom_charsreads back to the same value. - Where C++ leaves something unspecified, Pia picks one answer and states it: which of equal elements
min_elementreturns, the order of apartition's halves, the contents of a slice afterremove. Code may rely on what the contract says, and on nothing else. - Complexity clauses are documented, not proved. "At most
log2(n) + 1comparisons" goes in the doc comment. Pia has no cost proofs yet (see the gaps below), so the library is tested against them with benchmarks instead. - Laws a C++ concept states but cannot check are concept laws.
Ord's strict weak order (irreflexive, transitive, and transitive incomparability),Eq's equivalence andHash's agreement withEqare lemmas proved for every instance (ADR 0034), so asortover a type whoselessis not a strict weak order is rejected where it is instantiated, rather than misbehaving at run time.
3. No exceptions: closed error sets¶
- A C++ function that throws becomes one that returns
result t e, whereeis a closed variant naming exactly the errors that can happen (ADR 0028). There is nostd::exceptionhierarchy, nowhat()and nonoexcept: a function with no error set cannot fail. std::bad_allocisOutOfMemory, returned by every allocating call and handled at its call site (ADR 0026). No container function allocates without saying so in its type.- C++'s pairs of checked and unchecked accessors collapse.
v[i]andv.at(i)are bothv.[i]with the bound proved. An accessor that returnsoptionexists only where the caller cannot be expected to prove the bound, such as a lookup by key. std::errcandstd::error_codebecome the error variants of the module that raises them;Pia.Sysdecodeserrnointo its own variants (ADR 0023).
4. Allocators are explicit, and every container is a pmr container¶
- Every allocating function takes a
borrowedallocator that meetsAllocator(ADR 0026). There is nostd::allocatorand no default global heap, so C++'sstd::pmris the only model, and<memory_resource>'smonotonic_buffer_resource,unsynchronized_pool_resourceandsynchronized_pool_resourcearePia.Mem'sarena,poolandsharedallocators. <scoped_allocator>has nothing to do: a container's elements that allocate are given their allocator explicitly, and borrows (ADR 0026) prove that none outlives it.- There is no
unique_ptr; there is onebox(Dean, 2026-11-22). C++'sunique_ptr<T>is a nullable pointer plus a deleter, and its type says nothing about what it points to beyondT. Pia'sboxis an owning, non-null allocation from a stored allocator, and its parameter is either a type or a concept:box tholds at. Calls on it are direct, and proofs see the concrete value, as for any othert.box c, for a conceptcsuch asShapeorFunction u8 bool(ADR 0044), holds a value of any type that meetsc. Widening a box (box.widen) checks, statically, that its type meetscand that the instance's definitions satisfy the requirements' contracts and laws. The box carries the instance's table of requirement functions, and a call through it is an indirect call whose proof uses only the requirement's contract, as ADR 0043 proves a generic body over an abstract type. So everything in the box is proved to have the concept's shape, and no caller can tell which type it is. This replaces C++'s virtual base classes andstd::function, and is the type-erased box that ADR 0044 reserves forPia.Fn(box (Function a r)is Pia'smove_only_function).- A box is never null: an optional box is
option (box t), whoseNoneis the null pointer in the layout. It has no deleter: dropping it drops the value (through the table, for a concept box) and returns the memory to the allocator it stores, so a custom deleter is a custom allocator. It isDrop, so it is freed at scope end, or@linearwhen the program must free it explicitly; moving it moves ownership and copying it does not exist. make_uniqueisbox.make alloc v, which can fail like any allocation.get,operator*andoperator->are borrows (borrowed(b)),releaseisbox.release, which leaves the box empty untilbox.fillrefills it, andresetisbox.removethenbox.fill.- The core part, a concept used as a type argument, and the rules for calls through a box (only requirements with one receiver) are ADR 0046.
shared_ptrandweak_ptrarePia.Mem.Ref'srefandweak(ADR 0047): counted, with shared values changed only under leases proved exclusive, so strong cycles cannot be built; one thread first, and an atomicshared_refwith M12.
The allocators, vec and the containers built on them (issue #72) are owned by the Pia.Mem work (#59, #71, #86, #88). This ADR maps C++'s containers onto that work and adds the ones #72 does not list, but does not change Pia.Mem's design.
5. Iterators become slices and indices¶
- A pair of iterators
[first, last)over contiguous storage is a slice[t], and a sub-range isxs.[a .. b]. An output iterator is anoutslice whose length is proved to be enough, socopy,transformandset_unioncannot write past the end. A position is ausizeindex, and "an iterator to the found element, orlast" is an index in0 .. len xs, withlen xsmeaning not found, so an index result composes with slicing as C++'s iterators do with ranges. std::spanis the slice type itself,std::array<T, N>is[T; N], andstd::string_viewis astrslice. None needs a library type.- Node-based containers (
list,map,set) expose a cursor type of their own, an index into their node pool, proved valid by the container's invariant, instead of a general iterator concept. Iterator categories, sentinels,iterator_traitsand<iterator>'s adaptors have no Pia counterpart. - Lazy views (
<ranges>'sviews::filter,transform,zip,chunk) wait for function values (see the gaps). The eager algorithms over slices come first.
6. Comparators and predicates come from concepts first¶
- Algorithms that compare use
EqandOrdinstances found by argument-dependent lookup (ADR 0034), and hashing usesHash. A different order is a different type: a record that wraps the value and has its ownless, as Rust'sReversedoes. This is the common case in C++ too, and it monomorphises and vectorises. <compare>'sstrong_ordering,weak_orderingandpartial_orderingare variants inPia.Core.Order, withcompare(the three-way comparison) derived fromlessandsame, and@derive(ord)as a library metaprogram (#61).- There are no
_ifvariants (Dean, 2026-11-22).find_if,count_if,remove_if,replace_if,copy_if,partition_pointand the rest are overloads offind,count,remove,replace,copyand so on, chosen by the argument's type (ADR 0034):find xs xtakes a value and needsEq t, andfind xs ptakes a predicatet -> bool. The same goes for an operation or a comparator:sort xsusesOrd t, andsort xs lesstakes a comparator, whose contract requires a strict weak order;accumulate xs inituses+, andaccumulate xs init optakes the operation.all_of,any_ofandnone_ofareall,anyandnone, which take only a predicate. When the elements are themselves functions, the value overload'sEqconstraint fails and soft failure drops it (ADR 0035), so the two forms never clash. - A predicate's contract is part of the overload's. The algorithm's
ensuresnamesp x, so the predicate has to be a pure, total function whose result a spec can mention, and an effectful or partial predicate is rejected at the call. These overloads wait for function values and lambdas, designed separately (see the gaps).
7. Text is UTF-8 bytes¶
stris[u8]refined to valid UTF-8, andstringis avec u8with the same invariant (docs/stdlib.md,Pia.Io).charin C++'s sense isu8, and a code point isu32{0..0x10FFFF}without the surrogates.- There are no wide characters (
wchar_t,<cwctype>,wstring), nochar8_t/char16_t/char32_tstring types and no locales (<locale>,<clocale>,<codecvt>). Classification is ASCII (<cctype>) inPia.Core.Ascii; Unicode properties, normalisation and UTF-16 conversion for foreign APIs come later inPia.Text, from tables generated by metaprograms. std::formatandstd::printarePia.Text.Format: format strings are checked when the program is compiled, as C++20's are, by a metaprogram that reads the literal and the argument types.std::formatterspecialisations are instances of aFormatconcept.
8. Type traits and concepts are metaprograms¶
<type_traits> and most of <concepts> exist because C++ templates cannot ask questions about types directly. In Pia, a metaprogram can (ADR 0035): is_integral t is a type match, make_unsigned is a type-level function, enable_if is a where clause, and std::same_as, std::integral and std::totally_ordered are concepts in Pia.Core.Order and Pia.Core.Int. Pia.Core.Traits collects the type-level metaprograms that turn out to be needed, rather than mirroring the header. <typeinfo>, <typeindex> and <any> have no counterpart: there is no run-time type information, and dynamic values are dyn (ADR 0003).
9. Tuples are a core type¶
Tuples are first-class types and values in the core, as arrays and records are, rather than a library type waiting for variadic generics (Dean, 2026-11-22). This needs a core change of its own, with its own ADR, before <tuple> is done; this section records what the library expects of it.
- Values and types share one syntax.
(a, b, c)is a tuple value and, since types are values,(u8, str, bool)is its type: a tuple of types is a tuple type.()is the empty tuple, the same value asunit. There is no one-element tuple:(x)isxin parentheses. - Access is positional.
t.0,t.1and so on, checked when compiled, so an index out of range is a type error, not a proof goal. Patterns destructure:match t with | (a, b) -> ...andlet (a, b) = t in ...; a pattern is told apart from a function literallet (x) => ...by the=>, as grammar.md already plans. - A tuple is an anonymous record. It has the same layout, ownership and modifier rules as a record whose fields are named
0,1, ...: layout optimisation (ADR 0030), struct-of-arrays buffers ([(u32, f32)]is one buffer per element),movedand@linearelements, borrows, and contracts that namet.0. Tuples of the same element types are the same type wherever they are written, which is what a named record is not. - Equality, order and hashing are structural.
Eq,OrdandHashinstances for a tuple come from its elements' instances, element by element and lexicographically, with the laws proved once for the generic definition (ADR 0043). This is C++'soperator<=>ontuple. - Tuple types are the type lists for variadic code. A type parameter can stand for a tuple type, and a metaprogram walks it with a type
matchon()and on a head and tail, proved terminating by structural recursion as ADR 0035 requires. That is enough forapply,tuple_cat,tuple_size,tuple_element,std::getby type, and argument packs forformatandmake_unique, without a separate pack feature.variant<Ts...>stays a named variant for now: an anonymous sum type is a separate decision. - Foreign code sees a tuple as the C struct of its elements, in the order the layout chooses and advertises in the ABI, as for records.
std::pair is a two-element tuple, std::tie is a tuple pattern, and structured bindings are let (a, b) = ....
10. Maths is proved in Pia, never libm¶
<cmath>, <numbers>, <complex>, floating-point <charconv> and the floating-point distributions of <random> are Pia.Math and wait for floats (M11), with error bounds in their contracts (ADR 0023, ADR 0038). Everything over integers comes first: <bit>, <numeric>, integer <charconv>, <chrono>'s integer durations and calendar, <ratio>, and <random>'s integer engines and uniform_int_distribution.
11. Module layout¶
The library is a tree of modules, one per file (ADR 0039), in lib/, named by path: lib/Pia/Core/Bit.pia holds module Pia.Core.Bit. Each module has a pia_library target in lib/Pia/<Layer>/BUILD.bazel, so it is proved on its own and its dependents build against its interface artifact.
| Module | C++23 headers | Needs |
|---|---|---|
Pia.Core.Bit |
<bit>, <bitset> |
the bit builtins below |
Pia.Core.Int |
<numeric>'s gcd lcm midpoint, <utility>'s cmp_less in_range, <limits>, <climits>, <cstdint>, <cstdlib>'s abs div, C++26's saturating arithmetic |
available now |
Pia.Core.Order |
<compare>, <concepts>'s comparison concepts, <utility>'s swap exchange |
available now |
Pia.Core.Option, Pia.Core.Result |
<optional>, <expected> (<variant> is core sum types) |
available now; monadic operations wait for function values |
Pia.Core.Algo (Search, Modify, Sort, Heap, Set, Perm) |
<algorithm>, <numeric>'s iota accumulate reduce partial_sum inner_product adjacent_difference |
available now for the comparison-based forms |
Pia.Core.Ascii, Pia.Core.Utf8, Pia.Core.Charconv |
<cctype>, <cuchar>, <string_view>, <charconv> for integers |
available now |
Pia.Core.Hash |
std::hash |
available now |
Pia.Core.Fixed |
C++26's inplace_vector, a ring buffer |
value parameters in generic types |
Pia.Core.Rand |
<random>'s integer engines and integer distributions |
available now |
Pia.Core.Ratio, Pia.Time |
<ratio>, <chrono>, <ctime> |
integer durations and the civil calendar now; clocks need Pia.Sys.Clock; time zones need files |
Pia.Mem, Pia.Mem.* |
<memory>, <memory_resource>, <new>, <vector>, <deque>, <list>, <forward_list>, <map>, <set>, <unordered_map>, <unordered_set>, <flat_map>, <flat_set>, <queue>, <stack> |
the allocators (#59, #71) and #72 |
Pia.Text, Pia.Text.Format |
<string>, <format>, <print> |
Pia.Mem.Vec; print needs Pia.Io |
Pia.Io, Pia.Sys.Fs |
<iostream> and the stream headers, <cstdio>, <filesystem> |
Pia.Sys (#57) |
Pia.Rt |
<stacktrace>, <csignal>, <cassert>'s abort |
#58 |
Pia.Math |
<cmath>, <numbers>, <complex>, <cfenv>, <stdfloat> |
floats (M11) |
Pia.Sync, Pia.Task, Pia.Chan |
<atomic>, <thread>, <mutex>, <shared_mutex>, <condition_variable>, <semaphore>, <latch>, <barrier>, <future>, <stop_token>, <generator>, <execution> |
atomics and the concurrency runtime (M12, ADR 0027) |
Pia.Fn |
<functional> |
function values |
Pia.Text.Regex |
<regex> |
later: a linear-time automaton engine, so matching is proved to terminate |
Out of scope, with what replaces each: <exception> and <stdexcept> (error variants), <typeinfo>, <typeindex> and <any> (metaprograms and dyn), <locale>, <clocale>, <codecvt> and <cwctype> (UTF-8 only), <valarray> (vec<T, k> and spread), <csetjmp> (nothing), <cstdarg> (Pia has no C variadics, and calling a variadic C function is not supported), <initializer_list> (array literals), <scoped_allocator> (explicit allocators), <iosfwd>, <streambuf> and <syncstream> (the reader and writer concepts), and <version> (the compiler version is a metaprogram builtin if it is ever needed).
12. Every component is proved and tested¶
- A library module is accepted only when it proves with no unproved goals and no
--allow-unproved.assume,unsafeand@trustedappear only inPia.Sys.PosixandPia.Rt, sopiac --trust-reporton any program that uses only the library lists nothing from it outside those two. - Each module has a lit test in
tests/std/that checks its goal counts, and a test program that runs it on examples, including the edge cases the C++ standard calls out (empty ranges, one element, all equal, the extreme values of each integer type). - Proving the library is part of the build-time benchmark (ADR 0032):
lib/joinstools/bench/baseline.json, and a library PR that raises proof cost by more than the gate allows has to say why.
13. Phases¶
Each phase is a set of issues, one PR each, with a module, its tests, and its row of docs/cpp-std.md updated.
- Phase 0. This ADR, docs/cpp-std.md, and
lib/with its Bazel rules and thetests/stdharness. - Phase 1,
Pia.Corewithout allocation, buildable today: - the core bit builtins ADR 0023 lists and does not have yet (
clz,ctz,popcount,rotl,rotr,bswap), thenPia.Core.Bit(<bit>); Pia.Core.Int(gcd,lcm,midpoint,cmp_lessand friends,in_range, limits, saturating arithmetic);Pia.Core.Order(Eq,Ordwith the strict weak order laws,ordering,compare,swap,exchange, lexicographic comparison);Pia.Core.OptionandPia.Core.Result(<optional>,<expected>without the monadic operations);Pia.Core.Algo.Search:find,count,contains,equal,mismatch,starts_with,ends_with,search,adjacent_find,min_element,max_element,minmax_element,lexicographical_compare,is_sorted,is_sorted_until;Pia.Core.Algo.Modify:fill,copy,copy_backward,swap_ranges,reverse,rotate,shift_left,shift_right,replace,remove,unique, and<numeric>'siota,accumulate,reduce,partial_sum,adjacent_difference,inner_product, with overflow in their contracts;Pia.Core.Algo.SortandHeap(binary search family,make_heapand friends,sortas heapsort first since it needs no recursion or buffer,partial_sort,nth_element,partition,mergeinto anoutslice,is_permutation,next_permutation,prev_permutation), each sort proved to return a sorted permutation;Pia.Core.Algo.Set(includes,set_union,set_intersection,set_difference,set_symmetric_differenceintooutslices);Pia.Core.Ascii,Pia.Core.Utf8and integerPia.Core.Charconv, withto_chars/from_charsround trips proved;Pia.Core.Hash,Pia.Core.Rand(integer engines such asminstd_randandmt19937, and an unbiaseduniform_int),Pia.Core.RatioandPia.Time's durations and civil calendar.- Phase 2, containers and strings over
Pia.Mem, with #72:string,deque,flat_map,flat_set,priority_queue,stack,queue,unordered_map,unordered_set,mapandset(B-trees whose nodes live in a pool),listandforward_list(pool-indexed nodes),box(forunique_ptr, virtual bases andmove_only_function), and the algorithms that need a buffer (stable_sort,stable_partition,inplace_merge). - Phase 3, the outside world, after
Pia.SysandPia.Io(#57, #58):format,print, readers and writers for the stream headers,<filesystem>,<cstdio>,<chrono>'s clocks,<stacktrace>. - Phase 4, floats (M11):
Pia.Math, floating-point<charconv>, the floating-point distributions. - Phase 5, concurrency (M12):
Pia.Sync,Pia.Task,Pia.Chan, as ADR 0027 designs them. - Phase 6, function values: the predicate, operation and comparator overloads of the phase 1 algorithms (C++'s
_ifforms),transform, monadicoptionandresult,<functional>, and<ranges>'s views. - Later:
<regex>,<mdspan>(with value parameters in generic types), time zones, Unicode tables,shared_ptr.
14. Language gaps are listed, not worked around¶
Where the library needs something the language does not have, the library waits for it, and the gap is an issue of its own. Known now:
| Gap | Blocks |
|---|---|
| Imported types and generic functions across interfaces (ADR 0039 step 3) | every program importing a generic library module; until then a module's generics are proved by the definition check, and its tests are in the module's own test file |
Concepts as type arguments (box c: a type-erased box whose contents are proved to meet c; ADR 0046) |
box over a concept, Pia.Fn's dynamic function values, replacements for virtual dispatch |
| Function values, closures and lambdas ("functions as values are not supported yet"; being designed with generic and typed lambdas) | phase 6: predicate, operation and comparator overloads, views, <functional> |
Value parameters in generic types ([t; n] for a parameter n; designed in design.md) |
std::array algorithms generic over N, bitset<N>, inplace_vector<T, N>, mdspan extents |
| Core tuple types, values and patterns (section 9; an ADR of their own) | <tuple>, pair as a tuple, structured bindings, apply, argument packs for format and make_unique, multi-value returns in <algorithm> (minmax, mismatch) |
| Anonymous sum types | std::variant<Ts...> beyond named variants, and visit over one |
| Recursive types (ADR 0047) | ref and weak for shared_ptr and weak_ptr, recursive values such as the JSON DOM (ADR 0045); the node containers use pool indices instead, which is their design rather than a workaround |
Calls inside a callee's ensures are unconstrained at the caller |
composing generic algorithms whose contracts name each other's spec functions |
| Floats (M11), atomics and the concurrency runtime (M12) | phases 4 and 5 |
| Cost proofs | proving C++'s complexity guarantees |
Payloads of fixed-array type in constructors, partial moves, @derive(drop) |
some container element types |
Consequences¶
- A C++ programmer can find every
stdfacility, or why it is missing, from one page, and the page stays current because every library PR updates it. - Pia's library promises more than C++'s: each function's contract is its specification, proved, and C++'s undefined behaviour becomes a compile-time obligation on the caller.
- Some C++ idioms do not survive: iterator pairs, exceptions, default allocators, comparator objects in containers' types, locales and wide strings. Each has a stated replacement.
- The phases are ordered by what the language can do today, so most of
<algorithm>,<bit>,<numeric>,<charconv>and<compare>can land before function values or floats, and the gap table says which language work unblocks the most of the rest. - The library becomes the largest body of proved Pia code, so it is also the main test of the verifier's speed and of ADR 0037's idioms; slow or awkward proofs found here become verifier issues.