9Now we know enough to write two quick examples: the Sieve of
10Eratosthenes (a naive prime sieve) and a tictactoe engine.
12(Note that these are not examples of perfect Hoon code; they are
13meant for teaching. They err, more than usual for Hoon, on the
14side of simple manual construction over high-octane FP.)
16## `sieve`: code in basic style
18A function that produces the list of primes less than or equal to
19than its argument, an atom:
23:var field/(set atom) (silt (gulf 2 thru)) :: 3
27 (sort (~(tap in field) ~) lth) :: 7
30 :var factor/atom 2 :: 10
31 :loop :like ..main :: 11
32 :if (gth (mul factor factor) thru) :: 12
34 :moar(factor +(factor), ..main (reap factor)) :: 14
37 :gate factor/atom :: 17
38 :var count/atom (mul 2 factor) :: 18
39 :loop :like ..reap :: 19
40 :if (gth count thru) :: 20
43 count (add count factor) :: 23
44 field (~(del in field) count) :: 24
49## `sieve`: explanation
51Line `1`: we are building a gate (function) whose sample
52(argument) is `thru`, an atom.
54Line `2`: the product of this gate is a list of atoms.
56Line `3`: we introduce a variable `field`, whose type is a set of
57atoms. The `silt` function converts a list to a set. The `gulf`
58function creates the range `2` through `thru` inclusive, as a
61Line `4`: below, we introduce a core which does our work. With
62reverse flow, we compute `main` on this core, then `abet` on the
63product of `main`. (`abet:main` means `:rap(abet main)`.)
65The core is a state machine; `main` produces the core itself,
66with the computation completed. `abet` then extracts the result.
68Line `5`: we wrap a core around our existing subject. The
69`field` and `thru` atoms are within the payload and of course
72Lines `6` and `7`: the `abet` arm, which produces the field as a
73sorted list. `in` is the set core; `tap` is an arm which
74produces a gate that exports the set as a list, prepending it to
75a tail which is nil in this case (and most cases). We sort this
76list in ascending order with `lth` (less-than).
78Line `9`: the `main` arm, which performs the main calculation.
80Line `10`: we declare a counter, `factor`, which will iterate
81up through the factors we're removing.
83Line `11`: we start a loop. The product of the loop is cast to
84`..main`, which is the core we're in (`+1.main`).
86Lines `12` and `13`: we terminate the loop, returning `..main`,
87if the square of the counter exceeds the range.
89Line `14`: repeats the loop, incrementing the counter, and
90replacing the core with a version sieved by `reap`.
92Lines `16` and `17`: the `reap` arm, which produces a gate that
93removes multiples of a factor from the field (set of possible
94primes). The sample of this gate is the factor to remove.
96Line `18`: we declare a variable `count`, an atom, with the
97initial value `(mul 2 factor)`.
99Line `19`: we enter a loop. This loop produces the core itself.
101Lines `20` and `21`: we are done if the counter is out of range.
103Lines `22` to `25`: repeat the loop, stepping `count` to the next
104multiple of `factor`, and deleting it from `field`.
106## `sieve`: code in kernel style
108Finally, the same code transliterated into runes (and in a more
109concise "kernel" style).
114=+ fed=(silt (gulf 2 top))
117++ abet (sort (~(tap in fed)) lth)
121 ?: (gth (mul fac fac) top)
123 $(fac +(fac), ..main (reap fac))
131 $(cot (add cot fac), fed (~(del in fed) cot))
135### `toe`: tictactoe engine
137The `toe` tictactoe engine applies a list of events to a game
138state, producing a list of game actions. Events are tictactoe
139moves (`move)`; actions are tictactoe results (`fact`).
142:rap :gate feed/(list move) :: 1
144 :loop :cast (list fact) :: 3
146 :sip this/(unit fact) :: 5
148 (~(do go game) i.feed) :: 7
149 :var rest/(list fact) :moar(feed t.feed) :: 8
150 :ifno(this rest [u.this rest]) :: 9
154 ++ spot {x/atom y/atom} :: 13
155 ++ fact :book {$tie $~} :: 14
158 ++ move :book {$x p/spot} :: 17
162 ++ game :bank w/? :: 21
169 :door half/side :: 28
170 ++ bit :gate(a/atom =(1 (cut 0 [a 1] half))) :: 29
171 ++ off :gate(a/spot (add x.a (mul 3 y.a))) :: 30
172 ++ get :gate(a/spot (bit (off a))) :: 31
173 ++ set :gate(a/spot (con half (bex (off a)))) :: 32
174 ++ win :calt lien :: 33
175 (rip 4 0wl04h0.4A0Aw.4A00s.0e070) :: 34
176 :gate(a/atom =(a (dis a half))) :: 35
179 :door game/game :: 38
182 :cast {(unit fact) ^game} :: 41
184 {$x *} :sure(w.game ~(mo on p.act)) :: 43
185 {$o *} :deny(w.game ~(mo on p.act)) :: 44
189 ++ nu :like(game [& 0 0]) :: 48
191 :door here/spot :: 50
192 ++ is :or (~(get bo a.game) here) :: 51
193 (~(get bo z.game) here) :: 52
195 ++ mo :cast {(unit fact) ^game} :: 54
197 :var next/side (~(set bo a.game) here) :: 56
198 :if ~(win bo next) :: 57
199 [[~ %win ?:(w.game %x %o)] nu] :: 58
200 [~ game(w !w.game, a z.game, z next)] :: 59
206### `toe`: explanation
208Line `1` wraps a conclusion around a stack of cores. The cores
209at `11` and `26` are the data structures and functions of the
210engine; the conclusion is the interface and top-level logic. It
211produces a gate whose sample is `feed`, a list of `move`.
213Line `2` declares a blank new variable named `game`, whose type
214is the `game` mold (tictactoe game state) defined on line `21`.
215Under line `2`, `game` pulls the variable and `^game` the mold.
217Line `3` enters a loop producing a list of game events, `fact`.
219Line `4` produces nil, `~`, if `feed` is empty.
221Lines `5` through `7` apply the move. We open a `go` core on the
222current game state, produce a `do` gate, and call that gate on
223the move. The product of the `do` gate is a pair; the head is
224`(unit fact)`, either `~` or `[~ fact]`; the tail is a new
225`game`. The `:sip` twig declares the head as a new variable,
226`this`; it modifies the subject by setting `game` to the tail.
228Line `8` declares a new variable, `rest`, which is all the facts
229generated by the moves in `t.feed`, the tail of the move list.
230(Yes, this is gratuitous head recursion.) Line `9` produces
231`rest` if the `i.feed` move produced no fact; otherwise it
232prepends the new fact to `rest`.
234Line `11` wraps a library core around a data structure core, a
235common pattern. (You could put the molds and the gates in the
236same core, but the compiler couldn't constant-fold gate samples
237that use these molds, an efficiency cost.)
239Line `12` defines the `side` mold, an alias for `atom` (`@`).
240A side is a half of the board, as a marked/unmarked bitfield.
242Line `13` defines the `spot` mold, a 2D coordinate.
244Lines `14` to `16` define the `fact` mold, a game event (ie,
245output action), as a book (tagged union). There are two forms of
246`fact`: `[%tie ~]` (reporting a tie), and `[%win cord]`
247(reporting that `'X'` or `'O'` won a game.
249Lines `17` to `20` define the `move` mold, a game move. This is
250either `[%x spot]`, `[%o spot]`, or `[%z ~]` to reset the board.
252Lines `21` to `23` define the `game` mold, the game state. This
253is an `a` side, the next side to move; a `z` side, the other
254side; and `w`, which is `&` (yes) if `a` is X and `b` is O; `|`
255otherwise. (Obviously, we swap `a` and `z` on every move.)
257Line `26` introduces the library core, which contains the doors
258`bo` (for `side` bitfields) and `go` (for game state).
260Lines `27` to `28` begin the `bo` core, a door around `half`, a
263Line `29` is the `bit` gate, which tests a bit in `half`.
265Line `30` is the `off` gate, which maps from a `spot` (coordinate)
268Line `31` is the `get` gate, which produces the bit at a `spot`.
270Line `32` is the `set` gate, which produces a new side with the
271bit at a `spot` set. It works by ORing (`con`) `half` with the
272binary exponent (`bex`) of the offset (`off`) of the spot.
274Lines `33` to `35` are a victory test. It calls the
275standard `lien` function, which iterates a boolean gate over a
276list and produces `&` (yes) if any item tests as true, against a
277list of magic bitfields (expanded from a base64 constant), each
278of which must equal itself when ANDed (`dis`) with `half`.
280Lines `37` to `38` begin the `go` core, a door around a `game`.
281Again we name the sample `game`. (This is a legitimate style,
284Lines `40` to `42` begin the `do` gate, whose sample is `act`, a
285move. The product is cast to a pair of `(unit fact)` and a new
286game state, written `^game` to skip to the second binding.
288Lines `42` through `46` switch on the form of `act`. If it's an
289`%x` or `%o` move, we assert that it's the right turn, then use
290the `mo` door to apply the move. If it's a `%z` move, we clear
293Line `48` is the `nu` arm, which produces a blank game state.
295Lines `49` to `50` begin the `on` door, a door around `here`, a
296`spot`. Just as we have multiple computed attributes against the
297game state, we have multiple computed attributes (`is` and `mo`)
298against the combination of game state and coordinate.
300Lines `51` to `53` define the `is` arm, which is `&` (yes) iff
301either `a` or `z` has played `here`.
303Line `54` begins the `mo` arm, which produces (with a
304nonessential cast) a pair of `(unit fact)` and game state.
305Again we see `^game` to pull the mold, since `game` is the
306current game state in the `go` core.
308Line `55` asserts that no one has yet played `here`.
310Line `56` declares a variable `next`, as game side `a` with
313Lines `57` and `58` test if `a` has won with this move. If so,
314we produce the win fact and a blank game state.
316Line `59` produces no fact (since nothing has happened), but
317inverts the `w` turn indicator, replaces side `a` with the other
318side `z`, and replaces `z` with `next`.
320### `toe`: code, kernel style
327 =^ did/(unit fact) mag (~(do go mag) i.fed)
329 ?~(did res [u.did res])
336 ++ move $% {$x p/spot}
348 ++ bit |=(a/atom =(1 (cut 0 [a 1] haf)))
349 ++ get |=(a/spot (bit (off a)))
350 ++ off |=(a/spot (add x.a (mul 3 y.a)))
351 ++ set |=(a/spot (con haf (bex (off a))))
353 (rip 4 0wl04h0.4A0Aw.4A00s.0e070)
354 |=(a/atom =(a (dis a haf)))
360 ^- {(unit fact) game}
362 {$x *} ?>(w.mag ~(mo on p.act))
363 {$o *} ?<(w.mag ~(mo on p.act))
371 ++ is ?| (~(get bo a.mag) her)
372 (~(get bo z.mag) her)
374 ++ mo ^- {(unit fact) game}
376 =+ next=(~(set bo a.mag) her)
378 [[~ %win ?:(w.mag %x %o)] nu]
379 [~ mag(w !w.mag, a z.mag, z next)]
387++ atom @ :: just an atom
388++ axis @ :: tree address
389++ bean ? :: 0=&=yes, 1=|=no
390++ bloq @ :: blockclass
391++ char @tD :: UTF-8 byte
392++ cord @t :: text atom (UTF-8)
393++ gate $-(* *) :: general gate
394++ knot @ta :: ASCII text
396++ null $~ :: null, nil, etc
397++ time @da :: galactic time
398++ path (list knot) :: eg, fs location
399++ tang (list tank) :: error trace
401 $% {$leaf p/tape} :: printing formats
402 $: $palm :: backstep list
403 p/{p/tape q/tape r/tape s/tape} ::
406 $: $rose :: flat list
407 p/{p/tape q/tape r/tape} :: mid open close
411++ tanq :: future tank
412 $? {$~ p/(list tanq)} :: list of printables
413 {$~ $~ p/tape} :: simple string
414 (pair @tas tanq) :: captioned
416++ tape (list char) :: string as list
417++ term @tas :: ascii symbol
418++ wall (list tape) :: text lines (no \n)
419++ wain (list cord) :: text lines (no \n)
424++ tree |* a/gate :: binary tree
425 $@($~ {n/a l/(tree a) r/(tree a)}) ::
426++ trap |*(a/_* _|?(*a)) :: makes perfect sense
427++ trel |* {a/gate b/gate c/gate} :: just a triple
429++ each |* {a/gate b/gate} :: either a or b
430 $%({$& p/a} {$| p/b}) :: a default
432++ qual |* {a/gate b/gate c/gate d/gate} :: just a quadruple
434++ pair |*({a/gate b/gate} {p/a q/b}) :: just a pair
435++ quid |*({a/gate b/*} {a _b}) :: for =^
436++ quip |*({a/gate b/*} {(list a) _b}) :: for =^
437++ unit |* a/gate :: maybe
439++ list |* a/gate :: null-term list
440 $@($~ {i/a t/(list a)}) ::
441++ lone |*(a/gate p/a) :: just one thing
445++ map |* {a/gate b/gate} :: associative tree
446 $@($~ {n/{p/a q/b} l/(map a b) r/(map a b)}) ::
447++ qeu |* a/gate :: queue
448 $@($~ {n/a l/(qeu a) r/(qeu a)}) ::
449++ set |* a/gate :: set
450 $@($~ {n/a l/(set a) r/(set a)}) ::
451++ jar |*({a/gate b/gate} (map a (list b))) :: map of lists
452++ 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