caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: yminsky@cs.cornell.edu
Cc: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>, <caml-list@inria.fr>
Subject: Re: [Caml-list] When is a function polymorphic?
Date: Wed, 30 Mar 2005 17:26:38 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.44.0503301719080.3339-100000@localhost> (raw)
In-Reply-To: <891bd339050330165142478f37@mail.gmail.com>

On Wed, 30 Mar 2005, Yaron Minsky wrote:

> I think I screwed up the original examples a bit.  I think the effect
> I'm looking at doesn't depend on the "as" notation in particular.
> Here's another example that doesn't use "as":
>
> # function Some x -> Some () | None -> None;;
> - : 'a option -> unit option = <fun>
> # function Some x -> Some () | x -> x;;
> - : unit option -> unit option = <fun>
>
> The reason I want this is for the following example.  Consider some
> complicated union type with a single parameter:
>
> type 'a foo = A of 'a | B of int | C of string * string | ... | ZZ of float
>
> I want a function that converts an 'a foo to a unit foo.  I tried to
> write it this way:
>
> function A _ -> A () | x -> x
>
> But this ends up having type: unit foo -> unit foo, which isn't what I
> want at all.  Any idea of how to achieve this cleanly?

With polymorphic variants, you can do something like this:

# type const = [ `B of int
               | `C ];;
  type const = [ `B of int | `C ]

# type 'a poly =
    [ `A of 'a
    | const ];;
    type 'a poly = [ `A of 'a | `B of int | `C ]

# let f : 'a poly -> unit poly= function
    `A _ -> `A ()
  | #const as x -> x;;
    val f : 'a poly -> unit poly = <fun>



Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr



  parent reply	other threads:[~2005-03-31  1:26 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 ` [Caml-list] " Jacques Garrigue
2005-03-31  0:51   ` Yaron Minsky
2005-03-31  1:15     ` Eric Cooper
2005-03-31  1:26     ` Martin Jambon [this message]
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=Pine.LNX.4.44.0503301719080.3339-100000@localhost \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    --cc=yminsky@cs.cornell.edu \
    /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).