是否可以将一组递归函数拆分为 OCaml 中的 2 个文件?

Is it possible to split a set of recursive functions into 2 files in OCaml?

我有一个非常非常长的文件,其中包含一组递归函数。递归是必要的,代码也是有效的。但我只想将文件拆分为 2 个或更多文件以获得更好的可读性。但我不明白我们如何在 OCaml 中拆分 let rec ... and ......

有谁知道 OCaml 是否提供任何机制(例如,指定接口或编写 makefile 的方法)来这样做?

非常非常长的文件可能如下所示:

let rec f1 () =
  ...
  f7 (); (* f7 is called only once by all the functions in the file *)
  f2 ();
  ...
and f2 () =
  ...
  f1 ();
...
and f7 () =
  ...
  f1 (); 
  ...

有一种方法:互递归函子。请参阅 this great article on separate compilation in OCaml and this 鼓舞人心的文章,其中提供了主要思想。

这是一个例子。我创建了 4 个界面 mli 文件:

$ ls *.mli
Make_moduleA.mli  Make_moduleB.mli  ModuleA.mli  ModuleB.mli

和 3 个实现文件:

$ ls *.ml
Make_moduleA.ml  Make_moduleB.ml  main.ml

这是接口文件的内容:

(* ModuleA.mli *)
module type ModuleA = sig
   val fa : int -> unit
end

(* ModuleB.mli *)
module type ModuleB = sig
   val fb : int -> unit
end

(* Make_moduleA.mli *)
open ModuleA
open ModuleB
module type Make_moduleA_sig =
  functor (Mb : ModuleB) ->
    sig
      val fa : int -> unit
    end
module Make_moduleA : Make_moduleA_sig

(* Make_moduleB.mli *)
open ModuleA
open ModuleB
module type Make_moduleB_sig =
  functor (Ma : ModuleA) ->
    sig
      val fb : int -> unit
    end
module Make_moduleB : Make_moduleB_sig

和互递归仿函数:

(* Make_moduleA.ml *)
open ModuleA
open ModuleB    
module type Make_moduleA_sig =
  functor (Mb : ModuleB) ->
    sig
      val fa : int -> unit
    end
module Make_moduleA_impl =
  functor (Mb : ModuleB) ->
    struct
      let rec fa (n : int) =
        if n > 0 then
          (Printf.printf "A: %d\n" n;
           Mb.fb (n - 1))
    end
module Make_moduleA = (Make_moduleA_impl : Make_moduleA_sig)

(* Make_moduleB.ml *)
open ModuleA
open ModuleB    
module type Make_moduleB_sig =
  functor (Ma : ModuleA) ->
    sig
      val fb : int -> unit
    end    
module Make_moduleB_impl =
  functor (Ma : ModuleA) ->
    struct
      let rec fb (n : int) =
        if n > 0 then
          (Printf.printf "B: %d\n" n;
           Ma.fa (n - 1))
    end    
module Make_moduleB = (Make_moduleB_impl : Make_moduleB_sig)

现在让我们合并模块:

(* main.ml *)
open ModuleA
open ModuleB
open Make_moduleA
open Make_moduleB

module rec MAimpl : ModuleA = Make_moduleA(MBimpl)
and MBimpl : ModuleB = Make_moduleB(MAimpl)

let _ =     (* just a small test *)
  MAimpl.fa 4;
  print_endline "--------------";
  MBimpl.fb 4

构建命令序列:

ocamlc -c ModuleA.mli
ocamlc -c ModuleB.mli
ocamlc -c Make_moduleA.mli
ocamlc -c Make_moduleB.mli

ocamlc -c Make_moduleA.ml
ocamlc -c Make_moduleB.ml
ocamlc -c main.ml

ocamlc Make_moduleA.cmo Make_moduleB.cmo main.cmo

测试结果:

$ build.sh && ./a.out
A: 4
B: 3
A: 2
B: 1
--------------
B: 4
A: 3
B: 2
A: 1

仅供参考,还有另一种方法,不带仿函数。您必须将其他模块中的函数作为函数的参数。示例:我们递归定义了 evenodd,但我们希望 even 位于模块 A 中,而 odd 位于模块 [=19] 中=].

第一个文件:

(* A.ml *)
let rec even odd n =  
  if n=0
  then true
  else 
    if n=1 
    then false
    else (odd even) (n-1) ;;

第二个文件:

(* B.ml *)
let rec odd even n =  
if n=1
  then true
  else 
    if n=0 
    then false
    else even odd (n-1) ;;

第三个文件:

(* C.ml *)
let even = A.even B.odd 
and odd  = B.odd A.even ;;
print_endline (if even 5 then  "TRUE" else "FALSE") ;;
print_endline (if odd 5 then  "TRUE" else "FALSE") ;;

编译(必须使用选项-rectypes):

ocamlc -rectypes -c A.ml 
ocamlc -rectypes -c B.ml
ocamlc -rectypes -c C.ml
ocamlc -rectypes  A.cmo B.cmo C.cmo 

我不确定我是否会推荐它,但它确实有效。