on master help sign in
Various fixes and improvements.
7b5629aC. Guy Yarvin
docs.mddeleted−13
@@ -1,13 +0,0 @@
1−---
2−anchor: none
3−footer: true
4−---
5−
6−<div class="lead">
7− <div class="logo inverse"></div>
8− <h1 class="blue i-b">Urbit docs</h1>
9−</div>
10−
11−<div class="sections">
12− <kids grid="true"></kids>
13−</div>
docs/about.md+1−1
@@ -4,4 +4,4 @@ sort: 1
4
4
5# About
5# About
6
6
7−<list />
7+<list/>
docs/about/overview.md+2−3
@@ -1,11 +1,10 @@
1---
1---
2next: true
2next: true
3sort: 1
3sort: 1
4−title: System overview
4+title: Technical overview
5−navhome: /main
6---
5---
7
6
8−# System overview
7+# Technical overview
9
8
10<div class="row"><div class="col-md-8">
9<div class="row"><div class="col-md-8">
11
10
docs/programming/runtime.md → docs/about/runtime.mdrenamed

The content is the same.

docs/programming/runtime/vere.md → docs/about/runtime/vere.mdrenamed

The content is the same.

docs/arvo.mdnew file+7
@@ -0,0 +1,7 @@
1+---
2+sort: 4
3+---
4+
5+# Arvo
6+
7+<list/>
docs/programming/arvo/internals.md → docs/arvo/internals.mdrenamed

The content is the same.

docs/programming/arvo/internals/ames.md → docs/arvo/internals/ames.mdrenamed

The content is the same.

docs/programming/arvo/internals/ames/commentary.md → docs/arvo/internals/ames/commentary.mdrenamed

The content is the same.

docs/programming/arvo/internals/behn.md → docs/arvo/internals/behn.mdrenamed

The content is the same.

docs/programming/arvo/internals/clay.md → docs/arvo/internals/clay.mdrenamed

The content is the same.

docs/programming/arvo/internals/clay/architecture.md → docs/arvo/internals/clay/architecture.mdrenamed

The content is the same.

docs/programming/arvo/internals/clay/commentary.md → docs/arvo/internals/clay/commentary.mdrenamed

The content is the same.

docs/programming/arvo/internals/dill.md → docs/arvo/internals/dill.mdrenamed

The content is the same.

docs/programming/arvo/internals/eyre.md → docs/arvo/internals/eyre.mdrenamed

The content is the same.

docs/programming/arvo/internals/eyre/commentary.md → docs/arvo/internals/eyre/commentary.mdrenamed

The content is the same.

docs/programming/arvo/internals/ford.md → docs/arvo/internals/ford.mdrenamed

The content is the same.

docs/programming/arvo/internals/ford/commentary.md → docs/arvo/internals/ford/commentary.mdrenamed

The content is the same.

docs/programming/arvo/internals/gall.md → docs/arvo/internals/gall.mdrenamed

The content is the same.

docs/programming/arvo/system.md → docs/arvo/system.mdrenamed

The content is the same.

docs/programming/arvo/system/api.md → docs/arvo/system/api.mdrenamed

The content is the same.

docs/programming/arvo/system/basic.md → docs/arvo/system/basic.mdrenamed

The content is the same.

docs/programming/arvo/system/generators.md → docs/arvo/system/generators.mdrenamed

The content is the same.

docs/programming/arvo/system/http.md → docs/arvo/system/http.mdrenamed

The content is the same.

docs/programming/arvo/system/marks.md → docs/arvo/system/marks.mdrenamed

The content is the same.

docs/programming/arvo/system/network.md → docs/arvo/system/network.mdrenamed

The content is the same.

docs/programming/arvo/system/security-driver.md → docs/arvo/system/security-driver.mdrenamed

The content is the same.

docs/programming/arvo/system/state.md → docs/arvo/system/state.mdrenamed

The content is the same.

docs/programming/arvo/system/subject.md → docs/arvo/system/subject.mdrenamed

The content is the same.

docs/programming/arvo/system/subscriptions.md → docs/arvo/system/subscriptions.mdrenamed

The content is the same.

docs/programming/arvo/system/web-apps.md → docs/arvo/system/web-apps.mdrenamed

The content is the same.

docs/hoon.mdnew file+7
@@ -0,0 +1,7 @@
1+---
2+sort: 3
3+---
4+
5+# Hoon
6+
7+<list/>
docs/programming/hoon/advanced.md → docs/hoon/advanced.mdrenamed+3−3
@@ -48,7 +48,7 @@ changed two stems: `$core` and `$face`. We added polymorphism to
48
48
49If cores never changed, we wouldn't need polymorphism. Of
49If cores never changed, we wouldn't need polymorphism. Of
50course, nouns are immutable and never change, but we use them as
50course, nouns are immutable and never change, but we use them as
51−templates to construct a new noun
51+templates to construct new nouns around.
52
52
53Suppose we take a core, a cell `{battery payload}`, and replace
53Suppose we take a core, a cell `{battery payload}`, and replace
54the payload with a different noun. Then, we invoke an arm from
54the payload with a different noun. Then, we invoke an arm from
@@ -103,8 +103,8 @@ just think about them intuitively.
103Each core has a "metal" `p.q` which defines its variance model.
103Each core has a "metal" `p.q` which defines its variance model.
104A core can be *invariant* (`$gold`), *bivariant* (`$lead`),
104A core can be *invariant* (`$gold`), *bivariant* (`$lead`),
105*covariant* (`$zinc`), or *contravariant* (`$iron`). The default
105*covariant* (`$zinc`), or *contravariant* (`$iron`). The default
106−is gold; a gold core can be converted to any metal, and any metal
106+is gold; a gold core can be cast or converted to any metal, and
107−can be converted to lead.
107+any metal can be cast or converted to lead.
108
108
109A gold core `a` has a read-write payload; another core `b` that
109A gold core `a` has a read-write payload; another core `b` that
110nests within it (ie, can be substituted for it) must be a gold
110nests within it (ie, can be substituted for it) must be a gold
docs/programming/hoon/basic.md → docs/hoon/basic.mdrenamed

The content is the same.

docs/programming/hoon/catalog.md → docs/hoon/catalog.mdrenamed

The content is the same.

docs/programming/hoon/concepts.md → docs/hoon/concepts.mdrenamed

The content is the same.

docs/programming/hoon/demo.md → docs/hoon/demo.mdrenamed

The content is the same.

docs/programming/hoon/examples.md → docs/hoon/examples.mdrenamed

The content is the same.

docs/programming/hoon/exercises.md → docs/hoon/exercises.mdrenamed

The content is the same.

docs/programming/hoon/library.md → docs/hoon/library.mdrenamed

The content is the same.

docs/programming/hoon/library/1a.md → docs/hoon/library/1a.mdrenamed

The content is the same.

docs/programming/hoon/library/1b.md → docs/hoon/library/1b.mdrenamed

The content is the same.

docs/programming/hoon/library/1c.md → docs/hoon/library/1c.mdrenamed

The content is the same.

docs/programming/hoon/library/2a.md → docs/hoon/library/2a.mdrenamed

The content is the same.

docs/programming/hoon/library/2b.md → docs/hoon/library/2b.mdrenamed

The content is the same.

docs/programming/hoon/library/2c.md → docs/hoon/library/2c.mdrenamed

The content is the same.

docs/programming/hoon/library/2d.md → docs/hoon/library/2d.mdrenamed

The content is the same.

docs/programming/hoon/library/2e.md → docs/hoon/library/2e.mdrenamed

The content is the same.

docs/programming/hoon/library/2f.md → docs/hoon/library/2f.mdrenamed

The content is the same.

docs/programming/hoon/library/2g.md → docs/hoon/library/2g.mdrenamed

The content is the same.

docs/programming/hoon/library/2h.md → docs/hoon/library/2h.mdrenamed

The content is the same.

docs/programming/hoon/library/2i.md → docs/hoon/library/2i.mdrenamed

The content is the same.

docs/programming/hoon/library/2j.md → docs/hoon/library/2j.mdrenamed

The content is the same.

docs/programming/hoon/library/2k.md → docs/hoon/library/2k.mdrenamed

The content is the same.

docs/programming/hoon/library/2l.md → docs/hoon/library/2l.mdrenamed

The content is the same.

docs/programming/hoon/library/2m.md → docs/hoon/library/2m.mdrenamed

The content is the same.

docs/programming/hoon/library/2n.md → docs/hoon/library/2n.mdrenamed

The content is the same.

docs/programming/hoon/library/2o.md → docs/hoon/library/2o.mdrenamed

The content is the same.

docs/programming/hoon/library/2p.md → docs/hoon/library/2p.mdrenamed

The content is the same.

docs/programming/hoon/library/2q.md → docs/hoon/library/2q.mdrenamed

The content is the same.

docs/programming/hoon/library/3a.md → docs/hoon/library/3a.mdrenamed

The content is the same.

docs/programming/hoon/library/3b.md → docs/hoon/library/3b.mdrenamed

The content is the same.

docs/programming/hoon/library/3c.md → docs/hoon/library/3c.mdrenamed

The content is the same.

docs/programming/hoon/library/3d.md → docs/hoon/library/3d.mdrenamed

The content is the same.

docs/programming/hoon/library/3e.md → docs/hoon/library/3e.mdrenamed

The content is the same.

docs/programming/hoon/library/3f.md → docs/hoon/library/3f.mdrenamed

The content is the same.

docs/programming/hoon/library/3g.md → docs/hoon/library/3g.mdrenamed

The content is the same.

docs/programming/hoon/library/4a.md → docs/hoon/library/4a.mdrenamed

The content is the same.

docs/programming/hoon/library/4b.md → docs/hoon/library/4b.mdrenamed

The content is the same.

docs/programming/hoon/library/4c.md → docs/hoon/library/4c.mdrenamed

The content is the same.

docs/programming/hoon/library/4d.md → docs/hoon/library/4d.mdrenamed

The content is the same.

docs/programming/hoon/library/4e.md → docs/hoon/library/4e.mdrenamed

The content is the same.

docs/programming/hoon/library/4f.md → docs/hoon/library/4f.mdrenamed

The content is the same.

docs/programming/hoon/library/4g.md → docs/hoon/library/4g.mdrenamed

The content is the same.

docs/programming/hoon/library/4h.md → docs/hoon/library/4h.mdrenamed

The content is the same.

docs/programming/hoon/library/4i.md → docs/hoon/library/4i.mdrenamed

The content is the same.

docs/programming/hoon/library/4j.md → docs/hoon/library/4j.mdrenamed

The content is the same.

docs/programming/hoon/library/4k.md → docs/hoon/library/4k.mdrenamed

The content is the same.

docs/programming/hoon/library/4l.md → docs/hoon/library/4l.mdrenamed

The content is the same.

docs/programming/hoon/library/4m.md → docs/hoon/library/4m.mdrenamed

The content is the same.

docs/programming/hoon/library/4n.md → docs/hoon/library/4n.mdrenamed

The content is the same.

docs/programming/hoon/library/4o.md → docs/hoon/library/4o.mdrenamed

The content is the same.

docs/programming/hoon/library/5a.md → docs/hoon/library/5a.mdrenamed

The content is the same.

docs/programming/hoon/library/5b.md → docs/hoon/library/5b.mdrenamed

The content is the same.

docs/programming/hoon/library/5c.md → docs/hoon/library/5c.mdrenamed

The content is the same.

docs/programming/hoon/library/5d.md → docs/hoon/library/5d.mdrenamed

The content is the same.

docs/programming/hoon/library/5e.md → docs/hoon/library/5e.mdrenamed

The content is the same.

docs/programming/hoon/library/5f.md → docs/hoon/library/5f.mdrenamed

The content is the same.

docs/programming/hoon/library/5g.md → docs/hoon/library/5g.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse.md → docs/hoon/library/zuse.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/crua.md → docs/hoon/library/zuse/core/crua.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/epur.md → docs/hoon/library/zuse/core/epur.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/fu.md → docs/hoon/library/zuse/core/fu.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/jo.md → docs/hoon/library/zuse/core/jo.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/poja.md → docs/hoon/library/zuse/core/poja.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/poxa.md → docs/hoon/library/zuse/core/poxa.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/poxo.md → docs/hoon/library/zuse/core/poxo.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/core/yu.md → docs/hoon/library/zuse/core/yu.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/diff.md → docs/hoon/library/zuse/diff.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/clan.md → docs/hoon/library/zuse/gate/clan.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/dawn.md → docs/hoon/library/zuse/gate/dawn.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/daws.md → docs/hoon/library/zuse/gate/daws.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/deal.md → docs/hoon/library/zuse/gate/deal.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/deft.md → docs/hoon/library/zuse/gate/deft.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/dust.md → docs/hoon/library/zuse/gate/dust.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/earl.md → docs/hoon/library/zuse/gate/earl.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/earn.md → docs/hoon/library/zuse/gate/earn.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/fain.md → docs/hoon/library/zuse/gate/fain.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/feel.md → docs/hoon/library/zuse/gate/feel.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/file.md → docs/hoon/library/zuse/gate/file.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/foal.md → docs/hoon/library/zuse/gate/foal.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/fray.md → docs/hoon/library/zuse/gate/fray.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/fuel.md → docs/hoon/library/zuse/gate/fuel.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/furl.md → docs/hoon/library/zuse/gate/furl.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/glam.md → docs/hoon/library/zuse/gate/glam.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/glon.md → docs/hoon/library/zuse/gate/glon.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/gnom.md → docs/hoon/library/zuse/gate/gnom.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/jape.md → docs/hoon/library/zuse/gate/jape.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/jesc.md → docs/hoon/library/zuse/gate/jesc.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/joba.md → docs/hoon/library/zuse/gate/joba.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/jobe.md → docs/hoon/library/zuse/gate/jobe.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/jone.md → docs/hoon/library/zuse/gate/jone.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/lead.md → docs/hoon/library/zuse/gate/lead.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/mang.md → docs/hoon/library/zuse/gate/mang.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/meat.md → docs/hoon/library/zuse/gate/meat.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/moon.md → docs/hoon/library/zuse/gate/moon.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/mung.md → docs/hoon/library/zuse/gate/mung.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/perk.md → docs/hoon/library/zuse/gate/perk.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/pojo.md → docs/hoon/library/zuse/gate/pojo.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/saxo.md → docs/hoon/library/zuse/gate/saxo.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/scanf.md → docs/hoon/library/zuse/gate/scanf.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/sein.md → docs/hoon/library/zuse/gate/sein.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/sifo.md → docs/hoon/library/zuse/gate/sifo.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/stud.md → docs/hoon/library/zuse/gate/stud.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/taco.md → docs/hoon/library/zuse/gate/taco.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/tact.md → docs/hoon/library/zuse/gate/tact.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/tame.md → docs/hoon/library/zuse/gate/tame.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/tell.md → docs/hoon/library/zuse/gate/tell.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/tome.md → docs/hoon/library/zuse/gate/tome.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/tope.md → docs/hoon/library/zuse/gate/tope.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/txml.md → docs/hoon/library/zuse/gate/txml.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/unt.md → docs/hoon/library/zuse/gate/unt.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/urld.md → docs/hoon/library/zuse/gate/urld.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/gate/urle.md → docs/hoon/library/zuse/gate/urle.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/mold/udal.md → docs/hoon/library/zuse/mold/udal.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/mold/udon.md → docs/hoon/library/zuse/mold/udon.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/mold/umph.md → docs/hoon/library/zuse/mold/umph.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/mold/unce.md → docs/hoon/library/zuse/mold/unce.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/mold/upas.md → docs/hoon/library/zuse/mold/upas.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/mold/urge.md → docs/hoon/library/zuse/mold/urge.mdrenamed

The content is the same.

docs/programming/hoon/library/zuse/rsa.md → docs/hoon/library/zuse/rsa.mdrenamed

The content is the same.

docs/programming/hoon/mission.md → docs/hoon/mission.mdrenamed+2−1
@@ -7,7 +7,8 @@ title: Mission
7# Mission
7# Mission
8
8
9Hoon is a strict, typed functional language which compiles itself
9Hoon is a strict, typed functional language which compiles itself
10−to Nock, a combinator interpreter whose spec fits on a T-shirt.
10+to [Nock](../../nock), a combinator interpreter whose spec fits
11+on a T-shirt.
11
12
12Hoon's design goal is to support higher-order typed functional
13Hoon's design goal is to support higher-order typed functional
13programming without category theory, formal logic, or other
14programming without category theory, formal logic, or other
docs/programming/hoon/syntax.md → docs/hoon/syntax.mdrenamed

The content is the same.

docs/programming/hoon/troubleshooting.md → docs/hoon/troubleshooting.mdrenamed

The content is the same.

docs/hoon/twig.mdnew file+75
@@ -0,0 +1,75 @@
1+---
2+sort: 6
3+next: true
4+title: Expressions
5+---
6+
7+# Expressions
8+
9+A Hoon expression is called a `twig`. A `twig` is an AST node:
10+the noun that the Hoon compiler makes when it parses a source
11+expression.
12+
13+The definition of `twig` is the definition of Hoon. So let's
14+define the syntax and semantics of Hoon, a twig at a time. But
15+first, basic twig anatomy.
16+
17+## Twig anatomy and jargon
18+
19+A twig is a tagged union (`book`) with the form `{stem bulb}`,
20+where the `stem` is an atomic symbol. The bulb for each stem has
21+its own type, which varies by stem. (Compare with Lisp, where
22+every AST node is a tag and a list of child nodes.)
23+
24+There are no user-level macros. All stems are built into Hoon.
25+Most stems are implemented as internal macros, however.
26+
27+The bulb is usually a tuple, sometimes a list or map. An element
28+of this structure is a *subexpression* or just *leg*.
29+
30+Usually a subexpression is itself a `twig`; sometimes it's an
31+`@tas` (symbol) or a `wing` (reference path).
32+
33+## Moldy moss and woody seed
34+
35+When defining each pages in the `twig` book, we never use `twig`
36+itself as a leg. Instead, we use the mold `moss` to describe
37+twigs whose product is used as a mold, and `seed` for twigs whose
38+product could be anything. Both `moss` and `seed` are just
39+aliases for `twig`, but they serve as documentation.
40+
41+For adjectives, we say *moldy* and *woody* respectively. An
42+example of X is the woody form of X; a mold producing X is its
43+moldy form.
44+
45+For instance, `[%foo %bar]` is woody: a twig producing the noun
46+`[7.303.014 7.496.034]`. `{$foo $bar}` is moldy: a twig
47+producing a function whose product is always `[7.303.014
48+7.496.034]`.
49+
50+This mold of course is a noun (a gate). The value of that noun
51+is not `[7.303.014 7.496.034]`, or anything remotely like it.
52+The noun is a pile of code. It is probably a big pile of code,
53+because most cores include a lot of formulas, contexts, etc,
54+generally including the whole standard library. (Having a
55+pointer to the standard library does not burden a noun much,
56+unless of course you're trying to print that noun and you don't
57+have a span that describes it.)
58+
59+And of course, Hoon is a functional language, so seeds sometimes
60+produce a mold -- because we do pass around functions as values.
61+Don't worry about this issue. It will totally make sense soon.
62+
63+But writing `[%foo %bar]` where you mean `{$foo $bar}`, or the
64+reverse, is a common beginner's mistake in Hoon. At least it
65+always results in a compiler error (since the noun types are so
66+different). There's really no way around learning not to make
67+this mistake the hard way, but designating subexpressions as
68+`moss` or `seed` can't hurt.
69+
70+## Twig categories
71+
72+Fortunately, the twigs fit neatly into clear categories which
73+match their rune prefix. Read forward by category:
74+
75+<div><list/></div>
docs/programming/hoon/twig/atom.md → docs/hoon/twig/atom.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core.md → docs/hoon/twig/bar-core.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core/cab-door.md → docs/hoon/twig/bar-core/cab-door.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core/cen-core.md → docs/hoon/twig/bar-core/cen-core.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core/dot-trap.md → docs/hoon/twig/bar-core/dot-trap.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core/hep-loop.md → docs/hoon/twig/bar-core/hep-loop.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core/tar-gill.md → docs/hoon/twig/bar-core/tar-gill.mdrenamed

The content is the same.

docs/programming/hoon/twig/bar-core/tis-gate.md → docs/hoon/twig/bar-core/tis-gate.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold.md → docs/hoon/twig/buc-mold.mdrenamed+1−1
@@ -3,7 +3,7 @@ sort: 7
3next: true
3next: true
4---
4---
5
5
6−# Molds (`$` runes).
6+# Molds (`$` runes)
7
7
8A *mold* is a `gate` (function) that helps us build simple and
8A *mold* is a `gate` (function) that helps us build simple and
9rigorous data structures. (In fact, since "mold" sounds nasty,
9rigorous data structures. (In fact, since "mold" sounds nasty,
docs/programming/hoon/twig/buc-mold/cab-shoe.md → docs/hoon/twig/buc-mold/cab-shoe.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/cen-book.md → docs/hoon/twig/buc-mold/cen-book.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/col-bank.md → docs/hoon/twig/buc-mold/col-bank.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/hep-lamb.md → docs/hoon/twig/buc-mold/hep-lamb.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/ket-bush.md → docs/hoon/twig/buc-mold/ket-bush.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/pat-claw.md → docs/hoon/twig/buc-mold/pat-claw.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/tis-coat.md → docs/hoon/twig/buc-mold/tis-coat.mdrenamed

The content is the same.

docs/programming/hoon/twig/buc-mold/wut-pick.md → docs/hoon/twig/buc-mold/wut-pick.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call.md → docs/hoon/twig/cen-call.mdrenamed+1−1
@@ -3,7 +3,7 @@ sort: 6
3next: true
3next: true
4---
4---
5
5
6−# Full make (`%` runes)
6+# Make and call (`%` runes)
7
7
8We've already covered the irregular-only twigs `$wing` and
8We've already covered the irregular-only twigs `$wing` and
9`$limb`. These are simple forms of the *invocation* family of
9`$limb`. These are simple forms of the *invocation* family of
docs/programming/hoon/twig/cen-call/cab-keep.md → docs/hoon/twig/cen-call/cab-keep.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call/dot-lace.md → docs/hoon/twig/cen-call/dot-lace.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call/hep-call.md → docs/hoon/twig/cen-call/hep-call.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call/ket-calq.md → docs/hoon/twig/cen-call/ket-calq.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call/lus-calt.md → docs/hoon/twig/cen-call/lus-calt.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call/sig-open.md → docs/hoon/twig/cen-call/sig-open.mdrenamed

The content is the same.

docs/programming/hoon/twig/cen-call/tis-make.md → docs/hoon/twig/cen-call/tis-make.mdrenamed

The content is the same.

docs/programming/hoon/twig/col-cell.md → docs/hoon/twig/col-cell.mdrenamed+2−1
@@ -6,7 +6,8 @@ next: true
6# Cell construction (`:` runes)
6# Cell construction (`:` runes)
7
7
8The cell twigs, `$cons` and friends, are simple and regular.
8The cell twigs, `$cons` and friends, are simple and regular.
9−All `:` twigs expand to `$cons`.
9+All `:` twigs expand to `$cons`, which makes a pair (just like
10+the Lisp `cons` operator).
10
11
11## Twigs
12## Twigs
12
13
docs/programming/hoon/twig/col-cell/cab-scon.md → docs/hoon/twig/col-cell/cab-scon.mdrenamed

The content is the same.

docs/programming/hoon/twig/col-cell/hep-cons.md → docs/hoon/twig/col-cell/hep-cons.mdrenamed

The content is the same.

docs/programming/hoon/twig/col-cell/ket-conq.md → docs/hoon/twig/col-cell/ket-conq.mdrenamed

The content is the same.

docs/programming/hoon/twig/col-cell/lus-cont.md → docs/hoon/twig/col-cell/lus-cont.mdrenamed

The content is the same.

docs/programming/hoon/twig/col-cell/sig-conl.md → docs/hoon/twig/col-cell/sig-conl.mdrenamed

The content is the same.

docs/programming/hoon/twig/col-cell/tar-conp.md → docs/hoon/twig/col-cell/tar-conp.mdrenamed

The content is the same.

docs/programming/hoon/twig/dot-nock.md → docs/hoon/twig/dot-nock.mdrenamed

The content is the same.

docs/programming/hoon/twig/dot-nock/ket-wish.md → docs/hoon/twig/dot-nock/ket-wish.mdrenamed

The content is the same.

docs/programming/hoon/twig/dot-nock/lus-bump.md → docs/hoon/twig/dot-nock/lus-bump.mdrenamed

The content is the same.

docs/programming/hoon/twig/dot-nock/tar-nock.md → docs/hoon/twig/dot-nock/tar-nock.mdrenamed

The content is the same.

docs/programming/hoon/twig/dot-nock/tis-same.md → docs/hoon/twig/dot-nock/tis-same.mdrenamed

The content is the same.

docs/programming/hoon/twig/dot-nock/wut-deep.md → docs/hoon/twig/dot-nock/wut-deep.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast.md → docs/hoon/twig/ket-cast.mdrenamed+1−1
@@ -3,7 +3,7 @@ sort: 9
3next: true
3next: true
4---
4---
5
5
6−# Type manipulation (`^` runes).
6+# Type manipulation (`^` runes)
7
7
8`$cast`, `$like`, and `$name` let us adjust spans without
8`$cast`, `$like`, and `$name` let us adjust spans without
9violating type constraints.
9violating type constraints.
docs/programming/hoon/twig/ket-cast/bar-iron.md → docs/hoon/twig/ket-cast/bar-iron.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast/hep-cast.md → docs/hoon/twig/ket-cast/hep-cast.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast/lus-like.md → docs/hoon/twig/ket-cast/lus-like.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast/pam-zinc.md → docs/hoon/twig/ket-cast/pam-zinc.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast/sig-burn.md → docs/hoon/twig/ket-cast/sig-burn.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast/tis-name.md → docs/hoon/twig/ket-cast/tis-name.mdrenamed

The content is the same.

docs/programming/hoon/twig/ket-cast/wut-lead.md → docs/hoon/twig/ket-cast/wut-lead.mdrenamed

The content is the same.

docs/programming/hoon/twig/limb.md → docs/hoon/twig/limb.mdrenamed

The content is the same.

docs/programming/hoon/twig/sem-make.md → docs/hoon/twig/sem-make.mdrenamed+1−1
@@ -3,7 +3,7 @@ sort: 11
3next: true
3next: true
4---
4---
5
5
6−# Macros (`;` runes)
6+# Random macros (`;` runes)
7
7
8Miscellaneous useful macros.
8Miscellaneous useful macros.
9
9
docs/programming/hoon/twig/sem-make/col-wad.md → docs/hoon/twig/sem-make/col-wad.mdrenamed

The content is the same.

docs/programming/hoon/twig/sem-make/fas-nub.md → docs/hoon/twig/sem-make/fas-nub.mdrenamed

The content is the same.

docs/programming/hoon/twig/sem-make/sem-fry.md → docs/hoon/twig/sem-make/sem-fry.mdrenamed

The content is the same.

docs/programming/hoon/twig/sem-make/sig-dip.md → docs/hoon/twig/sem-make/sig-dip.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint.md → docs/hoon/twig/sig-hint.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/bar-show.md → docs/hoon/twig/sig-hint/bar-show.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/buc-poll.md → docs/hoon/twig/sig-hint/buc-poll.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/cab-lurk.md → docs/hoon/twig/sig-hint/cab-lurk.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/cen-fast.md → docs/hoon/twig/sig-hint/cen-fast.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/fas-funk.md → docs/hoon/twig/sig-hint/fas-funk.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/gal-thin.md → docs/hoon/twig/sig-hint/gal-thin.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/gar-hint.md → docs/hoon/twig/sig-hint/gar-hint.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/lus-memo.md → docs/hoon/twig/sig-hint/lus-memo.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/pam-dump.md → docs/hoon/twig/sig-hint/pam-dump.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/tis-ddup.md → docs/hoon/twig/sig-hint/tis-ddup.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/wut-warn.md → docs/hoon/twig/sig-hint/wut-warn.mdrenamed

The content is the same.

docs/programming/hoon/twig/sig-hint/zap-peep.md → docs/hoon/twig/sig-hint/zap-peep.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow.md → docs/hoon/twig/tis-flow.mdrenamed+2
@@ -26,6 +26,8 @@ Another way to change the subject is to mutate it. With the
26instead of reading, *installing* a new value at that leg. Of
26instead of reading, *installing* a new value at that leg. Of
27course, we are creating a copy, not modifying the original.
27course, we are creating a copy, not modifying the original.
28
28
29+There are many flow stems, all small variations on these three.
30+
29## Twigs
31## Twigs
30
32
31<list dataPreview="true" className="runes" linkToFragments="true"></list>
33<list dataPreview="true" className="runes" linkToFragments="true"></list>
docs/programming/hoon/twig/tis-flow/bar-new.md → docs/hoon/twig/tis-flow/bar-new.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/col-fix.md → docs/hoon/twig/tis-flow/col-fix.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/dot-set.md → docs/hoon/twig/tis-flow/dot-set.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/fas-var.md → docs/hoon/twig/tis-flow/fas-var.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/gal-rap.md → docs/hoon/twig/tis-flow/gal-rap.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/gar-per.md → docs/hoon/twig/tis-flow/gar-per.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/hep-nip.md → docs/hoon/twig/tis-flow/hep-nip.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/ket-sip.md → docs/hoon/twig/tis-flow/ket-sip.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/lus-pin.md → docs/hoon/twig/tis-flow/lus-pin.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/sem-rev.md → docs/hoon/twig/tis-flow/sem-rev.mdrenamed

The content is the same.

docs/programming/hoon/twig/tis-flow/tar-aka.md → docs/hoon/twig/tis-flow/tar-aka.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test.md → docs/hoon/twig/wut-test.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/bar-or.md → docs/hoon/twig/wut-test/bar-or.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/col-if.md → docs/hoon/twig/wut-test/col-if.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/dot-lest.md → docs/hoon/twig/wut-test/dot-lest.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/gal-deny.md → docs/hoon/twig/wut-test/gal-deny.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/gar-sure.md → docs/hoon/twig/wut-test/gar-sure.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/hep-case.md → docs/hoon/twig/wut-test/hep-case.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/ket-ifcl.md → docs/hoon/twig/wut-test/ket-ifcl.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/lus-deft.md → docs/hoon/twig/wut-test/lus-deft.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/pam-and.md → docs/hoon/twig/wut-test/pam-and.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/pat-ifat.md → docs/hoon/twig/wut-test/pat-ifat.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/sig-ifno.md → docs/hoon/twig/wut-test/sig-ifno.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/tis-fits.md → docs/hoon/twig/wut-test/tis-fits.mdrenamed

The content is the same.

docs/programming/hoon/twig/wut-test/zap-not.md → docs/hoon/twig/wut-test/zap-not.mdrenamed

The content is the same.

docs/programming/hoon/twig/zap-wild.md → docs/hoon/twig/zap-wild.mdrenamed+1−1
@@ -3,7 +3,7 @@ sort: 13
3next: true
3next: true
4---
4---
5
5
6−# Special tools (`!` runes).
6+# Special tools (`!` runes)
7
7
8## Twigs
8## Twigs
9
9
docs/programming/hoon/twig/zap-wild/gar-wrap.md → docs/hoon/twig/zap-wild/gar-wrap.mdrenamed

The content is the same.

docs/programming/hoon/twig/zap-wild/tis-code.md → docs/hoon/twig/zap-wild/tis-code.mdrenamed

The content is the same.

docs/programming/hoon/twig/zap-wild/wut-need.md → docs/hoon/twig/zap-wild/wut-need.mdrenamed

The content is the same.

docs/programming/hoon/twig/zap-wild/zap-fail.md → docs/hoon/twig/zap-wild/zap-fail.mdrenamed

The content is the same.

docs/nock.mdnew file+8
@@ -0,0 +1,8 @@
1+---
2+sort: 5
3+title: Nock
4+---
5+
6+# Nock
7+
8+<list/>
docs/programming/nock/definition.md → docs/nock/definition.mdrenamed

The content is the same.

docs/nock/example.mdnew file+364
@@ -0,0 +1,364 @@
1+---
2+sort: 3
3+next: true
4+title: Example
5+---
6+
7+# Nock by hand
8+
9+Some people write assembly language by hand. Some of them have a
10+good reason to do so. But there's no good reason to write Nock by
11+hand, ever.
12+
13+Except for the purposes of learning Nock. It's not necessary to
14+know Nock to program in Hoon. But it's quite desirable. The
15+feeling of understanding the system at two levels is pleasant.
16+
17+## Decrement in Nock
18+
19+A good practice exercise for Nock is a decrement formula. Ie, a
20+formula `f` which implements the partial function that produces
21+`(s - 1)` if `s` is a nonzero atom, and otherwise does not
22+terminate.
23+
24+As we know, the equivalent formula for increment is
25+
26+```
27+[4 0 1]
28+```
29+
30+Thus:
31+
32+```
33+~>zod:dojo .*(42 [4 0 1])
34+43
35+```
36+
37+Of course, increment is built into Nock. So, ha, that's easy.
38+
39+The best way to learn Nock is to *stop reading right now*, and
40+go write your own decrement formula. There's no substitute for
41+doing it yourself. But the second best way is faster...
42+
43+## Decrement in Hoon
44+
45+How do we decrement? A good way to start is to gaze fondly on
46+how we'd do it if we actually had a real language, ie, Hoon.
47+Here is a minimal decrement in Hoon:
48+
49+```
50+=> a=. :: line 1
51+=+ b=0 :: line 2
52+|- :: line 3
53+?: =(a +(b)) :: line 4
54+ b :: line 5
55+$(b +(b)) :: line 6
56+```
57+
58+Or for fun, on one line:
59+
60+```
61+=>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b))))))
62+```
63+
64+Does Hoon actually work?
65+
66+```
67+~zod:dojo> =>(42 =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b)))))))
68+41
69+```
70+
71+Let's translate this into English. How do we decrement the
72+subject? First (line 1), we rename the subject `a`. Second
73+(line 2), we add a variable, `b`, an atom with value `0`.
74+Third (line 3), we loop. Fourth, we test if `a` equals `b` plus
75+1 (line 4), produce `b` if it does (line 5), repeat the loop with
76+`b` set to `b` plus 1 (line 6) if it doesn't. Obviously, while
77+the syntax is unusual, the algorithm is anything but deep. We
78+are calculating `b` minus one by counting up from `0`.
79+
80+(Obviously, this is an O(n) algorithm. Is there a better way?
81+There is not. Do we actually do this in practice? Yes and no.)
82+
83+Unfortunately we are missing a third of our Rosetta stone. We
84+have decrement in Hoon and we have it in English. How do we
85+express this in Nock? What will the Hoon compiler generate from
86+the code above? Let's work through it line by line.
87+
88+Nock has no types, variable names, etc. So line 1 is a no-op.
89+
90+How do we add a variable (line 2)? We compute a new subject,
91+which is a cell of the present subject and the variable. With
92+this new subject, we execute another formula.
93+
94+Since `0` is a constant, a formula that produces it is
95+
96+```
97+[1 0]
98+```
99+
100+To combine `0` with the subject, we compute
101+
102+```
103+[[1 0] [0 1]]
104+```
105+
106+which, if our subject is 42, gives us
107+
108+```
109+[0 42]
110+```
111+
112+which we can use as the subject for an inner formula, `g`.
113+Composing our new variable with `g`, we have `f` as
114+
115+```
116+[2 [[1 0] [0 1]] [1 g]]
117+```
118+
119+which seems a little funky for something so simple. But we
120+can simplify it with the composition macro, `7`:
121+
122+```
123+[7 [[1 0] [0 1]] g]
124+```
125+
126+and still further with the augmentation macro, `8`:
127+
128+```
129+[8 [1 0] g]
130+```
131+
132+If you refer back to the Nock definition, you'll see that all
133+these formulas are semantically equivalent.
134+
135+Let's continue with our decrement. So what's `g`? We seem to
136+loop. Does Nock have a loop instruction? It most certainly does
137+not. So what do we do?
138+
139+We build a noun called a *core* - a construct which is behind any
140+kind of interesting control flow in Hoon. Of course, the Nock
141+programmer is not constrained to use the same techniques as the
142+Hoon compiler, but it is probably a good idea.
143+
144+In Hoon, all the flow structures from your old life as an Earth
145+programmer become cores. Functions and/or closures are cores,
146+objects are cores modules are cores, even loops are cores.
147+
148+The core is just a cell whose tail is data (possibly containing
149+other cores) and whose head is code (containing one or more
150+formulas). The tail is the *payload* and the head is the
151+*battery*. Hence your core is
152+
153+```
154+[bat pay]
155+```
156+
157+To activate a core, pick a formula out of the battery, and use
158+the entire core (*not* just the payload) as the subject.
159+
160+(A core formula is called an *arm*. An arm is almost like an
161+object-oriented method, but not quite - a method would be an arm
162+that produces a function on an argument. The arm is just a
163+function of the core, ie, a computed attribute.)
164+
165+Of course, because we feed it the entire core, our arm can
166+invoke itself (or any other formula in the battery). Hence, it
167+can loop. And this is what a loop is - the simplest of cores.
168+
169+We need to do two things with this core: create it, and activate
170+it. To be precise, we need two formulas: a formula which
171+produces the core, and one which activates its subject. We can
172+compose these functions with the handy `7` instruction:
173+
174+```
175+[8 [1 0] [7 p a]]
176+```
177+
178+`p` produces our core, `a` activates it. Let's take these in
179+reverse order. How do we activate a core?
180+
181+Since we have only one formula, it's the battery itself.
182+Thus we want to execute Nock with the whole core (already the
183+subject, and the entire battery (slot `2`). Hence, `a` is
184+
185+```
186+[2 [0 1] [0 2]]
187+```
188+
189+We could also use the handy `9` macro - which almost seems
190+designed for firing arms on cores:
191+
192+```
193+[9 2 [0 1]]
194+```
195+
196+Which leaves us seeking
197+
198+```
199+[8 [1 0] [7 p [9 2 0 1]]]
200+```
201+
202+And all we have to do is build the core, `p`. How do we build a
203+core? We add code to the subject, just as we added a variable
204+above. The initial value of our counter was a constant, `0`.
205+The initial (and permanent) value of our battery is a constant,
206+the loop formula `l`. So `p` is
207+
208+```
209+[8 [1 l] [0 1]]
210+```
211+
212+Which would leave us seeking
213+
214+```
215+[8 [1 0] [7 [8 [1 l] [0 1]] [9 2 0 1]]]
216+```
217+
218+except that we have duplicated the `8` pattern again, since we
219+know
220+
221+```
222+[7 [8 [1 l] [0 1]] [9 2 0 1]]
223+```
224+
225+is equivalent to
226+
227+```
228+[8 [1 l] [9 2 0 1]]
229+```
230+
231+so the full value of `f` is
232+
233+```
234+[8 [1 0] [8 [1 l] [9 2 0 1]]]
235+```
236+
237+Thus our only formula to compose is the loop body, `l`.
238+Its subject is the loop core:
239+
240+```
241+[bat pay]
242+```
243+
244+where `bat` is just the loop formula, and `pay` is the pair `[a
245+b]`, `a` being the input subject, and `b` the counter. Thus we
246+could also write this subject as
247+
248+```
249+[l b a]
250+```
251+
252+and we see readily that `a` is at slot `7`, `b` `6`, `l` `2`.
253+With this subject, we need to express the Hoon loop body
254+
255+```
256+?: =(a +(b)) :: line 4
257+ b :: line 5
258+$(b +(b)) :: line 6
259+```
260+
261+This is obviously an if statement, and it calls for `6`. Ie:
262+
263+```
264+[6 t y n]
265+```
266+
267+Giving our decrement program as:
268+
269+```
270+[8 [1 0] [8 [1 6 t y n] [9 2 0 1]]]
271+```
272+
273+For `t`, how do we compute a flag that is yes (`0`) if `a` equals
274+`b` plus one? Equals, we recall, is `5`. So `t` can only be
275+
276+```
277+[5 [0 7] [4 0 6]]
278+```
279+
280+If so, our product `y` is just the counter `b`:
281+
282+```
283+[0 6]
284+```
285+
286+And if not? We have to re-execute the loop with the counter
287+incremented. If we were executing it with the same counter,
288+obviously an infinite loop, we could use the same core:
289+
290+```
291+[9 2 0 1]
292+```
293+
294+But instead we need to construct a new core with the counter
295+incremented:
296+
297+```
298+[l +(b) a]
299+```
300+
301+ie,
302+
303+```
304+[[0 2] [4 0 6] [0 7]]
305+```
306+
307+and `n` is:
308+
309+```
310+[9 2 [[0 2] [4 0 6] [0 7]]]
311+```
312+
313+Hence our complete decrement. Let's reformat vertically so we
314+can actually read it:
315+
316+```
317+[8
318+ [1 0]
319+ [ 8
320+ [ 1
321+ [ 6
322+ t
323+ y
324+ n
325+ ]
326+ ]
327+ [9 2 0 1]
328+ ]
329+]
330+```
331+
332+which becomes
333+
334+```
335+[8
336+ [1 0]
337+ [ 8
338+ [ 1
339+ [ 6
340+ [5 [0 7] [4 0 6]]
341+ [0 6]
342+ [9 2 [[0 2] [4 0 6] [0 7]]]
343+ ]
344+ ]
345+ [9 2 0 1]
346+ ]
347+]
348+```
349+
350+or, on one line without superfluous brackets:
351+
352+```
353+[8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1]
354+```
355+
356+which works for the important special case, 42:
357+
358+```
359+~zod:dojo> .*(42 [8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1])
360+41
361+```
362+
363+If you understood this, you understand Nock. At least in principle!
364+Now, go write an adder. Or don't.
docs/nock/explanation.mdnew file+561
@@ -0,0 +1,561 @@
1+---
2+sort: 2
3+next: true
4+title: Explanation
5+---
6+
7+# An explanation of Nock
8+
9+To make Nock make sense, let's work through the spec line by line.
10+First the data model:
11+
12+## Nouns
13+
14+```
15+A noun is an atom or a cell. An atom is any natural number.
16+A cell is any ordered pair of nouns.
17+```
18+
19+Nouns are the dumbest data model ever. Nouns make JSON look like
20+XML and XML look like ASN.1. It may also remind you of Lisp's
21+S-expressions - you can think of nouns as "S-expressions without
22+the S."
23+
24+To be exact, a noun *is* an S-expression, except that classic
25+S-expressions have multiple atom types ("S" is for "symbol").
26+Since Nock is designed to be used with a higher-level type system
27+(such as Hoon's), it does not need low-level types. An atom is
28+just an unsigned integer of any size.
29+
30+For instance, it's common to represent strings (or even whole
31+text files) as atoms, arranging them LSB first - so "foo" becomes
32+`0x6f6f66`. How do we know to print this as "foo", not `0x6f6f66`?
33+We need external information - such as a Hoon type. Similarly,
34+other common atomic types - signed integers, floating point, etc
35+- are all straightforward to map into atoms.
36+
37+It's also important to note that, unlike Lisp, Nock cannot create
38+cyclical data structures. It is normal and common for nouns in a
39+Nock runtime system to have acyclic structure - shared subtrees.
40+But there is no Nock computation that can make a child point to
41+its parent. One consequence: Nock has no garbage collector.
42+(Nor can dag structure be detected, as with Lisp `eq`.)
43+
44+There is also no single syntax for nouns. If you have nouns you
45+have Nock; if you have Nock you have Hoon; if you have Hoon, you
46+can write whatever parser you like.
47+
48+## Reductions
49+
50+It's important to recognize that the pseudocode of the Nock spec
51+is just that: pseudocode. It looks a little like Hoon. It isn't
52+Hoon - it's just pseudocode. Or in other words, just English.
53+At the bottom of every formal system is a system of axioms, which
54+can only be written in English. (Why pseudocode, not Hoon? Since
55+Hoon is defined in Nock, this would only give a false impression
56+of nonexistent precision.)
57+
58+The logic of this pseudocode is a pattern-matching reduction,
59+matching from the top down. To compute Nock, repeatedly reduce
60+with the first line that matches. Let's jump right in!
61+
62+### Nock principles
63+
64+```
65+nock(a) *a
66+```
67+
68+Nock is a pure (stateless) function from noun to noun. In our
69+pseudocode (and only in our pseudocode) we express this with the
70+prefix operator `*`.
71+
72+This function is defined for every noun, but on many nouns it
73+does nothing useful. For instance, if `a` is an atom, `*a`
74+reduces to... `*a`. In theory, this means that Nock spins
75+forever in an infinite loop. In other words, Nock produces no
76+result - and in practice, your interpreter will stop.
77+
78+(Another way to see this is that Nock has "crash-only" semantics.
79+There is no exception mechanism. The only way to catch Nock
80+errors is to simulate Nock in a higher-level virtual Nock -
81+which, in fact, we do all the time. A simulator (or a practical
82+low-level interpreter) can report, out of band, that Nock would
83+not terminate. It cannot recognize all infinite loops, of
84+course, but it can catch the obvious ones - like `*42`.)
85+
86+Normally `a` in `nock(a)` is a cell `[s f]`, or as we say
87+
88+```
89+[subject formula]
90+```
91+
92+Intuitively, the formula is your function and the subject is
93+its argument. We call them something different because Hoon,
94+or any other high-level language built on Nock, will build its
95+own function calling convention which *does not* map directly
96+to `*[subject formula]`.
97+
98+### Noun syntax
99+
100+```
101+[a b c] [a [b c]]
102+```
103+
104+Ie, brackets (in our pseudocode, as in Hoon) associate to the
105+right. For those with Lisp experience, it's important to note
106+that Nock and Hoon use tuples or "improper lists" much more
107+heavily than Lisp. The list terminator, normally 0, is never
108+automatic. So the Lisp list
109+
110+```
111+(a b c)
112+```
113+
114+becomes the Nock noun
115+
116+```
117+[a b c 0]
118+```
119+
120+which is equivalent to
121+
122+```
123+[a [b [c 0]]]
124+```
125+
126+Note that we can and do use unnecessary brackets anyway, for
127+emphasis. Let's move on to the axiomatic functions:
128+
129+### Trivial operators
130+
131+```
132+?[a b] 0
133+?a 1
134++[a b] +[a b]
135++a 1 + a
136+=[a a] 0
137+=[a b] 1
138+```
139+
140+Here we define more pseudocode operators, which we'll use in
141+reductions further down. So far we have four built-in functions:
142+`*` meaning Nock itself, `?` testing whether a noun is a cell or
143+an atom, `+` incrementing an atom, and `=` testing for equality.
144+Again, no rocket science here.
145+
146+We should note that in Nock and Hoon, `0` (pronounced "yes") is
147+true, and `1` ("no") is false. Why? As in Unix, using zero for
148+success generalizes smoothly to multiple error codes. And it's
149+strange for success not to equal truth. Or at least, this is
150+our official excuse.
151+
152+### Tree addressing
153+
154+```
155+/[1 a] a
156+/[2 a b] a
157+/[3 a b] b
158+/[(a + a) b] /[2 /[a b]]
159+/[(a + a + 1) b] /[3 /[a b]]
160+```
161+
162+Slightly more interesting is our tree numbering. Every noun is
163+of course a tree. The `/` operator - pronounced "slot" - imposes
164+an address space on that tree, mapping every nonzero atom to a
165+tree position.
166+
167+1 is the root. The head of every node `n` is `2n`; the tail is
168+`2n+1`. Thus a simple tree:
169+
170+```
171+ 1
172+ 2 3
173+4 5 6 7
174+ 14 15
175+```
176+
177+If the value of every leaf is its tree address, this tree is
178+
179+```
180+[[4 5] [6 14 15]]
181+```
182+
183+and, for some examples of addressing:
184+
185+```
186+/[1 [[4 5] [6 14 15]]]
187+```
188+
189+is `[[4 5] [6 14 15]]`
190+
191+```
192+/[2 [[4 5] [6 14 15]]]
193+```
194+
195+is `[4 5]`
196+
197+```
198+/[3 [[4 5] [6 14 15]]]
199+```
200+
201+is `[6 14 15]`, and
202+
203+```
204+/[7 [[4 5] [6 14 15]]]
205+```
206+
207+is `[14 15]`
208+
209+Hopefully this isn't terribly hard to follow.
210+
211+## Instructions
212+
213+These rules define Nock itself - ie, the `*` operator.
214+
215+### `0`, slot
216+
217+```
218+*[a 0 b] /[b a]
219+```
220+
221+`0` is Nock's tree-addressing instruction. Let's try it out
222+from the Arvo command line.
223+
224+Note that we're using Hoon syntax here. Since we do not use Nock
225+from Hoon all that often (it's sort of like embedding assembly in
226+C), we've left it a little cumbersome. In Hoon, instead of
227+writing `*[a 0 b]`, we write
228+```
229+.*(a [0 b])
230+```
231+
232+So, to reuse our slot example, let's try the interpreter:
233+
234+```
235+~zod:dojo> .*([[4 5] [6 14 15]] [0 7])
236+```
237+
238+produces `[14 15]`.
239+
240+### `1`, constant
241+
242+```
243+*[a 1 b] b
244+```
245+
246+`1` is the constant instruction. It produces its argument without
247+reference to the subject. So
248+
249+```
250+~zod:dojo> .*(42 [1 153 218])
251+```
252+
253+yields `[153 218]`.
254+
255+### `2`, evaluate
256+
257+`2` is the Nock instruction:
258+
259+```
260+*[a 2 b c] *[*[a b] *[a c]]
261+```
262+
263+If you can compute a subject and a
264+formula, you can evaluate them in the interpreter. In most
265+fundamental languages, like Lisp, `eval` is a curiosity. But
266+Nock has no `apply` - so all our work gets done with `2`.
267+
268+Let's convert the previous example into a stupid use of `2`,
269+with constant subject and formula:
270+
271+```
272+~zod:dojo> .*(77 [2 [1 42] [1 1 153 218]])
273+```
274+
275+This gives the same `[153 218]`
276+
277+### `3`, `4`, and `5`, axiomatic functions
278+
279+```
280+*[a 3 b] ?*[a b]
281+*[a 4 b] +*[a b]
282+*[a 5 b] =*[a b]
283+```
284+
285+In lines 23-25, we meet our axiomatic functions again:
286+
287+For instance, if `x` is a formula that calculates some product,
288+`[4 x]` calculates that product plus one. Hence:
289+
290+```
291+~zod:dojo> .*(57 [0 1])
292+57
293+~zod:dojo> .*([132 19] [0 3])
294+19
295+~zod:dojo> .*(57 [4 0 1])
296+58
297+~zod:dojo> .*([132 19] [4 0 3])
298+20
299+```
300+
301+### Distribution
302+
303+```
304+*[a [b c] d] [*[a b c] *[a d]]
305+```
306+
307+Since Nock of an atom just crashes, the practical domain of the
308+Nock function is always a cell. Conventionally, the head of this
309+cell is the "subject," the tail is the "formula," and the result
310+of Nocking it is the "product." Basically, the subject is your
311+data and the formula is your code.
312+
313+We could write this line less formally:
314+
315+```
316+*[subject [formula-x formula-y]]
317+=> [*[subject formula-x] *[subject formula-y]]
318+```
319+
320+In other words, if you have two Nock formulas `x` and `y`, a
321+formula that computes the pair of them is just `[x y]`. We can
322+recognize this because no atom is a valid formula, and
323+every formula that *does not* use line 19 has an atomic head.
324+
325+If you know Lisp, you can think of this feature as a sort of
326+"implicit cons." Where in Lisp you would write `(cons x y)`,
327+in Nock you write `[x y]`.
328+
329+For example,
330+
331+```
332+~zod:dojo> .*(42 [4 0 1])
333+```
334+
335+where `42` is the subject (data) and `[4 0 1]` is the formula
336+(code), happens to evaluate to `43`. Whereas
337+
338+```
339+~zod:dojo> .*(42 [3 0 1])
340+```
341+
342+is `1`. So if we evaluate
343+
344+```
345+~zod:dojo> .*(42 [[4 0 1] [3 0 1]])
346+```
347+
348+we get
349+
350+```
351+[43 1]
352+```
353+
354+Except for the crash defaults (lines 6, 10, 17, and 35), we've actually
355+completed all the _essential_ aspects of Nock. The instructions up
356+through 5 provide all necessary computational functionality.
357+Nock, though very simple, is actually much more complex than it
358+formally needs to be.
359+
360+Operators 6 through 10 are macros. They exist because Nock is
361+not a toy, but a practical interpreter. Let's see them all
362+together:
363+
364+## Macros
365+
366+```
367+*[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
368+*[a 7 b c] *[a 2 b 1 c]
369+*[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
370+*[a 9 b c] *[a 7 c 2 [0 1] 0 b]
371+*[a 10 [b c] d] *[a 8 c 7 [0 3] d]
372+*[a 10 b c] *[a c]
373+```
374+
375+Notice that instructions `6` through `10` are macros: each just
376+expands to a larger formula. Let's try to figure out what these
377+macros do, simplest first:
378+
379+### `10`, hint
380+
381+```
382+*[a 10 b c] *[a c]
383+```
384+
385+If `x` is an atom and `y` is a formula, the formula `[10 x y]`
386+appears to be equivalent to... `y`. For instance:
387+
388+```
389+.*([132 19] [10 37 [4 0 3]])
390+```
391+
392+Why would we want to do this? `10` is actually a hint instruction.
393+The `37` in this example is discarded information - it is not
394+used, formally, in the computation. It may help the interpreter
395+compute the expression more efficiently, however.
396+
397+Every Nock computes the same result - but not all at the same
398+speed. What hints are supported? What do they do? Hints are a
399+higher-level convention which do not, and should not, appear in
400+the Nock spec. Some are defined in Hoon. Indeed, a naive Nock
401+interpreter not optimized for Hoon will run Hoon quite poorly.
402+When it gets the product, however, the product will be right.
403+
404+There is another instruction `10` rule:
405+
406+```
407+*[a 10 [b c] d] *[a 8 c 7 [0 3] d]
408+```
409+
410+Once we see what `7` and `8` do, we'll see that this complex hint
411+throws away an arbitrary `b`, but computes the formula `c`
412+against the subject and... throws away the product. This formula
413+is simply equivalent to `d`. Of course, in practice the product
414+of `c` will be put to some sordid and useful use. It could even
415+wind up as a side effect.
416+
417+(Why do we even care that `c` is computed? Because `c` could
418+crash. A correct Nock cannot simply ignore it, and treat both
419+variants of `10` as equivalent.)
420+
421+### `7`, compose
422+
423+```
424+*[a 7 b c] *[a 2 b 1 c]
425+```
426+
427+Suppose we have two formulas, `b` and `c`. What is the formula
428+`[7 b c]`? This example will show you:
429+
430+```
431+~zod:dojo> .*(42 [7 [4 0 1] [4 0 1]])
432+44
433+```
434+
435+Good old composition, on Nock formulas. It's easy to see how
436+this is built out of `2`. The data to evaluate is simply `b`,
437+and the formula is `c` quoted.
438+
439+### `8`, extend
440+
441+```
442+*[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
443+```
444+
445+`8` is `7`, except that the subject for `c` is not simply the
446+product of `b`, but the ordered pair of the product of `b` and
447+the original subject. Hence:
448+
449+```
450+.*(42 [8 [4 0 1] [0 1]])
451+[43 42]
452+```
453+
454+```
455+~zod:dojo> .*(42 [8 [4 0 1] [4 0 3]])
456+43
457+```
458+
459+Why would we want to do this? Imagine a higher-level language
460+in which the programmer declares a variable. This language is
461+likely to generate an `8`, because the variable is computed
462+against the present subject, and used in a calculation which
463+depends both on the original subject and the new variable.
464+
465+For extra credit, explain why we can't just define
466+
467+```
468+*[a 8 b c] *[a 7 [b 0 1] c]
469+```
470+
471+### `9`, invoke
472+
473+`9` is a calling convention:
474+
475+```
476+*[a 9 b c] *[a 7 c 2 [0 1] 0 b]
477+```
478+
479+With `c`, we produce a noun which contains both code and data - a
480+*core*. We use this core as the subject, and evaulate the
481+formula within it at slot `b`.
482+
483+### `6`, if-then-else
484+
485+This looks much scarier than it is:
486+
487+```
488+*[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
489+```
490+
491+If `b` evaluates to `0`, we produce `c`; if `b` evaluates to `1`,
492+we produce `d`; otherwise, we crash.
493+
494+For instance:
495+
496+```
497+~zod:dojo> .*(42 [6 [1 0] [4 0 1] [1 233]])
498+43
499+~zod:dojo> .*(42 [6 [1 1] [4 0 1] [1 233]])
500+233
501+```
502+
503+In real life, of course, the Nock implementor knows that `6` is
504+"if" and implements it as such. There is no practical sense in
505+reducing through this macro, or any of the others. We could have
506+defined "if" as a built-in function, like increment - except that
507+we can write "if" as a macro. If a funky macro.
508+
509+We can actually simplify the semantics of `6`, at the expense of
510+breaking the system a little, by creating a macro that works as
511+"if" only if `b` is a proper boolean and produces `0` or `1`.
512+Perhaps we have a higher-level type system which checks this.
513+
514+This simpler "if" would be:
515+
516+```
517+*[a 6 b c d] *[a [2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]]
518+```
519+
520+Or without so many unnecessary brackets:
521+
522+```
523+*[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] [4 4 b]]
524+```
525+
526+How does this work? We've replaced `[6 b c d]` with the formula
527+`[2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]`. We see two uses of `2`,
528+our evaluation instruction - an outer and an inner.
529+
530+Call the inner one `i`. So we have `[2 [0 1] i]`. Which means
531+that, to calculate our product, we use `[0 1]` - that is, the
532+original subject - as the subject; and the product of `i` as
533+the formula.
534+
535+Okay, cool. So `i` is `[2 [1 c d] [[1 0] [4 4 b]]]`. We compute
536+Nock with subject `[1 c d]`, formula `[[1 0] [4 4 b]]`.
537+
538+Obviously, `[1 c d]` produces just `[c d]` - that is, the ordered
539+pair of the "then" and "else" formulas. `[[1 0] [4 4 b]]` is a
540+line 19 cell - its head is `[1 0]`, producing just `0`, its tail
541+`[4 4 b]`, producing... what? Well, if `[4 b]` is `b` plus `1`,
542+`[4 4 b]` is `b` plus `2`.
543+
544+We're assuming that `b` produces either `0` or `1`. So `[4 4 b]`
545+yields either `2` or `3`. `[[1 0] [4 4 b]]` is either `[0 2]` or
546+`[0 3]`. Applied to the subject `[c d]`, this gives us either
547+`c` or `d` - the product of our inner evaluation `i`. This is
548+applied to the original subject, and the result is "if."
549+
550+But we need the full power of the funk, because if `b` produces,
551+say, `7`, all kinds of weirdness will result. We'd really like
552+`6` to just crash if the test product is not a boolean. How can
553+we accomplish this? This is an excellent way to prove to
554+yourself that you understand Nock: figure out what the real `6`
555+does. Or you could just agree that `6` is "if," and move on.
556+
557+(It's worth noting that in practical, compiler-generated Nock, we
558+never do anything as funky as these `6` macro internals. There's
559+no reason we couldn't build formulas at runtime, but we have no
560+reason to and we don't - except when actually metaprogramming.
561+As in most languages, normally code is code and data is data.)
docs/programming/nock/implementations.md → docs/nock/implementations.mdrenamed

The content is the same.

docs/programming/nock/implementations/c.md → docs/nock/implementations/c.mdrenamed

The content is the same.

docs/programming/nock/implementations/clojure.md → docs/nock/implementations/clojure.mdrenamed

The content is the same.

docs/programming/nock/implementations/haskell.md → docs/nock/implementations/haskell.mdrenamed

The content is the same.

docs/programming/nock/implementations/hoon.md → docs/nock/implementations/hoon.mdrenamed

The content is the same.

docs/programming/nock/implementations/python.md → docs/nock/implementations/python.mdrenamed

The content is the same.

docs/programming/nock/implementations/ruby.md → docs/nock/implementations/ruby.mdrenamed

The content is the same.

docs/programming/nock/implementations/scala.md → docs/nock/implementations/scala.mdrenamed

The content is the same.

docs/programming.mddeleted−12
@@ -1,12 +0,0 @@
1−---
2−sort: 3
3−---
4−
5−# Programming
6−
7−Most people should start by [learning Hoon](programming/hoon). If you're a
8−madman and just want something fun fast, skim the Hoon docs, then go to [Arvo
9−systems programming](programming/arvo/system). If you're interested in the
10−theory, learn [Nock](programming/nock) first; otherwise, learn it last.
11−
12−<kids/>
docs/programming/arvo.mddeleted−10
@@ -1,10 +0,0 @@
1−---
2−sort: 2
3−title: Arvo
4−---
5−
6−## Arvo
7−
8−Arvo is the Urbit operating environment.
9−
10−<list src="."></list>
docs/programming/hoon.mddeleted−10
@@ -1,10 +0,0 @@
1−---
2−sort: 1
3−title: Hoon
4−---
5−
6−## Hoon
7−
8−Hoon is a strict, typed, pure functional language that compiles itself to Nock.
9−
10−<list src="."></list>
docs/programming/hoon/twig.mddeleted−53
@@ -1,53 +0,0 @@
1−---
2−sort: 6
3−next: true
4−title: Expressions
5−---
6−
7−# Expressions
8−
9−A Hoon expression is called a `twig`. A `twig` is an AST node:
10−the noun that the Hoon compiler makes when it parses a source
11−expression.
12−
13−The definition of `twig` is the definition of Hoon. So let's
14−define the syntax and semantics of Hoon, a twig at a time. But
15−first, basic twig anatomy.
16−
17−## Twig anatomy and jargon
18−
19−A twig is a tagged union (`book`) with the form `{stem bulb}`,
20−where the `stem` is an atomic symbol. The bulb for each stem has
21−its own type, which varies by stem. (Compare with Lisp, where
22−every AST node is a tag and a list of child nodes.)
23−
24−There are no user-level macros. All stems are built into Hoon.
25−Most stems are implemented as internal macros, however.
26−
27−The bulb is usually a tuple, sometimes a list or map. An element
28−of this structure is a *subexpression*.
29−
30−Usually a subexpression is itself a `twig`; sometimes it's an
31−`@tas` (symbol) or a `wing` (reference path).
32−
33−## Moss versus seed
34−
35−Solely for clarity, we use the type alias `moss` for twigs whose
36−product is used as a mold. A `moss` is just a twig, both in
37−syntax and semantics; again, the difference is how we use it.
38−
39−Twigs are described as *mossy* (or *moldy*) and *seedy*; an
40−example of X is the seedy form of X, a mold producing X is its
41−mossy form.
42−
43−For instance, `[%foo %bar]` is seedy: a twig producing the noun
44−`[7.303.014 7.496.034]`. `{$foo $bar}` is mossy: a twig
45−producing a function whose product is always `[7.303.014
46−7.496.034]`.
47−
48−## Twig categories
49−
50−Fortunately, the twigs fit neatly into clear categories which
51−match their rune prefix. Read forward by category:
52−
53−<div><list/></div>
docs/programming/nock.mddeleted−10
@@ -1,10 +0,0 @@
1−---
2−sort: 3
3−title: Nock
4−---
5−
6−## Nock
7−
8−Nock is a simple, Turing-complete combinator interpreter.
9−
10−<list src="."></list>
docs/programming/nock/reference.mddeleted−742
@@ -1,742 +0,0 @@
1−---
2−sort: 3
3−title: Reference
4−---
5−
6−Nock Reference
7−==============
8−
9−Nouns
10−-----
11−
12− 1 :: A noun is an atom or a cell.
13− 2 :: An atom is a natural number.
14− 3 :: A cell is an ordered pair of nouns.
15−
16−An **atom** is a natural number - ie, an unsigned integer. Nock does not
17−limit the size of **atoms**, or know what an **atom** means.
18−
19−For instance, the **atom** `97` might mean the number `97`, or it might
20−mean the letter `a` (ASCII 97). A very large **atom** might be the
21−number of grains of sand on the beach - or it might be a GIF of your
22−children playing on the beach. Typically when we represent strings or
23−files as **atoms**, the first byte is the low byte. But even this is
24−just a convention. An **atom** is an **atom**.
25−
26−A **cell** is an ordered pair of any two **nouns** - **cell** or
27−**atom**. We group cells with square brackets:
28−
29− [1 1]
30− [34 45]
31− [[3 42] 12]
32− [[1 0] [0 [1 99]]]
33−
34−*Nouns* are the dumbest data model ever. **Nouns** make JSON look like
35−XML and XML look like ASN.1. It may also remind you of Lisp's
36−S-expressions - you can think of nouns as "S-expressions without the S."
37−
38−To be exact, a *noun* is an S-expression, except that classic
39−S-expressions have multiple **atom** types ("S" is for "symbol"). Since
40−Nock is designed to be used with a higher-level type system (such as
41−Hoon's), it does not need low-level types. An **atom** is just an
42−unsigned integer of any size.
43−
44−For instance, it's common to represent strings (or even whole text
45−files) as atoms, arranging them LSB first - so "foo" becomes `0x6f6f66`.
46−How do we know to print this as "foo", not `0x6f6f66`? We need external
47−information - such as a Hoon type. Similarly, other common atomic
48−types - signed integers, floating point, etc — are all straightforward
49−to map into **atoms**.
50−
51−It's also important to note that, unlike Lisp, Nock cannot create
52−cyclical data structures. It is normal and common for **nouns** in a
53−Nock runtime system to have acyclic structure - shared subtrees. But
54−there is no Nock computation that can make a child point to its parent.
55−One consequence: Nock has no garbage collector. (Nor can dag structure
56−be detected, as with Lisp eq.)
57−
58−There is also no single syntax for **nouns**. If you have **nouns** you
59−have Nock; if you have Nock you have Hoon; if you have Hoon, you can
60−write whatever parser you like.
61−
62−------------------------------------------------------------------------
63−
64−The Nock Function
65−-----------------
66−
67− 5 :: nock(a) *a
68−
69−Nock is a pure (stateless) function from **noun** to **noun**. In our
70−pseudocode (and only in our pseudocode) we express this with the prefix
71−operator `*`.
72−
73−A Nock program is given meaning by a process of reduction. To compute
74−`nock(x)`, where `x` is any **noun**, we step through the rules from the
75−top down, find the first left-hand side that matches `x`, and reduce it
76−to the right-hand side (in more mathematical notation, we might write
77−line 5 as `nock(a) -> *a`, a style we do use in documentation)
78−
79−When we use variable names, like `a`, in the pseudocode spec, we simply
80−mean that the rule fits for any **noun** `a`.
81−
82−So `nock(x)` is `*x`, for any **noun** `x`. And how do we reduce `*x`?
83−Looking up, we see that lines 23 through 39 reduce `*x` - for different
84−patterns of `x`.
85−
86−Normally `a` in `nock(a)` is a **cell** `[s f]`, or
87−
88− [subject formula]
89−
90−Intuitively, the formula is your function and the subject is its
91−argument. Hoon, or any other high-level language built on Nock, will
92−build its own function calling convention which does not map directly to
93−`*[subject formula]`.
94−
95−------------------------------------------------------------------------
96−
97−Bracket Grouping
98−----------------
99−
100− 6 :: [a b c] [a [b c]]
101−
102−Brackets associate to the right.
103−
104−So instead of writing
105−
106− [2 [6 7]]
107− [2 [6 [14 15]]]
108− [2 [6 [[28 29] [30 31]]]]
109− [2 [6 [[28 29] [30 [62 63]]]]]
110−
111−we can write
112−
113− [2 6 7]
114− [2 6 14 15]
115− [2 6 [28 29] 30 31]
116− [2 6 [28 29] 30 62 63]
117−
118−While this notational convenience is hardly rocket science, it's
119−surprising how confusing it can be, especially if you have a Lisp
120−background. Lisp's "S-expressions" are very similar to **nouns**, except
121−that Lisp has multiple types of **atom**, and Lisp's syntax
122−automatically adds list terminators to groups.
123−
124−For those with Lisp experience, it's important to note that Nock and
125−Hoon use tuples or "improper lists" much more heavily than Lisp. The
126−list terminator, normally 0, is never automatic. So the Lisp list
127−
128− (a b c)
129−
130−becomes the Nock **noun**
131−
132− [a b c 0]
133−
134−which is equivalent to
135−
136− [a [b [c 0]]]
137−
138−Note that we can and do use unnecessary brackets anyway, for emphasis.
139−
140−------------------------------------------------------------------------
141−
142−Axiomatic Functions
143−-------------------
144−
145− 8 :: ?[a b] 0
146− 9 :: ?a 1
147− 10 :: +[a b] +[a b]
148− 11 :: +a 1 + a
149− 12 :: =[a a] 0
150− 13 :: =[a b] 1
151−
152−Here we define three of Nock's four axiomatic functions: **Cell-test**,
153−**Increment** and **Equals** (the fourth axiomatic function, called
154−**Address**, is defined in lines 16 through 21). These functions are
155−just pseudocode, not actual Nock syntax, and are only used to define the
156−behaviour of certain Nock operators.
157−
158−We should note that in Nock and Hoon, `0` (pronounced "yes") is true,
159−and `1` ("no") is false. This convention is the opposite of
160−old-fashioned booleans, so we try hard to say "yes" and "no" instead of
161−"true" and "false."
162−
163−------------------------------------------------------------------------
164−
165−### Cell-test `?`
166−
167−`?` (pronounced "wut") tests whether is a **noun** is a **cell**. Again,
168−`0` means "yes", `1` means "no":
169−
170− 8 :: ?[a b] 0
171− 9 :: ?a 1
172−
173−------------------------------------------------------------------------
174−
175−### Increment `+`
176−
177−`+` (pronounced "lus") adds `1` to an **atom**:
178−
179− 10 :: +[a b] +[a b]
180− 11 :: +a 1 + a
181−
182−------------------------------------------------------------------------
183−
184−### Equals `=`
185−
186−`=` (pronounced "tis") tests a cell for equality. `0` means "yes", `1`
187−means "no":
188−
189− 12 :: =[a a] 0
190− 13 :: =[a b] 1
191− 14 :: =a =a
192−
193−Testing an **atom** for equality makes no sense and logically fails to
194−terminate.
195−
196−Because `+` works only for **atoms**, whereas `=` works only for
197−**cells**, the error rules match first for `+` and last for `=`.
198−
199−------------------------------------------------------------------------
200−
201−Noun Address
202−------------
203−
204− 16 :: /[1 a] a
205− 17 :: /[2 a b] a
206− 18 :: /[3 a b] b
207− 19 :: /[(a + a) b] /[2 /[a b]]
208− 20 :: /[(a + a + 1) b] /[3 /[a b]]
209− 21 :: /a /a
210−
211−We define a **noun** as a binary tree - where each node branches to a
212−left and right child - and assign an address, or **axis**, to every
213−element in the tree. The root of the tree is `/1`. The left child
214−(**head**) of every node at `/a` is `/2a`; the right child (**tail**) is
215−`/2a+1`. (Writing `(a + a)` is just a clever way to write `2a`, while
216−minimizing the set of pseudocode forms.)
217−
218−`1` is the root. The **head** of every **axis** `n` is `2n`; the
219−**tail** is `2n+1`. Thus a simple tree:
220−
221− 1
222− 2 3
223− 4 5 6 7
224− 14 15
225−
226−If the value of every leaf is its tree address, this tree is
227−
228− [[4 5] [6 14 15]]
229−
230−Let's use the example `[[97 2] [1 42 0]]`. So
231−
232− /[1 [97 2] [1 42 0]] -> [[97 2] [1 42 0]]
233−
234−because `/1` is the root of the tree, ie, the whole **noun**.
235−
236−Its left child (**head**) is `/2` (i.e. `(1 + 1)`):
237−
238− /[2 [97 2] [1 42 0]] -> [97 2]
239−
240−And its right child (**tail**) is `/3` (i.e. `(1 + 1 + 1)`):
241−
242− /[3 [97 2] [1 42 0]] -> [1 42 0]
243−
244−And delving into `/3`, we see `/(3 + 3)` and `/(3 + 3 + 1)`:
245−
246− /[6 [97 2] [1 42 0]] -> 1
247− /[7 [97 2] [1 42 0]] -> [42 0]
248−
249−It's also fun to build nouns in which every atom is its own axis:
250−
251− 1
252− [2 3]
253− [2 6 7]
254− [[4 5] 6 7]
255− [[4 5] 6 14 15]
256− [[4 5] [12 13] 14 15]
257− [[4 [10 11]] [12 13] 14 15]
258− [[[8 9] [10 11]] [12 13] 14 30 31]
259−
260−------------------------------------------------------------------------
261−
262−Distribution
263−------------
264−
265− 23 :: *[a [b c] d] [*[a b c] *[a d]]
266−
267−The practical domain of the Nock function is always a **cell**. When `a`
268−is an **atom**, `*a`, or `nock(a)`, is always an error. Conventionally,
269−Nock proper is always a function of a **cell**. The **head** of this
270−**cell** is the **subject**, the **tail** is the **formula**
271−
272− [subject formula]
273−
274−and the result of passing a **noun** through the Nock function is called
275−the **product**.
276−
277− nock([subject formula]) => product
278−
279−or
280−
281− *[subject formula] => product
282−
283−The **subject** is your data and the **formula** is your code. And the
284−**product** is your code's output.
285−
286−Notice that `a` in the Nock rules is always the **subject**, except line
287−39, which is a crash default for malformed **nouns** that do not
288−evaluate.
289−
290−A **formula** is always a **cell**. But when we look inside that
291−**cell**, we see two basic kinds of **formulas**:
292−
293− [operator operands]
294− [formula-x formula-y]
295−
296−An **operator** is always an **atom** (`0` through `10`). A **formula**
297−is always a **cell**. Line 23 distinguishes these forms:
298−
299− 23 :: *[a [b c] d] [*[a b c] *[a d]]
300−
301−If your code contains multiple **formulas**, the **subject** will
302−distribute over those **formulas**.
303−
304−In other words, if you have two Nock **formulas** `x` and `y`, a
305−**formula** that computes the pair of them is just the **cell** `[x y]`.
306−
307− *[subject [x y]] -> [*[subject x] *[subject y]]
308−
309−No **atom** is a valid **formula**, and every **formula** that does not
310−use line 23 has an atomic **head**.
311−
312−Suppose you have two **formulas** `f` and `g`, each of which computes
313−some function of the **subject** `s`. You can then construct the
314−**formula** `h` as `[f g]`; and `h(s) = [f(s) g(s)]`.
315−
316−For example:
317−
318− *[[19 42] [0 3] 0 2]
319−
320−The **subject** `s` is `[19 42]`. The **formula** `h` is `[[0 3] 0 2]`.
321−
322− *[s h]
323−
324−The **head** of `h` is `f`, which is `[0 3]`. The **tail** of `h` is
325−`g`, which is `[0 2]`.
326−
327− *[s [f g]]
328−
329−by the distribution rule, `*[s [f g]]` is
330−
331− [*[s f] *[s g]]
332−
333−or
334−
335− [*[[19 42] [0 3]] *[[19 42] 0 2]]
336−
337−`*[s f]` is `f(s)` and produces `42`. `*[s g]` is `g(s)` and produces
338−19.
339−
340−Since `h(s)` is `[f(s) g(s)]`, `h(s)` is `[42 19]`:
341−
342− *[[19 42] [0 3] 0 2] -> [42 19]
343−
344−------------------------------------------------------------------------
345−
346−Operator 0: Axis
347−----------------
348−
349− 25 :: *[a 0 b] /[b a]
350−
351−Operator 0 is Nock's tree address or **axis** operator, using the tree
352−addressing structure defined in lines 16 through 20. `*[a 0 b]` simply
353−returns the value of the part of `a` at **axis** `b`. For any subject
354−`a`, the formula `[0 b]` produces `/[b a]`.
355−
356−For example,
357−
358− *[[19 42] 0 3] -> /[3 19 42] -> 42.
359−
360−------------------------------------------------------------------------
361−
362−Operator 1: Just
363−----------------
364−
365− 26 :: *[a 1 b] b
366−
367−`1` is the constant, or **Just operator**. It produces its operand `b`
368−without reference to the **subject**.
369−
370−For example,
371−
372− *[42 1 57] -> 57
373−
374−**Operator 1** is named **Just** because it produces "just" its operand.
375−
376−------------------------------------------------------------------------
377−
378−Operator 2: Fire
379−----------------
380−
381− 27 :: *[a 2 b c] *[*[a b] *[a c]]
382−
383−**Operator 2** is the **Fire operator**, which brings us the essential
384−magic of recursion. Given the **formula** `[2 b c]`, `b` is a
385−**formula** for generating a new **subject**; `c` is a **formula** for
386−generating a new **formula**. To compute `*[a 2 b c]`, we evaluate both
387−`b` and `c` against the current **subject** `a`.
388−
389−A common use of **Fire** is to evaluate data inside the **subject** as
390−code.
391−
392−For example:
393−
394− *[[[40 43] [4 0 1]] [2 [0 4] [0 3]]] -> 41
395−
396− *[[[40 43] [4 0 1]] [2 [0 5] [0 3]]] -> 44
397−
398−**Operator 2** is called **Fire** because it "fires" Nock **formulas**
399−at its (possibly modified) **subject**.
400−
401−------------------------------------------------------------------------
402−
403−Operator 3: Depth
404−-----------------
405−
406− 28 :: *[a 3 b] ?*[a b]
407−
408−**Operator 3** applies the **Cell-test** function defined in lines 8 and
409−9 to the product of `*[a b]`.
410−
411−**Operator 3** is called **Depth** because it tests the "depth" of a
412−noun. **Cell-test** properly refers to the pseudocode function `?`.
413−
414−------------------------------------------------------------------------
415−
416−Operator 4: Bump
417−----------------
418−
419− 29 :: *[a 4 b] +*[a b]
420−
421−**Operator 4** applies the **Increment** function defined in lines 10
422−and 11 to the product of `*[a b]`.
423−
424−**Operator 4** is called **Bump** because it "bumps" the atomic product
425−`*[a b]` up by 1. **Increment** properly refers to the pseudocode
426−function `+`.
427−
428−------------------------------------------------------------------------
429−
430−Operator 5: Same
431−----------------
432−
433− 30 :: *[a 5 b] =*[a b]
434−
435−**Operator 5** applies the Equals function defined in lines 12, 13 and
436−14 to the product of \*[a b].
437−
438−**Operator 5** is called the **Same** operator, because it tests if the
439−head and tail of the product of `*[a b]` are the same. "Equals" properly
440−refers to the pseudocode function "=".
441−
442−------------------------------------------------------------------------
443−
444−Operator 6: If
445−--------------
446−
447− 32 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
448−
449−**Operator 6** is a primitive known to every programmer - **If**. Its
450−operands, a **test formula** `b`, a **then formula** `c` and an **else
451−formula** `d`.
452−
453−If the **test** `b` applied to the **subject** evaluates to `0` ("yes"),
454−
455− *[a b] -> 0
456−
457−then **If** produces the result of `c`, the **then formula**, applied to
458−the **subject**,
459−
460− *[a c]
461−
462−Else, if applying the **test** to the **subject** produces `1` ("no"),
463−
464− *[a b] -> 1
465−
466−**Operator 6** produces the result of `d`, the **else formula**, applied
467−to the **subject**,
468−
469− *[a d]
470−
471−If `*[a b]` produces any value other than `0` ("yes") or `1` ("no"),
472−**If** crashes.
473−
474−Let's examine the internals of **Operator 6**:
475−
476−**If** could have been defined as a built-in pseudocode function, like
477−**Increment**:
478−
479− :: $[0 b c] b
480− :: $[1 b c] c
481−
482−Then **Operator 6** could have been restated quite compactly:
483−
484− :: *[a 6 b c d] *[a $[*[a b] c d]]
485−
486−However, this is unnecessary complexity. **If** can be written as a
487−macro using the primitive operators:
488−
489− 32 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
490−
491−Reducing the right-hand side (an excellent exercise for the reader)
492−produces:
493−
494− *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
495−
496−Which is the reduced pseudocode form of **Operator 6**.
497−
498−Additionally, we could simplify the semantics of **If**, at the expense
499−of breaking the system, by creating a macro that works as if and only if
500−`*[a b]` produces either `0` or `1`.
501−
502−This simpler **If** would be:
503−
504− :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] [4 4 b]]
505−
506−which reduces to:
507−
508− *[a *[[c d] [0 ++*[a b]]]]
509−
510−Let's examine the internals of this macro with a test example:
511−
512− *[[40 43] [6 [3 0 1] [4 0 2] [4 0 1]]]
513−
514−Fitting this to the reduced form:
515−
516− *[[40 43] *[[4 0 2] [4 0 3]] [0 ++*[[40 43] [3 0 1]]]]]
517−
518−Our test:
519−
520− *[[40 43] [3 0 1]]
521−
522−produces a 0,
523−
524− *[[40 43] *[[[4 0 2] [4 0 3]] [0 ++0]]]]
525−
526−which gets incremented twice
527−
528− *[[40 43] *[[[4 0 2] [4 0 3]] [0 2]]]
529−
530−and is used as an axis to select the head of [[4 0 2] [4 0 3]]
531−
532− *[[40 43] [4 0 2]]
533−
534−which increments `40` to produce `41`. Had the **test** produced a "no"
535−instead of a "yes", **If** would have incremented the **tail** of the
536−subject instead of the **head**.
537−
538−The real **If** is only slightly more complicated:
539−
540− :: *[a 6 b c d] *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
541−
542−There is an extra step in the real **If** to prevent unexpected
543−behaviour if the test produces a value other than 0 ("yes") or 1 ("no").
544−The real **If** will crash if this happens and the naive **If** may not
545−(the reader will find it a useful exercise to figure out why).
546−
547−It's worth noting that practical, compiler-generated Nock never does
548−anything as funky as these **Operator 6** macro internals.
549−
550−------------------------------------------------------------------------
551−
552−Operator 7: Compose
553−-------------------
554−
555− 33 :: *[a 7 b c] *[a 2 b 1 c]
556−
557−**Operator 7** implements function composition. To use a bit of math
558−notation, if we define the formula `[7 b c]` as a function `d(x)`:
559−
560− d(a) == c(b(a))
561−
562−This is apparent from the reduced pseudocode form of **Operator 7**:
563−
564− *[*[a b] c]
565−
566−As an example,
567−
568− *[[42 44] [7 [4 0 3] [3 0 1]]] -> 1
569−
570−The above sequentially applies the **formulas** `[4 0 3]` and `[3 0 1]`
571−to our subject `[42 44]`, first incrementing the **head**, then testing
572−the **depth**.
573−
574−------------------------------------------------------------------------
575−
576−Operator 8: Push
577−----------------
578−
579− 34 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
580−
581−**Operator 8** pushes a new **noun** onto our **subject**, not at all
582−unlike pushing a new variable onto the stack.
583−
584−The internals of **Operator 8** are similar to **Operator 7**, except
585−that the **subject** for `c` is not simply the **product** of `b`, but
586−the ordered pair of the **product** of `b` and the original **subject**.
587−
588−This is apparent from the reduced pseudocode form of **Operator 8**:
589−
590− *[[*[a b] a] c]
591−
592−**Operator 8** evaluates the **formula** `c` with the **cell** of
593−`*[a b]` and the original **subject** `a`. In math notation, if we
594−define `[8 b c]` as `d(x)`:
595−
596− d(a) == c([b(a) a])
597−
598−Suppose, for the purposes of the **formula** `c`, we need not just the
599−**subject** `a`, but some intermediate **noun** computed from the
600−**subject** that will be useful in the calculation of `c`. **Operator
601−8** applies **formula** `c` to a new **subject** that is the pair of the
602−intermediate value and the old **subject**.
603−
604−In higher-level languages that compile to Nock, variable declarations
605−are likely to generate an **Operator 8**, because the variable is
606−computed against the present **subject**, and used in a calculation
607−which depends both on the original **subject** and the new variable.
608−
609−------------------------------------------------------------------------
610−
611−Op 9: Call
612−----------
613−
614− 35 :: *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
615−
616−**Operator 9** is the **Call** operator and is used for calling and
617−applying **formulas** inside **noun** structures called **cores**
618−
619−A **noun** can contain both data and code. By convention, all
620−interesting flow control in Nock is done with **cores**, which are
621−**cells** whose head is code (containing one or more **formulas**) and
622−whose **tail** is data (possibly containing other **cores**):
623−
624−[code data]
625−
626−All flow structures in other languages not built on Nock correspond to
627−**cores**. Functions and/or closures are **cores**, objects are
628−**cores**, modules are **cores**, even loops are **cores** (Nock, of
629−course, does not have a loop operator).
630−
631−The head of a **core** is called the **battery** and the tail is called
632−the **payload**:
633−
634− [battery payload]
635−
636−The **payload** of a core is any useful data needed for computation.
637−
638−The **battery** of a **core** is a **noun** containing one or more
639−**arms**, which are **formulas** whose **subject** is the entire
640−**core**.
641−
642−For example, in the case of the **battery** containing three **arms**:
643−
644− [arm1 [arm2 arm3]]
645−
646−Where `arm1` is at **axis** `/2` in the **battery**, `arm2` is at `/6`,
647−and `arm3` is at `/7`. The **axes** will differ depending on the number
648−of **arms**.
649−
650−Of course, because the **subject** of an **arm** is the entire **core**,
651−an **arm** can invoke itself (or any other **arm** in the **battery**).
652−Hence, it can loop. And this is what a loop is - the simplest of
653−**cores**.
654−
655−Also, by convention, we have terms for different kinds of **cores**,
656−depending on their structure:
657−
658−A **trap** is a **core** whose battery contains a single **arm**. By
659−convention, this arm is called `$` (pronounced 'buc'), which is the
660−empty-name. All **traps** have the following structure:
661−
662− [$ payload]
663−
664−A **door** is a **core** with a **payload** of the form
665−`[sample context]`. The **sample** is dynamic data (such as the
666−arguments of a function) and the **context** is any data that might be
667−useful (such as other **cores**, variables, or the entire kernel of your
668−OS). All **doors** have the following structure:
669−
670− [battery [sample context]]
671−
672−A **gate** is a **core** that is both a **door** and a **trap**.
673−**Gates** are the Nock equivalent of lambdas or functions. All **gates**
674−have the structure:
675−
676− [$ [sample context]]
677−
678−**Operator 9** constructs a **core** and activates it by calling one of
679−its **arms**.
680−
681−Looking reduced pseudocode form of **Call**:
682−
683− *[*[a c] *[*[a c] 0 b]]
684−
685−**Call** applies a **formula** `c` to the **subject** `a`, where
686−`*[a c]` produces a **core**. **Call** then calls an **arm** `b` of the
687−**core** produced by `*[a c]` and reflexively applies it to the same
688−**core**.
689−
690−------------------------------------------------------------------------
691−
692−Op 10: Hint
693−-----------
694−
695− 36 :: *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
696− 37 :: *[a 10 b c] *[a c]
697−
698−**Operator 10** serves a hint to the interpreter.
699−
700−If `b` is an atom and `c` is a **formula**, the **formula** `[10 b c]`
701−appears to be equivalent to `c`. Likewise if `[b c]` is a **cell**,
702−`[10 [b c] d]` appears to be equivalent to `d`. **Operator 10** is
703−actually a hint operator. The `b` or `[b c]`is discarded information -
704−it is not used, formally, in the computation. It may help the
705−interpreter compute the expression more efficiently, however.
706−
707−Every Nock computes the same result - but not all at the same speed.
708−What hints are supported? What do they do? Hints are a higher-level
709−convention which do not, and should not, appear in the Nock spec. Some
710−are defined in Hoon. Indeed, a naive Nock interpreter not optimized for
711−Hoon will run Hoon quite poorly. When it gets the product, however, the
712−product will be right. (Why is the `c` in `[b c]` computed? Because `c`
713−could crash. A correct Nock cannot simply ignore it, and treat both
714−variants of `10` as equivalent.)
715−
716−------------------------------------------------------------------------
717−
718−Op 11: Retrieve
719−-----------
720−
721−**operator 11** retrieves data from the global namespace at the path it is given.
722−
723−nock 11 is an additional nock operation provided by arvo to provide access to the global namespace. It's provided by arvo, so even though it's not technically a part of the nock, any code that arvo runs has access to it. When nock running on top of arvo hits nock 11, it either produces a value or blocks. The most common usage is with clay, where ^(%cx /path/to/file) will produce the referred-to-file.
724−
725−Crash default
726−-------------
727−
728− 39 :: *a *a
729−
730−The nock function is defined for every **noun**, but on many **nouns**
731−it does nothing useful. For instance, if `a` is an **atom**, `*a`
732−reduces to... `*a`. In theory, this means that Nock spins forever in an
733−infinite loop. In other words, Nock produces no result - and in
734−practice, your interpreter will stop.
735−
736−Another way to see this is that Nock has "crash-only" semantics. There
737−is no exception mechanism. The only way to catch Nock errors is to
738−simulate Nock in a higher-level virtual Nock - which, in fact, we do all
739−the time. A simulator (or a practical low-level interpreter) can report,
740−out of band, that Nock would not terminate. It cannot recognize all
741−infinite loops, of course (cf. Halting problem), but it can recognize
742−common and obvious ones, which is usually sufficient in practice.
docs/programming/nock/tutorial.mddeleted−1045
@@ -1,1045 +0,0 @@
1−---
2−sort: 2
3−next: true
4−title: Tutorial
5−---
6−
7−# Nock tutorial
8−
9−So let's learn Nock! But wait - why learn Nock? After all,
10−we're going to be programming in Hoon, not Nock.
11−
12−Like JVM bytecode, Nock is as inscrutable as assembly language.
13−In fact, you can think of it as a sort of "functional assembly
14−language." There are sometimes reasons to program in real
15−assembly language. There is never a reason to program in Nock.
16−Except to learn Nock.
17−
18−Indeed, it is not necessary for the Hoon programmer to learn
19−Nock. We recommend it strongly, however, because Hoon has a very
20−special relationship with Nock - not unlike the relationship
21−between C and assembly language.
22−
23−Just as C is a very shallow layer over the raw CPU, Hoon is a
24−very shallow layer over raw Nock - often little more than a
25−macro. If you try to learn C without understanding the CPU under
26−it, you will be forever bemused by why it works the way it does.
27−
28−So let's learn Nock! But wait - which Nock? Nock, though more
29−frozen than Walt Disney, does have versions. Nock versions are
30−measured by integer degrees Kelvin, newer being colder. The
31−newest, Nock 5K - roughly the temperature of Neptune. No change
32−is anticipated between 5K and absolute zero, though you never
33−know. Any such change would certainly be quite painful.
34−
35−#1.1 Definition#
36−
37−The best way to learn Nock is to read the spec and write your own
38−naive interpreter. Here is Nock 5K:
39−
40−**1. Structures**
41−
42− A noun is an atom or a cell. An atom is a natural number.
43− A cell is an ordered pair of nouns.
44−
45−**2. Pseudocode**
46−
47− 1 :: nock(a) *a
48− 2 :: [a b c] [a [b c]]
49− 3 ::
50− 4 :: ?[a b] 0
51− 5 :: ?a 1
52− 6 :: +[a b] +[a b]
53− 7 :: +a 1 + a
54− 8 :: =[a a] 0
55− 9 :: =[a b] 1
56− 10 :: =a =a
57− 11 ::
58− 12 :: /[1 a] a
59− 13 :: /[2 [a b]] a
60− 14 :: /[3 [a b]] b
61− 15 :: /[(a + a) b] /[2 /[a b]]
62− 16 :: /[(a + a + 1) b] /[3 /[a b]]
63− 17 :: /a /a
64− 18 ::
65− 19 :: *[a [[b c] d]] [*[a [b c]] *[a d]]
66− 20 ::
67− 21 :: *[a [0 b]] /[b a]
68− 22 :: *[a [1 b]] b
69− 23 :: *[a [2 [b c]]] *[*[a b] *[a c]]
70− 24 :: *[a [3 b]] ?*[a b]
71− 25 :: *[a [4 b]] +*[a b]
72− 26 :: *[a [5 b]] =*[a b]
73− 27 ::
74− 28 :: *[a [6 [b [c d]]]] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
75− 29 :: *[a [7 [b c]]] *[a 2 b 1 c]
76− 30 :: *[a [8 [b c]]] *[a 7 [[7 [0 1] b] 0 1] c]
77− 31 :: *[a [9 [b c]]] *[a 7 c 2 [0 1] 0 b]
78− 32 :: *[a [10 [[b c] d]]] *[a 8 c 7 [0 3] d]
79− 33 :: *[a [10 [b c]]] *[a c]
80− 34 ::
81− 35 :: *a *a
82−
83−Your interpreter should be no more than a page of code in
84−any language. For extra credit, `6`-`10` are macros; implement
85−them directly. For extra extra credit, optimize tail calls.
86−To test your code, write a decrement formula b such that
87−`*[a b]` is `a - 1` for any atomic nonzero `a`.
88−
89−#1.2 Installation#
90−
91−The second best way to learn Nock is to boot up your own Arvo
92−virtual computer. See the Arvo tutorial for instructions.
93−
94−#1.3 Nock#
95−
96−To make Nock make sense, let's work through Nock 5K line by line.
97−First the data model:
98−
99−##1. Structures##
100−
101− A noun is an atom or a cell. An atom is any natural number.
102− A cell is any ordered pair of nouns.
103−
104−Nouns are the dumbest data model ever. Nouns make JSON look like
105−XML and XML look like ASN.1. It may also remind you of Lisp's
106−S-expressions - you can think of nouns as "S-expressions without
107−the S."
108−
109−To be exact, a noun _is_ an S-expression, except that classic
110−S-expressions have multiple atom types ("S" is for "symbol").
111−Since Nock is designed to be used with a higher-level type system
112−(such as Hoon's), it does not need low-level types. An atom is
113−just an unsigned integer of any size.
114−
115−For instance, it's common to represent strings (or even whole
116−text files) as atoms, arranging them LSB first - so "foo" becomes
117−`0x6f6f66`. How do we know to print this as "foo", not `0x6f6f66`?
118−We need external information - such as a Hoon type. Similarly,
119−other common atomic types - signed integers, floating point, etc
120−- are all straightforward to map into atoms.
121−
122−It's also important to note that, unlike Lisp, Nock cannot create
123−cyclical data structures. It is normal and common for nouns in a
124−Nock runtime system to have acyclic structure - shared subtrees.
125−But there is no Nock computation that can make a child point to
126−its parent. One consequence: Nock has no garbage collector.
127−(Nor can dag structure be detected, as with Lisp `eq`.)
128−
129−There is also no single syntax for nouns. If you have nouns you
130−have Nock; if you have Nock you have Hoon; if you have Hoon, you
131−can write whatever parser you like.
132−
133−Let's continue:
134−
135−##2. Pseudocode##
136−
137−It's important to recognize that the pseudocode of the Nock spec
138−is just that: pseudocode. It looks a little like Hoon. It isn't
139−Hoon - it's just pseudocode. Or in other words, just English.
140−At the bottom of every formal system is a system of axioms, which
141−can only be written in English. (Why pseudocode, not Hoon? Since
142−Hoon is defined in Nock, this would only give a false impression
143−of nonexistent precision.)
144−
145−The logic of this pseudocode is a pattern-matching reduction,
146−matching from the top down. To compute Nock, repeatedly reduce
147−with the first line that matches. Let's jump right in!
148−
149−##Line 1:##
150−
151− 1 :: nock(a) *a
152−
153−Nock is a pure (stateless) function from noun to noun. In our
154−pseudocode (and only in our pseudocode) we express this with the
155−prefix operator `*`.
156−
157−This function is defined for every noun, but on many nouns it
158−does nothing useful. For instance, if `a` is an atom, `*a`
159−reduces to... `*a`. In theory, this means that Nock spins
160−forever in an infinite loop. In other words, Nock produces no
161−result - and in practice, your interpreter will stop.
162−
163−(Another way to see this is that Nock has "crash-only" semantics.
164−There is no exception mechanism. The only way to catch Nock
165−errors is to simulate Nock in a higher-level virtual Nock -
166−which, in fact, we do all the time. A simulator (or a practical
167−low-level interpreter) can report, out of band, that Nock would
168−not terminate. It cannot recognize all infinite loops, of
169−course, but it can catch the obvious ones - like `*42`.)
170−
171−Normally `a` in `nock(a)` is a cell `[s f]`, or as we say
172−
173− [subject formula]
174−
175−Intuitively, the formula is your function and the subject is
176−its argument. We call them something different because Hoon,
177−or any other high-level language built on Nock, will build its
178−own function calling convention which *does not* map directly
179−to `*[subject formula]`.
180−
181−##Line 2:##
182−
183− 2 :: [a b c] [a [b c]]
184−
185−Ie, brackets (in our pseudocode, as in Hoon) associate to the
186−right. For those with Lisp experience, it's important to note
187−that Nock and Hoon use tuples or "improper lists" much more
188−heavily than Lisp. The list terminator, normally 0, is never
189−automatic. So the Lisp list
190−
191− (a b c)
192−
193−becomes the Nock noun
194−
195− [a b c 0]
196−
197−which is equivalent to
198−
199− [a [b [c 0]]]
200−
201−Note that we can and do use unnecessary brackets anyway, for
202−emphasis.
203−
204−Let's move on to the axiomatic functions.
205−
206−##Lines 4-10:##
207−
208− 4 :: ?[a b] 0
209− 5 :: ?a 1
210− 6 :: +[a b] +[a b]
211− 7 :: +a 1 + a
212− 8 :: =[a a] 0
213− 9 :: =[a b] 1
214−
215−Here we define more pseudocode operators, which we'll use in
216−reductions further down. So far we have four built-in functions:
217−`*` meaning Nock itself, `?` testing whether a noun is a cell or
218−an atom, `+` incrementing an atom, and `=` testing for equality.
219−Again, no rocket science here.
220−
221−We should note that in Nock and Hoon, `0` (pronounced "yes") is
222−true, and `1` ("no") is false. Why? It's fresh, it's different,
223−it's new. And it's annoying. And it keeps you on your toes.
224−And it's also just intuitively right.
225−
226−
227−##Lines 12-16:##
228−
229− 12 :: /[1 a] a
230− 13 :: /[2 a b] a
231− 14 :: /[3 a b] b
232− 15 :: /[(a + a) b] /[2 /[a b]]
233− 16 :: /[(a + a + 1) b] /[3 /[a b]]
234−
235−Slightly more interesting is our tree numbering. Every noun is of course a tree. The `/` operator - pronounced
236−"slot" - imposes an address space on that tree, mapping every
237−nonzero atom to a tree position.
238−
239−1 is the root. The head of every node `n` is `2n`; the tail is
240−`2n+1`. Thus a simple tree:
241−
242− 1
243− 2 3
244− 4 5 6 7
245− 14 15
246−
247−If the value of every leaf is its tree address, this tree is
248−
249− [[4 5] [6 14 15]]
250−
251−and, for some examples of addressing:
252−
253− /[1 [[4 5] [6 14 15]]]
254−
255−is `[[4 5] [6 14 15]]`
256−
257− /[2 [[4 5] [6 14 15]]]
258−
259−is `[4 5]`
260−
261− /[3 [[4 5] [6 14 15]]]
262−
263−is `[6 14 15]`, and
264−
265− /[7 [[4 5] [6 14 15]]]
266−
267−is `[14 15]`
268−
269−I do hope this isn't so terribly hard to follow.
270−
271−##Line 21:##
272−
273−Now we enter the definition of Nock itself - ie, the `*`
274−operator.
275−
276− 21 :: *[a 0 b] /[b a]
277−
278−`0` is simply Nock's tree-addressing operator. Let's try it out
279−from the Arvo command line.
280−
281−Note that we're using Hoon syntax here. Since we do not use Nock
282−from Hoon all that often (it's sort of like embedding assembly in
283−C), we've left it a little cumbersome. In Hoon, instead of
284−writing `*[a 0 b]`, we write
285−
286− .*(a [0 b])
287−
288−So, to reuse our slot example, let's try the interpreter:
289−
290− ~tasfyn-partyv> .*([[4 5] [6 14 15]] [0 7])
291−
292−gives, while the sky remains blue and the sun rises in the east:
293−
294− [14 15]
295−
296−Even stupider is line 21:
297−
298−##Line 21:##
299−
300− 21 :: *[a 1 b] b
301−
302−`1` is the constant operator. It produces its argument without
303−reference to the subject. So
304−
305− ~tasfyn-partyv> .*(42 [1 153 218])
306−
307−yields
308−
309− [153 218]
310−
311−
312−##Line 23:##
313−
314− 23 :: *[a 2 b c] *[*[a b] *[a c]]
315−
316−Line 22 brings us the essential magic of recursion.
317−`2` is the Nock operator. If you can compute a subject and a
318−formula, you can evaluate them in the interpreter. In most
319−fundamental languages, like Lisp, `eval` is a curiosity. But
320−Nock has no `apply` - so all our work gets done with `2`.
321−
322−Let's convert the previous example into a stupid use of `2`:
323−
324− ~tasfyn-partyv> .*(77 [2 [1 42] [1 1 153 218]])
325−
326−with a constant subject and a constant formula, gives the same
327−
328− [153 218]
329−
330−Like so:
331−
332− *[77 [2 [1 42] [1 1 153 218]]]
333−
334− 22 :: *[a 2 b c] *[*[a b] *[a c]]
335−
336− *[*[77 [1 42]] *[77 [1 1 153 218]]]
337−
338− 21 :: *[a 1 b] b
339−
340− *[42 *[77 [1 1 153 218]]]
341−
342− *[42 1 153 218]
343−
344− [153 218]
345−
346−##Lines 24-26:##
347−
348− 24 :: *[a 3 b] ?*[a b]
349− 25 :: *[a 4 b] +*[a b]
350− 26 :: *[a 5 b] =*[a b]
351−
352−In lines 23-25, we meet our axiomatic functions again:
353−
354−For instance, if `x` is a formula that calculates some product,
355−`[4 x]` calculates that product plus one. Hence:
356−
357− ~tasfyn-partyv> .*(57 [0 1])
358− 57
359−
360−and
361−
362− ~tasfyn-partyv> .*([132 19] [0 3])
363− 19
364−
365−and
366−
367− ~tasfyn-partyv> .*(57 [4 0 1])
368− 58
369−
370−and
371−
372− ~tasfyn-partyv> .*([132 19] [4 0 3])
373− 20
374−
375−If this seems obvious, you're doin' good. Finally, we jump back up
376−to line 18, the trickiest in the spec:
377−
378−##Line 19:##
379−
380− 19 :: *[a [b c] d] [*[a b c] *[a d]]
381−
382−Um, what?
383−
384−Since Nock of an atom just crashes, the practical domain of the
385−Nock function is always a cell. Conventionally, the head of this
386−cell is the "subject," the tail is the "formula," and the result
387−of Nocking it is the "product." Basically, the subject is your
388−data and the formula is your code.
389−
390−We could write line 19 less formally:
391−
392− *[subject [formula-x formula-y]]
393− => [*[subject formula-x] *[subject formula-y]]
394−
395−In other words, if you have two Nock formulas `x` and `y`, a
396−formula that computes the pair of them is just `[x y]`. We can
397−recognize this because no atom is a valid formula, and
398−every formula that _does not_ use line 19 has an atomic head.
399−
400−If you know Lisp, you can think of this feature as a sort of
401−"implicit cons." Where in Lisp you would write `(cons x y)`,
402−in Nock you write `[x y]`.
403−
404−For example,
405−
406− ~tasfyn-partyv> .*(42 [4 0 1])
407−
408−where `42` is the subject (data) and `[4 0 1]` is the formula
409−(code), happens to evaluate to `43`. Whereas
410−
411− ~tasfyn-partyv> .*(42 [3 0 1])
412−
413−is `1`. So if we evaluate
414−
415− ~tasfyn-partyv> .*(42 [[4 0 1] [3 0 1]])
416−
417−we get
418−
419− [43 1]
420−
421−Except for the crash defaults (lines 6, 10, 17, and 35), we've actually
422−completed all the _essential_ aspects of Nock. The operators up
423−through 5 provide all necessary computational functionality.
424−Nock, though very simple, is actually much more complex than it
425−formally needs to be.
426−
427−Operators 6 through 10 are macros. They exist because Nock is
428−not a toy, but a practical interpreter. Let's see them all
429−together:
430−
431−##Lines 28-33:##
432−
433− 28 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
434− 29 :: *[a 7 b c] *[a 2 b 1 c]
435− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
436− 31 :: *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
437− 32 :: *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
438− 33 :: *[a 10 b c] *[a c]
439−
440−Whoa! Have we entered rocket-science territory? Let's try to
441−figure out what these strange formulas do - simplest first.
442−The simplest is clearly line 33:
443−
444− 33 :: *[a 10 b c] *[a c]
445−
446−If `x` is an atom and `y` is a formula, the formula `[10 x y]`
447−appears to be equivalent to... `y`. For instance:
448−
449− ~tasfyn-partyv> .*([132 19] [10 37 [4 0 3]])
450− 20
451−
452−Why would we want to do this? `10` is actually a hint operator.
453−The `37` in this example is discarded information - it is not
454−used, formally, in the computation. It may help the interpreter
455−compute the expression more efficiently, however.
456−
457−Every Nock computes the same result - but not all at the same
458−speed. What hints are supported? What do they do? Hints are a
459−higher-level convention which do not, and should not, appear in
460−the Nock spec. Some are defined in Hoon. Indeed, a naive Nock
461−interpreter not optimized for Hoon will run Hoon quite poorly.
462−When it gets the product, however, the product will be right.
463−
464−There is another reduction for hints - line 32:
465−
466− 32 :: *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
467−
468−Once we see what `7` and `8` do, we'll see that this complex hint
469−throws away an arbitrary `b`, but computes the formula `c`
470−against the subject and... throws away the product. This formula
471−is simply equivalent to `d`. Of course, in practice the product
472−of `c` will be put to some sordid and useful use. It could even
473−wind up as a side effect, though we try not to get _that_ sordid.
474−
475−(Why do we even care that `c` is computed? Because `c` could
476−crash. A correct Nock cannot simply ignore it, and treat both
477−variants of `10` as equivalent.)
478−
479−We move on to the next simplest operator, `7`. Line 29:
480−
481− 29 :: *[a 7 b c] *[a 2 b 1 c]
482−
483−Suppose we have two formulas, `b` and `c`. What is the formula
484−`[7 b c]`? This example will show you:
485−
486− ~tasfyn-partyv> .*(42 [7 [4 0 1] [4 0 1]])
487− 44
488−
489−`7` is an old mathematical friend, function composition. It's
490−easy to see how this is built out of `2`. The data to evaluate
491−is simply `b`, and the formula is `c` quoted.
492−
493−Line 30 looks very similar:
494−
495− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
496−
497−Indeed, `8` is `7`, except that the subject for `c` is not simply
498−the product of `b`, but the ordered pair of the product of `b`
499−and the original subject. Hence:
500−
501− ~tasfyn-partyv> .*(42 [8 [4 0 1] [0 1]])
502− [43 42]
503−
504−and
505−
506− ~tasfyn-partyv> .*(42 [8 [4 0 1] [4 0 3]])
507− 43
508−
509−Why would we want to do this? Imagine a higher-level language
510−in which the programmer declares a variable. This language is
511−likely to generate an `8`, because the variable is computed
512−against the present subject, and used in a calculation which
513−depends both on the original subject and the new variable.
514−
515−For extra credit, explain why we can't just define
516−
517− *[a 8 b c] *[a 7 [b 0 1] c]
518−
519−Another simple macro is line 31:
520−
521− 31 :: *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
522−
523−`9` is a calling convention. With `c`, we produce a noun which
524−contains both code and data - a _core_. We use this core as the
525−subject, and apply the formula within it at slot `b`.
526−
527−And finally, we come to the piece de resistance - line 28:
528−
529− 28 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
530−
531−Great giblets! WTF is this doing? It seems we've finally
532−arrived at some real rocket science.
533−
534−Actually, `6` is a primitive known to every programmer - good old
535−"if." If `b` evaluates to `0`, we produce `c`; if `b` evaluates
536−to `1`, we produce `d`; otherwise, we crash.
537−
538−For instance:
539−
540− ~tasfyn-partyv> .*(42 [6 [1 0] [4 0 1] [1 233]])
541− 43
542−
543−and
544−
545− ~tasfyn-partyv> .*(42 [6 [1 1] [4 0 1] [1 233]])
546− 233
547−
548−In real life, of course, the Nock implementor knows that `6` is
549−"if" and implements it as such. There is no practical sense in
550−reducing through this macro, or any of the others. We could have
551−defined "if" as a built-in function, like increment - except that
552−we can write "if" as a macro. If a funky macro.
553−
554−It's a good exercise, however, to peek inside the funk.
555−
556−We can actually simplify the semantics of `6`, at the expense of
557−breaking the system a little, by creating a macro that works as
558−"if" only if `b` is a proper boolean and produces `0` or `1`.
559−Perhaps we have a higher-level type system which checks this.
560−
561−This simpler "if" would be:
562−
563− *[a 6 b c d] *[a [2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]]
564−
565−Or without so many unnecessary brackets:
566−
567− *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] [4 4 b]]
568−
569−How does this work? We've replaced `[6 b c d]` with the formula
570−`[2 [0 1] [2 [1 c d] [[1 0] [4 4 b]]]]`. We see two uses of `2`,
571−our evaluation operator - an outer and an inner.
572−
573−Call the inner one `i`. So we have `[2 [0 1] i]`. Which means
574−that, to calculate our product, we use `[0 1]` - that is, the
575−original subject - as the subject; and the product of `i` as
576−the formula.
577−
578−Okay, cool. So `i` is `[2 [1 c d] [[1 0] [4 4 b]]]`. We compute
579−Nock with subject `[1 c d]`, formula `[[1 0] [4 4 b]]`.
580−
581−Obviously, `[1 c d]` produces just `[c d]` - that is, the ordered
582−pair of the "then" and "else" formulas. `[[1 0] [4 4 b]]` is a
583−line 19 cell - its head is `[1 0]`, producing just `0`, its tail
584−`[4 4 b]`, producing... what? Well, if `[4 b]` is `b` plus `1`,
585−`[4 4 b]` is `b` plus `2`.
586−
587−We're assuming that `b` produces either `0` or `1`. So `[4 4 b]`
588−yields either `2` or `3`. `[[1 0] [4 4 b]]` is either `[0 2]` or
589−`[0 3]`. Applied to the subject `[c d]`, this gives us either
590−`c` or `d` - the product of our inner evaluation `i`. This is
591−applied to the original subject, and the result is "if."
592−
593−But we need the full power of the funk, because if `b` produces,
594−say, `7`, all kinds of weirdness will result. We'd really like
595−`6` to just crash if the test product is not a boolean. How can
596−we accomplish this? This is an excellent way to prove to
597−yourself that you understand Nock: figure out what the real `6`
598−does. Or you could just agree that `6` is "if," and move on.
599−
600−(It's worth noting that in practical, compiler-generated Nock, we
601−never do anything as funky as these `6` macro internals. There's
602−no reason we couldn't build formulas at runtime, but we have no
603−reason to and we don't - except when actually metaprogramming.
604−As in most languages, normally code is code and data is data.)
605−
606−#1.4 Decrement in Nock#
607−
608−A good practice exercise for Nock is a decrement formula. Ie, a
609−formula `f` which implements the partial function that produces
610−`(s - 1)` if `s` is a nonzero atom, and otherwise does not
611−terminate.
612−
613−The normal Hoon programmer has written one Nock formula: this
614−one. Since decrement uses all the Nock techniques the Hoon
615−compiler uses, the exercise is a good foundation. After you
616−write decrement (or just follow this example), you'll never need
617−to deal with Nock again.
618−
619−As we know, the equivalent formula for increment is
620−
621− [4 0 1]
622−
623−Thus:
624−
625− ~>tasfyn-partyv .*(42 [4 0 1])
626− 43
627−
628−Of course, increment is built into Nock. So, ha, that's easy.
629−
630−How do we decrement? A good way to start is to gaze fondly on
631−how we'd do it if we actually had a real language, ie, Hoon.
632−Here is a minimal decrement in Hoon:
633−
634− => a=. :: line 1
635− =+ b=0 :: line 2
636− |- :: line 3
637− ?: =(a +(b)) :: line 4
638− b :: line 5
639− $(b +(b)) :: line 6
640−
641−Or for fun, on one line:
642−
643− =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b))))))
644−
645−Does Hoon actually work?
646−
647− ~tasfyn-partyv> =>(42 =>(a=. =+(b=0 |-(?:(=(a +(b)) b $(b +(b)))))))
648− 41
649−
650−Let's translate this into English. How do we decrement the
651−subject? First (line 1), we rename the subject `a`. Second
652−(line 2), we add a variable, `b`, an atom with value `0`.
653−Third (line 3), we loop. Fourth, we test if `a` equals `b` plus
654−1 (line 4), produce `b` if it does (line 5), repeat the loop with
655−`b` set to `b` plus 1 (line 6) if it doesn't. Obviously, while
656−the syntax is unusual, the algorithm is anything but deep. We
657−are calculating `b` minus one by counting up from `0`.
658−
659−(Obviously, this is an O(n) algorithm. Is there a better way?
660−There is not. Do we actually do this in practice? Yes and no.)
661−
662−Unfortunately we are missing a third of our Rosetta stone. We
663−have decrement in Hoon and we have it in English. How do we
664−express this in Nock? What will the Hoon compiler generate from
665−the code above? Let's work through it line by line.
666−
667−Nock has no types, variable names, etc. So line 1 is a no-op.
668−
669−How do we add a variable (line 2)? We compute a new subject,
670−which is a cell of the present subject and the variable. With
671−this new subject, we execute another formula.
672−
673−Since `0` is a constant, a formula that produces it is
674−
675− [1 0]
676−
677−To combine `0` with the subject, we compute
678−
679− [[1 0] [0 1]]
680−
681−which, if our subject is 42, gives us
682−
683− [0 42]
684−
685−which we can use as the subject for an inner formula, `g`.
686−Composing our new variable with `g`, we have `f` as
687−
688− [2 [[1 0] [0 1]] [1 g]]
689−
690−which seems a little funky for something so simple. But we
691−can simplify it with the composition macro, `7`:
692−
693− [7 [[1 0] [0 1]] g]
694−
695−and still further with the augmentation macro, `8`:
696−
697− [8 [1 0] g]
698−
699−If you refer back to the Nock definition, you'll see that all
700−these formulas are semantically equivalent.
701−
702−Let's continue with our decrement. So what's `g`? We seem to
703−loop. Does Nock have a loop operator? It most certainly does
704−not. So what do we do?
705−
706−We build a noun called a _core_ - a construct which is behind any
707−kind of interesting control flow in Hoon. Of course, the Nock
708−programmer is not constrained to use the same techniques as the
709−Hoon compiler, but it is probably a good idea.
710−
711−In Hoon, all the flow structures from your old life as an Earth
712−programmer become cores. Functions and/or closures are cores,
713−objects are cores modules are cores, even loops are cores.
714−
715−The core is just a cell whose tail is data (possibly containing
716−other cores) and whose head is code (containing one or more
717−formulas). The tail is the _payload_ and the head is the
718−_battery_. Hence your core is
719−
720− [bat pay]
721−
722−To activate a core, pick a formula out of the battery, and use
723−the entire core (_not_ just the payload) as the subject.
724−
725−(A core formula is called an _arm_. An arm is almost like an
726−object-oriented method, but not quite - a method would be an arm
727−that produces a function on an argument. The arm is just a
728−function of the core, ie, a computed attribute.)
729−
730−Of course, because we feed it the entire core, our arm can
731−invoke itself (or any other formula in the battery). Hence, it
732−can loop. And this is what a loop is - the simplest of cores.
733−
734−We need to do two things with this core: create it, and activate
735−it. To be precise, we need two formulas: a formula which
736−produces the core, and one which activates its subject. We can
737−compose these functions with the handy `7` operator:
738−
739− [8 [1 0] [7 p a]]
740−
741−`p` produces our core, `a` activates it. Let's take these in
742−reverse order. How do we activate a core?
743−
744−Since we have only one formula, it's the battery itself.
745−Thus we want to execute Nock with the whole core (already the
746−subject, and the entire battery (slot `2`). Hence, `a` is
747−
748− [2 [0 1] [0 2]]
749−
750−We could also use the handy `9` macro - which almost seems
751−designed for firing arms on cores:
752−
753− [9 2 [0 1]]
754−
755−Which leaves us seeking
756−
757− [8 [1 0] [7 p [9 2 0 1]]]
758−
759−And all we have to do is build the core, `p`. How do we build a
760−core? We add code to the subject, just as we added a variable
761−above. The initial value of our counter was a constant, `0`.
762−The initial (and permanent) value of our battery is a constant,
763−the loop formula `l`. So `p` is
764−
765− [8 [1 l] [0 1]]
766−
767−Which would leave us seeking
768−
769− [8 [1 0] [7 [8 [1 l] [0 1]] [9 2 0 1]]]
770−
771−except that we have duplicated the `8` pattern again, since we
772−know
773−
774− [7 [8 [1 l] [0 1]] [9 2 0 1]]
775−
776−is equivalent to
777−
778− [8 [1 l] [9 2 0 1]]
779−
780−so the full value of `f` is
781−
782− [8 [1 0] [8 [1 l] [9 2 0 1]]]
783−
784−Thus our only formula to compose is the loop body, `l`.
785−Its subject is the loop core:
786−
787− [bat pay]
788−
789−where `bat` is just the loop formula, and `pay` is the pair `[a
790−b]`, `a` being the input subject, and `b` the counter. Thus we
791−could also write this subject as
792−
793− [l b a]
794−
795−and we see readily that `a` is at slot `7`, `b` `6`, `l` `2`.
796−With this subject, we need to express the Hoon loop body
797−
798− ?: =(a +(b)) :: line 4
799− b :: line 5
800− $(b +(b)) :: line 6
801−
802−This is obviously an if statement, and it calls for `6`. Ie:
803−
804− [6 t y n]
805−
806−Giving our decrement program as:
807−
808− [8 [1 0] [8 [1 6 t y n] [9 2 0 1]]]
809−
810−For `t`, how do we compute a flag that is yes (`0`) if `a` equals
811−`b` plus one? Equals, we recall, is `5`. So `t` can only be
812−
813− [5 [0 7] [4 0 6]]
814−
815−If so, our product `y` is just the counter `b`:
816−
817− [0 6]
818−
819−And if not? We have to re-execute the loop with the counter
820−incremented. If we were executing it with the same counter,
821−obviously an infinite loop, we could use the same core:
822−
823− [9 2 0 1]
824−
825−But instead we need to construct a new core with the counter
826−incremented:
827−
828− [l +(b) a]
829−
830−ie,
831−
832− [[0 2] [4 0 6] [0 7]]
833−
834−and `n` is:
835−
836− [9 2 [[0 2] [4 0 6] [0 7]]]
837−
838−Hence our complete decrement. Let's reformat vertically so we
839−can actually read it:
840−
841− [8
842− [1 0]
843− [ 8
844− [ 1
845− [ 6
846− t
847− y
848− n
849− ]
850− ]
851− [9 2 0 1]
852− ]
853− ]
854−
855−which becomes
856−
857− [8
858− [1 0]
859− [ 8
860− [ 1
861− [ 6
862− [5 [0 7] [4 0 6]]
863− [0 6]
864− [9 2 [[0 2] [4 0 6] [0 7]]]
865− ]
866− ]
867− [9 2 0 1]
868− ]
869− ]
870−
871−or, on one line without superfluous brackets:
872−
873− [8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1]
874−
875−which works for the important special case, 42:
876−
877− ~tasfyn-partyv> .*(42 [8 [1 0] 8 [1 6 [5 [0 7] 4 0 6] [0 6] 9 2 [0 2] [4 0 6] 0 7] 9 2 0 1])
878− 41
879−
880−If you understood this, you understand Nock. At least in principle!
881−
882−If you want to play around more with Nock, the command line will
883−start getting unwieldy. Fortunately, the standard install
884−contains the above Nock decrement packaged as an Arvo app, which
885−you can edit and change if you'd like to get ambitious. Just run
886−
887− ~tasfyn-partyv> :toy/ndec 19
888− 18
889−
890−The file driving this is
891−
892− hub/$seat/toy/app/ndec.holw
893−
894−Edit this file, ignoring everything above the Nock formula, and
895−hit return in the console to see it update:
896−
897− ~tasfyn-partyv>
898− : ~tasfyn-partyv/toy/app/ndec/holw/
899−
900−If decrement seems fun - why not write add? I wrote a Nock adder
901−a long, long time ago. But I've forgotten where I put it. There
902−is absolutely no use in this exercise, except to prove to
903−yourself that you've mastered Nock.
904−
905−#Appendix A: Operator Reductions#
906−
907−##`6` Reduction:##
908−
909− 28 :: *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
910−
911− *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
912−
913− 23 :: *[a 2 b c] *[*[a b] *[a c]]
914−
915− *[*[a 0 1] *[a 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]]
916−
917− 21 :: *[a 0 b] /[b a]
918−
919− *[a *[a 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]]
920−
921− 23 :: *[a 2 b c] *[*[a b] *[a c]]
922−
923− *[a *[*[a [1 c d]] *[a [1 0] 2 [1 2 3] [1 0] 4 4 b]]]
924−
925− 22 :: *[a 1 b] b
926−
927− 19 :: *[a [b c] d] [*[a b c] *[a d]]
928−
929− *[a *[[c d] [*[a 1 0] *[a 2 [1 2 3] [1 0] 4 4 b]]]]
930−
931− 22 :: *[a 1 b] b
932−
933− *[a *[[c d] [0 *[a 2 [1 2 3] [1 0] 4 4 b]]]]
934−
935− 23 :: *[a 2 b c] *[*[a b] *[a c]]
936−
937− *[a *[[c d] [0 *[*[a [1 2 3]] *[a [1 0] 4 4 b]]]]]
938−
939− 22 :: *[a 1 b] b
940−
941− *[a *[[c d] [0 *[[2 3] *[a [1 0] 4 4 b]]]]]
942−
943− 19 :: *[a [b c] d] [*[a b c] *[a d]]
944−
945− *[a *[[c d] [0 *[[2 3] [*[a [1 0]] *[a 4 4 b]]]]]]
946−
947− 22 :: *[a 1 b] b
948−
949− *[a *[[c d] [0 *[[2 3] [0 *[a 4 4 b]]]]]]
950−
951− 25 :: *[a 4 b] +*[a b]
952−
953− *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
954−
955−**`6` Reduced:**
956−
957− 6r :: *[a 6 b c d] *[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]
958−
959−##`7` Reduction:##
960−
961− 29 :: *[a 7 b c] *[a 2 b 1 c]
962−
963− *[a 2 b 1 c]
964−
965− 23 :: *[a 2 b c] *[*[a b] *[a c]]
966−
967− *[*[a b] *[a 1 c]]
968−
969− 22: *[a 1 b] b
970−
971− *[*[a b] c]
972−
973−**`7` Reduced:**
974−
975− 7r :: *[a 7 b c] *[*[a b] c]
976−
977−##`8` Reduction:##
978−
979− 30 :: *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
980−
981− *[a 7 [[7 [0 1] b] 0 1] c]
982−
983− 7r :: *[a 7 b c] *[*[a b] c]
984−
985− *[*[a [7 [0 1] b] 0 1]] c]
986−
987− 19 :: *[a [b c] d] [*[a b c] *[a d]]
988−
989− *[[*[a [7 [0 1] b]] *[a 0 1]] c]
990−
991− 21 :: *[a 0 b] /[b a]
992−
993− *[[*[a [7 [0 1] b]] /[1 a]] c]
994−
995− 12 :: /[1 a] a
996−
997− *[[*[a [7 [0 1] b]] a] c]
998−
999− 7r :: *[a 7 b c] *[*[a b] c]
1000−
1001− *[[*[*[a 0 1]] b] a] c]
1002−
1003−**`8` Reduced:**
1004−
1005− 8r :: *[a 8 b c] *[[*[a b] a] c]
1006−
1007−
1008−##`9` Reduction:##
1009−
1010− 31 :: *[a 9 b c] *[a 7 c [2 [0 1] [0 b]]]
1011−
1012− *[a 7 c [2 [0 1] [0 b]]]
1013−
1014− 7r :: *[a 7 b c] *[*[a b] c]
1015−
1016− *[*[a c] [2 [0 1] [0 b]]]
1017−
1018− 23 :: *[a 2 b c] *[*[a b] *[a c]]
1019−
1020− *[*[*[a c] [0 1]] *[*[a c] [0 b]]]
1021−
1022− 21 :: *[a 0 b] /[b a]
1023−
1024−**`9` Reduced:**
1025−
1026− 9r :: *[a 9 b c] *[*[a c] *[*[a c] 0 b]]
1027−
1028−
1029−##`10` Reduction:##
1030−
1031− *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
1032−
1033− 8r :: *[a 8 b c] [[*[a b] a] c]
1034−
1035− *[[*[a c] a] 7 [0 2] d]
1036−
1037− 7r :: *[a 7 b c] *[*[a b] c]
1038−
1039− *[*[[*[a c] a] 0 3] d]
1040−
1041− 21 :: *[a 0 b] /[b a]
1042−
1043−**`10` reduced:**
1044−
1045− 10r :: *[a 10 [b c] d] *[a d]
docs/using.md+1−1
@@ -4,4 +4,4 @@ sort: 2
4
4
5# Using
5# Using
6
6
7−<list />
7+<list/>