Yue Li wrote:
functor myfctor(s: A) :> B = struct type element = s.element end
From above, I understand that functor is compiled to a lambda
expression. But I don't quite see what the body of the lambda expression does.
functor myfctor(s: A) :> B = struct type element = s.element type set = element list end
Here why there are two more functions "size-list" and "boxed-list" appended to the end of code tree?
Functors are implemented as functions, actually normally inline functions. I think, though, that looking into the details of what's going on here is going to be distracting and probably not relevant to what you're trying to do. It's all to do with the implementation of type identifiers at run-time. At one time there was no run-time data associated with a type but from version 5.3 types have print and equality functions that have to be passed into and out of functors. In 5.4 some extra functions, "box" and "size" were added. These functions are created when a type is declared and packaged up into a tuple.
It's complicated in your example because you have an opaque sharing constraint on the result signature. That means that a distinct "print reference" has to be created so that if necessary a pretty printer can be installed on the result type without affecting the implementation type. The code: POLY_SYS_alloc_store G $(LIT1 G, LIT40 G, POLY_SYS_load_word G $(INDIRECT(1, LOCAL(0,2)) G, LIT0 G) G ) creates a new reference using the current value of a reference as its initial value. It's essentially "val r = ref(!x)". You can think of your first functor as roughly equivalent to val funct = fn (eq, pr, box, size) => (eq, ref(!pr), box, size) so that the print reference of the result type is initialised to the currently installed print function for the implementing type but the equality, size and box functions of the implementing type are inherited directly.
You'd probably find it easier to look at the core language rather than the modules language but even there you may find things that are confusing. For example, functions that take tuples or are curried are generated as pairs of mutually recursive functions.
Regards, David