Rework generate_raw to avoid need to intern in prop tests

Also, this commit makes `apply_term` automatically re-intern the
  program since it isn't safe to apply any term onto a UPLC program. In
  particular, terms that introduce new let-bindings (via lambdas) will
  mess with the already generated DeBruijn indices.

  The problem doesn't occur for pure constant terms like Data. So we
  still have a safe and fast version 'apply_data' when needed.
This commit is contained in:
KtorZ
2024-03-03 19:24:35 +01:00
parent 1134b8d7d0
commit 30841fe000
9 changed files with 127 additions and 120 deletions

View File

@@ -7,6 +7,7 @@ use crate::{
eval_result::EvalResult,
Machine,
},
parser::interner::Interner,
};
use num_bigint::BigInt;
use num_traits::ToPrimitive;
@@ -18,7 +19,6 @@ use pallas::ledger::{
},
traverse::ComputeHash,
};
use serde::{
self,
de::{self, Deserialize, Deserializer, MapAccess, Visitor},
@@ -58,21 +58,8 @@ where
}
}
/// We use this to apply the validator to Datum,
/// then redeemer, then ScriptContext. If datum is
/// even necessary (i.e. minting policy).
pub fn apply_term(&self, term: &Term<T>) -> Self {
let applied_term = Term::Apply {
function: Rc::new(self.term.clone()),
argument: Rc::new(term.clone()),
};
Program {
version: self.version,
term: applied_term,
}
}
/// A convenient and faster version that `apply_term` since the program doesn't need to be
/// re-interned (constant Data do not introduce new bindings).
pub fn apply_data(&self, plutus_data: PlutusData) -> Self {
let applied_term = Term::Apply {
function: Rc::new(self.term.clone()),
@@ -86,6 +73,27 @@ where
}
}
impl Program<Name> {
/// We use this to apply the validator to Datum,
/// then redeemer, then ScriptContext. If datum is
/// even necessary (i.e. minting policy).
pub fn apply_term(&self, term: &Term<Name>) -> Self {
let applied_term = Term::Apply {
function: Rc::new(self.term.clone()),
argument: Rc::new(term.clone()),
};
let mut program = Program {
version: self.version,
term: applied_term,
};
Interner::new().program(&mut program);
program
}
}
impl<'a, T> Display for Program<T>
where
T: Binder<'a>,