fix: nested list issues in when statements

This commit is contained in:
Kasey White 2023-01-03 00:49:09 -05:00 committed by Lucas
parent 88d5d1b7f7
commit 542962a2ea
3 changed files with 108 additions and 123 deletions

View File

@ -134,7 +134,6 @@ pub enum Air {
tail_name: String, tail_name: String,
next_tail_name: Option<String>, next_tail_name: Option<String>,
complex_clause: bool, complex_clause: bool,
inverse: bool,
}, },
TupleClause { TupleClause {
@ -153,6 +152,14 @@ pub enum Air {
tipo: Arc<Type>, tipo: Arc<Type>,
}, },
ListClauseGuard {
scope: Vec<u64>,
tipo: Arc<Type>,
tail_name: String,
next_tail_name: Option<String>,
inverse: bool,
},
Discard { Discard {
scope: Vec<u64>, scope: Vec<u64>,
}, },
@ -264,6 +271,7 @@ impl Air {
| Air::Clause { scope, .. } | Air::Clause { scope, .. }
| Air::ListClause { scope, .. } | Air::ListClause { scope, .. }
| Air::ClauseGuard { scope, .. } | Air::ClauseGuard { scope, .. }
| Air::ListClauseGuard { scope, .. }
| Air::Discard { scope } | Air::Discard { scope }
| Air::Finally { scope } | Air::Finally { scope }
| Air::If { scope, .. } | Air::If { scope, .. }

View File

@ -1283,7 +1283,6 @@ pub fn monomorphize(
tail_name, tail_name,
complex_clause, complex_clause,
next_tail_name, next_tail_name,
inverse,
} => { } => {
if tipo.is_generic() { if tipo.is_generic() {
let mut tipo = tipo.clone(); let mut tipo = tipo.clone();
@ -1295,7 +1294,6 @@ pub fn monomorphize(
tail_name, tail_name,
complex_clause, complex_clause,
next_tail_name, next_tail_name,
inverse,
}; };
needs_variant = false; needs_variant = false;
} }
@ -1317,6 +1315,27 @@ pub fn monomorphize(
needs_variant = false; needs_variant = false;
} }
} }
Air::ListClauseGuard {
scope,
tipo,
tail_name,
next_tail_name,
inverse,
} => {
if tipo.is_generic() {
let mut tipo = tipo.clone();
find_generics_to_replace(&mut tipo, &generic_types);
new_air[index] = Air::ListClauseGuard {
scope,
tipo,
tail_name,
next_tail_name,
inverse,
};
needs_variant = false;
}
}
Air::RecordAccess { Air::RecordAccess {
scope, scope,
index: record_index, index: record_index,

View File

@ -617,7 +617,6 @@ impl<'a> CodeGenerator<'a> {
tail_name: subject_name, tail_name: subject_name,
next_tail_name: next_tail, next_tail_name: next_tail,
complex_clause: *clause_properties.is_complex_clause(), complex_clause: *clause_properties.is_complex_clause(),
inverse: false,
}); });
match clause_properties { match clause_properties {
@ -758,10 +757,6 @@ impl<'a> CodeGenerator<'a> {
} }
*clause_properties.needs_constr_var() = false; *clause_properties.needs_constr_var() = false;
pattern_vec.push(Air::Discard {
scope: scope.clone(),
});
self.when_recursive_ir( self.when_recursive_ir(
pattern, pattern,
pattern_vec, pattern_vec,
@ -1191,29 +1186,12 @@ impl<'a> CodeGenerator<'a> {
let new_tail_name = "__list_tail".to_string(); let new_tail_name = "__list_tail".to_string();
if elements.is_empty() { if elements.is_empty() {
pattern_vec.push(Air::ListClause { pattern_vec.push(Air::ListClauseGuard {
scope: scope.clone(), scope,
tipo: pattern_type.clone(), tipo: pattern_type.clone(),
tail_name: item_name.clone(), tail_name: item_name.clone(),
next_tail_name: None, next_tail_name: None,
complex_clause: false, inverse: false,
inverse: true,
});
pattern_vec.push(Air::Discard {
scope: scope.clone(),
});
pattern_vec.push(Air::Var {
scope,
constructor: ValueConstructor::public(
pattern_type.clone(),
ValueConstructorVariant::LocalVariable {
location: Span::empty(),
},
),
name: "__other_clauses_delayed".to_string(),
variant_name: String::new(),
}); });
} else { } else {
for (index, _) in elements.iter().enumerate() { for (index, _) in elements.iter().enumerate() {
@ -1241,29 +1219,12 @@ impl<'a> CodeGenerator<'a> {
_ => unreachable!(), _ => unreachable!(),
}; };
pattern_vec.push(Air::ListClause { pattern_vec.push(Air::ListClauseGuard {
scope: scope.clone(), scope: scope.clone(),
tipo: pattern_type.clone(), tipo: pattern_type.clone(),
tail_name: prev_tail_name, tail_name: prev_tail_name,
next_tail_name: Some(tail_name), next_tail_name: Some(tail_name),
complex_clause: false, inverse: true,
inverse: false,
});
pattern_vec.push(Air::Discard {
scope: scope.clone(),
});
pattern_vec.push(Air::Var {
scope: scope.clone(),
constructor: ValueConstructor::public(
pattern_type.clone(),
ValueConstructorVariant::LocalVariable {
location: Span::empty(),
},
),
name: "__other_clauses_delayed".to_string(),
variant_name: "".to_string(),
}); });
self.when_ir( self.when_ir(
@ -1275,54 +1236,20 @@ impl<'a> CodeGenerator<'a> {
scope.clone(), scope.clone(),
); );
} else { } else {
pattern_vec.push(Air::ListClause { pattern_vec.push(Air::ListClauseGuard {
scope: scope.clone(), scope: scope.clone(),
tipo: pattern_type.clone(), tipo: pattern_type.clone(),
tail_name: prev_tail_name, tail_name: prev_tail_name,
next_tail_name: Some(tail_name.clone()), next_tail_name: Some(tail_name.clone()),
complex_clause: false, inverse: true,
inverse: false,
}); });
pattern_vec.push(Air::Discard { pattern_vec.push(Air::ListClauseGuard {
scope: scope.clone(),
});
pattern_vec.push(Air::Var {
scope: scope.clone(),
constructor: ValueConstructor::public(
pattern_type.clone(),
ValueConstructorVariant::LocalVariable {
location: Span::empty(),
},
),
name: "__other_clauses_delayed".to_string(),
variant_name: String::new(),
});
pattern_vec.push(Air::ListClause {
scope: scope.clone(), scope: scope.clone(),
tipo: pattern_type.clone(), tipo: pattern_type.clone(),
tail_name: tail_name.clone(), tail_name: tail_name.clone(),
next_tail_name: None, next_tail_name: None,
complex_clause: false, inverse: false,
inverse: true,
});
pattern_vec.push(Air::Discard {
scope: scope.clone(),
});
pattern_vec.push(Air::Var {
scope: scope.clone(),
constructor: ValueConstructor::public(
pattern_type.clone(),
ValueConstructorVariant::LocalVariable {
location: Span::empty(),
},
),
name: "__other_clauses_delayed".to_string(),
variant_name: String::new(),
}); });
self.when_ir( self.when_ir(
@ -1341,29 +1268,12 @@ impl<'a> CodeGenerator<'a> {
_ => unreachable!(), _ => unreachable!(),
}; };
pattern_vec.push(Air::ListClause { pattern_vec.push(Air::ListClauseGuard {
scope: scope.clone(), scope: scope.clone(),
tipo: pattern_type.clone(), tipo: pattern_type.clone(),
tail_name: prev_tail_name, tail_name: prev_tail_name,
next_tail_name: Some(tail_name), next_tail_name: Some(tail_name),
complex_clause: false, inverse: true,
inverse: false,
});
pattern_vec.push(Air::Discard {
scope: scope.clone(),
});
pattern_vec.push(Air::Var {
scope: scope.clone(),
constructor: ValueConstructor::public(
pattern_type.clone(),
ValueConstructorVariant::LocalVariable {
location: Span::empty(),
},
),
name: "__other_clauses_delayed".to_string(),
variant_name: "".to_string(),
}); });
self.when_ir( self.when_ir(
@ -3482,7 +3392,6 @@ impl<'a> CodeGenerator<'a> {
.into(), .into(),
argument: Term::Delay(term.into()).into(), argument: Term::Delay(term.into()).into(),
} }
.force_wrap()
} else { } else {
term = delayed_if_else( term = delayed_if_else(
Term::Apply { Term::Apply {
@ -3499,26 +3408,11 @@ impl<'a> CodeGenerator<'a> {
Air::ListClause { Air::ListClause {
tail_name, tail_name,
next_tail_name, next_tail_name,
inverse,
complex_clause, complex_clause,
.. ..
} => { } => {
// discard to pop off
let _ = arg_stack.pop().unwrap();
// the body to be run if the clause matches
// the next branch in the when expression
let (body, mut term) = if inverse {
let term = arg_stack.pop().unwrap();
let body = arg_stack.pop().unwrap(); let body = arg_stack.pop().unwrap();
let mut term = arg_stack.pop().unwrap();
(body, term)
} else {
let body = arg_stack.pop().unwrap();
let term = arg_stack.pop().unwrap();
(body, term)
};
let arg = if let Some(next_tail_name) = next_tail_name { let arg = if let Some(next_tail_name) = next_tail_name {
Term::Apply { Term::Apply {
@ -3619,7 +3513,7 @@ impl<'a> CodeGenerator<'a> {
.into(), .into(),
} }
} else if tipo.is_list() { } else if tipo.is_list() {
todo!() unreachable!()
} else { } else {
Term::Apply { Term::Apply {
function: DefaultFunction::EqualsInteger.into(), function: DefaultFunction::EqualsInteger.into(),
@ -3646,6 +3540,70 @@ impl<'a> CodeGenerator<'a> {
arg_stack.push(term); arg_stack.push(term);
} }
Air::ListClauseGuard {
tail_name,
next_tail_name,
inverse,
..
} => {
// the body to be run if the clause matches
// the next branch in the when expression
let mut term = arg_stack.pop().unwrap();
term = if let Some(next_tail_name) = next_tail_name {
Term::Apply {
function: Term::Lambda {
parameter_name: Name {
text: next_tail_name,
unique: 0.into(),
},
body: term.into(),
}
.into(),
argument: Term::Apply {
function: Term::Builtin(DefaultFunction::TailList).force_wrap().into(),
argument: Term::Var(Name {
text: tail_name.clone(),
unique: 0.into(),
})
.into(),
}
.into(),
}
} else {
term
};
if !inverse {
term = choose_list(
Term::Var(Name {
text: tail_name,
unique: 0.into(),
}),
Term::Delay(term.into()),
Term::Var(Name {
text: "__other_clauses_delayed".to_string(),
unique: 0.into(),
}),
)
.force_wrap();
} else {
term = choose_list(
Term::Var(Name {
text: tail_name,
unique: 0.into(),
}),
Term::Var(Name {
text: "__other_clauses_delayed".to_string(),
unique: 0.into(),
}),
Term::Delay(term.into()),
)
.force_wrap();
}
arg_stack.push(term);
}
Air::Finally { .. } => { Air::Finally { .. } => {
let _clause = arg_stack.pop().unwrap(); let _clause = arg_stack.pop().unwrap();
} }