From: Julien Signoles <Julien.Signoles@lri.fr>
To: Arthur Chan <baguasquirrel@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Mutually recursive functions in different modules
Date: Wed, 19 Sep 2007 10:44:01 +0200 (CEST) [thread overview]
Message-ID: <Pine.LNX.4.63.0709191038310.17511@serveur9-10.lri.fr> (raw)
In-Reply-To: <74cabd9e0709172327g42d34407wc7027db6d8c6fba6@mail.gmail.com>
Hello,
> Is it possible to have mutually recursive functions in separate modules?
I know (at least) 4 solutions to your problem: one use recursive modules
as suggested by Jacques Garrigue, one use higher-order functions as
suggested by Jean-Christophe Filliatre, one use functors and one use
references on functions.
For example, if you want something (stupid) like
module A = struct let f x = if x <= 0 then 0 else B.f (x - 2) end
module B = struct let f x = if x = 1 then 1 else A.f (x - 2) end
you can write:
(* 1- using recursive modules *)
module rec A : sig val f : int -> int end = struct
let f x = if x <= 0 then 0 else B.f (x - 2)
end and B : sig val f : int -> int end = struct
let f x = if x = 1 then 1 else A.f (x - 2)
end
(* 2- using higher-order functions *)
module A' = struct let f g x = if x <= 0 then 0 else g (x - 2) end
module B = struct let rec f x = if x = 1 then 1 else A'.f f (x - 2) end
module A = struct let f = A'.f B.f end
(* 3- using functors *)
module FA(X:sig val f : int -> int end) = struct
let f x = if x <= 0 then 0 else X.f (x - 2)
end
module B = struct
let rec f x =
let module A = FA(struct let f = f end) in
if x = 1 then 1 else A.f (x - 2)
end
module A = FA(struct let f = B.f end)
(* 4- using references on functions *)
module A' = struct let f = ref (fun _ -> assert false) end
module B = struct let f x = if x = 1 then 1 else !A'.f (x - 2) end
module A = struct
let () = A'.f := fun x -> if x <= 0 then 0 else B.f (x - 2)
let f = !A'.f
end
In my opinion, solution 1 is the more natural when A and B are in the
same file.
Hope this helps,
Julien
--
mailto:Julien.Signoles@lri.fr ; http://www.lri.fr/~signoles
"In theory, practice and theory are the same,
but in practice they are different" (Larry McVoy)
next prev parent reply other threads:[~2007-09-19 8:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-18 6:27 Arthur Chan
2007-09-18 7:53 ` [Caml-list] " Jacques Garrigue
2007-09-18 14:16 ` Yitzhak Mandelbaum
2007-09-18 11:17 ` Jean-Christophe Filliatre
2007-09-19 8:44 ` Julien Signoles [this message]
2007-09-19 11:40 ` Andreas Rossberg
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=Pine.LNX.4.63.0709191038310.17511@serveur9-10.lri.fr \
--to=julien.signoles@lri.fr \
--cc=baguasquirrel@gmail.com \
--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