add closure size variable

This commit is contained in:
Folkert 2020-10-02 00:50:18 +02:00
parent 0422d565ce
commit 2e1e87ad6a
21 changed files with 546 additions and 262 deletions

View file

@ -37,6 +37,7 @@ const NUM_BUILTIN_IMPORTS: usize = 7;
/// These can be shared between definitions, they will get instantiated when converted to Type
const FUVAR: VarId = VarId::from_u32(1000);
const TOP_LEVEL_CLOSURE_VAR: VarId = VarId::from_u32(1001);
fn shared(base: SolvedType) -> SolvedType {
SolvedType::Apply(
@ -831,12 +832,16 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
// , Attr Shared (a -> b)
// -> Attr * (List b)
add_type(Symbol::LIST_MAP, {
let_tvars! { a, b, star1, star2 };
let_tvars! { a, b, star1, star2, closure };
unique_function(
vec![
list_type(star1, a),
shared(SolvedType::Func(vec![flex(a)], Box::new(flex(b)))),
shared(SolvedType::Func(
vec![flex(a)],
Box::new(flex(closure)),
Box::new(flex(b)),
)),
],
list_type(star2, b),
)
@ -846,12 +851,16 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
// , Attr Shared (a -> Attr * Bool)
// -> Attr * (List a)
add_type(Symbol::LIST_KEEP_IF, {
let_tvars! { a, star1, star2, star3 };
let_tvars! { a, star1, star2, star3, closure };
unique_function(
vec![
list_type(star1, a),
shared(SolvedType::Func(vec![flex(a)], Box::new(bool_type(star2)))),
shared(SolvedType::Func(
vec![flex(a)],
Box::new(flex(closure)),
Box::new(bool_type(star2)),
)),
],
list_type(star3, a),
)
@ -862,7 +871,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
// , b
// -> b
add_type(Symbol::LIST_WALK_RIGHT, {
let_tvars! { u, a, b, star1 };
let_tvars! { u, a, b, star1, closure };
unique_function(
vec![
@ -875,6 +884,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
),
shared(SolvedType::Func(
vec![attr_type(u, a), flex(b)],
Box::new(flex(closure)),
Box::new(flex(b)),
)),
flex(b),
@ -1032,7 +1042,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
// , b
// -> b
add_type(Symbol::SET_FOLDL, {
let_tvars! { star, u, a, b };
let_tvars! { star, u, a, b, closure };
unique_function(
vec![
@ -1045,6 +1055,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
),
shared(SolvedType::Func(
vec![attr_type(u, a), flex(b)],
Box::new(flex(closure)),
Box::new(flex(b)),
)),
flex(b),
@ -1129,7 +1140,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
// , Attr * (a -> b)
// -> Attr * (Result b e)
add_type(Symbol::RESULT_MAP, {
let_tvars! { star1, star2, star3, a, b, e };
let_tvars! { star1, star2, star3, a, b, e, closure };
unique_function(
vec![
SolvedType::Apply(
@ -1143,7 +1154,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
Symbol::ATTR_ATTR,
vec![
flex(star2),
SolvedType::Func(vec![flex(a)], Box::new(flex(b))),
SolvedType::Func(vec![flex(a)], Box::new(flex(closure)), Box::new(flex(b))),
],
),
],
@ -1169,7 +1180,14 @@ fn flex(tvar: VarId) -> SolvedType {
fn unique_function(args: Vec<SolvedType>, ret: SolvedType) -> SolvedType {
SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![flex(FUVAR), SolvedType::Func(args, Box::new(ret))],
vec![
flex(FUVAR),
SolvedType::Func(
args,
Box::new(SolvedType::Flex(TOP_LEVEL_CLOSURE_VAR)),
Box::new(ret),
),
],
)
}