From: Eliot Handelman <eliot@colba.net>
Cc: caml-list@inria.fr
Subject: Polymorphism problem
Date: Mon, 20 Apr 2009 21:44:54 -0400 [thread overview]
Message-ID: <49ED2516.9030103@colba.net> (raw)
In-Reply-To: <200904202215.27735.jon@ffconsultancy.com>
Hi list,
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?
Here's my workaround:
let rec int_getter = function
X x -> x.x_v
| Y y -> (int_getter y.y_x)
let rec getter = function
X x -> x.x_v
| Y y -> y.y_arr.(int_getter y.y_x)
where now the type of getter is 'a kind -> 'a as needed. I have no
choice but to use this at present -- is there a better method?
thanks for wisdom,
-- eliot
next prev parent reply other threads:[~2009-04-21 1:44 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 ` Eliot Handelman [this message]
2009-04-21 8:50 ` [Caml-list] Polymorphism problem Mauricio Fernandez
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=49ED2516.9030103@colba.net \
--to=eliot@colba.net \
--cc=caml-list@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