Type Debugger for OCaml

September 24, 2016


What is the Type Debugger for OCaml?

Have you ever been confused by the type error messages produced by OCaml? If you have, you might have wondered why the OCaml compiler does not produce better error messages. The reason is: it can't. During type inference, the OCaml compiler finds a type conflict between two subexpressions: one requires one type and the other provides a different type. However, there is no way for the OCaml compiler to know which of the two types is the right type for the programmer. Thus, the OCaml compiler arbitrarily chooses one of them and reports it as a type error.

The Type Debugger in this page improves on this situation by asking questions to the programmer. From the answers to the questions, it understands the programmer's intention and leads the programmer to the real source of the type error. This idea of asking questions to obtain programmer's intention is due to Shapiro's algorithmic program debugging and is used by Chitil to design a type debugger for a small subset of Haskell. We extend this approach to support full OCaml by reusing the type inferencer in the OCaml compiler. The Type Debugger has been used successfully in the functional programming course in Ochanomizu University.


We are developing the Type Debugger using the following environments:

Installation via OPAM


The patch is placed under download directory. To compile, see 4.02.1+type-debugger.comp.

Emacs interface

The tuareg-ochadai.el file supports:
Highlight Error
Highlight the last error (or warning) message in the *caml-toplevel* buffer.
Evaluate Buffer
Similar to tuareg-evaluate-buffer, but it first saves the current buffer, kill the OCaml toplevel, and load the file freshly.
Compile Program
Execute make in the current directory.
Answer Yes/No/Quit
Send the answer to the Type Debugger.
See the new tuareg menu for key combinations.

Vim interface

To start the type debugger: To debug:
To answer Yes
type (backslash followed by y) or :TypeDebuggerSendYes.
To answer No
type (backslash followed by n) or :TypeDebuggerSendNo.
To answer Quit
type (backslash followed by q) or :TypeDebuggerSendQuit.
To answer Exit
type (backslash followed by e) or :TypeDebuggerExit.

Papers and talks


This document was translated from LATEX by HEVEA.