caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oleg@okmij.org
To: rossberg@mpi-sws.org
Cc: caml-list@inria.fr
Cc: roberto@dicosmo.org, gabriel.scherer@gmail.com
Subject: Re: [Caml-list] Equality between abstract type definitions
Date: 26 Oct 2013 09:07:11 -0000	[thread overview]
Message-ID: <20131026090711.13453.qmail@www1.g3.pair.com> (raw)
In-Reply-To: <526A7A3F.2090405@mpi-sws.org>


Perhaps a bit of history may help. In the Haskell compiler GHC, until
version 6 as I recall, type variables in annotations have the same
semantics as presently in OCaml. That is, type variables in Haskell
were all `flexible', with OCaml's scoping rules. When GHC started to
play with GADTs (ca 2006), the semantics of type variables has changed
-- to what Andreas is proposing. That is, type variables became rigid,
like they are in SML. The scoping rules are cleaned up (although
whether the rules became simpler is the subject of a debate -- there are
lots of corner cases to trip the unwary). The changeover from flexible
to rigid did break all code that used type variables in annotations --
including mine. Well, one learns to live with it. It wasn't the first
or the last time when a new version of GHC breaks code. Almost always the
change is for the better. It seems the community prefers acute but
short pain to the dull and prolonged.

> succ :: a -> a
> succ n = n+1
>
> here is the system's answer
>
>     No instance for (Num a)
>       arising from a use of `+'
>     In the expression: n + 1
>     In an equation for `succ': succ n = n + 1
>
> Is this really more new-user friendly?

Perhaps this is a too advanced example for new users. Polymorphic
numerals of Haskell per se are not new-user friendly. One is welcome
to try to explain to new users the following error message:

> *Prelude> 1 + "a"
>
> <interactive>:1163:3:
>     No instance for (Num [Char])
>       arising from a use of `+'
>     Possible fix: add an instance declaration for (Num [Char])
>     In the expression: 1 + "a"
>     In an equation for `it': it = 1 + "a"

And there are people who may be tempted to follow the proposed fix.

In all fairness, although the error message is greatly off-putting to
the new users, the surprise does not last long and people quickly get
past it. It means the education does work. The same education quickly
tells the users the problem with the original succ example: the
type inferred typed turns out not as general as the type specified
in the annotation. The type variable cannot be quantified without
constraints: the Num a constraint should be added. A better example

    foo :: a -> a
    foo = not

        Couldn't match type `a' with `Bool'
          `a' is a rigid type variable bound by
              the type signature for foo :: a -> a at <interactive>:1165:20
        Expected type: a -> a
          Actual type: Bool -> Bool
        In the expression: not
        In an equation for `foo': foo = not

gives a much more understandable error message, even to new users. GHC
error messages are generally very good: note how the error message
tells where the type variable is bound.

Since all type variables in Haskell are rigid, there is no longer any
convenient way to tell GHC that I think two sub-expression in a large
definition should have the same type. That's a pity. OCaml's type
variables are great for that. OCaml type variables can be understood
purely declaratively, as an assertion from the programmer that all
sub-expressions marked by the same type variables must have the same
type, whatever it turns out to be. I often feel the need for such
annotations in Haskell. There are workarounds, but they are ugly.

My own suggestion is to make clear that type variables in the 'val'
declarations are universally quantified. That is, the inferred type
of 
        let foo = fun x -> x
should be printed as
        val foo : 'a. 'a -> 'a
Likewise, such syntax should be encouraged for val declarations in
modules (omitting the quantifier "'a ." should also be acceptable for
val declarations, perhaps prompting a warning).




  reply	other threads:[~2013-10-26  9:07 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-24 22:57 Peter Frey
2013-10-24 23:23 ` Jacques Garrigue
2013-10-25  6:44   ` Andreas Rossberg
2013-10-25  8:29     ` Roberto Di Cosmo
2013-10-25  9:59       ` Ivan Gotovchits
2013-10-25 11:09         ` Gabriel Scherer
2013-10-25 14:24           ` Andreas Rossberg
2013-10-25 20:32             ` Yaron Minsky
2013-10-25 20:44               ` Jacques Le Normand
2013-10-26  1:08                 ` Norman Hardy
2013-10-26  5:28                   ` Jacques Garrigue
2013-10-27 12:16               ` Andreas Rossberg
2013-10-27 12:56                 ` Yaron Minsky
2013-10-27 14:28                   ` Gabriel Scherer
2013-10-27 14:43                     ` Yaron Minsky
2013-10-27 15:25                       ` Gabriel Scherer
2013-10-27 15:41                         ` Yaron Minsky
2013-10-25 12:35         ` Roberto Di Cosmo
2013-10-25 12:45           ` Jonathan Protzenko
2013-10-25 13:20             ` Roberto Di Cosmo
2013-10-25 14:03       ` Andreas Rossberg
2013-10-26  9:07         ` oleg [this message]
2013-10-26 14:11           ` Didier Remy
2013-10-26 17:32         ` Didier Remy
2013-10-27 12:07           ` Andreas Rossberg
2013-10-27 14:10             ` Roberto Di Cosmo
2013-10-28  3:30     ` Jacques Garrigue

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=20131026090711.13453.qmail@www1.g3.pair.com \
    --to=oleg@okmij.org \
    --cc=caml-list@inria.fr \
    --cc=rossberg@mpi-sws.org \
    /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).