caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* type-checking difficulties in recursive functions
@ 2005-04-27 19:14 Mike Hamburg
  2005-04-27 20:13 ` [Caml-list] " Andreas Rossberg
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Hamburg @ 2005-04-27 19:14 UTC (permalink / raw)
  To: caml-list

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
	else
		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
	else
		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?

Thanks,
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 :-)


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

end of thread, other threads:[~2005-04-27 20:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-27 19:14 type-checking difficulties in recursive functions Mike Hamburg
2005-04-27 20:13 ` [Caml-list] " Andreas Rossberg
2005-04-27 20:55   ` brogoff

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