skew_list

View Source

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.1, stable API; 28 exports, 73 proved laws. Requires Erlang/OTP 27 or later.

Why

StructureFront cons/tlRead/replace element N
Plain listO(1)O(N)
arrayO(n): every index shiftsO(log n)
queueO(1) amortisedO(n)
skew_listO(1) worst caseO(log n)

API

FunctionCostSame result as
new() -> SO(1)[]
cons(X, S) -> SO(1)[X | L]
hd(S) -> XO(1)erlang:hd/1
tl(S) -> SO(1)erlang:tl/1
length(S) -> NO(1)erlang:length/1
nth(N, S) -> XO(log n)lists:nth/2
update(N, X, S) -> SO(log n)position N replaced; raises as lists:nth/2
nthtail(N, S) -> SO(log n)lists:nthtail/2
to_list(S) -> LO(n)the elements in order
from_list(L) -> SO(length(L))the elements of L; badarg as erlang:length/1
foldl(F, Acc, S) -> AccO(n)lists:foldl/3
foldr(F, Acc, S) -> AccO(n)lists:foldr/3
map(F, S) -> SO(n)lists:map/2; keeps the tree shapes
filter(Pred, S) -> SO(n)lists:filter/2; {bad_filter, V} for a non-boolean
append(S1, S2) -> SO(length(S1))lists:append/2
reverse(S) -> SO(n)lists:reverse/1
last(S) -> XO(log n)lists:last/1
member(X, S) -> boolean()O(n), stops at the first matchlists:member/2
all(Pred, S), any(Pred, S)O(n), stop at the first decisive elementlists: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) -> SO(log n) time and memory (shared subtrees)lists:duplicate/2
zip(S1, S2) -> SO(n), trees paired directlylists: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:

ComponentVersion
Erlang/OTP29.1.3 (compiler 10.0.8)
Elixir1.20.5
Lynx0126a980b13b80d962b20f07713b8c263e475017
Leanleanprover/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.

Operationskew_listplain listarray
nth81 ns499.6 µs30 ns
update112 ns8.3 ms94 ns
cons + tl17 ns7 ns–
memory (words)2,000,0282,000,0001,133,400
last57 ns2.0 ms
sublist, 16 from the middle236 ns502.2 µs
duplicate291 ns, 420 words4.4 ms
foldl3.8 ms9.8 ms
map10.5 ms19.9 ms
reverse10.6 ms18.2 ms
append 16 at the end10.7 ms19.0 ms
append 16 in front367 ns16.7 ms
split in half4.5 ms4.3 ms
member, missing3.0 ms912.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