ADR 0057: Lists of lists¶
Status: Final
Type: Feature
Created: 2026-10-05
Related-ADRs: 0024 (the cascade and the cycle the refusal cited, under its "Release"; and the former 0036 and 0037, instance elements and list fields, the precedents), 0027 (the list), 0031 (member types from a position), 0035 (the l kind)
Context¶
POST Python's grammar allows List[annotation] recursively, so list[list[int]] has always been POST. postpile refused it at three sites (the annotation, a literal, and a comprehension), citing ADR 0024's cascade and cycle. A nested container would be the first element type with an outgoing reference, so the cascading free could become unbounded and a reference cycle constructible.
Neither ground holds for a list of lists:
- No cycle can be built. A static type nests finitely: a
list[list[int]]cannot contain itself, because the element type islist[int]. - The cascade is bounded. The release recurses at most as deep as the annotation is written.
ADR 0036 (now part of 0024) had found the same shape for instances: the refusal had outlived its grounds. This one had too. The fourth naive application (tools/naive/life.py, a grid as list[list[bool]]) is what showed it.
Decision¶
list[list[T]] is a list whose element is a list, at any depth the annotation writes, for every T a list already holds (a scalar, str, or an instance).
The element position has a dedicated dtype, minted per inner element. nested_list_dtype(T) is cached, so any pair of list[list[int]] positions agree by identity, as Point positions do. It is a position only. The value read out of it is a List whose element is T, recorded on the value as for any list. A row then supports everything a list already does. g[y][x], g[y].append(v), len(g[0]), for v in g[y] and print(g) all work through _list_local, which gates list operations. It reads a subscript chain once the outer element is known to be a list, so a subscript that is not one is never evaluated twice.
The runtime gets one element kind and three helpers. l, reserved since ADR 0035, is now released and retained by __pp_elem_incref/decref. That makes the outer list's release walk, slice copy and splice exact. __pp_list_append_list, __pp_list_get_list and __pp_list_set_list are the instance trio with the list's refcount:
- a read borrows:
LIST_BORROWINGlists__pp_list_get_list; the frontend marks the value as it does for a list-typed field; - a store retains the new row before releasing the old one, for
g[0] = g[0].
A list of lists compares and sorts. ==, !=, <, <=, > and >= lower between two lists nested to the same depth whose leaves are ints and bools, or strs, or tuples of those, and xs.sort() and sorted(xs) lower over such a list. CPython compares two lists element by element, trying identity before ==, and orders them by the first element that differs, then by length. For these leaves identity and == agree, so a three-way comparison that recurses through the rows (__pp_list_cmp, with an l case) gives CPython's answer. The sort is the runtime's stable merge sort with that comparator, so equal rows keep their order, as Timsort keeps them. A leaf that is an unannotated [] is provably empty and meets any leaf. A float leaf is refused, since a NaN would decide, as for a flat float list (A192). Lists nested to different depths, and int leaves against str leaves, are refused: CPython's == is False there and < raises TypeError.
A comprehension's element takes the declared row type: back: list[list[int]] = [[] for _ in range(n)] builds rows of int, as a list literal's elements do.
A list of sets. A set element is a dedicated position, minted per member type as a row is (set_elem_dtype) and released as e. The list unit has three accessors for it. The read is borrowed, classified with the map runtime's borrowers; append and store retain the new set before releasing the old. A literal, a comprehension of set() and append build one, and an index reads and stores. for, len, +, *, slicing, del, pop, insert, extend, reverse, clear, copy.deepcopy, any, all, and print, str and f-strings of it lower, the last three showing each set in this runtime's order (A179). What compares sets is refused, with its reason: in, count, index and remove, which compare by ==, == between two lists of sets, and sorted, sort, min and max, which would order sets by inclusion.
Consequences¶
- An element store
g[y] = []takes its member type fromg, as a slice store does (ADR 0031's fourth position, one level in). tests/b_integration/test_nested_lists.pyholds the differential on every backend, withPOST-LIST live=0andPOST-OBJ live=0.test_asan.pygained a program that reads, aliases, replaces and drops rows 300 times.- The p2w corpus gains
chained_assignment.py(matrix[0][0] = matrix[1][1] = 1) on C and LLVM. QBE 1.3 cannot build that whole program: its register allocator dies with "no more pm slots", although each section builds alone. That limit belongs to the QBE backend on large script mains; it is held as a strictTOOLCHAIN_LIMITSentry in the corpus test. - linalg, which sorts a
list[list[list[int]]]and compares the rows, and loop, which keeps a list of sets, compile and print CPython's output apart from their timing lines.