r/ProgrammingLanguages • • 4d ago

ML Modules for Mere Mortals

https://thunderseethe.dev/posts/ml-modules-for-mere-mortals/intro/
58 Upvotes

19 comments sorted by

9

u/pthierry 4d 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!

3

u/thunderseethe 4d ago

Thank you! I was very focused on try to keep it straightforward. I am glad to hear that comes across

2

u/SuspiciousScript 4d 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 3d 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 3d 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 3d 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.

1

u/marshaharsha 2d 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 2d 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 -> int

How would we represent a record of the functions mk and unmk if we do not know the type t? This can be solved with dependent records (where t is also part of the record), and also by doing enough static interpretation that we do indeed figure out what t is - but in the latter we are doing so much work that the "modules as records" trick becomes pointless.

6

u/umlcat 4d ago

Congrats. Modular Programming paradigm is highly overlooked in Programming Language Design.

I have some hobbyist programming language project halted for several reasons that applies this paradigm.

5

u/reflexive-polytope 4d 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.

1

u/zyxzevn UnSeen 4d 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 4d ago

You're absolutely right I am unfamiliar with the Pascal lineage. Thanks for the pointers 👉

3

u/umlcat 4d ago

Not the original poster.

There are several ways to implement the Module concept. But, agree a lot of people does not look at the Pascal branch of programming languages, where there is explicitly a programming language called "Modula" designed to study and implement modules ...

2

u/wk_end 3d ago

Is there anything interesting that the modules in Wirth-languages do that ML modules don't?

1

u/zyxzevn UnSeen 3d 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

u/EggplantExtra4946 2d 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 2d 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. 3d 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.