diff --git a/compiler/builtins/bitcode/src/dict.zig b/compiler/builtins/bitcode/src/dict.zig index e08781104f..fee8c7fcf9 100644 --- a/compiler/builtins/bitcode/src/dict.zig +++ b/compiler/builtins/bitcode/src/dict.zig @@ -768,7 +768,7 @@ pub fn dictWalk( const key = dict.getKey(i, alignment, key_width, value_width); const value = dict.getValue(i, alignment, key_width, value_width); - caller(data, key, value, b2, b1); + caller(data, b2, key, value, b1); std.mem.swap([*]u8, &b1, &b2); }, diff --git a/compiler/builtins/bitcode/src/list.zig b/compiler/builtins/bitcode/src/list.zig index 2b83458408..a2f9cfd6df 100644 --- a/compiler/builtins/bitcode/src/list.zig +++ b/compiler/builtins/bitcode/src/list.zig @@ -560,7 +560,7 @@ pub fn listWalk( const size = list.len(); while (i < size) : (i += 1) { const element = source_ptr + i * element_width; - caller(data, element, b2, b1); + caller(data, b2, element, b1); std.mem.swap([*]u8, &b1, &b2); } @@ -607,7 +607,7 @@ pub fn listWalkBackwards( while (i > 0) { i -= 1; const element = source_ptr + i * element_width; - caller(data, element, b2, b1); + caller(data, b2, element, b1); std.mem.swap([*]u8, &b1, &b2); } @@ -658,7 +658,7 @@ pub fn listWalkUntil( inc_n_data(data, 1); } - caller(data, element, bytes_ptr, bytes_ptr); + caller(data, bytes_ptr, element, bytes_ptr); // [ Continue ..., Stop ] const tag_id = has_tag_id(0, bytes_ptr); diff --git a/compiler/builtins/src/std.rs b/compiler/builtins/src/std.rs index a733cec6ef..9c03042f41 100644 --- a/compiler/builtins/src/std.rs +++ b/compiler/builtins/src/std.rs @@ -793,7 +793,7 @@ pub fn types() -> MutMap { list_type(flex(TVAR1)), flex(TVAR2), closure( - vec![flex(TVAR1), flex(TVAR2)], + vec![flex(TVAR2), flex(TVAR1)], TVAR3, Box::new(until_type(flex(TVAR2))), ), @@ -1173,13 +1173,13 @@ pub fn types() -> MutMap { Box::new(set_type(flex(TVAR1))), ); - // Set.walk : Set a, (a, b -> b), b -> b + // Set.walk : Set a, (b, a -> b), b -> b add_top_level_function_type!( Symbol::SET_WALK, vec![ set_type(flex(TVAR1)), - closure(vec![flex(TVAR1), flex(TVAR2)], TVAR3, Box::new(flex(TVAR2))), flex(TVAR2), + closure(vec![flex(TVAR2), flex(TVAR1)], TVAR3, Box::new(flex(TVAR2))), ], Box::new(flex(TVAR2)), ); diff --git a/compiler/can/src/builtins.rs b/compiler/can/src/builtins.rs index 25457fbee6..05dcb00746 100644 --- a/compiler/can/src/builtins.rs +++ b/compiler/can/src/builtins.rs @@ -2550,7 +2550,7 @@ fn set_contains(symbol: Symbol, var_store: &mut VarStore) -> Def { dict_contains(symbol, var_store) } -/// Set.walk : Set k, (k, accum -> accum), accum -> accum +/// Set.walk : Set k, (accum, k -> accum), accum -> accum fn set_walk(symbol: Symbol, var_store: &mut VarStore) -> Def { let dict_var = var_store.fresh(); let func_var = var_store.fresh(); @@ -2560,7 +2560,7 @@ fn set_walk(symbol: Symbol, var_store: &mut VarStore) -> Def { let user_function = Box::new(( func_var, - no_region(Var(Symbol::ARG_2)), + no_region(Var(Symbol::ARG_3)), var_store.fresh(), accum_var, )); @@ -2568,8 +2568,8 @@ fn set_walk(symbol: Symbol, var_store: &mut VarStore) -> Def { let call_func = Call( user_function, vec![ - (key_var, no_region(Var(Symbol::ARG_5))), - (accum_var, no_region(Var(Symbol::ARG_6))), + (accum_var, no_region(Var(Symbol::ARG_5))), + (key_var, no_region(Var(Symbol::ARG_6))), ], CalledVia::Space, ); @@ -2581,11 +2581,11 @@ fn set_walk(symbol: Symbol, var_store: &mut VarStore) -> Def { return_type: accum_var, name: Symbol::SET_WALK_USER_FUNCTION, recursive: Recursive::NotRecursive, - captured_symbols: vec![(Symbol::ARG_2, func_var)], + captured_symbols: vec![(Symbol::ARG_3, func_var)], arguments: vec![ - (key_var, no_region(Pattern::Identifier(Symbol::ARG_5))), + (accum_var, no_region(Pattern::Identifier(Symbol::ARG_5))), + (key_var, no_region(Pattern::Identifier(Symbol::ARG_6))), (Variable::EMPTY_RECORD, no_region(Pattern::Underscore)), - (accum_var, no_region(Pattern::Identifier(Symbol::ARG_6))), ], loc_body: Box::new(no_region(call_func)), }; @@ -2594,8 +2594,8 @@ fn set_walk(symbol: Symbol, var_store: &mut VarStore) -> Def { op: LowLevel::DictWalk, args: vec![ (dict_var, Var(Symbol::ARG_1)), + (accum_var, Var(Symbol::ARG_2)), (wrapper_var, wrapper), - (accum_var, Var(Symbol::ARG_3)), ], ret_var: accum_var, }; @@ -2604,8 +2604,8 @@ fn set_walk(symbol: Symbol, var_store: &mut VarStore) -> Def { symbol, vec![ (dict_var, Symbol::ARG_1), - (func_var, Symbol::ARG_2), - (accum_var, Symbol::ARG_3), + (accum_var, Symbol::ARG_2), + (func_var, Symbol::ARG_3), ], var_store, body, diff --git a/compiler/gen_llvm/src/llvm/build.rs b/compiler/gen_llvm/src/llvm/build.rs index ea3eef69d2..eac79e9978 100644 --- a/compiler/gen_llvm/src/llvm/build.rs +++ b/compiler/gen_llvm/src/llvm/build.rs @@ -4501,7 +4501,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>( match list_layout { Layout::Builtin(Builtin::EmptyList) => default, Layout::Builtin(Builtin::List(element_layout)) => { - let argument_layouts = &[**element_layout, *default_layout]; + let argument_layouts = &[*default_layout, **element_layout]; let roc_function_call = roc_function_call( env, @@ -4872,7 +4872,7 @@ fn run_higher_order_low_level<'a, 'ctx, 'env>( panic!("key type unknown") } Layout::Builtin(Builtin::Dict(key_layout, value_layout)) => { - let argument_layouts = &[**key_layout, **value_layout, *default_layout]; + let argument_layouts = &[*default_layout, **key_layout, **value_layout]; let roc_function_call = roc_function_call( env, diff --git a/compiler/mono/src/alias_analysis.rs b/compiler/mono/src/alias_analysis.rs index b218875425..3cf3ab797e 100644 --- a/compiler/mono/src/alias_analysis.rs +++ b/compiler/mono/src/alias_analysis.rs @@ -641,7 +641,7 @@ fn call_spec( match op { DictWalk => { let dict = env.symbols[&call.arguments[0]]; - let default = env.symbols[&call.arguments[1]]; + let state = env.symbols[&call.arguments[1]]; let closure_env = env.symbols[&call.arguments[3]]; let bag = builder.add_get_tuple_field(block, dict, DICT_BAG_INDEX)?; @@ -653,16 +653,16 @@ fn call_spec( let val = builder.add_get_tuple_field(block, first, 1)?; let argument = if closure_env_layout.is_none() { - builder.add_make_tuple(block, &[key, val, default])? + builder.add_make_tuple(block, &[state, key, val])? } else { - builder.add_make_tuple(block, &[key, val, default, closure_env])? + builder.add_make_tuple(block, &[state, key, val, closure_env])? }; builder.add_call(block, spec_var, module, name, argument)?; } ListWalk | ListWalkBackwards | ListWalkUntil => { let list = env.symbols[&call.arguments[0]]; - let default = env.symbols[&call.arguments[1]]; + let state = env.symbols[&call.arguments[1]]; let closure_env = env.symbols[&call.arguments[3]]; let bag = builder.add_get_tuple_field(block, list, LIST_BAG_INDEX)?; @@ -671,9 +671,9 @@ fn call_spec( let first = builder.add_bag_get(block, bag)?; let argument = if closure_env_layout.is_none() { - builder.add_make_tuple(block, &[first, default])? + builder.add_make_tuple(block, &[state, first])? } else { - builder.add_make_tuple(block, &[first, default, closure_env])? + builder.add_make_tuple(block, &[state, first, closure_env])? }; builder.add_call(block, spec_var, module, name, argument)?; } diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index 4feb79df72..cc21209828 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -4039,9 +4039,12 @@ pub fn with_hole<'a>( ListWalk | ListWalkUntil | ListWalkBackwards | DictWalk => { debug_assert_eq!(arg_symbols.len(), 3); - let closure_index = 2; - let closure_data_symbol = arg_symbols[closure_index]; - let closure_data_var = args[closure_index].0; + const LIST_INDEX: usize = 0; + const DEFAULT_INDEX: usize = 1; + const CLOSURE_INDEX: usize = 2; + + let closure_data_symbol = arg_symbols[CLOSURE_INDEX]; + let closure_data_var = args[CLOSURE_INDEX].0; let stmt = match_on_closure_argument!( env, @@ -4050,7 +4053,7 @@ pub fn with_hole<'a>( closure_data_symbol, closure_data_var, op, - [arg_symbols[0], arg_symbols[1]], + [arg_symbols[LIST_INDEX], arg_symbols[DEFAULT_INDEX]], layout, assigned, hole @@ -4062,47 +4065,35 @@ pub fn with_hole<'a>( // actual closure, and the default is either the number 1 or 0 // this can be removed when we define builtin modules as proper modules - let stmt = { - const ARG_INDEX: usize = 0; + let stmt = assign_to_symbol( + env, + procs, + layout_cache, + args[LIST_INDEX].0, + Located::at_zero(args[LIST_INDEX].1.clone()), + arg_symbols[LIST_INDEX], + stmt, + ); - assign_to_symbol( - env, - procs, - layout_cache, - args[ARG_INDEX].0, - Located::at_zero(args[ARG_INDEX].1.clone()), - arg_symbols[ARG_INDEX], - stmt, - ) - }; + let stmt = assign_to_symbol( + env, + procs, + layout_cache, + args[DEFAULT_INDEX].0, + Located::at_zero(args[DEFAULT_INDEX].1.clone()), + arg_symbols[DEFAULT_INDEX], + stmt, + ); - let stmt = { - const ARG_INDEX: usize = 1; - - assign_to_symbol( - env, - procs, - layout_cache, - args[ARG_INDEX].0, - Located::at_zero(args[ARG_INDEX].1.clone()), - arg_symbols[ARG_INDEX], - stmt, - ) - }; - - { - const ARG_INDEX: usize = 2; - - assign_to_symbol( - env, - procs, - layout_cache, - args[ARG_INDEX].0, - Located::at_zero(args[ARG_INDEX].1.clone()), - arg_symbols[ARG_INDEX], - stmt, - ) - } + assign_to_symbol( + env, + procs, + layout_cache, + args[CLOSURE_INDEX].0, + Located::at_zero(args[CLOSURE_INDEX].1.clone()), + arg_symbols[CLOSURE_INDEX], + stmt, + ) } ListMap2 => { debug_assert_eq!(arg_symbols.len(), 3); diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index e43ac44ebc..ecef19861b 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -3405,7 +3405,7 @@ mod solve_expr { else Ok { position, cost: 0.0 } - Set.walk model.openSet folder (Ok { position: boom {}, cost: 0.0 }) + Set.walk model.openSet (Ok { position: boom {}, cost: 0.0 }) folder |> Result.map (\x -> x.position) astar : Model position -> Result position [ KeyNotFound ]* @@ -3689,7 +3689,7 @@ mod solve_expr { List.walkBackwards "# ), - "List a, (a, b -> b), b -> b", + "List a, b, (a, b -> b) -> b", ); } @@ -3702,7 +3702,7 @@ mod solve_expr { empty = [] - List.walkBackwards empty (\a, b -> a + b) 0 + List.walkBackwards empty 0 (\a, b -> a + b) "# ), "I64", diff --git a/compiler/test_gen/src/gen_dict.rs b/compiler/test_gen/src/gen_dict.rs index a4efd8cc88..bb46b46ad8 100644 --- a/compiler/test_gen/src/gen_dict.rs +++ b/compiler/test_gen/src/gen_dict.rs @@ -191,7 +191,7 @@ fn from_list_with_fold() { myDict : Dict I64 I64 myDict = [1,2,3] - |> List.walk (\value, accum -> Dict.insert accum value value) Dict.empty + |> List.walk Dict.empty (\accum, value -> Dict.insert accum value value) Dict.values myDict "# @@ -214,7 +214,7 @@ fn from_list_with_fold() { myDict = # 25 elements (8 + 16 + 1) is guaranteed to overflow/reallocate at least twice range 0 25 [] - |> List.walk (\value, accum -> Dict.insert accum value value) Dict.empty + |> List.walk Dict.empty (\accum, value -> Dict.insert accum value value) Dict.values myDict |> List.len @@ -373,7 +373,7 @@ fn intersection() { indoc!( r#" dict1 : Dict I64 {} - dict1 = + dict1 = Dict.empty |> Dict.insert 1 {} |> Dict.insert 2 {} @@ -382,14 +382,14 @@ fn intersection() { |> Dict.insert 5 {} dict2 : Dict I64 {} - dict2 = + dict2 = Dict.empty |> Dict.insert 0 {} |> Dict.insert 2 {} |> Dict.insert 4 {} - Dict.intersection dict1 dict2 - |> Dict.len + Dict.intersection dict1 dict2 + |> Dict.len "# ), 2, @@ -403,7 +403,7 @@ fn intersection_prefer_first() { indoc!( r#" dict1 : Dict I64 I64 - dict1 = + dict1 = Dict.empty |> Dict.insert 1 1 |> Dict.insert 2 2 @@ -412,14 +412,14 @@ fn intersection_prefer_first() { |> Dict.insert 5 5 dict2 : Dict I64 I64 - dict2 = + dict2 = Dict.empty |> Dict.insert 0 100 |> Dict.insert 2 200 |> Dict.insert 4 300 - Dict.intersection dict1 dict2 - |> Dict.values + Dict.intersection dict1 dict2 + |> Dict.values "# ), RocList::from_slice(&[4, 2]), @@ -433,7 +433,7 @@ fn difference() { indoc!( r#" dict1 : Dict I64 {} - dict1 = + dict1 = Dict.empty |> Dict.insert 1 {} |> Dict.insert 2 {} @@ -442,14 +442,14 @@ fn difference() { |> Dict.insert 5 {} dict2 : Dict I64 {} - dict2 = + dict2 = Dict.empty |> Dict.insert 0 {} |> Dict.insert 2 {} |> Dict.insert 4 {} - Dict.difference dict1 dict2 - |> Dict.len + Dict.difference dict1 dict2 + |> Dict.len "# ), 3, @@ -463,7 +463,7 @@ fn difference_prefer_first() { indoc!( r#" dict1 : Dict I64 I64 - dict1 = + dict1 = Dict.empty |> Dict.insert 1 1 |> Dict.insert 2 2 @@ -472,14 +472,14 @@ fn difference_prefer_first() { |> Dict.insert 5 5 dict2 : Dict I64 I64 - dict2 = + dict2 = Dict.empty |> Dict.insert 0 100 |> Dict.insert 2 200 |> Dict.insert 4 300 - Dict.difference dict1 dict2 - |> Dict.values + Dict.difference dict1 dict2 + |> Dict.values "# ), RocList::from_slice(&[5, 3, 1]), @@ -493,7 +493,7 @@ fn walk_sum_keys() { indoc!( r#" dict1 : Dict I64 I64 - dict1 = + dict1 = Dict.empty |> Dict.insert 1 1 |> Dict.insert 2 2 diff --git a/compiler/test_gen/src/gen_list.rs b/compiler/test_gen/src/gen_list.rs index 9530e6d85a..719e61da44 100644 --- a/compiler/test_gen/src/gen_list.rs +++ b/compiler/test_gen/src/gen_list.rs @@ -399,13 +399,13 @@ fn list_walk_backwards_empty_all_inline() { fn list_walk_backwards_with_str() { assert_evals_to!( r#"List.walkBackwards [ "x", "y", "z" ] "<" Str.concat"#, - RocStr::from("xyz<"), + RocStr::from(" - if Num.isEven a then + if Num.isEven b then Continue (a + b) else - Stop b + Stop a List.walkUntil [ 2, 4, 8, 9 ] 0 helper"#, 2 + 4 + 8, diff --git a/compiler/test_gen/src/gen_set.rs b/compiler/test_gen/src/gen_set.rs index 2ac72f847c..41551f7c28 100644 --- a/compiler/test_gen/src/gen_set.rs +++ b/compiler/test_gen/src/gen_set.rs @@ -163,7 +163,7 @@ fn walk_sum() { assert_evals_to!( indoc!( r#" - Set.walk (Set.fromList [1,2,3]) (\x, y -> x + y) 0 + Set.walk (Set.fromList [1,2,3]) 0 (\x, y -> x + y) "# ), 6, diff --git a/examples/benchmarks/AStar.roc b/examples/benchmarks/AStar.roc index ed8b1ac1f8..ebe15f07d9 100644 --- a/examples/benchmarks/AStar.roc +++ b/examples/benchmarks/AStar.roc @@ -101,13 +101,17 @@ astar = \costFn, moveFn, goal, model -> newNeighbors = Set.difference neighbors modelPopped.evaluated + modelWithNeighbors : Model position modelWithNeighbors = { modelPopped & openSet: Set.union modelPopped.openSet newNeighbors } + walker : Model position, position -> Model position + walker = \amodel, n -> updateCost current n amodel + modelWithCosts = - Set.walk newNeighbors (\n, m -> updateCost current n m) modelWithNeighbors + Set.walk newNeighbors modelWithNeighbors walker astar costFn moveFn goal modelWithCosts @@ -117,17 +121,17 @@ astar = \costFn, moveFn, goal, model -> # openSet: Set.remove model.openSet current, # evaluated: Set.insert model.evaluated current, # } -# +# # neighbors = moveFn current -# +# # newNeighbors = Set.difference neighbors modelPopped.evaluated -# +# # modelWithNeighbors = { modelPopped & openSet: Set.union modelPopped.openSet newNeighbors } -# +# # # a lot goes wrong here # modelWithCosts = -# Set.walk newNeighbors (\n, m -> updateCost current n m) modelWithNeighbors -# +# Set.walk newNeighbors modelWithNeighbors (\n, m -> updateCost current n m) +# # modelWithCosts