caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yaron Minsky <yminsky@janestreet.com>
To: ocaml-core@googlegroups.com, caml-list@inria.fr
Subject: [Caml-list] short-types patch
Date: Sat, 27 Oct 2012 09:51:54 -0400	[thread overview]
Message-ID: <CACLX4jTR=bR8R-ZKMqPLkeCm6fuBpT6vLzH2BO4xBtAAYo85ZQ@mail.gmail.com> (raw)

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

                 reply	other threads:[~2012-10-27 13:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACLX4jTR=bR8R-ZKMqPLkeCm6fuBpT6vLzH2BO4xBtAAYo85ZQ@mail.gmail.com' \
    --to=yminsky@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=ocaml-core@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).