skew_list

Okasaki's skew binary random-access list for Erlang: a persistent sequence with O(1) worst-case cons, hd and tl, and O(log n) nth, update and nthtail. Positions are 1-based and argument order follows lists. Every export has laws comparing it with the plain-list operation, proved in Lean 4 with Lynx.

Status: 1.0.0, stable API; 28 exports, 73 proved laws.

Why

Structure Front cons/tl Read/replace element N
Plain list O(1) O(N)
array O(n): every index shifts O(log n)
queue O(1) amortised O(n)
skew_list O(1) worst case O(log n)

API

Function Cost Same result as
new() -> S O(1) []
cons(X, S) -> S O(1) [X | L]
hd(S) -> X O(1) erlang:hd/1
tl(S) -> S O(1) erlang:tl/1
length(S) -> N O(1) erlang:length/1
nth(N, S) -> X O(log n) lists:nth/2
update(N, X, S) -> S O(log n) position N replaced; raises as lists:nth/2
nthtail(N, S) -> S O(log n) lists:nthtail/2
to_list(S) -> L O(n) the elements in order
from_list(L) -> S O(length(L)) the elements of L; badarg as erlang:length/1
foldl(F, Acc, S) -> Acc O(n) lists:foldl/3
foldr(F, Acc, S) -> Acc O(n) lists:foldr/3
map(F, S) -> S O(n) lists:map/2; keeps the tree shapes
filter(Pred, S) -> S O(n) lists:filter/2; {bad_filter, V} for a non-boolean
append(S1, S2) -> S O(length(S1)) lists:append/2
reverse(S) -> S O(n) lists:reverse/1
last(S) -> X O(log n) lists:last/1
member(X, S) -> boolean() O(n), stops at the first match lists:member/2
all(Pred, S), any(Pred, S) O(n), stop at the first decisive element lists:all/2, lists:any/2
sublist(S, Len), sublist(S, Start, Len) O(Len + log n) lists:sublist/2,3
split(N, S) -> {S1, S2} O(N + log n) lists:split/2
duplicate(N, X) -> S O(log n) time and memory (shared subtrees) lists:duplicate/2
zip(S1, S2) -> S O(n), trees paired directly lists:zip/2; function_clause for unequal lengths
unzip(S) -> {S1, S2} O(n) lists:unzip/1; function_clause for a non-pair
mapfoldl(F, Acc, S), mapfoldr(F, Acc, S) O(n) lists:mapfoldl/3, lists:mapfoldr/3; {badmatch, V} for a non-pair result

Functions that take a fun raise function_clause if it is not a fun of the right arity, even on an empty list, and pass through whatever the fun raises. Two skew lists are =:= exactly when their elements are: each length has one shape.

What is proven

The laws are in the law catalogue. All 73 are kernel-checked on:

Component Version
Erlang/OTP 29.1.3 (compiler 10.0.8)
Elixir 1.20.5
Lynx 0126a980b13b80d962b20f07713b8c263e475017
Lean leanprover/lean4:v4.36.0
skew_list.beam MD5 (beam_lib:md5/1) 6a761aaf2d4f962af6b5df765f86dbbe

proofs/verify.exs records these in proofs/_build/verification_report.json; the same OTP and compiler produce the same .beam.

For the eight functions that take a fun, the result or exception equals that of lists for every deterministic fun (one whose outcome depends only on its arguments: no process dictionary, ETS, self(), time, rand or I/O). That is a Lean lemma (*_agrees) over every Lynx dispatcher. That F is called with the same arguments in the same order as by lists is proved in the Lean model and tested, but not checked against the compiled code. The laws themselves range over nine pure sample funs in skew_list_laws:samples/0.

Trusted, not proven: Lynx's translator and runtime model (including its models of lists:reverse/2, lists:member/2 and ++), the compiler back end, the runtime system, Lean's kernel, the reference functions elements/1 and ref_update/3, that a deterministic fun behaves like some dispatcher's pure, and that the compiled module's translation describes how it runs in the caller. Exceptions match lists on the pinned OTP (hd/1, tl/1 and from_list/1 raise the BIFs' own badarg on any release). Other releases are covered by EUnit and PropEr only; the laws also held unchanged on OTP 27.3.4.20 and 28.5.0.5.

Benchmarks

At 1,000,000 elements, OTP 29.1.3 on an Intel Core i9-14900KF (Linux). Full tables from 16 elements up are in the benchmark results; skew_list_bench reproduces them.

Operation skew_list plain list array
nth 81 ns 499.6 µs 30 ns
update 112 ns 8.3 ms 94 ns
cons + tl 17 ns 7 ns –
memory (words) 2,000,028 2,000,000 1,133,400
last 57 ns 2.0 ms
sublist, 16 from the middle 236 ns 502.2 µs
duplicate 291 ns, 420 words 4.4 ms
foldl 3.8 ms 9.8 ms
map 10.5 ms 19.9 ms
reverse 10.6 ms 18.2 ms
append 16 at the end 10.7 ms 19.0 ms
append 16 in front 367 ns 16.7 ms
split in half 4.5 ms 4.3 ms
member, missing 3.0 ms 912.5 µs

For foldl through append, the plain-list column converts with to_list/1, runs the lists function and converts back; the other rows run the lists function on a plain list. queue matches skew_list on cons + tl; array reads 3 to 5 times faster but has no cheap front insert. Times can move by up to 1.5x with where garbage collections fall.

License

MIT