From 579beab90104f2e1f2dc95bdfe3e8182a55fc390 Mon Sep 17 00:00:00 2001 From: Ian McLerran Date: Wed, 15 Jan 2025 15:14:37 -0600 Subject: [PATCH 1/4] Result.map to Result.map_ok --- crates/compiler/builtins/roc/Decode.roc | 2 +- crates/compiler/builtins/roc/List.roc | 2 +- crates/compiler/builtins/roc/Result.roc | 6 +++--- crates/compiler/module/src/symbol.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/compiler/builtins/roc/Decode.roc b/crates/compiler/builtins/roc/Decode.roc index ce7d996af5..978b47ada7 100644 --- a/crates/compiler/builtins/roc/Decode.roc +++ b/crates/compiler/builtins/roc/Decode.roc @@ -165,4 +165,4 @@ from_bytes = \bytes, fmt -> ## Transform the `val` of a [DecodeResult] map_result : DecodeResult a, (a -> b) -> DecodeResult b -map_result = \{ result, rest }, mapper -> { result: Result.map(result, mapper), rest } +map_result = \{ result, rest }, mapper -> { result: Result.map_ok(result, mapper), rest } diff --git a/crates/compiler/builtins/roc/List.roc b/crates/compiler/builtins/roc/List.roc index c7aa005c5b..a204e5769d 100644 --- a/crates/compiler/builtins/roc/List.roc +++ b/crates/compiler/builtins/roc/List.roc @@ -1384,7 +1384,7 @@ map_try = \list, to_result -> list, [], \state, elem -> - Result.map( + Result.map_ok( to_result(elem), \ok -> List.append(state, ok), diff --git a/crates/compiler/builtins/roc/Result.roc b/crates/compiler/builtins/roc/Result.roc index ff7cfec885..8ab7f21007 100644 --- a/crates/compiler/builtins/roc/Result.roc +++ b/crates/compiler/builtins/roc/Result.roc @@ -2,7 +2,7 @@ module [ Result, is_ok, is_err, - map, + map_ok, map_err, map_both, map2, @@ -60,8 +60,8 @@ with_default = \result, default -> ## ## Functions like `map` are common in Roc; see for example [List.map], ## `Set.map`, and `Dict.map`. -map : Result a err, (a -> b) -> Result b err -map = \result, transform -> +map_ok : Result a err, (a -> b) -> Result b err +map_ok = \result, transform -> when result is Ok(v) -> Ok(transform(v)) Err(e) -> Err(e) diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 61e8d7dbbb..162c0665e7 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1521,7 +1521,7 @@ define_builtins! { 0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias 1 RESULT_IS_ERR: "is_err" 2 RESULT_ON_ERR: "on_err" - 3 RESULT_MAP: "map" + 3 RESULT_MAP_OK: "map_ok" 4 RESULT_MAP_ERR: "map_err" 5 RESULT_WITH_DEFAULT: "with_default" 6 RESULT_TRY: "try" From 326558337cead416f7ddbe5eeff0dc0a136276f5 Mon Sep 17 00:00:00 2001 From: Ian McLerran Date: Wed, 15 Jan 2025 15:18:29 -0600 Subject: [PATCH 2/4] update mono tests --- .../generated/choose_correct_recursion_var_under_record.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt b/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt index d7f2dcc714..b3224cfe9e 100644 --- a/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt +++ b/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt @@ -30,7 +30,7 @@ procedure Test.2 (Test.6): let Test.9 : List [C List [C List *self, C *self], C [C List *self, C *self]] = UnionAtIndex (Id 0) (Index 0) Test.6; joinpoint #Derived_gen.3: dec Test.9; - let Test.21 : Str = "ValueNotExposed { module_name: ModuleName(IdentStr { string: \"Result\" }), ident: Ident(IdentStr { string: \"withDefault\" }), region: @662-680, exposed_values: ['is_err', 'on_err', 'map', 'map_err', 'with_default', 'try', 'is_ok', 'map_both', 'map2', 'on_err!'] }"; + let Test.21 : Str = "ValueNotExposed { module_name: ModuleName(IdentStr { string: \"Result\" }), ident: Ident(IdentStr { string: \"withDefault\" }), region: @662-680, exposed_values: ['is_err', 'on_err', 'map_ok', 'map_err', 'with_default', 'try', 'is_ok', 'map_both', 'map2', 'on_err!'] }"; Crash Test.21 in let #Derived_gen.4 : Int1 = lowlevel RefCountIsUnique Test.6; From 297dd0233e28499a79c164b38a4019f6e28b38c5 Mon Sep 17 00:00:00 2001 From: Ian McLerran Date: Wed, 15 Jan 2025 19:52:14 -0600 Subject: [PATCH 3/4] update internal references to Result.map --- crates/cli/tests/benchmarks/AStar.roc | 2 +- crates/cli/tests/benchmarks/closure.roc | 10 +++++----- crates/compiler/builtins/roc/Result.roc | 4 ++-- .../tests/fixtures/build/app_with_deps/AStar.roc | 2 +- .../tests/fixtures/build/module_with_deps/AStar.roc | 2 +- crates/compiler/uitest/tests/ability/smoke/decoder.txt | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/cli/tests/benchmarks/AStar.roc b/crates/cli/tests/benchmarks/AStar.roc index 20856b8d7f..63437b57d1 100644 --- a/crates/cli/tests/benchmarks/AStar.roc +++ b/crates/cli/tests/benchmarks/AStar.roc @@ -32,7 +32,7 @@ cheapest_open = \cost_fn, model -> ) |> Quicksort.sort_by(.cost) |> List.first - |> Result.map(.position) + |> Result.map_ok(.position) |> Result.map_err(\_ -> {}) reconstruct_path : Dict position position, position -> List position where position implements Hash & Eq diff --git a/crates/cli/tests/benchmarks/closure.roc b/crates/cli/tests/benchmarks/closure.roc index 49febbab5c..7598d61361 100644 --- a/crates/cli/tests/benchmarks/closure.roc +++ b/crates/cli/tests/benchmarks/closure.roc @@ -12,7 +12,7 @@ main! = \{} -> closure1 : {} -> Result {} [] closure1 = \_ -> Ok(foo(to_unit_borrowed, "a long string such that it's malloced")) - |> Result.map(\_ -> {}) + |> Result.map_ok(\_ -> {}) to_unit_borrowed = \x -> Str.count_utf8_bytes(x) @@ -25,8 +25,8 @@ closure2 = \_ -> x = "a long string such that it's malloced" Ok({}) - |> Result.map(\_ -> x) - |> Result.map(to_unit) + |> Result.map_ok(\_ -> x) + |> Result.map_ok(to_unit) to_unit = \_ -> {} @@ -37,7 +37,7 @@ closure3 = \_ -> x = "a long string such that it's malloced" Ok({}) - |> Result.try(\_ -> Ok(x) |> Result.map(\_ -> {})) + |> Result.try(\_ -> Ok(x) |> Result.map_ok(\_ -> {})) # # --- closure4 : {} -> Result {} [] @@ -47,4 +47,4 @@ closure4 = \_ -> Ok({}) |> Result.try(\_ -> Ok(x)) - |> Result.map(\_ -> {}) + |> Result.map_ok(\_ -> {}) diff --git a/crates/compiler/builtins/roc/Result.roc b/crates/compiler/builtins/roc/Result.roc index 8ab7f21007..ee9a73e402 100644 --- a/crates/compiler/builtins/roc/Result.roc +++ b/crates/compiler/builtins/roc/Result.roc @@ -54,8 +54,8 @@ with_default = \result, default -> ## function on it. Then returns a new `Ok` holding the transformed value. If the ## result is `Err`, this has no effect. Use [map_err] to transform an `Err`. ## ```roc -## Result.map(Ok(12), Num.neg) -## Result.map(Err("yipes!"), Num.neg) +## Result.map_ok(Ok(12), Num.neg) +## Result.map_ok(Err("yipes!"), Num.neg) ## ``` ## ## Functions like `map` are common in Roc; see for example [List.map], diff --git a/crates/compiler/load_internal/tests/fixtures/build/app_with_deps/AStar.roc b/crates/compiler/load_internal/tests/fixtures/build/app_with_deps/AStar.roc index 385ca3a030..3f0fb2d411 100644 --- a/crates/compiler/load_internal/tests/fixtures/build/app_with_deps/AStar.roc +++ b/crates/compiler/load_internal/tests/fixtures/build/app_with_deps/AStar.roc @@ -37,7 +37,7 @@ cheapest_open = \cost_function, model -> Ok(smallest_so_far) Set.walk(model.open_set, Err(KeyNotFound), folder) - |> Result.map(\x -> x.position) + |> Result.map_ok(\x -> x.position) reconstruct_path : Map position position, position -> List position reconstruct_path = \came_from, goal -> diff --git a/crates/compiler/load_internal/tests/fixtures/build/module_with_deps/AStar.roc b/crates/compiler/load_internal/tests/fixtures/build/module_with_deps/AStar.roc index 43e240a890..8c1c29dee2 100644 --- a/crates/compiler/load_internal/tests/fixtures/build/module_with_deps/AStar.roc +++ b/crates/compiler/load_internal/tests/fixtures/build/module_with_deps/AStar.roc @@ -37,7 +37,7 @@ cheapest_open = \cost_function, model -> Ok(smallest_so_far) Set.walk(model.open_set, Err(KeyNotFound), folder) - |> Result.map(\x -> x.position) + |> Result.map_ok(\x -> x.position) reconstruct_path : Dict position position, position -> List position where position implements Hash & Eq reconstruct_path = \came_from, goal -> diff --git a/crates/compiler/uitest/tests/ability/smoke/decoder.txt b/crates/compiler/uitest/tests/ability/smoke/decoder.txt index 734ae96b3f..ee9b7900e5 100644 --- a/crates/compiler/uitest/tests/ability/smoke/decoder.txt +++ b/crates/compiler/uitest/tests/ability/smoke/decoder.txt @@ -37,7 +37,7 @@ decoder = @MDecoder \lst, fmt -> #^^^^^^^{-1} MyU8#decoder(12): MDecoder MyU8 fmt where fmt implements MDecoderFormatting when decode_with lst u8 fmt is { result, rest } -> - { result: Result.map result (\n -> @MyU8 n), rest } + { result: Result.map_ok result (\n -> @MyU8 n), rest } my_u8 : Result MyU8 _ my_u8 = from_bytes [15] (@Linear {}) From 14d99c690759941764bab94f72e4a6828aea47db Mon Sep 17 00:00:00 2001 From: Ian McLerran Date: Wed, 15 Jan 2025 21:30:19 -0600 Subject: [PATCH 4/4] update solve_expr and test_gen tests with map_ok --- crates/compiler/solve/tests/solve_expr.rs | 2 +- crates/compiler/test_gen/src/gen_abilities.rs | 4 ++-- crates/compiler/test_gen/src/gen_list.rs | 18 +++++++++--------- crates/compiler/test_gen/src/gen_result.rs | 10 +++++----- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/compiler/solve/tests/solve_expr.rs b/crates/compiler/solve/tests/solve_expr.rs index cbb8ef592e..95972c63cf 100644 --- a/crates/compiler/solve/tests/solve_expr.rs +++ b/crates/compiler/solve/tests/solve_expr.rs @@ -3479,7 +3479,7 @@ mod solve_expr { Ok { position, cost: 0.0 } Set.walk model.open_set (Ok { position: boom {}, cost: 0.0 }) folder - |> Result.map (\x -> x.position) + |> Result.map_ok (\x -> x.position) astar : Model position -> Result position [KeyNotFound] where position implements Hash & Eq astar = \model -> cheapest_open model diff --git a/crates/compiler/test_gen/src/gen_abilities.rs b/crates/compiler/test_gen/src/gen_abilities.rs index f8b43953b4..2293ff4fb8 100644 --- a/crates/compiler/test_gen/src/gen_abilities.rs +++ b/crates/compiler/test_gen/src/gen_abilities.rs @@ -337,7 +337,7 @@ fn decode() { # impl MDecoding for MyU8 decoder = @MDecoder \lst, fmt -> { result, rest } = decode_with lst u8 fmt - { result: Result.map result (\n -> @MyU8 n), rest } + { result: Result.map_ok result (\n -> @MyU8 n), rest } myU8 = when from_bytes [15] (@Linear {}) is @@ -2210,7 +2210,7 @@ fn issue_4772_weakened_monomorphic_destructure() { Err (ParsingFailure "not a number") main = - get_number |> Result.map .val |> Result.with_default 0 + get_number |> Result.map_ok .val |> Result.with_default 0 "# ), 1234i64, diff --git a/crates/compiler/test_gen/src/gen_list.rs b/crates/compiler/test_gen/src/gen_list.rs index 22bfceed2c..33e4271423 100644 --- a/crates/compiler/test_gen/src/gen_list.rs +++ b/crates/compiler/test_gen/src/gen_list.rs @@ -554,7 +554,7 @@ fn list_split_first() { assert_evals_to!( r" List.split_first [2, 3, 0, 4, 0, 6, 0, 8, 9] 0 - |> Result.map .before + |> Result.map_ok .before ", RocResult::ok(RocList::::from_slice(&[2, 3])), RocResult, ()> @@ -562,7 +562,7 @@ fn list_split_first() { assert_evals_to!( r" List.split_first [2, 3, 0, 4, 0, 6, 0, 8, 9] 0 - |> Result.map .after + |> Result.map_ok .after ", RocResult::ok(RocList::::from_slice(&[4, 0, 6, 0, 8, 9])), RocResult, ()> @@ -587,7 +587,7 @@ fn list_split_last() { assert_evals_to!( r" List.split_last [2, 3, 0, 4, 0, 6, 0, 8, 9] 0 - |> Result.map .before + |> Result.map_ok .before ", RocResult::ok(RocList::::from_slice(&[2, 3, 0, 4, 0, 6])), RocResult, ()> @@ -595,7 +595,7 @@ fn list_split_last() { assert_evals_to!( r" List.split_last [2, 3, 0, 4, 0, 6, 0, 8, 9] 0 - |> Result.map .after + |> Result.map_ok .after ", RocResult::ok(RocList::::from_slice(&[8, 9])), RocResult, ()> @@ -2164,7 +2164,7 @@ fn first_wildcard_empty_list() { assert_evals_to!( indoc!( r" - List.last [] |> Result.map (\_ -> 0i64) + List.last [] |> Result.map_ok (\_ -> 0i64) " ), RocResult::err(()), @@ -2209,7 +2209,7 @@ fn last_wildcard_empty_list() { assert_evals_to!( indoc!( r" - List.last [] |> Result.map (\_ -> 0i64) + List.last [] |> Result.map_ok (\_ -> 0i64) " ), RocResult::err(()), @@ -2261,7 +2261,7 @@ fn get_wildcard_empty_list() { indoc!( r" List.get [] 0 - |> Result.map (\_ -> {}) + |> Result.map_ok (\_ -> {}) " ), RocResult::err(()), @@ -2978,7 +2978,7 @@ fn list_min() { indoc!( r" List.min [] - |> Result.map (\_ -> {}) + |> Result.map_ok (\_ -> {}) " ), RocResult::err(()), @@ -3003,7 +3003,7 @@ fn list_max() { indoc!( r" List.max [] - |> Result.map (\_ -> {}) + |> Result.map_ok (\_ -> {}) " ), RocResult::err(()), diff --git a/crates/compiler/test_gen/src/gen_result.rs b/crates/compiler/test_gen/src/gen_result.rs index e46045dff6..de4fd7daf1 100644 --- a/crates/compiler/test_gen/src/gen_result.rs +++ b/crates/compiler/test_gen/src/gen_result.rs @@ -56,7 +56,7 @@ fn result_map() { result = Ok 2 result - |> Result.map (\x -> x + 1) + |> Result.map_ok (\x -> x + 1) |> Result.with_default 0 " ), @@ -71,7 +71,7 @@ fn result_map() { result = Err {} result - |> Result.map (\x -> x + 1) + |> Result.map_ok (\x -> x + 1) |> Result.with_default 0 " ), @@ -120,7 +120,7 @@ fn err_type_var() { assert_evals_to!( indoc!( r" - Result.map (Ok 3) (\x -> x + 1) + Result.map_ok (Ok 3) (\x -> x + 1) |> Result.with_default -1 " ), @@ -138,7 +138,7 @@ fn err_type_var_annotation() { ok : Result I64 * ok = Ok 3 - Result.map ok (\x -> x + 1) + Result.map_ok ok (\x -> x + 1) |> Result.with_default -1 " ), @@ -156,7 +156,7 @@ fn err_empty_tag_union() { ok : Result I64 [] ok = Ok 3 - Result.map ok (\x -> x + 1) + Result.map_ok ok (\x -> x + 1) |> Result.with_default -1 " ),