3

I'm putting together an intro OCaml project for a CS class and part of it involves implementing list operations. I want them to be able to use Pervasives, but not List or any other standard library modules. Is there a way to set up ocamlbuild so it only links against Pervasives in the standard library?

SaxSalute
  • 349
  • 2
  • 8

2 Answers2

2

You can use the -nostdlib option of the compilers but that will hide both Pervasives and List.

What you want is difficult to achieve since both compilation units are part of the same library archive namely stdlib.cma.

You could maybe try to compile your own copy of Pervasives and use the above flag.

Daniel Bünzli
  • 5,119
  • 20
  • 21
  • Good answer. @SaxSalute: make sure you put pervasives.cm[oi] in the working directory, but don't put the .ml. You really don't want your students looking at that code. – PatJ Feb 13 '17 at 12:48
2

I see two opportunities: either remove module directly from the OCaml standard library or hide them by overloading with a module with different (possibly empty) signature.

The first variant requires editing OCaml distribution Makefiles. With opam and is not that scary, actually, as you can patch OCaml quite easily and distribute each patched OCaml as a separate compiler. To remove module from the stdlib archive you will need to edit stdlib/Makefile.shared, stdlib/StdlibModules, and stdlib.mllib. After you've removed the unnecessary modules, you can do:

./configure
make world.opt
make install

Another option is to (ab)use the -open command line argument of ocamlc. When this option is specified with a name of a module, this module will be automatically opened in the compiled program. For example, you can write your own overlay over a standard library, that has the following interface (minimal.mli):

module List = sig end (* or whatever you want to expose *)

and then you can compile either with ocamlc -open minimal ..., or, with ocamlbuild: ocamlbuild -cflags -open,minimal ... (you can also use _tags file to pass the open flag, or write an ocamlbuild plugin).

ivg
  • 34,431
  • 2
  • 35
  • 63
  • I think that for our purposes, hiding the List module with a blank module signature is a perfect solution! I don't know why I didn't think of that. Thanks so much! – SaxSalute Feb 13 '17 at 16:31