caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Remi Vanicat <vanicat@debian.org>
To: caml-list@inria.fr
Subject: Re: polymorphic variants and promotion
Date: Tue, 19 Feb 2008 13:48:28 +0100	[thread overview]
Message-ID: <871w796rj7.dlv@maison.homelinux.org> (raw)
In-Reply-To: <926565e50802190223r456d0ec2we4b8a4f51b134f42@mail.gmail.com>

"Johannes Kanig" <johannes.kanig@lri.fr> writes:

> 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

Well, you have to explicitly coerce :

let h a = function                                              
      | [] -> g a                                                 
      | xs -> (List.fold_left f a xs : [ `A of _ ] :> [> `A of _ | `B of _ ]) ;;

What I don't understand is why this :

let h a = function                                              
      | [] -> g a                                                 
      | xs -> (List.fold_left f a xs :> [> `A of _ | `B of _ ]) ;;

do not work ? I understood that we need to explicitly coerce, but the 
[ `A of _ ] is just the information the compiler will find by itself,
why must we give it to it again ?

-- 
Rémi Vanicat


  parent reply	other threads:[~2008-02-19 13:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-19 10:23 Johannes Kanig
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 [this message]
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=871w796rj7.dlv@maison.homelinux.org \
    --to=vanicat@debian.org \
    --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;
as well as URLs for NNTP newsgroup(s).