caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* A function returning itself
@ 2008-11-24 22:37 Paolo Donadeo
  2008-11-24 22:43 ` [Caml-list] " Mathias Kende
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Donadeo @ 2008-11-24 22:37 UTC (permalink / raw)
  To: OCaml mailing list

Hi everybody, I need your help again.

In a particular application I need (or at least I think so...) to
create a function that, besides it's own computation, returns itself.
Something like:

$ cat test.ml
let rec f a b =
  let computation = a + b in
    (computation, f)
;;

This file doesn't compile; the error is:

$ ocamlc -c test.ml
File "test.ml", line 3, characters 4-20:
This expression has type int * (int -> int -> int * 'a)
but is here used with type int * 'a

If I use the -rectypes option, it seems to work, even though the
inferred type is quite cryptic (to me):

$ ocamlc -c -rectypes -i test.ml
val f : int -> int -> int * 'a as 'a

Since I don't completely understand the inner implications of
-rectypes and a quick check in the mailing list archives revealed that
"rectypes is evil", the question I pose is whether is it possible to
avoid the -rectypes option in this particular case, possibly with the
definition of an auxiliary type.

My idea is that the function makes a computation and then decides if
the next computation of the same type will be carried out by "f"
itself, or by another function with identical signature.


-- 
Paolo
~
~
:wq


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

* Re: [Caml-list] A function returning itself
  2008-11-24 22:37 A function returning itself Paolo Donadeo
@ 2008-11-24 22:43 ` Mathias Kende
  2008-11-24 22:49   ` Paolo Donadeo
  0 siblings, 1 reply; 3+ messages in thread
From: Mathias Kende @ 2008-11-24 22:43 UTC (permalink / raw)
  To: Paolo Donadeo; +Cc: OCaml mailing list

Le lundi 24 novembre 2008 à 23:37 +0100, Paolo Donadeo a écrit :
> Since I don't completely understand the inner implications of
> -rectypes and a quick check in the mailing list archives revealed that
> "rectypes is evil", the question I pose is whether is it possible to
> avoid the -rectypes option in this particular case, possibly with the
> definition of an auxiliary type.

Usually, using an intermediate type with a constructor, you can achieve
such thing. Here, something along the line of this should work :

type r = {res: int; f: int -> int -> r}
let rec fct a b = {res = a + b; f = fct}

Mathias


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

* Re: [Caml-list] A function returning itself
  2008-11-24 22:43 ` [Caml-list] " Mathias Kende
@ 2008-11-24 22:49   ` Paolo Donadeo
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Donadeo @ 2008-11-24 22:49 UTC (permalink / raw)
  To: OCaml mailing list

> Usually, using an intermediate type with a constructor, you can achieve
> such thing. Here, something along the line of this should work :

Thanks, this is exactly what I need.


-- 
Paolo
~
~
:wq


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

end of thread, other threads:[~2008-11-24 22:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-24 22:37 A function returning itself Paolo Donadeo
2008-11-24 22:43 ` [Caml-list] " Mathias Kende
2008-11-24 22:49   ` Paolo Donadeo

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