From 05c01a81f56dc76308fcde872fb7c75bd255ff20 Mon Sep 17 00:00:00 2001 From: Eric Newbury Date: Fri, 21 Jan 2022 15:34:24 -0500 Subject: [PATCH] adding List.sortAsc builtin --- compiler/builtins/src/std.rs | 7 +++++++ compiler/can/src/builtins.rs | 34 ++++++++++++++++++++++++++++++- compiler/module/src/symbol.rs | 1 + compiler/test_gen/src/gen_list.rs | 11 ++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/compiler/builtins/src/std.rs b/compiler/builtins/src/std.rs index 53bd661fe4..d28ab24cb0 100644 --- a/compiler/builtins/src/std.rs +++ b/compiler/builtins/src/std.rs @@ -1302,6 +1302,13 @@ pub fn types() -> MutMap { Box::new(list_type(flex(TVAR1))), ); + // sortAsc : List (Num a) -> List (Num a) + add_top_level_function_type!( + Symbol::LIST_SORT_ASC, + vec![list_type(num_type(flex(TVAR1)))], + Box::new(list_type(num_type(flex(TVAR1)))) + ); + // find : List elem, (elem -> Bool) -> Result elem [ NotFound ]* { let not_found = SolvedType::TagUnion( diff --git a/compiler/can/src/builtins.rs b/compiler/can/src/builtins.rs index 0f47ed431a..9e2f8a13e7 100644 --- a/compiler/can/src/builtins.rs +++ b/compiler/can/src/builtins.rs @@ -51,7 +51,7 @@ macro_rules! macro_magic { /// even those that are relied on transitively! pub fn builtin_dependencies(symbol: Symbol) -> &'static [Symbol] { match symbol { - // Symbol::LIST_SORT_ASC => &[Symbol::LIST_SORT_WITH, Symbol::NUM_COMPARE], + Symbol::LIST_SORT_ASC => &[Symbol::LIST_SORT_WITH, Symbol::NUM_COMPARE], Symbol::LIST_PRODUCT => &[Symbol::LIST_WALK, Symbol::NUM_MUL], Symbol::LIST_SUM => &[Symbol::LIST_WALK, Symbol::NUM_ADD], Symbol::LIST_JOIN_MAP => &[Symbol::LIST_WALK, Symbol::LIST_CONCAT], @@ -141,6 +141,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option LIST_WALK_BACKWARDS => list_walk_backwards, LIST_WALK_UNTIL => list_walk_until, LIST_SORT_WITH => list_sort_with, + LIST_SORT_ASC => list_sort_asc, LIST_ANY => list_any, LIST_ALL => list_all, LIST_FIND => list_find, @@ -3418,6 +3419,37 @@ fn list_sort_with(symbol: Symbol, var_store: &mut VarStore) -> Def { lowlevel_2(symbol, LowLevel::ListSortWith, var_store) } +/// List.sortAsc : List (Num a) -> List (Num a) +fn list_sort_asc(symbol: Symbol, var_store: &mut VarStore) -> Def { + let list_var = var_store.fresh(); + let closure_var = var_store.fresh(); + let ret_var = list_var; + + let function = ( + var_store.fresh(), + Loc::at_zero(Expr::Var(Symbol::LIST_SORT_WITH)), + var_store.fresh(), + ret_var, + ); + + let body = Expr::Call( + Box::new(function), + vec![ + (list_var, Loc::at_zero(Var(Symbol::ARG_1))), + (closure_var, Loc::at_zero(Var(Symbol::NUM_COMPARE))), + ], + CalledVia::Space, + ); + + defn( + symbol, + vec![(list_var, Symbol::ARG_1)], + var_store, + body, + ret_var, + ) +} + /// List.any: List elem, (elem -> Bool) -> Bool fn list_any(symbol: Symbol, var_store: &mut VarStore) -> Def { lowlevel_2(symbol, LowLevel::ListAny, var_store) diff --git a/compiler/module/src/symbol.rs b/compiler/module/src/symbol.rs index 2110083ec4..2f436f5092 100644 --- a/compiler/module/src/symbol.rs +++ b/compiler/module/src/symbol.rs @@ -1110,6 +1110,7 @@ define_builtins! { 52 LIST_ALL: "all" 53 LIST_DROP_IF: "dropIf" 54 LIST_DROP_IF_PREDICATE: "#dropIfPred" + 55 LIST_SORT_ASC: "sortAsc" } 5 RESULT: "Result" => { 0 RESULT_RESULT: "Result" imported // the Result.Result type alias diff --git a/compiler/test_gen/src/gen_list.rs b/compiler/test_gen/src/gen_list.rs index b9db2a0c0b..25985526d2 100644 --- a/compiler/test_gen/src/gen_list.rs +++ b/compiler/test_gen/src/gen_list.rs @@ -2421,6 +2421,17 @@ fn list_sort_with() { ); } +#[test] +#[cfg(any(feature = "gen-llvm"))] +fn list_sort_asc() { + assert_evals_to!("List.sortAsc []", RocList::from_slice(&[]), RocList); + assert_evals_to!( + "List.sortAsc [ 4,3,2,1 ]", + RocList::from_slice(&[1, 2, 3, 4]), + RocList + ); +} + #[test] #[cfg(any(feature = "gen-llvm"))] fn list_any() {