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
type-checking difficulties in recursive functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Mike Hamburg <hamburg@f...>
Subject: type-checking difficulties in recursive functions
I'm having trouble with a recursive function which tries to use itself, 
instantiated to a different type, in the recursion.

As a trivial example similar to what I want to do, consider:

let rec foo p x =
	if p then x
		let something = foo false "Hello"
		in x;;
val foo : bool -> string -> string = <fun>

Now, foo is actually safe as a function from bool -> 'a -> 'a, but the 
recursive declaration doesn't generalize and so the checker doesn't 
realize this.  With magic, I can get the right type relatively safely 
by writing

let rec string_foo p : 'string -> 'string = Obj.magic foo p
and foo p x =
	if p then x
		let something = string_foo false "Hello"
		in x;;
val string_foo : bool -> string -> string = <fun>
val foo : bool -> 'a -> 'a = <fun>

but is there a way to avoid magic?

Mike Hamburg
P.S. I couldn't find the bug in my Netcgi program, but by deleting and 
rewriting part of it, I got it working :-)