OCaml 中嵌套签名的示例?

发布于 2024-10-06 12:16:07 字数 179 浏览 13 评论 0原文

在 OCaml 中,您可以嵌套签名:

module type FOO =
sig
  module type BAR
  (* … *)
end

我只是想知道是否有人有任何使用中的示例,因为我想不出任何需要它的地方。我想它可能在函子的返回签名中有用,但我想不出任何具体的东西。

In OCaml, you can nest signatures:

module type FOO =
sig
  module type BAR
  (* … *)
end

I was just wondering if anyone had any examples of this in use, since I can’t think of any places where it would be needed. I imagine it’s probably useful in the return signatures of functors, but I can’t think of any specific things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-10-13 12:16:07

我记得看到过一些模块(可能是电池),其中包含一个 Infix 模块,可以单独打开,并且只有在真正需要时才可以打开。例如,

module Rational = 
  struct
    let add a b = ...
    let sub a b = ...

    module Infix =
      struct
        let (<+>) = add
        let (<->) = sub
      end
  end

通过这种方式,如果您要打开 Rational.Infix 模块,您就不会取消范围(?) 任何与 Rational 中同名的函数>。

我正在开发一个项目,我们使用模块来划分类型。让模块仅定义一种类型并操作该类型有助于组织;特别是当模块很小并且拥有单独的文件并不有利,并且变体类型没有意义时。

module Node = 
  struct 

  end
module Edge = 
  struct

  end

type 'a tree = { nodes : 'a Node.t; edges : 'a Edge.t; }

我们还使用它们,尽管它们是单独的文件(与 -mlpack 结合),用于我们生物数据所需的所有解析器——Nexus、Fasta、Phylip 等。

最后,通常在对新算法进行原型设计时,我们会先用 ocaml 编写它,然后再开发 C 版本。我们通常将 ocaml 版本保留在具有相同函数名称的内部模块中。

module Align = 
  struct
    module OCaml = 
      struct

      end
  end

I recall seeing a few modules (maybe in batteries), that included an Infix module inside that could be opened separately and only when truly wanted. For example,

module Rational = 
  struct
    let add a b = ...
    let sub a b = ...

    module Infix =
      struct
        let (<+>) = add
        let (<->) = sub
      end
  end

In this way if you were to open the Rational.Infix module, you wouldn't de-scope(?) any functions with the same names as anything in Rational.

I am working on a project where we use modules to demarcate types. Having a module define only one type and manipulate that type helps in organization; especially when the modules are small and having a separate file wouldn't be advantageous, and variant types don't make sense.

module Node = 
  struct 

  end
module Edge = 
  struct

  end

type 'a tree = { nodes : 'a Node.t; edges : 'a Edge.t; }

We also use them, although as separate files (combined with -mlpack), for all the parsers we need for biological data --Nexus, Fasta, Phylip, et cetera.

Lastly, often when prototying a new algorithm we will write it in ocaml first, then work on a C version. We usually keep the ocaml version in a inner module with the same function names.

module Align = 
  struct
    module OCaml = 
      struct

      end
  end
星星的轨迹 2024-10-13 12:16:07

我想到的第一个例子: http://caml.inria .fr/pub/docs/manual-ocaml/libref/type_Map.html

(这确实是一个函子签名)

First example which came to my mind : http://caml.inria.fr/pub/docs/manual-ocaml/libref/type_Map.html

(it's indeed a functor signature)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文