Skip to content

Commit

Permalink
Add StructuralCompare skeleton
Browse files Browse the repository at this point in the history
I'm modeling implementation of the Sort ability for arbitrary types
after how it's done for Eq/NotEq.
  • Loading branch information
jwoudenberg committed May 18, 2024
1 parent 10b42f1 commit 37a2ac2
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 @@ -1534,6 +1534,7 @@ define_builtins! {
88 LIST_LEN_USIZE: "lenUsize"
89 LIST_SORT: "Sort" exposed_type=true
90 LIST_COMPARE: "compare"
unexposed 91 LIST_STRUCTURAL_COMPARE: "structuralCompare"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias
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 37a2ac2

Please sign in to comment.