tuple destructuring nested and not now works

This commit is contained in:
Kasey White 2022-12-26 23:44:56 -05:00 committed by Lucas
parent a08c615da4
commit 919ea6c723
3 changed files with 170 additions and 70 deletions

View File

@ -383,9 +383,7 @@ pub fn convert_data_to_type(term: Term<Name>, field_type: &Arc<Type>) -> Term<Na
}
.into(),
argument: Term::Apply {
function: Term::Builtin(DefaultFunction::UnListData)
.force_wrap()
.into(),
function: Term::Builtin(DefaultFunction::UnListData).into(),
argument: term.into(),
}
.into(),
@ -514,11 +512,11 @@ pub fn rearrange_clauses(
// if we have a pattern with no clause guards and a tail then no lists will get past here to other clauses
match &clause.pattern[0] {
Pattern::Var { .. } => {
last_clause_index = index;
last_clause_index = index + 1;
last_clause_set = true;
}
Pattern::Discard { .. } => {
last_clause_index = index;
last_clause_index = index + 1;
last_clause_set = true;
}
Pattern::List {
@ -532,6 +530,7 @@ pub fn rearrange_clauses(
.iter()
.all(|element| matches!(element, Pattern::Var { .. } | Pattern::Discard { .. }))
&& !last_clause_set
&& !elements.is_empty()
{
last_clause_index = index + 1;
last_clause_set = true;
@ -627,8 +626,7 @@ pub fn list_access_to_uplc(
}
.into(),
argument: Term::Apply {
function: Term::Force(Term::Builtin(DefaultFunction::TailList).into())
.into(),
function: Term::Builtin(DefaultFunction::TailList).force_wrap().into(),
argument: Term::Var(Name {
text: format!(
"tail_index_{}_{}",
@ -697,8 +695,7 @@ pub fn list_access_to_uplc(
)
.into(),
argument: Term::Apply {
function: Term::Force(Term::Builtin(DefaultFunction::TailList).into())
.into(),
function: Term::Builtin(DefaultFunction::TailList).force_wrap().into(),
argument: Term::Var(Name {
text: format!(
"tail_index_{}_{}",
@ -1264,6 +1261,8 @@ pub fn monomorphize(
find_generics_to_replace(&mut tipo, &generic_types);
needs_variant = false;
new_indices.push((ind, name, tipo));
} else {
new_indices.push((ind, name, tipo));
}
}
new_air[index] = Air::FieldsExpose {
@ -1291,7 +1290,40 @@ pub fn monomorphize(
}
}
Air::RecordUpdate { .. } => todo!(),
Air::TupleAccessor { .. } => todo!(),
Air::TupleAccessor { scope, names, tipo } => {
if tipo.is_generic() {
let mut tipo = tipo.clone();
find_generics_to_replace(&mut tipo, &generic_types);
new_air[index] = Air::TupleAccessor { scope, names, tipo };
needs_variant = false;
}
}
Air::TupleClause {
scope,
tipo,
indices,
predefined_indices,
subject_name,
count,
complex_clause,
} => {
if tipo.is_generic() {
let mut tipo = tipo.clone();
find_generics_to_replace(&mut tipo, &generic_types);
new_air[index] = Air::TupleClause {
scope,
tipo,
indices,
predefined_indices,
subject_name,
count,
complex_clause,
};
needs_variant = false;
}
}
_ => {}
}
}

View File

@ -11,7 +11,7 @@ use uplc::{
ast::{
builder::{
self, apply_wrap, choose_list, constr_index_exposer, delayed_choose_list,
delayed_if_else, if_else, CONSTR_FIELDS_EXPOSER, CONSTR_GET_FIELD,
delayed_if_else, if_else, repeat_tail_list, CONSTR_FIELDS_EXPOSER, CONSTR_GET_FIELD,
},
Constant as UplcConstant, Name, NamedDeBruijn, Program, Term, Type as UplcType,
},
@ -1018,10 +1018,6 @@ impl<'a> CodeGenerator<'a> {
type_map.insert(label, field_type);
}
println!("TYPE MAP IS {type_map:#?}");
println!("Type args are {:#?}", tipo.arg_types());
println!("ARGUMENTS ARE {:#?}", arguments);
let arguments_index = arguments
.iter()
.filter_map(|item| {
@ -1141,7 +1137,6 @@ impl<'a> CodeGenerator<'a> {
defined_indices.insert((index, name));
}
}
println!("DEFINED INDICES ARE {:#?}", defined_indices);
for (index, name) in previous_defined_names {
let new_name = names
@ -1179,8 +1174,6 @@ impl<'a> CodeGenerator<'a> {
_ => unreachable!(),
}
println!("CLAUSE PROPERTIES IS NOW {:#?}", clause_properties);
pattern_vec.append(&mut nested_pattern);
pattern_vec.append(values);
}
@ -1473,7 +1466,7 @@ impl<'a> CodeGenerator<'a> {
};
pattern_vec.push(Air::TupleClause {
scope: scope.clone(),
scope,
tipo: pattern_type.clone(),
indices: defined_indices,
predefined_indices: HashSet::new(),
@ -2314,7 +2307,8 @@ impl<'a> CodeGenerator<'a> {
module,
..
} => {
let name = if *func_name == name {
let name = if *func_name == name || name == format!("{module}_{func_name}")
{
format!("{module}_{func_name}{variant_name}")
} else {
format!("{func_name}{variant_name}")
@ -2627,9 +2621,8 @@ impl<'a> CodeGenerator<'a> {
)
.into(),
argument: Term::Apply {
function: Term::Force(
Term::Builtin(DefaultFunction::TailList).into(),
)
function: Term::Builtin(DefaultFunction::TailList)
.force_wrap()
.into(),
argument: Term::Var(Name {
text: format!("__list_{}", list_id),
@ -3602,10 +3595,10 @@ impl<'a> CodeGenerator<'a> {
} else {
Term::Apply {
function: DefaultFunction::EqualsInteger.into(),
argument: Term::Var(Name {
argument: constr_index_exposer(Term::Var(Name {
text: subject_name,
unique: 0.into(),
})
}))
.into(),
}
};
@ -3794,7 +3787,6 @@ impl<'a> CodeGenerator<'a> {
.into(),
argument: Term::Apply {
function: Term::Builtin(DefaultFunction::TailList)
.force_wrap()
.force_wrap()
.into(),
argument: prev_tail.into(),
@ -3814,7 +3806,6 @@ impl<'a> CodeGenerator<'a> {
.into(),
argument: Term::Apply {
function: Term::Builtin(DefaultFunction::TailList)
.force_wrap()
.force_wrap()
.into(),
argument: prev_tail.into(),
@ -3891,7 +3882,7 @@ impl<'a> CodeGenerator<'a> {
arg_stack.push(term);
} else {
let mut term = Term::Constant(UplcConstant::ProtoList(UplcType::Data, vec![]));
for (arg, tipo) in args.into_iter().zip(tuple_sub_types.into_iter()) {
for (arg, tipo) in args.into_iter().zip(tuple_sub_types.into_iter()).rev() {
term = Term::Apply {
function: Term::Apply {
function: Term::Builtin(DefaultFunction::MkCons)
@ -3936,6 +3927,7 @@ impl<'a> CodeGenerator<'a> {
}
Air::TupleIndex { .. } => todo!(),
Air::TupleAccessor { tipo, names, .. } => {
let inner_types = tipo.get_inner_types();
let value = arg_stack.pop().unwrap();
let mut term = arg_stack.pop().unwrap();
let list_id = self.id_gen.next();
@ -3962,7 +3954,8 @@ impl<'a> CodeGenerator<'a> {
body: term.into(),
}
.into(),
argument: Term::Apply {
argument: convert_data_to_type(
Term::Apply {
function: Term::Builtin(DefaultFunction::SndPair)
.force_wrap()
.force_wrap()
@ -3972,13 +3965,16 @@ impl<'a> CodeGenerator<'a> {
unique: 0.into(),
})
.into(),
}
},
&inner_types[1],
)
.into(),
}
.into(),
}
.into(),
argument: Term::Apply {
argument: convert_data_to_type(
Term::Apply {
function: Term::Builtin(DefaultFunction::FstPair)
.force_wrap()
.force_wrap()
@ -3988,7 +3984,9 @@ impl<'a> CodeGenerator<'a> {
unique: 0.into(),
})
.into(),
}
},
&inner_types[0],
)
.into(),
}
.into(),
@ -4105,32 +4103,105 @@ impl<'a> CodeGenerator<'a> {
Air::TupleClause {
tipo,
indices,
predefined_indices,
subject_name,
complex_clause,
..
} => {
let term = arg_stack.pop().unwrap();
let mut term = arg_stack.pop().unwrap();
let tuple_types = tipo.get_inner_types();
if tuple_types.len() == 2 {
for (index, name) in indices.iter() {
if *index == 0 {
// apply_wrap(
// Term::Builtin(DefaultFunction::FstPair)
// .force_wrap()
// .force_wrap(),
// Term::Var(Name {
// text: subject_name.clone(),
// unique: 0.into(),
// }),
// )
term = apply_wrap(
Term::Lambda {
parameter_name: Name {
text: name.clone(),
unique: 0.into(),
},
body: term.into(),
},
convert_data_to_type(
apply_wrap(
Term::Builtin(DefaultFunction::FstPair)
.force_wrap()
.force_wrap(),
Term::Var(Name {
text: subject_name.clone(),
unique: 0.into(),
}),
),
&tuple_types[*index].clone(),
),
);
} else {
term = apply_wrap(
Term::Lambda {
parameter_name: Name {
text: name.clone(),
unique: 0.into(),
},
body: term.into(),
},
convert_data_to_type(
apply_wrap(
Term::Builtin(DefaultFunction::SndPair)
.force_wrap()
.force_wrap(),
Term::Var(Name {
text: subject_name.clone(),
unique: 0.into(),
}),
),
&tuple_types[*index].clone(),
),
);
}
}
} else {
for (index, name) in indices.iter() {
term = apply_wrap(
Term::Lambda {
parameter_name: Name {
text: name.clone(),
unique: 0.into(),
},
body: term.into(),
},
convert_data_to_type(
apply_wrap(
Term::Builtin(DefaultFunction::HeadList).force_wrap(),
repeat_tail_list(
Term::Var(Name {
text: subject_name.clone(),
unique: 0.into(),
}),
*index,
),
),
&tuple_types[*index].clone(),
),
);
}
}
if complex_clause {
let next_clause = arg_stack.pop().unwrap();
term = apply_wrap(
Term::Lambda {
parameter_name: Name {
text: "__other_clauses_delayed".to_string(),
unique: 0.into(),
},
body: term.into(),
},
Term::Delay(next_clause.into()),
)
}
arg_stack.push(term);
}
}
}
}

View File

@ -570,7 +570,6 @@ where
Ok(programs)
}
// TODO: revisit ownership and lifetimes of data in this function
fn collect_scripts(
&mut self,
verbose: bool,
@ -595,12 +594,8 @@ where
let mut scripts = Vec::new();
for module in self
.checked_modules
.values()
.filter(|checked| checked.package == self.config.name.to_string())
{
for (_index, def) in module.ast.definitions().enumerate() {
for module in self.checked_modules.values() {
for def in module.ast.definitions() {
match def {
Definition::Fn(func) => {
functions.insert(
@ -611,15 +606,15 @@ where
},
func,
);
if should_collect(def) {
if should_collect(def) && module.package == self.config.name.to_string() {
scripts.push((module.input_path.clone(), module.name.clone(), func));
}
}
Definition::Test(func) => {
if should_collect(def) {
if should_collect(def) && module.package == self.config.name.to_string() {
scripts.push((module.input_path.clone(), module.name.clone(), func));
}
// indices_to_remove.push(index);
}
Definition::TypeAlias(ta) => {
type_aliases.insert((module.name.clone(), ta.alias.clone()), ta);
@ -673,10 +668,12 @@ where
.generate(*left_src, vec![], false)
.try_into()
.unwrap();
let right = CodeGenerator::new(&functions, &data_types, &self.module_types)
.generate(*right_src, vec![], false)
.try_into()
.unwrap();
Some(EvalHint {
bin_op,
left,