on 93773de help sign in
128 lines 3.9 kB history raw
1---
2next: true
3sort: 5
4title: Marks
5---
7We've used predefined marks already, but we haven't yet created
8our own marks. Let's write a sample mark of our own, then chain
9it together with some preexisting ones to have fun with type
10conversions.
12Let's make a small "cord" mark. "Cord" is a name we use for `@t`
13text, but there's no predefined mark for it. This is
14`/mar/cord.hoon`:
16```
17/? 314
18|_ cod/@t
19++ grab
20 |%
21 ++ atom |=(arg/@ `@t`(scot %ud arg))
22 --
23++ grow
24 |%
25 ++ md cod
26 --
27--
28```
30Let's go through this line by line. `/? 314` is the required
31version number, just like in apps. After that everything's in a
32`|_`, which is a `|%` core, but with input arguments. In our
33case, the argument is the marked data.
35There are three possible top-level arms in the `|_` core,
36`++grab`, `++grow`, and `++grad`. `++grad` is used for revision
37control, and is covered elsewhere. `++grab` specifies
38functions to convert from another mark to the current mark.
39`++grow` specifies how to convert from the current mark to
40another one.
42In our case, we only define one arm in `++grab`, namely `++atom`.
43This allows us to convert any atom to a cord. `++scot` is a
44standard library function in `hoon.hoon` which pretty-prints
45atoms of various types. We want to treat the atom as an
46unsigned decimal number, so we give `%ud` and the given argument
47to `++scot`. Thus, the form of an arm in `++grab` is
48`|=(other-mark-type this-mark-value)`.
50In `++grow`, we just convert to the `md` mark, which is just a
51`@t` internally, but it has fancy conversion functions to things
52like html. `++md` isn't a function because the "argument" is
53already a `@t`, so we can just produce the new value directly.
55Let's play around a bit with this mark. First, let's take a
56marked atom and convert it to our new mark.
58```
59~fintud-macrep:dojo> &atom 9
61~fintud-macrep:dojo> &cord &atom 9
62'9'
63~fintud-macrep:dojo> &cord &atom &cord &atom 9
64'57'
65```
67ASCII 9 is 57. There's no requirement, implicit or otherwise,
68that conversions back and forth between marks be inverses of each
69other. These are semantic conversions, in that they refer to the
70same concept. They're not isomorphisms.
72Let's play around a little more:
74```
75~fintud-macrep:dojo> &md &cord &atom 17
76'17'
77~fintud-macrep:dojo> &hymn &md &cord &atom 17
78[ [%html ~]
79 [[%head ~] [[%title ~] [[%~. [%~. "Untitled"] ~] ~] ~] ~]
80 [ [%body ~]
81 [ i=[g=[n=%meta a=~[[n=%value v="{}"] [n=%name v="frontmatter"] [n=[%urb %front] v=""]]] c=~]
82 t=[i=[g=[n=%p a=~] c=~[[g=[n=%$ a=~[[n=%$ v="17"]]] c=~]]] t=~]
83 ]
84 ]
85 ~
87```
89That was exciting. If you squint at that, it looks an
90awful lot like html. Our value `17` is still in there, in the
91body. `urb` is the mark we render most web pages to. It makes
92sure you have a complete skeleton of a web page. Of course, by
93the time this gets to the web page, it's plain html. Let's do
94the final step in the conversion.
96```
97~fintud-macrep:dojo> &mime &hymn &md &cord &atom 17
98[ [%text %html ~]
99 p=121
100 q='<html><head><title>Untitled</title></head><body><meta value="{}" name="frontmatter" urb:front="" /><p>17</p></body></html>'
102```
104This is a mime-typed octet stream with type `/text/html`, length
105121 bytes, and our lowly number `17` rendered to a web page.
106`cord` was just one step in the chain.
108Of course, arvo can infer some parts of this chain:
110```
111~fintud-macrep:dojo> &cord 17
112'17'
113```
115Likewise, `md` is the only way to get from `cord` to `hymn`, so
116that can be omitted. If we omit the `hymn`, though, we get:
118```
119~fintud-macrep:dojo> &mime &cord 17
120[[%text %x-markdown ~] p=1 q='7']
121```
123Here, we converted straight from `cord` to `md` to `mime`. Arvo
124decided that was the most straightforward conversion, but it
125gives a different result than passing it through the `hymn`
126mark.
128The minimal correct input, then, is `&mime &hymn &cord &atom 17`.