List Info

Thread: "ocaml_beginners"::[] Covariants and contravariants for the clueless




"ocaml_beginners"::[] Covariants and contravariants for the clueless
country flaguser name
Portugal
2007-05-18 05:25:30

Hello,

I have seen the following types declared for the stdlib Map:
type +'a t
type (+'a) t

Could anyone tell me where I can get (very) basic info on this? I have
seen it and a "- 'a" in the manual but the explanation is beyond me.
Any simple examples appreciated.

TIA,
Hugo F.

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Covariants and contravariants for the clueless
country flaguser name
Moldova, Republic of
2007-05-18 07:35:15

Shalom, Hugo.

HF> I have seen the following types declared for the stdlib Map:
HF> type +'a t
HF> type (+'a) t
HF>
HF> Could anyone tell me where I can get (very) basic info on this? I have
HF> seen it and a "- 'a" in the manual but the explanation is beyond me.
HF> Any simple examples appreciated.

I could not understand this thing too, until have written
some amount of code that uses subtyping (it is available for
polymorphic variants and objects).
If there are two types, t and t', and t' is subtype of t,
and type u = (t) Map.t and type u' = (t') Map.t, then:
1. if Map.t is declared as "type ('a) t", then u is not
subtype of u', and u' is not subtype of u.
2. if Map.t is declared as "type (+'a) t", then u' is a
subtype of u -- this is covariance, and subtyping relation
works in forward direction.
3. if Map.t is declared as "type (-'a) t", then u is a
subtype of u' -- this is contravariance, and subtyping
relation works in opposite direction.
Of course, real Map.t is declared as type (+'a) t, and
this is very reasonable.
And one more explanation:
http://caml.inria.fr/pub/ml-archives/caml-list/2001/11/fa520f3646112ef86c00075de268c661.en.html

--
WBR,
dmitry mailto: gds-mlsts%40moldavcable.com">gds-mlstsmoldavcable.com

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Covariants and contravariants for the clueless
country flaguser name
Portugal
2007-05-21 03:50:33

Dmitry,

Thank you very much.

Regards,
Hugo F.

dmitry grebeniuk wrote:
> Shalom, Hugo.
>
> HF> I have seen the following types declared for the stdlib Map:
>; HF> type +'a t
> HF> type (+'a) t
> HF>
> HF> Could anyone tell me where I can get (very) basic info on this? I have
>; HF> seen it and a "- 'a" in the manual but the explanation is beyond me.
> HF> Any simple examples appreciated.
>
> I could not understand this thing too, until have written
> some amount of code that uses subtyping (it is available for
> polymorphic variants and objects).
> If there are two types, t and t', and t' is subtype of t,
> and type u = (t) Map.t and type u' = (t') Map.t, then:
> 1. if Map.t is declared as "type ('a) t", then u is not
> subtype of u', and u' is not subtype of u.
> 2. if Map.t is declared as "type (+'a) t", then u' is a
> subtype of u -- this is covariance, and subtyping relation
> works in forward direction.
> 3. if Map.t is declared as "type (-'a) t", then u is a
> subtype of u' -- this is contravariance, and subtyping
> relation works in opposite direction.
> Of course, real Map.t is declared as type (+'a) t, and
> this is very reasonable.
> And one more explanation:
> http://caml.inria.fr/pub/ml-archives/caml-list/2001/11/fa520f3646112ef86c00075de268c661.en.html
>;
>

__._,_.___
.

__,_._,___
[1-3]

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