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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Naming polymorphic variant types
From: "Nick Alexander" <nalexander@amavi.com>

> module type TEST =
>   sig
>     type outer = [ `A | `B]
>     and inner = [ `B]
>     val x : outer -> outer
>     val y : inner
>   end
> module Test : TEST
> # let p = Test.y;;
> val p : Test.inner = `B
> # let q = Test.x p;;
>                  ^
> This expression has type Test.inner = [ `B] but is here used with type
>   Test.outer = [ `A | `B]
> The first variant type does not allow tag(s) `A
>
> I'm confused.  Can I get an explanation for why the parameter to a
> function with a sum variant type needs all summands to be possible
> arguments? 

Inner is a subtype of outer, but they cannot be unified (unification
requires equality, not subtyping).
 
You can get subtyping by writing explicit coercions:

# let q = Test.x (p :> Test.outer);;
val q : Test.outer = `A

You can also give polymorphic types to your values in the interface:
  val x : [< outer] -> outer
and
  val y : [> inner]
are both valid, and one of them is enough to allow (Test.x p) without
coercion. I.e., [< outer] can be unified with inner, and [> inner] can
be unified with outer.

        Jacques Garrigue
-------------------
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