Skip to content

Commit

Permalink
Add StructuralSort skeleton
Browse files Browse the repository at this point in the history
This is modeling Compare after Eq/NotEq, which I'm not entirely sure
makes sense. Do we want to implement Sort for arbitrary types like
records and lists? I'm going to get folks' thoughts on Zulip.
  • Loading branch information
jwoudenberg committed May 6, 2024
1 parent 87b99fd commit b72c407
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/compiler/builtins/roc/List.roc
Original file line number Diff line number Diff line change
Expand Up @@ -1329,3 +1329,7 @@ iterBackwardsHelp = \list, state, f, prevIndex ->

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/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
3 changes: 3 additions & 0 deletions crates/compiler/gen_llvm/src/llvm/lowlevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,9 @@ 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 => {
panic!("TODO: implement this")
}
Hash => {
unimplemented!()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/compiler/gen_wasm/src/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,10 @@ impl<'a> LowLevelCall<'a> {

Eq | NotEq => self.eq_or_neq(backend),

Compare => {
panic!("TODO: implement this")
}

BoxExpr | UnboxExpr => {
unreachable!("The {:?} operation is turned into mono Expr", self.lowlevel)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/compiler/module/src/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub enum LowLevel {
And,
Or,
Not,
Compare,
Hash,
PtrCast,
PtrStore,
Expand Down Expand Up @@ -352,6 +353,7 @@ map_symbol_to_lowlevel! {
And <= BOOL_AND;
Or <= BOOL_OR;
Not <= BOOL_NOT;
Compare <= LIST_STRUCTURAL_COMPARE;
Unreachable <= LIST_UNREACHABLE;
DictPseudoSeed <= DICT_PSEUDO_SEED;
}
3 changes: 2 additions & 1 deletion crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Symbol {
self,
// The `structuralEq` call used deriving structural equality, which will wrap the `Eq`
// low-level implementation.
&Self::BOOL_STRUCTURAL_EQ
&Self::BOOL_STRUCTURAL_EQ | &Self::LIST_STRUCTURAL_COMPARE
)
}

Expand Down Expand Up @@ -1743,6 +1743,7 @@ define_builtins! {
16 SORT: "Sort" => {
0 LIST_SORT: "Sort"
1 LIST_COMPARE: "compare"
unexposed 2 LIST_STRUCTURAL_COMPARE: "structuralCompare"
}

num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro)
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/mono/src/drop_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC {
| ListReleaseExcessCapacity
| StrReleaseExcessCapacity => RC::Rc,

Eq | NotEq => RC::NoRc,
Eq | NotEq | Compare => RC::NoRc,

And | Or | NumAdd | NumAddWrap | NumAddChecked | NumAddSaturated | NumSub | NumSubWrap
| NumSubChecked | NumSubSaturated | NumMul | NumMulWrap | NumMulSaturated
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/mono/src/inc_dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] {
ListReleaseExcessCapacity => &[OWNED],
StrReleaseExcessCapacity => &[OWNED],

Eq | NotEq => &[BORROWED, BORROWED],
Eq | NotEq | Compare => &[BORROWED, BORROWED],

And | Or | NumAdd | NumAddWrap | NumAddChecked | NumAddSaturated | NumSub | NumSubWrap
| NumSubChecked | NumSubSaturated | NumMul | NumMulWrap | NumMulSaturated
Expand Down

0 comments on commit b72c407

Please sign in to comment.