Version française
Home     About     Download     Resources     Contact us    
Browse thread
polymorphic variant and subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <remi.vanicat@l...>
Subject: polymorphic variant and subtyping
I have try this :

# type 'a t = [ `A of 'a];;
type 'a t = [ `A of 'a]
# type arg1 = [`A | `B];;
type arg1 = [ `A | `B]
# type arg2 = [`A];;
 type arg2 = [ `A]
# type t1 = arg1 t;;
type t1 = arg1 t
# type t2 = arg2 t;;
type t2 = arg2 t
# let a2 = (`A : arg2);;
val a2 : arg2 = `A
# let a1 = (a1 :> arg1);;
val a1 : arg1 = `A
# let t2 = (`A a2 : t2);;
val t2 : t2 = `A `A
# let t1 = (t2 :> t1);;
This expression cannot be coerced to type t1 = [ `A of arg1]; it has type
  t2 = [ `A of arg2]
but is here used with type arg1 #t as 'a = 'a
Type arg2 = [ `A] is not compatible with type arg1 = [ `A | `B] 

but 
# let f : t2 -> t1 = function
    `A x -> `A (x :> arg1)

do what i want. It seems to me that caml don't infere subtyping
relationship, when ithe subtyping relation come from argument of the
type. 

Is there a good way in ocaml of doing this ? 


-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat