Merge branch 'list-push' of github.com:rtfeldman/roc into list-push

This commit is contained in:
Chad Stearns 2020-06-27 15:12:01 -04:00
commit 20b0e986ad
8 changed files with 784 additions and 498 deletions

View file

@ -498,7 +498,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
),
);
// push : List a -> a -> List a
// push : List elem -> elem -> List elem
add_type(
Symbol::LIST_PUSH,
SolvedType::Func(
@ -513,6 +513,15 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
SolvedType::Func(vec![flex(TVAR1)], Box::new(list_type(flex(TVAR1)))),
);
// repeat : Int, elem -> List elem
add_type(
Symbol::LIST_REPEAT,
SolvedType::Func(
vec![int_type(), flex(TVAR1)],
Box::new(list_type(flex(TVAR1))),
),
);
// len : List * -> Int
add_type(
Symbol::LIST_LEN,

File diff suppressed because it is too large Load diff

View file

@ -433,11 +433,7 @@ pub fn build_expr<'a, 'ctx, 'env>(
let builder = env.builder;
if elems.is_empty() {
let struct_type = collection(ctx, env.ptr_bytes);
// The pointer should be null (aka zero) and the length should be zero,
// so the whole struct should be a const_zero
BasicValueEnum::StructValue(struct_type.const_zero())
empty_list(env)
} else {
let len_u64 = elems.len() as u64;
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
@ -1471,6 +1467,131 @@ fn call_with_args<'a, 'ctx, 'env>(
"cast_collection",
)
}
Symbol::LIST_REPEAT => {
// List.repeat : Int, elem -> List elem
debug_assert!(args.len() == 2);
// Number of repeats
let list_len = args[0].0.into_int_value();
let builder = env.builder;
let ctx = env.context;
let (elem, elem_layout) = args[1];
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
// list_len > 0
// We have to do a loop below, continuously adding the `elem`
// to the output list `List elem` until we have reached the
// number of repeats. This `comparison` is used to check
// if we need to do any looping; because if we dont, then we
// dont need to allocate memory for the index or the check
// if index != 0
let comparison = builder.build_int_compare(
IntPredicate::UGT,
list_len,
ctx.i64_type().const_int(0, false),
"atleastzero",
);
let build_then = || {
// Allocate space for the new array that we'll copy into.
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
let list_ptr = {
let bytes_len = elem_bytes;
let len_type = env.ptr_int();
let len = len_type.const_int(bytes_len, false);
env.builder
.build_array_malloc(elem_type, len, "create_list_ptr")
.unwrap()
// TODO check if malloc returned null; if so, runtime error for OOM!
};
let index_name = "#index";
let start_alloca = builder.build_alloca(ctx.i64_type(), index_name);
builder.build_store(start_alloca, list_len);
let loop_bb = ctx.append_basic_block(parent, "loop");
builder.build_unconditional_branch(loop_bb);
builder.position_at_end(loop_bb);
// #index = #index - 1
let curr_index = builder
.build_load(start_alloca, index_name)
.into_int_value();
let next_index = builder.build_int_sub(
curr_index,
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(start_alloca, next_index);
let elem_ptr =
unsafe { builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index") };
// Mutate the new array in-place to change the element.
builder.build_store(elem_ptr, elem);
// #index != 0
let end_cond = builder.build_int_compare(
IntPredicate::NE,
ctx.i64_type().const_int(0, false),
curr_index,
"loopcond",
);
let after_bb = ctx.append_basic_block(parent, "afterloop");
builder.build_conditional_branch(end_cond, loop_bb, after_bb);
builder.position_at_end(after_bb);
let ptr_bytes = env.ptr_bytes;
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(list_ptr, int_type, "list_cast_ptr");
let struct_type = collection(ctx, ptr_bytes);
let mut struct_val;
// Store the pointer
struct_val = builder
.build_insert_value(
struct_type.get_undef(),
ptr_as_int,
Builtin::WRAPPER_PTR,
"insert_ptr",
)
.unwrap();
// Store the length
struct_val = builder
.build_insert_value(struct_val, list_len, Builtin::WRAPPER_LEN, "insert_len")
.unwrap();
builder.build_bitcast(
struct_val.into_struct_value(),
collection(ctx, ptr_bytes),
"cast_collection",
)
};
let build_else = || empty_list(env);
let struct_type = collection(ctx, env.ptr_bytes);
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
)
}
Symbol::INT_DIV_UNSAFE => {
debug_assert!(args.len() == 2);
@ -1637,6 +1758,16 @@ enum InPlace {
Clone,
}
fn empty_list<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValueEnum<'ctx> {
let ctx = env.context;
let struct_type = collection(ctx, env.ptr_bytes);
// The pointer should be null (aka zero) and the length should be zero,
// so the whole struct should be a const_zero
BasicValueEnum::StructValue(struct_type.const_zero())
}
fn bounds_check_comparison<'ctx>(
builder: &Builder<'ctx>,
elem_index: IntValue<'ctx>,

View file

@ -507,6 +507,15 @@ mod gen_builtins {
assert_evals_to!("List.single 5.6", &[5.6], &'static [f64]);
}
#[test]
fn list_repeat() {
assert_evals_to!("List.repeat 5 1", &[1, 1, 1, 1, 1], &'static [i64]);
assert_evals_to!("List.repeat 4 2", &[2, 2, 2, 2], &'static [i64]);
assert_evals_to!("List.repeat 0 []", &[], &'static [i64]);
assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]);
}
#[test]
fn empty_list_len() {
with_larger_debug_stack(|| {

View file

@ -242,8 +242,8 @@ mod test_uniq_load {
loaded_module,
hashmap! {
"swap" => "Attr * (Attr Shared Int, Attr Shared Int, Attr * (List (Attr Shared a)) -> Attr * (List (Attr Shared a)))",
"partition" => "Attr * (Attr Shared Int, Attr Shared Int, Attr b (List (Attr Shared (Num (Attr c a)))) -> Attr * [ Pair (Attr * Int) (Attr b (List (Attr Shared (Num (Attr c a))))) ])",
"quicksort" => "Attr Shared (Attr b (List (Attr Shared (Num (Attr c a)))), Attr Shared Int, Attr Shared Int -> Attr b (List (Attr Shared (Num (Attr c a)))))",
"partition" => "Attr * (Attr Shared Int, Attr Shared Int, Attr b (List (Attr Shared (Num (Attr Shared a)))) -> Attr * [ Pair (Attr Shared Int) (Attr b (List (Attr Shared (Num (Attr Shared a))))) ])",
"quicksort" => "Attr Shared (Attr b (List (Attr Shared (Num (Attr Shared a)))), Attr Shared Int, Attr Shared Int -> Attr b (List (Attr Shared (Num (Attr Shared a)))))",
},
);
});

View file

@ -672,6 +672,7 @@ define_builtins! {
0 STR_STR: "Str" imported // the Str.Str type alias
1 STR_AT_STR: "@Str" // the Str.@Str private tag
2 STR_ISEMPTY: "isEmpty"
3 STR_APPEND: "append"
}
6 LIST: "List" => {
0 LIST_LIST: "List" imported // the List.List type alias
@ -692,6 +693,7 @@ define_builtins! {
15 LIST_FIRST: "first"
16 LIST_FIRST_ARG: "first#list"
17 LIST_SINGLE: "single"
18 LIST_REPEAT: "repeat"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" imported // the Result.Result type alias

View file

@ -1475,7 +1475,7 @@ mod test_uniq_solve {
quicksort
"#
),
"Attr Shared (Attr b (List (Attr Shared (Num (Attr c a)))), Attr Shared Int, Attr Shared Int -> Attr b (List (Attr Shared (Num (Attr c a)))))"
"Attr Shared (Attr b (List (Attr Shared (Num (Attr Shared a)))), Attr Shared Int, Attr Shared Int -> Attr b (List (Attr Shared (Num (Attr Shared a)))))"
);
})
}
@ -1885,12 +1885,12 @@ mod test_uniq_solve {
4 + 4
"#
),
"Attr * (Num (Attr * *))",
"Attr a (Num (Attr a *))",
);
}
#[test]
fn list_get() {
fn list_get_at() {
infer_eq(
indoc!(
r#"
@ -2004,7 +2004,7 @@ mod test_uniq_solve {
list
"#
),
"Attr * (Attr a (List (Attr Shared (Num (Attr b c)))) -> Attr a (List (Attr Shared (Num (Attr b c)))))",
"Attr * (Attr a (List (Attr Shared (Num (Attr Shared b)))) -> Attr a (List (Attr Shared (Num (Attr Shared b)))))",
);
}
@ -2024,19 +2024,6 @@ mod test_uniq_solve {
);
}
#[test]
fn list_set() {
infer_eq(indoc!(r#"List.set"#), "Attr * (Attr (* | a | b) (List (Attr a c)), Attr * Int, Attr (a | b) c -> Attr * (List (Attr a c)))");
}
#[test]
fn list_map() {
infer_eq(
indoc!(r#"List.map"#),
"Attr * (Attr * (List a), Attr Shared (a -> b) -> Attr * (List b))",
);
}
#[test]
fn list_map_identity() {
infer_eq(
@ -2045,14 +2032,6 @@ mod test_uniq_solve {
);
}
#[test]
fn list_foldr() {
infer_eq(
indoc!(r#"List.foldr"#),
"Attr * (Attr * (List a), Attr Shared (a, b -> b), b -> b)",
);
}
#[test]
fn list_foldr_sum() {
infer_eq(
@ -2063,15 +2042,72 @@ mod test_uniq_solve {
sum
"#
),
"Attr * (Attr * (List (Attr * (Num (Attr a b)))) -> Attr * (Num (Attr a b)))",
"Attr * (Attr (* | a) (List (Attr a (Num (Attr a b)))) -> Attr c (Num (Attr c b)))",
);
}
#[test]
fn num_add() {
infer_eq(
"Num.add",
"Attr * (Attr a (Num (Attr a b)), Attr c (Num (Attr c b)) -> Attr d (Num (Attr d b)))",
);
}
#[test]
fn list_isempty() {
infer_eq("List.isEmpty", "Attr * (Attr * (List *) -> Attr * Bool)");
}
#[test]
fn list_len() {
infer_eq("List.len", "Attr * (Attr * (List *) -> Attr * Int)");
}
#[test]
fn list_get() {
infer_eq("List.get", "Attr * (Attr (* | a) (List (Attr a b)), Attr * Int -> Attr * (Result (Attr a b) (Attr * [ OutOfBounds ]*)))");
}
#[test]
fn list_set() {
infer_eq("List.set", "Attr * (Attr (* | a | b) (List (Attr a c)), Attr * Int, Attr (a | b) c -> Attr * (List (Attr a c)))");
}
#[test]
fn list_single() {
infer_eq("List.single", "Attr * (a -> Attr * (List a))");
}
#[test]
fn list_repeat() {
infer_eq(
"List.repeat",
"Attr * (Attr * Int, Attr Shared a -> Attr * (List (Attr Shared a)))",
);
}
#[test]
fn list_push() {
infer_eq(
indoc!(r#"List.push"#),
"Attr * (Attr (* | a | b) (List (Attr a c)), Attr (a | b) c -> Attr * (List (Attr a c)))"
"List.push",
"Attr * (Attr * (List a), a -> Attr * (List a))",
);
}
#[test]
fn list_map() {
infer_eq(
"List.map",
"Attr * (Attr * (List a), Attr Shared (a -> b) -> Attr * (List b))",
);
}
#[test]
fn list_foldr() {
infer_eq(
"List.foldr",
"Attr * (Attr (* | a) (List (Attr a b)), Attr Shared (Attr a b, c -> c), c -> c)",
);
}
@ -2085,7 +2121,7 @@ mod test_uniq_solve {
singleton
"#
),
"Attr * (Attr (* | a) b -> Attr * (List (Attr a b)))",
"Attr * (a -> Attr * (List a))",
);
}
@ -2099,8 +2135,7 @@ mod test_uniq_solve {
reverse
"#
),
"Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr b c)))",
// "Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr a c)))",
"Attr * (Attr (* | a) (List (Attr a b)) -> Attr * (List (Attr a b)))",
);
}
@ -2116,6 +2151,97 @@ mod test_uniq_solve {
);
}
#[test]
fn set_empty() {
infer_eq("Set.empty", "Attr * (Set *)");
}
#[test]
fn set_singelton() {
infer_eq("Set.singleton", "Attr * (a -> Attr * (Set a))");
}
#[test]
fn set_union() {
infer_eq(
"Set.union",
"Attr * (Attr * (Set (Attr * a)), Attr * (Set (Attr * a)) -> Attr * (Set (Attr * a)))",
);
}
#[test]
fn set_diff() {
infer_eq(
"Set.diff",
"Attr * (Attr * (Set (Attr * a)), Attr * (Set (Attr * a)) -> Attr * (Set (Attr * a)))",
);
}
#[test]
fn set_foldl() {
infer_eq(
"Set.foldl",
"Attr * (Attr (* | a) (Set (Attr a b)), Attr Shared (Attr a b, c -> c), c -> c)",
);
}
#[test]
fn set_insert() {
infer_eq("Set.insert", "Attr * (Attr * (Set a), a -> Attr * (Set a))");
}
#[test]
fn set_remove() {
infer_eq(
"Set.remove",
"Attr * (Attr * (Set (Attr a b)), Attr * b -> Attr * (Set (Attr a b)))",
);
}
#[test]
fn map_empty() {
infer_eq("Map.empty", "Attr * (Map * *)");
}
#[test]
fn map_singelton() {
infer_eq("Map.singleton", "Attr * (a, b -> Attr * (Map a b))");
}
#[test]
fn map_get() {
infer_eq("Map.get", "Attr * (Attr (* | a) (Map (Attr * b) (Attr a c)), Attr * b -> Attr * (Result (Attr a c) (Attr * [ KeyNotFound ]*)))");
}
#[test]
fn map_insert() {
infer_eq(
"Map.insert",
"Attr * (Attr * (Map a b), a, b -> Attr * (Map a b))",
);
}
#[test]
fn str_is_empty() {
infer_eq("Str.isEmpty", "Attr * (Attr * Str -> Attr * Bool)");
}
#[test]
fn str_append() {
infer_eq(
"Str.append",
"Attr * (Attr * Str, Attr * Str -> Attr * Str)",
);
}
#[test]
fn result_map() {
infer_eq(
"Result.map",
"Attr * (Attr * (Result a b), Attr * (a -> c) -> Attr * (Result c b))",
);
}
#[test]
fn use_correct_ext_var() {
infer_eq(
@ -2367,14 +2493,18 @@ mod test_uniq_solve {
}
#[test]
fn equals() {
fn bool_eq() {
infer_eq(
indoc!(
r#"
\a, b -> a == b
"#
),
"Attr * (a, a -> Attr * Bool)",
"\\a, b -> a == b",
"Attr * (Attr * a, Attr * a -> Attr * Bool)",
);
}
#[test]
fn bool_neq() {
infer_eq(
"\\a, b -> a != b",
"Attr * (Attr * a, Attr * a -> Attr * Bool)",
);
}
@ -2409,7 +2539,7 @@ mod test_uniq_solve {
_ -> 3
"#
),
"Attr * (Attr Shared (Num (Attr * *)) -> Attr * (Num (Attr * *)))",
"Attr * (Attr Shared (Num (Attr Shared *)) -> Attr * (Num (Attr * *)))",
);
}
}

View file

@ -129,11 +129,11 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
..
} => {
match ch {
'\u{8}' => {
// In Linux, we get one of these when you press
// backspace, but in macOS we don't. In both, we
'\u{8}' | '\u{7f}' => {
// In Linux, we get a '\u{8}' when you press backspace,
// but in macOS we get '\u{7f}'. In both, we
// get a Back keydown event. Therefore, we use the
// Back keydown event and ignore this, resulting
// Back keydown event and ignore these, resulting
// in a system that works in both Linux and macOS.
}
'\u{e000}'..='\u{f8ff}'