Extension of the module from the card in OCaml

I have a StringMap module created by the Map.Make functor specified for the String type:

 module StringMap = Map.Make(String) 

In addition to the usual operations provided by Map , I would like to add additional definitions in this module, for example, my_own_function , so that I can call StringMap.my_own_function . Does anyone know where I should define such functions and their signature?

+4
source share
1 answer

You can use the include keyword inside the new module to add all the same functions. This was also extended to the signature in OCaml 3.12 .

 module StringMap = struct include Map.Make(String) end 

If you want to access the map structure, you need to add a special external function Obj.magic or %identity . Type redefinition must be accurate since type checking is not performed,

 module Make (Ord : Map.OrderedType) = struct include Map.Make(Ord) type 'a impl = Empty | Node of 'a impl * key * 'a * 'a impl * int external impl_of_t : 'at -> 'a impl = "%identity" external t_of_impl : 'a impl -> 'at = "%identity" let cardinal map = let rec cardinal = function | Empty -> 0 | Node(l,_,_,r,_) -> cardinal l + 1 + cardinal r in cardinal (impl_of_t map) end 
+6
source

Source: https://habr.com/ru/post/1388233/


All Articles