on master help sign in
parametric polymorphism done with |$
3af6460JONATHAN PAPROCKI
tutorials/hoon/molds.md+10−2
@@ -5,16 +5,24 @@ template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/molds/"]
5aliases = ["/docs/learn/hoon/hoon-tutorial/molds/"]
6+++
6+++
7
7
8−A mold is a function that coerces a [noun](/docs/glossary/noun/) to a type or crashes.
8+A mold is an idempotent function that coerces a [noun](/docs/glossary/noun/) to
9+be of a specific type or crashes.
9
10
10−Let's take some examples from `hoon.hoon`. `|$` is a new rune as of the writing of this document so you may see this spelled in slightly different way if you don't have the latest version of `hoon.hoon`. `|$` is the mold builder rune which takes a list of molds and produces a mold.
11+`|$` is a new rune as of the writing of this document so you may see this spelled in slightly different way if you don't have the latest version of `hoon.hoon`. `|$` is the mold builder rune which takes a list of molds and produces a mold.
11
12
13+`|$` implements [parametric
14+polymorphism](https://en.wikipedia.org/wiki/Parametric_polymorphism) in Hoon,
15+and as such we call gates produced with `|$` a **wet gate**. We discuss what this
16+means in further detail in the upcoming lesson on [polymorphism](@/docs/tutorials/hoon/type-polymorphism.md).
17+
18+Let's look at some examples from `hoon.hoon`.
12```hoon
19```hoon
13++ pair
20++ pair
14 |$ [head tail]
21 |$ [head tail]
15 [p=head q=tail]
22 [p=head q=tail]
16```
23```
17
24
25+
18Here is a very simple mold builder. It takes two molds and produces a mold that is a pair of those with the faces `p` and `q`. An example of using this would be `(pair @ud @ud)` which would produce a mold for a cell of `@ud` and `@ud`.
26Here is a very simple mold builder. It takes two molds and produces a mold that is a pair of those with the faces `p` and `q`. An example of using this would be `(pair @ud @ud)` which would produce a mold for a cell of `@ud` and `@ud`.
19
27
20```hoon
28```hoon
tutorials/hoon/type-polymorphism.md+14−6
@@ -189,14 +189,22 @@ It is good practice to include a cast in all gates, even wet gates. But in many
189
189
190As is the case with dry arms, there is a type-check associated with each wet arm. Or, rather, there are potentially many checks for each wet arm, one for each wing in the code that resolves to a wet arm. The product type of that arm is inferred at each call site, using the argument value type given at that site. If the type inference doesn't go through -- e.g., if there is a cast that doesn't succeed relative to the call site argument value -- then the compile will crash with a `nest-fail`. Otherwise the type check goes through with the inferred type of the wet arm at that call site. So the product type of a wet gate can differ from one call site to another, as we saw with `switch` above.
190As is the case with dry arms, there is a type-check associated with each wet arm. Or, rather, there are potentially many checks for each wet arm, one for each wing in the code that resolves to a wet arm. The product type of that arm is inferred at each call site, using the argument value type given at that site. If the type inference doesn't go through -- e.g., if there is a cast that doesn't succeed relative to the call site argument value -- then the compile will crash with a `nest-fail`. Otherwise the type check goes through with the inferred type of the wet arm at that call site. So the product type of a wet gate can differ from one call site to another, as we saw with `switch` above.
191
191
192−### Parametric Polymorphism and `+*` Arms
192+### Parametric Polymorphism and `|$`
193
193
194−We may use the `+*` rune to define an arm that produced a more complex type from one or more simpler types. This rune provides a way of making use of **parametric polymorphism** in Hoon.
194+We first encountered `|$` in the lesson on
195+[molds](@/docs/tutorials/hoon/molds.md) as a wet gate that is a mold builder rune which takes in a
196+list of molds and produces a new mold. Here we take another look at this rune as
197+an implementation of **parametric polymorphism** in Hoon.
195
198
196−For example, we have `list`s, `tree`s, and `set`s in Hoon, which are each defined in `hoon.hoon` with `+*` arms. (Take a moment to see for yourself.) Each `+*` arm is followed by an argument definition, inside brackets `[ ]`. After that subexpression comes another that defines a type, relative to the input value. For example, here is the definition of `list` from `hoon.hoon`:
199+For example, we have `list`s, `tree`s, and `set`s in Hoon, which are each
200+defined in `hoon.hoon` as wet gate mold builders. Take a moment to see for
201+yourself. Each `++` arm is followed by `|$` and a list of labels for input
202+types inside brackets `[ ]`. After that subexpression comes another that
203+defines a type that is parametrically polymorphic with respect to the input values. For example, here is the definition of `list` from `hoon.hoon`:
197
204
198```hoon
205```hoon
199−+* list [item]
206+++ list
207+ |$ [item]
200 :: null-terminated list
208 :: null-terminated list
201 ::
209 ::
202 :: mold generator: produces a mold of a null-terminated list of the
210 :: mold generator: produces a mold of a null-terminated list of the
@@ -205,9 +213,9 @@ For example, we have `list`s, `tree`s, and `set`s in Hoon, which are each define
205 $@(~ [i=item t=(list item)])
213 $@(~ [i=item t=(list item)])
206```
214```
207
215
208−The `+*` rune is especially useful for defining [containers](https://en.wikipedia.org/wiki/Container_%28abstract_data_type%29) of various kinds. Indeed, `list`s, `tree`s, and `set`s are all examples of containers. You can have a `(list @)`, a `(list ^)`, a `(list *)`, and so on. Or a `(tree @)`, a `(tree ^)`, a `(tree *)`, etc. And the same for `set`.
216+The `|$` rune is especially useful for defining [containers](https://en.wikipedia.org/wiki/Container_%28abstract_data_type%29) of various kinds. Indeed, `list`s, `tree`s, and `set`s are all examples of containers. You can have a `(list @)`, a `(list ^)`, a `(list *)`, and so on. Or a `(tree @)`, a `(tree ^)`, a `(tree *)`, etc. And the same for `set`.
209
217
210−One nice thing about containers defined by `+*` is that they nest in the expected way. Intuitively a `(list @)` should nest under `(list *)`, because `@` nests under `*`. And so it does:
218+One nice thing about containers defined by `|$` is that they nest in the expected way. Intuitively a `(list @)` should nest under `(list *)`, because `@` nests under `*`. And so it does:
211
219
212```
220```
213> =a `(list @)`~[11 22 33]
221> =a `(list @)`~[11 22 33]