Browse thread
[Caml-list] polymorphic function not recognised as such
-
Benjamin Geer
- Jean-Baptiste Rouquier
-
Benjamin Geer
- Olivier Andrieu
- Michal Moskal
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Olivier Andrieu <andrieu@i...> |
| Subject: | Re: [Caml-list] polymorphic function not recognised as such |
Benjamin Geer [Sunday 14 December 2003] :
>
> Jean-Baptiste Rouquier wrote:
> > compare_things : thing -> thing -> ('a -> 'a -> bool) -> bool
> > doesn't mean that comparison_fun has to be polymorphic, but it means
> > that any function with type ('a -> 'a -> bool), where 'a is any type, is
> > acceptable. For instance, the type checker would accept a function of
> > the type (char -> char -> bool).
>
> OK, but it seems that the type checker has inferred the type of
> comparison_fun just by looking at its first use in compare_things,
> and has decided that it must be monomorphic; this is an incorrect
> inference.
> My naive impression is that if the type checker was willing to
> accept both uses of comparison_fun as valid (or perhaps if it also
> looked at the places where compare_things is called), it would
> conclude that comparison_fun does indeed have to be polymorphic.
> In that case, I would get a compile error if I tried to pass a
> non-polymorphic function to compare_things. Would this be this
> asking too much? (I suspect the answer is yes; I'm just
> curious...)
You could use a record holding a field with a fully polymorphic type :
type t = { c : 'a. 'a -> 'a -> bool }
let compare_things t1 t2 comparison_fun =
match (t1, t2) with
(Int i, _) -> comparison_fun.c i (int_of_thing t2)
| (String s, _) -> comparison_fun.c s (string_of_thing t2) ;;
--
Olivier
-------------------
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