Skip to content

Implement BTreeMap and BTreeSet (§11.7, §18.6) #414

Closed

Description

@ehartford

Summary

Add BTreeMap and BTreeSet to the standard collection surface promised by §11.7, §18.6, collection literals, and comprehensions. These should be ordinary safe stdlib collections with deterministic ordered iteration, not aliases to HashMap/HashSet.

Spec

§11.7's Contains table includes BTreeSet[T] for T: Ord and BTreeMap[K, V] for K: Ord, both with O(log n) key/element lookup. §18.6's module map lists std.collections as the home for Vec, HashMap, HashSet, BTreeMap, SlotMap, and Handle. §4.3c and §13.6 use BTreeSet/BTreeMap as expected-type targets for literals and comprehensions.

Current state

lib/std/collections.w defines Vec[T], HashMap[K, V], HashSet[T], Handle[T], SlotMap[T], and related iterator/entry helpers, but no BTreeMap or BTreeSet (lib/std/collections.w:8-58). lib/std/traits.w:14-15 defines Ord, but the existing HashMap/HashSet runtime path is hash-table-based and not ordered. docs/with-specification.md:4831-4834 names BTree membership behavior and docs/with-specification.md:9946 names BTreeMap in the collection module map.

Implementation plan

  1. Land Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 first if generic collection methods still rely on erased TY_GENERIC_INST; ordered collections should be implemented as real generic stdlib types, not hardcoded type-name branches.
  2. Add pub type BTreeMap[K, V] and pub type BTreeSet[T] in lib/std/collections.w with stable node storage. A pragmatic first implementation can use sorted Vec-backed storage if documented complexity is temporarily not O(log n), but the issue should not be closed until the O(log n) tree representation or a follow-up split is explicit.
  3. Implement new, len, is_empty, clear, insert, get, contains, remove, ordered iteration, keys, and values for BTreeMap; implement new, insert, contains, remove, ordered iteration, and set operations for BTreeSet.
  4. Add Contains/operator support through normal trait methods once Prelude operator traits Add/Sub/Mul/Div/MatMul/Neg (§11.7) #469/Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 support the trait path. Avoid adding BTree* branches to every compiler builtin table unless the bootstrap sequence requires a temporary shim.
  5. Wire collect[BTreeMap], collect[BTreeSet], collection literals, and comprehensions through Implement collect[C] multi-target (HashMap, HashSet, BTreeMap, BTreeSet, String) #388/Implement collection literals: map literals [k: v], empty [:], expected-type targets (§4.3c) #389/Implement target-polymorphic comprehensions incl. map form [k: v for ...] (§13.6) #390.
  6. Ensure deterministic traversal order. This matters for fixpoint stability and should be called out in tests.

Test plan

Add test/spec/spec_ss11_7_btree_contains.w for in/not in on BTreeSet and key membership on BTreeMap.

Add test/spec/spec_ss18_6_btree_collections.w covering construction, insertion, replacement, removal, ordered iteration, and values/keys.

After #388/#389/#390, add literal/comprehension coverage:

  • let s: BTreeSet[i32] = [3, 1, 2]
  • let m: BTreeMap[str, i32] = ["b": 2, "a": 1]
  • [x for x in xs] with expected BTreeSet
  • [k: v for ...] with expected BTreeMap

Acceptance criteria

  • BTreeMap and BTreeSet are exported from std.collections/prelude consistently with existing collections.
  • Lookup and membership require Ord keys/elements and produce ordered deterministic behavior.
  • Insert, replace, remove, iteration, and clear behave correctly for empty, singleton, duplicate, and multi-level cases.
  • Literal/comprehension/collect integration works once dependent issues land.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#391 is a prerequisite for a non-intrinsic generic implementation. #388, #389, and #390 integrate BTree collections with collect, literals, and comprehensions.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.7.1.46, 11.7.1.47.

Activity

  1. ehartford commented on Jun 15, 2026

    @ehartford
    ContributorAuthor

    Implemented in d73ebc8. Added real stdlib BTreeMap/BTreeSet APIs with deterministic ordered semantics, Ord enforcement, insert/replace/remove/clear, ordered keys/values/items, set operations, Contains support, and integration with collect, collection literals, and comprehensions. Added positive spec coverage and negative Ord diagnostics. Follow-up for replacing the bootstrap sorted-Vec storage with an O(log n) tree representation is #591. Rebased remote build also required 070ae08 to keep the seed-built with-sha256 utility bootstrap-compatible. Verification after rebase passed: build, fixpoint, full test suite, and explicit test-green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions