r/ProgrammingLanguages • u/thunderseethe • 8d ago
ML Modules for Mere Mortals
https://thunderseethe.dev/posts/ml-modules-for-mere-mortals/intro/2
u/SuspiciousScript 8d ago
Very interesting. The ability to pass different implementations of a signature to a module (e.g. custom orderings) reminds me a bit of Scala's implicits.
5
u/thunderseethe 7d ago
Theres good reason for that! Take a look at https://www.cl.cam.ac.uk/~jdy22/papers/modular-implicits.pdf they describe a very similar system to scala implicits but for modules. It's a common ask to be able to implicitly specify what you mean in a scope (traits are also a flavor of this ofc)
1
u/reflexive-polytope 8d ago
Does Scala let you say that “set of integers ordered ascendingly” and “set of integers ordered descendingly” are different types?
9
u/Athas Futhark 7d ago
I am a big fan of modules! Here are some things that expand upon the article:
The module syntax and nomenclature in Standard ML (and to a degree in OCaml) is not so good, and the superficial ugliness hinders understanding. Even just using curly braces a bit as in the post helps. I also think OCaml's term "module type" instead of the very generic "signature" is superior, and likewise with "parameterised module" instead of "functor" (even though it is a mouthful, "functor" means too many different things).
One nice thing about modules is that they are easy to compile in various ways. Their semantics are entirely given by name substitution, and you can also just turn every module into a record of values of functions (although this will be untyped unless you have fancy dependent records). The ability to fully statically evaluate modules means they are a guaranteed "zero-cost abstraction" (if implemented that way).
ML modules tend to be somewhat verbose in practice, even in the best designs. I don't mind that so much, but compared to related features like type classes, I think they are best for handling complicated cases, and are a little clumsy when you want something basic like writing a single function that is polymorphic in some number type.
2
u/marshaharsha 6d ago
Can you explain why a “record of values of functions” would have to be untyped? I would have thought that having record types and function types would be enough. Do you just mean that the abstract-type components of a module (often written “type t”) can’t be typed, or is even a module consisting solely of values untypable?
I mean in the absence of dependent types.
2
u/Athas Futhark 6d ago
Do you just mean that the abstract-type components of a module (often written “type t”) can’t be typed
Yes exactly, this is the reason, but that also affects any module components of type
t. Assume we have a module with this signature:type t type mk : int -> t type unmk : t -> intHow would we represent a record of the functions
mkandunmkif we do not know the typet? This can be solved with dependent records (wheretis also part of the record), and also by doing enough static interpretation that we do indeed figure out whattis - but in the latter we are doing so much work that the "modules as records" trick becomes pointless.
5
u/reflexive-polytope 8d ago
I can't imagine programming without ML modules anymore.
Or rather, I can, and it's precisely because I can that I'm disgusted by the mere thought.
2
u/zyxzevn UnSeen 8d ago
Since you write about modules, have a look at Object Pascal. Lazarus and Delphi.
... If we wanted to replicate this setup in an OO language..
I think that you are referring to OO-languages that do not have modules, but use classes like modules, like Java.
2
u/thunderseethe 8d ago
You're absolutely right I am unfamiliar with the Pascal lineage. Thanks for the pointers 👉
3
2
u/wk_end 7d ago
Is there anything interesting that the modules in Wirth-languages do that ML modules don't?
1
u/zyxzevn UnSeen 7d ago
Wirth?
Anyway, the ObjectPascal modules usually compile to code. So you get very fast compilation times in return. FreePascal calls itself the fastest compiler.
The modules have a separate interface and implementation. This means that you can define types or functions in the implementation that are stay completely local. You can also mix modules, as long each type is complete in the implementation. Each module also has its own initialization section. This can help with memory management.
2
2
u/EggplantExtra4946 6d ago
It's like abstract classes with type parameters (ML module instances can define types) but with static calls instead of virtual calls.
2
u/snugar_i 6d ago
Thank you for the article! I don't know anything about ML and I have to admit that even after reading the article, I'm not really sure how this is much different or better than regular OO interfaces. You say "If we wanted to replicate this setup in an OO language, however, we would need runtime objects or, heaven forbid, a DI framework" and yeah, we would likely need runtime objects (unless it's a language with monomorphized generics, then they might disappear), but why is that a bad thing? And we sure don't need a DI framework to do DI, which is basically what you're doing with the modules in the last example?
1
u/Key_River7180 Nain. 8d ago
Again the module system is very overlooked. Take as an example Modula-2, it's much more interesting than "cool modules" yet the name implies so.
I like using a definition/source, like the C and header split but doesn't suck. Or like you know... hierarchical modules and export, that also works.
1
u/oscarryz Yz 3d ago
When I was looking for a way to add encapsulation to my language without having to add an special keyword I "discovered" that function signatures were the way to go. Later I learned about ML Modules which validate my mental model and I followed them to refine my design.
In my language there is only one construct, a block of code, which are in essence closures and thus, acts as objects too ( https://wiki.c2.com/?ClosuresAndObjectsAreEquivalent ).
The signature of these closures uses the #(...) syntax, so. The signature of an `add` function/closure, that takes two params `a`, and `b` of type `Int`, and returns an `Int` would be
add #(a Int, b Int, Int) The implementation body could have many more variables, but only these three types are exposed.
Because these are closures, they can have functions/closures themselves, which gives me _module_ like behavior.
The first example in the article could be like this:
set_module #(
Set Vec<Int>, // upper case means "class" or "instantable"
new #(Set),
insert #(Set, Int, Set),
contains #(Set, Int, Bool),
remove #(Set, Int, Set)
)I really like this combination of simplicity and functionality.
10
u/pthierry 8d ago
Very clear explanation, I've tried to understand Ocaml's modules once or twice and found the explanations… unsatisfying or puzzling, I guess. Now I can go back to the source with this knowledge, thanks!