Potential Network SIG

Yusaku Hashimoto nonowarn at gmail.com
Mon Aug 24 12:46:46 EDT 2009


On Sat, Aug 22, 2009 at 1:49 PM, Thomas
DuBuisson<thomas.dubuisson at gmail.com> wrote:
> 2) Maintain type safety by using type classes for most things.
> Unlike Network.Fancy and Network.Socket (which have IPv4 and IPv6 as
> constructors of the same data type), I think we should allow for the
> possibility that some users of the library will be limited to just one
> IP version without resorting to partial functions.  I suggest type
> classes to cover this aspect (class Address, class Port, etc).

I was trying to add another type safety for socket operations, and you
can get it by ` darcs get
http://patch-tag.com/r/phantom-socket/pullrepo phantom-socket`.

My approach is having state of socket in its type, and having states
of socket before/after actions in its types. This forces programmers
not to write wrong operations like listening on socket before binding.

E.g., Function for creating socket which is connected to somewhere has
the type `HostName -> PortNumber -> IO (Socket Connected)`. Note type
Socket has parameter in its type of returning value, this represents
the state of connected socket.

And a function for listening with specifying backlog's size has the
type `BackLog -> SockAct Bound Listening ()`. SockAct is an indexed
monad[1] has socket state before/after its action in its *type*. Here,
SockAct has three parameters. The first is the before-state of the
action, the second is the after-state, and the last is the type of
value wrapped in monad.

Working example is at here[2]. If you swapped some lines in
ixdo-block, you would see compile error, not run-time error. (ixdo is
do-notation in indexed monad, it is provided by ixdopp, preprocessor
for de-sugaring ixdo-block before actual compiling)

But it ensures only compile-time's safety, can't ensure run-time's
safety (consider if client closes socket while sending). Currently, if
run-time and compile-time's states become not same, program just

So I think my implementation can't be used in real world, but I hope
this would give you some ideas for type-safe network libraries.

[1]: http://hackage.haskell.org/packages/archive/category-extras/0.44.4/doc/html/Control-Monad-Indexed.html
[2]: http://patch-tag.com/r/phantom-socket/snapshot/current/content/pretty/test/Test.hs


More information about the Libraries mailing list