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: book/src/rfc/rfc-0-generic-parameters.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
This RFC proposes to support const generics in noname. The generic parameters can be resolved from the observed arguments, such as constants, arrays, or structs. This improves reusability and modularity of the code. It is a prerequisite for supporting generic array with symbolic size.
5
5
6
6
## Code Examples
7
-
Here is a few previews of how the generic parameters can be used, and what features it would unlock.
7
+
Here are a few previews of how the generic parameters can be used, and what features it would unlock.
8
8
9
9
Allow functions to create array with symbolic size:
10
10
```rust
@@ -104,7 +104,7 @@ Below is the diagram of the compiler pipeline with the MAST phase added:
To support the generic syntax as shown in the examples above, we need to make changes to the AST Parser support generic syntax. Furthermore, because the generic parameters can't be resolved at TAST phase, the some type checkings will be less strict and deferred to MAST phase.
107
+
To support the generic syntax as shown in the examples above, we need to make changes to the AST Parser support generic syntax. Furthermore, because the generic parameters can't be resolved at TAST phase, some type checkings will be less strict and deferred to MAST phase.
108
108
109
109
Here is a list of cases where the type checks can't be done at TAST phase, as they need to resolve the generic values:
110
110
@@ -172,7 +172,7 @@ fn main(pub xx: Field) {
172
172
```
173
173
174
174
175
-
The newly added phase MAST will be responsible for resolving the generic values from the observed arguments. It includes type checking on the monomorphized types that are bypass in the TAST phase.
175
+
The newly added phase MAST will be responsible for resolving the generic values from the observed arguments. It includes type checking on the monomorphized types that are bypassed in the TAST phase.
176
176
177
177
### Generic Syntax
178
178
@@ -184,7 +184,7 @@ For example, with the turbofish, we could do something like:
This is a rare case where the generic parameter can't be trivially resolved from the observed arguments. To get it work without any advanced inference setups, it would require manually passing the value of `N` to the function via turbofish syntax, such as:
187
+
This is a rare case where the generic parameter can't be trivially resolved from the observed arguments. To get it to work without any advanced inference setups, it would require manually passing the value of `N` to the function via turbofish syntax, such as:
In the function scope, it might need to determine whether a variable is a generic parameter or not. We rules strings with at least 2 letters, which should be all capitalized, as generic parameters.
204
+
In the function scope, it might need to determine whether a variable is a generic parameter or not. We rule strings with at least 2 letters, which should be all capitalized, as generic parameters.
205
205
206
206
### AST Parser
207
207
Parser will need to collect the generic identifiers for the following constructions `FunctionDef`. It will add a new `TyKind`, the `GenericSizedArray(type, size)`. The size of `GenericSizedArray` is represented by a `Symbolic` value, which can contain generic parameters or concrete values.
@@ -255,7 +255,7 @@ FunctionDef{
255
255
The TAST use these metadata of generic parameters for type checking the consistency of generic identifiers. In MAST phase, they will be useful for resolving the generic values from the observed arguments.
256
256
257
257
### TAST
258
-
The generic values are resolved from the observed arguments. If the generic parameters are declared, they should be used in the function body. We need to check if the generic parameters declared make senses.
258
+
The generic values are resolved from the observed arguments. If the generic parameters are declared, they should be used in the function body. We need to check if the generic parameters declared make sense.
259
259
260
260
*Type check generic parameters for functions*
261
261
```rust
@@ -420,7 +420,7 @@ Here is an overview of the monomorphization process:
420
420
421
421
4. In the function instantiation process, all the AST nodes will be regenerated. This new AST will be stored under the monomorphized function name.
422
422
423
-
5. After monomorphized a function, it should add the name of the original function to a list that records which function AST to delete at the end. We can't not delete the original function AST immediately, because it might be called at different places.
423
+
5. After monomorphized a function, it should add the name of the original function to a list that records which function AST to delete at the end. We can't delete the original function AST immediately, because it might be called at different places.
424
424
425
425
6. In each function block scope, it should type check the return types, by comparing the propagated return type and the defined return type. All these types should be in concrete form without generic parameters involved.
426
426
@@ -433,7 +433,7 @@ All the updates are done to the existing stores in the TAST.
433
433
434
434
435
435
### Circuit Synthesizer
436
-
Circuit synthesizer will rely on the monomorphized AST to compile the circuit. To synthesizer, the workflow will be the same as before, but with the monomorphized AST. It doesn't need to be aware of the newly added support related to generics. The added MAST phase simplifies what needs to be done in the circuit synthesizer to support the generic features, in comparison to the alternative approach described in the following section.
436
+
Circuit synthesizer will rely on the monomorphized AST to compile the circuit. To the synthesizer, the workflow will be the same as before, but with the monomorphized AST. It doesn't need to be aware of the newly added support related to generics. The added MAST phase simplifies what needs to be done in the circuit synthesizer to support the generic features, in comparison to the alternative approach described in the following section.
437
437
438
438
## Alternative approach
439
439
[One alternative approach](https://github.com/zksecurity/noname/pull/136) to the monomorphization described above is to propagate the generic values directly in circuit writer, without the need to add the MAST phase.
@@ -450,4 +450,4 @@ For example, when it is computing for the `ExprKind::ArrayAccess`, it can use th
450
450
451
451
This approach would require a significant refactor of the circuit writer's compilation process. It would require changes to the assumptions from using `VarOrRef` to structured `ComputedExpr`. It would also need to rely on `ComputedExpr` to do some additional checks instead of just relying on types. This would require quite a number of additional assumptions between the `ComputedExpr`, the actual types and generic parameters.
452
452
453
-
Therefore, we thought the monomorphization approach is more straightforward and easier to maintain in a long run, considering the pipeline of the compiler.
453
+
Therefore, we thought the monomorphization approach is more straightforward and easier to maintain in the long run, considering the pipeline of the compiler.
0 commit comments