This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
IO.u
164 lines (122 loc) · 4.35 KB
/
IO.u
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- Handles are unique identifiers.
-- The implementation of IO in the runtime will supply Haskell
-- file handles and map those to Unison handles.
-- A pure implementation of I/O might use some kind of pure supply
-- of unique IDs instead.
type Handle = Handle Text
-- Ditto for sockets
type Socket = Socket Text
-- Builtin handles: standard in, out, error
namespace IO where
stdin: Handle
stdin = Handle "stdin"
stdout: Handle
stdout = Handle "stdout"
stderr: Handle
stderr = Handle "stderr"
printLine : Text -> {IO} ()
printLine t =
IO.putText stdout t
IO.putText stdout "\n"
-- IO Modes from the Haskell API
type IOMode = Read | Write | Append | ReadWrite
-- IO error types from the Haskell API
type IOErrorType
= AlreadyExists
| NoSuchThing
| ResourceBusy
| ResourceExhausted
| EOF
| IllegalOperation
| PermissionDenied
| UserError
type ErrorLocation = ErrorLocation Text
type ErrorDescription = ErrorDescription Text
type FilePath = FilePath Text
type IOError =
IOError
(Optional Handle)
IOErrorType
ErrorLocation
ErrorDescription
(Optional FilePath)
type SeekMode = Absolute | Relative | FromEnd
-- If the buffer size is not specified,
-- use an implementation-specific size.
type BufferMode = Line | Block (Optional Nat)
type EpochTime = EpochTime Nat
-- Either a host name e.g., "unisonweb.org" or a numeric host address
-- string consisting of a dotted decimal IPv4 address or an IPv6 address
-- e.g., "192.168.0.1".
type HostName = HostName Text
type PortNumber = Nat
-- Represents a 32-bit host address
type HostAddress = HostAddress Int
-- Internet protocol v4 socket address
type SocketAddress = SocketAddress HostAddress PortNumber
ability IO where
-- Basic file IO
openFile : FilePath -> IOMode ->{IO} Handle
closeFile : Handle ->{IO} ()
isEOF : Handle ->{IO} Boolean
isFileOpen : Handle ->{IO} Boolean
-- Text input and output
--getChar : Handle ->{IO} Char
getLine : Handle ->{IO} Text
-- Get the entire contents of the file as text
getText : Handle ->{IO} Text
-- putChar : Handle -> Char ->{IO} ()
putText : Handle -> Text ->{IO} ()
-- Handling I/O errors.
-- Question: can we do better?
throw : IOError ->{IO} a
catch : '{IO} a -> (IOError ->{IO} a) ->{IO} a
-- File positioning
isSeekable : Handle ->{IO} Boolean
seek : Handle -> SeekMode -> Int ->{IO} ()
position : Handle ->{IO} Int
-- File buffering
getBuffering : Handle ->{IO} (Optional BufferMode)
setBuffering : Handle -> Optional BufferMode ->{IO} ()
-- Should we expose mutable arrays for byte buffering?
-- Inclined to say no, although that sounds a lot like
-- a decision to just be slow.
-- We'll need a byte buffer manipulation library in that case.
-- getBytes : Handle -> Nat ->{IO} Bytes
-- putBytes : Handle -> Bytes ->{IO} ()
-- getBytes : Handle -> Nat -> ByteArray ->{IO} Nat
-- putBytes : Handle -> Nat -> ByteArray ->{IO} ()
systemTime : {IO} EpochTime
-- File system operations
getCurrentDirectory : {IO} FilePath
setCurrentDirectory : FilePath ->{IO} ()
directoryContents : FilePath ->{IO} [FilePath]
fileExists : FilePath -> {IO} Boolean
isDirectory : FilePath ->{IO} Boolean
createDirectory : FilePath ->{IO} ()
removeDirectory : FilePath ->{IO} ()
renameDirectory : FilePath -> FilePath -> {IO} ()
removeFile : FilePath ->{IO} ()
renameFile : FilePath -> FilePath ->{IO} ()
getFileTimestamp : FilePath ->{IO} EpochTime
getFileSize : FilePath ->{IO} Nat
-- Network I/O
-- Glossing over address families (ipv4, ipv6),
-- and socket types (stream, raw, etc)
-- Creates a socket and binds it to a the given local port
serverSocket : SocketAddress -> {IO} Socket
-- Creates a socket connected to the given remote address
clientSocket : SocketAddress -> {IO} Socket
socketToHandle : Socket ->{IO} Handle
handleToSocket : Handle ->{IO} Socket
closeSocket : Socket ->{IO} ()
-- Accept a connection on a socket.
-- Returns a socket that can send and receive data on a new connection,
-- together with the remote host information.
accept : Socket ->{IO} (Socket, SocketAddress)
-- Returns the number of bytes actually sent
-- send : Socket -> Bytes ->{IO} Int
-- scatter/gather mode network I/O
-- sendMany : Socket -> [Bytes] ->{IO} Int
-- Read the spefified number of bytes from the socket.
-- receive : Socket -> Int ->{IO} Bytes