on master help sign in
update ket runes and clarify =^ explanation
8fb9e52Tinnus Napbus
content/docs/hoon/reference/rune/ket.md+436−128
@@ -4,28 +4,56 @@ weight = 11
4template = "doc.html"
4template = "doc.html"
5aliases = ["docs/reference/hoon-expressions/rune/ket/"]
5aliases = ["docs/reference/hoon-expressions/rune/ket/"]
6+++
6+++
7−[`^-` ("kethep")](#kethep), [`^+` ("ketlus")](#ketlus), and
8−[`^=` ("kettis")](#kettis) let us adjust types without violating type
9−constraints.
10
7
8+[`^-` ("kethep")](#kethep), [`^+` ("ketlus")](#ketlus), and [`^=`
9+("kettis")](#kettis) let us adjust types without violating type constraints.
10+
11−The `nest` algorithm which tests subtyping is conservative;
11+The `nest` algorithm which tests subtyping is conservative; it never allows
12−it never allows invalid nests, it sometimes rejects valid nests.
12+invalid nests, it sometimes rejects valid nests.
13+
14+## `^|` "ketbar"
15+
16+Convert a gold core to an iron core (contravariant).
17+
18+#### Syntax
19+
20+One argument, fixed.
21+
22+<table>
23+<tr><th>Form</th><th>Syntax</th></tr>
24+<tr>
25+<td>Tall</td>
26+<td>
27+<pre>
28+^| p
29+</pre>
30+</td>
31+</tr>
32+<tr>
33+<td>Wide</td>
34+<td>
35+<pre>
36+^|(p)
37+</pre>
38+</td>
39+</tr>
40+<tr>
41+<td>Irregular</td>
42+<td>None.</td>
43+</tr>
44+</table>
45+
46+#### AST
13
47
14−## Runes
48+```hoon
15−
49+[%ktbr p=hoon]
16−### `^|` "ketbar"
50+```
17−
18−`[%ktbr p=hoon]`: convert a gold core to an iron core (contravariant).
19
51
20−##### Produces
52+#### Produces
21
53
22`p` as an iron core; crash if not a gold core.
54`p` as an iron core; crash if not a gold core.
23
55
24−##### Syntax
25−
26−Regular: **1-fixed**.
27−
28−##### Discussion
56+#### Discussion
29
57
30An iron core is an opaque function (gate or door).
58An iron core is an opaque function (gate or door).
31
59
@@ -37,7 +65,7 @@ Informally, a function fits an interface if the function has a
37more specific result and/or a less specific argument than the
65more specific result and/or a less specific argument than the
38interface.
66interface.
39
67
40−##### Examples
68+#### Examples
41
69
42The prettyprinter shows the core metal (`.` gold, `|` iron):
70The prettyprinter shows the core metal (`.` gold, `|` iron):
43
71
@@ -49,34 +77,68 @@ The prettyprinter shows the core metal (`.` gold, `|` iron):
49<1|gcq [@ @n <250.yur 41.wda 374.hzt 100.kzl 1.ypj %151>]>
77<1|gcq [@ @n <250.yur 41.wda 374.hzt 100.kzl 1.ypj %151>]>
50```
78```
51
79
80+---
81+
52−### `^:` "ketcol"
82+## `^:` "ketcol"
83+
84+Mold gate for type `p`.
85+
86+**Note this rune is now redundant.**
53
87
54−`[%ktcl p=spec]`: mold gate for type `p`.
88+#### Syntax
55
89
56−##### Produces
90+One argument, fixed.
57
91
58−A gate that returns the sample value if it's of the correct type, but crashes otherwise.
92+<table>
93+<tr><th>Form</th><th>Syntax</th></tr>
94+<tr>
95+<td>Tall</td>
96+<td>
97+<pre>
98+^: p
99+</pre>
100+</td>
101+</tr>
102+<tr>
103+<td>Wide</td>
104+<td>
105+<pre>
106+^:(p)
107+</pre>
108+</td>
109+</tr>
110+<tr>
111+<td>Irregular</td>
112+<td>None.</td>
113+</tr>
114+</table>
59
115
60−##### Syntax
116+#### AST
61
117
62−Regular: **1-fixed**.
118+```hoon
119+[%ktcl p=spec]
120+```
121+
122+#### Produces
63
123
64−##### Discussion
124+A gate that returns the sample value if it's of the correct type, but crashes
125+otherwise.
65
126
66−A mold is an idempotent gate that is guaranteed to produce a noun of that type.
127+#### Discussion
67−If an input value isn't of the correct type, the bunt value of that type is
68−returned. (See `^*`.)
69
128
70−`^:` is used to produce a gate that is much like a mold, except that instead of
129+`^:` is used to produce a mold that crashes if its sample is of the wrong type.
71−producing a bunt value when the input value is of the wrong type, it crashes.
130+
131+Molds used to produced their bunt value if they couldn't mold their sample. This
132+is no longer the case: molds now crash if molding fails, so **this rune is
133+redundant**.
72
134
73One may expect that `^:(path /foo)` would result in a syntax error since `^:`
135One may expect that `^:(path /foo)` would result in a syntax error since `^:`
74−only takes one child, but instead it will parse as `=< ^ %:(path /foo)`. Since
136+only takes one child, but instead it will parse as `=< ^ %:(path /foo)`. Since
75`:` is the irregular syntax for `=<` this is is parsed as "get `^` (i.e. the
137`:` is the irregular syntax for `=<` this is is parsed as "get `^` (i.e. the
76mold for cells) from a subject of `(path /foo)`", with `:` being the irregular
138mold for cells) from a subject of `(path /foo)`", with `:` being the irregular
77syntax for `=<`.
139syntax for `=<`.
78
140
79−##### Examples
141+#### Examples
80
142
81```
143```
82> ^: @
144> ^: @
@@ -94,31 +156,60 @@ syntax for `=<`.
94ford: %ride failed to execute:
156ford: %ride failed to execute:
95```
157```
96
158
159+---
160+
97−### `^.` "ketdot"
161+## `^.` "ketdot"
98−
162+
99−`[%ktdt p=hoon q=hoon]`: typecast on value produced by passing `q` to `p`.
163+Typecast on value produced by passing `q` to `p`.
100−
164+
101−##### Expands to
165+#### Syntax
166+
167+Two arguments, fixed.
168+
169+<table>
170+<tr><th>Form</th><th>Syntax</th></tr>
171+<tr>
172+<td>Tall</td>
173+<td>
174+<pre>
175+^. p
176+q
177+</pre>
178+</td>
179+</tr>
180+<tr>
181+<td>Wide</td>
182+<td>
183+<pre>
184+^.(p q)
185+</pre>
186+</td>
187+</tr>
188+<tr>
189+<td>Irregular</td>
190+<td>None.</td>
191+</tr>
192+</table>
193+
194+#### AST
102
195
103```hoon
196```hoon
104−^+(%:(p q) q)
197+[%ktdt p=hoon q=hoon]
105```
198```
106
199
107−##### Syntax
200+#### Expands to
108−
109−Regular: **2-fixed**.
110
201
111```hoon
202```hoon
112−^. p=hoon q=hoon
203+^+(%:(p q) q)
113```
204```
114
205
115−##### Discussion
206+#### Discussion
116
207
117`p` produces a gate and q is any Hoon expression.
208`p` produces a gate and q is any Hoon expression.
118
209
119−`^.` is particularly useful when `p` is a gate that 'cleans up' the type information about some piece of data. For example, `limo` is used to turn a raw noun of the appropriate shape into a genuine list. Hence we can use `^.` to cast with `limo` and similar gates, ensuring that the product has the desired type.
210+`^.` is particularly useful when `p` is a gate that 'cleans up' the type information about some piece of data. For example, `limo` is used to turn a raw noun of the appropriate shape into a genuine list. Hence we can use `^.` to cast with `limo` and similar gates, ensuring that the product has the desired type.
120
211
121−##### Examples
212+#### Examples
122
213
123```
214```
124> =mylist [11 22 33 ~]
215> =mylist [11 22 33 ~]
@@ -135,31 +226,66 @@ mint-vain
135~[22 33]
226~[22 33]
136```
227```
137
228
229+---
230+
138−### `^-` "kethep"
231+## `^-` "kethep"
232+
233+Typecast by explicit type label.
234+
235+#### Syntax
236+
237+Two arguments, fixed.
238+
239+<table>
240+<tr><th>Form</th><th>Syntax</th></tr>
241+<tr>
242+<td>Tall</td>
243+<td>
244+<pre>
245+^- p
246+q
247+</pre>
248+</td>
249+</tr>
250+<tr>
251+<td>Wide</td>
252+<td>
253+<pre>
254+^-(p q)
255+</pre>
256+</td>
257+</tr>
258+<tr>
259+<td>Irregular</td>
260+<td>
261+<pre>
262+`p`q
263+</pre>
264+</td>
265+</tr>
266+</table>
267+
268+#### AST
139
269
270+```hoon
140−`[%kthp p=spec q=hoon]`: typecast by explicit type label.
271+[%kthp p=spec q=hoon]
272+```
141
273
142−##### Expands to
274+#### Expands to
143
275
144```hoon
276```hoon
145^+(^*(p) q)
277^+(^*(p) q)
146```
278```
147
279
148−##### Syntax
149−
150−Regular: **2-fixed**.
151−
152−Irregular: `` `foo`baz`` is `^-(foo ^-(@ baz))`.
153−
154−##### Discussion
280+#### Discussion
155
281
156It's a good practice to put a `^-` ("kethep") at the top of every arm
282It's a good practice to put a `^-` ("kethep") at the top of every arm
157−(including gates, loops, etc). This cast is strictly necessary
283+(including gates, loops, etc). This cast is strictly necessary
158only in the presence of head recursion (otherwise you'll get a
284only in the presence of head recursion (otherwise you'll get a
159`rest-loop` error, or if you really screw up spectacularly an
285`rest-loop` error, or if you really screw up spectacularly an
160infinite loop in the compiler).
286infinite loop in the compiler).
161
287
162−##### Examples
288+#### Examples
163
289
164```
290```
165~zod:dojo> (add 90 7)
291~zod:dojo> (add 90 7)
@@ -179,44 +305,103 @@ infinite loop in the compiler).
179[~ ~.a]
305[~ ~.a]
180```
306```
181
307
182−### `^+` "ketlus"
308+## `^+` "ketlus"
309+
310+Typecast by inferred type.
311+
312+#### Syntax
313+
314+Two arguments, fixed.
315+
316+<table>
317+<tr><th>Form</th><th>Syntax</th></tr>
318+<tr>
319+<td>Tall</td>
320+<td>
321+<pre>
322+^+ p
323+q
324+</pre>
325+</td>
326+</tr>
327+<tr>
328+<td>Wide</td>
329+<td>
330+<pre>
331+^+(p q)
332+</pre>
333+</td>
334+</tr>
335+<tr><td>Irregular</td><td>None.</td></tr>
336+</table>
337+
338+#### AST
183
339
340+```hoon
184−`[%ktls p=hoon q=hoon]`: typecast by inferred type.
341+[%ktls p=hoon q=hoon]
185−
342+```
186−##### Produces
187−
188−The value of `q` with the type of `p`, if the type of `q` nests within the type of `p`. Otherwise, `nest-fail`.
189
343
190−##### Syntax
344+#### Produces
191
345
192−Regular: **2-fixed**.
346+The value of `q` with the type of `p`, if the type of `q` nests within the type
347+of `p`. Otherwise, `nest-fail`.
193
348
194−##### Examples
349+#### Examples
195
350
196```
351```
197~zod:dojo> ^+('text' %a)
352~zod:dojo> ^+('text' %a)
198'a'
353'a'
199```
354```
200
355
201−### `^&` "ketpam"
356+---
202
357
203−`[%ktpm p=hoon]`: convert a core to a zinc core (covariant).
358+## `^&` "ketpam"
204
359
205−##### Produces
360+Convert a core to a zinc core (covariant).
206
361
207−`p` as a zinc core; crash if `p` isn't a gold or zinc core.
362+#### Syntax
363+
364+One argument, fixed.
365+
366+<table>
367+<tr><th>Form</th><th>Syntax</th></tr>
368+<tr>
369+<td>Tall</td>
370+<td>
371+<pre>
372+^& p
373+</pre>
374+</td>
375+</tr>
376+<tr>
377+<td>Wide</td>
378+<td>
379+<pre>
380+^&(p)
381+</pre>
382+</td>
383+</tr>
384+<tr><td>Irregular</td><td>None.</td></tr>
385+</table>
386+
387+#### AST
208
388
209−##### Syntax
389+```hoon
390+[%ktpm p=hoon]
391+```
210
392
211−Regular: **1-fixed**.
393+#### Produces
394+
395+`p` as a zinc core; crash if `p` isn't a gold or zinc core.
212
396
213−##### Discussion
397+#### Discussion
214
398
215−A zinc core has a read-only sample and an opaque context. See [Advanced types](/docs/hoon/reference/advanced).
399+A zinc core has a read-only sample and an opaque context. See [Advanced types](/docs/hoon/reference/advanced).
216
400
217−##### Examples
401+#### Examples
218
402
219−The prettyprinter shows the core metal in the arm labels `1.xoz` and `1&xoz` below (`.` is gold, `&` is zinc):
403+The prettyprinter shows the core metal in the arm labels `1.xoz` and `1&xoz`
404+below (`.` is gold, `&` is zinc):
220
405
221```
406```
222> |=(@ 1)
407> |=(@ 1)
@@ -251,53 +436,112 @@ ford: %slim failed:
251ford: %ride failed to compute type:
436ford: %ride failed to compute type:
252```
437```
253
438
254−### `^~` "ketsig"
439+---
255
440
256−`[%ktsg p=hoon]`: fold constant at compile time.
441+## `^~` "ketsig"
257
442
258−##### Produces
443+Fold constant at compile time.
259
444
260−`p`, folded as a constant if possible.
445+#### Syntax
261
446
262−##### Syntax
447+One argument, fixed.
263
448
264−Regular: **1-fixed**.
449+<table>
450+<tr><th>Form</th><th>Syntax</th></tr>
451+<tr>
452+<td>Tall</td>
453+<td>
454+<pre>
455+^~ p
456+</pre>
457+</td>
458+</tr>
459+<tr>
460+<td>Wide</td>
461+<td>
462+<pre>
463+^~(p)
464+</pre>
465+</td>
466+</tr>
467+<tr><td>Irregular</td><td>None.</td></tr>
468+</table>
265
469
266−##### Examples
470+#### AST
267
471
472+```hoon
473+[%ktsg p=hoon]
268```
474```
269−~zod:dojo> (make '|-(42)')
270−[%8 p=[%1 p=[1 42]] q=[%9 p=2 q=[%0 p=1]]]
271
475
272−~zod:dojo> (make '^~(|-(42))')
476+#### Produces
273−[%1 p=42]
274−```
275
477
276−### `^*` "kettar"
478+`p`, folded as a constant if possible.
277
479
278−`[%kttr p=spec]`: Produce example type value.
480+#### Examples
279
481
280−##### Produces
482+```
483+> (make '|-(42)')
484+[%8 p=[%1 p=[1 42]] q=[%9 p=2 q=[%0 p=1]]]
281
485
282−A default value (i.e., 'bunt value') of the type `p`.
486+> (make '^~(|-(42))')
487+[%1 p=42]
488+```
283
489
490+---
491+
492+## `^*` "kettar"
493+
494+Produce example type value.
495+
284−##### Syntax
496+#### Syntax
497+
498+One argument, fixed.
499+
500+<table>
501+<tr><th>Form</th><th>Syntax</th></tr>
502+<tr>
503+<td>Tall</td>
504+<td>
505+<pre>
506+^* p
507+</pre>
508+</td>
509+</tr>
510+<tr>
511+<td>Wide</td>
512+<td>
513+<pre>
514+^*(p)
515+</pre>
516+</td>
517+</tr>
518+<tr>
519+<td>Irregular</td>
520+<td>
521+<pre>
522+*p
523+</pre>
524+</td>
525+</tr>
526+</table>
527+
528+`p` is any structure expression.
285
529
286−Regular: **1-fixed**.
530+#### AST
287
531
288```hoon
532```hoon
289−^* p=spec
533+[%kttr p=spec]
290```
534```
291
535
292−Irregular: `*p`.
536+#### Produces
293
537
294−`p` is any structure expression.
538+A default value (i.e., 'bunt value') of the type `p`.
295
539
296−##### Examples
540+#### Examples
297
541
298Regular:
542Regular:
299
543
300−```hoon
544+```
301> ^* @
545> ^* @
3020
5460
303
547
@@ -313,7 +557,7 @@ Regular:
313
557
314Irregular:
558Irregular:
315
559
316−```hoon
560+```
317> *@
561> *@
3180
5620
319
563
@@ -324,55 +568,119 @@ Irregular:
324""
568""
325```
569```
326
570
571+---
572+
327−### `^=` "kettis"
573+## `^=` "kettis"
328−
574+
329−`[%ktts p=skin q=hoon]`: Bind name to a value.
575+Bind name to a value.
330−
576+
331−##### Produces
577+#### Syntax
332−
578+
333−If `p` is a term, the product `q` with type `[%face p q]`. `p`
579+Two arguments, fixed.
334−may also be a tuple of terms, or a term-skin pair; the type of
580+
335−`q` must divide evenly into cells to match it.
581+<table>
582+<tr><th>Form</th><th>Syntax</th></tr>
583+<tr>
584+<td>Tall</td>
585+<td>
586+<pre>
587+^= p
588+q
589+</pre>
590+</td>
591+</tr>
592+<tr>
593+<td>Wide</td>
594+<td>
595+<pre>
596+^=(p q)
597+</pre>
598+</td>
599+</tr>
600+<tr>
601+<td>Irregular</td>
602+<td>
603+<pre>
604+p=q
605+</pre>
606+</td>
607+</tr>
608+</table>
609+
610+#### AST
336
611
337−##### Syntax
612+```hoon
613+[%ktts p=skin q=hoon]
614+```
338
615
339−Regular: **2-fixed**.
616+#### Produces
340
617
341−Irregular: `foo=baz` is `^=(foo baz)`.
618+If `p` is a term, the product `q` with type `[%face p q]`. `p` may also be a
619+tuple of terms, or a term-skin pair; the type of `q` must divide evenly into
620+cells to match it.
342
621
343−##### Examples
622+#### Examples
344
623
345```
624```
346−~zod:dojo> a=1
625+> a=1
347a=1
626a=1
348
627
349−~zod:dojo> ^= a
628+> ^= a
350− 1
629+ 1
351a=1
630a=1
352
631
353−~zod:dojo> ^=(a 1)
632+> ^=(a 1)
354a=1
633a=1
355
634
356−~zod:dojo> [b c d]=[1 2 3 4]
635+> [b c d]=[1 2 3 4]
357[b=1 c=2 d=[3 4]]
636[b=1 c=2 d=[3 4]]
358
637
359−~zod:dojo> [b c d=[x y]]=[1 2 3 4]
638+> [b c d=[x y]]=[1 2 3 4]
360[b=1 c=2 d=[x=3 y=4]]
639[b=1 c=2 d=[x=3 y=4]]
361```
640```
362
641
363−### `^?` "ketwut"
642+---
364
643
365−`[%ktwt p=hoon]`: convert any core to a lead core (bivariant).
644+## `^?` "ketwut"
366
645
367−##### Produces
646+Convert any core to a lead core (bivariant).
368
647
369−`p` as a lead core; crash if not a core.
648+#### Syntax
370
649
371−##### Syntax
650+One argument, fixed.
372
651
373−Regular: **1-fixed**.
652+<table>
653+<tr><th>Form</th><th>Syntax</th></tr>
654+<tr>
655+<td>Tall</td>
656+<td>
657+<pre>
658+^? p
659+</pre>
660+</td>
661+</tr>
662+<tr>
663+<td>Wide</td>
664+<td>
665+<pre>
666+^?(p)
667+</pre>
668+</td>
669+</tr>
670+<tr><td>Irregular</td><td>None.</td></tr>
671+</table>
374
672
673+#### AST
674+
675+```hoon
676+[%ktwt p=hoon]
677+```
678+
679+#### Produces
680+
681+`p` as a lead core; crash if not a core.
682+
375−##### Discussion
683+#### Discussion
376
684
377A lead core is an opaque generator; the payload can't be read or
685A lead core is an opaque generator; the payload can't be read or
378written.
686written.
@@ -383,14 +691,14 @@ Theorem: if type `x` nests within type `a`, a lead core producing
383Informally, a more specific generator can be used as a less
691Informally, a more specific generator can be used as a less
384specific generator.
692specific generator.
385
693
386−##### Examples
694+#### Examples
387
695
388The prettyprinter shows the core metal (`.` gold, `?` lead):
696The prettyprinter shows the core metal (`.` gold, `?` lead):
389
697
390```
698```
391−~zod:dojo> |=(@ 1)
699+> |=(@ 1)
392<1.gcq [@ @n <250.yur 41.wda 374.hzt 100.kzl 1.ypj %151>]>
700<1.gcq [@ @n <250.yur 41.wda 374.hzt 100.kzl 1.ypj %151>]>
393
701
394−~zod:dojo> ^?(|=(@ 1))
702+> ^?(|=(@ 1))
395<1?gcq [@ @n <250.yur 41.wda 374.hzt 100.kzl 1.ypj %151>]>
703<1?gcq [@ @n <250.yur 41.wda 374.hzt 100.kzl 1.ypj %151>]>
396```
704```
content/docs/hoon/reference/rune/tis.md+14−9
@@ -486,15 +486,20 @@ s
486
486
487#### Discussion
487#### Discussion
488
488
489−`p` is a new name (possibly with type annotation, e.g., `a=@`) of a value to be
489+- `p` is a new name (optionally with type) to pin to the subject.
490−pinned to the subject. The value of `p` is the head of the product of `r`. `q`
490+- `q` is the name of an existing wing of the subject.
491−is given the value of the tail of `r`'s product. Then `s` is evaluated against
491+- `r` is an expression that produces `[p-value new-q-value]`.
492−this new subject.
492+- `s` is some more code to be evaluted against the modified subject.
493−
493+
494−We generally use `=^` when we have a state machine with a function, `r`, that
494+This is a bit like doing `=/` and `=.` at the same time. It's useful for state
495−produces a cell, whose head is a result and whose tail is a new
495+machines, where you want to produce both effects and a new state. For example,
496−state. The head value is given a new name `p`, and the
496+many arms of a Gall agent produce `[effects new-state]` in the form of a `(quip card _this)`. In the `++on-poke` arm, you might have something like:
497−tail is stuffed back into wherever we stored the old state, `q`.
497+
498+```hoon
499+=^ cards state
500+ (handle-poke !<(action vase))
501+[cards this]
502+```
498
503
499This may also remind you of Haskell's State monad.
504This may also remind you of Haskell's State monad.
500
505