Search code examples
ocaml

OCaml map of int keys :: where is the 'simple' int module to use with the Map.Make functor?


I need an OCaml map with keys of type int so I am using Map.Make to create one. However it seems that the standard modules 'only' provide modules like Big_int, Int32, Int64 and Nativeint which require conversions. So I have to do things like:

module IntMap = Map.Make(Int32) 
let a_map = IntMap.add (Int32.of_int 0) "zero" IntMap.empty ;;

... which I would rather avoid or define my own silly Int module do deal with simple int literals or values without requiring conversion functions:

module Int = struct                       
   type t = int                                              
   let compare x y = if x < y then -1 else if x > y then 1 else 0 end ;;  
module IntMap = Map.Make(Int) 
let a_map = IntMap.add 0 "zero" IntMap.empty ;;

Am I missing something obvious here?


Solution

  • The simplest way to have an int map is to do the following:

    module IntMap = Map.Make(struct type t = int let compare = compare end)