Re-enable List.get

This commit is contained in:
Richard Feldman 2020-04-28 19:33:10 -04:00
parent 357cd3bd70
commit 353377c29e

View file

@ -26,115 +26,90 @@ use roc_types::subs::{VarStore, Variable};
/// lookup (if the bounds check passed). That internal function is hardcoded in code gen, /// lookup (if the bounds check passed). That internal function is hardcoded in code gen,
/// which works fine because it doesn't involve any open tag unions. /// which works fine because it doesn't involve any open tag unions.
pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> { pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
vec![/*list_get(var_store),*/ list_first(var_store)] vec![list_get(var_store), list_first(var_store)]
} }
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]* /// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
// fn list_get(var_store: &VarStore) -> Def { fn list_get(var_store: &VarStore) -> Def {
// use crate::expr::Expr::*; use crate::expr::Expr::*;
// use crate::pattern::Pattern::*;
// let args = vec![ defn(
// ( Symbol::LIST_GET,
// var_store.fresh(), vec![Symbol::LIST_GET_ARG_LIST, Symbol::LIST_GET_ARG_INDEX],
// no_region(Identifier(Symbol::LIST_GET_ARG_LIST)), var_store,
// ), // Perform a bounds check. If it passes, delegate to List.#getUnsafe
// ( If {
// var_store.fresh(), cond_var: var_store.fresh(),
// no_region(Identifier(Symbol::LIST_GET_ARG_INDEX)), branch_var: var_store.fresh(),
// ), branches: vec![(
// ]; // if-condition
no_region(
// // Perform a bounds check. If it passes, delegate to List.#getUnsafe // index < List.len list
// let body = If { call(
// cond_var: var_store.fresh(), Symbol::NUM_LT,
// branch_var: var_store.fresh(), vec![
// branches: vec![( Var(Symbol::LIST_GET_ARG_INDEX),
// // if-condition call(
// no_region( Symbol::LIST_LEN,
// // index < List.len list vec![Var(Symbol::LIST_GET_ARG_LIST)],
// call( var_store,
// Symbol::NUM_LT, ),
// vec![ ],
// Var(Symbol::LIST_GET_ARG_INDEX), var_store,
// call( ),
// Symbol::LIST_LEN, ),
// vec![Var(Symbol::LIST_GET_ARG_LIST)], // then-branch
// var_store, no_region(
// ), // Ok
// ], tag(
// var_store, "Ok",
// ), vec![
// ), // List.getUnsafe list index
// // then-branch Call(
// no_region( Box::new((
// // Ok var_store.fresh(),
// tag( no_region(Var(Symbol::LIST_GET_UNSAFE)),
// "Ok", var_store.fresh(),
// vec![ )),
// // List.getUnsafe list index vec![
// Call( (var_store.fresh(), no_region(Var(Symbol::LIST_GET_ARG_LIST))),
// Box::new(( (
// var_store.fresh(), var_store.fresh(),
// no_region(Var(Symbol::LIST_GET_UNSAFE)), no_region(Var(Symbol::LIST_GET_ARG_INDEX)),
// var_store.fresh(), ),
// )), ],
// vec![ CalledVia::Space,
// (var_store.fresh(), no_region(Var(Symbol::LIST_GET_ARG_LIST))), ),
// ( ],
// var_store.fresh(), var_store,
// no_region(Var(Symbol::LIST_GET_ARG_INDEX)), ),
// ), ),
// ], )],
// CalledVia::Space, final_else: Box::new(
// ), // else-branch
// ], no_region(
// var_store, // Err
// ), tag(
// ), "Err",
// )], vec![tag("OutOfBounds", Vec::new(), var_store)],
// final_else: Box::new( var_store,
// // else-branch ),
// no_region( ),
// // Err ),
// tag( },
// "Err", )
// vec![tag("OutOfBounds", Vec::new(), var_store)], }
// var_store,
// ),
// ),
// ),
// };
// let expr = Closure(
// var_store.fresh(),
// Symbol::LIST_GET,
// Recursive::NotRecursive,
// args,
// Box::new((no_region(body), var_store.fresh())),
// );
// Def {
// loc_pattern: no_region(Identifier(Symbol::LIST_GET)),
// loc_expr: no_region(expr),
// expr_var: var_store.fresh(),
// pattern_vars: SendMap::default(),
// annotation: None,
// }
// }
/// List.first : List elem -> Result elem [ ListWasEmpty ]* /// List.first : List elem -> Result elem [ ListWasEmpty ]*
fn list_first(var_store: &VarStore) -> Def { fn list_first(var_store: &VarStore) -> Def {
use crate::expr::Expr::*; use crate::expr::Expr::*;
use crate::pattern::Pattern::*;
let args = vec![(
var_store.fresh(),
no_region(Identifier(Symbol::LIST_FIRST_ARG)),
)];
defn(
Symbol::LIST_FIRST,
vec![Symbol::LIST_FIRST_ARG],
var_store,
// Perform a bounds check. If it passes, delegate to List.getUnsafe. // Perform a bounds check. If it passes, delegate to List.getUnsafe.
let body = If { If {
// TODO Use "when" instead of "if" so that we can have False be the first branch. // TODO Use "when" instead of "if" so that we can have False be the first branch.
// We want that for branch prediction; usually we expect the list to be nonempty. // We want that for branch prediction; usually we expect the list to be nonempty.
cond_var: var_store.fresh(), cond_var: var_store.fresh(),
@ -177,23 +152,8 @@ fn list_first(var_store: &VarStore) -> Def {
), ),
), ),
), ),
}; },
)
let expr = Closure(
var_store.fresh(),
Symbol::LIST_FIRST,
Recursive::NotRecursive,
args,
Box::new((no_region(body), var_store.fresh())),
);
Def {
loc_pattern: no_region(Identifier(Symbol::LIST_FIRST)),
loc_expr: no_region(expr),
expr_var: var_store.fresh(),
pattern_vars: SendMap::default(),
annotation: None,
}
} }
#[inline(always)] #[inline(always)]
@ -231,3 +191,30 @@ fn call(symbol: Symbol, args: Vec<Expr>, var_store: &VarStore) -> Expr {
CalledVia::Space, CalledVia::Space,
) )
} }
#[inline(always)]
fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) -> Def {
use crate::expr::Expr::*;
use crate::pattern::Pattern::*;
let closure_args = args
.into_iter()
.map(|symbol| (var_store.fresh(), no_region(Identifier(symbol))))
.collect();
let expr = Closure(
var_store.fresh(),
fn_name,
Recursive::NotRecursive,
closure_args,
Box::new((no_region(body), var_store.fresh())),
);
Def {
loc_pattern: no_region(Identifier(fn_name)),
loc_expr: no_region(expr),
expr_var: var_store.fresh(),
pattern_vars: SendMap::default(),
annotation: None,
}
}