Rework choose_data_xxx API to include force/delay inside functions.
This commit is contained in:
parent
d23a5b2f11
commit
23a3134642
|
@ -955,76 +955,64 @@ pub fn softcast_data_to_type_otherwise(
|
||||||
) -> Term<Name> {
|
) -> Term<Name> {
|
||||||
let uplc_type = field_type.get_uplc_type();
|
let uplc_type = field_type.get_uplc_type();
|
||||||
|
|
||||||
let just_then = then.clone();
|
let callback = |v| then.lambda(name).apply(v);
|
||||||
|
|
||||||
let then_delayed = |v| then.lambda(name).apply(v).delay();
|
|
||||||
|
|
||||||
value.as_var("__val", |val| match uplc_type {
|
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) => {
|
Some(UplcType::Bls12_381MlResult) => {
|
||||||
unreachable!("attempted to cast Data into Bls12_381MlResult ?!")
|
unreachable!("attempted to cast Data into Bls12_381MlResult ?!")
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(UplcType::Integer) => {
|
Some(UplcType::Integer) => Term::choose_data_integer(val, callback, &otherwise_delayed),
|
||||||
Term::choose_data_integer(val, then_delayed, &otherwise_delayed).force()
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(UplcType::ByteString) => {
|
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(
|
Some(UplcType::String) => Term::choose_data_bytearray(
|
||||||
val,
|
val,
|
||||||
|bytes| then_delayed(Term::decode_utf8().apply(bytes)),
|
|bytes| callback(Term::decode_utf8().apply(bytes)),
|
||||||
&otherwise_delayed,
|
&otherwise_delayed,
|
||||||
)
|
),
|
||||||
.force(),
|
|
||||||
|
|
||||||
Some(UplcType::List(_)) if field_type.is_map() => {
|
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(_)) => {
|
Some(UplcType::List(_)) => Term::choose_data_list(val, callback, &otherwise_delayed),
|
||||||
Term::choose_data_list(val, then_delayed, &otherwise_delayed).force()
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(UplcType::Bls12_381G1Element) => Term::choose_data_bytearray(
|
Some(UplcType::Bls12_381G1Element) => Term::choose_data_bytearray(
|
||||||
val,
|
val,
|
||||||
|bytes| then_delayed(Term::bls12_381_g1_uncompress().apply(bytes)),
|
|bytes| callback(Term::bls12_381_g1_uncompress().apply(bytes)),
|
||||||
&otherwise_delayed,
|
&otherwise_delayed,
|
||||||
)
|
),
|
||||||
.force(),
|
|
||||||
|
|
||||||
Some(UplcType::Bls12_381G2Element) => Term::choose_data_bytearray(
|
Some(UplcType::Bls12_381G2Element) => Term::choose_data_bytearray(
|
||||||
val,
|
val,
|
||||||
|bytes| then_delayed(Term::bls12_381_g2_uncompress().apply(bytes)),
|
|bytes| callback(Term::bls12_381_g2_uncompress().apply(bytes)),
|
||||||
&otherwise_delayed,
|
&otherwise_delayed,
|
||||||
)
|
),
|
||||||
.force(),
|
|
||||||
|
|
||||||
Some(UplcType::Pair(_, _)) => Term::choose_data_list(
|
Some(UplcType::Pair(_, _)) => Term::choose_data_list(
|
||||||
val,
|
val,
|
||||||
|list| list.unwrap_pair_or(then_delayed, &otherwise_delayed),
|
|list| list.unwrap_pair_or(callback, &otherwise_delayed),
|
||||||
&otherwise_delayed,
|
&otherwise_delayed,
|
||||||
)
|
),
|
||||||
.force(),
|
|
||||||
|
|
||||||
Some(UplcType::Bool) => Term::choose_data_constr(
|
Some(UplcType::Bool) => Term::choose_data_constr(
|
||||||
val,
|
val,
|
||||||
|constr| constr.unwrap_bool_or(then_delayed, &otherwise_delayed),
|
|constr| constr.unwrap_bool_or(callback, &otherwise_delayed),
|
||||||
&otherwise_delayed,
|
&otherwise_delayed,
|
||||||
)
|
),
|
||||||
.force(),
|
|
||||||
|
|
||||||
Some(UplcType::Unit) => Term::choose_data_constr(
|
Some(UplcType::Unit) => Term::choose_data_constr(
|
||||||
val,
|
val,
|
||||||
|constr| constr.unwrap_void_or(then_delayed, &otherwise_delayed),
|
|constr| constr.unwrap_void_or(callback, &otherwise_delayed),
|
||||||
&otherwise_delayed,
|
&otherwise_delayed,
|
||||||
)
|
),
|
||||||
.force(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -514,13 +514,15 @@ impl Term<Name> {
|
||||||
where
|
where
|
||||||
F: FnOnce(Term<Name>) -> Term<Name>,
|
F: FnOnce(Term<Name>) -> Term<Name>,
|
||||||
{
|
{
|
||||||
Term::Var(var.clone()).choose_data(
|
Term::Var(var.clone())
|
||||||
otherwise.clone(),
|
.choose_data(
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
callback(Term::un_i_data().apply(Term::Var(var))),
|
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
|
/// Continue a computation provided that the current term is a Data-wrapped
|
||||||
|
@ -529,13 +531,15 @@ impl Term<Name> {
|
||||||
where
|
where
|
||||||
F: FnOnce(Term<Name>) -> Term<Name>,
|
F: FnOnce(Term<Name>) -> Term<Name>,
|
||||||
{
|
{
|
||||||
Term::Var(var.clone()).choose_data(
|
Term::Var(var.clone())
|
||||||
otherwise.clone(),
|
.choose_data(
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
callback(Term::un_b_data().apply(Term::Var(var))),
|
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
|
/// Continue a computation provided that the current term is a Data-wrapped
|
||||||
|
@ -544,13 +548,15 @@ impl Term<Name> {
|
||||||
where
|
where
|
||||||
F: FnOnce(Term<Name>) -> Term<Name>,
|
F: FnOnce(Term<Name>) -> Term<Name>,
|
||||||
{
|
{
|
||||||
Term::Var(var.clone()).choose_data(
|
Term::Var(var.clone())
|
||||||
otherwise.clone(),
|
.choose_data(
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
callback(Term::unlist_data().apply(Term::Var(var))),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
callback(Term::unlist_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
|
/// Continue a computation provided that the current term is a Data-wrapped
|
||||||
|
@ -559,13 +565,15 @@ impl Term<Name> {
|
||||||
where
|
where
|
||||||
F: FnOnce(Term<Name>) -> Term<Name>,
|
F: FnOnce(Term<Name>) -> Term<Name>,
|
||||||
{
|
{
|
||||||
Term::Var(var.clone()).choose_data(
|
Term::Var(var.clone())
|
||||||
otherwise.clone(),
|
.choose_data(
|
||||||
callback(Term::unmap_data().apply(Term::Var(var))),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
callback(Term::unmap_data().apply(Term::Var(var))).delay(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
)
|
otherwise.clone(),
|
||||||
|
)
|
||||||
|
.force()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Continue a computation provided that the current term is a Data-wrapped
|
/// Continue a computation provided that the current term is a Data-wrapped
|
||||||
|
@ -574,13 +582,15 @@ impl Term<Name> {
|
||||||
where
|
where
|
||||||
F: FnOnce(Term<Name>) -> Term<Name>,
|
F: FnOnce(Term<Name>) -> Term<Name>,
|
||||||
{
|
{
|
||||||
Term::Var(var.clone()).choose_data(
|
Term::Var(var.clone())
|
||||||
callback(Term::Var(var)),
|
.choose_data(
|
||||||
otherwise.clone(),
|
callback(Term::Var(var)).delay(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
otherwise.clone(),
|
otherwise.clone(),
|
||||||
)
|
otherwise.clone(),
|
||||||
|
)
|
||||||
|
.force()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert an arbitrary 'term' into a bool term and pass it into a 'callback'.
|
/// Convert an arbitrary 'term' into a bool term and pass it into a 'callback'.
|
||||||
|
|
Loading…
Reference in New Issue