7
votes

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?

5

5 Answers

6
votes

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.

4
votes

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).

2
votes

String (the usual functions on the string datatype) is not in str.cma, it's Str (functions for manipulation regular expressions) that is.

There is a similar rationale for the nums.cma name: it mostly encapsulates the module Num which is a layer on top of different "big numbers" libraries (Nat, Big_int, Ratio). Note that nowadays you may want to use Zarith instead.

1
votes

I never noticed this problem, but you're right that you can't tell anything from the name of the module. Since I usually have to read the documentation first, I've always just gotten the info from the docs (listed at the beginning of each section for the standard modules).

A suaver solution is to use GODI and its associated findlib mechanism, which looks like it solves this problem and many others. In particular, it scales beyond just the modules that come with the OCaml distribution.

1
votes

As Po' Lazarus suggested, I used ocamlobjinfo to construct a mapping between the .cma files and the modules defined for easy reference.

  • bigarray.cma:
    • BigArray
  • dbm.cma:
    • Dbm
  • dynlink.cma:
    • Dynlinkaux
    • Dynlink
  • graphics.cma:
    • Graphics
    • GraphicsX11
  • nums.cma:
    • Int_misc
    • Nat
    • Big_int
    • Arith_flags
    • Ratio
    • Num
    • Arith_status
  • stdlib.cma
    • Pervasives
    • Array
    • List
    • Char
    • String
    • Sys
    • Hashtbl
    • Sort
    • Marshal
    • Obj
    • Int32
    • Int64
    • Nativeint
    • Lexing
    • Parsing
    • Set
    • Map
    • Stack
    • Queue
    • CamlinternalLazy
    • Lazy
    • Stream
    • Buffer
    • Printf
    • Format
    • Scanf
    • Arg
    • Printexc
    • Gc
    • Digest
    • Random
    • Callback
    • CamlinternalOO
    • Oo
    • CamlinternalMod
    • Genlex
    • Weak
    • Filename
    • Complex
    • ArrayLabels
    • ListLabels
    • StringLabels
    • MoreLabels
    • StdLabels
  • str.cma:
    • Str
  • toplevellib.cma:
    • Misc
    • Tbl
    • Config
    • Clflags
    • Terminfo
    • Ccomp
    • Warnings
    • Consistbl
    • Linenum
    • Location
    • Longident
    • Syntaxerr
    • Parser
    • Lexer
    • Parse
    • Printast
    • Unused_var
    • Ident
    • Path
    • Primitive
    • Types
    • Btype
    • Oprint
    • Subst
    • Predef
    • Datarepr
    • Env
    • Typedtree
    • Ctype
    • Printtyp
    • Includeclass
    • Mtype
    • Includecore
    • Includemod
    • Parmatch
    • Typetexp
    • Stypes
    • Typecore
    • Typedecl
    • Typeclass
    • Typemod
    • Lambda
    • Printlambda
    • Typeopt
    • Switch
    • Matching
    • Translobj
    • Translcore
    • Translclass
    • Translmod
    • Simplif
    • Runtimedef
    • Meta
    • Instruct
    • Bytegen
    • Printinstr
    • Opcodes
    • Emitcode
    • Bytesections
    • Dll
    • Symtable
    • Bytelink
    • Bytelibrarian
    • Bytepackager
    • Pparse
    • Errors
    • Compile
    • Main_args
    • Genprintval
    • Toploop
    • Trace
    • Topdirs
    • Topmain
  • unix.cma:
    • Unix
    • UnixLabels