[MLton] Structures inside a function?

Matthew Fluet fluet@cs.cornell.edu
Wed, 19 Jan 2005 20:22:57 -0500 (EST)

> Hi! I recently ran into a problem where I needed to use a structure inside a
> function. However, this doesn't appear to be possible; what do I do instead?

Generally, I think you need to refactor things.  

Perhaps you can make ResidueWithoutModulus and MathWithoutModulus functors 
that parameterize each function requiring the modulus with the explicit 
modulus argument.

You can then make Residue and Math essentially special cases of the above, 
where you have the modulus up front and can simplify the exported 
functions to not have the modulus argument.

Then you could write:

functor Factor(E : EUCLIDEAN_DOMAIN) =
    structure R = ResidueWithoutModulus(struct structure Base = E)
    structure M = MathWithoutModulus(R)

    fun factor x =
	open M
        val f1 = f1 x
        val f2 = f2 x
        (* use various methods defined in Math to factor x *)

where each of the fi functions are from the MathWithoutModulus functor.

> I have run into this in several other places, but have always avoided
> the problem. Here, I don't see any choice other than to redefine all the
> methods I need from Math (lots) in local scope, which really sucks.

Hopefully the refactoring above will require you just to seed each of the 
MathWithoutModulus functions with the modulus.