diff --git a/Cargo.lock b/Cargo.lock index 7bcd6344..8761dbf5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2735,7 +2735,6 @@ dependencies = [ "peg", "pretty", "pretty_assertions", - "proptest", "secp256k1", "serde", "serde_json", diff --git a/crates/uplc/Cargo.toml b/crates/uplc/Cargo.toml index e729ac64..48fb7c3a 100644 --- a/crates/uplc/Cargo.toml +++ b/crates/uplc/Cargo.toml @@ -42,7 +42,6 @@ thiserror = "1.0.39" [dev-dependencies] hex = "0.4.3" indoc = "2.0.1" -proptest = "1.1.0" [features] default = ["dep:secp256k1"] diff --git a/crates/uplc/examples/identity_builder_ext.rs b/crates/uplc/examples/identity_builder_ext.rs deleted file mode 100644 index 71e6e9c2..00000000 --- a/crates/uplc/examples/identity_builder_ext.rs +++ /dev/null @@ -1,15 +0,0 @@ -use uplc::program_builder::{Builder, WithLambda, WithTerm, WithVar}; - -trait WithIdentity: WithTerm + WithLambda + WithVar { - fn with_identity(self, name_str: &str) -> Self::Next { - self.with_lambda(name_str).with_var(name_str) - } -} - -impl WithIdentity for T {} - -fn main() { - let my_var = "some_var"; - let program = Builder::start(1, 2, 3).with_identity(my_var).build_named(); - println!("{program:#?}"); -} diff --git a/crates/uplc/proptest-regressions/program_builder/constant.txt b/crates/uplc/proptest-regressions/program_builder/constant.txt deleted file mode 100644 index 574947f8..00000000 --- a/crates/uplc/proptest-regressions/program_builder/constant.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Seeds for failure cases proptest has generated in the past. It is -# automatically read and these particular cases re-run before any -# novel cases are generated. -# -# It is recommended to check this file in to source control so that -# everyone who runs the test benefits from these saved cases. -cc a6cf04d97e92892f9c9342f5df205d81b7d686231ba69c9f36ba1166bd21d9a7 # shrinks to int = 0 -cc ccfa96fb2d6133adc65044ad94e69d282fa104c68128782ecfa2badb8c79b12c # shrinks to some_string = "\"" diff --git a/crates/uplc/proptest-regressions/program_builder/tests.txt b/crates/uplc/proptest-regressions/program_builder/tests.txt deleted file mode 100644 index 73181782..00000000 --- a/crates/uplc/proptest-regressions/program_builder/tests.txt +++ /dev/null @@ -1,7 +0,0 @@ -# Seeds for failure cases proptest has generated in the past. It is -# automatically read and these particular cases re-run before any -# novel cases are generated. -# -# It is recommended to check this file in to source control so that -# everyone who runs the test benefits from these saved cases. -cc db95a6ed96ee7cee987ba4b71bc6ffa9cf5abe295fa57a0eade27ed47335c1a3 # shrinks to maj = 0, min = 0, patch = 0 diff --git a/crates/uplc/src/lib.rs b/crates/uplc/src/lib.rs index 1d894a8f..8f3d7800 100644 --- a/crates/uplc/src/lib.rs +++ b/crates/uplc/src/lib.rs @@ -7,7 +7,6 @@ pub mod machine; pub mod optimize; pub mod parser; mod pretty; -pub mod program_builder; pub mod tx; pub use pallas_codec::utils::KeyValuePairs; diff --git a/crates/uplc/src/program_builder.rs b/crates/uplc/src/program_builder.rs deleted file mode 100644 index 44ba1932..00000000 --- a/crates/uplc/src/program_builder.rs +++ /dev/null @@ -1,110 +0,0 @@ -#![cfg_attr(test, allow(non_snake_case))] - -use crate::ast::{Name, Program, Term, Unique}; -use std::cell::RefCell; -use std::collections::HashMap; - -#[cfg(test)] -mod tests; - -mod apply; -mod builtin; -mod constant; -mod delay; -mod error; -mod force; -mod lambda; -mod var; - -pub use apply::*; -pub use builtin::*; -pub use constant::*; -pub use delay::*; -pub use error::*; -pub use force::*; -pub use lambda::*; -pub use var::*; - -pub struct Builder { - version: (usize, usize, usize), - term: Term, -} - -struct Context { - next_unique: isize, - names: HashMap, -} - -impl Context { - pub fn new() -> Context { - Context { - next_unique: 0, - names: HashMap::new(), - } - } - - pub fn get_name(&mut self, name_str: &str) -> Name { - if let Some(unique) = self.names.get(name_str) { - Name { - text: name_str.to_string(), - unique: *unique, - } - } else { - let next_unique = self.next_unique; - self.next_unique = next_unique + 1; - let unique = Unique::new(next_unique); - self.names.insert(name_str.to_string(), unique); - Name { - text: name_str.to_string(), - unique, - } - } - } -} - -pub struct Core { - version: (usize, usize, usize), - ctx: RefCell, -} - -pub trait WithTerm -where - Self: Sized, -{ - type Next; - - fn next(self, term: Term) -> Self::Next; - fn get_name(&self, name_str: &str) -> Name; -} - -impl WithTerm for Core { - type Next = Builder; - fn next(self, term: Term) -> Self::Next { - Builder { - version: self.version, - term, - } - } - - fn get_name(&self, name_str: &str) -> Name { - let mut ctx = self.ctx.borrow_mut(); - ctx.get_name(name_str) - } -} - -impl Builder { - /// Max: `9223372036854775807` - pub fn start(maj: usize, min: usize, patch: usize) -> Core { - Core { - version: (maj, min, patch), - ctx: RefCell::new(Context::new()), - } - } - - pub fn build_named(&self) -> Program { - Program { - version: self.version, - term: self.term.clone(), - } - } -} diff --git a/crates/uplc/src/program_builder/apply.rs b/crates/uplc/src/program_builder/apply.rs deleted file mode 100644 index 7170df7c..00000000 --- a/crates/uplc/src/program_builder/apply.rs +++ /dev/null @@ -1,97 +0,0 @@ -use std::rc::Rc; - -use crate::ast::{Name, Term}; -use crate::program_builder::WithTerm; - -pub struct ApplyBuilderFunction { - outer: T, -} - -pub struct ApplyBuilderArgument { - outer: T, - function: Term, -} - -impl WithTerm for ApplyBuilderFunction { - type Next = ApplyBuilderArgument; - - fn next(self, term: Term) -> Self::Next { - ApplyBuilderArgument { - outer: self.outer, - function: term, - } - } - - fn get_name(&self, name_str: &str) -> Name { - self.outer.get_name(name_str) - } -} - -impl WithTerm for ApplyBuilderArgument { - type Next = T::Next; - - fn next(self, term: Term) -> Self::Next { - let term = Term::Apply { - function: Rc::new(self.function), - argument: Rc::new(term), - }; - self.outer.next(term) - } - - fn get_name(&self, name_str: &str) -> Name { - self.outer.get_name(name_str) - } -} - -pub trait WithApply: WithTerm { - fn with_apply(self) -> ApplyBuilderFunction { - ApplyBuilderFunction { outer: self } - } -} - -// This is a naive blanket impl. If needed, we can control which states of the builder can -// call this by implementing manually. -impl WithApply for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::{Builder, WithConstant, WithLambda, WithVar}; - - #[test] - fn build_named__with_apply() { - let my_var = "i_0"; - let code = r"(program - 1.2.3 - [(lam i_0 i_0) (con integer 1)] - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_apply() - .with_lambda(my_var) - .with_var(my_var) - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } - - #[test] - fn build_named__with_apply__with_lambda_as_arg() { - let my_var = "i_0"; - let their_var = "i_1"; - let code = r"(program - 1.2.3 - [(lam i_0 i_0) (lam i_1 (con integer 1))] - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_apply() - .with_lambda(my_var) - .with_var(my_var) - .with_lambda(their_var) - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/builtin.rs b/crates/uplc/src/program_builder/builtin.rs deleted file mode 100644 index 300a8647..00000000 --- a/crates/uplc/src/program_builder/builtin.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::ast::Term; -use crate::builtins::DefaultFunction; -use crate::program_builder::WithTerm; - -pub trait WithBuiltin: WithTerm { - // TODO: Add all the builtin variants explicitly - fn with_builtin(self, builtin: DefaultFunction) -> Self::Next { - let term = Term::Builtin(builtin); - self.next(term) - } -} - -impl WithBuiltin for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::Builder; - - #[test] - fn build_named() { - let code = r"(program - 11.22.33 - (builtin addInteger) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(11, 22, 33) - .with_builtin(DefaultFunction::AddInteger) - .build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/constant.rs b/crates/uplc/src/program_builder/constant.rs deleted file mode 100644 index 09257da0..00000000 --- a/crates/uplc/src/program_builder/constant.rs +++ /dev/null @@ -1,135 +0,0 @@ -use crate::ast::{Constant, Term}; -use crate::program_builder::WithTerm; - -pub trait WithConstant: WithTerm { - fn with_int(self, int: i128) -> Self::Next { - let term = Term::Constant(Constant::Integer(int.into()).into()); - self.next(term) - } - - fn with_byte_string(self, bytes: Vec) -> Self::Next { - let term = Term::Constant(Constant::ByteString(bytes).into()); - self.next(term) - } - - fn with_string(self, string: String) -> Self::Next { - let term = Term::Constant(Constant::String(string).into()); - self.next(term) - } - - fn with_unit(self) -> Self::Next { - let term = Term::Constant(Constant::Unit.into()); - self.next(term) - } - - fn with_bool(self, bool: bool) -> Self::Next { - let term = Term::Constant(Constant::Bool(bool).into()); - self.next(term) - } -} - -// This is a naive blanket impl. If needed, we can control which states of the builder can -// call this by implementing manually. -impl WithConstant for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::Builder; - use proptest::prelude::*; - - proptest! { - #[test] - fn build_named__with_const( - int: i128 - ) { - let code = format!(r"(program - 11.22.33 - (con integer {int}) - )"); - let expected = parser::program(&code).unwrap(); - let actual = Builder::start(11, 22, 33).with_int(int).build_named(); - assert_eq!(expected, actual); - } - } - - proptest! { - #[test] - fn build_named__with_bytestring( - bytes: Vec - ) { - let bstring = hex::encode(&bytes); - let code = format!(r"(program - 11.22.33 - (con bytestring #{bstring}) - )"); - let expected = parser::program(&code).unwrap(); - let actual = Builder::start(11, 22, 33) - .with_byte_string(bytes) - .build_named(); - assert_eq!(expected, actual); - } - } - - prop_compose! { - fn safe_string()( - some_string: String - ) -> String { - some_string.chars().filter(|a| *a != '\"').collect() - } - } - - proptest! { - #[test] - fn build_named__with_string( - some_string in safe_string() - ) { - let code = format!( - r#"(program - 11.22.33 - (con string "{}") - )"#, - &some_string - ); - let expected = parser::program(&code).unwrap(); - let actual = Builder::start(11, 22, 33) - .with_string(some_string) - .build_named(); - assert_eq!(expected, actual); - } - } - - #[test] - fn build_named__with_unit() { - let code = r"(program - 11.22.33 - (con unit ()) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(11, 22, 33).with_unit().build_named(); - assert_eq!(expected, actual); - } - - #[test] - fn build_named__with_true() { - let code = r"(program - 11.22.33 - (con bool True) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(11, 22, 33).with_bool(true).build_named(); - assert_eq!(expected, actual); - } - - #[test] - fn build_named__with_false() { - let code = r"(program - 11.22.33 - (con bool False) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(11, 22, 33).with_bool(false).build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/delay.rs b/crates/uplc/src/program_builder/delay.rs deleted file mode 100644 index e991391b..00000000 --- a/crates/uplc/src/program_builder/delay.rs +++ /dev/null @@ -1,67 +0,0 @@ -use std::rc::Rc; - -use crate::ast::{Name, Term}; -use crate::program_builder::WithTerm; - -pub struct DelayBuilder { - outer: T, -} - -impl WithTerm for DelayBuilder { - type Next = T::Next; - - fn next(self, term: Term) -> Self::Next { - let term = Term::Delay(Rc::new(term)); - self.outer.next(term) - } - - fn get_name(&self, name_str: &str) -> Name { - self.outer.get_name(name_str) - } -} - -pub trait WithDelay: WithTerm { - fn with_delay(self) -> DelayBuilder { - DelayBuilder { outer: self } - } -} - -// This is a naive blanket impl. If needed, we can control which states of the builder can -// call this by implementing manually. -impl WithDelay for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::{Builder, WithConstant, WithLambda}; - - #[test] - fn build_named__with_delay() { - let code = r"(program - 1.2.3 - (delay (con integer 1)) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_delay() - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } - - #[test] - fn build_named__with_delay__with_lambda() { - let code = r"(program - 1.2.3 - (delay (lam i_0 (con integer 1))) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_delay() - .with_lambda("i_0") - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/error.rs b/crates/uplc/src/program_builder/error.rs deleted file mode 100644 index b5bed3c5..00000000 --- a/crates/uplc/src/program_builder/error.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::ast::Term; -use crate::program_builder::WithTerm; - -pub trait WithError: WithTerm { - fn with_error(self) -> Self::Next { - let term = Term::Error; - self.next(term) - } -} - -impl WithError for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::Builder; - - #[test] - fn build_named__with_error() { - let code = r"(program - 11.22.33 - (error) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(11, 22, 33).with_error().build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/force.rs b/crates/uplc/src/program_builder/force.rs deleted file mode 100644 index b2b7a1af..00000000 --- a/crates/uplc/src/program_builder/force.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::rc::Rc; - -use crate::ast::{Name, Term}; -use crate::program_builder::WithTerm; - -pub struct ForceBuilder { - outer: T, -} - -impl WithTerm for ForceBuilder { - type Next = T::Next; - - fn next(self, term: Term) -> Self::Next { - let term = Term::Force(Rc::new(term)); - self.outer.next(term) - } - - fn get_name(&self, name_str: &str) -> Name { - self.outer.get_name(name_str) - } -} - -pub trait WithForce: WithTerm { - fn with_force(self) -> ForceBuilder { - ForceBuilder { outer: self } - } -} - -// This is a naive blanket impl. If needed, we can control which states of the builder can -// call this by implementing manually. -impl WithForce for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::{Builder, WithConstant, WithLambda}; - - #[test] - fn build_named__with_force() { - let code = r"(program - 1.2.3 - (force (lam i_0 (con integer 1))) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_force() - .with_lambda("i_0") - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/lambda.rs b/crates/uplc/src/program_builder/lambda.rs deleted file mode 100644 index 5b92b64b..00000000 --- a/crates/uplc/src/program_builder/lambda.rs +++ /dev/null @@ -1,76 +0,0 @@ -use std::rc::Rc; - -use crate::ast::{Name, Term}; -use crate::program_builder::WithTerm; - -pub struct LambdaBuilder { - outer: T, - parameter_name: Name, -} - -impl WithTerm for LambdaBuilder { - type Next = T::Next; - - fn next(self, term: Term) -> Self::Next { - let term = Term::Lambda { - parameter_name: self.parameter_name.into(), - body: Rc::new(term), - }; - self.outer.next(term) - } - - fn get_name(&self, name_str: &str) -> Name { - self.outer.get_name(name_str) - } -} - -pub trait WithLambda: WithTerm { - fn with_lambda(self, name_str: &str) -> LambdaBuilder { - let parameter_name = self.get_name(name_str); - LambdaBuilder { - outer: self, - parameter_name, - } - } -} - -// This is a naive blanket impl. If needed, we can control which states of the builder can -// call this by implementing manually. -impl WithLambda for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::constant::WithConstant; - use crate::program_builder::Builder; - - #[test] - fn build_named__with_lam() { - let code = r"(program - 1.2.3 - (lam i_0 (con integer 1)) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_lambda("i_0") - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } - - #[test] - fn build_named__with_nested_lam() { - let code = r"(program - 1.2.3 - (lam i_0 (lam i_1 (con integer 1))) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_lambda("i_0") - .with_lambda("i_1") - .with_int(1) - .build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/tests.rs b/crates/uplc/src/program_builder/tests.rs deleted file mode 100644 index 3fbd66d5..00000000 --- a/crates/uplc/src/program_builder/tests.rs +++ /dev/null @@ -1,33 +0,0 @@ -use super::*; -use crate::parser; -use crate::program_builder::constant::WithConstant; -use proptest::prelude::*; - -prop_compose! { - fn arb_version()( - maj: isize, - min: isize, - patch: isize, - ) -> (usize, usize, usize){ - let maj = maj.unsigned_abs(); - let min = min.unsigned_abs(); - let patch = patch.unsigned_abs(); - - (maj, min, patch) - } -} - -proptest! { - #[test] - fn build_named__with_version( - (maj, min, patch) in arb_version(), - ) { - let code = format!(r"(program - {maj}.{min}.{patch} - (con integer 11) - )"); - let expected = parser::program(&code).unwrap(); - let actual = Builder::start(maj, min, patch).with_int(11).build_named(); - assert_eq!(expected, actual); - } -} diff --git a/crates/uplc/src/program_builder/var.rs b/crates/uplc/src/program_builder/var.rs deleted file mode 100644 index 60e4ab84..00000000 --- a/crates/uplc/src/program_builder/var.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::ast::Term; -use crate::program_builder::WithTerm; - -pub trait WithVar: WithTerm { - fn with_var(self, name_str: &str) -> Self::Next { - let name = self.get_name(name_str); - let term = Term::Var(name.into()); - self.next(term) - } -} - -// This is a naive blanket impl. If needed, we can control which states of the builder can -// call this by implementing manually. -impl WithVar for T {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser; - use crate::program_builder::{Builder, WithLambda}; - - #[test] - fn build_named__with_var() { - let var_name = "i_0"; - let code = r"(program - 1.2.3 - (lam i_0 i_0) - )"; - let expected = parser::program(code).unwrap(); - let actual = Builder::start(1, 2, 3) - .with_lambda(var_name) - .with_var(var_name) - .build_named(); - assert_eq!(expected, actual); - } -}