Re: higher-order functors

Vesa Karvonen <vesa.karvonen-bbCR+/[email protected]> Tue, 23 Jan 2007 10:51:58 +0200
Newsgroups gmane.comp.lang.sml.smlnj
Message-ID <[email protected]>
Quoting Dave Herman <[email protected]>:
> I found the need for a higher-order functor like the following:
> 
>      functor Generator(functor Coroutine(type result) : COROUTINE)
>                    : GENERATOR =
>      struct
>          datatype signal = ...
>          structure C = Coroutine(type result = signal)
>          ...
>      end
[...]
> What's a good workaround for this kind of use case? I'd like the 
> Coroutine module to be abstract in the type of values passed between 
> coroutines, and I'd like the Generator module to be abstract in the 
> implementation of Coroutines.

To avoid higher-order functors, couldn't you just move the signal type
outside of the Generator?  It would look roughly like this:

  functor Coroutine (type result) :>
     COROUTINE where type result = result = struct
     ...
  end

  structure GeneratorSignal = struct
     datatype t = ...
  end

  functor Generator (Coroutine : COROUTINE
                        where type result = GeneratorSignal.t) :>
     GENERATOR = struct
     type signal = GeneratorSignal.t
     structure Coroutine = Coroutine
     ...
  end

Instantiation of the Generator would look roughly like this:

  structure G = Generator (Coroutine (type result = GeneratorSignal.t))

Note that, despite appearances, this scheme does not actually force you
to reveal that GeneratorSignal.t = G.signal = Coroutine.result.  What
I mean is that, if you want to export the result and/or the signal type
from Generator, you can do so using opaque signature ascription (as used
above), to hide the connection to GeneratorSignal.t.  Specifically, given
the above code and, for example,

  signature GENERATOR = sig
     type signal
     structure Coroutine : COROUTINE
     ...
  end

then, without further explicit constraints, all of the types

  G.signal,
  G.Coroutine.result, and
  GeneratorSignal.t

would be distinct.

-Vesa Karvonen

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV