ocaml: phase 6 Map.Make / Set.Make functors (+4 tests, 418 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 45s

Both written in OCaml inside lib/ocaml/runtime.sx:

  module Map = struct module Make (Ord) = struct
    let empty = []
    let add k v m = ... (* sorted insert via Ord.compare *)
    let find_opt / find / mem / remove / bindings / cardinal
  end end

  module Set = struct module Make (Ord) = struct
    let empty = []
    let mem / add / remove / elements / cardinal
  end end

Sorted association list / sorted list backing — linear ops but
correct. Strong substrate-validation: Map.Make is a non-trivial
functor implemented entirely on top of the OCaml-on-SX evaluator.
This commit is contained in:
2026-05-08 15:50:22 +00:00
parent cd93b11328
commit 85867e329b
3 changed files with 113 additions and 2 deletions

View File

@@ -389,6 +389,90 @@
| Some v -> v
let mem t k = _hashtbl_mem t k
let length t = _hashtbl_length t
end ;;
module Map = struct
module Make (Ord) = struct
let empty = []
let rec add k v m =
match m with
| [] -> [(k, v)]
| (k2, v2) :: rest ->
begin
let c = Ord.compare k k2 in
if c = 0 then (k, v) :: rest
else if c < 0 then (k, v) :: m
else (k2, v2) :: add k v rest
end
let rec find_opt k m =
match m with
| [] -> None
| (k2, v) :: rest ->
if Ord.compare k k2 = 0 then Some v
else find_opt k rest
let find k m =
match find_opt k m with
| None -> failwith \"Map.find: not found\"
| Some v -> v
let rec mem k m =
match m with
| [] -> false
| (k2, _) :: rest -> if Ord.compare k k2 = 0 then true else mem k rest
let rec remove k m =
match m with
| [] -> []
| (k2, v) :: rest ->
if Ord.compare k k2 = 0 then rest else (k2, v) :: remove k rest
let rec bindings m = m
let rec cardinal m =
match m with
| [] -> 0
| _ :: t -> 1 + cardinal t
end
end ;;
module Set = struct
module Make (Ord) = struct
let empty = []
let rec mem x s =
match s with
| [] -> false
| h :: t ->
let c = Ord.compare x h in
if c = 0 then true
else if c < 0 then false
else mem x t
let rec add x s =
match s with
| [] -> [x]
| h :: t ->
let c = Ord.compare x h in
if c = 0 then s
else if c < 0 then x :: s
else h :: add x t
let rec remove x s =
match s with
| [] -> []
| h :: t ->
if Ord.compare x h = 0 then t else h :: remove x t
let rec elements s = s
let rec cardinal s =
match s with
| [] -> 0
| _ :: t -> 1 + cardinal t
end
end")
(define ocaml-stdlib-loaded false)