caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Oddness with recursive polymorphic variants
@ 2006-05-04 15:50 Jeremy Yallop
  2006-05-04 19:03 ` [Caml-list] " Nils Gesbert
  0 siblings, 1 reply; 5+ messages in thread
From: Jeremy Yallop @ 2006-05-04 15:50 UTC (permalink / raw)
  To: caml-list

I have two polymorphic variant types, as follows:

   type f = [`A | `B of f]
   type g = [f | `C]

Next, I have a function from f to g:

   let s1 : f -> g = function
     | `A -> `A
     | `B b -> b

Sadly, the compiler rejects this:

     Characters 57-58:
       | `B b -> b;;
                 ^
   This expression has type f but is here used with type g
   The first variant type does not allow tag(s) `C

The error message seems odd.  Why should it matter that g has more tags 
than f, since every value of f is a value of g (by definition)?

Indeed, minor variants of the function are accepted.  Both of the 
following are ok:

   let s2 : f -> g = function
     | `A -> `A
     | `B (#f as b) -> b

   let s3 : f -> g = function
     | `A -> `A
     | `B ((`A|`B _) as b) -> b

Am I missing something, or is this a bug?

Thanks,

Jeremy.


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

* Re: [Caml-list] Oddness with recursive polymorphic variants
  2006-05-04 15:50 Oddness with recursive polymorphic variants Jeremy Yallop
@ 2006-05-04 19:03 ` Nils Gesbert
  2006-05-04 20:30   ` Nils Gesbert
  2006-05-05  8:04   ` Jeremy Yallop
  0 siblings, 2 replies; 5+ messages in thread
From: Nils Gesbert @ 2006-05-04 19:03 UTC (permalink / raw)
  To: caml-list

Jeremy Yallop <j.d.yallop@sms.ed.ac.uk> a écrit :

» I have two polymorphic variant types, as follows:
» 
»    type f = [`A | `B of f]
»    type g = [f | `C]
» 
» Next, I have a function from f to g:
» 
»    let s1 : f -> g = function
»      | `A -> `A
»      | `B b -> b
» 
» Sadly, the compiler rejects this:
» 
»      Characters 57-58:
»        | `B b -> b;;
»                  ^
»    This expression has type f but is here used with type g
»    The first variant type does not allow tag(s) `C
» 
» The error message seems odd.  Why should it matter that g has more tags 
» than f, since every value of f is a value of g (by definition)?

f is indeed a subtype of g, but to forget the precise type of b (that is, forget the information that it cannot contain the tag `C), you have to use explicit coercion, i. e. :

  let s1 : f -> g = function
     | `A -> `A
     | `B b -> (b :> g)


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

* Re: [Caml-list] Oddness with recursive polymorphic variants
  2006-05-04 19:03 ` [Caml-list] " Nils Gesbert
@ 2006-05-04 20:30   ` Nils Gesbert
  2006-05-05  8:04   ` Jeremy Yallop
  1 sibling, 0 replies; 5+ messages in thread
From: Nils Gesbert @ 2006-05-04 20:30 UTC (permalink / raw)
  To: caml-list

Alternatively, you may also coerce directly the function :
let s1 = ((function `A -> `A | `B b -> b) :> f -> g)


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

* Re: [Caml-list] Oddness with recursive polymorphic variants
  2006-05-04 19:03 ` [Caml-list] " Nils Gesbert
  2006-05-04 20:30   ` Nils Gesbert
@ 2006-05-05  8:04   ` Jeremy Yallop
  1 sibling, 0 replies; 5+ messages in thread
From: Jeremy Yallop @ 2006-05-05  8:04 UTC (permalink / raw)
  To: caml-list

Thanks for all the replies.  My current understanding is as follows:

Given the types

    type f = [`A]
    type g = [f | `C]

then the following function is not acceptable

    let k (x:f) = (x:g)

because f and g are not unifiable: they are "closed rows" with different 
fields.  There are a number of ways to "open" the row, however:

    let k (#f as x:f) = (x:g)

This one is acceptable because the pattern "#f" means "an open row that 
includes all the tags in f".  (That's its type on the rhs, anyway.  The 
pattern (and the function) will accept exactly those tags in the type 
"f").  The type annotation on the parameter doesn't affect the type of 
"x", although the compiler does check that the type of the annotation 
and of the pattern can be unified.  The case where all the tags (only 
one in this case) are enumerated is treated identically:

    let k (`A as x:f) = (x:g)

Finally, the explicit coercion (:>).  Like the acceptable patterns, this 
"opens" the row, allowing it to be unified with "g" (or, indeed, with 
any other row type whose tag parameters don't clash with those of "f").

How does that sound?

Jeremy.


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

* Oddness with recursive polymorphic variants
@ 2006-05-04 15:54 Jeremy Yallop
  0 siblings, 0 replies; 5+ messages in thread
From: Jeremy Yallop @ 2006-05-04 15:54 UTC (permalink / raw)
  To: caml-list

I have two polymorphic variant types, as follows:

   type f = [`A | `B of f]
   type g = [f | `C]

Next, I have a function from f to g:

   let s1 : f -> g = function
     | `A -> `A
     | `B b -> b

Sadly, the compiler rejects this:

     Characters 57-58:
       | `B b -> b;;
                 ^
   This expression has type f but is here used with type g
   The first variant type does not allow tag(s) `C

The error message seems odd.  Why should it matter that g has more tags 
than f, since every value of f is a value of g (by definition)?

Indeed, minor variants of the function are accepted.  Both of the 
following are ok:

   let s2 : f -> g = function
     | `A -> `A
     | `B (#f as b) -> b

   let s3 : f -> g = function
     | `A -> `A
     | `B ((`A|`B _) as b) -> b

Am I missing something, or is this a bug?

Thanks,

Jeremy.


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

end of thread, other threads:[~2006-05-05  8:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-04 15:50 Oddness with recursive polymorphic variants Jeremy Yallop
2006-05-04 19:03 ` [Caml-list] " Nils Gesbert
2006-05-04 20:30   ` Nils Gesbert
2006-05-05  8:04   ` Jeremy Yallop
2006-05-04 15:54 Jeremy Yallop

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