let mut sorted_clauses = clauses;
// if we have a list sort clauses so we can plug holes for cases not covered by clauses
-
// TODO: while having 10000000 element list is impossible to destructure in plutus budget,
-
// let's sort clauses by a safer manner
-
// TODO: how shall tails be weighted? Since any clause after will not run
-
sorted_clauses.sort_by(|clause1, clause2| {
-
let clause1_len = match &clause1.pattern {
-
Pattern::List { elements, tail, .. } => {
-
+ usize::from(tail.is_some())
-
+ usize::from(clause1.guard.is_some())
-
let clause2_len = match &clause2.pattern {
-
Pattern::List { elements, tail, .. } => elements.len() + usize::from(tail.is_some()),
+
// Now we sort by elements + tail if possible and otherwise leave an index in place if var or discard
+
// This is a stable sort. i.e. matching elements amounts will remain in user given order.
+
sorted_clauses = sorted_clauses
+
.sorted_by(|(index1, clause1), (index2, clause2)| {
+
let clause1_len = match &clause1.pattern {
+
Pattern::List { elements, tail, .. } => {
+
Some(elements.len() + usize::from(tail.is_some()))
+
_ if clause1.guard.is_none() => Some(100000),
-
clause1_len.cmp(&clause2_len)
+
let clause2_len = match &clause2.pattern {
+
Pattern::List { elements, tail, .. } => {
+
Some(elements.len() + usize::from(tail.is_some()))
+
_ if clause2.guard.is_none() => Some(100001),
+
if let Some(clause1_len) = clause1_len {
+
if let Some(clause2_len) = clause2_len {
+
return clause1_len.cmp(&clause2_len);
let mut final_clauses = sorted_clauses.clone();
let mut holes_to_fill = vec![];
-
let mut assign_plug_in_name = None;
let mut last_clause_index = 0;
let mut last_clause_set = false;
// If we have a catch all, use that. Otherwise use todo which will result in error
// TODO: fill in todo label with description
-
let plug_in_then = if sorted_clauses[sorted_clauses.len() - 1].guard.is_none() {
-
match &sorted_clauses[sorted_clauses.len() - 1].pattern {
-
Pattern::Var { name, .. } => {
-
assign_plug_in_name = Some(name);
-
sorted_clauses[sorted_clauses.len() - 1].clone().then
-
Pattern::Discard { .. } => sorted_clauses[sorted_clauses.len() - 1].clone().then,
-
let tipo = sorted_clauses[sorted_clauses.len() - 1].then.tipo();
-
location: Span::empty(),
-
text: Box::new(TypedExpr::String {
+
let plug_in_then = |index: usize, last_clause: &TypedClause| {
+
if last_clause.guard.is_none() {
+
match &last_clause.pattern {
+
Pattern::Var { .. } | Pattern::Discard { .. } => last_clause.clone().then,
+
let tipo = last_clause.then.tipo();
-
tipo: crate::builtins::string(),
-
value: "Clause not filled".to_string(),
-
then: Box::new(TypedExpr::ErrorTerm {
-
location: Span::empty(),
+
text: Box::new(TypedExpr::String {
+
location: Span::empty(),
+
tipo: crate::builtins::string(),
+
value: format!("Clause hole found for {index} elements."),
+
then: Box::new(TypedExpr::ErrorTerm {
+
location: Span::empty(),
-
let tipo = sorted_clauses[sorted_clauses.len() - 1].then.tipo();
-
location: Span::empty(),
-
text: Box::new(TypedExpr::String {
-
location: Span::empty(),
-
tipo: crate::builtins::string(),
-
value: "Clause not filled".to_string(),
-
then: Box::new(TypedExpr::ErrorTerm {
+
let tipo = last_clause.then.tipo();
+
text: Box::new(TypedExpr::String {
+
location: Span::empty(),
+
tipo: crate::builtins::string(),
+
value: format!("Clause hole found for {index} elements."),
+
then: Box::new(TypedExpr::ErrorTerm {
+
location: Span::empty(),
+
let last_clause = &sorted_clauses[sorted_clauses.len() - 1];
+
let assign_plug_in_name = if let Pattern::Var { name, .. } = &last_clause.pattern {
for (index, clause) in sorted_clauses.iter().enumerate() {
if let Pattern::List { elements, .. } = &clause.pattern {
// found a hole and now we plug it
-
then: plug_in_then.clone(),
+
then: plug_in_then(elems_len, last_clause),
-
then: plug_in_then.clone(),
+
then: plug_in_then(elems_len, last_clause),
-
then: plug_in_then.clone(),
+
then: plug_in_then(index + 1, last_clause),
+
/// If the pattern is a list the return the number of elements and if it has a tail
+
/// Otherwise return None
+
pub fn get_list_elements_len_and_tail(
+
pattern: &Pattern<PatternConstructor, Arc<Type>>,
+
) -> Option<(usize, bool)> {
+
if let Pattern::List { elements, tail, .. } = &pattern {
+
Some((elements.len(), tail.is_some()))
+
} else if let Pattern::Assign { pattern, .. } = &pattern {
+
if let Pattern::List { elements, tail, .. } = pattern.as_ref() {
+
Some((elements.len(), tail.is_some()))
#[allow(clippy::too_many_arguments)]
pub fn list_access_to_uplc(