如何从SML中的另一个文件导入相对于导入程序的路径?

leo*_*sai 4 sml smlnj

我正在使用SML/NJ,我需要使用f1.sml另一个文件中某个文件中的一组函数f2.sml.

但是,我不是f2.sml直接运行,而是从其他地方导入它.

如果我使用带有相对于透视图的路径的use命令f2.sml,那么在我导入时,它将从正在运行的脚本透视图中查找提供的路径.f1.smlf2.smlf2.sml

我不能使用绝对路径,我不想合并这两个文件的内容.

很抱歉,如果这是一个简单的语言应用程序,但我是SML的新手,但还没有找到答案.

Ion*_*tan 7

我建议使用SML/NJ的编译管理器(CM).它是SML/NJ上下文中SML代码的构建系统.如果您需要更高级的功能,它会变得非常复杂,但它很容易上手.我会告诉你一个准系统结构,你可以根据需要进行调整.它已经安装了SML/NJ,因此没有安装过程.

我将在此示例中使用以下目录结构(不强加文件扩展名,只是约定):

.
??? build.cm
??? src
    ??? foo.fun
    ??? foo.sig
    ??? main.sml
Run Code Online (Sandbox Code Playgroud)

build.cm

group
  (* CM allows you to selectively export defined modules (structures,
     signatures and functors) by listing them here. It's useful for
     libraries. *)

  source (-)       (* export all defined modules *)

  structure Main   (* OR, export selectively *)
  signature FOO
  functor Foo
is
  (* Import the SML standard library, aka Basis.  *)
  (* See: http://sml-family.org/Basis/ *)
  $/basis.cm

  (* Import the SML/NJ library *)
  (* Provides extra data structures and algorithms. *)
  (* See: https://www.smlnj.org/doc/smlnj-lib/Manual/toc.html *)
  $/smlnj-lib.cm

  (* List each source file you want to be considered for compilation. *)
  src/main.sml
  src/foo.sig
  src/foo.fun
Run Code Online (Sandbox Code Playgroud)

SRC/main.sml

structure Main =
  struct
    (* You don't have to import the `Foo` functor. *)
    (* It's been done in build.cm already. *)
    structure F = Foo()

    fun main () =
      print (F.message ^ "\n")
  end
Run Code Online (Sandbox Code Playgroud)

SRC/foo.sig

signature FOO =
  sig
    val message : string
  end
Run Code Online (Sandbox Code Playgroud)

SRC/foo.fun

(* You don't have to import the `FOO` signature. *)
(* It's been done in build.cm already. *)
functor Foo() : FOO =
  struct
    val message = "Hello, World!"
  end
Run Code Online (Sandbox Code Playgroud)

用法

有了这个结构,你可以CM.make通过调用你定义的任何函数来开始编译使用和运行:

$ sml
Standard ML of New Jersey v110.82 [built: Tue Jan  9 20:54:02 2018]
- CM.make "build.cm";
val it = true : bool
-
- Main.main ();
Hello, World!
val it = () : unit
Run Code Online (Sandbox Code Playgroud)