Socket Type Definitions #
This module defines some basic types to be used across whole pacage:
Socket
: Opaque reference to underlying socketSockAddr
: Opaque reference to underlying socket addressAddressFamily
: Enumeration of supported address families
Socket #
Use NonemptyType
to implement Inhabited
for Socket
.
detailed explanation
Opaque reference to underlying platform specific socket.
To create a Socket
, refer to Socket.mk
. Then you
can manipulate the Socket
using common socket functions like
Socket.bind
,Socket.connect
,
etc. For all functions available, refer to the Socket
module.
NOTE: NonemptyType
is used to implement Inhabited
for Socket
. detailed explanation
import Socket
open Socket
def main : IO Unit := do
let s ← Socket.mk
-- some socket operations
s.close
Use NonemptyType
to implement Inhabited
for Socket
.
detailed explanation
Use NonemptyType
to implement Inhabited
for SockAddr
.
detailed explanation
Opaque reference to underlying socket address.
NOTE: Different from the C sockaddr
, SockAddr
also includes length.
Use NonemptyType
to implement Inhabited
for SockAddr
detailed explanation
- unspecified: Socket.AddressFamily
- inet: Socket.AddressFamily
- inet6: Socket.AddressFamily
Enumeration of supported address families,
which is used in Socket.mk
.
Equations
- Socket.instInhabitedAddressFamily = { default := Socket.AddressFamily.unspecified }
Equations
- Socket.instToStringAddressFamily = { toString := fun _fun_discr => match _fun_discr with | Socket.AddressFamily.unspecified => "AF_UNSPEC" | Socket.AddressFamily.inet => "AF_INET" | Socket.AddressFamily.inet6 => "AF_INET6" }
Convert AddressFamily
to String
.
- unspecified: Socket.SockType
- stream: Socket.SockType
- dgram: Socket.SockType
Enumeration of supported socket types,
which is used in Socket.mk
.
Equations
- Socket.instInhabitedSockType = { default := Socket.SockType.unspecified }
Equations
- Socket.instToStringSockType = { toString := fun _fun_discr => match _fun_discr with | Socket.SockType.unspecified => "SOCK_UNSPEC" | Socket.SockType.stream => "SOCK_STREAM" | Socket.SockType.dgram => "SOCK_DGRAM" }
- read: Socket.ShutdownHow
- write: Socket.ShutdownHow
- readwrite: Socket.ShutdownHow
Enumeration of how is socket shutdown,
which is used in Socket.shutdown
.
Equations
- Socket.instInhabitedShutdownHow = { default := Socket.ShutdownHow.read }
Get hostname of current machine.
Explanation: Usage of NonemptyType
#
As Socket
and SockAddr
are implemented usinglean_external_class
.
NonemptyType
is used to implement Inhabited
for these types.
A simple example of this trick can be found here in Lean core.