You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/aleo/03_language.md
+38-26Lines changed: 38 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,26 +6,26 @@ sidebar_label: Language
6
6
7
7
### Statically Typed
8
8
9
-
Aleo instructions is a **statically typed language**, which means we must know the type of each variable before executing a circuit.
9
+
Aleo instructions is a **statically typed language**, which means we must know the type of each register for the code to be valid.
10
10
11
11
### Explicit Types Required
12
12
13
-
There is no `undefined` or `null` value in Aleo instructions. When assigning a new variable, **the type of the value must be explicitly stated**.
13
+
There is no `undefined` or `null` value in Aleo instructions. Every register must have an e**xplicitly stated** or **statically inferred**.
14
14
15
15
### Pass by Value
16
16
17
-
Expressions in Aleo instructions are always **passed by value**, which means their values are always copied when they are used as function inputs or in right sides of assignments.
17
+
Arguments in Aleo instructions are always **passed by value**, which means their values are always copied when they are used as function inputs or in right sides of assignments.
18
18
19
19
### Register based
20
20
21
21
There are no variable names in Aleo instructions.
22
-
All variables are stored in registers denoted `rX` where `X` is a non-negative whole number starting from 0 `r0, r1, r2, etc.`.
22
+
All values are stored in registers denoted `rX` where `X` is a non-negative whole number starting from 0 `r0, r1, r2, etc.`.
23
23
24
24
## Data Types and Values
25
25
26
26
### Booleans
27
27
28
-
Aleo instructions supports the traditional `true` or `false` boolean values. The explicit `boolean` type for booleans in statements is required.
28
+
Aleo instructions supports the traditional `true` or `false` boolean values. The explicit `boolean` type for booleans in instructions is required.
29
29
30
30
```aleo
31
31
function main:
@@ -59,9 +59,9 @@ function main:
59
59
60
60
### Group Elements
61
61
62
-
The set of affine points on the elliptic curve passed into the Aleo instructions compiler forms a group.
62
+
The set of affine points on the elliptic curve that Aleo instructions are based on forms a group.
63
63
The curve is a Twisted Edwards curve with `a = -1` and `d = 3021`.
64
-
Aleo instructions supports a subgroup of the group, generated by a generator point, as a primitive data type.
64
+
Aleo instructions support a subgroup of the group, generated by a generator point, as a primitive data type.
65
65
A group element is denoted by the x-coordinate of its point; for example,
66
66
`2group` means the point `(2, 5553594316923449299484601589326170487897520766531075014687114064346375156608)`.
67
67
The generator point is `1540945439182663264862696551825005342995406165131907382295858612069623286213group`.
An Aleo program contains declarations of a [Program ID](#programid), [Imports](#import), [Functions](#function), [Closures](#closure), [Structs](#struct), [Records](#record),
106
-
[Mappings](#mapping), and [Finalize](#finalize). Ordering is only enforced for imports which must be at the top of file.
106
+
[Mappings](#mapping), and [Finalizers](#finalize). Ordering is only enforced for imports which must be at the top of the file.
107
107
Declarations are locally accessible within a program file.
108
108
If you need a declaration from another program file, you must import it.
109
109
110
110
### Program ID
111
111
112
112
A program ID is declared as `{name}.{network}`.
113
-
The first character of a `name` must be lowercase.
114
-
`name` can contain lowercase letters, numbers, and underscores.
113
+
`name` must start with a lowercase letter and only contain lowercase letters, digits, and underscores.
115
114
Currently, `aleo` is the only supported `network` domain.
116
115
117
116
```aleo showLineNumbers
@@ -153,7 +152,7 @@ function foo:
153
152
#### Function Inputs
154
153
155
154
A function input is declared as `input {register} as {type}.{visibility};`.
156
-
Function inputs must be declared just after the function name declaration.
155
+
Function inputs must be declared just after the function name declaration and before instructions.
157
156
158
157
```aleo showLineNumbers
159
158
// The function `foo` takes a single input `r0` with type `field` and visibility `public`.
@@ -175,8 +174,8 @@ Function outputs must be declared at the end of the function definition.
175
174
176
175
In the Aleo protocol, calling a function creates a transition that can consume and produce records on-chain.
177
176
Use the `aleo run` CLI command to pass inputs to a function and execute the program.
178
-
In Testnet3, program functions cannot call other internal program functions.
179
-
If you would like to develop "helper functions" that are called internally within a program, try writing a `closure`.
177
+
In Testnet, program functions cannot call other internal program functions.
178
+
If you would like to develop "helper functions" that are called internally within a program, write a `closure`.
180
179
181
180
#### Call an Imported Function
182
181
@@ -189,15 +188,22 @@ program bar.aleo;
189
188
190
189
function call_external:
191
190
input r0 as u64.private;
192
-
call foo.aleo/baz r0 into r1; // Externally call function `baz` in foo.aleo with argument `r0` and store the result in `r1`.
193
-
output r1;
191
+
input r1 as u64.private;
192
+
193
+
// Externally call function `baz` in foo.aleo with arguments `r0` and `r1`,
194
+
// and store the results in `r2` and `r3`.
195
+
call foo.aleo/baz r0 r1 into r2 r3;
196
+
197
+
output r2;
198
+
output r3;
194
199
```
195
200
196
201
### Closure
197
202
198
203
A closure is declared as `closure {name}:`.
199
204
Closures contain instructions that can compute values.
200
-
Closures are helper functions that cannot be executed directly. Closures may be called by other functions.
205
+
Closures are helper functions that cannot be executed directly from the CLI to create transitions.
206
+
Closures may be called by other functions.
201
207
202
208
```aleo showLineNumbers
203
209
closure foo:
@@ -216,8 +222,14 @@ program bar.aleo;
216
222
217
223
function call_internal:
218
224
input r0 as u64.private;
219
-
call foo r0 into r1; // Internally call closure `foo` with argument `r0` and store the result in `r1`.
220
-
output r1;
225
+
input r1 as u64.private;
226
+
227
+
// Internally call closure `foo` with arguments `r0` and `r1`,
228
+
// and store the results in `r2` and `r3`.
229
+
call foo r0 r1 into r2 r3;
230
+
231
+
output r2;
232
+
output r3;
221
233
```
222
234
223
235
### Struct
@@ -245,7 +257,7 @@ function new_array3:
245
257
246
258
### Array
247
259
248
-
An array is a data type declared as `[{value}, {value}]`.
260
+
An array is an aggregate value denoated as `[{value}, ..., {value}]`.
249
261
250
262
```aleo
251
263
[true, false, true]
@@ -340,7 +352,7 @@ mapping account:
340
352
341
353
#### Contains
342
354
343
-
A contains command that checks if a key exists in a mapping, e.g.`contains accounts[r0] into r1;`.
355
+
A contains command checks if a key exists in a mapping and stores the boolean result in the into register. For example:`contains accounts[r0] into r1;`.
344
356
345
357
#### Get
346
358
@@ -460,7 +472,7 @@ For example, `await r0;`.
460
472
An `await` command can only be used in a finalize block.
461
473
The operand must be a register containing a Future.
462
474
463
-
#### Indexing a future.
475
+
#### Indexing a future
464
476
A register containing a future can be indexed using the existing index syntax.
465
477
For example, `r0[0u32]`.
466
478
This would get the input of the future at that specific index.
@@ -530,8 +542,8 @@ finalize add_and_subtract:
530
542
There are a number of rules associated with using these components.
531
543
532
544
1. If a function has a finalize block, it must have exactly one async instruction.
533
-
2. If a function has a finalize block, it's last output must be a future.
534
-
3. If a function does not have a finalize block, it cannot have an async instruction`.
545
+
2. If a function has a finalize block, its last output must be a future.
546
+
3. If a function does not have a finalize block, it cannot have an async instruction.
535
547
4. All futures created by calls need to be input to the async instruction in the order they were produced.
536
548
5. An async call must reference the same function.
537
549
6. All calls must be made before invoking async.
@@ -620,8 +632,8 @@ Checkout the [Aleo Instructions opcodes](./04_opcodes.md) for a full list of sup
620
632
#### position, branch.eq, branch.neq
621
633
622
634
The `position` command, e.g. `position exit`, indicates a point to branch execution to.
623
-
The `branch.eq` command, e.g. `branch.eq r0 r1 to exit`, which branches execution to the position indicated by `exit` if `r0` and `r1` are equal.
624
-
The `branch.neq` command, e.g. `branch.neq r0 r1 to exit`, which branches execution to the position indicated by `exit` if `r0` and `r1` are not equal.
635
+
The `branch.eq` command, e.g. `branch.eq r0 r1 to exit`, branches execution to the position indicated by `exit` if `r0` and `r1` are equal.
636
+
The `branch.neq` command, e.g. `branch.neq r0 r1 to exit`, branches execution to the position indicated by `exit` if `r0` and `r1` are not equal.
625
637
626
638
** Example **
627
639
The finalize block exits successfully if the input is 0u8 and fails otherwise.
@@ -713,7 +725,7 @@ $ snarkvm execute foo
713
725
714
726
#### User Callable Program
715
727
716
-
By `asserting assert.eq self.caller self.signer;` on line 4, a developer can restrict their function such that it can only be called by users.
728
+
By asserting `assert.eq self.caller self.signer;` on line 4, a developer can restrict their function such that it can only be called by users.
0 commit comments