From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 1627CBC6B for ; Wed, 19 Sep 2007 10:42:50 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJV+8EbAXQInlGdsb2JhbACOEgIBAQcEBgcI X-IronPort-AV: E=Sophos;i="4.20,272,1186351200"; d="scan'208";a="16407690" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 19 Sep 2007 10:44:02 +0200 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l8J8hKZm011262 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 19 Sep 2007 10:43:23 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CAC9+8EaBrw8E/2dsb2JhbAA X-IronPort-AV: E=Sophos;i="4.20,272,1186351200"; d="scan'208";a="2902863" Received: from ext.lri.fr ([129.175.15.4]) by mail3-smtp-sop.national.inria.fr with ESMTP; 19 Sep 2007 10:44:02 +0200 Received: from localhost (localhost [127.0.0.1]) by ext.lri.fr (Postfix) with ESMTP id 8CBCDA4707; Wed, 19 Sep 2007 10:44:01 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at lri.fr Received: from ext.lri.fr ([127.0.0.1]) by localhost (ext.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lUoSGCFKlCqf; Wed, 19 Sep 2007 10:44:01 +0200 (CEST) Received: from smtp.lri.fr (vhost3-23 [129.175.3.23]) by ext.lri.fr (Postfix) with ESMTP id 76D63A4703; Wed, 19 Sep 2007 10:44:01 +0200 (CEST) Received: from serveur9-10.lri.fr (serveur9-10 [129.175.9.10]) by smtp.lri.fr (Postfix) with ESMTP id 7CEF5E0503; Wed, 19 Sep 2007 10:44:01 +0200 (CEST) Received: from localhost ([127.0.0.1] ident=signoles) by serveur9-10.lri.fr with esmtp (Exim 3.36 #1 (Debian)) id 1IXvA9-0004Yd-00; Wed, 19 Sep 2007 10:44:01 +0200 Date: Wed, 19 Sep 2007 10:44:01 +0200 (CEST) From: Julien Signoles To: Arthur Chan Cc: caml-list@inria.fr Subject: Re: [Caml-list] Mutually recursive functions in different modules In-Reply-To: <74cabd9e0709172327g42d34407wc7027db6d8c6fba6@mail.gmail.com> Message-ID: References: <74cabd9e0709172327g42d34407wc7027db6d8c6fba6@mail.gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at concorde with ID 46F0E128.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; signoles:01 signoles:01 lri:01 recursive:01 recursive:01 higher-order:01 filliatre:01 functors:01 struct:01 struct:01 sig:01 val:01 sig:01 val:01 higher-order:01 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)