Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Getting rid of impossible polymorphic variant tags from infered types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Florian Hars <hars@b...>
Subject: [Caml-list] Getting rid of impossible polymorphic variant tags from infered types
If I write a function to filter out all occurences of values of a specific 
polymorphic variant tag from a list, ocaml infers a return type for this 
function of any type that may contain the tag I have just removed:

# let f l = List.filter (function `Fnord -> false | _ -> true) l;;
val f : ([> `Fnord] as 'a) list -> 'a list = <fun>

While I unterstand why this type is infered, it is nonetheless wrong, it should 
be something like [> 'a \ `Fnord ] list (inventing notation on the fly).
Is there some decent way to tell the compiler that I know more about this 
function than the type checker does?

The following should be safe, but doesn't strike me as especially elegant and 
makes the type more restrictive than I'd like it to be:

type foo = [ `Foo | `Bar | `Fnord ]
type foo_flat = [ `Foo | `Bar ]

let f' l =
   let f_r (v: foo) (init: foo_flat list) =
     match v with
     | `Fnord -> init
     | _ -> Obj.magic v :: init
   in
   List.fold_right f_r l []

Yours, Florian.

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