Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] polymorphic variant question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: nadji@n...
Subject: Re: [Caml-list] polymorphic variant question
Thanks for all the answers but I search a more systematic one.
In fact this was a (too) stripped version of a problem that
is occuring quite frequently since I use a lot of pm.
Imagine I have a type :
type t = [`a|`b|`c]
And I have a function f whose *infered* type is [`a|`b] -> unit
I would like the compiler to know that (a semantically equivalent code as)
function `c -> () | x -> f x
is correct, without having to precise all the variant that x could be.
(that is, without saying :
function `c -> () | `a|`b as x -> f x
nor
type ab = [`a|`b]
type t = [ab | `c]
function `c -> () | #ab as x -> f x
)
I don't want to write type annotations all around because
- my pm are quite large
- a lot of functions like f are not published in my interface
- I think OCaml should be smart enough to do this

TIA,
Nadji




Remi VANICAT wrote:

> nadji@noos.fr writes:
>
> > Hi all,
> >
> > I am puzzled by the code below which doesn't work.
> >
> > # let f = function `B -> ();;
> > val f : [ `B] -> unit = <fun>
> > # let g x = match (x:[`A|`B]) with `A -> () | y -> f y;;
> > Characters 51-52:
> >   let g x = match (x:[`A|`B]) with `A -> () | y -> f y;;
> >                                                      ^
> > This expression has type [ `A | `B] but is here used with type [ `B]
> > (denoting the "y" from the "f y")
> >
> > How can I say to OCaml that "y" can't be of type `A ?
>
> well you can do the following :
>
> let g x = match (x:[`A|`B]) with `A -> () | `B as y -> f y;;
>
> for more complex case, you can do something like :
>
> type b = [ `B ]
>
> let g x = match (x:[`A|`B]) with `A -> () | #b as y -> f y;;
>
> --
> Rémi Vanicat
> vanicat@labri.u-bordeaux.fr
> http://dept-info.labri.u-bordeaux.fr/~vanicat
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners