List Info

Thread: "ocaml_beginners"::[] Polymorphic method doing my head in!




"ocaml_beginners"::[] Polymorphic method doing my head in!
user name
2006-11-21 20:28:31

Hi,

I'm getting this error ...

$ ocaml tree/tree.ml 20 10 n
File "tree/tree.ml", line 95, characters 51-411:
This method has type
('a -> (< compare : 'b -> int; to_string : string; .. > as 'b) -> 'a) ->
'a -> 'a
which is less general than 'c. ('c -> 'b -> 'c) -> 'c -> 'c

and I'm not making much process understanding what's going on.
Strangely, when I replace the definition fold_flt (which is the
offending method) to the type exactly as show, the error message is
exactly the same...

any clues as to how to get this to work, or why it will never work,
would be much appreciated. If you take the method out, the code works as
I haven't used the method yet. (It's a shame the refman example is so
simple ... and brief).

thanks,
Jonathan.

(*-----------------------*)
open Printf;;

class oInt (x:int) =
object (self:'a)
val mutable item = x

method get = item
method set x = item <- x
method to_string = Printf.sprintf "%d&quot; item
method compare (y:'a) =
if (self#get) < (y#get) then
-1
else if (self#get) > (y#get) then
1
else
0
end
;;

class oFloat (x:float) =
object (self:'a)
val mutable item = x

method get = item
method set x = item <- x
method to_string = Printf.sprintf "%.2f" item
method compare (y:'a) =
if (self#get) < (y#get) then
-1
else if (self#get) > (y#get) then
1
else
0
end
;;

class ['a] node (v:'a) (lft) (rgt) =
object (self)
val mutable item = v
val mutable left = lft
val mutable right = rgt
val mutable height = 1

method height = height
method item = item
method left = left
method right = right

method set_left lft = left <- lft
method set_right rgt = right <- rgt
method set_item itm = item <- itm

method print (i:int) =
(match right with
None -> ()
| Some y ->
y#print (i + 1)
);
printf "%*s(%s:%d)n&quot; (2*i) " " (item#to_string) height;
match left with
None -> ()
| Some y ->
y#print (i + 1)

method insert (itm:'a) dups =
let c = match (item#compare itm, left, right) with
(-1,_,None) -> right <- Some (new node itm None None); 1
|(-1,_,Some y) -> y#insert itm dups
|(1,None,_) -> left <- Some (new node itm None None); 1
|(1,Some y,_) -> y#insert itm dups
|(0,None,_) ->
if dups then
(left <- Some (new node itm None None); 1)
else
(item <- itm; 0)
|(0,Some y,_) ->
if dups then
y#insert itm dups
else
(item <- itm; 0)
|(_,_,_) -> 0
in
c

method iter (f:'a->unit) =
(match left with
None -> ()
| Some l -> l#iter f
);
f item;
(match right with
None -> ()
| Some r -> r#iter f)

method fold_lft: 'b.('b->;'a->'b)->'b-&gt;'b = fun fnc accum ->
let accuml = match left with
None -> accum
| Some l -> l#fold_lft fnc accum
in
let accumt = fnc accuml item in
let accumr = match right with
None -> accumt
| Some r -> r#fold_lft fnc accumt
in
accumr
end
;;

let prt_list lst =
printf "[ ";
let rec aux lst =
match lst with
hd::tl ->
printf "%s; " hd#to_string;
aux tl
| [] -> ()
in
aux lst;
printf "]n&quot;
;;

class ['a] avl dups =
object (self)
val duplicates = dups
val mutable count = 0
val mutable nodes = None

method duplicates = duplicates
method count = count

method insert (itm:'a) =
match nodes with
None ->
nodes <- Some (new node itm None None);
count <- 1
| Some n ->
count <- count + n#insert itm duplicates

method print (ind:int) =
match nodes with
None -> printf "()n&quot;
| Some n ->
n#print 1

method do_fold f =
match nodes with
None -> ()
| Some n -> n#iter f

end
;;

let rec do_int t1 n rng =
if n > 0 then
let _ = t1#insert (new oInt (Random.int rng)) in
do_int t1 (n - 1) rng
else
()
;;

let int_ver n rng yn =
let t1 = new avl (yn = "y&quot;) in
do_int t1 n rng;
t1#print 1;
printf "n%d elementsn&quot; t1#count
;;

let rec do_float t1 n rng =
if n > 0 then
let _ = t1#insert (new oFloat ((float_of_int (Random.int
rng))/.6.0)) in
do_float t1 (n - 1) rng
else
()
;;

let float_ver n rng yn =
let t1 = new avl (yn = "y&quot;) in
do_float t1 n rng;
t1#print 1;
printf "n%d elementsn&quot; t1#count
;;

let main () =
Random.init 2;
let arg = int_of_string Sys.argv.(1) in
let rng = int_of_string Sys.argv.(2) in
let yn = Sys.argv.(3) in
float_ver arg rng yn;
int_ver arg rng yn
;;

main();;

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )