From: "Johannes Kanig" <johannes.kanig@lri.fr>
To: caml-list@inria.fr
Subject: polymorphic variants and promotion
Date: Tue, 19 Feb 2008 11:23:15 +0100 [thread overview]
Message-ID: <926565e50802190223r456d0ec2we4b8a4f51b134f42@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1547 bytes --]
Hi,
I ran into the following problem using polymorphic variants. Suppose I have
2 functions f and g (the function definitions don't make much sense - they
are just there for typing purposes) :
# let f (`A a) (`A i) = `A i ;;
val f : [< `A of 'a ] -> [< `A of 'b ] -> [> `A of 'b ] = <fun>
# let g (`A a) = (`B a) ;;
val g : [< `A of 'a ] -> [> `B of 'a ] = <fun>
For simplicity I assume that the type variables all instantiate to "int".
Now, I want to construct a function h that takes an accumulator a and a list
of `A's (for example [`A 1; `A 2; ...]) that produces either an object of
type `A of int or an object of type `B of int in the following way:
# let h a = function
| [] -> g a
| xs -> List.fold_left f a xs;;
But this doesn't type:
This expression has type [ `A of 'a ] but is here used with type
[> `B of 'a ]
The first variant type does not allow tag(s) `B
I understand that the fold_left fixes the type of f the result of f to [`A
of int] instead of [>`A of int]. The example as above can be easily repaired
by replacing the second branch of the pattern matching by:
let `A _ as x = List.fold_left f a xs in x
which is fair enough (thanks to Romain Bardou for pointing this out).
What I don't understand is why the promotion of [`A of int] to [> `A of int]
is not possible in general:
# `A 1;;
- : [> `A of int ] = `A 1
# (`A 1 : [`A of int]) ;;
- : [ `A of int ] = `A 1
# ((`A 1 : [`A of int]) : [> `A of int]) ;;
- : [ `A of int ] = `A 1
Does anyone have an answer?
--
Johannes Kanig
johannes.kanig@lri.fr
[-- Attachment #2: Type: text/html, Size: 1874 bytes --]
next reply other threads:[~2008-02-19 10:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-19 10:23 Johannes Kanig [this message]
2008-02-19 12:38 ` [Caml-list] " Julien Signoles
2008-02-19 13:37 ` Keiko Nakata
2008-02-19 22:26 ` Christophe Raffalli
2008-02-20 4:50 ` Jacques Garrigue
2008-02-19 12:48 ` Remi Vanicat
2008-02-20 5:01 ` [Caml-list] " Jacques Garrigue
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=926565e50802190223r456d0ec2we4b8a4f51b134f42@mail.gmail.com \
--to=johannes.kanig@lri.fr \
--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