From 12de24e3a045472bb40745983a2d3ad73099e3d0 Mon Sep 17 00:00:00 2001 From: giles Date: Thu, 7 May 2026 09:32:30 +0000 Subject: [PATCH] =?UTF-8?q?haskell:=20Phase=2011=20=E2=80=94=20Data.Map=20?= =?UTF-8?q?bulk=20ops=20(fromList/toList/toAscList/keys/elems)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- lib/haskell/map.sx | 46 +++++++++++++++++++++++++++++++++++ plans/haskell-completeness.md | 10 +++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/haskell/map.sx b/lib/haskell/map.sx index cc30f288..da9671d8 100644 --- a/lib/haskell/map.sx +++ b/lib/haskell/map.sx @@ -274,3 +274,49 @@ (hk-map-left m) (hk-map-delete k (hk-map-right m)))) (:else (hk-map-glue (hk-map-left m) (hk-map-right m))))))))) + +(define + hk-map-from-list + (fn + (pairs) + (reduce + (fn (acc p) (hk-map-insert (first p) (nth p 1) acc)) + hk-map-empty + pairs))) + +(define + hk-map-to-asc-list + (fn + (m) + (cond + ((hk-map-empty? m) (list)) + (:else + (append + (hk-map-to-asc-list (hk-map-left m)) + (cons + (list (hk-map-key m) (hk-map-val m)) + (hk-map-to-asc-list (hk-map-right m)))))))) + +(define hk-map-to-list hk-map-to-asc-list) + +(define + hk-map-keys + (fn + (m) + (cond + ((hk-map-empty? m) (list)) + (:else + (append + (hk-map-keys (hk-map-left m)) + (cons (hk-map-key m) (hk-map-keys (hk-map-right m)))))))) + +(define + hk-map-elems + (fn + (m) + (cond + ((hk-map-empty? m) (list)) + (:else + (append + (hk-map-elems (hk-map-left m)) + (cons (hk-map-val m) (hk-map-elems (hk-map-right m)))))))) diff --git a/plans/haskell-completeness.md b/plans/haskell-completeness.md index ea524e0d..c06cded0 100644 --- a/plans/haskell-completeness.md +++ b/plans/haskell-completeness.md @@ -184,7 +184,7 @@ No OCaml changes are needed. The view type is fully representable as an SX dict. Leaf: `("Map-Empty")`. - [x] Core operations: `empty`, `singleton`, `insert`, `lookup`, `delete`, `member`, `size`, `null`. -- [ ] Bulk operations: `fromList`, `toList`, `toAscList`, `keys`, `elems`. +- [x] Bulk operations: `fromList`, `toList`, `toAscList`, `keys`, `elems`. - [ ] Combining: `unionWith`, `intersectionWith`, `difference`. - [ ] Transforming: `foldlWithKey`, `foldrWithKey`, `mapWithKey`, `filterWithKey`. - [ ] Updating: `adjust`, `insertWith`, `insertWithKey`, `alter`. @@ -304,6 +304,14 @@ No OCaml changes are needed. The view type is fully representable as an SX dict. _Newest first._ +**2026-05-07** — Phase 11 bulk operations (fromList/toList/toAscList/keys/elems): +- `hk-map-from-list` uses SX `reduce` — left-to-right, so duplicates resolve + with last-wins (matches GHC `fromList`). `to-asc-list` is in-order recursive + traversal returning `(list (list k v) ...)`. `to-list` aliases `to-asc-list`. + `keys` and `elems` are similar in-order extracts. All take SX-level pairs; + the Haskell-layer wiring (next iterations) translates Haskell cons + tuple + representations. + **2026-05-07** — Phase 11 core operations on `Data.Map` BST: - Added `hk-map-singleton`, `hk-map-insert`, `hk-map-lookup`, `hk-map-delete`, `hk-map-member`, `hk-map-null`. Insert recurses with `hk-map-balance` to