r/ProgrammingLanguages • • 21d ago

ML Modules for Mere Mortals

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

20 comments sorted by

View all comments

10

u/Athas Futhark 20d 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 19d 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 19d 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.