MLton’s implementation(s) of the MLNLFFI library differs from the SML/NJ implementation in two important ways:
- 
MLton cannot utilize the Unsafe.cast"cheat" described in Section 3.7 of Blume01. (MLton’s representation of closures and aggressive representation optimizations make anUnsafe.casteven more "unsafe" than in other implementations.)We have considered two solutions: - 
One solution is to utilize an additional type parameter (as described in Section 3.7 of Blume01): signature C = sig type ('t, 'f, 'c) obj eqtype ('t, 'f, 'c) obj' ... type ('o, 'f) ptr eqtype ('o, 'f) ptr' ... type 'f fptr type 'f ptr' ... structure T : sig type ('t, 'f) typ ... end endThe rule for ('t, 'f, 'c) obj,('t, 'f, 'c) ptr, and also('t, 'f) T.typis that wheneverF fptroccurs within the instantiation of't, then'fmust be instantiated toF. In all other cases,'fwill be instantiated tounit.(In the actual MLton implementation, an abstract type naf(not-a-function) is used instead ofunit.)While this means that type-annotated programs may not type-check under both the SML/NJ implementation and the MLton implementation, this should not be a problem in practice. Tools, like ml-nlffigen, which are necessarily implementation dependent (in order to make calls through a C function pointer), may be easily extended to emit the additional type parameter. Client code which uses such generated glue-code (e.g., Section 1 of Blume01) need rarely write type-annotations, thanks to the magic of type inference.
- 
The above implementation suffers from two disadvantages. First, it changes the MLNLFFI Library interface, meaning that the same program may not type-check under both the SML/NJ implementation and the MLton implementation (though, in light of type inference and the richer MLRepstructure provided by MLton, this point is mostly moot).Second, it appears to unnecessarily duplicate type information. For example, an external C variable of type int (* f[3])(int)(that is, an array of three function pointers), would be represented by the SML type(((sint -> sint) fptr, dec dg3) arr, sint -> sint, rw) obj. One might well ask why the'finstantiation (sint -> sintin this case) cannot be extracted from the'tinstantiation (((sint -> sint) fptr, dec dg3) arrin this case), obviating the need for a separate function-type type argument. There are a number of components to an complete answer to this question. Foremost is the fact that Standard ML supports neither (general) type-level functions nor intensional polymorphism.A more direct answer for MLNLFFI is that in the SML/NJ implemention, the definition of the types ('t, 'c) objand('t, 'c) ptrare made in such a way that the type variables'tand'care phantom (not contributing to the run-time representation of an('t, 'c) objor('t, 'c) ptrvalue), despite the fact that the types((sint -> sint) fptr, rw) ptrand((double -> double) fptr, rw) ptrnecessarily carry distinct (and type incompatible) run-time (C-)type information (RTTI), corresponding to the different calling conventions of the two C functions. TheUnsafe.cast"cheat" overcomes the type incompatibility without introducing a new type variable (as in the first solution above).Hence, the reason that function-type type cannot be extracted from the 'ttype variable instantiation is that the type of the representation of RTTI doesn’t even see the (phantom)'ttype variable. The solution which presents itself is to give up on the phantomness of the'ttype variable, making it available to the representation of RTTI.This is not without some small drawbacks. Because many of the types used to instantiate 'tcarry more structure than is strictly necessary for't’s RTTI, it is sometimes necessary to wrap and unwrap RTTI to accommodate the additional structure. (In the other implementations, the corresponding operations can pass along the RTTI unchanged.) However, these coercions contribute minuscule overhead; in fact, in a majority of cases, MLton’s optimizations will completely eliminate the RTTI from the final program.
 The implementation distributed with MLton uses the second solution. Bonus question: Why can’t one use a universal type to eliminate the use of Unsafe.cast?- 
Answer: ??? 
 
- 
- 
MLton (in both of the above implementations) provides a richer MLRepstructure, utilizingInt<N>andWord<N>structures.structure MLRep = struct structure Char = struct structure Signed = Int8 structure Unsigned = Word8 (* word-style bit-operations on integers... *) structure <<SignedBitops#>> = IntBitOps(structure I = Signed structure W = Unsigned) end structure Short = struct structure Signed = Int16 structure Unsigned = Word16 (* word-style bit-operations on integers... *) structure <<SignedBitops#>> = IntBitOps(structure I = Signed structure W = Unsigned) end structure Int = struct structure Signed = Int32 structure Unsigned = Word32 (* word-style bit-operations on integers... *) structure <<SignedBitops#>> = IntBitOps(structure I = Signed structure W = Unsigned) end structure Long = struct structure Signed = Int32 structure Unsigned = Word32 (* word-style bit-operations on integers... *) structure <<SignedBitops#>> = IntBitOps(structure I = Signed structure W = Unsigned) end structure <<LongLong#>> = struct structure Signed = Int64 structure Unsigned = Word64 (* word-style bit-operations on integers... *) structure <<SignedBitops#>> = IntBitOps(structure I = Signed structure W = Unsigned) end structure Float = Real32 structure Double = Real64 endThis would appear to be a better interface, even when an implementation must choose Int32andWord32as the representation for smaller C-types.