Factor out common UPLC logic for converting Data to Bool.

This commit is contained in:
KtorZ
2024-08-01 10:42:43 +02:00
committed by Kasey
parent 05504b9762
commit c3a61706b5
2 changed files with 69 additions and 75 deletions

View File

@@ -484,4 +484,35 @@ impl Term<Name> {
.lambda("__constr_var"),
)
}
/// Convert an arbitrary 'term' into a bool term and pass it into a 'callback'.
/// Continue the execution 'otherwise' with a different branch.
///
/// Note that the 'otherwise' term as well as the callback's result are expected
/// to be delayed terms.
pub fn unwrap_bool_or<F>(term: Term<Name>, callback: F, otherwise: &Term<Name>) -> Term<Name>
where
F: FnOnce(Term<Name>) -> Term<Name>,
{
Term::snd_pair()
.apply(Term::var("__pair__"))
.choose_list(
Term::less_than_equals_integer()
.apply(Term::integer(2.into()))
.apply(Term::fst_pair().apply(Term::var("__pair__")))
.delayed_if_then_else(
otherwise.clone(),
callback(
Term::equals_integer()
.apply(Term::fst_pair().apply(Term::var("__pair__")))
.apply(Term::integer(1.into())),
),
)
.delay(),
otherwise.clone(),
)
.force()
.lambda("__pair__")
.apply(Term::unconstr_data().apply(term))
}
}