caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Improved type error messages for Ocaml
@ 2017-01-10 10:45 Arthur Charguéraud
  2017-01-12 21:37 ` SP
  0 siblings, 1 reply; 4+ messages in thread
From: Arthur Charguéraud @ 2017-01-10 10:45 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 3148 bytes --]

Dear OCaml users,


It is my pleasure to announce the beta release of my patch for improved 
type error messages.

|    opam switch 4.02.2+improved-errors|


Quick demo:

*** With "ocamlc" ***


let _ = List.map (fun x -> x + 1) [2.0; 3.0]
                                    ^^^
Error: This expression has type float but an expression was expected of 
type int.

This message is very confusing to the user who intented to write: (fun x 
-> x +. 1.)


*** With "ocamlc -easy-type-errors" ***


let _ = List.map (fun x -> x + 1) [2.0; 3.0]
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error: The function `List.map' cannot be applied to the arguments provided.

    | Types of the expected arguments:    | Types of the provided arguments:
---|-------------------------------------|------------------------------------
  1 | 'a -> 'b                            | int -> int
  2 | 'a list                             | float list


This new message show all the types involved, and does not attempt to 
guess whether the user intended to write [2; 3]  or intended to write 
(fun x -> x +. 1.); Trying to guess is a bad idea, because roughly half 
of the time the guess is wrong, and it just adds more confusion.



Many more (cool) demos are available from the following sources:

  * paper:
    http://www.chargueraud.org/research/2015/ocaml_errors/ocaml_errors.pdf
  * slides:
    http://www.chargueraud.org/talks/2014_09_05_talk_ocaml_errors.pdf
  * video: https://www.youtube.com/watch?v=V_ipQZeBueg



How it works (short version):

When the flag "-easy-type-errors" is activated, the compiler behaves 
exactly as usual, except when a top level definition fails to 
type-check. At such point, the definition is type-checked again, using a 
slightly modified unification algorithm, able to produce messages that 
are (I argue) more informative for locating the error. The patched 
compiler is thus able to provide alternative error messages with zero 
performance overhead on successful compilations. For ill-typed programs, 
the new typing algorithm may be slightly slower than the original one 
(as it performs a larger number of generalizations), but it should never 
be orders of magnitude slower.

Pretty much all of OCaml is supported, with the notable exceptions of 
GADTs (by lack of time of expertise), and record field name overloading 
(due to the fact that it depends on the order in which unifications are 
performed). Note that top-level definitions involving such features will 
still compile properly, as long as it they do not contain any type 
error; if they do, then the error message will likely be uninformative, 
in which case the flag "-easy-type-errors" needs to be turned off.


If you like this patch and would like to see it one day integrated in 
the main distribution, please post feedback on the mailing list, to 
convince the developers and myself that it is worth further investment. 
I am especially interested in feedback on the use of the new error 
messages in the context of teaching OCaml.


Enjoy!

+
Arthur

[Many thanks to Armaël Guéneau and Gabriel Scherer for their help with 
the opam packaging.]



[-- Attachment #2: Type: text/html, Size: 5319 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Improved type error messages for Ocaml
  2017-01-10 10:45 [Caml-list] Improved type error messages for Ocaml Arthur Charguéraud
@ 2017-01-12 21:37 ` SP
  2017-01-12 22:10   ` Ivan Gotovchits
  0 siblings, 1 reply; 4+ messages in thread
From: SP @ 2017-01-12 21:37 UTC (permalink / raw)
  To: caml-list

Nice!

-- 
    SP

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Improved type error messages for Ocaml
  2017-01-12 21:37 ` SP
@ 2017-01-12 22:10   ` Ivan Gotovchits
  2017-01-13 15:43     ` Arthur Charguéraud
  0 siblings, 1 reply; 4+ messages in thread
From: Ivan Gotovchits @ 2017-01-12 22:10 UTC (permalink / raw)
  To: SP; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1412 bytes --]

Impressive!

By the way, one of the most common mistakes is to forget that a function
application binds tighter than
infix operators, e.g.,


       # print_int 2+2;;

Although your branch already provides a nice error message:

Error: The function `+' cannot be applied to the arguments provided.

   | Types of the expected arguments:    | Types of the provided arguments:
---|-------------------------------------|------------------------------------
 1 | int                                 | unit
 2 | int                                 | int


that is probably better than the default:

Error: This expression has type unit but an expression was expected of type
         int

it is still probably a good idea, to provide an ad-hoc error message here
(as you did for missing `()`, `!` and `rec`).

In this case, if an offending expression contains a binary operator which
has an application to the left and some simpl_expr
on the right, and if we can fix it by parenthesizing the expression, then
we can suggest adding parentheses around the expression.

Regards,
Ivan Gotovchits

On Thu, Jan 12, 2017 at 4:37 PM, SP <sp@orbitalfox.com> wrote:

> Nice!
>
> --
>     SP
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>

[-- Attachment #2: Type: text/html, Size: 3078 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Improved type error messages for Ocaml
  2017-01-12 22:10   ` Ivan Gotovchits
@ 2017-01-13 15:43     ` Arthur Charguéraud
  0 siblings, 0 replies; 4+ messages in thread
From: Arthur Charguéraud @ 2017-01-13 15:43 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2285 bytes --]


Hi Ivan,

Thanks for your feedback.

# print_int 2+2;;

I don't like the idea of testing whether the term would type-check with 
a different parenthesization. First of all, it might be expensive or 
complex to test. Second, it may lead to suggestions that are even more 
confusing.

One proposal that I would like better to handle your scenario is the 
following: if an infix binary operator (other than ';') has a first 
argument of type unit, then suggest that parentheses might be missing.

I can try to implement that, unless someone argues against this proposal.

Thanks,
+
Arthur

> Impressive!
>
> By the way, one of the most common mistakes is to forget that a 
> function application binds tighter than
> infix operators, e.g.,
>
>
>  # print_int 2+2;;
>
> Although your branch already provides a nice error message:
>
>     Error: The function `+' cannot be applied to the arguments provided.
>
>        | Types of the expected arguments:    | Types of the provided
>     arguments:
>     ---|-------------------------------------|------------------------------------
>      1 | int                     | unit
>      2 | int                     | int
>
>
> that is probably better than the default:
>
>     Error: This expression has type unit but an expression was
>     expected of type
>              int
>
> it is still probably a good idea, to provide an ad-hoc error message 
> here (as you did for missing `()`, `!` and `rec`).
>
> In this case, if an offending expression contains a binary operator 
> which has an application to the left and some simpl_expr
> on the right, and if we can fix it by parenthesizing the expression, 
> then we can suggest adding parentheses around the expression.
>
> Regards,
> Ivan Gotovchits
>
> On Thu, Jan 12, 2017 at 4:37 PM, SP <sp@orbitalfox.com 
> <mailto:sp@orbitalfox.com>> wrote:
>
>     Nice!
>
>     --
>         SP
>
>     --
>     Caml-list mailing list.  Subscription management and archives:
>     https://sympa.inria.fr/sympa/arc/caml-list
>     <https://sympa.inria.fr/sympa/arc/caml-list>
>     Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>     <http://groups.yahoo.com/group/ocaml_beginners>
>     Bug reports: http://caml.inria.fr/bin/caml-bugs
>     <http://caml.inria.fr/bin/caml-bugs>
>
>


[-- Attachment #2: Type: text/html, Size: 5562 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-01-13 15:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-10 10:45 [Caml-list] Improved type error messages for Ocaml Arthur Charguéraud
2017-01-12 21:37 ` SP
2017-01-12 22:10   ` Ivan Gotovchits
2017-01-13 15:43     ` Arthur Charguéraud

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).