on 63b60ca help sign in
365 lines 8.6 kB history raw
1---
2navhome: /docs/
3sort: 2
4next: true
5title: Atoms, auras, types
6---
8# Urbyte 1: Atoms, auras, types
10Thanks for reading Urbyte 0! In Urbyte 1, we'll look a little
11more deeply at atoms and other simple noun types.
13Let's restart our engines:
15```
16$ urbit mycomet
17[...]
18ames: on localhost, UDP 31337.
19http: live (insecure, public) on 8080
20http: live ("secure", public) on 8443
21http: live (insecure, loopback) on 12321
22~palnul_nocser:dojo>
23```
25## What's in an atom?
27It seems limiting to have only one kind of atom, the unsigned
28decimal. How are signed integers represented? Symbols?
29Floating-point values? Dates? IP addresses? Even just hex?
31Lisp's S-expression, with its own cells and atoms, is very
32similar to Hoon's nouns. But languages in the Lisp family mark
33each atom with a dynamic tag that defines the atom's meaning.
34These tags are effectively dynamic types.
36Hoon also tracks the meaning of atoms. But Hoon is statically
37typed, so an atom description is obviously a type. An atomic type
38in Hoon is called an *aura*. The aura syntax is just an ASCII
39string starting with `@`, like `@ud` for unsigned decimal.
41Hoon has a wide (but not extensible) variety of atom syntaxes.
42Each syntax for an atom produces an appropriate predefined aura.
43And Hoon's printer can invertibly print any aura Hoon can parse.
44Because atoms make great path nodes and paths make great URLs,
45all regular atom syntaxes use only URL-safe characters.
47## Examples
49With the dojo command `? expression`, you tell the dojo to print
50both the product of the expression, and the product's type.
52Let's type in some aura examples:
54### Unsigned integers
56```
57> 42
5842
60> ? 42
61 @ud
6242
64> ? 0x42
65 @ux
660x42
68> ? (add 0x42 42)
69 @
70108
72> ? `@ud`0x42
73 @ud
7466
76```
78So decimal is aura `@ud`, hexadecimal is `@ux`. The cast
79syntax—`` `@ud` ``—lets us change the type of an
80atom, converting hex `0x42` to a decimal `66`.
82An aura is soft type. The programmer can always insist on how
83to interpret an atom. Hoon doesn't have "dependent types"; it
84can't enforce any constraints the aura puts on the atom's value.
86### Signed integers
88```
89> -7
90-7
92> ? -7
93 @sd
95> --7
96--7
98> ? --7
99 @sd
100--7
102> ? (sum:si -7 --7)
103 @s
104--0
106> `@ud`-7
10713
109> `@ud`--7
11014
111```
113`--7` means "positive 7." `+7` might have been better, but `+`
114is not URL-safe.
116Hoon needs `--` to distinguish positive signed numbers because
117Hoon's type system doesn't feed back into its parser. The
118expression itself needs to control whether it's `@sd` or `@ud`.
120With unlimited atom width, traditional sign extension makes no
121sense. The sign bit is the low bit. Even atoms are positive,
122odd atoms are negative.
124And Hoon has no overloading or type detection; we need to use a
125different function to add signed integers. We'll always choose
126slightly clunkier syntax in exchange for much simpler semantics.
128### Symbols
130```
131> ? 'Ürbit'
132 @t
133'Ürbit'
135> ? `@ux`'Ürbit'
136 @ux
1370x7469.6272.9cc3
139> ? ~.urbit
140 @ta
141~.urbit
143> ? `@ux`'foo'
144 @ux
1450x6f.6f66
146```
148The `@t` aura is UTF-8 text, with the first byte low. Again,
149Hoon doesn't actually have the power to check statically that
150your UTF-8 is valid. Auras don't tell you what an atom is, just
151what you think it is.
153The familiar single-quote syntax for `@t` is an irregular form of
154the more unusual `~~` prefix. The `~.` prefix, aura `@ta`, is
155restricted to lowercase ASCII (a great common denominator). Of
156course, `~` is URL-safe and `'` is not.
158### Dates, floats, IP addresses...
160```
161> ? now
162 @da
163~2016.7.11..01.32.04..2c0d
165> ? ~h6
166 @dr
167~h6
169> `@ux`~h6
170 @ux
1710x5460.0000.0000.0000.0000
173> ? (add ~h6 now)
174 @
175170.141.184.502.236.089.414.812.327.632.537.911.296
177> `@da`(add ~h6 now)
178~2016.7.11..07.32.28..842f
180> ? .3.14
181 @rs
182.3.14
184> ? .127.0.0.1
185 @if
186.127.0.0.1
187```
189`@da` is a 128-bit absolute date (64 bits for fractions of a
190second, 64 bits for seconds); `@dr` is a relative date; `@rs` is
191a single-precision IEEE real; `@if` is an IPv4 address. This is
192not all Hoon's aura syntax, just a good range of examples.
194`(add ~h6 now)` is a good example of the use and limits of
195the aura system. Unlike the signed-integer sum, offsetting a
196date can use plain atomic addition. But the product of `add` is
197a flavorless atom with no aura, `@`. So we have to cast it back
198to an absolute date `@da`.
200## Auras explained
202Does an aura represent the units of the atom, its syntax, its
203semantics, its constraints?
205Any or all of the above. Programmers can even use their own aura
206strings, which nothing in Hoon recognizes. The set of syntaxes
207that Hoon knows how to parse and/or print is fixed. But there
208are plenty of good reasons to use your own user-defined auras.
210If your program uses both fortnights and furlongs, Hoon will not
211parse a user-defined fortnight syntax. It will not print furlongs
212in the dojo. But the type system will keep you from accidentally
213passing a furlong to a function that expects a fortnight.
215Hoon does interpret the aura string in one way: auras specialize
216to the right. For example, `@u` is any unsigned integer; `@ux`
217is an unsigned integer that likes to be printed as hex.
219Hoon's typechecker lets you go up or down the specialization
220ladder. It complains if you try to move across. For example,
221`@u` can silently convert into `@ux` or `@ux` into `@u`, but
222turning `@ud` into `@ux`, `@ux` into `@da`, etc, requires an
223explicit cast.
225## Constants, symbols, loobeans and null
227An atom type isn't just an aura. There's another choice: the
228type can be *general* and contain all atoms (like the types we've
229seen above), or *constant* and contain just one atom.
231A type describes a set of values. While auras may informally
232describe a format for which not all atoms are valid, any atom can
233be cast into the aura whether valid or invalid. But in a
234constant, the type contains just one atom.
236The regular syntax for an atomic constant is just to put `%` in
237front of the constant. Like:
239```
240> ? 42
241 @ud
24242
244> ? %42
245 $42
246%42
248> ? %~h6
249 $~h6
250%~h6
251```
253The `$` on the type indicates the constant. Note that constants
254still have auras; `~h6` (6 hours) is still a `@dr`.
256## Common irregulars
258There are a few special forms worth learning early: symbols,
259null, loobeans, and of course ships:
261```
262> ? %foo
263 $foo
264%foo
266> `@ux`%foo
2670x6f.6f66
269> .n
270%.n
272> |
273%.n
275> .y
276%.y
278> &
279%.y
281> `@ud`&
284> ~
287> `@ud`~
290> ? ~zod
291 @p
292~zod
294> `@ux`~forseg-bolbyn
2950x885e.8af9
296```
298`%symbol` is a constant with aura `@tas` (text, ASCII, symbol).
299The symbol rules, which are enforced by the Hoon parser but not
300(as we've discussed) the type system: a symbol is lowercase with
301infix hyphens ("kebab-case").
303Loobeans are not true and false, but yes (`%.y` or `&`) and no
304(`%.n` or `|`). This is because *yes is zero and no is one*.
305This makes sense at a certain mathematical level, though it
306was probably a mistake from a practical perspective.
308`~` is Hoon's nil: constant `0`, aura `@n`.
310And of course, `@p` is the phonemic base used for ship names.
311(It's useful for any kind of memorable number.)
313## Cells and types
315Naturally, all these atoms fit neatly into cells, and retain
316their types within the cells.
318```
319> ? [~h6 [.3.14 %foo] 0xdead.beef]
320 {@dr {@rs $foo} @ux}
321[~h6 [.3.14 %foo] 0xdead.beef]
323> `*`[~h6 [.3.14 %foo] 0xdead.beef]
324[398.449.671.992.126.314.905.600 [1.078.523.331 7.303.014] 3.735.928.559]
325```
327Another fun trick is to cast any noun to the `*` type, ie,
328generic noun. We then know nothing about the noun, so we print
329all its atoms as decimals.
331## And we're done
333And we're done! Type `|exit` to exit:
335```
336~palnul_nocser:dojo> |exit
337%drum-exit
339```
341## Questions and/or exercises
343As always, these questions are optional and only for fun.
345We didn't include anywhere near all Hoon's auras or syntaxes in
346this lesson. It's just a set of examples.
348* Try using the dojo's magic error erasure to stumble around the
349rest of Hoon's atom syntax. Any string that can't be extended
350into a valid expression will be trimmed back, with a beep, until
351it can. Return will beep if the expression is not complete.
352This feedback is harmless and does not use an electric shock.
353It enables stochastic reinforcement learning of Hoon syntax.
355* If `~h6` is six hours, how do you write forty-five minutes? Two
356days, six hours and forty-five minutes?
358* What would you expect the syntax for double-precision float to
359be? What about base64?
361* Does an absolute date have to include every zillisecond, or can
362it be pruned to a day or a year?
364* If there's an IP syntax, what about bitcoin addresses? What
365other auras do you think should be supported?