urbit/pub/docs/dev/hoon/principles/1-twigs.md

287 lines
8.6 KiB
Markdown
Raw Normal View History

2015-11-03 22:49:57 +03:00
---
next: true
2015-11-03 22:49:57 +03:00
sort: 1
2015-11-10 22:55:17 +03:00
spam: true
title: Hoon 101.1: twigs and legs
2015-11-03 22:49:57 +03:00
---
# Hoon 101.1: twigs and legs
2015-11-10 22:55:17 +03:00
In the [last chapter](0-nouns), we learned how to make nouns. In
this chapter we'll get into Hoon expressions, or *twigs*.
2015-11-03 22:49:57 +03:00
2015-11-10 22:55:17 +03:00
## How to use this tutorial
Ideally, you've installed an Urbit planet (if you have a ticket)
2015-11-13 22:41:36 +03:00
or comet (if you don't). See the [user doc](../../../user).
We recommend opening up the dojo and just typing the examples;
you don't know a language until you know it in your fingers.
2015-11-13 22:41:36 +03:00
Also, make sure you've worked through the chapters in order.
2015-11-03 22:49:57 +03:00
## Nock for Hoon programmers
2015-11-10 22:55:17 +03:00
Hoon compiles itself to a pico-interpreter called
[Nock](../../nock), a combinator algebra defined in 200 words. This
isn't the place to explain Nock (which relates to Hoon much as
assembly language relates to C), but Nock is just a way to
express a function as a noun.
2015-11-03 22:49:57 +03:00
2015-11-10 22:55:17 +03:00
Nock is a Turing-complete interpreter shaped like (pseudocode):
2015-11-03 22:49:57 +03:00
```
Nock(problem) => product
```
2015-11-10 22:55:17 +03:00
The `problem` is always a cell `[subject formula]`. The
function is the `formula`. The input to the function is the
`subject`. The output is the `product`.
2015-11-03 22:49:57 +03:00
## From Hoon to Nock
The Hoon parser turns an source expression (even one as simple as
`42` from the last chapter) into a noun called a `twig`. If you
know what an AST is, a twig is an AST. (If you don't know what
2015-11-10 22:55:17 +03:00
an AST is, it's not necessarily worth the student loans.)
2015-11-03 22:49:57 +03:00
To simplify slightly, the Hoon compiler is shaped like:
```
Hoon(subject-span function-twig) => [product-span formula-nock]
```
Hoon, like Nock, is a *subject-oriented* language. Your code is
always executed against one input noun, the subject. For any
subject noun in `subject-span` (ie, argument type), the compiler
produces a Nock formula that computes `function-twig` on that
subject, and a `product-span` that is the span of the product
(ie, result type).
2015-11-10 22:55:17 +03:00
> This is really a nontrivial difference. In a normal,
2015-11-03 22:49:57 +03:00
non-subject-oriented language, your code executes against a
scope, stack, environment, or other variable context, probably
2015-11-10 22:55:17 +03:00
not even a regular user-level value. For ordinary coders,
"subject-oriented programming" is one of the hardest things to
understand about Hoon; for some reason, your brain keeps wanting
the interpreter state to be more interesting.
2015-11-03 22:49:57 +03:00
## From constants to twigs
In the last chapter we were entering degenerate twigs like `42`.
Obviously a numeric constant doesn't use the subject at all, so
2015-11-10 22:55:17 +03:00
it's not a very interesting example.
2015-11-03 22:49:57 +03:00
2015-11-10 22:55:17 +03:00
Let's save a test subject as a dojo variable:
2015-11-03 22:49:57 +03:00
```
~tasfyn-partyv:dojo> =test [[[8 9] 5] [6 7]]
```
The `=test` command tells the dojo to rearrange its stock subject
to include this `test` noun. Let's check that it's there:
```
~tasfyn-partyv:dojo> test
[[[8 9] 5] 6 7]
```
2015-11-10 22:55:17 +03:00
> If you're wondering why `[6 7]` got printed as `6 7`, remember
that `[]` associates to the right. Also, `=test` is not in any
way Hoon syntax; it's dojo syntax. Every Hoon twig is a valid
dojo command, but not vice versa.
2015-11-03 22:49:57 +03:00
We want to use `test`, this harmless little noun, as the subject
for some equally harmless twigs. We can do this with the `:`
syntax, which composes twigs in the functional sense. The twig
2015-11-10 22:55:17 +03:00
`a:b` uses the product of twig `b` as the subject of twig `a`.
Trivial cases:
2015-11-03 22:49:57 +03:00
```
~tasfyn-partyv:dojo> 42:test
42
~tasfyn-partyv:dojo> 42:420
42
```
## Tree addressing
The simplest twigs produce a subtree, or "leg", of the subject.
A cell, of course, is a binary tree. The very simplest twig is
`.`, which produces the root of the tree - the whole subject:
```
~tasfyn-partyv:dojo> .:test
[[[8 9] 5] 6 7]
```
Like human languages, Hoon is full of irregular abbreviations.
The `.` syntax is a shorthand for `+1`:
```
~tasfyn-partyv:dojo> +1:test
[[[8 9] 5] 6 7]
```
Hoon has a simple tree addressing scheme (inherited from Nock):
the root is `1`, the head of `n` is `2n`, the tail is `2n+1`.
The twig syntax for a tree address is `+n`.
In our example noun, each leaf is its own tree address:
```
~tasfyn-partyv:dojo> +2:test
[[8 9] 5]
~tasfyn-partyv:dojo> +3:test
[6 7]
~tasfyn-partyv:dojo> +4:test
[8 9]
~tasfyn-partyv:dojo> +5:test
5
~tasfyn-partyv:dojo> +6:test
6
~tasfyn-partyv:dojo> +7:test
7
```
2015-11-10 22:55:17 +03:00
> An instinct for binary tree geometry develops over time as you
2015-11-03 22:49:57 +03:00
use the system, rather the way most programmers learn to do
2015-11-10 22:55:17 +03:00
binary math. No, really.
2015-11-03 22:49:57 +03:00
## Lark syntax
This alternative syntax for a tree address maps noun geometry
2015-11-10 22:55:17 +03:00
directly to a glyph. Lark syntax creates a recognizable
geometric shape by alternating between two head/tail pairs, read
left to right: `-` and `+`, `<` and `>`.
2015-11-03 22:49:57 +03:00
Thus `-` is `+2`, `+` is `+3`, `+<` is `+6`, `->` is `+5`, `-<+`
is `+9`, etc.
2015-11-10 22:55:17 +03:00
> Why lark syntax? Code full of numbers is ugly and distracting,
2015-11-03 22:49:57 +03:00
and looks like hardcoded constants. We actually almost never use
2015-11-10 22:55:17 +03:00
the `+` syntax.
2015-11-03 22:49:57 +03:00
## Simple faces
Tree addressing is cool, but it would be pretty tough to program
in Hoon if it was the only way of getting data out of a subject.
Let's introduce some new syntax:
```
~tasfyn-partyv:dojo> foo=42
foo=42
~tasfyn-partyv:dojo> ? foo=42
foo=@ud
foo=42
~tasfyn-partyv:dojo> ?? foo=42
[%face %foo [%atom %ud]]
foo=42
```
2015-11-10 22:55:17 +03:00
To extend our `++span` mold from the last chapter:
2015-11-03 22:49:57 +03:00
```
++ span
$% [%atom p=@tas]
[%cell p=span p=span]
[%cube p=* q=span]
[%face p=@tas q=span]
==
```
The `%face` span wraps a label around a noun. Then we can
get a leg by name. Let's make a new dojo variable:
```
~tasfyn-partyv:dojo> =test [[[8 9] 5] foo=[6 7]]
```
The syntax is what you might expect:
```
~tasfyn-partyv:dojo> test
[[[8 9] 5] foo=[6 7]]
~tasfyn-partyv:dojo> foo:test
[6 7]
```
Does this do what you expect it to do?
```
~tasfyn-partyv:dojo> +3:test
foo=[6 7]
~tasfyn-partyv:dojo> ? +3:test
foo=[@ud @ud]
foo=[6 7]
~tasfyn-partyv:dojo> ?? +3:test
[%face %foo [%cell [%atom %ud] [%atom %ud]]]
foo=[6 7]
```
## Interesting faces; wings
Let's look at a few more interesting face cases. First, suppose
we have two cases of `foo`?
```
~tasfyn-partyv:dojo> =test [[foo=[8 9] 5] foo=[6 7]]
~tasfyn-partyv:dojo> foo:test
[8 9]
```
In the tree search, the head wins. We can overcome this with a
`^` prefix, which tells the search to skip its first hit:
```
~tasfyn-partyv:dojo> ^foo:test
[6 7]
```
`^^foo` will skip two foos, `^^^foo` three, *ad infinitum*.
But what about nested labels?
```
~tasfyn-partyv:dojo> =test [[[8 9] 5] foo=[6 bar=7]]
~tasfyn-partyv:dojo> bar:test
2015-11-10 22:55:17 +03:00
/~tasfyn-partyv/home/~2015.11.7..21.40.21..1aec:<[1 1].[1 9]>
2015-11-03 22:49:57 +03:00
-find-limb.bar
find-none
```
We can't search *through* a label. If we want to get our `bar`
out, we need to search *into* it:
```
~tasfyn-partyv:dojo> bar.foo:test
7
```
`bar.foo` is what we call a `wing`, a search path in a noun.
Note that the wing runs from left to right, ie, the opposite of
most languages: `bar.foo` means "bar within foo."
2015-11-10 22:55:17 +03:00
Each step in a wing is a `limb`. (Most languages use metaphors;
Hoon abuses them.) A limb can be a tree address, like `+3` or
2015-11-03 22:49:57 +03:00
`.`, or a label like `foo`. We can combine them in one wing:
```
~tasfyn-partyv:dojo> bar.foo.+3:test
7
```
It's important to note the difference between `bar.foo:test`
and `bar:foo:test`, even though they produce the same product:
```
~tasfyn-partyv:dojo> bar:foo:test
7
```
`bar.foo` is one twig, which we run on the product of `test`.
That's different from running `bar` on the product of `foo` on
the product of `test`.
2015-11-10 22:55:17 +03:00
> You're probably used to name resolution in variable scopes
and flat records, but not in trees. Partly this is because the
tradition in language design is to prefer semantics that make it
easy to build simple symbol tables, because linear search of a
2015-11-10 22:55:17 +03:00
nontrivial tree is a bad idea on '80s hardware.
2015-11-03 22:49:57 +03:00
## Mutation
Mutation? Well, not really. We can't modify nouns; the concept
2015-11-10 22:55:17 +03:00
doesn't even make sense in Hoon (or Nock).
2015-11-03 22:49:57 +03:00
2015-11-10 22:55:17 +03:00
Rather, we build new nouns which are copies of old ones, but
with mutations. Let's build a "mutated" copy of our test noun:
2015-11-03 22:49:57 +03:00
```
~tasfyn-partyv:dojo> test
[[[8 9] 5] foo=[6 bar=7]]
~tasfyn-partyv:dojo> test(foo 42)
[[[8 9] 5] foo=42]
~tasfyn-partyv:dojo> test(+8 %eight, bar.foo [%hello %world])
[[[%eight 9] 5] foo=[6 [%hello %world]]]
```
As we see, there's no need for the mutant noun to be shaped
anything like the old noun. They're different nouns.
A mutation, like `+8 %eight`, specifies a wing and a twig.
The wing, like `+8` or `bar.foo`, defines a leg to replace.
The twig runs against the original subject.
Can we use mutation to build a cyclical noun? Nice try, but no:
```
~tasfyn-partyv:dojo> test(+8 test)
[[[[[[8 9] 5] foo=[6 bar=7]] 9] 5] foo=[6 bar=7]]
```
2015-11-10 22:55:17 +03:00
## Progress
Now, not only can you build a noun, you can get data out of it and
2015-11-10 22:55:17 +03:00
even evolve new, related nouns. We've still seen only two very
2015-11-13 22:41:36 +03:00
restricted kinds of twigs: constants and legs. In the [next chapter](2-syntax), we'll actually write some interesting expressions.