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

Adds List.last #816

Merged
merged 2 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cli/tests/repl_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ mod repl_eval {
expect_success("List.sum [ 1.1, 2.2, 3.3 ]", "6.6 : F64");
}

// TODO add test cases for empty lists once error messages in the repl are correct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure enough! I've filed a bug for this: #818

#[test]
fn list_first() {
expect_success(
"List.first [ 12, 9, 6, 3 ]",
"Ok 12 : Result (Num *) [ ListWasEmpty ]*",
);
}

#[test]
fn list_last() {
expect_success(
"List.last [ 12, 9, 6, 3 ]",
"Ok 3 : Result (Num *) [ ListWasEmpty ]*",
);
}

#[test]
fn empty_record() {
expect_success("{}", "{} : {}");
Expand Down
9 changes: 9 additions & 0 deletions compiler/builtins/src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {

add_type(
Symbol::LIST_FIRST,
top_level_function(
vec![list_type(flex(TVAR1))],
Box::new(result_type(flex(TVAR1), list_was_empty.clone())),
),
);

// last : List elem -> Result elem [ ListWasEmpty ]*
add_type(
Symbol::LIST_LAST,
top_level_function(
vec![list_type(flex(TVAR1))],
Box::new(result_type(flex(TVAR1), list_was_empty)),
Expand Down
32 changes: 26 additions & 6 deletions compiler/builtins/src/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,16 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
unique_function(vec![list_type(star1, a)], int_type(star2))
});

fn list_was_empty() -> SolvedType {
SolvedType::TagUnion(
vec![(TagName::Global("ListWasEmpty".into()), vec![])],
Box::new(SolvedType::Wildcard),
)
}

// List.first :
// Attr (* | u) (List (Attr u a)),
// -> Attr * (Result (Attr u a) (Attr * [ OutOfBounds ]*))
let list_was_empty = SolvedType::TagUnion(
vec![(TagName::Global("ListWasEmpty".into()), vec![])],
Box::new(SolvedType::Wildcard),
);

add_type(Symbol::LIST_FIRST, {
let_tvars! { a, u, star1, star2, star3 };

Expand All @@ -470,7 +472,25 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
],
)],
result_type(star2, attr_type(u, a), lift(star3, list_was_empty)),
result_type(star2, attr_type(u, a), lift(star3, list_was_empty())),
)
});

// List.last :
// Attr (* | u) (List (Attr u a)),
// -> Attr * (Result (Attr u a) (Attr * [ OutOfBounds ]*))
add_type(Symbol::LIST_LAST, {
let_tvars! { a, u, star1, star2, star3 };

unique_function(
vec![SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![
container(star1, vec![u]),
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
],
)],
result_type(star2, attr_type(u, a), lift(star3, list_was_empty())),
)
});

Expand Down
101 changes: 101 additions & 0 deletions compiler/can/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
LIST_SET => list_set,
LIST_APPEND => list_append,
LIST_FIRST => list_first,
LIST_LAST => list_last,
LIST_IS_EMPTY => list_is_empty,
LIST_SINGLE => list_single,
LIST_REPEAT => list_repeat,
Expand Down Expand Up @@ -152,6 +153,7 @@ pub fn builtin_defs(var_store: &mut VarStore) -> MutMap<Symbol, Def> {
Symbol::LIST_SET => list_set,
Symbol::LIST_APPEND => list_append,
Symbol::LIST_FIRST => list_first,
Symbol::LIST_LAST => list_last,
Symbol::LIST_IS_EMPTY => list_is_empty,
Symbol::LIST_SINGLE => list_single,
Symbol::LIST_REPEAT => list_repeat,
Expand Down Expand Up @@ -1897,6 +1899,105 @@ fn list_first(symbol: Symbol, var_store: &mut VarStore) -> Def {
)
}

/// List.last : List elem -> Result elem [ ListWasEmpty ]*
///
/// List.last :
/// Attr (* | u) (List (Attr u a)),
/// -> Attr * (Result (Attr u a) (Attr * [ OutOfBounds ]*))
fn list_last(symbol: Symbol, var_store: &mut VarStore) -> Def {
let arg_var = var_store.fresh();
let bool_var = var_store.fresh();
let list_var = var_store.fresh();
let len_var = var_store.fresh();
let num_var = var_store.fresh();
let list_elem_var = var_store.fresh();
let ret_var = var_store.fresh();

// Perform a bounds check. If it passes, delegate to List.getUnsafe.
let body = If {
cond_var: bool_var,
branch_var: var_store.fresh(),
branches: vec![(
// if-condition
no_region(
// List.len list != 0
RunLowLevel {
op: LowLevel::NotEq,
args: vec![
(len_var, Int(num_var, 0)),
(
len_var,
RunLowLevel {
op: LowLevel::ListLen,
args: vec![(list_var, Var(Symbol::ARG_1))],
ret_var: len_var,
},
),
],
ret_var: bool_var,
},
),
// list was not empty
no_region(
// Ok (List.getUnsafe list (Num.sub (List.len list) 1))
tag(
"Ok",
vec![
// List.getUnsafe list (Num.sub (List.len list) 1)
RunLowLevel {
op: LowLevel::ListGetUnsafe,
args: vec![
(list_var, Var(Symbol::ARG_1)),
(
len_var,
// Num.sub (List.len list) 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's cool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right approach for now, but actually we should use subWrap from #664 (once that's been implemented), because Num.sub should do overflow checking, and there's no chance of overflow yet.

(I say "should do" because currently only Num.add actually does it...maybe that's a good next project, actually!)

RunLowLevel {
op: LowLevel::NumSub,
args: vec![
(
arg_var,
// List.len list
RunLowLevel {
op: LowLevel::ListLen,
args: vec![(list_var, Var(Symbol::ARG_1))],
ret_var: len_var,
},
),
(arg_var, Int(num_var, 1)),
],
ret_var: len_var,
},
),
],
ret_var: list_elem_var,
},
],
var_store,
),
),
)],
final_else: Box::new(
// list was empty
no_region(
// Err ListWasEmpty
tag(
"Err",
vec![tag("ListWasEmpty", Vec::new(), var_store)],
var_store,
),
),
),
};

defn(
symbol,
vec![(list_var, Symbol::ARG_1)],
var_store,
body,
ret_var,
)
}

#[inline(always)]
fn no_region<T>(value: T) -> Located<T> {
Located {
Expand Down
46 changes: 46 additions & 0 deletions compiler/gen/tests/gen_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,52 @@ mod gen_list {
);
}

#[test]
fn last_int_list() {
assert_evals_to!(
indoc!(
r#"
when List.last [ 12, 9, 6, 3 ] is
Ok val -> val
Err _ -> -1
"#
),
3,
i64
);
}

#[test]
#[ignore]
fn last_wildcard_empty_list() {
assert_evals_to!(
indoc!(
r#"
when List.last [] is
Ok _ -> 5
Err _ -> -1
"#
),
-1,
i64
);
}

#[test]
fn last_empty_list() {
assert_evals_to!(
indoc!(
r#"
when List.last [] is
Ok val -> val
Err _ -> -1
"#
),
-1,
i64
);
}

#[test]
fn get_empty_list() {
assert_evals_to!(
Expand Down
1 change: 1 addition & 0 deletions compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ define_builtins! {
17 LIST_CONTAINS: "contains"
18 LIST_SUM: "sum"
19 LIST_WALK: "walk"
20 LIST_LAST: "last"
}
5 RESULT: "Result" => {
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
Expand Down