caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: yminsky@cs.cornell.edu, yminsky@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] When is a function polymorphic?
Date: Thu, 31 Mar 2005 09:37:24 +0900 (JST)	[thread overview]
Message-ID: <20050331.093724.70528102.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <891bd33905033014311636570a@mail.gmail.com>

From: Yaron Minsky <yminsky@gmail.com>

> I don't understand the following results.  It seems like these two
> examples should have the same type.  In this example, there isn't much
> of a difference between the two cases, but there are cases where this
> idiom is quite convenient.  Any idea how to salvage it?
> 
> # function Some x -> Some () | None as x -> x;;
> - : 'a option -> unit option = <fun>
> # function Some x -> Some () | x -> x;;
> - : unit option -> unit option = <fun>

The typechecker does something special about "as x" patterns.
Namely, rather than unifying the type of x with the type of the whole
input, it types the aliased pattern twice, and only unifies type
parameters which appear in the pattern. So this means that
  # function Some _ as x -> x | None -> Some () ;;
  - : unit option -> unit option = <fun>
since the type parameter appears in the argument to Some, while
  # function Some x -> Some () | None as x -> x;;
  - : 'a option -> unit option = <fun>
since it doesn't appear in None.
This clever typing only happens with "<pat> as <var>" patterns, so
your second example gets the standard result (it is assumed that x
could be anything, including Some, as the typing does not use the
result of the exhaustiveness analysis.)

Jacques Garrigue


  reply	other threads:[~2005-03-31  0:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-30 22:31 Yaron Minsky
2005-03-31  0:37 ` Jacques Garrigue [this message]
2005-03-31  0:51   ` [Caml-list] " Yaron Minsky
2005-03-31  1:15     ` Eric Cooper
2005-03-31  1:26     ` Martin Jambon
2005-03-31  2:42     ` Jacques Garrigue
2005-03-31  4:04       ` Yaron Minsky
2005-03-31  8:32         ` Jacques Garrigue
2005-03-31 12:04           ` Yaron Minsky
2005-03-31 12:48             ` Fermin Reig
2005-03-31 12:16           ` Jon Harrop
2005-04-01 16:26           ` Luc Maranget

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=20050331.093724.70528102.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=yminsky@cs.cornell.edu \
    --cc=yminsky@gmail.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).