fix: value-semantics for collection keys/elements; set literals evaluate

Close jolt-do7. Maps/sets keyed by a collection (a map, vector, ...) now compare
by value instead of Janet identity:
- PHM/PHS hash and compare keys through an injected canonicalizer (collection
  keys -> value-hashable struct/tuple); keys are still stored as-is
- map literals and core-assoc promote to a phm when a key is a collection
- frequencies/group-by use a phm base so collection elements/keys dedup by value
- set equality is value-based (from earlier)

Real bugs found and fixed along the way:
- set literals #{(inc 1)} did not evaluate their elements (stored raw forms!)
- the REPL printer rendered phm maps as {} (they hit the deftype branch); now a
  phm branch prints entries

Added spec cases (maps/collection-keys, sets/literals & value elements).
conformance 218/218, jpm test green.
This commit is contained in:
Yogthos 2026-06-05 01:02:00 -04:00
parent ad5539b0de
commit 0db7eb6ac8
6 changed files with 102 additions and 17 deletions

View file

@ -54,3 +54,11 @@
["count of nil map" "0" "(count nil)"]
["get from nil" "nil" "(get nil :a)"]
["immutability" "true" "(let [m {:a 1} n (assoc m :b 2)] (and (= m {:a 1}) (= n {:a 1 :b 2})))"])
(defspec "map / collection keys (by value)"
["vector key literal" ":v" "(get {[1 2] :v} [1 2])"]
["map key literal" ":v" "(get {(hash-map :a 1) :v} {:a 1})"]
["assoc vector key" ":v" "(get (assoc {} [1 2] :v) [1 2])"]
["key across repr" ":v" "(get (assoc {} (vec [1 2]) :v) [1 2])"]
["frequencies of maps" "2" "(get (frequencies [{:a 1} (hash-map :a 1)]) {:a 1})"]
["group-by collection key" "1" "(count (group-by identity [{:a 1} (hash-map :a 1)]))"])

View file

@ -23,6 +23,13 @@
["set as fn present" "2" "(#{1 2 3} 2)"]
["set as fn missing" "nil" "(#{1 2 3} 9)"])
(defspec "set / literals & value elements"
["literal evaluates elements" "#{2 4}" "#{(inc 1) (* 2 2)}"]
["map elements by value" "true" "(= #{{:a 1}} #{(hash-map :a 1)})"]
["contains? map by value" "true" "(contains? #{(hash-map :x 1)} {:x 1})"]
["dedup equal maps" "1" "(count (set [{:a 1} (hash-map :a 1)]))"]
["vector elements" "true" "(contains? #{[1 2]} (vec [1 2]))"])
(defspec "clojure.set"
["union" "#{1 2 3 4}" "(do (require (quote [clojure.set :as s])) (s/union #{1 2} #{3 4}))"]
["intersection" "#{2}" "(do (require (quote [clojure.set :as s])) (s/intersection #{1 2} #{2 3}))"]