1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

add docs, use alias in example

This commit is contained in:
schaeff 2021-10-12 12:10:02 +03:00
parent ba3752307c
commit eb492153f4
4 changed files with 36 additions and 5 deletions

View file

@ -128,6 +128,8 @@ struct Point {
}
```
Note that two struct definitions with the same members still introduce two entirely different types. For example, they cannot be compared with each other.
#### Declaration and Initialization
Initialization of a variable of a struct type always needs to happen in the same statement as a declaration, unless the struct-typed variable is declared within a function's signature.
@ -144,3 +146,13 @@ The variables within a struct instance, the so called members, can be accessed t
```zokrates
{{#include ../../../zokrates_cli/examples/book/struct_assign.zok}}
```
### Type aliases
Type aliases can be defined for any existing type. This can be useful for readability, or to specialize generic types.
Note that type aliases are just syntactic sugar: in the type system, a type and its alias are exactly equivalent. For example, they can be compared.
```zokrates
{{#include ../../../zokrates_cli/examples/book/type_aliases.zok}}
```

View file

@ -0,0 +1,11 @@
type MyField = field
type Rectangle<L, W> = bool[L][W]
type Square<S> = Rectangle<S, S>
def main():
MyField f = 42
Rectangle<2, 2> r = [[true; 2]; 2]
Square<2> s = r
return

View file

@ -8,6 +8,9 @@
// --------------------------
// | c21 | c22 || d21 | d22 |
type Grid<N> = field[N][N]
const field[4] PRIMES = [2, 3, 5, 7]
// We encode values in the following way:
// 1 -> 2
// 2 -> 3
@ -18,16 +21,22 @@
// assumption: `a, b, c, d` are all in `{ 2, 3, 5, 7 }`
def checkNoDuplicates(field a, field b, field c, field d) -> bool:
// as `{ 2, 3, 5, 7 }` are primes, the set `{ a, b, c, d }` is equal to the set `{ 2, 3, 5, 7}` if and only if the products match
return a * b * c * d == 2 * 3 * 5 * 7
return a * b * c * d == PRIMES[0] * PRIMES[1] * PRIMES[2] * PRIMES[3]
// returns `0` if and only if `x` in `{ 2, 3, 5, 7 }`
// returns true if and only if `x` is one of the `4` primes
def validateInput(field x) -> bool:
return (x-2) * (x-3) * (x-5) * (x-7) == 0
field res = 1
for u32 i in 0..4 do
res = res * (x - PRIMES[i])
endfor
return res == 0
// variables naming: box'row''column'
def main(field a21, field b11, field b22, field c11, field c22, field d21, private field a11, private field a12, private field a22, private field b12, private field b21, private field c12, private field c21, private field d11, private field d12, private field d22) -> bool:
field[4][4] a = [[a11, a12, b11, b12], [a21, a22, b21, b22], [c11, c12, d11, d12], [c21, c22, d21, d22]]
Grid<4> a = [[a11, a12, b11, b12], [a21, a22, b21, b22], [c11, c12, d11, d12], [c21, c22, d21, d22]]
bool res = true

View file

@ -4,7 +4,6 @@
// Where for an array, `field[N]` ends up being propagated to `field[42]` which is direct to turn into a concrete type,
// for structs, `Foo<N> { field[N] a }` is propagated to `Foo<42> { field[N] a }`. The missing step is replacing `N` by `42`
// *inside* the canonical type, so that it can be concretized in the same way arrays are.
// We apply this transformation only to the main function.
use crate::typed_absy::folder::*;
use crate::typed_absy::{