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
[Caml-list] polymorphic function not recognised as such
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Benjamin Geer <ben@s...>
Subject: [Caml-list] polymorphic function not recognised as such
Can anyone explain to me why the following doesn't compile:


type thing = Int of int | String of string ;;

let int_of_thing t =
   match t with
       Int i -> i
     | String s -> int_of_string s ;;

let string_of_thing t =
   match t with
       Int i -> string_of_int i
     | String s -> s ;;

let compare_things t1 t2 comparison_fun =
   match (t1, t2) with
       (Int i, _) -> comparison_fun i (int_of_thing t2)
     | (String s, _) -> comparison_fun s (string_of_thing t2) ;;

let is_less_than t1 t2 =
   compare_things t1 t2 (<) ;;

let is_greater_than t1 t2 =
   compare_things t1 t2 (>) ;;


The compiler says:

File "", line 16, characters 38-39:
This expression has type string but is here used with type int

It seems to have decided that in the function compare_things, 
comparison_fun is int -> int -> 'a because that's how it's used in the 
first pattern, not realising that I mean to pass a polymorphic function, 
'a -> 'a -> bool.

Is there a way to make this work?  (Specifying the type of 
comparison_fun as 'a -> 'a -> bool makes no difference.)


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: