在 OCaml 中使用函子作为接口

发布于 2024-09-13 05:02:52 字数 705 浏览 1 评论 0原文

我正在 OCaml 中开发一些算法,这些算法需要某些部分是“可插入的”,以便部分计算留给特定的计算机。

举个例子,假设我有一个像这样的签名:

module type Algorithm = sig
    val feed : float -> unit
    val nth : int -> (float -> float)
end

以及两个不同的实现,即 Alg1Alg2。这个Algorithm模块应该代表像这两个这样的各种实现的接口。

现在我需要另一个组件,我们将其称为 Executor ,它将是通过其接口使用 Alg1Alg2 的模块。

阅读有关函子的内容似乎我应该需要一个函子,它采用 Algorithm 并生成一个 ConcreteExecutor ,其中包含我需要的算法的特定实现。因此,Executor 是一种在其组件上进行参数化的模块。

我说得对吗?这是获得我需要的东西的最佳方式吗?我想知道这样的想法,因为我来自 Java/C++ 背景,所以我习惯使用接口和抽象类,并且我需要以正确的方式解决这个函子/模块抽象问题。

哪种语法是获得我想要的内容的正确语法?

提前致谢

I'm developing some algorithms in OCaml which need some parts to be "pluggable" so that part of the computation is left to specific computators.

Just to make an example suppose I have a signature like this one:

module type Algorithm = sig
    val feed : float -> unit
    val nth : int -> (float -> float)
end

And two different implementations that will be Alg1 and Alg2. This Algorithm module should represent the interface for various implementations like these two one.

Now I need another component, let's call it Executor that will be the module that uses Alg1 or Alg2 throught their interface..

Reading about functors it seems that I should need a functor that takes an Algorithm and produces a ConcreteExecutor with a specific implementation of the algorithm I need. So that Executor is a sort of module that is parametrized over one of its components..

Am I right? Is it the best way to obtain what I need? I'm wondering thinkgs like these because I come from a Java/C++ background so I'm used to use interfaces and abstract classes and I need to get into this functor/module abstraction issue in the correct way.

Which is the correct syntax to obtain what I want?

Thanks in advance

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

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

发布评论

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

评论(1

倾城月光淡如水﹏ 2024-09-20 05:02:52

是的,听起来函子就是你想要的。事实上,你可以看看标准库是如何使用函子的,因为源代码是可用的。在我的机器上,它位于 /usr/lib/ocaml/3.10.2/ 。例如,set.mli 包含以下内容:

module type OrderedType =
  sig
    type t
    val compare : t -> t -> int
  end

module type S
  sig
    ...
  end

module Make (Ord : OrderedType) : S with type elt = Ord.t

当您想在 OCaml 中使用集合时,您可以这样做:

module SSet = Set.Make(String);;

因此,在您的代码中,Algorithm 替换 OrderedType,Alg1/Alg2 替换 String,Executor 替换 Make,ConcreteExecutor 是 Executor( 的结果)阿尔格1/阿尔格2)。您还会注意到 string.mli/ml 不包含任何有关 OrderedType 的提及。 String 是一个 OrderedType,因为它具有由函数比较使用的类型 t。您不需要明确说明 String 是 OrderedType。

Yup, it sounds like functors are what you want. In fact, you can take a look at how the standard library uses functors as the source code is available. On my machine it's located at /usr/lib/ocaml/3.10.2/. As an example, set.mli contains the following:

module type OrderedType =
  sig
    type t
    val compare : t -> t -> int
  end

module type S
  sig
    ...
  end

module Make (Ord : OrderedType) : S with type elt = Ord.t

When you want to use a set in OCaml you do:

module SSet = Set.Make(String);;

So with your code, Algorithm replaces OrderedType, Alg1/Alg2 replaces String, Executor replaces Make, and ConcreteExecutor is the result of Executor(Alg1/Alg2). You'll also notice that string.mli/ml doesn't contain any mention of OrderedType. String is an OrderedType by virtue of it having a type t that is used by a function compare. You don't need to explicitly say that String is an OrderedType.

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