Skip to content

Commit

Permalink
Unify functions fx vars
Browse files Browse the repository at this point in the history
  • Loading branch information
agu-z committed Oct 15, 2024
1 parent 6c0c7ac commit d1f6ac2
Show file tree
Hide file tree
Showing 26 changed files with 222 additions and 70 deletions.
10 changes: 8 additions & 2 deletions crates/compiler/can/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,15 +972,21 @@ fn deep_copy_type_vars<C: CopyEnv>(
Structure(Apply(symbol, new_arguments))
})
}
Func(arguments, closure_var, ret_var) => {
Func(arguments, closure_var, ret_var, fx_var) => {
descend_slice!(arguments);

let new_closure_var = descend_var!(closure_var);
let new_ret_var = descend_var!(ret_var);
let new_fx_var = descend_var!(fx_var);

perform_clone!({
let new_arguments = clone_var_slice!(arguments);
Structure(Func(new_arguments, new_closure_var, new_ret_var))
Structure(Func(
new_arguments,
new_closure_var,
new_ret_var,
new_fx_var,
))
})
}
Record(fields, ext_var) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/can/src/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn index_var(
var = *structure;
}
Content::Structure(structure) => match structure {
FlatType::Func(_, _, _) => return Err(TypeError),
FlatType::Func(_, _, _, _) => return Err(TypeError),
FlatType::Apply(Symbol::LIST_LIST, args) => {
match (subs.get_subs_slice(*args), ctor) {
([elem_var], IndexCtor::List) => {
Expand Down
3 changes: 2 additions & 1 deletion crates/compiler/checkmate/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ impl AsSchema<Content> for subs::FlatType {
subs::FlatType::Apply(symbol, variables) => {
Content::Apply(symbol.as_schema(subs), variables.as_schema(subs))
}
subs::FlatType::Func(arguments, closure, ret) => Content::Function(
subs::FlatType::Func(arguments, closure, ret, _fx) => Content::Function(
arguments.as_schema(subs),
closure.as_schema(subs),
ret.as_schema(subs),
// [purity-inference] TODO: checkmate
),
subs::FlatType::Record(fields, ext) => {
Content::Record(fields.as_schema(subs), ext.as_schema(subs))
Expand Down
3 changes: 3 additions & 0 deletions crates/compiler/derive/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ fn wrap_in_decode_custom_decode_with(
this_decode_with_var_slice,
this_decode_with_clos_var,
this_decode_with_ret_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -140,6 +141,7 @@ fn wrap_in_decode_custom_decode_with(
args_slice,
fn_clos_var,
decode_with_result_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -185,6 +187,7 @@ fn wrap_in_decode_custom_decode_with(
this_decode_custom_args,
this_decode_custom_clos_var,
this_decode_custom_ret_var,
Variable::PURE,
)),
);

Expand Down
1 change: 1 addition & 0 deletions crates/compiler/derive/src/decoding/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub(crate) fn decoder(env: &mut Env<'_>, _def_symbol: Symbol) -> (Expr, Variable
elem_decoder_var_slice,
this_decode_list_clos_var,
this_decode_list_ret_var,
Variable::PURE,
)),
);

Expand Down
25 changes: 21 additions & 4 deletions crates/compiler/derive/src/decoding/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub(crate) fn decoder(
.insert_into_vars([initial_state_var, step_var, finalizer_var]),
decode_record_lambda_set,
record_decoder_var,
Variable::PURE,
);

synth_var(env.subs, Content::Structure(flat_type))
Expand Down Expand Up @@ -343,7 +344,12 @@ pub(super) fn step_field(

env.subs.set_content(
function_type,
Content::Structure(FlatType::Func(args_slice, closure_type, keep_or_skip_var)),
Content::Structure(FlatType::Func(
args_slice,
closure_type,
keep_or_skip_var,
Variable::PURE,
)),
)
};

Expand Down Expand Up @@ -407,8 +413,12 @@ fn custom_decoder(env: &mut Env<'_>, args: DecodingFieldArgs) -> (Variable, Expr
let decode_custom_closure_var = env.subs.fresh_unnamed_flex_var();
let this_decode_custom_var = {
let subs_slice = env.subs.insert_into_vars([this_custom_callback_var]);
let flat_type =
FlatType::Func(subs_slice, decode_custom_closure_var, decode_custom_ret_var);
let flat_type = FlatType::Func(
subs_slice,
decode_custom_closure_var,
decode_custom_ret_var,
Variable::PURE,
);

synth_var(env.subs, Content::Structure(flat_type))
};
Expand Down Expand Up @@ -578,6 +588,7 @@ fn custom_decoder_lambda(env: &mut Env<'_>, args: DecodingFieldArgs) -> (Variabl
subs_slice,
custom_callback_lambda_set_var,
custom_callback_ret_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -987,6 +998,7 @@ pub(super) fn finalizer(
env.subs.insert_into_vars([state_record_var, fmt_arg_var]),
closure_type,
return_type_var,
Variable::PURE,
);

// Fix up function_var so it's not Content::Error anymore
Expand Down Expand Up @@ -1277,7 +1289,12 @@ fn make_decode_with_vars(
.insert_into_vars([bytes_arg_var, decoder_var, fmt_arg_var]);
let this_decode_with_var = synth_var(
env.subs,
Content::Structure(FlatType::Func(subs_slice, lambda_set_var, rec_var)),
Content::Structure(FlatType::Func(
subs_slice,
lambda_set_var,
rec_var,
Variable::PURE,
)),
);

env.unify(decode_with_var, this_decode_with_var);
Expand Down
25 changes: 21 additions & 4 deletions crates/compiler/derive/src/decoding/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub(crate) fn decoder(env: &mut Env, _def_symbol: Symbol, arity: u32) -> (Expr,
.insert_into_vars([state_var, step_var, finalizer_var]),
decode_record_lambda_set,
tuple_decoder_var,
Variable::PURE,
);

synth_var(env.subs, Content::Structure(flat_type))
Expand Down Expand Up @@ -277,7 +278,12 @@ fn step_elem(
.insert_into_vars([bytes_arg_var, decoder_var, fmt_arg_var]);
let this_decode_with_var = synth_var(
env.subs,
Content::Structure(FlatType::Func(subs_slice, lambda_set_var, rec_var)),
Content::Structure(FlatType::Func(
subs_slice,
lambda_set_var,
rec_var,
Variable::PURE,
)),
);

env.unify(decode_with_var, this_decode_with_var);
Expand Down Expand Up @@ -547,6 +553,7 @@ fn step_elem(
subs_slice,
custom_callback_lambda_set_var,
custom_callback_ret_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -584,8 +591,12 @@ fn step_elem(
let decode_custom_closure_var = env.subs.fresh_unnamed_flex_var();
let this_decode_custom_var = {
let subs_slice = env.subs.insert_into_vars([this_custom_callback_var]);
let flat_type =
FlatType::Func(subs_slice, decode_custom_closure_var, decode_custom_ret_var);
let flat_type = FlatType::Func(
subs_slice,
decode_custom_closure_var,
decode_custom_ret_var,
Variable::PURE,
);

synth_var(env.subs, Content::Structure(flat_type))
};
Expand Down Expand Up @@ -706,7 +717,12 @@ fn step_elem(

env.subs.set_content(
function_type,
Content::Structure(FlatType::Func(args_slice, closure_type, keep_or_skip_var)),
Content::Structure(FlatType::Func(
args_slice,
closure_type,
keep_or_skip_var,
Variable::PURE,
)),
)
};

Expand Down Expand Up @@ -891,6 +907,7 @@ fn finalizer(
env.subs.insert_into_vars([state_record_var]),
closure_type,
return_type_var,
Variable::PURE,
);

// Fix up function_var so it's not Content::Error anymore
Expand Down
22 changes: 21 additions & 1 deletion crates/compiler/derive/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn to_encoder_list(env: &mut Env<'_>, fn_name: Symbol) -> (Expr, Variable) {
elem_var_slice,
to_encoder_clos_var,
elem_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -181,6 +182,7 @@ fn to_encoder_list(env: &mut Env<'_>, fn_name: Symbol) -> (Expr, Variable) {
elem_var_slice,
to_elem_encoder_lset,
elem_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -217,6 +219,7 @@ fn to_encoder_list(env: &mut Env<'_>, fn_name: Symbol) -> (Expr, Variable) {
this_encode_list_args_slice,
this_encode_list_clos_var,
this_list_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -276,6 +279,7 @@ fn to_encoder_list(env: &mut Env<'_>, fn_name: Symbol) -> (Expr, Variable) {
list_var_slice,
fn_clos_var,
this_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -356,6 +360,7 @@ fn to_encoder_record(
field_var_slice,
to_encoder_clos_var,
encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -438,6 +443,7 @@ fn to_encoder_record(
fields_list_var_slice,
encode_record_clos_var,
encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -488,6 +494,7 @@ fn to_encoder_record(
record_var_slice,
fn_clos_var,
this_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -561,6 +568,7 @@ fn to_encoder_tuple(
elem_var_slice,
to_encoder_clos_var,
encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -623,6 +631,7 @@ fn to_encoder_tuple(
elem_encoders_list_var_slice,
encode_tuple_clos_var,
encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -673,6 +682,7 @@ fn to_encoder_tuple(
tuple_var_slice,
fn_clos_var,
this_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -763,6 +773,7 @@ fn to_encoder_tag_union(
var_slice_of_sym_var,
to_encoder_clos_var,
encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -824,6 +835,7 @@ fn to_encoder_tag_union(
this_encode_tag_args_var_slice,
this_encode_tag_clos_var,
this_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -913,6 +925,7 @@ fn to_encoder_tag_union(
tag_union_var_slice,
fn_clos_var,
this_encoder_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -981,6 +994,7 @@ fn wrap_in_encode_custom(
this_append_with_args_var_slice,
this_append_with_clos_var,
Variable::LIST_U8,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -1031,7 +1045,12 @@ fn wrap_in_encode_custom(
let args_slice = env.subs.insert_into_vars(vec![bytes_var, fmt_var]);
env.subs.set_content(
fn_var,
Content::Structure(FlatType::Func(args_slice, fn_clos_var, Variable::LIST_U8)),
Content::Structure(FlatType::Func(
args_slice,
fn_clos_var,
Variable::LIST_U8,
Variable::PURE,
)),
);

// \bytes, fmt -[[fn_name captured_var]]-> Encode.appendWith bytes encoder fmt
Expand Down Expand Up @@ -1074,6 +1093,7 @@ fn wrap_in_encode_custom(
this_custom_args_var_slice,
this_custom_clos_var,
this_custom_encoder_var,
Variable::PURE,
)),
);

Expand Down
8 changes: 7 additions & 1 deletion crates/compiler/derive/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ fn call_hash_ability_member(
this_arguments_slice,
this_hash_clos_var,
this_out_hasher_var,
Variable::PURE,
)),
);

Expand Down Expand Up @@ -533,7 +534,12 @@ fn build_outer_derived_closure(
let args_slice = env.subs.insert_into_vars([hasher_var, val_var]);
env.subs.set_content(
fn_var,
Content::Structure(FlatType::Func(args_slice, fn_clos_var, body_var)),
Content::Structure(FlatType::Func(
args_slice,
fn_clos_var,
body_var,
Variable::PURE,
)),
);

(fn_var, fn_clos_var)
Expand Down
Loading

0 comments on commit d1f6ac2

Please sign in to comment.