-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix function instantiation #187
base: main
Are you sure you want to change the base?
Conversation
This fix was quick! 😄 fn zip(a1: [Field; LEN], a2: [Field; LEN]) -> [[Field; 2]; LEN] {
let mut result = [[0; 2]; LEN];
for index in 0..LEN {
result[index] = [a1[index], a2[index]];
}
return result;
}
fn sum_arr(arr1: [Field; 3], arr2: [Field; 3]) -> [Field; 3] {
let mut result = [0; 3];
let pairs = zip(arr1, arr2);
for idx in 0..3 {
let item = pairs[idx];
result[idx] = item[0] + item[1];
}
return result;
}
fn main(pub arr: [Field; 3]) -> [Field; 3] {
let result = sum_arr(arr, arr);
return result;
} The function A possible approach would be to always monomorphize the body of the functions recursively, and then put in scope the monomorphized symbol (I mean the |
Cool, thanks for taking a look :)
I think it already works this way. Even though the body of The current rule for monomorphization is:
Then in synthesizer phase (circuit writer), the replaced |
but yeah, your example doesn't compile. I am taking a look. |
Indeed, this is an issue as you described above. This is a great spot! @gio54321 We used to instantiate all the functions, including the non generic ones. But we tried to only instantiate the generic functions while keeping the non generic function as it is in order to minimize the affected scopes after monomorphization phase. That is why the use of The problem this code doesn't work as I described above is because it only keeps walking into the generic functions when it handles Therefore, I think updating the handling of |
This PR is to fix #185
Changes
GenericSizedArray
typeAnother potential approach
To properly avoid instantiating all functions but only the ones containing generic vars or function calls, it would need an additional function AST walker to check if it is a function needed to be instantiated, or add a flag
require_instantiation
during the AST walk in TAST phase.Then it can properly determine whether to instantiate a function, instead of simply relying on checking if it contains generic variables in the function signature.