caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mike Hamburg <hamburg@fas.harvard.edu>
To: caml-list@inria.fr
Subject: type-checking difficulties in recursive functions
Date: Wed, 27 Apr 2005 15:14:33 -0400	[thread overview]
Message-ID: <fe301a3cb44607962b354db7d852e4c7@fas.harvard.edu> (raw)

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


             reply	other threads:[~2005-04-27 19:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-27 19:14 Mike Hamburg [this message]
2005-04-27 20:13 ` [Caml-list] " Andreas Rossberg
2005-04-27 20:55   ` brogoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fe301a3cb44607962b354db7d852e4c7@fas.harvard.edu \
    --to=hamburg@fas.harvard.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).