Skip to content

Commit 5a7b74c

Browse files
committed
Add StructuralSort skeleton
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.
1 parent 19709ab commit 5a7b74c

File tree

8 files changed

+18
-3
lines changed

8 files changed

+18
-3
lines changed

crates/compiler/builtins/roc/List.roc

+4
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,7 @@ iterBackwardsHelp = \list, state, f, prevIndex ->
13291329

13301330
Sort implements
13311331
compare : a, a -> [LessThan, Equal, GreaterThan] where a implements Sort
1332+
1333+
# INTERNAL COMPILER USE ONLY: used to lower calls to `compare` to structural
1334+
# compare via the `Sort` low-level for derived types.
1335+
structuralCompare : a, a -> [LessThan, Equal, GreaterThan]

crates/compiler/can/src/builtins.rs

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ map_symbol_to_lowlevel_and_arity! {
216216
And; BOOL_AND; 2,
217217
Or; BOOL_OR; 2,
218218
Not; BOOL_NOT; 1,
219+
Compare; LIST_STRUCTURAL_COMPARE; 2,
219220
BoxExpr; BOX_BOX_FUNCTION; 1,
220221
UnboxExpr; BOX_UNBOX; 1,
221222
Unreachable; LIST_UNREACHABLE; 1,

crates/compiler/gen_llvm/src/llvm/lowlevel.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,9 @@ pub(crate) fn run_low_level<'a, 'ctx>(
12691269
let bool_val = env.builder.new_build_not(arg.into_int_value(), "bool_not");
12701270
BasicValueEnum::IntValue(bool_val)
12711271
}
1272+
Compare => {
1273+
panic!("TODO: implement this")
1274+
}
12721275
Hash => {
12731276
unimplemented!()
12741277
}

crates/compiler/gen_wasm/src/low_level.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,10 @@ impl<'a> LowLevelCall<'a> {
20872087

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

2090+
Compare => {
2091+
panic!("TODO: implement this")
2092+
}
2093+
20902094
BoxExpr | UnboxExpr => {
20912095
unreachable!("The {:?} operation is turned into mono Expr", self.lowlevel)
20922096
}

crates/compiler/module/src/low_level.rs

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub enum LowLevel {
113113
And,
114114
Or,
115115
Not,
116+
Compare,
116117
Hash,
117118
PtrCast,
118119
PtrStore,
@@ -352,6 +353,7 @@ map_symbol_to_lowlevel! {
352353
And <= BOOL_AND;
353354
Or <= BOOL_OR;
354355
Not <= BOOL_NOT;
356+
Compare <= LIST_STRUCTURAL_COMPARE;
355357
Unreachable <= LIST_UNREACHABLE;
356358
DictPseudoSeed <= DICT_PSEUDO_SEED;
357359
}

crates/compiler/module/src/symbol.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl Symbol {
112112
self,
113113
// The `structuralEq` call used deriving structural equality, which will wrap the `Eq`
114114
// low-level implementation.
115-
&Self::BOOL_STRUCTURAL_EQ
115+
&Self::BOOL_STRUCTURAL_EQ | &Self::LIST_STRUCTURAL_COMPARE
116116
)
117117
}
118118

@@ -1739,6 +1739,7 @@ define_builtins! {
17391739
16 SORT: "Sort" => {
17401740
0 LIST_SORT: "Sort"
17411741
1 LIST_COMPARE: "compare"
1742+
unexposed 2 LIST_STRUCTURAL_COMPARE: "structuralCompare"
17421743
}
17431744

17441745
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)

crates/compiler/mono/src/drop_specialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC {
15581558
| ListReleaseExcessCapacity
15591559
| StrReleaseExcessCapacity => RC::Rc,
15601560

1561-
Eq | NotEq => RC::NoRc,
1561+
Eq | NotEq | Compare => RC::NoRc,
15621562

15631563
And | Or | NumAdd | NumAddWrap | NumAddChecked | NumAddSaturated | NumSub | NumSubWrap
15641564
| NumSubChecked | NumSubSaturated | NumMul | NumMulWrap | NumMulSaturated

crates/compiler/mono/src/inc_dec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] {
13101310
ListReleaseExcessCapacity => &[OWNED],
13111311
StrReleaseExcessCapacity => &[OWNED],
13121312

1313-
Eq | NotEq => &[BORROWED, BORROWED],
1313+
Eq | NotEq | Compare => &[BORROWED, BORROWED],
13141314

13151315
And | Or | NumAdd | NumAddWrap | NumAddChecked | NumAddSaturated | NumSub | NumSubWrap
13161316
| NumSubChecked | NumSubSaturated | NumMul | NumMulWrap | NumMulSaturated

0 commit comments

Comments
 (0)