| 1 |
= MLNLFFIImplementation |
| 2 |
|
| 3 |
MLton's implementation(s) of the <<MLNLFFI#>> library differs from the |
| 4 |
SML/NJ implementation in two important ways: |
| 5 |
|
| 6 |
* MLton cannot utilize the `Unsafe.cast` "cheat" described in Section |
| 7 |
3.7 of <<References#Blume01,Blume01>>. (MLton's representation of |
| 8 |
<<Closure#,closures>> and |
| 9 |
<<PackedRepresentation#,aggressive representation>> optimizations make |
| 10 |
an `Unsafe.cast` even more "unsafe" than in other implementations.) |
| 11 |
+ |
| 12 |
-- |
| 13 |
We have considered two solutions: |
| 14 |
|
| 15 |
** One solution is to utilize an additional type parameter (as |
| 16 |
described in Section 3.7 of <<References#Blume01,Blume01>>): |
| 17 |
+ |
| 18 |
__________ |
| 19 |
[source,sml] |
| 20 |
---- |
| 21 |
signature C = sig |
| 22 |
type ('t, 'f, 'c) obj |
| 23 |
eqtype ('t, 'f, 'c) obj' |
| 24 |
... |
| 25 |
type ('o, 'f) ptr |
| 26 |
eqtype ('o, 'f) ptr' |
| 27 |
... |
| 28 |
type 'f fptr |
| 29 |
type 'f ptr' |
| 30 |
... |
| 31 |
structure T : sig |
| 32 |
type ('t, 'f) typ |
| 33 |
... |
| 34 |
end |
| 35 |
end |
| 36 |
---- |
| 37 |
|
| 38 |
The rule for `('t, 'f, 'c) obj`,`('t, 'f, 'c) ptr`, and also `('t, 'f) |
| 39 |
T.typ` is that whenever `F fptr` occurs within the instantiation of |
| 40 |
`'t`, then `'f` must be instantiated to `F`. In all other cases, `'f` |
| 41 |
will be instantiated to `unit`. |
| 42 |
__________ |
| 43 |
+ |
| 44 |
(In the actual MLton implementation, an abstract type `naf` |
| 45 |
(not-a-function) is used instead of `unit`.) |
| 46 |
+ |
| 47 |
While this means that type-annotated programs may not type-check under |
| 48 |
both the SML/NJ implementation and the MLton implementation, this |
| 49 |
should not be a problem in practice. Tools, like `ml-nlffigen`, which |
| 50 |
are necessarily implementation dependent (in order to make |
| 51 |
<<CallingFromSMLToCFunctionPointer#,calls through a C function |
| 52 |
pointer>>), may be easily extended to emit the additional type |
| 53 |
parameter. Client code which uses such generated glue-code (e.g., |
| 54 |
Section 1 of <<References#Blume01,Blume01>>) need rarely write type-annotations, |
| 55 |
thanks to the magic of type inference. |
| 56 |
|
| 57 |
** The above implementation suffers from two disadvantages. |
| 58 |
+ |
| 59 |
First, it changes the MLNLFFI Library interface, meaning that the same |
| 60 |
program may not type-check under both the SML/NJ implementation and |
| 61 |
the MLton implementation (though, in light of type inference and the |
| 62 |
richer `MLRep` structure provided by MLton, this point is mostly |
| 63 |
moot). |
| 64 |
+ |
| 65 |
Second, it appears to unnecessarily duplicate type information. For |
| 66 |
example, an external C variable of type `int (* f[3])(int)` (that is, |
| 67 |
an array of three function pointers), would be represented by the SML |
| 68 |
type `(((sint \-> sint) fptr, dec dg3) arr, sint \-> sint, rw) obj`. |
| 69 |
One might well ask why the `'f` instantiation (`sint \-> sint` in this |
| 70 |
case) cannot be _extracted_ from the `'t` instantiation |
| 71 |
(`((sint \-> sint) fptr, dec dg3) arr` in this case), obviating the |
| 72 |
need for a separate _function-type_ type argument. There are a number |
| 73 |
of components to an complete answer to this question. Foremost is the |
| 74 |
fact that <<StandardML#,Standard ML>> supports neither (general) |
| 75 |
type-level functions nor intensional polymorphism. |
| 76 |
+ |
| 77 |
A more direct answer for MLNLFFI is that in the SML/NJ implemention, |
| 78 |
the definition of the types `('t, 'c) obj` and `('t, 'c) ptr` are made |
| 79 |
in such a way that the type variables `'t` and `'c` are <<PhantomType#, |
| 80 |
phantom>> (not contributing to the run-time representation of an |
| 81 |
`('t, 'c) obj` or `('t, 'c) ptr` value), despite the fact that the |
| 82 |
types `((sint \-> sint) fptr, rw) ptr` and |
| 83 |
`((double \-> double) fptr, rw) ptr` necessarily carry distinct (and |
| 84 |
type incompatible) run-time (C-)type information (RTTI), corresponding |
| 85 |
to the different calling conventions of the two C functions. The |
| 86 |
`Unsafe.cast` "cheat" overcomes the type incompatibility without |
| 87 |
introducing a new type variable (as in the first solution above). |
| 88 |
+ |
| 89 |
Hence, the reason that _function-type_ type cannot be extracted from |
| 90 |
the `'t` type variable instantiation is that the type of the |
| 91 |
representation of RTTI doesn't even _see_ the (phantom) `'t` type |
| 92 |
variable. The solution which presents itself is to give up on the |
| 93 |
phantomness of the `'t` type variable, making it available to the |
| 94 |
representation of RTTI. |
| 95 |
+ |
| 96 |
This is not without some small drawbacks. Because many of the types |
| 97 |
used to instantiate `'t` carry more structure than is strictly |
| 98 |
necessary for `'t`’s RTTI, it is sometimes necessary to wrap and |
| 99 |
unwrap RTTI to accommodate the additional structure. (In the other |
| 100 |
implementations, the corresponding operations can pass along the RTTI |
| 101 |
unchanged.) However, these coercions contribute minuscule overhead; |
| 102 |
in fact, in a majority of cases, MLton's optimizations will completely |
| 103 |
eliminate the RTTI from the final program. |
| 104 |
|
| 105 |
The implementation distributed with MLton uses the second solution. |
| 106 |
|
| 107 |
Bonus question: Why can't one use a <<UniversalType#,universal type>> |
| 108 |
to eliminate the use of `Unsafe.cast`? |
| 109 |
|
| 110 |
** Answer: ??? |
| 111 |
-- |
| 112 |
|
| 113 |
* MLton (in both of the above implementations) provides a richer |
| 114 |
`MLRep` structure, utilizing ``Int__<N>__`` and ``Word__<N>__`` |
| 115 |
structures. |
| 116 |
+ |
| 117 |
-- |
| 118 |
[source,sml] |
| 119 |
---- |
| 120 |
structure MLRep = struct |
| 121 |
structure Char = |
| 122 |
struct |
| 123 |
structure Signed = Int8 |
| 124 |
structure Unsigned = Word8 |
| 125 |
(* word-style bit-operations on integers... *) |
| 126 |
structure <<SignedBitops#>> = IntBitOps(structure I = Signed |
| 127 |
structure W = Unsigned) |
| 128 |
end |
| 129 |
structure Short = |
| 130 |
struct |
| 131 |
structure Signed = Int16 |
| 132 |
structure Unsigned = Word16 |
| 133 |
(* word-style bit-operations on integers... *) |
| 134 |
structure <<SignedBitops#>> = IntBitOps(structure I = Signed |
| 135 |
structure W = Unsigned) |
| 136 |
end |
| 137 |
structure Int = |
| 138 |
struct |
| 139 |
structure Signed = Int32 |
| 140 |
structure Unsigned = Word32 |
| 141 |
(* word-style bit-operations on integers... *) |
| 142 |
structure <<SignedBitops#>> = IntBitOps(structure I = Signed |
| 143 |
structure W = Unsigned) |
| 144 |
end |
| 145 |
structure Long = |
| 146 |
struct |
| 147 |
structure Signed = Int32 |
| 148 |
structure Unsigned = Word32 |
| 149 |
(* word-style bit-operations on integers... *) |
| 150 |
structure <<SignedBitops#>> = IntBitOps(structure I = Signed |
| 151 |
structure W = Unsigned) |
| 152 |
end |
| 153 |
structure <<LongLong#>> = |
| 154 |
struct |
| 155 |
structure Signed = Int64 |
| 156 |
structure Unsigned = Word64 |
| 157 |
(* word-style bit-operations on integers... *) |
| 158 |
structure <<SignedBitops#>> = IntBitOps(structure I = Signed |
| 159 |
structure W = Unsigned) |
| 160 |
end |
| 161 |
structure Float = Real32 |
| 162 |
structure Double = Real64 |
| 163 |
end |
| 164 |
---- |
| 165 |
|
| 166 |
This would appear to be a better interface, even when an |
| 167 |
implementation must choose `Int32` and `Word32` as the representation |
| 168 |
for smaller C-types. |
| 169 |
-- |