Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sort ability #6615

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions crates/compiler/builtins/roc/List.roc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ module [
walkBackwardsUntil,
countIf,
chunksOf,
Sort,
compare,
]

import Bool exposing [Bool, Eq]
Expand Down Expand Up @@ -1324,3 +1326,10 @@ iterBackwardsHelp = \list, state, f, prevIndex ->
Break b -> Break b
else
Continue state

Sort implements
compare : a, a -> [LessThan, Equal, GreaterThan] where a implements Sort

# INTERNAL COMPILER USE ONLY: used to lower calls to `compare` to structural
# compare via the `Sort` low-level for derived types.
structuralCompare : a, a -> [LessThan, Equal, GreaterThan]
1 change: 1 addition & 0 deletions crates/compiler/builtins/roc/main.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package [
List,
Dict,
Set,
Sort
Decode,
Encode,
Hash,
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/can/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ map_symbol_to_lowlevel_and_arity! {
And; BOOL_AND; 2,
Or; BOOL_OR; 2,
Not; BOOL_NOT; 1,
Compare; LIST_STRUCTURAL_COMPARE; 2,
BoxExpr; BOX_BOX_FUNCTION; 1,
UnboxExpr; BOX_UNBOX; 1,
Unreachable; LIST_UNREACHABLE; 1,
Expand Down
16 changes: 16 additions & 0 deletions crates/compiler/derive_key/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub enum DeriveBuiltin {
Decoder,
Hash,
IsEq,
Compare,
ToInspector,
}

Expand All @@ -93,6 +94,7 @@ impl TryFrom<Symbol> for DeriveBuiltin {
Symbol::DECODE_DECODER => Ok(DeriveBuiltin::Decoder),
Symbol::HASH_HASH => Ok(DeriveBuiltin::Hash),
Symbol::BOOL_IS_EQ => Ok(DeriveBuiltin::IsEq),
Symbol::LIST_COMPARE => Ok(DeriveBuiltin::Compare),
Symbol::INSPECT_TO_INSPECTOR => Ok(DeriveBuiltin::ToInspector),
_ => Err(value),
}
Expand Down Expand Up @@ -127,6 +129,13 @@ impl Derived {
Symbol::BOOL_STRUCTURAL_EQ,
))
}
DeriveBuiltin::Compare => {
// If obligation checking passes, we always lower derived implementations of
// `compare` to the `Compare` low-level, to be fulfilled by the backends.
Ok(Derived::SingleLambdaSetImmediate(
Symbol::LIST_STRUCTURAL_COMPARE,
))
}
DeriveBuiltin::ToInspector => match FlatInspectable::from_var(subs, var) {
FlatInspectable::Immediate(imm) => Ok(Derived::Immediate(imm)),
FlatInspectable::Key(repr) => Ok(Derived::Key(DeriveKey::ToInspector(repr))),
Expand Down Expand Up @@ -161,6 +170,13 @@ impl Derived {
Symbol::BOOL_STRUCTURAL_EQ,
))
}
DeriveBuiltin::Compare => {
// If obligation checking passes, we always lower derived implementations of
// `compare` to the `Compare` low-level, to be fulfilled by the backends.
Ok(Derived::SingleLambdaSetImmediate(
Symbol::LIST_STRUCTURAL_COMPARE,
))
}
DeriveBuiltin::ToInspector => {
match inspect::FlatInspectable::from_builtin_alias(symbol).unwrap() {
FlatInspectable::Immediate(imm) => Ok(Derived::Immediate(imm)),
Expand Down
15 changes: 15 additions & 0 deletions crates/compiler/gen_llvm/src/llvm/lowlevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use crate::llvm::{
LLVM_SUB_WITH_OVERFLOW,
},
refcounting::PointerToRefcount,
sort::generic_compare,
};

use super::{build::Env, convert::zig_dec_type};
Expand Down Expand Up @@ -1269,6 +1270,20 @@ pub(crate) fn run_low_level<'a, 'ctx>(
let bool_val = env.builder.new_build_not(arg.into_int_value(), "bool_not");
BasicValueEnum::IntValue(bool_val)
}
Compare => {
// Sort.compare : elem, elem -> [LessThan, Equal, GreaterThan]
arguments_with_layouts!((lhs_arg, lhs_layout), (rhs_arg, rhs_layout));

generic_compare(
env,
layout_interner,
layout_ids,
lhs_arg,
rhs_arg,
lhs_layout,
rhs_layout,
)
}
Hash => {
unimplemented!()
}
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/gen_llvm/src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod externs;
mod intrinsics;
mod lowlevel;
pub mod refcounting;
pub mod sort;

mod align;
mod erased;
Expand Down
Loading