arXiv:2611.04929cs.PLNov 2026

Manuscript Offprint

arXiv:2611.04929

Properly defining Erlang lists

Albert Schimpf, Stefan Wehr, Annette Bieniusa

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

1 Introduction

Lists and tuples are the most common compound data structures in Erlang. A list is either the empty list [] or a cons cell [H | T], and the tail T may be any term:

[1, 2, 3] [1 | foo] [<<"ab">>, $c | <<"d">>]

% proper: [1 | [2 | [3 | []]]] % improper: the last tail is foo % an iolist: a binary may end the chain

A list whose chain of tails ends in [] is proper; otherwise it is improper. The guard is_list([1 | foo]) succeeds, because it inspects only the outermost constructor, but length([1 | foo]) raises badarg. Moreover, list_to_binary([2 | <<1>>]) returns <<2,1>>, while list_to_binary(<<<1>>) raises badarg.

Erlang’s type language describes lists by an element type and a termination type, through list(T), nonempty_list(T), maybe_improper_list(T, U) and nonempty_improper_list(T, U), and defines iolist() in terms of them (“Types and Function Specifications” 2025; Lindahl and Sagonas 2007).

A type checker for Erlang’s list type has to settle two questions.

The first is how to represent list types so that subtyping is decided correctly, and as precisely as the checker needs. A checker must reject

-spec widen(list(a | b)) -> list(a) | list(b). widen(L) -> L.

because widen([a, b]) returns a list that mixes a and b and is therefore neither a list(a) nor a list(b). How much precision is needed depends on what the checker does with the types. Knowing what the elements of a list can be and how the list ends is enough for a checker that does not look for non-exhaustive or redundant clauses. A checker that could notice that the third clause of

case L of [ ] -> 1; [1 | _] -> 2; [1, 2 | _] -> 3; _ -> 4 end

can never match. What it must not do is report the third clause of

case S of "" -> 1; "trace" -> 2; "trace2" -> 3; _ -> 4 end

which does match the string "trace2". A type that only says that a string pattern is a list of characters cannot tell the second and the third pattern apart. The pattern typing must approximate, the

Authors’ contact information: Albert Schimpf, RPTU Kaiserslautern-Landau, Kaiserslautern, Germany, schimpf@cs.uni-kl.de; Stefan Wehr, Offenburg University of Applied Sciences, Offenburg, Germany, stefan.wehr@hs-offenburg.de; Annette Bieniusa, RPTU Kaiserslautern-Landau, Kaiserslautern, Germany, bieniusa@cs.uni-kl.de.

second clause then seems to match every remaining string, and the third clause therefore seems dead. Avoiding such false alarms requires types that know precisely which element is where.

The second question is what to make of improper lists. Improper lists are rarely used. In the source code of OTP, fewer than 0.2% of the list literals are explicitly improper, and in three applications we measured the only improper list types in specifications are iolist() and iodata(). A checker may decide to ignore them and accept the unsoundness, as eqWalyzer does (todo ref). Still, the list representations may intermingle in unexpected ways. The syntax for lists is also the syntax for improper lists. Consequently, they are easily created by accident. One character separates [X, Y] from [X | Y], and L ++ X builds an improper list whenever L is non-empty and X is not a list. Once created, they reach code that never mentions them.

The operator ++ can even return a value that is not a list: [] ++ foo is foo, although the reference manual says that ++ "returns the resulting list". OTP's own compiler overlooks this case. Its type for ++ assumes a proper list, and OTP 28.5 compiles a function that tests the tail of such a result incorrectly (Section 2.2).

Erlang's standard library has an example of code that unintuitively mixes proper and improper lists. The function is_set/1 of the ordsets module checks whether a term is an ordered set:

-spec is_set(Ordset) -> boolean() when Ordset :: term(). is_set([E|Es]) -> is_set(Es, E); is_set([]) -> true; is_set(_) -> false.

is_set([E2|Es], E1) when E1 < E2 -> is_set(Es, E2); is_set([|_|], _) -> false; is_set([], _) -> true.

The specification promises a boolean for every term, but ordsets:is_set([1 | foo]) crashes with function_clause: the first clause passes the tail foo to the helper is_set/2, whose clauses cover only cons cells and []. A checker finds this bug only if it knows that the tail of a list matched against term() can be any term. For that it has to know what the list types that mention improper lists mean, namely whether [] is a maybe_improper_list(integer(), atom()), and whether a bare foo is. The reference manual does not say. It offers three types for improper lists, maybe_improper_list, nonempty_improper_list and nonempty_maybe_improper_list, and says little more about them than that their arguments are the "contents" and the "termination" and that they "define the set of Erlang terms one would expect" ("Types and Function Specifications" 2025). It does not say how the three differ, and two of them turn out to be names for the same type (Section 2). The developers of Gradualizer found three incompatible readings among themselves and left the question open (Norell 2018). Similarly, Söderqvist (2020) tried to recover the meaning from the output of TypEr. A user reported the definition of iolist() as a bug to both Elixir and OTP, reading the "maybe" in maybe_improper_list as already allowing [], and the maintainers who answered explained the name in different ways (Qqwy 2022a; 2022b). The answers to a long-standing Stack Overflow question disagree on whether an improper list is a list at all (Stack Overflow 2009). Finally, etylizer itself encoded the types wrongly twice, once without [] and once with the bare termination value (Schimpf 2026) (Appendix A.7).

The first question is well-defined. "What is a proper list?" and what list(T) means is not in dispute. Thus, a checker should have no difficulty with this part. We will see that a correct answer takes more care in a set-theoretic type system (Section 3 and Appendix A).

As for the second question, here it is a matter of interpretation. There is no document to be wrong against, and every project had to decide for itself what the improper list types mean.

We first settle what improper lists and their types mean (Section 2). We then show how to implement lists, proper and improper, correctly in a set-theoretic type system (Section 3), using etylizer (Schimpf et al. 2022; 2026), a set-theoretic type checker for Erlang. Finally, we look at other languages and type systems that have similar constructs (Section 4). The appendix records etylizer's earlier approaches and why they failed (Appendix A).

References (44)