on 69cd7eb help sign in
176 lines 6.9 kB history raw
1+++
2title = "Basic Types"
3weight = 4
4template = "doc.html"
5+++
6A type is usually understood to be a set of values. Hoon values are all nouns,
7so a Hoon type is a set of nouns.
9Hoon's type system conducts various type-checks at compile time in order to
10ensure type safety. For example, one's program might have an integer squaring
11function, such that given some atom `n` the return value is `n^2`. The output
12value should be an unsigned integer (i.e., an atom). Hoon uses type inference
13on the expression that defines the squaring function in to determine what
14possible values it could produce. If the inferred type 'nests' under the
15desired type then the program compiles; otherwise the compile fails with a
16`nest-fail` crash.
18(For an introduction on how to use Hoon's type system, see Chapter 2 of the Hoon
19tutorial.)
21In this document we discuss the recursive data structure Hoon uses for type
22inference. Because the Hoon compiler is written in Hoon this structure is
23likewise defined in Hoon.
25## `$type`
27Below is a simplified version the `+$ type` arm, which defines the data
28structure Hoon uses to keep track of types. But this data structure is used for
29more than simply type inference. It also handles the resolution of names,
30including both faces and arm names. (See Chapter 1 of the Hoon tutorial for an
31introduction to name resolution.)
33As noted, this is a simplified version of `type`. We undo and
34explain the simplifications in the [advanced types](@/docs/reference/hoon-expressions/advanced.md)
35section.
37```
38+$ term @tas
39+$ type $~ %noun
40 $@ $? %noun
41 %void
42 ==
43 $% [%atom p=term q=(unit @)]
44 [%cell p=type q=type]
45 [%core p=type q=(map term hoon)]
46 [%face p=term q=type]
47 [%fork p=(set type)]
48 [%hold p=type q=hoon]
49 ==
50```
52Effectively this structure defines a set of nouns that Hoon uses to keep track
53track of (1) type information, and (2) name resolution information. This set of
54nouns is recursively defined.
56### `?(%noun %void)`
58`%noun` is the label for a noun about which nothing else is known. Every piece
59of Hoon data is a noun, so everything nests under `%noun`.
61`%void` is the label for the empty set.
63### `[%cell p=type q=type]`
65`[%cell p=type q=type]` is for cells. The `type` of the head is labelled `p`
66and the tail `type` is `q`.
68### `[%fork p=(set type)]`
70`[%fork p=(set type)]` is for the union of all types in the set `p`. That is,
71when the type of an expression is known to be a `%fork`, the expression must
72evaluate as one of the types in `p`.
74Hoon uses branching (usually with conditional runes of the `?` family) for type
75inference in order to rule out which of the types in `p` can apply.
77### `[%hold p=type q=hoon]`
79A `%hold` type, with type `p` and hoon `q`, is a lazy reference to the type of
80`(mint p q)`. In English, it means: "the type of the product when we compile
81Hoon expression `q` against a subject with type `p`."
83### `[%face p=term q=type]`
85A `[%face p=term q=type]` wraps the label `p` around the type `q`. `p` is a
86`term` or `@tas`, an atomic ASCII string which obeys symbol rules: lowercase and
87digit only, infix hyphen, first character must be a lowercase letter.
89### `[%atom p=term q=(unit atom))]`
91`%atom` is for an atom, with two twists. `q` is a `unit`, Hoon's
92equivalent of a nullable pointer or a Haskell `Maybe`. If `q`
93is `~`, null, the type is **warm**; any atom is in the type.
94If `q` is `[~ x]`, where `x` is any atom, the type is **cold**;
95its only legal value is the constant `x`.
97`p` in the atom is a terminal used as an **aura**, or soft atom
98type. Auras are a lightweight, advisory representation of the
99units, semantics, and/or syntax of an atom. An aura is an atomic
100string; two auras are compatible if one is a prefix of the other.
102For instance, `@t` means UTF-8 text (LSB low), `@ta` means ASCII
103text, and `@tas` means an ASCII symbol. `@u` means an unsigned
104integer, `@ud` an unsigned decimal, `@ux` an unsigned
105hexadecimal. You can use a `@ud` atom as a `@u` or vice versa,
106but not as a `@tas`.
108Auras can also end with an optional, capitalized suffix, which
109defines the atom's bitwidth as a log starting from `A`. For
110example, `@udD` is an unsigned decimal byte; `@uxG` is an
111unsigned 64-bit hexadecimal.
113You can make up your own auras and are encouraged to do so, but
114here are some conventions bound to constant syntax:
116```
117@c UTF-32 codepoint
118@d date
119 @da absolute date
120 @dr relative date (ie, timespan)
121@n nil
122@p phonemic base (plot)
123@r IEEE floating-point
124 @rd double precision (64 bits)
125 @rh half precision (16 bits)
126 @rq quad precision (128 bits)
127 @rs single precision (32 bits)
128@s signed integer, sign bit low
129 @sb signed binary
130 @sd signed decimal
131 @sv signed base32
132 @sw signed base64
133 @sx signed hexadecimal
134@t UTF-8 text (cord)
135 @ta ASCII text (knot)
136 @tas ASCII text symbol (term)
137@u unsigned integer
138 @ub unsigned binary
139 @ud unsigned decimal
140 @uv unsigned base32
141 @uw unsigned base64
142 @ux unsigned hexadecimal
143```
145Auras are truly soft; you can turn any aura into any other,
146statically, by casting through the empty aura `@`. Hoon is not
147dependently typed and can't statically enforce data constraints
148(for example, it can't enforce that a `@tas` is really a symbol).
150### `[%core p=type q=(map term hoon)]`
152`%core` is for a code-data cell. The data (or **payload**) is the
153tail; the code (or **battery**) is the head. `p`, a type, is the
154type of the payload. `q` is a mapping of arm names and Hoon expressions. It is the source code for the core battery.
156(For an introduction to cores and arms, see Chapter 1 of the Hoon tutorial.)
158Each expression in the battery source is compiled to a formula, with
159the core itself as the subject. The battery is a tree of these
160formulas, or **arms**. An arm is a computed attribute against its
161core.
163All code-data structures in normal languages (functions, objects,
164modules, etc) become cores in Hoon. A Hoon battery looks a bit
165like a method table, but not every arm is a "method" in the OO
166sense. An arm is a computed attribute. A method is an arm whose
167product is a Hoon function (or **gate**).
169A gate (function, lambda, etc) is a core with one arm, whose name
170is the empty symbol `$`, and a payload whose shape is `[sample
171context]`. The **context** is the subject in which the gate was
172defined; the **sample** is the argument.
174To call this function on an argument `x`, replace the sample (at
175tree address `6` in the core) with `x`, then compute the arm.
176(Of course, we don't mutate the noun, we make a mutant copy.)