Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Mauricio Fernandez <mfp@acm.org>
To: eliot@colba.net, caml-list@inria.fr
Subject: Re: [Caml-list] Polymorphism problem
Date: Tue, 21 Apr 2009 10:50:26 +0200	[thread overview]
Message-ID: <20090421085026.GH8986@NANA.localdomain> (raw)
In-Reply-To: <49ED2516.9030103@colba.net>

On Mon, Apr 20, 2009 at 09:44:54PM -0400, Eliot Handelman wrote:
> Consider this:
> 
> type 'a x = { x_v : 'a }
> 
> and 'a y = { y_x : int kind;
>          y_arr : 'a array
>         }
> and 'a kind =
>     X of 'a x
>   | Y of 'a y
>     
> 
> I'd like to write a function _getter_ that's polymorphic over kind. This
> doesn't work, getting int kind -> int:
> 
> let rec getter = function
>     X x -> x.x_v
>   | Y y -> y.y_arr.(getter y.y_x)
>
> which seems surprising to me since getter y.y_x is an intermediate
> value that's never returned. Is this just a limitation of the type system or
> does this result make sense?

The above function requires polymorphic recursion, which OCaml doesn't support
directly. There are several ways to encode it, though, one involving recursive
modules and another rank-2 polymorphism:

# module rec M : sig val getter : 'a kind -> 'a end = struct
  let getter = function
    X x -> x.x_v
  | Y y -> y.y_arr.(M.getter y.y_x)
  end;;
module rec M : sig val getter : 'a kind -> 'a end
# M.getter;;
- : 'a kind -> 'a = <fun>

# type get = { get : 'a. 'a kind -> 'a };;
type get = { get : 'a. 'a kind -> 'a; }
# let rec get = { get = function X x -> x.x_v | Y y -> y.y_arr.(get.get y.y_x) };;
val get : get = {get = <fun>}
# let getter = get.get;;
val getter : 'a kind -> 'a = <fun>

-- 
Mauricio Fernandez  -   http://eigenclass.org


  reply	other threads:[~2009-04-21  8:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-20 21:15 Parallelized parsing Jon Harrop
2009-04-20 21:35 ` [Caml-list] " Mike Lin
2009-04-21  0:52   ` Yitzhak Mandelbaum
2009-04-21 15:55     ` Jon Harrop
2009-04-21  1:44 ` Polymorphism problem Eliot Handelman
2009-04-21  8:50   ` Mauricio Fernandez [this message]
2009-04-21  7:19 ` [Caml-list] Parallelized parsing David MENTRE
2009-04-21 16:04   ` Jon Harrop

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=20090421085026.GH8986@NANA.localdomain \
    --to=mfp@acm.org \
    --cc=caml-list@inria.fr \
    --cc=eliot@colba.net \
    /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