10Now we know enough to write two quick examples: the Sieve of
11Eratosthenes (a naive prime sieve) and a tic-tac-toe engine.
13(Note that these are not examples of perfect Hoon code; they are
14meant for teaching. They err, more than usual for Hoon, on the
15side of simple manual construction over high-octane FP.)
17## `sieve`: code in basic style
19A function that produces the list of primes less than or equal to
25=/ field=(set @) (sy (gulf 2 thru)) :: 3
29 (sort ~(tap in field) lth) :: 7
34 ?: (gth (mul factor factor) thru) :: 12
36 $(factor +(factor), ..main (reap factor)) :: 14
40 =/ count=@ (mul 2 factor) :: 18
42 ?: (gth count thru) :: 20
45 count (add count factor) :: 23
46 field (~(del in field) count) :: 24
51## `sieve`: explanation
56=/ field=(set @) (sy (gulf 2 thru)) :: 3
59Line `1`: we are building a gate (function) whose sample
60(argument) is `thru`, an atom.
62Line `2`: the product of this gate is a list of atoms.
64Line `3`: we introduce a variable `field`, whose type is a set of
65atoms. The `sy` function converts a list to a set. The `gulf`
66function creates the range `2` through `thru` inclusive, as a
69Line `4`: below, we introduce a core which does our work. With
70reverse flow, we compute `main` on this core, then `abet` on the
71product of `main`. (`abet:main` means `=<(abet main)`.)
73The core is a state machine; `main` produces the core itself,
74with the computation completed. `abet` then extracts the result.
79 (sort ~(tap in field) lth) :: 7
82Line `5`: we wrap a core around our existing subject. The
83`field` and `thru` atoms are within the payload and of course
86Lines `6` and `7`: the `abet` arm, which produces the field as a
87sorted list. `in` is the set core; `tap` is an arm that exports
88the set as a list. We sort this list in ascending order with
97Line `9`: the `main` arm, which performs the main calculation.
99Line `10`: we declare a counter, `factor`, which will iterate
100up through the factors we're removing.
102Line `11`: we start a loop. The product of the loop is cast to
103`..main`, which is the core we're in (`+1.main`).
106 ?: (gth (mul factor factor) thru) :: 12
108 $(factor +(factor), ..main (reap factor)) :: 14
111Lines `12` and `13`: we terminate the loop, returning `..main`,
112if the square of the counter exceeds the range.
114Line `14`: repeats the loop, incrementing the counter, and
115replacing the core with a version sieved by `reap`.
120 =/ count=@ (mul 2 factor) :: 18
123Lines `16` and `17`: the `reap` arm, which produces a gate that
124removes multiples of a factor from the field (set of possible
125primes). The sample of this gate is the factor to remove.
127Line `18`: we declare a variable `count`, an atom, with the
128initial value `(mul 2 factor)`.
132 ?: (gth count thru) :: 20
135 count (add count factor) :: 23
136 field (~(del in field) count) :: 24
141Line `19`: we enter a loop. This loop produces the core itself.
143Lines `20` and `21`: we are done if the counter is out of range.
145Lines `22` to `25`: repeat the loop, stepping `count` to the next
146multiple of `factor`, and deleting it from `field`.
148## `sieve`: code in kernel style
150Finally, the same code in a more concise "kernel" style.
154=+ fed=(sy (gulf 2 top))
157++ abet (sort (~(tap in fed)) lth)
161 ?: (gth (mul fac fac) top)
163 $(fac +(fac), ..main (reap fac))
171 $(cot (add cot fac), fed (~(del in fed) cot))
175### `toe`: tic-tac-toe engine
177The `toe` tic-tac-toe engine applies a list of events to a game
178state, producing a list of game actions. Events are tic-tac-toe
179moves (`move)`; actions are tic-tac-toe results (`fact`).
182=< |= feed/(list move) :: 1
184 |- ^- (list fact) :: 3
186 =^ this/(unit fact) :: 5
188 (~(do go game) i.feed) :: 7
189 =/ rest/(list fact) $(feed t.feed) :: 8
190 ?~(this rest [u.this rest]) :: 9
194 ++ spot {x/@ y/@} :: 13
195 ++ fact $% {$tie $~} :: 14
198 ++ move $% {$x p/spot} :: 17
210 ++ bit |=(a/@ =(1 (cut 0 [a 1] half))) :: 29
211 ++ off |=(a/spot (add x.a (mul 3 y.a))) :: 30
212 ++ get |=(a/spot (bit (off a))) :: 31
213 ++ set |=(a/spot (con half (bex (off a)))) :: 32
215 (rip 4 0wl04h0.4A0Aw.4A00s.0e070) :: 34
216 |=(a/@ =(a (dis a half))) :: 35
222 ^- {(unit fact) ^game} :: 41
224 {$x *} ?>(w.game ~(mo on p.act)) :: 43
225 {$o *} ?<(w.game ~(mo on p.act)) :: 44
229 ++ nu ^+(game [& 0 0]) :: 48
232 ++ is ?| (~(get bo a.game) here) :: 51
233 (~(get bo z.game) here) :: 52
235 ++ mo ^- {(unit fact) ^game} :: 54
237 =/ next/side (~(set bo a.game) here) :: 56
238 ?: ~(win bo next) :: 57
239 [[~ %win ?:(w.game %x %o)] nu] :: 58
240 [~ game(w !w.game, a z.game, z next)] :: 59
246### `toe`: explanation
249=< |= feed/(list move) :: 1
251 |- ^- (list fact) :: 3
255Line `1` wraps a conclusion around a stack of cores. The cores
256at `11` and `26` are the data structures and functions of the
257engine; the conclusion is the interface and top-level logic. It
258produces a gate whose sample is `feed`, a list of `move`.
260Line `2` declares a blank new variable named `game`, whose type
261is the `game` mold (tic-tac-toe game state) defined on line `21`.
262Under line `2`, `game` pulls the variable and `^game` the mold.
264Line `3` enters a loop producing a list of game events, `fact`.
266Line `4` produces nil, `~`, if `feed` is empty.
269 =^ this/(unit fact) :: 5
271 (~(do go game) i.feed) :: 7
274Lines `5` through `7` apply the move. We open a `go` core on the
275current game state, produce a `do` gate, and call that gate on
276the move. The product of the `do` gate is a pair; the head is
277`(unit fact)`, either `~` or `[~ fact]`; the tail is a new
278`game`. The `=^` hoon declares the head as a new variable,
279`this`; it modifies the subject by setting `game` to the tail.
282 =/ rest/(list fact) $(feed t.feed) :: 8
283 ?~(this rest [u.this rest]) :: 9
286Line `8` declares a new variable, `rest`, which is all the facts
287generated by the moves in `t.feed`, the tail of the move list.
288(Yes, this is gratuitous head recursion.) Line `9` produces
289`rest` if the `i.feed` move produced no fact; otherwise it
290prepends the new fact to `rest`.
295 ++ spot {x/@ y/@} :: 13
298Line `11` wraps a library core around a data structure core, a
299common pattern. (You could put the molds and the gates in the
300same core, but the compiler couldn't constant-fold gate samples
301that use these molds, an efficiency cost.)
303Line `12` defines the `side` mold, an alias for `atom` (`@`).
304A side is a half of the board, as a marked/unmarked bitfield.
306Line `13` defines the `spot` mold, a 2D coordinate.
309 ++ fact $% {$tie $~} :: 14
314Lines `14` to `16` define the `fact` mold, a game event (i.e.,
315output action), as a book (tagged union). There are two forms of
316`fact`: `[%tie ~]` (reporting a tie), and `[%win cord]`
317(reporting that `'X'` or `'O'` won a game.
320 ++ move $% {$x p/spot} :: 17
326Lines `17` to `20` define the `move` mold, a game move. This is
327either `[%x spot]`, `[%o spot]`, or `[%z ~]` to reset the board.
337Lines `21` to `23` define the `game` mold, the game state. This
338is an `a` side, the next side to move; a `z` side, the other
339side; and `w`, which is `&` (yes) if `a` is X and `b` is O; `|`
340otherwise. (Obviously, we swap `a` and `z` on every move.)
346 ++ bit |=(a/@ =(1 (cut 0 [a 1] half))) :: 29
347 ++ off |=(a/spot (add x.a (mul 3 y.a))) :: 30
348 ++ get |=(a/spot (bit (off a))) :: 31
349 ++ set |=(a/spot (con half (bex (off a)))) :: 32
352Line `26` introduces the library core, which contains the doors
353`bo` (for `side` bitfields) and `go` (for game state).
355Lines `27` to `28` begin the `bo` core, a door around `half`, a
358Line `29` is the `bit` gate, which tests a bit in `half`.
360Line `30` is the `off` gate, which maps from a `spot` (coordinate)
363Line `31` is the `get` gate, which produces the bit at a `spot`.
365Line `32` is the `set` gate, which produces a new side with the
366bit at a `spot` set. It works by ORing (`con`) `half` with the
367binary exponent (`bex`) of the offset (`off`) of the spot.
371 (rip 4 0wl04h0.4A0Aw.4A00s.0e070) :: 34
372 |=(a/@ =(a (dis a half))) :: 35
376Lines `33` to `35` are a victory test. It calls the
377standard `lien` function, which iterates a boolean gate over a
378list and produces `&` (yes) if any item tests as true, against a
379list of magic bitfields (expanded from a base64 constant), each
380of which must equal itself when ANDed (`dis`) with `half`.
387 ^- {(unit fact) ^game} :: 41
390Lines `37` to `38` begin the `go` core, a door around a `game`.
391Again we name the sample `game`. (This is a legitimate style,
394Lines `40` to `42` begin the `do` gate, whose sample is `act`, a
395move. The product is cast to a pair of `(unit fact)` and a new
396game state, written `^game` to skip to the second binding.
400 {$x *} ?>(w.game ~(mo on p.act)) :: 43
401 {$o *} ?<(w.game ~(mo on p.act)) :: 44
407Lines `42` through `46` switch on the form of `act`. If it's an
408`%x` or `%o` move, we assert that it's the right turn, then use
409the `mo` door to apply the move. If it's a `%z` move, we clear
413 ++ nu ^+(game [& 0 0]) :: 48
416 ++ is ?| (~(get bo a.game) here) :: 51
417 (~(get bo z.game) here) :: 52
421Line `48` is the `nu` arm, which produces a blank game state.
423Lines `49` to `50` begin the `on` door, a door around `here`, a
424`spot`. Just as we have multiple computed attributes against the
425game state, we have multiple computed attributes (`is` and `mo`)
426against the combination of game state and coordinate.
428Lines `51` to `53` define the `is` arm, which is `&` (yes) if
429either `a` or `z` has played `here`.
432 ++ mo ^- {(unit fact) ^game} :: 54
434 =/ next/side (~(set bo a.game) here) :: 56
435 ?: ~(win bo next) :: 57
436 [[~ %win ?:(w.game %x %o)] nu] :: 58
437 [~ game(w !w.game, a z.game, z next)] :: 59
443Line `54` begins the `mo` arm, which produces (with a
444nonessential cast) a pair of `(unit fact)` and game state.
445Again we see `^game` to pull the mold, since `game` is the
446current game state in the `go` core.
448Line `55` asserts that no one has yet played `here`.
450Line `56` declares a variable `next`, as game side `a` with
453Lines `57` and `58` test if `a` has won with this move. If so,
454we produce the win fact and a blank game state.
456Line `59` produces no fact (since nothing has happened), but
457inverts the `w` turn indicator, replaces side `a` with the other
458side `z`, and replaces `z` with `next`.
460### `toe`: code, kernel style
467 =^ did/(unit fact) mag (~(do go mag) i.fed)
469 ?~(did res [u.did res])
476 ++ move $% {$x p/spot}
488 ++ bit |=(a/@ =(1 (cut 0 [a 1] haf)))
489 ++ get |=(a/spot (bit (off a)))
490 ++ off |=(a/spot (add x.a (mul 3 y.a)))
491 ++ set |=(a/spot (con haf (bex (off a))))
493 (rip 4 0wl04h0.4A0Aw.4A00s.0e070)
494 |=(a/@ =(a (dis a haf)))
500 ^- {(unit fact) game}
502 {$x *} ?>(w.mag ~(mo on p.act))
503 {$o *} ?<(w.mag ~(mo on p.act))
511 ++ is ?| (~(get bo a.mag) her)
512 (~(get bo z.mag) her)
514 ++ mo ^- {(unit fact) game}
516 =+ next=(~(set bo a.mag) her)
518 [[~ %win ?:(w.mag %x %o)] nu]
519 [~ mag(w !w.mag, a z.mag, z next)]
527++ atom @ :: just an atom
528++ axis @ :: tree address
529++ bean ? :: 0=&=yes, 1=|=no
530++ bloq @ :: blockclass
531++ char @tD :: UTF-8 byte
532++ cord @t :: text atom (UTF-8)
533++ gate $-(* *) :: general gate
534++ knot @ta :: ASCII text
536++ null $~ :: null, nil, etc
537++ time @da :: galactic time
538++ path (list knot) :: eg, fs location
539++ tang (list tank) :: error trace
541 $% {$leaf p/tape} :: printing formats
542 $: $palm :: backstep list
543 p/{p/tape q/tape r/tape s/tape} ::
546 $: $rose :: flat list
547 p/{p/tape q/tape r/tape} :: mid open close
551++ tanq :: future tank
552 $? {$~ p/(list tanq)} :: list of printables
553 {$~ $~ p/tape} :: simple string
554 (pair @tas tanq) :: captioned
556++ tape (list char) :: string as list
557++ term @tas :: ascii symbol
558++ wall (list tape) :: text lines (no \n)
559++ wain (list cord) :: text lines (no \n)
564++ tree |* a/gate :: binary tree
565 $@($~ {n/a l/(tree a) r/(tree a)}) ::
566++ trap |*(a/_* _|?(*a)) :: makes perfect sense
567++ trel |* {a/gate b/gate c/gate} :: just a triple
569++ each |* {a/gate b/gate} :: either a or b
570 $%({$& p/a} {$| p/b}) :: a default
572++ qual |* {a/gate b/gate c/gate d/gate} :: just a quadruple
574++ pair |*({a/gate b/gate} {p/a q/b}) :: just a pair
575++ quid |*({a/gate b/*} {a _b}) :: for =^
576++ quip |*({a/gate b/*} {(list a) _b}) :: for =^
577++ unit |* a/gate :: maybe
579++ list |* a/gate :: null-term list
580 $@($~ {i/a t/(list a)}) ::
581++ lone |*(a/gate p/a) :: just one thing
585++ map |* {a/gate b/gate} :: associative tree
586 $@($~ {n/{p/a q/b} l/(map a b) r/(map a b)}) ::
587++ qeu |* a/gate :: queue
588 $@($~ {n/a l/(qeu a) r/(qeu a)}) ::
589++ set |* a/gate :: set
590 $@($~ {n/a l/(set a) r/(set a)}) ::
591++ jar |*({a/gate b/gate} (map a (list b))) :: map of lists
592++ jug |*({a/gate b/gate} (map a (set b))) :: map of sets
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list