on master help sign in
no bytes
commit167fda13ba66d02df8b76963dacfcab32c5b81b3 authorGalen Wolfe-Pauly <root@office-for.com> dateWed 20 Jul 2016, 15:49 (10 years ago) parent3dc9432
3 files changed−639
main/blog/byte-0.md−267 main/blog/byte-1.md−302 main/blog/byte-2.md−70
main/blog/byte-0.mddeleted−267
@@ -1,267 +0,0 @@
1−---
2−date: ~2016.7.16
3−type: post
4−title: Urbyte 0: setup, nouns
5−author: The Urbit Team
6−layout: urbit,post
7−comments: true
8−hide: true
9−navmode: navbar
10−navdpad: false
11−navselect: blog
12−navpath: /
13−navhome: /
14−navclass: urbit
15−---
16−<br /><br />
17−This is the first post in our new Urbyte series.
18−
19−The Urbyte concept is that you, an ordinary programmer, not a
20−kernel hacker or a Haskell magician (not that there's anything
21−wrong with kernel hackers or Haskell magicians) can learn Hoon,
22−Urbit's programming language.
23−
24−We're going to test this daring idea by teaching you Hoon, in
25−easily digestible, byte-size chunks. Each post in the "Urbytes"
26−series is designed to take about 15 minutes to consume. (Maybe
27−10, if you're a kernel hacker or a Haskell magician.)
28−
29−We'll assume no knowledge of Urbit except for previous lessons.
30−Ideally you have some basic familiarity with programming and can
31−operate the Unix command line.
32−
33−We strongly encourage you to type in all the examples in each
34−Urbyte. Urbytes are short, but not meant to be skimmed.
35−
36−## Install
37−
38−First, [install Urbit](http://urbit.org/docs/using/install/) on any
39−Mac or Unix machine. On Windows, make a virtual Linux machine
40−using VirtualBox or a similar tool.
41−
42−While you're downloading, read about Urbit ships:
43−
44−## Ships
45−
46−In the Urbyte exercises you'll be running what we call a "fake
47−`~zod`." A string starting with `~`, like `~zod`, is what Urbit
48−calls a *ship*.
49−
50−A ship is one number which means four things: a network address,
51−a cryptographic identity, a memorable name, and a single virtual
52−computer. We write this number in a base-256 format, where each
53−digit is a syllable.
54−
55−`~zod` is ship zero, making it a very important ship indeed.
56−Your "fakezod" thinks it's `~zod`, but no one can talk to it.
57−
58−This is not the true Urbit experience, of course. But it's
59−ideal if all you want to do is play around with Hoon. If you
60−have a real urbit ship, of course, you can use that instead.
61−
62−## Create and restart
63−
64−To create a fakezod, run:
65−
66−```
67−$ urbit -FI ~zod -c myzod
68−```
69−
70−This will take about 30 seconds and spin out a bunch of boot
71−messages. At the end, you'll see:
72−
73−```
74−ames: on localhost, UDP 31337.
75−http: live (insecure, public) on 8080
76−http: live ("secure", public) on 8443
77−http: live (insecure, loopback) on 12321
78−~zod:dojo>
79−```
80−
81−To make sure everything is working, type `(add 2 2)` at the
82−prompt, then hit return. Your screen now shows:
83−
84−```
85−ames: on localhost, UDP 31337.
86−http: live (insecure, public) on 8080
87−http: live ("secure", public) on 8443
88−http: live (insecure, loopback) on 12321
89−> (add 2 2)
90−4
91−~zod:dojo>
92−```
93−
94−That's super awesome! You made your first noun! We'll make a
95−few more in a second. First, quit Urbit with `^D`:
96−
97−```
98−> (add 2 2)
99−4
100−~zod:dojo>
101−$
102−```
103−
104−You now have an Urbit image, or *pier*, in the `myzod` directory.
105−Right now the only thing in your pier is Urbit's system files:
106−
107−```
108−$ ls -a myzod
109−./ ../ .urb/
110−```
111−
112−Restarting uses almost the same command, but without `-c`. There
113−are also fewer startup messages.
114−
115−```
116−$ urbit -FI ~zod myzod
117−[...]
118−ames: on localhost, UDP 31337.
119−http: live (insecure, public) on 8080
120−http: live ("secure", public) on 8443
121−http: live (insecure, loopback) on 12321
122−~zod:dojo>
123−```
124−
125−Note also that Urbit is an ACID database. You can stop it
126−politely with `^D`, abruptly with `^Z`, even violently with `kill
127−-9`. None of these should cause Urbit to lose data. If you do
128−manage to corrupt a pier, it's a bug; please let us know.
129−
130−## Let's make a noun
131−
132−We already made a noun. Let's make another. (We won't show the
133−`~zod:dojo> ` prompt in future, just the echoed command.)
134−
135−```
136−~zod:dojo> 42
137−```
138−
139−You'll see:
140−
141−```
142−> 42
143−42
144−~zod:dojo>
145−```
146−
147−## Understanding nouns
148−
149−A value in Urbit is called a *noun*. A noun is either an *atom*
150−(an unsigned integer of any size) or a *cell* (an ordered pair of
151−any two nouns).
152−
153−In a sentence, a noun is a binary tree whose leaves are numbers.
154−If you know Lisp, a noun is a minimalist S-expression. A noun
155−can't have cycles (pointer loops). An implementation can use
156−dags (shared subtrees), but the programmer can't detect them
157−(there's no pointer equality test).
158−
159−The absence of graph cycles makes nouns much easier to save in
160−persistent storage, send over the network, manage memory for
161−(Urbit has no tracing garbage collector), etc.
162−
163−## Let's make some atoms
164−
165−So one kind of atom is a number. And the syntax (or at least one
166−syntax) for a number is a decimal, like `42`. Seems legit.
167−
168−But just to be sure we understand this decimal thing, let's make
169−some atoms:
170−
171−```
172−> 4
173−4
174−```
175−
176−Seems pretty straightforward.
177−
178−```
179−> 42
180−42
181−```
182−
183−Why not try it again to make sure?
184−
185−```
186−> 420
187−420
188−```
189−
190−Can we add another zero?
191−
192−```
193−~zod:dojo> 420
194−```
195−
196−Wat? When you tried to type that next zero and turn `420` into
197−`4200`, Urbit actually *deleted the `0` and beeped at you.*
198−
199−But isn't an atom an unsigned integer of any size? It is.
200−But the syntax is actually `4.200`:
201−
202−```
203−> 4.200
204−4.200
205−> (mul 42 100)
206−4.200
207−```
208−
209−Just think of it as "4,200", written the German way with a dot.
210−
211−Why? Long decimals are unreadable. Human beings know this, so
212−we group them in threes. For some historical reason, programming
213−languages never copied this marvelous usability innovation.
214−
215−English notation for decimals is more common than German
216−notation. Unfortunately, dot is URL-safe and comma isn't, and
217−it's nice to have a regular syntax for atoms that's URL-safe.
218−
219−As for why Urbit deleted your zero: it parses your command line
220−as you type, and rejects any characters after the parser stops.
221−
222−## Let's make some cells
223−
224−There's not much mystery about cells. A cell has a head and a
225−tail, and puts brackets around them:
226−
227−```
228−> [42 420]
229−[42 420]
230−```
231−
232−`42` is the *head* and `420` is the *tail* of the cell. Cell
233−syntax nests to the right:
234−
235−```
236−> [4 [42 420]]
237−[4 42 420]
238−
239−> [4 42 420]
240−[4 42 420]
241−
242−> [[4 42] 420]
243−[[4 42] 420]
244−```
245−
246−If you look at these nouns as binary trees, you see
247−
248−```
249− [4 [42 420]] [[4 42] 420]
250− . .
251− / \ / \
252− 4 . . 420
253− / \ / \
254− 42 420 4 42
255−```
256−
257−Each number is an atom; each dot is a cell.
258−
259−## Questions and/or exercises
260−
261−And we're done with our very first Urbyte! Here's a question to
262−think about. These exercises are never required, but they may
263−be fun and/or interesting.
264−
265−The question today is: what is the right way to represent common
266−data structures as nouns? A linked list? A string? A *signed*
267−integer? A table or associative array?
main/blog/byte-1.mddeleted−302
@@ -1,302 +0,0 @@
1−---
2−date: ~2016.7.14
3−type: post
4−title: Urbyte 1: atoms, auras, types
5−author: The Urbit Team
6−layout: urbit,post
7−comments: true
8−hide: true
9−navmode: navbar
10−navdpad: false
11−navselect: blog
12−navpath: /
13−navhome: /
14−navclass: urbit
15−---
16−<br /><br />
17−Thanks for reading Urbyte 0! In Urbyte 1, we'll look a little
18−more deeply at atoms and other simple noun types.
19−
20−## The atom question
21−
22−It seems limiting to have only one kind of atom, the unsigned
23−decimal. How are signed integers represented? Symbols?
24−Floating-point values? Dates? IP addresses? Even just hex?
25−
26−The Lisp S-expression, with cons cells and atoms, is very similar
27−to Hoon's nouns. But languages in the Lisp family mark each atom
28−with a dynamic tag that defines the atom's meaning. These tags
29−are effectively dynamic types.
30−
31−Hoon also tracks the meaning of atoms. But Hoon is statically
32−type, so an atom description is obviously a type. An atomic type
33−in Hoon is called an *aura*. The aura syntax is just an ASCII
34−string starting with `@`, like `@ud` for unsigned decimal.
35−
36−## Examples
37−
38−With the dojo command `? expression`, you tell the dojo to print
39−both the product of the expression, and the product's type.
40−
41−Let's type in some aura examples:
42−
43−### Unsigned integers
44−
45−```
46−> 42
47−42
48−
49−> ? 42
50− @ud
51−42
52−
53−> ? 0x42
54− @ux
55−0x42
56−
57−> ? `@ud`0x42
58− @ud
59−66
60−
61−> ? (add 0x42 42)
62− @
63−108
64−```
65−
66−So decimal is aura `@ud`, hexadecimal is `@ux`. Note the `@ud`
67−cast syntax, which lets us change the type of an atom, and
68−converting hex `0x42` to a decimal.
69−
70−An aura is soft type. The programmer can always insist on how
71−to interpret an atom. Hoon doesn't have "dependent types"; and
72−can't enforce any constraints the aura puts on the atom's value.
73−
74−### Signed integers
75−
76−```
77−> -7
78−-7
79−
80−> ? -7
81− @sd
82−
83−> --7
84−--7
85−
86−> ? --7
87− @sd
88−--7
89−
90−> ? (sum:si -7 --7)
91− @s
92−--0
93−
94−> `@ud`-7
95−13
96−
97−> `@ud`--7
98−14
99−```
100−
101−`--7` means "positive 7." `+7` might have been better, but `+`
102−is not URL-safe.
103−
104−With unlimited atom width, traditional sign extension makes no
105−sense. The sign bit is the low bit. Even atoms are positive,
106−odd atoms are negative.
107−
108−Hoon needs `--` to distinguish positive signed numbers because
109−Hoon's type system doesn't feed back into its parser. The
110−expression itself needs to control whether it's `@sd` or `@ud`.
111−
112−And Hoon has no overloading or type detection; we need to use a
113−different function to add signed integers. We'll always choose
114−slightly clunkier syntax in exchange for much simpler semantics.
115−
116−### Symbols
117−
118−```
119−> ?
120− @t
121−'Ürbit'
122−
123−> ? `@ux`'Ürbit'
124− @ux
125−0x7469.6272.9cc3
126−
127−> ? `@ux`'foo'
128− @ux
129−0x6f.6f66
130−```
131−
132−The `@t` aura is UTF-8 text, with the first byte low. Again,
133−Hoon doesn't actually have the power to check statically that
134−your UTF-8 is valid. Auras don't tell you what an atom is, just
135−what you want it to be.
136−
137−### Dates, floats, IP addresses...
138−
139−```
140−> ? now
141− @da
142−~2016.7.11..01.32.04..2c0d
143−
144−> ? @~h6
145− @dr
146−~h6
147−
148−> `@ux`~h6
149− @ux
150−0x5460.0000.0000.0000.0000
151−
152−> ? (add ~h6 now)
153− @
154−170.141.184.502.236.089.414.812.327.632.537.911.296
155−
156−> `@da`(add ~h6 now)
157−~2016.7.11..07.32.28..842f
158−
159−> ? .3.14
160− @rs
161−.3.14
162−
163−> ? .127.0.0.1
164− @if
165−.127.0.0.1
166−```
167−
168−`@da` is a 128-bit absolute date (64 bits for fractions of a
169−second, 64 bits for seconds); `@dr` is a relative date; `@rs` is
170−a single-precision IEEE real; `@if` is an IPv4 address. This is
171−not all Hoon's aura syntax, but it's a good range of examples.
172−
173−`\`@da\~(add ~h6 now)` is a good example of the use and limits of
174−the aura system. Unlike the signed-integer sum, offsetting a
175−date can use plain atomic addition. But the product of `add` is
176−a flavorless atom with no aura, so we have to cast it back to a
177−date aura.
178−
179−## Auras explained
180−
181−Does an aura represent the units of the atom, its syntax, its
182−semantics, its constraints?
183−
184−It can mean any or all of these. Programmers can even use their
185−own aura strings which nothing in Hoon recognizes. The set of
186−syntaxes that Hoon knows how to parse and print is fixed, but
187−there are plenty of reasons to introduce user-defined auras.
188−
189−If your program uses both fortnights and furlongs, the aura
190−system will not parse a fortnight syntax or print furlongs in the
191−dojo. But it will keep you from accidentally confusing the two,
192−which is probably most of what you want.
193−
194−Hoon does interpret the string in one way: auras specialize to
195−the right. For example, `@u` is any unsigned integer; `@ux` is
196−an unsigned integer that likes to be printed as hex.
197−
198−Hoon's typechecker lets you go up or down the specialization
199−ladder, but complains if you try to go across. For example,
200−`@u` can silently convert into `@ux` or `@ux` into `@u`, but
201−turning `@ud` into `@ux`, `@ux` into `@da`, etc, requires an
202−explicit cast.
203−
204−## Constants, symbols, loobeans and null
205−
206−An atom type isn't just an aura. There's another choice: the
207−type can be *general* and contain all atoms (like the types we've
208−seen above), or *constant* and contain just one atom.
209−
210−A type describes a set of values. While auras may informally
211−describe a format for which not all atoms are valid, any atom can
212−be cast into the aura whether valid or invalid.
213−
214−In a constant, the type contains just one atom. Hoon actually
215−enforces this restriction.
216−
217−The regular syntax for an atomic constant is just to put `%` on
218−front of the constant. Like:
219−
220−```
221−> ? 42
222− @ud
223−42
224−
225−> ? %42
226− $42
227−42
228−
229−> ? %~h6
230− $~h6
231−~h6
232−```
233−
234−The `$` on the type indicates the constant. Note that constants
235−still have auras; `~h6` (6 hours) is still a `@dr`.
236−
237−## Common irregulars
238−
239−There are a few special forms worth learning early: symbols,
240−null, and loobeans:
241−
242−```
243−> ? %foo
244− $foo
245−%foo
246−
247−> `@ux`%foo
248−0x6f.6f66
249−
250−> .n
251−.n
252−
253−> |
254−.n
255−
256−> .y
257−.y
258−
259−> &
260−.y
261−
262−> `@ud`&
263−0
264−
265−> ~
266−~
267−
268−> `@ud`~
269−0
270−```
271−
272−`%symbol` is a constant with aura `@tas` (text, ASCII, symbol).
273−The symbol rules, which are enforced by the Hoon parser but not
274−(as we've discussed) the type system: a symbol is lowercase with
275−infix hyphens ("kebab-case").
276−
277−Loobeans are not true and false, but yes (`.y` or `&`) and no
278−(`.n` or `|`). This is because *yes is zero and no is one*.
279−This makes sense at a certain mathematical level, though it
280−was probably a mistake.
281−
282−`~` is Hoon's nil: constant `0`, aura `@n`.
283−
284−## Cells and types
285−
286−Naturally, all these atoms fit neatly into cells, and retain
287−their types within the cells.
288−
289−```
290−> ? [~h6 [.3.14 %foo] 0xdead.beef]
291− {@dr {@rs $foo} @ux}
292−[~h6 [.3.14 %foo] 0xdead.beef]
293−
294−> `*`[~h6 [.3.14 %foo] 0xdead.beef]
295−[398.449.671.992.126.314.905.600 [1.078.523.331 7.303.014] 3.735.928.559]
296−```
297−
298−Another fun trick is to cast any noun to the `*` type, ie,
299−generic noun. We then know nothing about the noun, so we print
300−all its atoms as decimals.
301−
302−That's all for today! Please wait for the next fun Urbyte.
main/blog/byte-2.mddeleted−70
@@ -1,70 +0,0 @@
1−---
2−date: ~2016.7.14
3−type: post
4−title: Urbyte 2: subject-oriented programming
5−author: Curtis Yarvin
6−layout: urbit,post
7−comments: true
8−hide: true
9−navmode: navbar
10−navdpad: false
11−navselect: blog
12−navpath: /
13−navhome: /
14−navclass: urbit
15−---
16−<br /><br />
17−
18−You're playing in the Urbit REPL/shell (`:dojo`). (If you
19−accidentally press `^X`, it will put you into the other default
20−app, `:talk`; press `^X` again to switch back.)
21−
22−The dojo actually has its own command language, which is a
23−superset of Hoon. Any input string which parses as a Hoon
24−expression is a dojo command that prints the product of the
25−expression. In the space of strings which don't parse as Hoon
26−expressions, the dojo has other commands.
27−
28−If this is just too abstract, think of the dojo as half Lisp
29−REPL, half Unix shell.
30−
31−### Setting dojo variables
32−
33−Like the Unix shell, the dojo has command variables. Type
34−
35−```
36−> =a 42
37−```
38−
39−You've set the dojo variable `a` to the value `42`. The dojo
40−puts this binding in the Hoon subject for expressions to use.
41−So you can write:
42−
43−```
44−> a
45−42
46−
47−> [4 a]
48−[4 42]
49−
50−> ? [4 a]
51− {@ud @ud}
52−[4 42]
53−```
54−
55−To unbind `a`, just omit the value:
56−
57−```
58−> =a
59−```
60−
61−You removed `a` from the subject. (We can bind it again later, or
62−bind over it without removing it.)
63−
64−```
65−> [4 a]
66−-find.a
67−```
68−
69−Now it's
70−