caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Getting rid of impossible polymorphic variant tags from infered types
@ 2004-03-01 15:09 Florian Hars
  2004-03-01 16:35 ` Claudio Sacerdoti Coen
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Hars @ 2004-03-01 15:09 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2004-03-03 10:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-01 15:09 [Caml-list] Getting rid of impossible polymorphic variant tags from infered types Florian Hars
2004-03-01 16:35 ` Claudio Sacerdoti Coen
2004-03-03 10:27   ` Florian Hars
2004-03-03 10:40     ` Claudio Sacerdoti Coen

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