ML Modules for Mere Mortals
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
I think everyone can agree code needs to be modular. Modularity lets us reason locally about our code no matter how big it grows. However, no one seems to agree on how code should be modular. There are a bunch of different approaches and each new language seems to bring their own spin on it.
我想大家都会同意代码需要模块化。无论代码增长到多大,模块化都能让我们在局部范围内对代码进行逻辑推理。然而,似乎没有人能就代码应该如何模块化达成一致。目前存在许多不同的方法,每种新语言似乎都带来了自己独特的见解。
Today we’re going to talk about a pretty old spin: ML Modules. ML here stands for Meta Language, as seen in SML and OCaml (and not machine learning). Despite SML first appearing in 1983, ML Modules have not caught on. OCaml is the sole production language you’ll find with an ML module system, and at the height of its popularity it’s niche.
今天我们要讨论的是一种相当古老的见解:ML 模块。这里的 ML 代表元语言(Meta Language),正如在 SML 和 OCaml 中所见(并非机器学习)。尽管 SML 最早出现在 1983 年,但 ML 模块并没有流行起来。OCaml 是你唯一能找到的带有 ML 模块系统的生产级语言,即便在其最受欢迎的时候,它也只是一个小众选择。
This is a shame because ML modules have a lot to offer. We’re going to talk about what ML modules are and then touch a little on why ML modules are. Who better after all to explain modules, than the guy who doesn’t know what’s in a module ?…On second thought, don’t answer that.…I already wrote the blog post.
这很遗憾,因为 ML 模块有很多优点。我们将讨论什么是 ML 模块,然后简单谈谈 ML 模块存在的意义。毕竟,还有谁比一个连模块里有什么都不知道的人更适合解释模块呢?……再想一想,别回答这个问题……反正我已经写好这篇博客了。
Modules exist to make our code modular, duh, but what does that mean in practice? I think most programmers have some intuitive sense of modularity: Bundle up related logic as one “thing”, Encapsulate invariants, Decouple from dependencies. Most languages have a way to do this, and if they don’t, one gets created for them (looking at you JavaScript).
模块的存在是为了让代码模块化,废话,但在实践中这意味着什么呢?我认为大多数程序员对模块化都有一些直觉上的理解:将相关逻辑捆绑为一个“事物”、封装不变量、与依赖项解耦。大多数语言都有实现这一点的方法,如果没有,人们也会为它们创造出来(说的就是你,JavaScript)。
ML modules achieves this with, you guessed it, the Module. ML modules, however, are a little different from the modules we know and love. To explain their differences, we’ll lean on a made-up Rust-inspired syntax. It will start off looking a little weird and gradually, as we introduce concepts, end up looking really weird (but hopefully more familiar than SML syntax). We’ll start by defining a module with just functions:
ML 模块通过你猜到的方式——“模块”来实现这一点。然而,ML 模块与我们熟悉和喜爱的模块略有不同。为了解释它们的区别,我们将借鉴一种虚构的、受 Rust 启发的语法。它起初看起来有点奇怪,随着我们引入概念,它会逐渐变得非常奇怪(但希望比 SML 语法更易懂)。我们先从定义一个仅包含函数的模块开始:
mod AModule {
fn add(a: i32, b: i32) -> i32 { a + b }
fn sub(a: i32, b: i32) -> i32 { a - b }
}
AModule bundles together related arithmetic functions as one thing we can reason about. Each function can see every other function in the module. This is useful, but any language with namespaces offers this.
AModule 将相关的算术函数捆绑在一起,作为一个我们可以进行推理的整体。每个函数都能看到模块中的其他所有函数。这很有用,但任何带有命名空间的语言都能做到这一点。
A little more unique is that ML modules will let us put not only function definitions in modules, but also type definitions. Here we have a Set module with a Set type, which is a sorted Vec
更独特的一点是,ML 模块不仅允许我们在模块中放入函数定义,还允许放入类型定义。这里我们有一个 Set 模块,其中包含一个 Set 类型,它是一个排序的 Vec
Not only can modules contain type definitions. Modules themselves have types. A module type is called a signature (so we don’t get it confused with “normal” types). The signature of our Set module is:
模块不仅可以包含类型定义,模块本身也有类型。模块类型被称为“签名”(signature),这样我们就不会把它与“普通”类型混淆。我们 Set 模块的签名如下:
sig FullSetSig {
type Set = Vec<i32>;
fn new() -> Set;
fn index_of(&Set, &i32) -> Result<usize, usize>;
fn insert(Set, i32) -> Set;
fn contains(&Set, &i32) -> bool;
fn remove(&Set, &i32) -> Set
}
A module signature declares every definition of a module alongside its type. This might look similar to the way we define a trait in Rust. That’s because much like traits act as interfaces in Rust, signatures act as interfaces in ML Modules. Modules having signatures is the single most important thing to understand about ML Modules. All the complexity and expressiveness of modules results from them having signatures and the wonderful wacky things that enables.
模块签名声明了模块的每一个定义及其类型。这看起来可能类似于我们在 Rust 中定义 trait 的方式。这是因为就像 trait 在 Rust 中充当接口一样,签名在 ML 模块中也充当接口。模块拥有签名是理解 ML 模块最重要的一点。模块的所有复杂性和表达能力都源于它们拥有签名,以及由此带来的奇妙而古怪的功能。
Signatures resemble the way a C/C++ implementation file conforms to its header. Modules, unlike .cc files, aren’t in a 1:1 relationship with signatures. Our Set module can conform to any signature that it matches. A module matches a signature when it defines every declaration in the signature and they agree on type.
签名类似于 C/C++ 实现文件遵循其头文件的方式。与 .cc 文件不同,模块与签名之间并非一一对应。我们的 Set 模块可以遵循它所匹配的任何签名。当一个模块定义了签名中的每一个声明,并且类型一致时,该模块就与签名匹配。
Matching a signature provides Encapsulation, hiding internal details of our module from our public API. Take a look at FullSetSig. Do callers of Set really need to know about index_of? Encapsulation advocates shout a resounding no: index_of is an implementation detail of Set. Set being a Vec
匹配签名提供了封装,向公共 API 隐藏了模块的内部细节。看看 FullSetSig。Set 的调用者真的需要知道 index_of 吗?封装倡导者会大声说不:index_of 是 Set 的实现细节。Set 是一个 Vec
We can encapsulate index_of by matching our Set module to a new signature. SetSig removes index_of, hiding the implementation detail. We can then ascribe our signature to our Set module. Now the rest of our code sees Set as SetSig without index_of. Importantly, our Set module hasn’t changed. index_of is still available inside the module. Ascribing the signature only changes the public view of the module, similar to a private function in Rust.
我们可以通过将 Set 模块匹配到一个新签名来封装 index_of。SetSig 移除了 index_of,从而隐藏了实现细节。然后我们可以将该签名赋予我们的 Set 模块。现在,代码的其他部分将 Set 视为没有 index_of 的 SetSig。重要的是,我们的 Set 模块本身并没有改变。index_of 在模块内部仍然可用。赋予签名只会改变模块的公共视图,类似于 Rust 中的私有函数。
A module’s definition isn’t the only place we can ascribe a signature. We can also make a “new” module by matching an old module to a new signature. Let’s say we want an InsertOnlySet. We can create that from our Set module purely by matching the signature: mod InsertOnlySet: InsertOnlySetSig = Set;. Now InsertOnlySet is effectively a whole new module, despite the underlying implementation being shared with Set. This is what’s cool about modules. We can repurpose modules “for free” simply by giving them a d…
模块定义并不是我们唯一可以赋予签名的地方。我们还可以通过将旧模块匹配到新签名来创建一个“新”模块。假设我们需要一个 InsertOnlySet。我们可以完全通过匹配签名从 Set 模块中创建它:mod InsertOnlySet: InsertOnlySetSig = Set;。现在,InsertOnlySet 实际上是一个全新的模块,尽管其底层实现与 Set 共享。这就是模块的酷炫之处。我们可以通过简单地赋予它们一个 d… 来“免费”地重用模块。