List Info

Thread: "ocaml_beginners"::[] Covariant types and polymorphic variants




"ocaml_beginners"::[] Covariant types and polymorphic variants
user name
2006-06-21 16:07:44
Hi,

Am I right in thinking that covariant parametric types allow
subtyping
on polymorphic variants?    I just want to check that this
is so,
because until now I have not used covariant parametric types
at all.

Rather contrived example, but...

type colour = [ `Blue | `Green | `Red ]

type +'a blob

val make_rob : [ `Blue | `Red ] -> [ `Blue | `Red ] blob
val make_green : unit -> [ `Green ] blob

val print_blob : colour blob -> unit

let x = make_rob `Blue in
  print_blob x

This works because of covariant parametric types, yes? 
Basically, 'x'
is an object of type `Blue blob which is a subtype of colour
blob.  If
blob was of specified as

type 'a blob

make_rob would have to return type colour blob for it to
work?  This
seems pretty neat.


Cheers,
Chris Campbell


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Yahoo! Groups gets a make over. See the new email design.
http://us.click.yahoo.com/XISQkA/lOaOAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] Covariant types and polymorphic variants
user name
2006-06-21 17:49:08
2006/6/21, Chris Campbell <cyberdanxgmail.com>:
> Hi,
>
> Am I right in thinking that covariant parametric types
allow subtyping
> on polymorphic variants?

'a t is covariant exactly mean that if you cn coerce foo
into bar, you
can coerce
foo t into bar t, so yes you are right.

>   I just want to check that this is so,
> because until now I have not used covariant parametric
types at all.
>
> Rather contrived example, but...

a simple example of covariant type is list, so another
example would be :

# let f x = (x : [ `Bla | `Bli ] :> [`Bla | `Bli | `Foo
]);;
val f : [ `Bla | `Bli ] -> [ `Bla | `Bli | `Foo ] =
<fun>


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Yahoo! Groups gets a make over. See the new email design.
http://us.click.yahoo.com/XISQkA/lOaOAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] Covariant types and polymorphic variants
user name
2006-06-21 20:46:26
Thanks.

How do we determine which polymorphic variant it has by
pattern matching?

Suppose we have

type dim =  [`three | `four | `two ]
type +'a vertex_array = [`float] Raw.t (* in .mli file, it
is abstract *)

val set_vertex3 : [ `four | `three ] vertex_array -> int
-> vertex3 -> unit

How do we make this work?

let set_vertex3 ?? i (a, b, c) =
  match ?? with
    | `three -> (Raw.sets_float v ~pos:(i*3) [| a; b; c
|])
    | `four -> (Raw.sets_float v ~pos:(i*4) [| a; b; c;
1.0 |])

I tried

let set_vertex3 (v : ([`three | `four] as vt) vertex_array)
i (a, b,
c) but the compiler didn't understand it.

Thanks,
Chris


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Something is new at Yahoo! Groups.  Check out the enhanced
email design.
http://us.click.yahoo.com/SISQkA/gOaOAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


"ocaml_beginners"::[] Covariant types and polymorphic variants
user name
2006-06-21 21:04:05
2006/6/21, Chris Campbell <cyberdanxgmail.com>:
> Thanks.
>
> How do we determine which polymorphic variant it has by
pattern matching?
>
> Suppose we have
>
> type dim =  [`three | `four | `two ]
> type +'a vertex_array = [`float] Raw.t (* in .mli
file, it is abstract *)
>
> val set_vertex3 : [ `four | `three ] vertex_array ->
int -> vertex3 -> unit
>
> How do we make this work?

It won't work. You cannot match on type. You match on
value. If you
want your function to do different thing if it's a three or
four
vertex_array, you have to put this information somewhere in
the value,
and not in the type.

Notice that type infromation are lost at runtime, only stay
the values.


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Great things are happening at Yahoo! Groups.  See the new
email design.
http://us.click.yahoo.com/TISQkA/hOaOAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )