Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between include, require and open in OCaml?

Tags:

ocaml

For example,

include: include Ppx_core

open: open Core.Std

require: #require "compiler-libs.common"

and use: #use "topfind"

like image 823
Yixing Liu Avatar asked Mar 06 '17 17:03

Yixing Liu


People also ask

What does :: mean in OCaml?

Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ). It is however worth noting that the symbol can be used in two different ways and it is also interpreted in two different ways by the compiler.

What is a signature in OCaml?

Signatures are interfaces for structures. A signature specifies which components of a structure are accessible from the outside, and with which type. It can be used to hide some components of a structure (e.g. local function definitions) or export some components with a restricted type.

What is Val in OCaml?

Well, val is a keyword in OCaml with several different uses. The cases you mention are both, in essence, that val is used in a module signature to specify values that appear in the module. Values are things like functions and expressions.

How do I load a module in OCaml?

You must compile the module you wish to include first, provide the location of the compiled files to compilation commands of modules depending on it, then provide it in the final compilation command line. The commands: $ cd foo $ ocamlc -c moduleA.ml $ cd .. will produce moduleA.


2 Answers

  • include re-exports the components of the module in the current structure: the module you are in will contain all definitions that are in Ppx_core.
  • open makes the components of the module directly accessible in the typing environment. Instead of typing Core.Std.element you can just type element.
  • #require is a Topfind command that finds a library and loads it, making its modules accessible to you.
  • #use behave as if copying a full file directly into your toplevel.

Note that the #-keywords are not part of the OCaml language but are toplevel commands: they won't work if you try to compile your file.

like image 67
PatJ Avatar answered Oct 13 '22 03:10

PatJ


The include Module.Name statement in the module definition will include all definitions from a module named Module.Name. The definitions will be included roughly as they were copy-pasted. If the include Module.Name occurs inside of the module type definition (aka signature definition), the the Module.Name should be a valid (known to a compiler) module type. It will include the definition of a module type as it is (without including any type sharing constraints).

The open Module.Name statement occurring in both module implementation and module signature, will allow you to refer to definitions (values, types, submodules) of a Module.Name without using a fully qualified named, i.e., using short names without the Module.Name prefix.

The #require statement is not a statement at all and is not a part of OCaml grammar. It is special directive of the OCaml toplevel - the interactive loop. The same as ipython has its own directives. The require directive will load the specified package, and all its dependencies. Moreover, this directive is not a part of a standard OCaml toplevel distribution, but is added by the topfind script that is a part of the ocamlfind toolkit. The #use directive is used to load and evaluate a script. For example #use "topfind" will load and evaluate the topfind script from the OCaml standard library folder. This script will register the require directive. There are also #load and #load_rec directives, that work on a more fine-granular level, rather than packages -- these directives are intendend to load libraries.

like image 21
ivg Avatar answered Oct 13 '22 03:10

ivg