on 705ca06 help sign in
272 lines 10 kB history raw
1+++
2title = "1.4 Gates (Hoon Functions)"
3weight = 6
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/gates/"]
6+++
8In this lesson you're going to learn about Hoon functions.
10## What is a Function?
12The word "function" is used in various ways, but let's start by talking about them in the [mathematical sense](https://en.wikipedia.org/wiki/Function_%28mathematics%29). Roughly put, a function takes one or more arguments (i.e., input values) and returns a value. The return value depends solely on the argument(s), and nothing else. For example, we can understand multiplication as a function: it takes two numbers and returns another number. It doesn't matter where you ask, when you ask, or what kind of hat you're wearing when you ask. If you pass the same two numbers (e.g., `3` and `4`), you get the same answer returned every time (`12`).
14That output value depends solely upon input value(s) is an important property of functions. This property is called [referential transparency](https://en.wikipedia.org/wiki/Referential_transparency), and we make use of it throughout the Urbit software stack.
16Functions are implemented in Hoon with a special kind of [core](/docs/glossary/core/) called a **gate**. In this lesson you'll learn what a [gate](/docs/glossary/gate/) is and how a gate represents a function. Along the way you'll build some example gates of your own.
18You've already used two functions from the Hoon standard library: `add` and `mul`. These are implemented with gates, and you call them from the Dojo in the following way:
20```
21> (add 2 3)
24> (mul 2 3)
26```
28## What is a Gate?
30A core is a cell: `[battery payload]`.
32A gate is a core with two distinctive properties: (1) the battery of a gate contains exactly one [arm](/docs/glossary/arm/), which has the special name `$`. The `$` [arm](/docs/glossary/arm/) contains the instructions for the function in question. And (2), the payload of a gate consists of a cell of `[sample context]`. The sample is the part of the payload that stores the "argument" (i.e., input value) of the function call. The context contains all other data that is needed for computing the `$` [arm](/docs/glossary/arm/) of the gate correctly.
34```
35Gate: [$ [Sample Context]]
36```
38As a tree, a gate looks like the following:
40```
41 Gate
42 / \
43 $ .
44 / \
45 Sample Context
46```
48Like all arms, `$` is computed with its parent core as the subject. When `$` is computed, the resulting value is called the "product" of the gate. No other data is used to calculate the product other than the data in the gate itself.
50## Creating Your First Gate
52Let's make a gate that takes any unsigned integer (i.e., an [atom](/docs/glossary/atom/)) as its sample and returns that value plus one as the product. To do this we'll use the `|=` rune. We'll bind this gate to the face `inc` for "increment":
54```
55> =inc |=(a=@ (add 1 a))
57> (inc 1)
60> (inc 12)
6113
63> (inc 123)
64124
65```
67The gate works as promised -- it takes any number `n` and returns `n+1`. Let's take a closer look at what the `|=` is doing.
69### The `|=` Rune
71We typically use the `|=` rune to create a gate. In the expression above the `|=` is immediately followed by a set of parentheses containing two subexpressions: `a=@` and `(add 1 a)`. The first defines the gate's sample (input value type), and the second defines the gate's product (value).
73In the example gate above, `inc`, the sample is defined by `a=@`. This means that the sample is defined as an atom, `@`, meaning that the gate will take as input anything of that type. The sample is given the face `a`. With the face it's easier to refer to the sample in later code, if desired.
75In `inc`, the product is defined by `(add 1 a)`. There's not much to it -- it returns the value of `a+1`!
77The second subexpression after the `|=` rune is used to build the gate's `$` arm. That's where all the computations go.
79## Anatomy of a Gate
81A gate is a one-armed core with a sample: `[$ [Sample Context]]`. Let's go over these parts a little more carefully, using `inc` as our example.
83```
84> inc
85< 1.jgn
86 { a/@
87 {our/@p now/@da eny/@uvJ}
88 <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
89 }
91```
93### The `$` Arm
95The arm of a gate encodes the instructions for the Hoon function in question.
97The pretty printer represents the `$` arm of `inc` as `1.jgn`. To see the actual [noun](/docs/glossary/noun/) of the `$` arm, enter `+2:inc` into the Dojo:
99```
100> +2:inc
101[8 [9 8 0 4.095] 9 2 [0 4] [[7 [0 3] 1 1] 0 14] 0 11]
102```
104This is un-computed Nock. You don't need to understand any of this.
106It's worth pointing out that the arm name, `$`, can be used like any other name. We can compute `$` directly with `$:inc` in the Dojo:
108```
109> $:inc
111```
113This result may seem a bit strange. We didn't call `inc` or in any other way pass it a number. Yet using `$` to evaluate `inc`'s arm seems to work -- sort of, anyway. Why is it giving us `1` as the return value? We can answer this question after we understand gate samples a little better.
115### The Sample
117The sample of a gate is the address reserved for storing the argument(s) to the Hoon function. The sample is always at the head of the gate's tail, i.e., `+6`.
119Let's look at the gate for `inc` again, paying particular attention to its sample:
121```
122> inc
123< 1.jgn
124 { a/@
125 {our/@p now/@da eny/@uvJ}
126 <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
127 }
129```
131We see `a/@`. This may not be totally clear, but it should make _some_ sense. This is the pretty-printer's way of indicating an atom with the face `a`. Let's take a closer look:
133```
134> +6:inc
135a=0
136```
138We see now that the sample of `inc` is the value `0`, and has `a` as a face. This is a placeholder value for the function argument. If you evaluate the `$` arm of `inc` without passing it an argument the placeholder value is used for the computation, and the return value will thus be `0+1`:
140```
141> $:inc
143```
145The placeholder value is sometimes called a **bunt** value. The bunt value is determined by the input type; for atoms, `@`, the bunt value is `0`.
147The face value of `a` comes from the way we defined the gate above: `|=(a=@ (add 1 a))`. It was so we could use `a` to refer to the sample to generate the product with `(add 1 a)`.
149### The Context
151The context of a gate contains other data that may be necessary for the `$` arm to evaluate correctly. The context is always located at the tail of the tail of the gate, i.e., `+7` of the gate. There is no requirement for the context to have any particular arrangement, though often it does.
153Let's look at the context of `inc`:
155```
156> +7:inc
157[ [ our=~zod
158 now=~2018.12.12..18.55.06..df2b
159 eny
160 \/0v2g0.cmm6d.1igpo.llg7o.4921d.nov38.vjsgp.mdfln.8pt9f.n23hj.qc6q9.8p7bd\/
161 .2eo59.qko43.1jgd2.rtc44.egooe.esvft.orohc.i9ufn.4rnur
162 \/ \/
163 ]
164 <19.anu 24.tmo 6.ipz 38.ard 119.spd 241.plj 51.zox 93.pqh 74.dbd 1.qct $141>
166```
168This is exactly the default Dojo subject, from before we put `inc` into the subject. The `|=` expression defines the context as whatever the subject is. This guarantees that the context has all the information it needs to have for the `$` arm to work correctly.
170### Exercise 1.4.1a
172Write a gate that takes an atom, `a=@`, and which returns double the value of `a`. Bind this gate to `double` and test it in the Dojo. A solution is given at the end of this lesson.
174## Gates Define Functions of the Sample
176The value of a function's output depends solely upon the input value. This is one of the features that make functions desirable in many programming contexts. It's worth going over how Hoon function calls implement this feature.
178In Hoon, one can use `(gate arg)` syntax to make a function call. For example:
180```
181> (inc 234)
182235
183```
185The name of the gate is `inc`. How is the `$` arm of `inc` evaluated? When a function call occurs, a copy of the `inc` gate is created, but with one modification; the sample is replaced with the function argument. Then the `$` arm is computed against this modified version of the `inc` gate.
187Remember that the default or "bunt" value of the sample of `inc` is `0`. In the function call above, a copy of the `inc` gate is made but with a sample value of `234`. When `$` is computed against this modified core, the product is `235`.
189Notice that neither the arm nor the context is modified before the arm is evaluated. That means that the only part of the gate that changes before the arm evaluation is the sample. Hence, we may understand each gate as defining a function whose argument is the sample. If you call a gate with the same sample, you'll get the same value returned to you every time.
191Let's unbind `inc` to keep the subject tidy:
193```
194> =inc
196> inc
197-find.inc
198```
200### Modifying the Context of a Gate
202It _is_ possible to modify the context of a gate when you make a function call; or, to be more precise, it's possible to call a mutant copy of the gate in which the context is modified. To illustrate this let's use another example gate. Let's write a gate which uses a value from the context to generate the product. Bind `b` to the value `10`:
204```
205> =b 10
207> b
20810
209```
211Now let's write a gate called `ten` that adds `b` to the input value:
213```
214> =ten |=(a=@ (add a b))
216> (ten 10)
21720
219> (ten 20)
22030
222> (ten 25)
22335
224```
226We can unbind `b` from the Dojo subject, and `ten` works just as well because it's using a copy of `b` stored its context:
228```
229> =b
231> (ten 15)
23225
234> (ten 35)
23545
237> b.+14.ten
23810
239```
241We can use `ten(b 25)` to produce a variant of `ten`. Calling this mutant version of `ten` causes a different value to be returned than we'd get with a normal `ten` call:
243```
244> (ten(b 25) 10)
24535
247> (ten(b 1) 25)
24826
250> (ten(b 75) 100)
251175
252```
254Before finishing the lesson let's unbind `ten`:
256```
257> =ten
258```
260## Exercise 1.4.1a: Answer
262Write a gate that takes an atom, `a=@`, and which returns double the value of `a`. Bind this gate to `double` and test it in the Dojo.
264```
265> =double |=(a=@ (mul 2 a))
267> (double 10)
26820
270> (double 25)
27150
272```