-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use an unified Function object, fix some problems, comments
- Loading branch information
1 parent
0c1ed5a
commit a3c9ad7
Showing
29 changed files
with
639 additions
and
165 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
crates/nargo_cli/tests/test_data_ssa_refactor/closures_mut_ref/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "closures_mut_ref" | ||
authors = [""] | ||
compiler_version = "0.8.0" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
crates/nargo_cli/tests/test_data_ssa_refactor/closures_mut_ref/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x = "0" |
19 changes: 19 additions & 0 deletions
19
crates/nargo_cli/tests/test_data_ssa_refactor/closures_mut_ref/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use dep::std; | ||
|
||
fn main(mut x: Field) { | ||
|
||
let add1 = |z| { | ||
*z = *z + 1; | ||
}; | ||
|
||
let add2 = |z| { | ||
*z = *z + 2; | ||
}; | ||
|
||
add1(&mut x); | ||
assert(x == 1); | ||
|
||
add2(&mut x); | ||
assert(x == 3); | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
crates/nargo_cli/tests/test_data_ssa_refactor/fibonacci_by_ref/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "fibonacci_by_ref" | ||
authors = [""] | ||
compiler_version = "0.8.0" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/fibonacci_by_ref/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
prev = "1" | ||
cur = "2" |
15 changes: 15 additions & 0 deletions
15
crates/nargo_cli/tests/test_data_ssa_refactor/fibonacci_by_ref/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fn fib_fn(a: Field, b: Field, res: &mut Field) { | ||
*res = a + b; | ||
} | ||
|
||
fn main(mut prev: Field, mut cur: Field) { | ||
|
||
let mut fib = prev + cur; | ||
for i in 1..10 { | ||
prev = cur; | ||
cur = fib; | ||
fib_fn(prev, cur, &mut fib); | ||
assert(prev + cur == fib); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_fn_selector/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "higher_order_fn_selector" | ||
authors = [""] | ||
compiler_version = "0.8.0" | ||
|
||
[dependencies] |
49 changes: 49 additions & 0 deletions
49
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_fn_selector/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
fn f(x: &mut Field) -> Field { | ||
*x = *x - 1; | ||
1 | ||
} | ||
|
||
fn g(x: &mut Field) -> Field { | ||
*x *= 2; | ||
1 | ||
} | ||
|
||
fn h(x: &mut Field) -> Field { | ||
*x *= 3; | ||
1 | ||
} | ||
|
||
use dep::std; | ||
|
||
fn selector(flag:&mut bool) -> fn(&mut Field) -> Field { //TODO: Can we have fn(&mut Field) -> () return type? | ||
let mut my_func = f; | ||
|
||
if *flag { | ||
my_func = g; | ||
} | ||
else { | ||
my_func = h; | ||
}; | ||
|
||
// Flip the flag for the next function call | ||
*flag = !(*flag); | ||
my_func | ||
} | ||
|
||
fn main() { | ||
|
||
let mut flag: bool = true; | ||
|
||
let mut x: Field = 100; | ||
let returned_func = selector(&mut flag); | ||
let status = returned_func(&mut x); | ||
|
||
assert(x == 200); | ||
|
||
let mut y: Field = 100; | ||
let returned_func2 = selector(&mut flag); | ||
let status2 = returned_func2(&mut y); | ||
|
||
assert(y == 300); | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_functions/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "higher_order_functions" | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
Empty file.
88 changes: 88 additions & 0 deletions
88
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_functions/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use dep::std; | ||
|
||
fn main() -> pub Field { | ||
let f = if 3 * 7 > 200 as u32 { foo } else { bar }; | ||
assert(f()[1] == 2); | ||
// Lambdas: | ||
assert(twice(|x| x * 2, 5) == 20); | ||
assert((|x, y| x + y + 1)(2, 3) == 6); | ||
|
||
// nested lambdas | ||
assert((|a, b| { | ||
a + (|c| c + 2)(b) | ||
})(0, 1) == 3); | ||
|
||
|
||
// Closures: | ||
let a = 42; | ||
let g = || a; | ||
assert(g() == 42); | ||
|
||
// TODO: enable this again after fixing #2054 | ||
// https://github.com/noir-lang/noir/issues/2054 | ||
// by @jfecher's PR https://github.com/noir-lang/noir/pull/2057 | ||
|
||
// Mutable variables cannot be captured, but you can | ||
// copy them into immutable variables and capture those: | ||
// let mut x = 2; | ||
// x = x + 1; | ||
// let z = x; | ||
|
||
// Add extra mutations to ensure we can mutate x without the | ||
// captured z changing. | ||
// x = x + 1; | ||
// TODO: this behavior changed in the new ssa backend: | ||
// now even z is changed, and it wasn't in the previous backend | ||
// assert(z == 2); | ||
// fails! | ||
// decide what to do after opening an issue about the simpler | ||
// variable alias case | ||
// | ||
// assert((|y| y + z)(1) == 4); | ||
|
||
let ret = twice(add1, 3); | ||
|
||
test_array_functions(); | ||
ret | ||
} | ||
|
||
/// Test the array functions in std::array | ||
fn test_array_functions() { | ||
let myarray: [i32; 3] = [1, 2, 3]; | ||
assert(myarray.any(|n| n > 2)); | ||
|
||
let evens: [i32; 3] = [2, 4, 6]; | ||
assert(evens.all(|n| n > 1)); | ||
|
||
assert(evens.fold(0, |a, b| a + b) == 12); | ||
assert(evens.reduce(|a, b| a + b) == 12); | ||
|
||
// TODO: is this a sort_via issue with the new backend, | ||
// or something more general? | ||
// | ||
// currently it fails only with `--experimental-ssa` with | ||
// "not yet implemented: Cast into signed" | ||
// but it worked with the original ssa backend | ||
// (before dropping it) | ||
// | ||
// let descending = myarray.sort_via(|a, b| a > b); | ||
// assert(descending == [3, 2, 1]); | ||
|
||
assert(evens.map(|n| n / 2) == myarray); | ||
} | ||
|
||
fn foo() -> [u32; 2] { | ||
[1, 3] | ||
} | ||
|
||
fn bar() -> [u32; 2] { | ||
[3, 2] | ||
} | ||
|
||
fn add1(x: Field) -> Field { | ||
x + 1 | ||
} | ||
|
||
fn twice(f: fn(Field) -> Field, x: Field) -> Field { | ||
f(f(x)) | ||
} |
1 change: 1 addition & 0 deletions
1
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_functions/target/c.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[],"param_witnesses":{},"return_type":null,"return_witnesses":[]},"bytecode":[155,194,56,97,194,4,0],"proving_key":null,"verification_key":null} |
1 change: 1 addition & 0 deletions
1
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_functions/target/main.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"z","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2],"z":[3]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/9WUTW6DMBSEJ/yFhoY26bYLjoAxBLPrVYpK7n+EgmoHamWXeShYQsYSvJ+Z9/kDwCf+1m58ArsXi3PgnUN7dt/u7P9fdi8fW8rlATduCW89GFe5l2iMES90YBd+EyTyjIjtGYIm+HF1eanroa0GpdV3WXW9acq66S9GGdWY5qcyWg+mNm3Xd23ZqVoP6tp0+moDJ5AxNOTUWdk6VUTsOSb6wtRPCuDYziaZAzGA92OMFCsAPCUqMAOcQg5gZwIb4BdsA+A9seeU6AtTPymAUzubZA7EAD6MMTKsAPCUqMAMcAY5gJ0JbIBfsQ2AD8SeM6IvTP2kAM7sbJI5EAP4OMbIsQLAU6ICM8A55AB2JrABfsM2AD4Se86Jvjy5freeQ2LPObGud6J+Ce5ADz6LzJqX9Z4W75HdgzszkQj0BC+Pr6PohSpl0kkg7hm84Zfq+8z36N/l9OyaLtcv2EfpKJUUAAA=","proving_key":null,"verification_key":null} |
Binary file added
BIN
+112 Bytes
crates/nargo_cli/tests/test_data_ssa_refactor/higher_order_functions/target/witness.tr
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
crates/nargo_cli/tests/test_data_ssa_refactor/inner_outer_cl/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "inner_outer_cl" | ||
authors = [""] | ||
compiler_version = "0.7.1" | ||
|
||
[dependencies] |
10 changes: 10 additions & 0 deletions
10
crates/nargo_cli/tests/test_data_ssa_refactor/inner_outer_cl/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() { | ||
let cl_outer = |x| { | ||
let cl_inner = |y| { | ||
x + y | ||
}; | ||
cl_inner(1) | ||
}; | ||
let result = cl_outer(1); | ||
assert(result == 2); | ||
} |
6 changes: 6 additions & 0 deletions
6
crates/nargo_cli/tests/test_data_ssa_refactor/ret_fn_ret_cl/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "ret_fn_ret_cl" | ||
authors = [""] | ||
compiler_version = "0.7.1" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
crates/nargo_cli/tests/test_data_ssa_refactor/ret_fn_ret_cl/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x = "10" |
29 changes: 29 additions & 0 deletions
29
crates/nargo_cli/tests/test_data_ssa_refactor/ret_fn_ret_cl/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use dep::std; | ||
|
||
fn f(x: Field) -> Field { | ||
x | ||
} | ||
|
||
fn ret_fn() -> fn(Field) -> Field { | ||
let y = 1; | ||
let inner_closure = |z| -> Field{ | ||
z + y | ||
}; | ||
std::println(inner_closure(1)); | ||
f | ||
} | ||
|
||
fn ret_closure() -> fn(Field) -> Field { | ||
let cl = |z: Field| -> Field { | ||
z | ||
}; | ||
cl | ||
} | ||
|
||
fn main(x : Field) { | ||
let result_fn = ret_fn(); | ||
assert(result_fn(x) == x); // Works | ||
|
||
let result_cl = ret_closure(); | ||
assert(result_cl(x) == x); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.