From 1567e42875b2f5a4b68485ec17a4d13b50e056fe Mon Sep 17 00:00:00 2001 From: microproofs Date: Fri, 17 Nov 2023 19:52:03 -0500 Subject: [PATCH] chore: fill in machine todos and cost model for case and constr This allows for several more tests to pass **Had to remove case-7 since it was incorrectly passing before** --- crates/uplc/src/machine.rs | 7 +++++-- crates/uplc/src/machine/cost_model.rs | 8 ++++---- crates/uplc/src/machine/error.rs | 5 +++++ crates/uplc/src/parser.rs | 2 +- .../conformance/evaluation/term/case/case-1/case-1.uplc | 4 ++++ .../evaluation/term/case/case-1/case-1.uplc.expected | 1 + .../conformance/evaluation/term/case/case-2/case-2.uplc | 4 ++++ .../evaluation/term/case/case-2/case-2.uplc.expected | 1 + .../conformance/evaluation/term/case/case-3/case-3.uplc | 4 ++++ .../evaluation/term/case/case-3/case-3.uplc.expected | 1 + .../conformance/evaluation/term/case/case-4/case-4.uplc | 4 ++++ .../evaluation/term/case/case-4/case-4.uplc.expected | 1 + .../conformance/evaluation/term/case/case-5/case-5.uplc | 4 ++++ .../evaluation/term/case/case-5/case-5.uplc.expected | 1 + .../conformance/evaluation/term/case/case-7/case-7.uplc | 4 ---- .../evaluation/term/case/case-7/case-7.uplc.expected | 1 - .../conformance/evaluation/term/case/case-8/case-8.uplc | 4 ++++ .../evaluation/term/case/case-8/case-8.uplc.expected | 1 + .../conformance/evaluation/term/case/case-9/case-9.uplc | 4 ++++ .../evaluation/term/case/case-9/case-9.uplc.expected | 1 + .../evaluation/term/constr/constr-1/constr-1.uplc | 4 ++++ .../term/constr/constr-1/constr-1.uplc.expected | 1 + .../evaluation/term/constr/constr-2/constr-2.uplc | 4 ++++ .../term/constr/constr-2/constr-2.uplc.expected | 1 + 24 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected delete mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc delete mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected create mode 100644 crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc create mode 100644 crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 81cd44ae..f6598d9a 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -240,9 +240,12 @@ impl Machine { env, t.clone(), )), - None => todo!(), + None => Err(Error::MissingCaseBranch( + branches, + Value::Constr { tag, fields }, + )), }, - _ => todo!("return a proper evaluation error"), + v => Err(Error::NonConstrScrutinized(v)), }, } } diff --git a/crates/uplc/src/machine/cost_model.rs b/crates/uplc/src/machine/cost_model.rs index 6b900258..2701fc0c 100644 --- a/crates/uplc/src/machine/cost_model.rs +++ b/crates/uplc/src/machine/cost_model.rs @@ -186,12 +186,12 @@ impl Default for MachineCosts { }, // Placeholder values constr: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, case: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, } } diff --git a/crates/uplc/src/machine/error.rs b/crates/uplc/src/machine/error.rs index 468cb518..bac398ca 100644 --- a/crates/uplc/src/machine/error.rs +++ b/crates/uplc/src/machine/error.rs @@ -20,6 +20,10 @@ pub enum Error { NonPolymorphicInstantiation(Value), #[error("Attempted to apply a non-function:\n\n{0:#?} to argument:\n\n{1:#?}")] NonFunctionalApplication(Value, Value), + #[error("Attempted to case a non-const:\n\n{0:#?}")] + NonConstrScrutinized(Value), + #[error("Cases: {0:#?}\n\n are missing branch for constr:\n\n{1:#?}")] + MissingCaseBranch(Vec>, Value), #[error("Type mismatch expected '{0}' got '{1}'")] TypeMismatch(Type, Type), #[error("Type mismatch expected '(list a)' got '{0}'")] @@ -36,6 +40,7 @@ pub enum Error { NotAConstant(Value), #[error("The evaluation never reached a final state")] MachineNeverReachedDone, + #[error("Decoding utf8")] Utf8(#[from] FromUtf8Error), #[error("Out of Bounds\n\nindex: {}\nbytestring: {}\npossible: 0 - {}", .0, hex::encode(.1), .1.len() - 1)] diff --git a/crates/uplc/src/parser.rs b/crates/uplc/src/parser.rs index 79451837..29adc8d7 100644 --- a/crates/uplc/src/parser.rs +++ b/crates/uplc/src/parser.rs @@ -145,7 +145,7 @@ peg::parser! { } rule case(interner: &mut Interner) -> Term - = "(" _* "case" _+ constr:term(interner) _* branches:(t:term(interner) _* { t })+ _* ")" { + = "(" _* "case" _+ constr:term(interner) _* branches:(t:term(interner) _* { t })* _* ")" { Term::Case { constr: constr.into(), branches } } diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc new file mode 100644 index 00000000..a08de31c --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc @@ -0,0 +1,4 @@ +-- select first branch +(program 1.1.0 + (case (constr 0 (con integer 0)) (lam x (con integer 1)) (lam x (con integer 2))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected new file mode 100644 index 00000000..c897a619 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-1/case-1.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 1)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc new file mode 100644 index 00000000..b9861165 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc @@ -0,0 +1,4 @@ +-- select second branch +(program 1.1.0 + (case (constr 1 (con integer 0)) (lam x (con integer 1)) (lam x (con integer 2))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected new file mode 100644 index 00000000..2b513f80 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-2/case-2.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 2)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc new file mode 100644 index 00000000..a4399f95 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc @@ -0,0 +1,4 @@ +-- select first branch and do computation with the args +(program 1.1.0 + (case (constr 0 (con integer 3) (con integer 2)) (lam x (lam y [(builtin addInteger) x y])) (lam x (lam y [(builtin subtractInteger) x y]))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected new file mode 100644 index 00000000..307ccca4 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-3/case-3.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 5)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc new file mode 100644 index 00000000..e471f4ec --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc @@ -0,0 +1,4 @@ +-- select second branch and do computation with the args +(program 1.1.0 + (case (constr 1 (con integer 3) (con integer 2)) (lam x (lam y [(builtin addInteger) x y])) (lam x (lam y [(builtin subtractInteger) x y]))) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected new file mode 100644 index 00000000..c897a619 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-4/case-4.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 1)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc new file mode 100644 index 00000000..5478b918 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc @@ -0,0 +1,4 @@ +-- case of non-constr +(program 1.1.0 + (case (con integer 1) (lam x x) (lam x x)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected new file mode 100644 index 00000000..ccc477ff --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-5/case-5.uplc.expected @@ -0,0 +1 @@ +evaluation failure \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc deleted file mode 100644 index 94153d58..00000000 --- a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc +++ /dev/null @@ -1,4 +0,0 @@ --- case can't be used before 1.1.0 -(program 1.0.0 - (case (con integer 1)) -) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected deleted file mode 100644 index fd569489..00000000 --- a/crates/uplc/test_data/conformance/evaluation/term/case/case-7/case-7.uplc.expected +++ /dev/null @@ -1 +0,0 @@ -parse error \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc new file mode 100644 index 00000000..5aae469b --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc @@ -0,0 +1,4 @@ +-- nullary case +(program 1.1.0 + (case (constr 0) (con integer 1) (con integer 2)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected new file mode 100644 index 00000000..c897a619 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-8/case-8.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (con integer 1)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc new file mode 100644 index 00000000..73b0719b --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc @@ -0,0 +1,4 @@ +-- empty case, aka -XEmptyCase +(program 1.1.0 + (case (constr 0)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected new file mode 100644 index 00000000..ccc477ff --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/case/case-9/case-9.uplc.expected @@ -0,0 +1 @@ +evaluation failure \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc new file mode 100644 index 00000000..9c413fa7 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc @@ -0,0 +1,4 @@ +-- empty constr +(program 1.1.0 + (constr 0 ) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected new file mode 100644 index 00000000..a65a6aca --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-1/constr-1.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (constr 0)) \ No newline at end of file diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc new file mode 100644 index 00000000..a27c1c66 --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc @@ -0,0 +1,4 @@ +-- constr with an argument +(program 1.1.0 + (constr 0 (con integer 1)) +) diff --git a/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected new file mode 100644 index 00000000..fafd655d --- /dev/null +++ b/crates/uplc/test_data/conformance/evaluation/term/constr/constr-2/constr-2.uplc.expected @@ -0,0 +1 @@ +(program 1.1.0 (constr 0 (con integer 1))) \ No newline at end of file