caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] short-types patch
@ 2012-10-27 13:51 Yaron Minsky
  0 siblings, 0 replies; only message in thread
From: Yaron Minsky @ 2012-10-27 13:51 UTC (permalink / raw)
  To: ocaml-core, caml-list

If you use Core or Async, you may have been frustrated by the way that
OCaml reports types, both in the toplevel and, more importantly, in
error messages.  Here's a mildly contrived example.

    let f l x =
      List.Assoc.add l (Int.of_string x) (String.to_list x)
      |! List.Assoc.map ~f:(fun x ->
           List.filter_map x ~f:(fun c ->
             if Char.is_alpha c then Some (String.of_char c)
             else None))

If you run this through the toplevel, you'll get the following
monstrous type.

val f :
  (Core.Std.Int.t, Core.Std.Char.t Core.Std.List.t) Core.Std.List.Assoc.t ->
  Core.Std.String.t ->
  (Core.Std.Int.t, Core.Std.String.t Core.Std.List.t) Core.Std.List.Assoc.t =
  <fun>
As you can see, the inferred types are a little ugly.

Happily, Jacques Garrigue wrote a patch to improve this, which you can
now try out in OPAM by typing:

    opam switch 4.00.1+short-types

Now, if you try the same thing, you get something far easier to read:

val f :
  (int, char list) List.Assoc.t -> string -> (int, string list) List.Assoc.t =
  <fun>

The basic heuristic is that OCaml looks at all of the different names
for a given type that it learns about during the inference process,
and among those, it displays the one with the fewest number of dots,
and among those with the minimal number of dots, it picks the most
recent definition.

Please try it out, and report any problems you run into!

y

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2012-10-27 13:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-27 13:51 [Caml-list] short-types patch Yaron Minsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).