Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml - how to see module's interface?

Tags:

module

ocaml

Is it possible to see the interface of a loaded module in interactive OCaml? I have (unsuccessfully) tried searching for such a possibility, and online docs/sources are not what I am looking for.

like image 744
mkf Avatar asked Dec 13 '13 22:12

mkf


2 Answers

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.)

like image 132
Jeffrey Scofield Avatar answered Oct 02 '22 12:10

Jeffrey Scofield


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.

like image 29
ghilesZ Avatar answered Oct 02 '22 13:10

ghilesZ