diff --git a/crates/aiken-lang/src/gen_uplc/builder.rs b/crates/aiken-lang/src/gen_uplc/builder.rs index d92be69f..e761c527 100644 --- a/crates/aiken-lang/src/gen_uplc/builder.rs +++ b/crates/aiken-lang/src/gen_uplc/builder.rs @@ -955,76 +955,64 @@ pub fn softcast_data_to_type_otherwise( ) -> Term { let uplc_type = field_type.get_uplc_type(); - let just_then = then.clone(); - - let then_delayed = |v| then.lambda(name).apply(v).delay(); + let callback = |v| then.lambda(name).apply(v); value.as_var("__val", |val| match uplc_type { - None => Term::choose_data_constr(val, then_delayed, &otherwise_delayed).force(), + None => Term::choose_data_constr(val, callback, &otherwise_delayed), - Some(UplcType::Data) => just_then.lambda(name).apply(Term::Var(val)), + Some(UplcType::Data) => callback(Term::Var(val)), Some(UplcType::Bls12_381MlResult) => { unreachable!("attempted to cast Data into Bls12_381MlResult ?!") } - Some(UplcType::Integer) => { - Term::choose_data_integer(val, then_delayed, &otherwise_delayed).force() - } + Some(UplcType::Integer) => Term::choose_data_integer(val, callback, &otherwise_delayed), Some(UplcType::ByteString) => { - Term::choose_data_bytearray(val, then_delayed, &otherwise_delayed).force() + Term::choose_data_bytearray(val, callback, &otherwise_delayed) } Some(UplcType::String) => Term::choose_data_bytearray( val, - |bytes| then_delayed(Term::decode_utf8().apply(bytes)), + |bytes| callback(Term::decode_utf8().apply(bytes)), &otherwise_delayed, - ) - .force(), + ), Some(UplcType::List(_)) if field_type.is_map() => { - Term::choose_data_map(val, then_delayed, &otherwise_delayed).force() + Term::choose_data_map(val, callback, &otherwise_delayed) } - Some(UplcType::List(_)) => { - Term::choose_data_list(val, then_delayed, &otherwise_delayed).force() - } + Some(UplcType::List(_)) => Term::choose_data_list(val, callback, &otherwise_delayed), Some(UplcType::Bls12_381G1Element) => Term::choose_data_bytearray( val, - |bytes| then_delayed(Term::bls12_381_g1_uncompress().apply(bytes)), + |bytes| callback(Term::bls12_381_g1_uncompress().apply(bytes)), &otherwise_delayed, - ) - .force(), + ), Some(UplcType::Bls12_381G2Element) => Term::choose_data_bytearray( val, - |bytes| then_delayed(Term::bls12_381_g2_uncompress().apply(bytes)), + |bytes| callback(Term::bls12_381_g2_uncompress().apply(bytes)), &otherwise_delayed, - ) - .force(), + ), Some(UplcType::Pair(_, _)) => Term::choose_data_list( val, - |list| list.unwrap_pair_or(then_delayed, &otherwise_delayed), + |list| list.unwrap_pair_or(callback, &otherwise_delayed), &otherwise_delayed, - ) - .force(), + ), Some(UplcType::Bool) => Term::choose_data_constr( val, - |constr| constr.unwrap_bool_or(then_delayed, &otherwise_delayed), + |constr| constr.unwrap_bool_or(callback, &otherwise_delayed), &otherwise_delayed, - ) - .force(), + ), Some(UplcType::Unit) => Term::choose_data_constr( val, - |constr| constr.unwrap_void_or(then_delayed, &otherwise_delayed), + |constr| constr.unwrap_void_or(callback, &otherwise_delayed), &otherwise_delayed, - ) - .force(), + ), }) } diff --git a/crates/uplc/src/builder.rs b/crates/uplc/src/builder.rs index 12c20d5a..741d9d74 100644 --- a/crates/uplc/src/builder.rs +++ b/crates/uplc/src/builder.rs @@ -514,13 +514,15 @@ impl Term { where F: FnOnce(Term) -> Term, { - Term::Var(var.clone()).choose_data( - otherwise.clone(), - otherwise.clone(), - otherwise.clone(), - callback(Term::un_i_data().apply(Term::Var(var))), - otherwise.clone(), - ) + Term::Var(var.clone()) + .choose_data( + otherwise.clone(), + otherwise.clone(), + otherwise.clone(), + callback(Term::un_i_data().apply(Term::Var(var))).delay(), + otherwise.clone(), + ) + .force() } /// Continue a computation provided that the current term is a Data-wrapped @@ -529,13 +531,15 @@ impl Term { where F: FnOnce(Term) -> Term, { - Term::Var(var.clone()).choose_data( - otherwise.clone(), - otherwise.clone(), - otherwise.clone(), - otherwise.clone(), - callback(Term::un_b_data().apply(Term::Var(var))), - ) + Term::Var(var.clone()) + .choose_data( + otherwise.clone(), + otherwise.clone(), + otherwise.clone(), + otherwise.clone(), + callback(Term::un_b_data().apply(Term::Var(var))).delay(), + ) + .force() } /// Continue a computation provided that the current term is a Data-wrapped @@ -544,13 +548,15 @@ impl Term { where F: FnOnce(Term) -> Term, { - Term::Var(var.clone()).choose_data( - otherwise.clone(), - otherwise.clone(), - callback(Term::unlist_data().apply(Term::Var(var))), - otherwise.clone(), - otherwise.clone(), - ) + Term::Var(var.clone()) + .choose_data( + otherwise.clone(), + otherwise.clone(), + callback(Term::unlist_data().apply(Term::Var(var))).delay(), + otherwise.clone(), + otherwise.clone(), + ) + .force() } /// Continue a computation provided that the current term is a Data-wrapped @@ -559,13 +565,15 @@ impl Term { where F: FnOnce(Term) -> Term, { - Term::Var(var.clone()).choose_data( - otherwise.clone(), - callback(Term::unmap_data().apply(Term::Var(var))), - otherwise.clone(), - otherwise.clone(), - otherwise.clone(), - ) + Term::Var(var.clone()) + .choose_data( + otherwise.clone(), + callback(Term::unmap_data().apply(Term::Var(var))).delay(), + otherwise.clone(), + otherwise.clone(), + otherwise.clone(), + ) + .force() } /// Continue a computation provided that the current term is a Data-wrapped @@ -574,13 +582,15 @@ impl Term { where F: FnOnce(Term) -> Term, { - Term::Var(var.clone()).choose_data( - callback(Term::Var(var)), - otherwise.clone(), - otherwise.clone(), - otherwise.clone(), - otherwise.clone(), - ) + Term::Var(var.clone()) + .choose_data( + callback(Term::Var(var)).delay(), + otherwise.clone(), + otherwise.clone(), + otherwise.clone(), + otherwise.clone(), + ) + .force() } /// Convert an arbitrary 'term' into a bool term and pass it into a 'callback'.