archives

Turning every module into a functor automatically?

IIRC, I've read about this approach in one of the ReadScheme module papers:

Say we have an ML-like higher-order module system, but every time a module M uses another module U, M automatically becomes a functor, that by default uses U, but could also be parameterized post-hoc to use another module V in place of U:

structure M = 
  let f x = U.do_something x

structure U =
  let do_something x = ...

By default, M would use U's implementation of do_something.

But a client could also supply another implementation (structure), as long as it has the same signature as U:

structure V =
  let do_something x = ...

structure M' = M parameterize U = V

The parameterize statement means that V should be used in place of U inside M'.

I think this would be quite convenient in some cases (e.g. supplying a different string implementation for a module – "monkey patching").

Comments?

Update: Martin Gasbichler's Fully-parameterized, first-class modules with hygienic macros calls this feature full parameterization.