How to import OCaml libraries

How to import 3rd party libraries (e.g. not standard libraries) in OCaml?

An answer by Gabriel Scherer on how to import Batteries is just great and answers this question with much information. Although it is for Batteries, the method is general.

The OCaml compiler (or toplevel, etc.) will find with no additional information only the libraries that are distributed along with the compiler (Array, List, etc.). For any other OCaml libraries that you install, you need to give additional information to the compiler as to where this library lies in your filesystem, so that it can find the corresponding modules (compilation units).

If Batteries is installed in your system in /path/foo, you can invoke the toplevel with “ocaml -I /path/foo” to add this path to the “include directories” where OCaml looks for compilation units. You probably don’t know where Batteries is installed, and it is fine because there is an utility for that: ocamlfind. “ocamlfind” is a small and very useful tool (developed by Gerd Stolpmann) that keeps information about where packages are and which libraries to load for each package, and can be used (ocamlfind ocamlc …) to pass the right options to the compiler to use any OCaml package you have installed.

In the toplevel, using ocamlfind requires a bit of in-toplevel scripting, encapsulated in a “topfind” file installed (by ocamlfind) at a place the toplevel knows by default. So the usual workflow to use Batteries (or really any package) from the toplevel is to first load “topfind”, and then use the #require directive (an ocamlfind-specific directive added by topfind) to load the desired package:

#use "topfind";;
#require "batteries";;

from here on, you should be good: using BatList directly will work.

What rixed is suggesting is to put a small bit of magic in your .ocamlinit (a file the toplevel inspects at startup) to automate the (#use “topfind”;;) bit. That’s very convenient in the long run, once you understand what’s happening.

— http://tech.groups.yahoo.com/group/ocaml_beginners/message/13913

Similar Posts

  • How to split a string by string in Bash?

    How to split a string by string in Bash? For example, “a string separated by space” => [“a”, “string”, “separated”, “by”, “space”] and “a,string,separated,by,comma” => [“a”, “string”, “separated”, “by”, “comma”] You can convert a string to an array using the grammar like inarr=(${a}) If the delimiter is not space and delimiter is a single character…

  • How to use encfs in Android?

    Is encfs available in an Android phone? You may try Encdroid a piece of free software released under the GNU General Public License. It is an Android application. It can access EncFS volumes on cloud storage or internal/USB storage devices. Google Play Store Link: https://play.google.com/store/apps/details?id=org.mrpdaemon.android.encdroid&hl=en Source code: https://github.com/mrpdaemon/encdroid Read more: encfs on CentOS 6 can’t…

  • Latex is stuck with a strange problem, see more information for details.

    $ make pdflatex main.tex This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=pdflatex) restricted write18 enabled. entering extended mode (./main.tex LaTeX2e <2016/02/01> Babel <3.9q> and hyphenation patterns for 81 language(s) loaded. (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) (./usenix.sty (/usr/share/texlive/texmf-dist/tex/latex/psnfss/mathptmx.sty)) (/usr/share/texlive/texmf-dist/tex/latex/graphics/epsfig.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/graphicx.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty) (/usr/share/texlive/texmf-dist/tex/latex/graphics/graphics.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/trig.sty) (/usr/share/texlive/texmf-dist/tex/latex/latexconfig/graphics.cfg) (/usr/share/texlive/texmf-dist/tex/latex/pdftex-def/pdftex.def (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/infwarerr.sty) (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))))) (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)…

  • How to change strings in MySQL tables

    How to change strings in MySQL tables? e.g. I want to change domain.com to www.domain.com. Use the REPLACE functions of MySQL: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace One example is like this: UPDATE table SET field = REPLACE(field, ‘domain.com’, ‘www.domain.com’) WHERE field LIKE ‘%domain.com%’ The WHERE clause is not needed but can make execution faster. Read more: How to find…

Leave a Reply

Your email address will not be published. Required fields are marked *