public snapshot · areas/examples/raft-nib/ · system@main · commit da2bf624f03d · shared 2026-11-23T17:23:12Z· frozen: this page does not change when the repository does
1-- Raft: leader election and log replication, every node a fibre on the
2-- simulated network (`docs/language/world.md`).
3--
4-- Each node listens on its own address, dials every peer for its outbound
5-- messages, and reads each peer's inbound connection in a fibre of its own.
6-- Every reader, and a timer, sends into the node's one mailbox (a channel), and
7-- the node's main loop takes one event at a time from it: a peer's message or a
8-- tick. That loop is the whole of Raft's logic, and it never waits on anything
9-- but its mailbox.
10--
11-- What a model needs to see is effects, so the program's state is where a real
12-- implementation keeps it:
13--
14-- `Store` the persistent state — current term, vote, log — which Raft
15-- requires to survive a crash. Every write is a journalled
16-- operation, before the message it justifies is sent.
17-- `Machine` the state machine a committed entry is applied to, once, in
18-- index order.
19-- messages JSON, so a model reads `term`, `kind` and `entries` out of what
20-- one node sent another.
21use "std/net.nib"
22use "std/time.nib"
23use "std/random.nib"
24use "std/io.nib"
25 26data Entry where
27 term :: Int;
28 cmd :: String
29 30-- One record shape for every message: the kind says which fields mean
31-- something, and a model reads them all the same way.
32data Msg where
33 kind :: String;
34 term :: Int;
35 from :: String;
36 last_index :: Int;
37 last_term :: Int;
38 granted :: Bool;
39 prev_index :: Int;
40 prev_term :: Int;
41 entries :: Vec<Entry>;
42 commit :: Int;
43 success :: Bool;
44 match_index :: Int
45 46data Ev where Tick; Got(Msg)
47 48-- A node's volatile state. `term`, `voted` and `log` are copies of what
49-- `Store` holds, kept beside the rest so one value is the node's whole view.
50data Node where
51 me :: String;
52 peers :: Vec<String>;
53 role :: String;
54 term :: Int;
55 voted :: String;
56 log :: Vec<Entry>;
57 commit :: Int;
58 applied :: Int;
59 votes :: Vec<String>;
60 next :: Map<String, Int>;
61 matched :: Map<String, Int>;
62 deadline :: Int;
63 beat :: Int;
64 links :: Map<String, Handle>
65 66-- What `Store` gives back to a node that starts: everything it saved.
67data Saved where
68 term :: Int;
69 voted :: String;
70 log :: Vec<Entry>
71 72interface Store where
73 recover(node: String) -> Saved;
74 save(node: String, term: Int, voted: String) -> ();
75 append(node: String, keep: Int, entries: Vec<Entry>) -> ()
76 77-- Two rules this program can be built without, so a check of each has a
78-- known-wrong program to refuse. A node grants at most one vote per term
79-- (`one_vote`), and only to a candidate whose log is at least as up to date as
80-- its own (`check_log`). `Main` binds both as Raft states them; `Mutant`
81-- votes for every candidate that asks; `Careless` ignores the candidate's log.
82interface Rules where
83 one_vote() -> Bool;
84 check_log() -> Bool
85 86effector Strict is Rules where
87 one_vote() = true
88 check_log() = true
89 90effector Lax is Rules where
91 one_vote() = false
92 check_log() = true
93 94effector Blind is Rules where
95 one_vote() = true
96 check_log() = false
97 98interface Machine where
99 apply(node: String, index: Int, cmd: String) -> ();
100 applied(node: String) -> Vec<String>
101102-- An effector's cells outlive the node's fibres, so a crash loses none of
103-- what `Disk` holds: a node that starts again recovers it.
104effector Disk is Store where
105 unkeyed var terms :: Map<String, Int> = {};
106 unkeyed var votes :: Map<String, String> = {};
107 unkeyed var logs :: Map<String, Vec<Entry>> = {};
108 recover(node) =
109 Saved{ term: Map.get_or(terms, node, 0), voted: Map.get_or(votes, node, ""),
110 log: Map.get_or(logs, node, #[]) }
111 save(node, term, voted) =
112 terms := Map.set(terms, node, term); votes := Map.set(votes, node, voted); ()
113 append(node, keep, entries) =
114 logs := Map.set(logs, node, Vec.append(Vec.take(Map.get_or(logs, node, #[]), keep), entries)); ()
115116-- A store that writes as `Disk` does but recovers nothing, as a node that
117-- never syncs its writes would: after a crash it has forgotten its term and
118-- its vote. `Forgetful` binds it, so a check of durability has a program to
119-- refuse.
120effector Ram is Store where
121 unkeyed var logs :: Map<String, Vec<Entry>> = {};
122 recover(node) = Saved{ term: 0, voted: "", log: #[] }
123 save(node, term, voted) = ()
124 append(node, keep, entries) =
125 logs := Map.set(logs, node, Vec.append(Vec.take(Map.get_or(logs, node, #[]), keep), entries)); ()
126127-- A node that starts again applies its log from the first entry, so applying
128-- an index the machine already has changes nothing.
129effector Kv is Machine where
130 unkeyed var done :: Map<String, Vec<String>> = {};
131 apply(node, index, cmd) =
132 let had = Map.get_or(done, node, #[]);
133 if index == Vec.size(had) + 1 then (done := Map.set(done, node, Vec.push(had, cmd)); ())
134 else ()
135 applied(node) = Map.get_or(done, node, #[])
136137module Raft where
138 -- ---- the cluster ------------------------------------------------------
139 func port :: -> Int = 7000
140 func hosts :: -> Vec<String> = #["10.0.0.1", "10.0.0.2", "10.0.0.3"]
141 func heartbeat_ms :: -> Int = 50
142 func max_log :: -> Int = 6
143 func run_ms :: -> Int = 3000
144 func tick_ms :: -> Int = 10
145146 func others :: me: String -> Vec<String> =
147 Vec.filter(hosts()) { h -> h != me }
148149 func majority :: -> Int = Vec.size(hosts()) / 2 + 1
150151 -- ---- reading a message ------------------------------------------------
152 func entries_of :: items, acc: Vec<Entry> -> Vec<Entry> raises Err.MissingField, Err.NotAnObject, Err.WrongType =
153 match (items)
154 [e, ...rest] => entries_of(rest, Vec.push(acc, Entry{ term: Json.int(e, "term"), cmd: Json.string(e, "cmd") }))
155 [] => acc
156 end
157158 -- A message from a peer is input: a field of the wrong shape is a
159 -- `WrongType` the reader drops, as it drops a message that is not JSON.
160 func decode :: text: String -> Msg raises Err.InvalidJson, Err.MissingField, Err.NotAnObject, Err.NotAList, Err.WrongType =
161 let doc = Json.parse(text);
162 Msg{ kind: Json.string(doc, "kind"), term: Json.int(doc, "term"),
163 from: Json.string(doc, "from"),
164 last_index: Json.int(doc, "last_index"), last_term: Json.int(doc, "last_term"),
165 granted: Json.bool(doc, "granted"), prev_index: Json.int(doc, "prev_index"),
166 prev_term: Json.int(doc, "prev_term"),
167 entries: entries_of(Json.items(Json.field(doc, "entries")), #[]),
168 commit: Json.int(doc, "commit"), success: Json.bool(doc, "success"),
169 match_index: Json.int(doc, "match_index") }
170171 -- ---- messages -----------------------------------------------------------
172 func blank :: kind: String, term: Int, from: String -> Msg =
173 Msg{ kind: kind, term: term, from: from, last_index: 0, last_term: 0, granted: false,
174 prev_index: 0, prev_term: 0, entries: #[], commit: 0, success: false, match_index: 0 }
175176 func vote_req :: n: Node -> Msg =
177 #{ blank("vote_req", n.term, n.me) with last_index: Vec.size(n.log), last_term: last_term(n.log) }
178179 func vote :: n: Node, granted: Bool -> Msg =
180 #{ blank("vote", n.term, n.me) with granted: granted }
181182 func append_req :: n: Node, peer: String -> Msg =
183 let prev = Map.get_or(n.next, peer, 1) - 1;
184 #{ blank("append", n.term, n.me) with prev_index: prev, prev_term: term_at(n.log, prev),
185 entries: Vec.drop(n.log, prev), commit: n.commit }
186187 func append_reply :: n: Node, ok: Bool, upto: Int -> Msg =
188 #{ blank("append_ok", n.term, n.me) with success: ok, match_index: upto }
189190 -- ---- the log -------------------------------------------------------------
191 -- Index `i` is the log's `i`th entry, from 1; index 0 is before the log and
192 -- has term 0.
193 func term_at :: log: Vec<Entry>, i: Int -> Int =
194 if i <= 0 || i > Vec.size(log) then 0
195 else try Vec.get(log, i - 1).term rescue Err.IndexOutOfRange(_) => 0 end
196197 func last_term :: log: Vec<Entry> -> Int = term_at(log, Vec.size(log))
198199 -- The log after an `append` at `prev`: entries that agree with what is there
200 -- are kept, and the first that disagrees cuts the rest. An empty `append` (a
201 -- heartbeat) cuts nothing, which is Raft's rule and the easy one to break.
202 func merge :: log: Vec<Entry>, at: Int, incoming: Vec<Entry> -> Vec<Entry> =
203 if Vec.size(incoming) == 0 then log
204 else
205 let head = try Vec.get(incoming, 0) rescue Err.IndexOutOfRange(_) => Entry{ term: 0, cmd: "" } end;
206 if at < Vec.size(log) && term_at(log, at + 1) == head.term then
207 merge(log, at + 1, Vec.drop(incoming, 1))
208 else
209 Vec.append(Vec.take(log, at), incoming)
210211 -- ---- talking to peers ----------------------------------------------------
212 -- A lost message is Raft's ordinary case, so a send that fails is dropped.
213 -- A connection that has closed (its peer crashed) is dialled again, once,
214 -- on the next send to that peer, and a peer that does not answer is left
215 -- without a link until a later send finds it back.
216 func deliver :: conn: Handle, text: String -> Bool ! Net.Socket =
217 try (let _ = @Net.Socket.send(conn, Bytes.from_string(text)); true)
218 rescue Err.Closed(_) => false
219 rescue HandleRefused(_) => false
220 end
221222 func redial :: n: Node, peer: String -> Node ! Net.Socket =
223 try #{ n with links: Map.set(n.links, peer, @Net.Socket.connect(peer, port())) }
224 rescue Err.ConnectFailed(_) => #{ n with links: Map.remove(n.links, peer) }
225 rescue Err.BindFailed(_) => #{ n with links: Map.remove(n.links, peer) }
226 end
227228 func send :: n: Node, peer: String, m: Msg -> Node ! Net.Socket =
229 let text = try Json.render(m) rescue Err.Unrenderable(_) => "" end;
230 let linked = Map.has(n.links, peer);
231 let ok = text != "" && linked
232 && (try deliver(Map.get(n.links, peer), text) rescue Err.MissingField(_, _) => false end);
233 if ok || text == "" then n
234 else
235 let r = redial(n, peer);
236 let _ = if Map.has(r.links, peer)
237 then (try deliver(Map.get(r.links, peer), text) rescue Err.MissingField(_, _) => false end)
238 else false;
239 r
240241 func broadcast :: n: Node, m: Msg -> Node ! Net.Socket =
242 Vec.fold(n.peers, n, { acc, p -> send(acc, p, m) })
243244 func replicate :: n: Node -> Node ! Net.Socket =
245 Vec.fold(n.peers, n, { acc, p -> send(acc, p, append_req(acc, p)) })
246247 -- ---- the rules -------------------------------------------------------------
248 func election_timeout :: -> Int ! Random.Source =
249 150 + (try @Random.Source.next_int(150) rescue Err.OutOfRange(_) => 0 end)
250251 -- A term newer than ours, from anyone, makes us a follower of it.
252 func observe :: n: Node, term: Int -> Node ! Store =
253 if term > n.term then
254 let _ = @Store.save(n.me, term, "");
255 #{ n with role: "follower", term: term, voted: "", votes: #[] }
256 else n
257258 func start_election :: n: Node, now: Int -> Node ! Store, Net.Socket, Random.Source =
259 let term = n.term + 1;
260 let _ = @Store.save(n.me, term, n.me);
261 let c = #{ n with role: "candidate", term: term, voted: n.me, votes: #[n.me], deadline: now + election_timeout() };
262 broadcast(c, vote_req(c))
263264 func become_leader :: n: Node, now: Int -> Node ! Net.Socket =
265 let next = Vec.fold(n.peers, {}, { m, p -> Map.set(m, p, Vec.size(n.log) + 1) });
266 let matched = Vec.fold(n.peers, {}, { m, p -> Map.set(m, p, 0) });
267 replicate(#{ n with role: "leader", next: next, matched: matched, beat: now + heartbeat_ms() })
268269 func up_to_date :: n: Node, m: Msg -> Bool =
270 let mine = last_term(n.log);
271 m.last_term > mine || (m.last_term == mine && m.last_index >= Vec.size(n.log))
272273 func on_vote_req :: n: Node, m: Msg, now: Int -> Node ! Store, Net.Socket, Random.Source, Rules =
274 let free = n.voted == "" || n.voted == m.from || !@Rules.one_vote();
275 let fresh = up_to_date(n, m) || !@Rules.check_log();
276 let grant = m.term == n.term && free && fresh;
277 if grant then
278 let _ = @Store.save(n.me, n.term, m.from);
279 let g = #{ n with voted: m.from, deadline: now + election_timeout() };
280 send(g, m.from, vote(g, true))
281 else send(n, m.from, vote(n, false))
282283 func on_vote :: n: Node, m: Msg, now: Int -> Node ! Net.Socket =
284 if n.role == "candidate" && m.term == n.term && m.granted
285 && Vec.find_index(n.votes) { v -> v == m.from } < 0 then
286 let votes = Vec.push(n.votes, m.from);
287 let c = #{ n with votes: votes };
288 if Vec.size(votes) >= majority() then become_leader(c, now) else c
289 else n
290291 func on_append :: n: Node, m: Msg, now: Int -> Node ! Store, Net.Socket, Random.Source =
292 if m.term < n.term then send(n, m.from, append_reply(n, false, 0))
293 else
294 let f = #{ n with role: "follower", deadline: now + election_timeout() };
295 if m.prev_index > Vec.size(f.log) || term_at(f.log, m.prev_index) != m.prev_term then
296 send(f, m.from, append_reply(f, false, 0))
297 else
298 let log = merge(f.log, m.prev_index, m.entries);
299 let _ = if Vec.size(m.entries) > 0 then @Store.append(f.me, Vec.size(log) - Vec.size(Vec.drop(log, m.prev_index)), Vec.drop(log, m.prev_index)) else ();
300 let upto = m.prev_index + Vec.size(m.entries);
301 let g = #{ f with log: log };
302 let h = if m.commit > g.commit then #{ g with commit: if m.commit < upto then m.commit else upto } else g;
303 send(h, m.from, append_reply(h, true, upto))
304305 -- The largest index a majority holds in the leader's own term, if it is past
306 -- the commit point.
307 func commit_from :: n: Node, i: Int -> Int =
308 if i <= n.commit then n.commit
309 else
310 let holders = 1 + Vec.size(Vec.filter(n.peers) { p -> Map.get_or(n.matched, p, 0) >= i });
311 if holders >= majority() && term_at(n.log, i) == n.term then i
312 else commit_from(n, i - 1)
313314 func on_append_ok :: n: Node, m: Msg -> Node ! Net.Socket =
315 if n.role != "leader" || m.term != n.term then n
316 else if m.success then
317 let had = Map.get_or(n.matched, m.from, 0);
318 let upto = if m.match_index > had then m.match_index else had;
319 let p = #{ n with next: Map.set(n.next, m.from, upto + 1), matched: Map.set(n.matched, m.from, upto) };
320 #{ p with commit: commit_from(p, Vec.size(p.log)) }
321 else
322 let back = Map.get_or(n.next, m.from, 1) - 1;
323 let p = #{ n with next: Map.set(n.next, m.from, if back < 1 then 1 else back) };
324 send(p, m.from, append_req(p, m.from))
325326 -- Every committed entry not yet applied, in index order, once.
327 func apply_committed :: n: Node -> Node ! Machine =
328 if n.applied >= n.commit then n
329 else
330 let i = n.applied + 1;
331 let cmd = try Vec.get(n.log, i - 1).cmd rescue Err.IndexOutOfRange(_) => "" end;
332 let _ = @Machine.apply(n.me, i, cmd);
333 apply_committed(#{ n with applied: i })
334335 -- A leader proposes the next command on each heartbeat, until its log is full.
336 func propose :: n: Node -> Node ! Store =
337 if Vec.size(n.log) >= max_log() then n
338 else
339 let e = Entry{ term: n.term, cmd: "{n.me}/t{n.term}/{Vec.size(n.log) + 1}" };
340 let _ = @Store.append(n.me, Vec.size(n.log), #[e]);
341 #{ n with log: Vec.push(n.log, e) }
342343 func on_tick :: n: Node, now: Int -> Node ! Store, Net.Socket, Random.Source =
344 if n.role == "leader" then
345 if now >= n.beat then
346 let p = replicate(propose(n));
347 #{ p with beat: now + heartbeat_ms() }
348 else n
349 else if now >= n.deadline then start_election(n, now)
350 else n
351352 func handle :: n: Node, ev: Ev, now: Int -> Node ! Store, Net.Socket, Random.Source, Machine, Rules =
353 let next =
354 match ev
355 Tick => on_tick(n, now)
356 Got(m) =>
357 let o = observe(n, m.term);
358 if m.kind == "vote_req" then on_vote_req(o, m, now)
359 else if m.kind == "vote" then on_vote(o, m, now)
360 else if m.kind == "append" then on_append(o, m, now)
361 else if m.kind == "append_ok" then on_append_ok(o, m)
362 else o
363 end;
364 apply_committed(next)
365366 -- ---- the fibres of one node ----------------------------------------------
367 -- A tick every `tick_ms`, and one more at `stop`, so the loop wakes to see
368 -- that the run is over rather than waiting on a mailbox nobody will fill.
369 func timer :: mb: Channel<Ev>, stop: Int -> () ! Time.Clock =
370 let _ = @Time.Clock.sleep(tick_ms());
371 let sent = Channel.send(mb, Tick);
372 if @Time.Clock.now() >= stop || !sent then () else timer(mb, stop)
373374 -- A reader ends with its connection: a peer that crashed dials again, and
375 -- the acceptor gives that connection a reader of its own.
376 func reader :: conn: Handle, mb: Channel<Ev>, stop: Int -> () ! Net.Socket, Time.Clock =
377 let bytes = try @Net.Socket.recv(conn)
378 rescue Err.Closed(_) => Bytes.from_string("")
379 rescue HandleRefused(_) => Bytes.from_string("")
380 end;
381 if @Time.Clock.now() >= stop || Bytes.length(bytes) == 0 then ()
382 else
383 let sent =
384 try Channel.send(mb, Got(decode(Bytes.to_string(bytes))))
385 rescue Err.InvalidJson(_) => true
386 rescue Err.MissingField(_, _) => true
387 rescue Err.NotAnObject(_) => true
388 rescue Err.NotAList(_) => true
389 rescue Err.WrongType(_) => true
390 end;
391 if sent then reader(conn, mb, stop) else ()
392393 -- Every connection a peer opens gets a reader, for as long as the node runs:
394 -- a peer that restarts opens new ones.
395 func acceptor :: sw: Switch, mb: Channel<Ev>, stop: Int -> () ! Net.Socket, Time.Clock =
396 if @Time.Clock.now() >= stop then ()
397 else
398 let _ = try
399 let conn = @Net.Socket.accept(100);
400 let _ = fork(sw) { -> reader(conn, mb, stop) };
401 ()
402 rescue Err.BindFailed(_) => ()
403 end;
404 acceptor(sw, mb, stop)
405406 -- One try at each peer: a peer not listening yet is dialled again by the
407 -- first send to it.
408 func dial_all :: peers: Vec<String> -> Map<String, Handle> ! Net.Socket =
409 Vec.fold(peers, {}, { links, p ->
410 try Map.set(links, p, @Net.Socket.connect(p, port()))
411 rescue Err.ConnectFailed(_) => links
412 rescue Err.BindFailed(_) => links
413 end })
414415 func run_loop :: n: Node, mb: Channel<Ev>, stop: Int -> Node ! Store, Net.Socket, Random.Source, Machine, Rules, Time.Clock raises Err.Closed, HandleRefused =
416 let now = @Time.Clock.now();
417 if now >= stop then n
418 else
419 let ev = Channel.recv(mb);
420 run_loop(handle(n, ev, @Time.Clock.now()), mb, stop)
421422 -- One life of a node: listen, recover what `Store` saved, and run until
423 -- `stop`. A crash ends it from outside, killing every fibre it has.
424 func node :: me: String, stop: Int -> () ! Store, Net.Socket, Random.Source, Machine, Rules, Time.Clock, Io.Print =
425 try
426 let _ = @Net.Socket.listen(me, port());
427 let saved = @Store.recover(me);
428 let peers = others(me);
429 Switch.run { sw ->
430 let mb = Channel.make(sw);
431 let _ = fork(sw) { -> acceptor(sw, mb, stop) };
432 let _ = fork(sw) { -> timer(mb, stop) };
433 let start = Node{ me: me, peers: peers, role: "follower", term: saved.term, voted: saved.voted,
434 log: saved.log, commit: 0, applied: 0, votes: #[], next: {}, matched: {},
435 deadline: @Time.Clock.now() + election_timeout(), beat: 0,
436 links: dial_all(peers) };
437 let fin = run_loop(start, mb, stop);
438 let _ = Channel.close(mb);
439 @Io.Print.println("{me}: {fin.role} in term {fin.term}, log {Vec.size(fin.log)}, commit {fin.commit}")
440 }
441 rescue Err.BindFailed(m) => @Io.Print.println("{me}: {m}")
442 rescue Err.Closed(m) => @Io.Print.println("{me}: {m}")
443 rescue HandleRefused(m) => @Io.Print.println("{me}: {m}")
444 end
445446 -- A node's supervisor, outside the node: when a crash kills the node's
447 -- fibres, the await is refused, and the supervisor starts the node again.
448 -- Its `listen` waits until the world brings the host back.
449 func keep :: me: String, stop: Int -> () ! Store, Net.Socket, Random.Source, Machine, Rules, Time.Clock, Io.Print =
450 let crashed =
451 try Switch.run { sw -> let _ = Fiber.await(fork(sw) { -> node(me, stop) }); false }
452 rescue HandleRefused(_) => true
453 end;
454 if crashed && @Time.Clock.now() < stop then keep(me, stop) else ()
455456 -- ---- the check at the end -------------------------------------------------
457 -- State machine safety, as the program can see it: every two nodes applied
458 -- the same command at every index both reached. The model checks this of
459 -- the journal; the program checks it of itself, so a run says so too.
460 func agree :: a: Vec<String>, b: Vec<String>, i: Int -> Bool =
461 if i >= Vec.size(a) || i >= Vec.size(b) then true
462 else
463 let same = try Vec.get(a, i) == Vec.get(b, i) rescue Err.IndexOutOfRange(_) => false end;
464 if same then agree(a, b, i + 1) else false
465466 func consistent :: logs: Vec<Vec<String>> -> Bool =
467 Vec.all(logs) { a -> Vec.all(logs) { b -> agree(a, b, 0) } }
468469 func main :: -> String ! Store, Net.Socket, Random.Source, Machine, Rules, Time.Clock, Io.Print raises HandleRefused =
470 let _ = Switch.run { sw ->
471 let fs = Vec.map(hosts()) { h -> fork(sw) { -> keep(h, run_ms()) } };
472 Vec.fold(fs, (), { _, f -> Fiber.await(f) })
473 };
474 let logs = Vec.map(hosts()) { h -> @Machine.applied(h) };
475 let lens = Vec.fold(logs, "", { acc, l -> acc ++ " " ++ "{Vec.size(l)}" });
476 if consistent(logs) then "safe: applied{lens}" else "UNSAFE: the nodes applied different commands"
477478wire Main runs Raft.main where
479 Store = Disk, Machine = Kv, Rules = Strict,
480 Net.Socket = Net.Sim, Time.Clock = Time.Sim, Random.Source = Random.Sim,
481 Io.Print = Io.Stdout
482483wire Careless runs Raft.main where
484 Store = Disk, Machine = Kv, Rules = Blind,
485 Net.Socket = Net.Sim, Time.Clock = Time.Sim, Random.Source = Random.Sim,
486 Io.Print = Io.Stdout
487488wire Mutant runs Raft.main where
489 Store = Disk, Machine = Kv, Rules = Lax,
490 Net.Socket = Net.Sim, Time.Clock = Time.Sim, Random.Source = Random.Sim,
491 Io.Print = Io.Stdout
492493wire Forgetful runs Raft.main where
494 Store = Ram, Machine = Kv, Rules = Strict,
495 Net.Socket = Net.Sim, Time.Clock = Time.Sim, Random.Source = Random.Sim,
496 Io.Print = Io.Stdout