Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference (if any) between Standard ML's module system and OCaml module system?

Tags:

ocaml

sml

ml

My question is if there is any difference between Standard ML's module system and OCaml module system? Has OCaml all the support of functors , ascriptions etc... that SML has?

like image 804
Dragno Avatar asked Mar 23 '13 08:03

Dragno


People also ask

What are OCaml modules?

ocaml modules allow to package together data structures definitions and functions operating on them. This allow to reduce code size and name confusion, as any identifier can appear in different modules, with different meanings, without any interference.

What is ML module?

ml is a user interface to the Modules package. The Modules package provides for the dynamic modification of the user's environment via modulefiles. ml acts as a shortcut command to the module command thus it supports all the command line switches and module sub-commands that are supported by module.

Is OCaml an SML?

OCaml and SML Standard ML has a formal Definition, while OCaml is specified by its lone implementation and informal documentation. Standard ML has a number of compilers, while OCaml has only one.

What is an OCaml signature?

2 Signatures 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.


2 Answers

There are some differences feature-wise, as well as semantically.

Features SML supports but not OCaml:

  • transparent signature ascription
  • module-level let
  • symmetric sharing constraints
  • syntactic sugar for functors over types and values

Features OCaml 4 has but not SML:

  • higher-order functors
  • recursive modules
  • local modules
  • nested signatures
  • modules as first-class values
  • general module sharing (sig with module A = M)
  • module type of

Several SML implementations provide some of these as extensions, however: e.g. higher-order functors (SML/NJ, Moscow ML, Alice ML), local and first-class modules (Moscow ML, Alice ML), module sharing (SML/NJ, Alice ML), nested signatures (Moscow ML, Alice ML), and recursive modules (Moscow ML).

Semantics-wise, the biggest difference is in the treatment of type equivalence, especially with respect to functors:

  • In SML, functors are generative, which means that applying the same functor twice to the same argument always yields fresh types.

  • In OCaml, functors are applicative, which means that applying the same functor twice to the exact same argument (plus additional syntactic restrictions) reproduces equivalent types. This semantics is more flexible, but can also break abstraction (see e.g. the examples we give in this paper, Section 8).

    Edit: OCaml 4 added the ability to optionally make functors generative.

  • OCaml has a purely syntactic notion of signatures, which means that certain type equivalences cannot be expressed by the type system, and are silently dropped.

    Edit: Consider this example:

     module F (X : sig type t end) = struct type u = X.t -> unit type v = X.t end module M = F (struct type t = int end : sig type t end) 

    The type of M is simply sig type u type v end and has thus lost any information about the relation between its types u and v, because that cannot generally be expressed in the surface syntax.

Another notable difference is that OCaml's module type system is undecidable (i.e, type checking may not terminate), due to its permission of abstract signatures, which SML does not allow.

like image 62
Andreas Rossberg Avatar answered Nov 11 '22 02:11

Andreas Rossberg


As for semantics, a much better and elaborate answer is given by Andreas Rossberg above. However, concerning syntax this site might be what you are looking for.

like image 38
chris Avatar answered Nov 11 '22 02:11

chris