ML Modules for Mere Mortals: Signatures and Encapsulation
This personal blog post by thunderseethe explains ML modules[1], a module system used in languages such as SML and OCaml. The author notes that ML stands for Meta Language, not machine learning. SML first appeared in 1983, and the author says OCaml is the only production language with an ML module system. The examples use an invented syntax inspired by Rust.
A module can bundle functions and types together. A module's type is called a signature[2], which lists each definition in the module with its type, and a module matches a signature when it defines every listed item with matching types. Leaving index_of out of a signature hides it from callers while the module itself stays unchanged. The same module can also be given a different signature to make a new module, such as InsertOnlySet.
Declaring the type Set as an abstract type[3] hides that it is a Vec<i32>, so outside code cannot break the sorted order. Callers can only pass the value to the module's functions. The Set module still works only for i32, and the next step the author takes is generics.
Read the original thunderseethe.devDiscussion
Discussion
Commenters point to real uses of the idea: the Fox project's networking stack in SML in the mid-1990s, and OCaml modules in MirageOS and Docker's VPNKit. One commenter compares signatures to classes with structural typing, and another describes a similar Python attempt that had no enforced type safety and disrupted IDEs. A third simply says they wish they could work in OCaml or F#.
Links from the post and discussion
- FoxNet paper: SML modules in a networking stack cs.cmu.edu
- MirageOS blog on its OCaml network stack mirageos.org
Sources
-
Thunderseethe's Devlog
ML Modules for Mere Mortals
OriginalDiscussion · 12Discussion · 5DiscussionDiscussionDiscussion
-
KRGeekNews KO
누구나 이해할 수 있는 ML 모듈


