caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] polymorphic variants in match statements
@ 2012-01-24  0:53 Milan Stanojević
  2012-01-24  1:21 ` Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Milan Stanojević @ 2012-01-24  0:53 UTC (permalink / raw)
  To: caml-list

Hi, we're trying to understand the type inference with polymorphic
variants in match statements. This is a simplification of an actual
case that happened in practice.

1)
let f i a =
  match i, a with
  | true, `A -> `B
  | false, x -> x

fails with
File "foo.ml", line 4, characters 16-17:
Error: This expression has type [< `A ]
       but an expression was expected of type [> `B ]
       The first variant type does not allow tag(s) `B

2) changing false to _
let f i a =
  match i, a with
  | true, `A -> `B
  | _, x -> x

this succeeds with
val f : bool -> ([> `A | `B ] as 'a) -> 'a

3) changing x in (1) to _ , and using a on the right side
let f i a =
  match i, a with
  | true, `A -> `B
  | false, _ -> a

this fails in the same way as (1)

4) finally adding another case to match statement
let f i a =
  match i, a with
  | true, `A -> `B
  | false, x -> x
  | true, x -> x

this succeeds with the same type as (2)


So it seems there is some interaction between type inference and
exhaustivnest of the match statements.

Can someone shed some light on what is going on here?

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

end of thread, other threads:[~2012-02-11  1:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-24  0:53 [Caml-list] polymorphic variants in match statements Milan Stanojević
2012-01-24  1:21 ` Jacques Garrigue
2012-01-24 17:42   ` Milan Stanojević
2012-01-25  1:21     ` Jacques Garrigue
2012-02-10 22:20     ` Milan Stanojević
2012-02-11  1:14       ` Jacques Garrigue

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