Add from-array constructors for all collection types#7
Conversation
There was a problem hiding this comment.
Build & Tests
- Build: pass (local aarch64 + CI on macOS/Ubuntu). Pre-existing compiler warnings about
prn/deletefor Rc types (not new). - Tests: pass for 8/10 collection types. All new
from-arraytests green:- List (27/27), Queue (25/25), Deque (23/23), Trie (26/26), Vector (20/20), Heap (15/15), OrdMap (34/34), OrdSet (22/22)
- HashMap and HashSet crash with a pre-existing null-pointer dereference in Lambda dispatch (verified: same crash on
mainwithout this PR's changes). Not caused by this PR.
Findings
None. Examined all 10 from-array implementations in detail:
-
List — manual reverse iteration with
prependis the correct approach.Array.reducewithprependwould reverse the order; iterating end-to-start and prepending preserves it. Thei = (- (Array.length arr-ref) 1)initializer handles the empty-array case correctly (i = -1, loop body skipped). -
Value-based collections (Queue, Deque, Vector, OrdSet, HashSet, Heap) — all use
Array.reducewith the collection's insert/enqueue/push-back, copying each element with@x. Verified each function's signature: all take value by-value and collection by-ref. Types match. -
Pair-based collections (Trie, OrdMap, HashMap) — all use
Array.reduce, copying key and value from each pair. Trie'sinserttakes(Ref (Array %key-part-type))for the key (by ref) and%value-typeby value — thefrom-arraycorrectly passes(Pair.a p)(a ref) and@(Pair.b p)(a copy). OrdMap and HashMap both take key and value by value, so@(Pair.a p)and@(Pair.b p)are correct. -
Type signatures — all
from-arraysignatures correctly match their collection's element/pair types and return%name. Trie'sfrom-arraycorrectly requires(Ref (Array (Pair (Array %key-part-type) %value-type)) q). -
Tests — each collection has a correctness/round-trip test and an empty-array edge case. The round-trip tests (
from-arraythento-array) verify both order preservation and element integrity. -
Note on HashMap/HashSet: the PR adds
from-arrayto these types and includes tests, but the tests can't be verified locally due to the pre-existing crash. CI passes, so these tests run successfully on the CI machines (likely different UBSan configuration or compiler version).
Verdict: merge
All implementations are type-correct, order-preserving, and handle edge cases. CI is green on both platforms. The HashMap/HashSet crashes are pre-existing and unrelated.
Summary
All collection types had
to-arraybut nofrom-array. This addsfrom-arrayconstructors to all 10 collection types, enabling convenient construction from existing array data.Value-based collections (take
(Ref (Array T))):prependArray.reducewithenqueueArray.reducewithpush-backArray.reducewithinsertArray.reducewithinsertArray.reducewithinsertArray.reducewithpush-backPair-based collections (take
(Ref (Array (Pair K V)))):Array.reducewithinsertArray.reducewithinsertArray.reducewithinsertEach
from-arrayincludes two tests: a round-trip or correctness check, and an empty-array edge case.Note: the task mentioned Stack but no
define-stackmacro exists in the codebase, so it was skipped.Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.