Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which .cma file corresponds with which module in OCaml?

Tags:

ocaml

If I'm programming in the OCaml toploop and I want to use a package from the OCaml standard library or some other library, how I can find out which .cma file to load? In the standard library for example, String is in str.cma and Big_int is in nums.cma, so the filenames are not discernible from the module name or description.

Is there an easy way to look up the correct file for a module?

like image 746
Matthew Piziak Avatar asked Mar 15 '12 03:03

Matthew Piziak


2 Answers

Typically, given an Ocaml library .cma you can get the modules it defines using objinfo (a.k.a. ocamlobjinfo notably on Debian, Ubuntu, …). So, given library paths (/usr/lib/ocaml etc.) and time to spend, it should be possible to construct a mapping between modules and Ocaml library.

like image 118
Po' Lazarus Avatar answered Sep 21 '22 02:09

Po' Lazarus


First, you don't really want to know which cma to load, rather you want to know which package to load via ocamlfind. Next thing to notice is that ocaml compilers need to perform the same thing to compile the project - i.e. by the name of the module referenced in source code find the compiled interface for that module. So let's emulate that behaviour. Compilers get the include paths from command-line, but we have to search all possible include paths. So here we go :

for i in $(ocamlfind list | cut -d ' ' -f 1) ; do
  if [ -r $(ocamlfind query $i)/XXX.cmi ] ; then
    echo $i; break;
  fi ;
done

or

ocamlfind printconf path | xargs -n1 -I/ find / -name XXX.cmi

NB the mapping from module name to filename is not unique - e.g. SomeModule can be represented either by someModule.cmi or SomeModule.cmi (less common).

like image 24
ygrek Avatar answered Sep 23 '22 02:09

ygrek