on ff18ced help sign in
urbit.org/tutorials/hoon/lead-polymorphism.md
157 lines 6.1 kB history raw
1+++
2title = "2.5.2 Walkthrough: Lead Polymorphism"
3weight = 33
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/lead-polymorphism/"]
6+++
8There are four kinds of cores: gold, iron, zinc, and lead. You are able to use core-variance rules to create programs which take other programs as arguments. Which particular rules depends on which kind of core your program needs to complete.
10Lead cores have opaque payloads. That is, the payload can not be written to or read from. Below is an example of using lead cores. The program produces a list populated by the first 10 elements of `fib`.
12```hoon
13=< (to-list (take fib 10))
14|%
15++ stream
16 |* of=mold
17 $_ ^? |.
18 ^- $@(~ [item=of more=^$])
19 ~
20++ stream-type
21 |* s=(stream)
22 $_ => (s)
23 ?~ . !!
24 item
25++ to-list
26 |* s=(stream)
27 %- flop
28 =| r=(list (stream-type s))
29 |- ^+ r
30 =+ (s)
31 ?~ - r
32 %= $
33 r [item r]
34 s more
35 ==
36++ take
37 |* [s=(stream) n=@]
38 =| i=@
39 ^+ s
40 |.
41 ?: =(i n) ~
42 =+ (s)
43 ?~ - ~
44 :- item
45 %= ..$
46 i +(i)
47 s more
48 ==
49++ fib
50 ^- (stream @ud)
51 =+ [p=0 q=1]
52 |. :- q
53 %= .
54 p q
55 q (add p q)
56 ==
57--
58```
60There are five arms in this core: `stream`, `stream-type`, `to-list`, `take`, and `fib`.
63```hoon
64++ stream
65 |* of=mold
66 $_ ^? |.
67 ^- $@(~ [item=of more=^$])
68 ~
69```
71`stream` is a mold-builder. It's a wet gate that takes one argument, `of`, which is a `mold`, and produces a trap -- a function with no argument.
73`$_` is a rune that produces a type from an example; `^?` converts (casts) a core to lead; `|.` forms the trap. So to follow this sequence we read it backwards: we create a trap, convert it to a lead trap (making its payload inaccessible), and then use that lead trap as an example to produce a type from.
75With the line `^- $@(~ [item=of more=^$])`, the output of the trap will be cast into a new type. `$@` is the rune to describe a data structure that can either be an atom or a cell. The first part describes the atom, which here is going to be `~`. The second part describes a cell, which we define to have the head of type `of` with the face `item`, and a tail with a face of `more`. The expression `^$` is not a rune, but rather a reference to the enclosing wet gate, so the tail of this cell will be of the same type produced by this wet gate.
77The final `~` here is used as the type produced when initially calling this wet gate. This is valid because it nests within the type we defined on the previous line.
79Now you can see that a `stream` is either `~` or a pair of a value of some type and a `stream`. This type represents an infinite series.
81```hoon
82++ stream-type
83 |* s=(stream)
84 $_ => (s)
85 ?~ . !!
86 item
87```
89`stream-type` is a wet gate that produces the type of items stored in the `stream` arm. The `(stream)` syntax is a shortcut for `(stream *)`; a stream of some type.
91Calling a `stream`, which is a trap, will either produce `item` and `more` or it will produce `~`. If it does produce `~`, the `stream` is empty and we can't find what type it is, so we simply crash with `!!`.
93```hoon
94++ take
95 |* [s=(stream) n=@]
96 =| i=@
97 ^+ s
98 |.
99 ?: =(i n) ~
100 =+ (s)
101 ?~ - ~
102 :- item
103 %= ..$
104 i +(i)
105 s more
106 ==
107```
109We're going to skip over `to-list` and examine `take` first. `take` is another wet gate. This time it takes a `stream` `s` and an atom `n`. We add an atom to the subject and then make sure that the trap we are creating is going to be of the same type as `s`, the `stream` we passed in.
111If `i` and `n` are equal, the trap will produce `~`. Otherwise, `s` is called and has its result put on the front of the subject. If its value is `~`, then the trap again produces `~`. Otherwise the trap produces a cell of `item`, the first part of the value of `s`, and a new trap that increments `i`, and sets `s` to be the `more` trap which produces the next value of the `stream`. The result here is a `stream` that will only ever produce `n` items, even if the stream otherwise would have been infinite.
113```hoon
114++ to-list
115 |* s=(stream)
116 %- flop
117 =| r=(list (stream-type s))
118 |- ^+ r
119 =+ (s)
120 ?~ - r
121 %= $
122 r [item r]
123 s more
124 ==
125```
127Now with that in hand, let's go back to `to-list`. It's also a wet gate that takes `s`, a `stream`, only here it will, as you might expect, produce a `list`. The rest of this wet gate is straightforward, but we can examine it quickly anyway. As is the proper style, this list that is produced will be reversed, so `flop` is used to put it in the order it is in the stream. Recall that adding to the front of a list is cheap, while adding to the back is expensive.
129`r` is added to the subject as an empty `list` of whatever type is produced by `s`. A new trap is formed and called, and it will produce the same type as `r`. Then `s` is called and has its value added to the subject. If the result is `~`, the trap produces `r`. Otherwise, we want to call the trap again, adding `item` to the front of `r` and changing `s` to `more`. Now the utility of `take` should be clear. We don't want to feed `to-list` an infinite stream as it would never terminate.
131```hoon
132++ fib
133 ^- (stream @ud)
134 =+ [p=0 q=1]
135 |. :- q
136 %= .
137 p q
138 q (add p q)
139 ==
140```
142The final arm in our core is `fib`, which is a `stream` of `@ud` and therefore an iron core. It's subject contains `p` and `q`, which will not be accessible outside of this trap. The product of the trap is a pair of an `@ud` and the trap that will produce the next `@ud` in the Fibonacci series.
145```hoon
146=< (to-list (take fib 10))
147```
149Finally, the first line of our program will take the first 10 elements of `fib` and produce them as a list.
151```
152~[1 1 2 3 5 8 13 21 34 55]
153```
155This example is a bit overkill for simply calculating the Fibonacci series, but it nicely illustrates how you might use iron cores. Instead of `fib`, you could have any `stream` that produces some stream of values and you'd still be able to use the `take`, `to-list`, and `stream-type` arms.
157[Next Up: Reading -- Gall](../gall)