Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to examine a module/signature at ocaml top level?

Tags:

ocaml

Please: I would like to examine a module's signature; is there a function to do this? Just typing the name of the module doesn't work:

# List ;;
Error: Unbound constructor List

In fact I want to do this for modules I define on the interactive top level.

Sorry if the answer is obvious - hard to search for this! Thanks.

like image 241
user3375878 Avatar asked Dec 05 '22 07:12

user3375878


2 Answers

The standard trick for this is to make a new module synonym:

# module Mylist = List;;
module Mylist :
  sig
    val length : 'a list -> int
    val hd : 'a list -> 'a
    val tl : 'a list -> 'a list
    . . .
    val sort : ('a -> 'a -> int) -> 'a list -> 'a list
    val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
    val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
    val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
  end
# 

Update

Since OCaml 4.02 this trick no longer works. Instead there is a toplevel directive for the purpose:

# #show_module List;;
module List :
  sig
    val length : 'a list -> int
    val hd : 'a list -> 'a
    val tl : 'a list -> 'a list
    . . .
    val sort : ('a -> 'a -> int) -> 'a list -> 'a list
    val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
    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

like image 175
Jeffrey Scofield Avatar answered Dec 06 '22 21:12

Jeffrey Scofield


In OCaml versions >= 4.02, you can now also do this in the interactive interpreter:

# #show_module List;;
module List :
  sig
    val length : 'a list -> int
    ...
  end

Or just use #show List;;

like image 40
dubiousjim Avatar answered Dec 06 '22 20:12

dubiousjim