by jez · 9y ago · 0 comments
It's really hard to do justice explaining how amazing modules are. They capture the essence of abstraction incredibly well, giving you plenty of expressive power (alongside an equally powerful type system). Importantly, they compose; you can write functions from modules to modules!
(This is even more impressive than you think: modules have runtime (dynamic) AND compile time (static) components. You've certainly written functions on runtime values before, and you may have even written functions on static types before. But have you written one function that operates on both a static and a dynamic thing at the same time? And what kind of power does this give you? Basically, creating abstractions is effortless.)
To learn more, I recommend you read Danny Gratzer's "A Crash Course on ML Modules"[1]. It's a good jumping off point. From there, try your hand at learning SML or Ocaml and tinker. ML modules are great!
[1]: https://jozefg.bitbucket.io/posts/2015-01-08-modules.html
So, if anything, what I'd like to see is a dependently typed language whose type language is a first-order, computation-free (no non-constructor function application) subset of the value language.
"We show how Damas/Milner-style type inference can be integrated into such a language; it is incomplete, but only in ways that are already present in existing ML implementations."
Furthermore, OCaml essentially already has first-class modules - just the syntax is much more awkward than is possible with 1ML.
In ML, you can have multiple modules (structures) conforming to a common interface (signature), but their abstract type components are in general not identifiable - with good reason. For instance, let's say you have an API for heaps:
(* For algorithmic reasons, it is useful to distinguish between
* heaps whose underlying data structure is a tree (e.g. pairing
* heaps) and heaps whose underlying data structure is a forest
* (e.g. binomial heaps). I will only use the former in this
* example. *)
signature TREE_HEAP :
sig
type key (* abstract *)
type 'a entry = key * 'a
type 'a heap (* again abstract *)
val pure : 'a entry -> 'a heap
val head : 'a heap -> 'a entry
val tail : 'a heap -> 'a heap option
val merge : 'a heap * 'a heap -> 'a heap
end
Of course, there are multiple ways to implement this API. For example, pairing heaps and Brodal-Okasaki's asymptotically optimal heaps: signature ORD_KEY :
sig
type key
val <= : key * key -> bool
end
functor PairingHeap (K : ORD_KEY) : TREE_HEAP =
struct
(* blah blah blah *)
end
functor BrodalOkasakiHeap (K : ORD_KEY) : TREE_HEAP =
struct
(* blah blah blah *)
end
structure H1 = PairingHeap (IntKey)
structure H2 = BrodalOkasakiHeap (IntKey)
However, `H1.heap` and `H2.heap` are two different abstract types. That is, for example, you can't attempt to merge a pairing heap with a Brodal-Okasaki heap - that would be a (static!) type error.But, can you use multiple implementations of the TREE_HEAP in the same code? (I think so, but it's been a long while.) Can you use two versions of BrodalOkasakiHeap in the same code? (Probably not.) Can module A use one version of BrodalOkasakiHeap while module B uses another, in the same program?
Yes.
> Can you use two versions of BrodalOkasakiHeap in the same code?
ML doesn't have a notion of “version”, but of course you can provide two different implementations of the same API if you so desire. However, these implementations, while interchangeable (a client can swap either implementation with the other, as long as this is done consistently), are not compatible (a client can't treat both implementations as if they were the same).