[Alice-users] Re: Functors that return environments with Functors
Andreas Rossberg
rossberg at ps.uni-sb.de
Mon Jun 27 12:50:33 CEST 2005
Hi Chris,
> So, am I just trying to abuse the notion of Functors?
No, not at all. For the first problem, you have just stumbled over a
peculiarity of SML's functor syntax. Let me explain.
The basic syntax for functor definitions is
functor F (X : sigexp) = modexp
Now, for convenience, there exists syntactic sugar that allows you to write
functor F (specs) = modexp
where `specs' is a list of specifications like they occur inside a
signature. It desugars into
functor F (X : sig specs end) = let open X in modexp end
where X is an identifier invented by the compiler. This sugar is pretty
convenient, as it allows you to write
functor F (type t val x : t) = ...
functor G () = ... (* empty signature! *)
and so on. Analogous sugar exists for functor application, where
F (decs)
actually means
F (struct decs end)
Now, what happens in your example? You wrote:
> functor Dummy1 (structure BType : T)
which actually means
functor Dummy1 (X : sig structure BType : T end)
and you tried to apply it like
> Dummy1(CType)
But your CType module has signature T, not signature `sig structure
BType : T end'! Hence the compiler complains that the structure BType is
missing in it.
There are two ways to bring this in line: either simplify the definition
of Dummy1 to
functor Dummy1 (BType : T)
or change the application to
Dummy1 (structure BType = CType)
whatever suites your taste more. The latter style basically provides
keyword arguments for functors, but you have to use them consistently.
> Well, having come to a dead end on passing around modules, I stripped down
> to the bone the feature that I was really want to get to - self referring
> functors. But it seems that Functors can't be recursive called (if that's
> the proper term for the effect that I'm trying to do here.
Right, ML does not currently allow recursive modules. They are a hot
research topic, but there are unsolved semantic issues. To get an idea,
just consider that recursive modules basically can make any language
entity recursive through the back door. It is not obvious how to
generally preclude malformed examples like
structure rec A = (val x = B.y)
and B = (val y = A.x)
or
structure rec A = (type t = B.u)
and B = (type u = A.t)
In the presence of higher-order functors, this can be difficult to
discover - you needed to somehow track dependency information in module
signatures. All approaches proposed so far in literature are either very
restrictive, or horribly complicated, or both.
> So what I really wanted to do was have a Functor that is passed a module as
> in the second example, and have it return another functor that modifies that
> environment as a module parameter to itself. Kind of a Declarative Functor
> if you will.
I'm not entirely sure I understand what you are trying to do, but it
sounds easy enough:
functor F (X : ENV) = let functor G () = ...X... in G end
or simpler, using currying :-) :
functor F (X : ENV) () = ...X...
If you really need the "...X..." part to invoke F recursively then I'm
afraid that you cannot do that directly. But in Alice ML, you can
actually duck the the lack of functor recursion by going through dynamic
typing with packages! Here is an example of a simple (non-terminating)
recursive functor F:
signature F' = (val self : package) -> T -> T
functor F' (val self : package) (X : T) = (* auxiliary *)
(unpack self : F') (val self = self) X
structure F = F' (val self = pack F' : F')
Note that F is a functor with signature T->T. You may discover the echo
of a fixpoint combinator in the above code... :-)
Cheers,
- Andreas
--
Andreas Rossberg, rossberg at ps.uni-sb.de
Let's get rid of those possible thingies! -- TB
More information about the alice-users
mailing list