linear-socket: Typed sockets

[ gpl, library, network ] [ Propose Tags ]

Please see

[Skip to Readme]


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,,
Change log
Dependencies base (>=4.7 && <5), bytestring, network [details]
License GPL-3.0-only
Copyright Copyright (C) 2018 Allele Dev
Author Allele Dev
Category Network
Source repo head: git clone
Uploaded by alleledev at 2018-09-29T23:20:51Z
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 2472 total (26 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-09-29 [all 1 reports]

Readme for linear-socket-

[back to package description]


Maintainer Allele Dev (
Funding $0 USD
Copyright Copyright (C) 2017 Allele Dev
License GPL-3


A networking socket library aiming to add tracking of socket properties at the type level. Currently, the following are tracked:

  • socket status: unconnected, open, bound, etc
  • socket family: inet4, inet6, unix
  • socket protocol: tcp, udp
  • socket shutdown state: available, can't send, can't receive, etc

This makes it so that many socket programming errors are caught before the program is ever run. For example:

  • sending or receiving from a TCP socket that isn't connected
    • can still sendTo/recvFrom for UDP sockets
  • listening on a socket that hasn't been bound
  • accepting from a socket that hasn't started listening
  • sending from a socket that has had it's send capability shutdown
  • receiving from a socket that has had it's recv capability shutdown

However, the protection isn't perfect. In particular, until linear types are used, the following is one way to cause errors in lieu of the type protection:

  1. Create a thread taking a socket and modify the socket in the parent view:
sock <- tcp4Socket
bound <- bind serverAddress sock
server <- listen 1 bound
forkIO (doThing server)
close server

doThings :: SSocket f p s sh -> IO ()
doThings server = do
  accept server -- this will crash because of the close above



import Network.Typed.Socket
import Network.Socket (tupleToHostAddress)

Setting a TCP/IPv4 server address:

serverAddress = SockAddrInet 2291 (tupleToHostAddress (127,0,0,1))

An echo server:

-- point-free style
pfServer =
    >>= bind serverAddress
    >>= listen 1
    >>= (\server ->
      accept server >>= (\(client, _) ->
        recv 32 client >>= (\bs -> send bs client)))

-- explicit do-notation
doServer = do
  sock <- tcp4Socket
  bound <- bind serverAddress sock
  server <- listen 1 bound
  (client, _) <- accept server
  bs <- recv 32 client
  send bs client

An echo client:

-- point-free style
pfClient =
    >>= connect serverAddress
    >>= (\client -> send "fish" client >> recv 32 client)

-- explicit do-notation
doClient = do
  sock <- tcp4Socket
  client <- connect serverAddress sock
  send "fish" client
  recv 32 client


Contributions are welcome! Documentation, examples, code, and feedback - they all help.

Be sure to review the included code of conduct. This project adheres to the Contributor's Covenant. By participating in this project you agree to abide by its terms.

This project currently has no funding, so it is maintained strictly on the basis of its use to me. No guarantees are made about attention to issues or contributions, or timeliness thereof.

Developer Setup

The easiest way to start contributing is to install stack. stack can install GHC/Haskell for you, and automates common developer tasks.

The key commands are:

  • stack setup: install GHC
  • stack build: build the project
  • stack clean: clean build artifacts
  • stack haddock: builds documentation
  • stack test: run all tests
  • stack bench: run all benchmarks
  • stack ghci: start a REPL instance


This project is distributed under the GPL-3 license. See the included LICENSE file for more details.