3

I wrote a interface for a map data structure in an MLI file and then the implementation of maps using association lists in a file of the same name but with extension of "ML". this works. but now I want to write a second implementation of the map interface using Direct Address Map and may be a 3rd implementation of map using binary search trees.

How do I do this? As per ocaml documentation I must have the MLI and ML file names the same for them to work.

If I pick a different name ex: map_interace.ml and associationlist_map.ml and binarysearchtree_map.ml then dune complains that it cannot even see the module signatures defined in "map_interface.ml".

What I want to do is to define the interface signatures of a map in one file and then have 3 separate files for 3 separate implementations of the map and 3 separate files for test cases for each of the map implementations.

but because of the file naming conventions of ocaml and dune I am stuck because they need the same file name for MLI, implementation and the test case. (sorry if I'm wrong here).

What is the right way to structure my project ... what kind of file names should I chose?

Edit: I found this thread on googling. Interface with multiple implementations in OCaml but it seems that its asking me to implement the same interface again and again in different MLI files. and this doesn't make any sense to me. why should I write the interface 3 times?

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
  • 1
    You probably want some variation of the [_intf trick](https://www.craigfe.io/posts/the-intf-trick) where you define a single Foo_intf.ml that has the module signature defined as `module type INTF = sig ... end`, and then do `include Foo_intf.Intf` from each mli file – zehnpaard Mar 14 '22 at 20:47
  • 1
    actually, maybe it would be easiest to just define a single `map.mli` without a corresponding `map.ml` and then do `include Map` from `associationlist_map.mli` and `binarysearchtree_map.mli` – zehnpaard Mar 14 '22 at 21:03
  • 2
    Why do you need an explicit interface in the first place? And why do you need it to be the exact same interface for all implementations? `.mli` files are entirely optional, and you don't need an explicit interface to swap one implementation for another. Unless you want to pass them into a functor, but even then you don't need the implementations to explicitly reference the same interface. Since modules are structurally typed they'll fit as long as they include the things you're actually using. – glennsl Mar 14 '22 at 21:59
  • 2
    Also, is it important that the interfaces are exactly the same? Or do you want different implementations to be instantiable with different parameters, for example? Or have some implementations-specific operations, but share a common set across implementations? In OCaml, interfaces applied to implementations will only hide information. It will not assign some notion of equality between implementations that reference the same interface. – glennsl Mar 14 '22 at 22:04

0 Answers0