9

I'm a OCaml newbie working with some pretty complex (at least for me) OCaml code I didn't write. It would help a lot to understand it if I could see the inferred types for some of the values, as I can do with F# and Visual Studio by hovering over any value, e.g.:

F# showing inferred type

(screenshot borrowed from http://theburningmonk.com/2010/01/learning-f-part-1/)

I guess I could break down the code and feed it to the toplevel to get the types, but is there any other, simpler way?

nlucaroni
  • 47,556
  • 6
  • 64
  • 86
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275

4 Answers4

8

First, you must compile your file (foo.ml) with the -annot flag. This generates the annotation file (foo.annot) that contains type information about all identifiers inside. You can include this as part of your makefile or build process.

Then, you need to rely on your IDE to display that information. I do this in Tuareg (the emacs mode) using Ctrl+C, Ctrl+T, which highlights the current expression and displays its type.

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
7

OcaIDE which is an O'Caml plug-in for Eclipse displays the inferred types on the fly while editing your code.

Example:

enter image description here

0xFF
  • 4,140
  • 7
  • 41
  • 58
1

You could enter the code into the ocaml toplevel. Yeah, it's not as pretty, but it'll show the types.

aneccodeal
  • 8,531
  • 7
  • 45
  • 74
  • yes, I know I could do this, but it's pretty complex code, it would be quite time-consuming to break down the code enough to get to the types I want to see. – Mauricio Scheffer May 12 '11 at 17:29
  • Toplevel + rlwrap is quite usable, if only there was a way to [save it](http://stackoverflow.com/questions/3966925/saving-my-running-toplevel-for-later)... – Gaius May 18 '11 at 15:16
1

What works for me when reading OCaml code is to begin with the .mli files to see what the overall structure of the project is. From there, and having an idea of what are the dependencies, you can explore from the toplevel.

Alternatively, you can ocamlc -i the file in question for an overview of all the declared types and values, or use Tuareg mode in Emacs as replied elsewhere, if you are of that persuasion.