on master help sign in
Merge pull request #569 from urbit/rob-edits
33496efrmariani
learn/hoon/hoon-tutorial/iron-polymorphism.md+3−3
@@ -1,5 +1,5 @@
1+++
1+++
2−title = "Walkthrough: Iron Polymorphism and Wet Polymorphism"
2+title = "2.5.1 Walkthrough: Iron Polymorphism and Wet Polymorphism"
3weight = 32
3weight = 32
4template = "doc.html"
4template = "doc.html"
5+++
5+++
@@ -11,7 +11,7 @@ Polymorphism is a programming concept that allows a a piece of code to use diffe
11A dry gate (also simply a _gate_) is the kind that you're already familiar with by now: a one-armed core with a sample. A wet gate is also a one-armed core with a sample, but there is a difference. With a dry gate, when you pass in an argument and the code gets compiled, the type system will try to cast to the type specified by the gate; if you pass something that does not fit in the specified type, for example a `cord` instead of a `cell` you will get a nest failure. On the other hand, when you pass arguments to a wet gate, their types are preserved and type analysis is done at the definition site of the gate -- in our case, `fold` in `hoon.hoon` -- rather than the call site.
11A dry gate (also simply a _gate_) is the kind that you're already familiar with by now: a one-armed core with a sample. A wet gate is also a one-armed core with a sample, but there is a difference. With a dry gate, when you pass in an argument and the code gets compiled, the type system will try to cast to the type specified by the gate; if you pass something that does not fit in the specified type, for example a `cord` instead of a `cell` you will get a nest failure. On the other hand, when you pass arguments to a wet gate, their types are preserved and type analysis is done at the definition site of the gate -- in our case, `fold` in `hoon.hoon` -- rather than the call site.
12
12
13```
13```
14−++ fold
14+++ fold
15 |* [state=mold elem=mold]
15 |* [state=mold elem=mold]
16 |= [[st=state xs=(list elem)] f=$-([state elem] state)]
16 |= [[st=state xs=(list elem)] f=$-([state elem] state)]
17 ^- state
17 ^- state
@@ -87,4 +87,4 @@ Here `fold` will produce a gate that takes an `atom` and applies a gate to a `li
87The key takeaway from both of these examples is that the gates provided are _iron polymorphic_ with respect to the definition of the type in `fold`. They are iron polymorphic because samples `s` and `e` nest under the types `state` and `elem`. In second case case, that's because when we provided those to `fold`, it was was stated they were `@` and `@`. In the first case, we stated that `state` was `(list @)` and `elem` was `@`. In both cases, the sample of each gate nest inside the types defined when we called the wet gate `fold`.
87The key takeaway from both of these examples is that the gates provided are _iron polymorphic_ with respect to the definition of the type in `fold`. They are iron polymorphic because samples `s` and `e` nest under the types `state` and `elem`. In second case case, that's because when we provided those to `fold`, it was was stated they were `@` and `@`. In the first case, we stated that `state` was `(list @)` and `elem` was `@`. In both cases, the sample of each gate nest inside the types defined when we called the wet gate `fold`.
88
88
89
89
90−[Next Up: Reading -- Gall](../gall)
90+[Next Up: Walkthrough -- Lead Polymorphism](../lead-polymorphism)
learn/hoon/hoon-tutorial/lead-polymorphism.md+22−14
@@ -1,10 +1,12 @@
1+++
1+++
2−title = "Walkthrough: Lead Polymorphism"
2+title = "2.5.2 Walkthrough: Lead Polymorphism"
3weight = 33
3weight = 33
4template = "doc.html"
4template = "doc.html"
5+++
5+++
6
6
7−There 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. Lead cores have opaque payloads. That is, the payload can not be written to or read from. Let's take an example of using lead cores.
7+There 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.
8+
9+Lead 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`.
8
10
9```
11```
10=< (to-list (take fib 10))
12=< (to-list (take fib 10))
@@ -54,20 +56,22 @@ There are four kinds of cores: gold, iron, zinc, and lead. You are able to use c
54--
56--
55```
57```
56
58
57−There are 5 arms in this core: `stream`, `stream-type`, `to-list`, `take`, and `fib`.
59+There are five arms in this core: `stream`, `stream-type`, `to-list`, `take`, and `fib`.
58
60
59
61
60```
62```
61++ stream
63++ stream
62 |* of=mold
64 |* of=mold
63− $_ ^? |.
65+ $_ ^? |.
64 ^- $@(~ [item=of more=^$])
66 ^- $@(~ [item=of more=^$])
65 ~
67 ~
66```
68```
67
69
68−`stream` mold builder. It is a wet gate that takes one argument `of` which is a `mold`. `$_` is a rune that produces a type from an example. Here we're going to create a `trap` with `|.` and cast it to lead with `^?`. This means that it's payload will be inaccessible.
70+`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.
71+
72+`$_` 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.
69
73
70−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 half describes a cell with 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.
74+With 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.
71
75
72The 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.
76The 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.
73
77
@@ -81,7 +85,9 @@ Now you can see that a `stream` is either `~` or a pair of a value of some type
81 item
85 item
82```
86```
83
87
88+`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.
89+
84−`stream-type` is a wet gate that produces the type of items stored in the `stream`. Since calling 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 `!!`
90+Calling 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 `!!`.
85
91
86```
92```
87++ take
93++ take
@@ -99,9 +105,9 @@ Now you can see that a `stream` is either `~` or a pair of a value of some type
99 ==
105 ==
100```
106```
101
107
102−We'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` passed in.
108+We'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.
103
109
104−If `i` and `n` are equal, the trap will produce `~`. Otherwise, `s` is kicked and it's result put on the front of the subject. If it's 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` the 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.
110+If `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.
105
111
106```
112```
107++ to-list
113++ to-list
@@ -117,9 +123,9 @@ If `i` and `n` are equal, the trap will produce `~`. Otherwise, `s` is kicked an
117 ==
123 ==
118```
124```
119
125
120−Now with that in hand, let's go back to `to-list`. Again it's a wet gate that takes `s`, a `stream` only here it will, as you might expect, produce a `list`. The rest of this gate is straight forward 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.
126+Now 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.
121
127
122−`r` is added to the subject as an empty `list` of whatever type is produced by `s`. A new trap is formed and kicked and will produce the same type as `r`. Then `s` is kicked and it's 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.
128+`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.
123
129
124```
130```
125++ fib
131++ fib
@@ -132,17 +138,19 @@ Now with that in hand, let's go back to `to-list`. Again it's a wet gate that ta
132 ==
138 ==
133```
139```
134
140
135−The final arm in this 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.
141+The 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.
136
142
137
143
138```
144```
139=< (to-list (take fib 10))
145=< (to-list (take fib 10))
140```
146```
141
147
142−Finally, the first line of our program, will take the first 10 elements of `fib` and produce them as a list.
148+Finally, the first line of our program will take the first 10 elements of `fib` and produce them as a list.
143
149
144```
150```
145~[1 1 2 3 5 8 13 21 34 55]
151~[1 1 2 3 5 8 13 21 34 55]
146```
152```
147
153
148−This example is a bit over kill 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.
154+This 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.
155+
156+[Next Up: Reading -- Gall](../gall)