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 @) (silt (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 @) (silt (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 `silt` 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 which
88produces a gate that exports the set as a list, prepending it to
89a tail which is nil in this case (and most cases). We sort this
90list in ascending order with `lth` (less-than).
98Line `9`: the `main` arm, which performs the main calculation.
100Line `10`: we declare a counter, `factor`, which will iterate
101up through the factors we're removing.
103Line `11`: we start a loop. The product of the loop is cast to
104`..main`, which is the core we're in (`+1.main`).
107 ?: (gth (mul factor factor) thru) :: 12
109 $(factor +(factor), ..main (reap factor)) :: 14
112Lines `12` and `13`: we terminate the loop, returning `..main`,
113if the square of the counter exceeds the range.
115Line `14`: repeats the loop, incrementing the counter, and
116replacing the core with a version sieved by `reap`.
121 =/ count/@ (mul 2 factor) :: 18
124Lines `16` and `17`: the `reap` arm, which produces a gate that
125removes multiples of a factor from the field (set of possible
126primes). The sample of this gate is the factor to remove.
128Line `18`: we declare a variable `count`, an atom, with the
129initial value `(mul 2 factor)`.
133 ?: (gth count thru) :: 20
136 count (add count factor) :: 23
137 field (~(del in field) count) :: 24
142Line `19`: we enter a loop. This loop produces the core itself.
144Lines `20` and `21`: we are done if the counter is out of range.
146Lines `22` to `25`: repeat the loop, stepping `count` to the next
147multiple of `factor`, and deleting it from `field`.
149## `sieve`: code in kernel style
151Finally, the same code in a more concise "kernel" style.
155=+ fed=(silt (gulf 2 top))
158++ abet (sort (~(tap in fed)) lth)
162 ?: (gth (mul fac fac) top)
164 $(fac +(fac), ..main (reap fac))
172 $(cot (add cot fac), fed (~(del in fed) cot))
176### `toe`: tic-tac-toe engine
178The `toe` tic-tac-toe engine applies a list of events to a game
179state, producing a list of game actions. Events are tic-tac-toe
180moves (`move)`; actions are tic-tac-toe results (`fact`).
183=< |= feed/(list move) :: 1
185 |- ^- (list fact) :: 3
187 =^ this/(unit fact) :: 5
189 (~(do go game) i.feed) :: 7
190 =/ rest/(list fact) $(feed t.feed) :: 8
191 ?~(this rest [u.this rest]) :: 9
195 ++ spot {x/@ y/@} :: 13
196 ++ fact $% {$tie $~} :: 14
199 ++ move $% {$x p/spot} :: 17
211 ++ bit |=(a/@ =(1 (cut 0 [a 1] half))) :: 29
212 ++ off |=(a/spot (add x.a (mul 3 y.a))) :: 30
213 ++ get |=(a/spot (bit (off a))) :: 31
214 ++ set |=(a/spot (con half (bex (off a)))) :: 32
216 (rip 4 0wl04h0.4A0Aw.4A00s.0e070) :: 34
217 |=(a/@ =(a (dis a half))) :: 35
223 ^- {(unit fact) ^game} :: 41
225 {$x *} ?>(w.game ~(mo on p.act)) :: 43
226 {$o *} ?<(w.game ~(mo on p.act)) :: 44
230 ++ nu ^+(game [& 0 0]) :: 48
233 ++ is ?| (~(get bo a.game) here) :: 51
234 (~(get bo z.game) here) :: 52
236 ++ mo ^- {(unit fact) ^game} :: 54
238 =/ next/side (~(set bo a.game) here) :: 56
239 ?: ~(win bo next) :: 57
240 [[~ %win ?:(w.game %x %o)] nu] :: 58
241 [~ game(w !w.game, a z.game, z next)] :: 59
247### `toe`: explanation
250=< |= feed/(list move) :: 1
252 |- ^- (list fact) :: 3
256Line `1` wraps a conclusion around a stack of cores. The cores
257at `11` and `26` are the data structures and functions of the
258engine; the conclusion is the interface and top-level logic. It
259produces a gate whose sample is `feed`, a list of `move`.
261Line `2` declares a blank new variable named `game`, whose type
262is the `game` mold (tic-tac-toe game state) defined on line `21`.
263Under line `2`, `game` pulls the variable and `^game` the mold.
265Line `3` enters a loop producing a list of game events, `fact`.
267Line `4` produces nil, `~`, if `feed` is empty.
270 =^ this/(unit fact) :: 5
272 (~(do go game) i.feed) :: 7
275Lines `5` through `7` apply the move. We open a `go` core on the
276current game state, produce a `do` gate, and call that gate on
277the move. The product of the `do` gate is a pair; the head is
278`(unit fact)`, either `~` or `[~ fact]`; the tail is a new
279`game`. The `=^` hoon declares the head as a new variable,
280`this`; it modifies the subject by setting `game` to the tail.
283 =/ rest/(list fact) $(feed t.feed) :: 8
284 ?~(this rest [u.this rest]) :: 9
287Line `8` declares a new variable, `rest`, which is all the facts
288generated by the moves in `t.feed`, the tail of the move list.
289(Yes, this is gratuitous head recursion.) Line `9` produces
290`rest` if the `i.feed` move produced no fact; otherwise it
291prepends the new fact to `rest`.
296 ++ spot {x/@ y/@} :: 13
299Line `11` wraps a library core around a data structure core, a
300common pattern. (You could put the molds and the gates in the
301same core, but the compiler couldn't constant-fold gate samples
302that use these molds, an efficiency cost.)
304Line `12` defines the `side` mold, an alias for `atom` (`@`).
305A side is a half of the board, as a marked/unmarked bitfield.
307Line `13` defines the `spot` mold, a 2D coordinate.
310 ++ fact $% {$tie $~} :: 14
315Lines `14` to `16` define the `fact` mold, a game event (i.e.,
316output action), as a book (tagged union). There are two forms of
317`fact`: `[%tie ~]` (reporting a tie), and `[%win cord]`
318(reporting that `'X'` or `'O'` won a game.
321 ++ move $% {$x p/spot} :: 17
327Lines `17` to `20` define the `move` mold, a game move. This is
328either `[%x spot]`, `[%o spot]`, or `[%z ~]` to reset the board.
338Lines `21` to `23` define the `game` mold, the game state. This
339is an `a` side, the next side to move; a `z` side, the other
340side; and `w`, which is `&` (yes) if `a` is X and `b` is O; `|`
341otherwise. (Obviously, we swap `a` and `z` on every move.)
347 ++ bit |=(a/@ =(1 (cut 0 [a 1] half))) :: 29
348 ++ off |=(a/spot (add x.a (mul 3 y.a))) :: 30
349 ++ get |=(a/spot (bit (off a))) :: 31
350 ++ set |=(a/spot (con half (bex (off a)))) :: 32
353Line `26` introduces the library core, which contains the doors
354`bo` (for `side` bitfields) and `go` (for game state).
356Lines `27` to `28` begin the `bo` core, a door around `half`, a
359Line `29` is the `bit` gate, which tests a bit in `half`.
361Line `30` is the `off` gate, which maps from a `spot` (coordinate)
364Line `31` is the `get` gate, which produces the bit at a `spot`.
366Line `32` is the `set` gate, which produces a new side with the
367bit at a `spot` set. It works by ORing (`con`) `half` with the
368binary exponent (`bex`) of the offset (`off`) of the spot.
372 (rip 4 0wl04h0.4A0Aw.4A00s.0e070) :: 34
373 |=(a/@ =(a (dis a half))) :: 35
377Lines `33` to `35` are a victory test. It calls the
378standard `lien` function, which iterates a boolean gate over a
379list and produces `&` (yes) if any item tests as true, against a
380list of magic bitfields (expanded from a base64 constant), each
381of which must equal itself when ANDed (`dis`) with `half`.
388 ^- {(unit fact) ^game} :: 41
391Lines `37` to `38` begin the `go` core, a door around a `game`.
392Again we name the sample `game`. (This is a legitimate style,
395Lines `40` to `42` begin the `do` gate, whose sample is `act`, a
396move. The product is cast to a pair of `(unit fact)` and a new
397game state, written `^game` to skip to the second binding.
401 {$x *} ?>(w.game ~(mo on p.act)) :: 43
402 {$o *} ?<(w.game ~(mo on p.act)) :: 44
408Lines `42` through `46` switch on the form of `act`. If it's an
409`%x` or `%o` move, we assert that it's the right turn, then use
410the `mo` door to apply the move. If it's a `%z` move, we clear
414 ++ nu ^+(game [& 0 0]) :: 48
417 ++ is ?| (~(get bo a.game) here) :: 51
418 (~(get bo z.game) here) :: 52
422Line `48` is the `nu` arm, which produces a blank game state.
424Lines `49` to `50` begin the `on` door, a door around `here`, a
425`spot`. Just as we have multiple computed attributes against the
426game state, we have multiple computed attributes (`is` and `mo`)
427against the combination of game state and coordinate.
429Lines `51` to `53` define the `is` arm, which is `&` (yes) if
430either `a` or `z` has played `here`.
433 ++ mo ^- {(unit fact) ^game} :: 54
435 =/ next/side (~(set bo a.game) here) :: 56
436 ?: ~(win bo next) :: 57
437 [[~ %win ?:(w.game %x %o)] nu] :: 58
438 [~ game(w !w.game, a z.game, z next)] :: 59
444Line `54` begins the `mo` arm, which produces (with a
445nonessential cast) a pair of `(unit fact)` and game state.
446Again we see `^game` to pull the mold, since `game` is the
447current game state in the `go` core.
449Line `55` asserts that no one has yet played `here`.
451Line `56` declares a variable `next`, as game side `a` with
454Lines `57` and `58` test if `a` has won with this move. If so,
455we produce the win fact and a blank game state.
457Line `59` produces no fact (since nothing has happened), but
458inverts the `w` turn indicator, replaces side `a` with the other
459side `z`, and replaces `z` with `next`.
461### `toe`: code, kernel style
468 =^ did/(unit fact) mag (~(do go mag) i.fed)
470 ?~(did res [u.did res])
477 ++ move $% {$x p/spot}
489 ++ bit |=(a/@ =(1 (cut 0 [a 1] haf)))
490 ++ get |=(a/spot (bit (off a)))
491 ++ off |=(a/spot (add x.a (mul 3 y.a)))
492 ++ set |=(a/spot (con haf (bex (off a))))
494 (rip 4 0wl04h0.4A0Aw.4A00s.0e070)
495 |=(a/@ =(a (dis a haf)))
501 ^- {(unit fact) game}
503 {$x *} ?>(w.mag ~(mo on p.act))
504 {$o *} ?<(w.mag ~(mo on p.act))
512 ++ is ?| (~(get bo a.mag) her)
513 (~(get bo z.mag) her)
515 ++ mo ^- {(unit fact) game}
517 =+ next=(~(set bo a.mag) her)
519 [[~ %win ?:(w.mag %x %o)] nu]
520 [~ mag(w !w.mag, a z.mag, z next)]
528++ atom @ :: just an atom
529++ axis @ :: tree address
530++ bean ? :: 0=&=yes, 1=|=no
531++ bloq @ :: blockclass
532++ char @tD :: UTF-8 byte
533++ cord @t :: text atom (UTF-8)
534++ gate $-(* *) :: general gate
535++ knot @ta :: ASCII text
537++ null $~ :: null, nil, etc
538++ time @da :: galactic time
539++ path (list knot) :: eg, fs location
540++ tang (list tank) :: error trace
542 $% {$leaf p/tape} :: printing formats
543 $: $palm :: backstep list
544 p/{p/tape q/tape r/tape s/tape} ::
547 $: $rose :: flat list
548 p/{p/tape q/tape r/tape} :: mid open close
552++ tanq :: future tank
553 $? {$~ p/(list tanq)} :: list of printables
554 {$~ $~ p/tape} :: simple string
555 (pair @tas tanq) :: captioned
557++ tape (list char) :: string as list
558++ term @tas :: ascii symbol
559++ wall (list tape) :: text lines (no \n)
560++ wain (list cord) :: text lines (no \n)
565++ tree |* a/gate :: binary tree
566 $@($~ {n/a l/(tree a) r/(tree a)}) ::
567++ trap |*(a/_* _|?(*a)) :: makes perfect sense
568++ trel |* {a/gate b/gate c/gate} :: just a triple
570++ each |* {a/gate b/gate} :: either a or b
571 $%({$& p/a} {$| p/b}) :: a default
573++ qual |* {a/gate b/gate c/gate d/gate} :: just a quadruple
575++ pair |*({a/gate b/gate} {p/a q/b}) :: just a pair
576++ quid |*({a/gate b/*} {a _b}) :: for =^
577++ quip |*({a/gate b/*} {(list a) _b}) :: for =^
578++ unit |* a/gate :: maybe
580++ list |* a/gate :: null-term list
581 $@($~ {i/a t/(list a)}) ::
582++ lone |*(a/gate p/a) :: just one thing
586++ map |* {a/gate b/gate} :: associative tree
587 $@($~ {n/{p/a q/b} l/(map a b) r/(map a b)}) ::
588++ qeu |* a/gate :: queue
589 $@($~ {n/a l/(qeu a) r/(qeu a)}) ::
590++ set |* a/gate :: set
591 $@($~ {n/a l/(set a) r/(set a)}) ::
592++ jar |*({a/gate b/gate} (map a (list b))) :: map of lists
593++ 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