collections: ConsStack, a persistent stack with shared tails (C7) #198

Closed
opened 2026-10-08 13:42:01 +00:00 by orion · 2 comments
Collaborator

Goal

ConsStack<Item>: an implementation of Stack as a persistent cons list, so that a copy (var rest = stack) shares its tail and push/pop are O(1) with no copy (COLLECTIONS.md section 3.3 table, section 3.10 point 4). The tour (section 6, question 6) shows it doing what a List cannot.

Acceptance criteria

  • public type ConsStack<Item> with Stack<Item>, From<Iterate<Item>>; push, pop, peek, length, iterate (top first), clear, Equals/Hash as ArrayStack.
  • Copy semantics: pushing or popping on a copy never changes the original, and the copies share nodes (no copy of the tail).
  • ConsStack.of(...) / literal through From as for ArrayStack.
  • Conformance program including the persistence cases (undo/backtracking shape); benchmark against ArrayStack for keeping many versions.

Seed impact

None. A new public type in std/collections written in plain TorbScript over a List; no syntax change, no new native, no runtime change (runtime/list.c stays the one storage, COLLECTIONS.md section 3.10 point 5), and the compiler looks up none of the new names by string (section 6a). Not breaking: only additions.

Size

One PR, one feat(collections) commit (plus tests and docs in the same commit or a second test/docs one).

Common to the C7 types

  • Export from std/collections/src/lib.trb (public use ...), doc comments with the cost profile of the implementation, as ArrayQueue has.
  • A conformance program in tests/conformance/ (.trb + .expected), run natively and in the VM, in the style of queue-grows.trb.
  • A benchmark in benchmarks/ (one small program plus its C counterpart, see benchmarks/README.md) against the array implementation. It is a measurement, not a gate.
  • skills/ regenerated when docs change; the worker does not run gates locally, CI does.

Source

## Goal `ConsStack<Item>`: an implementation of `Stack` as a persistent cons list, so that a copy (`var rest = stack`) shares its tail and `push`/`pop` are O(1) with no copy (COLLECTIONS.md section 3.3 table, section 3.10 point 4). The tour (section 6, question 6) shows it doing what a `List` cannot. ## Acceptance criteria - [ ] `public type ConsStack<Item> with Stack<Item>, From<Iterate<Item>>`; `push`, `pop`, `peek`, `length`, `iterate` (top first), `clear`, `Equals`/`Hash` as `ArrayStack`. - [ ] Copy semantics: pushing or popping on a copy never changes the original, and the copies share nodes (no copy of the tail). - [ ] `ConsStack.of(...)` / literal through `From` as for `ArrayStack`. - [ ] Conformance program including the persistence cases (undo/backtracking shape); benchmark against `ArrayStack` for keeping many versions. ## Seed impact None. A new public type in `std/collections` written in plain TorbScript over a `List`; no syntax change, no new native, no runtime change (`runtime/list.c` stays the one storage, COLLECTIONS.md section 3.10 point 5), and the compiler looks up none of the new names by string (section 6a). Not breaking: only additions. ## Size One PR, one `feat(collections)` commit (plus tests and docs in the same commit or a second `test`/`docs` one). ## Common to the C7 types - Export from `std/collections/src/lib.trb` (`public use ...`), doc comments with the cost profile of the implementation, as `ArrayQueue` has. - A conformance program in `tests/conformance/` (`.trb` + `.expected`), run natively and in the VM, in the style of `queue-grows.trb`. - A benchmark in `benchmarks/` (one small program plus its C counterpart, see `benchmarks/README.md`) against the array implementation. It is a measurement, not a gate. - `skills/` regenerated when docs change; the worker does not run gates locally, CI does. ## Source - [COLLECTIONS.md C7](https://git.torb.dev/torbscript/language/blob/main/docs/design/COLLECTIONS.md#L992), parent #139
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-20261008 until 2026-10-08T20:05:00Z

[loop:torbscript] claim implement by session:loop-torbscript-20261008 until 2026-10-08T20:05:00Z
Author
Collaborator

[loop:torbscript] implement: PR #202 open, CI green, next: review

[loop:torbscript] implement: PR https://git.torb.dev/torbscript/language/pulls/202 open, CI green, next: review
argus closed this issue 2026-10-08 15:09:57 +00:00
Sign in to join this conversation.
No labels
loop
ready
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language#198
No description provided.