caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* coercions issues
@ 2007-03-12 20:11 Till Varoquaux
  2007-03-12 21:11 ` [Caml-list] " Chris King
  0 siblings, 1 reply; 3+ messages in thread
From: Till Varoquaux @ 2007-03-12 20:11 UTC (permalink / raw)
  To: ocaml ml

I still can't wrap my head around the whole polymorphic variant of
Ocaml (although I am making steady progresses). Anyways here is my
latest sore point:

This expression cannot be coerced to type
  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m;
it has type O.o Ast_js.T.expr Mon.m but is here used with type
  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m
Type
  O.o Ast_js.T.expr =
    [ `Binop of To.binop * To.expr * To.expr
    | `Cst of To.constant
    | `Ecall of To.ident * To.arg list
    | `Fun of To.ident list * To.bloc
    | `Lval of To.lvalue
    | `Unop of To.unop * To.expr ]
is not compatible with type
  O.o Ast.T.expr =
    [ `Binop of To.binop * To.expr * To.expr
    | `Cst of To.constant
    | `Ecall of To.ident * To.arg list
    | `Epos of Ast.location * To.expr
    | `Fun of To.ident list * To.bloc
    | `Lval of To.lvalue
    | `Unop of To.unop * To.expr ]
The first variant type does not allow tag(s) `Epos

I am a little confused as to why this coercion is impossible... It
looks like a simple upward cast to me.
Cheers,
Till


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] coercions issues
  2007-03-12 20:11 coercions issues Till Varoquaux
@ 2007-03-12 21:11 ` Chris King
  2007-03-12 21:35   ` Till Varoquaux
  0 siblings, 1 reply; 3+ messages in thread
From: Chris King @ 2007-03-12 21:11 UTC (permalink / raw)
  To: Till Varoquaux; +Cc: ocaml ml

On 3/12/07, Till Varoquaux <till.varoquaux@gmail.com> wrote:
> This expression cannot be coerced to type
>  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m;
> it has type O.o Ast_js.T.expr Mon.m but is here used with type
>  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m
> Type
>  O.o Ast_js.T.expr =
>    [ `Binop of To.binop * To.expr * To.expr
>    | `Cst of To.constant
>    | `Ecall of To.ident * To.arg list
>    | `Fun of To.ident list * To.bloc
>    | `Lval of To.lvalue
>    | `Unop of To.unop * To.expr ]
> is not compatible with type
>  O.o Ast.T.expr =
>    [ `Binop of To.binop * To.expr * To.expr
>    | `Cst of To.constant
>    | `Ecall of To.ident * To.arg list
>    | `Epos of Ast.location * To.expr
>    | `Fun of To.ident list * To.bloc
>    | `Lval of To.lvalue
>    | `Unop of To.unop * To.expr ]
> The first variant type does not allow tag(s) `Epos

What sort of type is 'a O.m (or 'a Mon.m)?  If it's something whose 'a
member is mutable, then such a coercion will be impossible (it would
allow you to set the member to have the `Epos tag, which it can't).
This would also happen if 'a O.m is declared as an abstract type,
since O'Caml assumes invariance by default.  In the latter case, you
need to specify that 'a O.m is covariant with its parameter:

type +'a O.m

This tells Caml that if 'a is a subtype of 'b, then 'a O.m is indeed a
subtype of 'b O.m and you will be allowed to perform the above
coercion.

Hope this helps.

- Chris


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] coercions issues
  2007-03-12 21:11 ` [Caml-list] " Chris King
@ 2007-03-12 21:35   ` Till Varoquaux
  0 siblings, 0 replies; 3+ messages in thread
From: Till Varoquaux @ 2007-03-12 21:35 UTC (permalink / raw)
  To: Chris King; +Cc: ocaml ml

Great!

That was indeed really helpfull! I really missed out that last bit, I
sould have payed more attention.

'a Mon.m and 'a O.m are monadic values. O.m and Mon.m are the same
type.  My intuition would be that they should be variant  ((a :> b m)
should behave like a >>= (fun x -> return (x:>b) where a is a value m
a monadic type and b a supertype of a)) can anyone concur that.

Cheers,
Till

On 3/12/07, Chris King <colanderman@gmail.com> wrote:
> On 3/12/07, Till Varoquaux <till.varoquaux@gmail.com> wrote:
> > This expression cannot be coerced to type
> >  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m;
> > it has type O.o Ast_js.T.expr Mon.m but is here used with type
> >  O.o Ast.T.expr O.m = O.o Ast.T.expr Mon.m
> > Type
> >  O.o Ast_js.T.expr =
> >    [ `Binop of To.binop * To.expr * To.expr
> >    | `Cst of To.constant
> >    | `Ecall of To.ident * To.arg list
> >    | `Fun of To.ident list * To.bloc
> >    | `Lval of To.lvalue
> >    | `Unop of To.unop * To.expr ]
> > is not compatible with type
> >  O.o Ast.T.expr =
> >    [ `Binop of To.binop * To.expr * To.expr
> >    | `Cst of To.constant
> >    | `Ecall of To.ident * To.arg list
> >    | `Epos of Ast.location * To.expr
> >    | `Fun of To.ident list * To.bloc
> >    | `Lval of To.lvalue
> >    | `Unop of To.unop * To.expr ]
> > The first variant type does not allow tag(s) `Epos
>
> What sort of type is 'a O.m (or 'a Mon.m)?  If it's something whose 'a
> member is mutable, then such a coercion will be impossible (it would
> allow you to set the member to have the `Epos tag, which it can't).
> This would also happen if 'a O.m is declared as an abstract type,
> since O'Caml assumes invariance by default.  In the latter case, you
> need to specify that 'a O.m is covariant with its parameter:
>
> type +'a O.m
>
> This tells Caml that if 'a is a subtype of 'b, then 'a O.m is indeed a
> subtype of 'b O.m and you will be allowed to perform the above
> coercion.
>
> Hope this helps.
>
> - Chris
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-03-12 21:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-12 20:11 coercions issues Till Varoquaux
2007-03-12 21:11 ` [Caml-list] " Chris King
2007-03-12 21:35   ` Till Varoquaux

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).