[MLton] Phantom typing of sockets broken in MLton

Vesa Karvonen vesa.a.j.k at gmail.com
Wed Oct 15 12:42:14 PDT 2008


On Wed, Oct 15, 2008 at 6:04 PM, Matthew Fluet <fluet at tti-c.org> wrote:
[...]
> Using Socket :> SOCKET_EXTRA leads to lots of type errors in the Basis
> Library; Deleting where type ('a, 'b) Socket.sock = ('a, 'b) Socket.sock
> doesn't lead to type errors in the Basis Library itself, but does lose the
> type equivalence between the visible Socket.sock and the Socket.sock type
> mentioned in INetSock and UnixSock.

Yeah, I think that the easiest way to fix this is to expose some more
stuff in the SOCKET_EXTRA signature.  Below is a patch proposal (which
I haven't yet fully verified to be correct).

-Vesa Karvonen

Index: net/socket.sml
===================================================================
--- net/socket.sml	(revision 6920)
+++ net/socket.sml	(working copy)
@@ -5,7 +5,7 @@
  * See the file MLton-LICENSE for details.
  *)

-structure Socket : SOCKET_EXTRA =
+structure Socket :> SOCKET_EXTRA =
 struct

 structure Prim = PrimitiveFFI.Socket
@@ -15,6 +15,8 @@

 structure Sock = Net.Sock
 type sock = Sock.t
+val fromRep = Sock.fromRep
+val toRep = Sock.toRep
 val sockToWord = C_Sock.castToSysWord o Sock.toRep
 val wordToSock = Sock.fromRep o C_Sock.castFromSysWord
 val sockToFD = PrePosix.FileDesc.fromRep o Sock.toRep
@@ -64,6 +66,7 @@
 structure SOCK =
    struct
       type sock_type = SockType.t
+      val toRep = SockType.toRep
       val stream = SockType.fromRep Prim.SOCK.STREAM
       val dgram = SockType.fromRep Prim.SOCK.DGRAM
       val names : (string * sock_type) list =
@@ -80,6 +83,7 @@
             SOME (_, st) => SOME st
           | NONE => NONE
    end
+structure SOCKExtra = SOCK

 structure CtlExtra =
    struct
Index: net/generic-sock.sml
===================================================================
--- net/generic-sock.sml	(revision 6920)
+++ net/generic-sock.sml	(working copy)
@@ -12,19 +12,19 @@
       structure PESC = PE.SysCall

       fun socket' (af, st, p) =
-         (Net.Sock.fromRep o PESC.simpleResult)
+         (Socket.fromRep o PESC.simpleResult)
          (fn () => Prim.socket (Net.AddrFamily.toRep af,
-                                Net.SockType.toRep st,
+                                Socket.SOCKExtra.toRep st,
                                 C_Int.fromInt p))

       fun socketPair' (af, st, p) =
          let
             val a : C_Sock.t array = Array.array (2, C_Sock.fromInt 0)
-            val get = fn i => Net.Sock.fromRep (Array.sub (a, i))
+            val get = fn i => Socket.fromRep (Array.sub (a, i))
          in
             PESC.syscall
             (fn () => (Prim.socketPair (Net.AddrFamily.toRep af,
-                                        Net.SockType.toRep st,
+                                        Socket.SOCKExtra.toRep st,
                                         C_Int.fromInt p,
                                         a),
                        fn _ => (get 0, get 1)))
Index: net/socket.sig
===================================================================
--- net/socket.sig	(revision 6920)
+++ net/socket.sig	(working copy)
@@ -165,14 +165,21 @@
 signature SOCKET_EXTRA =
   sig
     include SOCKET
+    val fromRep : C_Sock.t -> ('af, 'sock_type) sock
+    val toRep : ('af, 'sock_type) sock -> C_Sock.t
     val sockToWord: ('af, 'sock_type) sock -> SysWord.word
     val wordToSock: SysWord.word -> ('af, 'sock_type) sock
     val sockToFD: ('af, 'sock_type) sock -> Posix.FileSys.file_desc
     val fdToSock: Posix.FileSys.file_desc -> ('af, 'sock_type) sock
-    type pre_sock_addr
+    type pre_sock_addr = Word8.word array
     val unpackSockAddr: 'af sock_addr -> Word8.word vector
     val newSockAddr: unit -> (pre_sock_addr * C_Socklen.t ref * (unit
-> 'af sock_addr))

+    structure SOCKExtra:
+      sig
+         val toRep : SOCK.sock_type -> C_Sock.t
+      end
+
     structure CtlExtra:
        sig
           type level = C_Int.int



More information about the MLton mailing list