Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
variant filtering
[ 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: variant filtering
From: Manuel Fahndrich <>

> Variants are great, but pattern matching should filter used cases in the
> default branch. This seems to be a dual of the record modification problem
> we've seen some time ago on this list.
> Consider the following code:
> type 'a status = [`Success 'a | `FailureA | `FailureB | `FailureC ];;
> let f () = (`Success "Yeah" :> string status)
> let g () =
>    match f () with
>      `Success s -> `Success 1
>    | other      -> (other :> int status)

Indeed the above code will cause a type error.
However, with variants there is a workaround:

let g () =
  match f () with
    `Success s -> `Success 1
  | `FailureA | `FailureB | `FailureC as other -> other

Compared to Pottier's Wallace (or Pessaux's exceptions), you have to
explicitely give the list of the cases matched by other. Still this
will produce the same code as your example would have (had it be

Originally, this was a design choice to avoid having to handle
specifically row variables: now you cannot name them, but only the
variant type containing them. This means that two different variant
types cannot share the same row variable, as would be needed in your

With the conjunctive typing in ocaml 2.99 variants, there may also be
new theoretical problems for an approach where rows can be shared. I
did not really check.

Jacques Garrigue      Kyoto University     garrigue at
		<A HREF=>JG</A>