caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Antoine Delignat-Lavaud <antoine.delignat-lavaud@dptinfo.ens-cachan.fr>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Why does value restriction not apply to the empty list ?
Date: Sun, 11 Jan 2009 17:31:54 +0100	[thread overview]
Message-ID: <496A1EFA.8020501@inria.fr> (raw)
In-Reply-To: <496887BE.8030804@dptinfo.ens-cachan.fr>

Antoine Delignat-Lavaud wrote:

> I chose to solve the problem of polymorphic references by adding value
> restriction* to my inferer, using ocaml to check my results.
> Not knowing whether the empty list should be considered a value or an
> expression, I copied Ocaml's behavior and made it a value.

Yes, the empty list is a value, like all other constants.

> As a result, my inferer gave the following expression the integer type :
> let el = [] in if hd el then 1 else hd el ;;
> which is the expected result since el has polymorphic type 'a list
> but does not look right because it is used as both a bool list and an
> int list.

It is perfectly right.  The empty list can of course be used both as a
bool list and an int list; that's exactly what parametric polymorphism
is all about.

Richard Jones wrote:

> But the same if statement within a function definition causes an error:
>
> # let f el =            
>   if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;;
>                                                           ^^^^^^^^^^
> This expression has type int but is here used with type string

This is Hindley-Milner polymorphism at work: only "let"-bound
variables can have polymorphic types, while function parameters are
monomorphic.

- Xavier Leroy


      parent reply	other threads:[~2009-01-11 16:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-10 11:34 Antoine Delignat-Lavaud
2009-01-10 12:59 ` [Caml-list] " Richard Jones
2009-01-10 13:10   ` Arnaud Spiwack
     [not found] ` <527cf6bc0901100556n40b54b0amff84a7707aacb0ae@mail.gmail.com>
2009-01-10 17:48   ` Antoine Delignat-Lavaud
2009-01-11 16:31 ` Xavier Leroy [this message]

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=496A1EFA.8020501@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=antoine.delignat-lavaud@dptinfo.ens-cachan.fr \
    --cc=caml-list@yquem.inria.fr \
    /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).