@@ -0,0 +1,120 @@
1++++
2+title = "Intro to Gall"
3+weight = 0
4+template = "doc.html"
5++++
6+
7+Arvo is divided up into parts called vanes. Gall is one of those vanes and it is the one responsible for providing an api for building stateful applications in Urbit. Gall is a complex system that takes a lot of getting used to so let's dip our toes in with a very simple app.
8+
9+
10+```
11+|%
12++$ effect (pair bone syscall)
13++$ syscall [%wait path @da]
14+--
15+|_ [bowl:gall ~]
16+++ poke-noun
17+ |= t=@dr
18+ ^+ [*(list effect) +>.$]
19+ :_ +>.$ :_ ~
20+ [ost %wait /(scot %da now) (add now t)]
21+++ wake
22+ |= [=wire error=(unit tang)]
23+ ^+ [*(list effect) +>.$]
24+ ~& "Timer went off!"
25+ [~ +>.$]
26+--
27+```
28+
29+The first thing to notice is that we are creating a `core` and a `door`. This is a typical style of gall programming where your types are defined in the first `core` and your application is defined in the following `door`.
30+
31+```
32+|%
33++$ effect (pair bone syscall)
34++$ syscall [%wait path @da]
35+--
36+```
37+
38+The `core` here defines two types `effect` and `syscall`. An `effect` is a `pair` of `bone` and `syscall`. A `bone` is an opaque reference to a set of events. A `syscall` is a request to Arvo to do something for us. In this case the only valid `syscall` will be `%wait` which we'll discuss in a bit. It's important to note that the names `effect` and `syscall` are not important and you can call them whatever you would like. Commonly you will see them called `move` and `card` respectively but we've changed the names here to try to help provide some clarity over the older convention.
39+
40+The sample of the `door` is
41+
42+```
43+[bowl:gall ~]
44+```
45+
46+You can find the full definition of `bowl` in `sys/zuse.hoon` but for now it's enough to know that this is the default app state and includes various faces for information. You can see a short list of some of them below.
47+
48+`our` The current ship
49+`src` The ship the request originated with
50+`ost` A reference to a chain of events
51+`eny` Entropy
52+`now` The current time of the event
53+
54+The `door` we've made has two arms `poke-noun` and `wake`. Gall is capable of dispatching `pokes`, or requests, to an app based on the mark of the data given along with that poke. These are sent to the arm with the name that matches the mark of the data. Here we use the generic `noun` mark
55+
56+
57+```
58+++ poke-noun
59+ |= t=@dr
60+ ^+ [*(list effect) +>.$]
61+ :_ +>.$ :_ ~
62+ [ost %wait /(scot %da now) (add now t)]
63+```
64+
65+We're going to create a gate that takes a single `@dr` argument. `@dr` is an aura for a 128-bit relative date. Here are a few examples.
66+
67+
68+```
69+~s17 (17 seconds)
70+~m20 (20 minutes)
71+~d42 (42 days)
72+```
73+
74+As a mater of good type hygiene, we explicitly cast the output of this gate with `^+` to ensure we are producing the correct thing for gall to handle. `^+` is the rune for cast by example. Our example is a cell of `list` of `effect`, which we `bunt` with `*`, and `+>.$`, the enclosing core which is our `door`.
75+
76+Next we're going to use the `:_` rune which is just the inverted form a `:-` the cell construction rune. We use it twice so the actual data will end up looking something like
77+
78+```
79+[[[ost %wait /(scot %da now) (add now t)] ~] +>.$]
80+```
81+
82+but we want to follow the principle of heaviest twigs to the bottom. `ost` is the `bone`, opaque reference to a chain of events, that we already have contained in the `bowl` so we're just going to continue to use it here. `%wait` is the name of the `syscall`, in this case a request to the behn vain to start a timer for us.
83+
84+After `%wait` we have the `path`, a `list` of `cords` that serves as the unique identifier for this `syscall`
85+
86+```
87+/(scot %da now)
88+```
89+
90+The `/` here is an irregular syntax to make a `path` and `scot` is going to transform `now`, the current time as given to us in the `bowl` from a `@da` to a `cord`.
91+
92+The final part of this `syscall` is
93+
94+```
95+(add now t)
96+```
97+
98+`now` is again the current time as a `@da` and `t` was our `@dr`. Through the magic of mathematics, we can add these two to get a `@da` that is `t` time in the future from now.
99+
100+That's all for our `poke-noun` arm. What about when the timer goes off? `behn` will create a `move` similar to how we created one only this time it will end up being dispatched back to us in the `++wake` arm.
101+
102+```
103+++ wake
104+ |= [=wire error=(unit tang)]
105+ ^+ [*(list effect) +>.$]
106+ ~& "Timer went off!"
107+ [~ +>.$]
108+```
109+
110+`wake` is a `gate` that has two arguments, a `wire` and a `(unit tang)` with the face `error`. The syntax used here `=wire` is a shortcut for `wire=wire`. A `wire` is a specialized kind of `path`. This will be the `path` we gave in our original `syscall` so that we can use to dispatch on if we wanted to do different things based on which event this is. In this case, we don't.
111+
112+Next we have the same cast we used in `++poke-noun` to make sure we are producing the correct thing for gall. These casts are not strictly speaking necessary as the type system can infer what the type will be but they can be very useful both for debugging our own code and for someone coming behind us trying to determine what our code should be producing.
113+
114+```
115+~& "Timer went off!"
116+```
117+
118+`~&` is the debugging printf rune. Here we're simply going to output a message. We could however do any other computation here we wanted to happen when `++wait` gets called.
119+
120+Finally we need to produce our `effect`, which here is simply `[~ +>.$]` since we have no `syscall` or `cards` we want to make and we are not changing the state of our app, we just use the `door` without changing it's sample.