r/ocaml • • 16d ago

ML Modules for Mere Mortals

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

4 comments sorted by

6

u/kevinclancy_ 16d ago edited 16d ago

Nice! There's a real need for more and better educational material on ML-style module systems.

One important aspect of module systems that often gets overlooked is that a signature implies constraints that OCaml's signature language has no formal way to express. These constraints relate multiple calls to the signature's functions over time, and they're often called *laws* or *behavioral laws*.

Laws are relevant to your set example. We expect a set ADT to contain a value v immediately after we insert v. But we could write a struct (or "mod" in your syntax) that satisfies SetSig and implements contains as a constant function that always returns false. The type system would let us seal that struct with SetSig, but it wouldn't behave as expected.

Laws aren't limited to input/output relations among a signature's functions. They also cover how values of abstract types mediate hidden mutable state. I don't think many programmers (myself included) have the formal know-how to reason about this carefully. Anyone interested should look into the Gospel specification language. It's an imperfect, research-grade tool, but it's also the most comprehensive specification language for OCaml.

Laws get more interesting (and perhaps more problematic) with functors. The SetSig law above relates v across calls to insert and contains, so it needs some notion of identity for values like v. Your implementation uses cmp for identity, but how would we communicate that in SetSig? OCaml can't parameterize signatures by modules, but we might be able to express the dependency with some combination of nested modules, sharing constraints, and comments.

I'm not a fan of the term "functor" either. It was borrowed from category theory, but it doesn't correspond precisely to the category-theoretic definition. Another term that seems to have been smuggled in from category theory is "fibring," which (incorrectly, in my view) gets used as an alternative name for sharing constraints.

In your final paragraph, you say "ML Modules are neat and I think they deserve more love and exploration in programming languages." I think the reason they haven't gotten more love and exploration is that, while they're intuitively powerful, their formal foundations are a mess and they're extremely hard to implement. I believe Simon Peyton-Jones once said that ML-style module systems have a "low power-to-cost ratio." I take him to mean that they're only slightly more powerful than other languages' modularity features, but drastically harder to implement.

I noticed that your parameterized module example doesn't use sharing constraints, but the syntax suggests you might be using parameterized signatures. Are you designing your own module system?

I've actually been writing a set of personal notes about ML style module systems. Maybe I should share them here.

4

u/thunderseethe 16d ago

I am! In my perfect world you wouldn't need sharing constraints and the compiler would track the identity of your abstract types for you to know when they're equal. You can find work towards this across the MixML/Backpack literature. 

Easier said than done of course. I suspect you might still need sharing constraints for generative functors, but I would take them appearing far less often than they do today in fully functorialized modules.

The sig depending on cmp is kind of the great debate in my mind. Do modules need full dep types (types depending on values) or not? Fing Modules says no and it suffices to track the identity of your values with phantom types. I hope to use the same approach to solve the problem 

2

u/kevinclancy_ 12d ago edited 12d ago

I'm not sure if my notes are polished enough that I want to post them as a top-level item on r/ocaml at this point, but you can find them here: https://kevinclancy.github.io/understanding-module-systems/

While I found that F-ing Modules improved my understanding of ML style module systems, it didn't manage to convince me that it's a good approach for guiding an actual implementation. The problem is that the typing relation produces System F-omega types, which don't necessarily resemble what one sees in the surface language. Blaudeau's recent paper "Fulfilling OCaml Modules" tries to handle this by using "anchoring", but he later abandoned it in his ZipML paper.

One problem I have with OCaml's module system in particular is that there are various language features that overlap with modules. For example, records can sometimes be used instead of modules and vice versa. I imagine the same goes for objects, though I don't use them often. Scala might be an improvement over this because its objects serve the role of modules as "owners" of abstract types. 1ML is also relevant here.

I'm at the point where I'm wondering if I should be learning Lean as an alternative to OCaml. While Lean's module system isn't as expressive as OCaml's module system, Lean's structure definitions do seem to subsume OCaml modules in many ways. Lean structures correspond to OCaml signatures, while Lean structure instances correspond to OCaml structs. True abstract types can be represented as fields with type `Type u`, while the sorts of abstract types that are typically substituted using sharing constraints (e.g., a Set signature's `element` type) can be defined using struct parameters. It's more likely that we would want these parameters to be instances of other "module structs" rather than lone types, because they have their own operations and laws associated with them (like how `cmp` is associated with the element type in your example).

2

u/thunderseethe 12d ago

I think there's definitely something to be said for modules culminating in dependent types as a particular mode of use of dependent records. That's a cleaner and simpler world view then the one where your language is stratified into module/core lang as separate things. It also helps with the signature avoidance problem. 

The appeal in modules for me is that the split between module and core lang gives a clean boundary for compiletime/runtime. Modules compile away leaving only the core language at runtime. In that vein, I'm more in favor of the static interpretation over fing modules. Knowing that you can represent the semantics of modules via system fw is very nice, but I agree with your assessment that a practical implementation probably does not want to elaborate to system fw