Map(3)

πŸ’– Map(3) πŸ’–

okay so bestie, welcome to the Map page from the OCaml library manual β€” yes that's the whole header, Map(3) and all, we're not copying it, we're curating it ✨


🌸 NAME 🌸

Map β€” association tables over ordered types. that's it, that's the vibe πŸŽ€


πŸ¦‹ Module πŸ¦‹

the module is literally called Module Map. main character energy fr πŸ’…


πŸ‘‘ Documentation πŸ‘‘

so it's Module Map, and it has this thing : sig end β€” yeah, sig end, iconic, we love a signature 🫢

✨ the whole concept ✨

Association tables over ordered types. πŸ’–

this module implements applicative association tables, also known as finite maps or dictionaries (iykyk), and you get all of that given a total ordering function over the keys. the keys need a total order, that's the whole deal πŸ”₯

and listen β€” all operations over maps are purely applicative (no side-effects). no sneaky mutation, no surprises, no cap. πŸ’€

the implementation uses balanced binary trees, which is why searching and insertion take time logarithmic in the size of the map. logarithmic bestie, that's the good stuff ⭐

🌈 for instance 🌈

okay so here's the example, and it ate:

module IntPairs =
  struct
    type t = int * int
    let compare (x0,y0) (x1,y1) =
      match Stdlib.compare x0 x1 with
          0 -> Stdlib.compare y0 y1
        | c -> c
  end

module PairsMap = Map.Make(IntPairs)

let m = PairsMap.(empty |> add (0,1) "hello" |> add (1,0) "world")

so what's happening here? this creates a new module PairsMap, with a new type 'a PairsMap.t of maps from int * int to 'a. in this example, m contains string values, so its type is string PairsMap.t. literally that simple, slay πŸ’…βœ¨

🧚 the module types 🧚

  • module type OrderedType = sig end β€” input signature of the functor Map.Make. yes, sig end. yes, it's empty-looking here. yes, it's still important πŸ™ˆ
  • module type S = sig end β€” output signature of the functor Map.Make. also sig end. balance πŸ’–

πŸ“ the functor πŸ“

  • module Make : (Ord : OrderedType) -> sig end β€” functor building an implementation of the map structure given a totally ordered type. like, you hand it an Ord that's an OrderedType, and it hands you back a sig end. that's the trade, that's the deal, periodt πŸ‘‘
ngl, the sig ends look like a lot of nothing, but that's exactly what the page says, so we're keeping them exactly as-is bestie 🫢

and that's the whole page! πŸŽ€ source/version and date realness: this came from OCamldoc, dated 2026-06-22, and it's page Map(3). mwah, go build some balanced binary trees πŸ’œπŸŒˆπŸ¦‹

πŸ“¦ where this page comes from