OCaml - how to see module's interface?

The standard trick for this is to define a synonym for the module, which induces the toplevel to list the interface.

$ ocaml
        OCaml version 4.00.1

# #load "str.cma";; 
# module S = Str;;
module S :
  sig
    type regexp = Str.regexp
    val regexp : string -> regexp
    val regexp_case_fold : string -> regexp
    val quote : string -> string
    val regexp_string : string -> regexp
    val regexp_string_case_fold : string -> regexp
    val string_match : regexp -> string -> int -> bool
    . . .
    val first_chars : string -> int -> string
    val last_chars : string -> int -> string
  end

Update

(Note that this answer is from 2013. Recent revisions of OCaml provide a toplevel directive to show a module interface:

# #show_module Str;;
module Str :
  sig
    type regexp
    val regexp : string -> regexp
    . . .
    val first_chars : string -> int -> string
    val last_chars : string -> int -> string
  end

So the semi-clever workaround is no longer required.

(There are many new directives. Type #help;; at toplevel to get a list.)


Both utop and ocaml interpreters added the #show directive since a moment. It does exactly what you want, as in the following example :

    │ Welcome to utop version 1.19.3 (using OCaml version 4.04.0)  │        
    └──────────────────────────────────────────────────────────────┘        

Type #utop_help for help about using utop. 

─( 15:12:33 )─< command 0 >──────────────────────────────────────{ counter: 0 }─
utop # #show List;;
module List :                                                                     
sig                                                                               
  val length : 'a list -> int                                                 
  val cons : 'a -> 'a list -> 'a list
  val hd : 'a list -> 'a
  ...
  val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
  val sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a list
  val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
end

PS:i'm using 4.04 version but i know that it also works for 4.03.> and maybe before that too.


In OCaml toplevel version 4.04.0, the trick of defining a module's synonym works no more:

# module L = List;;
module L = List
#

but you can use the include directive:

# module L = struct include List end;;
module L :
  sig
    val length : 'a list -> int
    val cons : 'a -> 'a list -> 'a list
    val hd : 'a list -> 'a
    val tl : 'a list -> 'a list
    val nth : 'a list -> int -> 'a
    val rev : 'a list -> 'a list
...
    val sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a list
    val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
  end
#

Tags:

Module

Ocaml