add ListIsUnique lowlevel operation

This commit is contained in:
Folkert 2022-05-06 13:56:35 +02:00
parent fcfe66f84d
commit 5755475b3a
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
9 changed files with 29 additions and 0 deletions

View file

@ -1355,3 +1355,9 @@ pub fn listFindUnsafe(
return .{ .value = null, .found = false }; return .{ .value = null, .found = false };
} }
} }
pub fn listIsUnique(
list: RocList,
) callconv(.C) bool {
return list.isEmpty() or list.isUnique();
}

View file

@ -56,6 +56,7 @@ comptime {
exportListFn(list.listAny, "any"); exportListFn(list.listAny, "any");
exportListFn(list.listAll, "all"); exportListFn(list.listAll, "all");
exportListFn(list.listFindUnsafe, "find_unsafe"); exportListFn(list.listFindUnsafe, "find_unsafe");
exportListFn(list.listIsUnique, "is_unique");
} }
// Dict Module // Dict Module

View file

@ -345,6 +345,7 @@ pub const LIST_REPLACE_IN_PLACE: &str = "roc_builtins.list.replace_in_place";
pub const LIST_ANY: &str = "roc_builtins.list.any"; pub const LIST_ANY: &str = "roc_builtins.list.any";
pub const LIST_ALL: &str = "roc_builtins.list.all"; pub const LIST_ALL: &str = "roc_builtins.list.all";
pub const LIST_FIND_UNSAFE: &str = "roc_builtins.list.find_unsafe"; pub const LIST_FIND_UNSAFE: &str = "roc_builtins.list.find_unsafe";
pub const LIST_IS_UNIQUE: &str = "roc_builtins.list.is_unique";
pub const DEC_FROM_STR: &str = "roc_builtins.dec.from_str"; pub const DEC_FROM_STR: &str = "roc_builtins.dec.from_str";
pub const DEC_FROM_F64: &str = "roc_builtins.dec.from_f64"; pub const DEC_FROM_F64: &str = "roc_builtins.dec.from_f64";

View file

@ -150,6 +150,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
LIST_ANY => list_any, LIST_ANY => list_any,
LIST_ALL => list_all, LIST_ALL => list_all,
LIST_FIND => list_find, LIST_FIND => list_find,
LIST_IS_UNIQUE => list_is_unique,
DICT_LEN => dict_len, DICT_LEN => dict_len,
DICT_EMPTY => dict_empty, DICT_EMPTY => dict_empty,
DICT_SINGLE => dict_single, DICT_SINGLE => dict_single,
@ -3773,6 +3774,11 @@ fn list_find(symbol: Symbol, var_store: &mut VarStore) -> Def {
) )
} }
/// List.isUnique : List * -> Bool
fn list_is_unique(symbol: Symbol, var_store: &mut VarStore) -> Def {
lowlevel_1(symbol, LowLevel::ListIsUnique, var_store)
}
/// Dict.len : Dict * * -> Nat /// Dict.len : Dict * * -> Nat
fn dict_len(symbol: Symbol, var_store: &mut VarStore) -> Def { fn dict_len(symbol: Symbol, var_store: &mut VarStore) -> Def {
let arg1_var = var_store.fresh(); let arg1_var = var_store.fresh();

View file

@ -5667,6 +5667,15 @@ fn run_low_level<'a, 'ctx, 'env>(
update_mode, update_mode,
) )
} }
ListIsUnique => {
// List.isUnique : List a -> Bool
debug_assert_eq!(args.len(), 1);
let list = load_symbol(scope, &args[0]);
let list = list_to_c_abi(env, list).into();
call_bitcode_fn(env, &[list], bitcode::LIST_IS_UNIQUE)
}
NumToStr => { NumToStr => {
// Num.toStr : Num a -> Str // Num.toStr : Num a -> Str
debug_assert_eq!(args.len(), 1); debug_assert_eq!(args.len(), 1);

View file

@ -277,6 +277,8 @@ impl<'a> LowLevelCall<'a> {
_ => internal_error!("invalid storage for List"), _ => internal_error!("invalid storage for List"),
}, },
ListIsUnique => self.load_args_and_call_zig(backend, bitcode::LIST_IS_UNIQUE),
ListMap | ListMap2 | ListMap3 | ListMap4 | ListMapWithIndex | ListKeepIf | ListWalk ListMap | ListMap2 | ListMap3 | ListMap4 | ListMapWithIndex | ListKeepIf | ListWalk
| ListWalkUntil | ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith | ListWalkUntil | ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith
| ListAny | ListAll | ListFindUnsafe | DictWalk => { | ListAny | ListAll | ListFindUnsafe | DictWalk => {

View file

@ -53,6 +53,7 @@ pub enum LowLevel {
ListAny, ListAny,
ListAll, ListAll,
ListFindUnsafe, ListFindUnsafe,
ListIsUnique,
DictSize, DictSize,
DictEmpty, DictEmpty,
DictInsert, DictInsert,

View file

@ -1252,6 +1252,7 @@ define_builtins! {
55 LIST_SORT_DESC: "sortDesc" 55 LIST_SORT_DESC: "sortDesc"
56 LIST_SORT_DESC_COMPARE: "#sortDescCompare" 56 LIST_SORT_DESC_COMPARE: "#sortDescCompare"
57 LIST_REPLACE: "replace" 57 LIST_REPLACE: "replace"
58 LIST_IS_UNIQUE: "#isUnique"
} }
5 RESULT: "Result" => { 5 RESULT: "Result" => {
0 RESULT_RESULT: "Result" // the Result.Result type alias 0 RESULT_RESULT: "Result" // the Result.Result type alias

View file

@ -1031,6 +1031,8 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
ExpectTrue => arena.alloc_slice_copy(&[irrelevant]), ExpectTrue => arena.alloc_slice_copy(&[irrelevant]),
ListIsUnique => arena.alloc_slice_copy(&[borrowed]),
BoxExpr | UnboxExpr => { BoxExpr | UnboxExpr => {
unreachable!("These lowlevel operations are turned into mono Expr's") unreachable!("These lowlevel operations are turned into mono Expr's")
} }