From 6bca7a6381984f5b8cedf86dda5a281ff8ea68a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dac=C3=ADk?= Date: Wed, 19 Feb 2025 17:17:52 +0100 Subject: [PATCH] [Tests] Add unfolding tests --- tests/InductiveDefinition_tests.ml | 82 +++++++++++++++++++++++++++++- tests/testables/Builder.ml | 2 +- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/tests/InductiveDefinition_tests.ml b/tests/InductiveDefinition_tests.ml index 4073f52..0c0a86c 100644 --- a/tests/InductiveDefinition_tests.ml +++ b/tests/InductiveDefinition_tests.ml @@ -11,12 +11,14 @@ let ls_def x y = SL.mk_exists' [SL_builtins.loc_ls] (fun [n] -> SL.mk_star [ SL.mk_distinct [x; y]; - SL.mk_pto x y; - SL.mk_predicate "ls" [n; x] + SL.mk_pto x n; + SL.mk_predicate "ls" [n; y] ])] let ls = InductiveDefinition.mk "ls" [Var.x; Var.y] @@ ls_def x y +let sid_ls = InductiveDefinition.ID_map.of_list [("ls", ls)] + let instantiate_test1 () = let actual = InductiveDefinition.instantiate ~refresh:false ls [u; v] in let expected = ls_def u v in @@ -27,11 +29,87 @@ let instantiate_test2 () = let expected = ls_def u v in SL.check_equal actual expected + +(** Unfolding tests *) + +(* TODO: we could test more properties of unfolded formulas *) + +let unfold_test1 () = + let actual = InductiveDefinition.unfold sid_ls ls [x; y] 0 in + let expected = SL.mk_eq [x; y] in + SL.check_equal actual expected + +let unfold_test2 () = + let actual = InductiveDefinition.unfold sid_ls ls [x; y] 1 in + let expected = + SL.mk_or [ + SL.mk_eq [x; y]; + SL.mk_exists' [SL_builtins.loc_ls] (fun [n] -> + SL.mk_star [ + SL.mk_distinct [x; y]; + SL.mk_pto x n; + SL.mk_eq [n; y] + ])] + in + SL.check_equal actual expected + +let unfold_test3 () = + let actual = InductiveDefinition.unfold sid_ls ls [x; y] 2 in + let expected = + SL.mk_or [ + SL.mk_eq [x; y]; + SL.mk_exists' [SL_builtins.loc_ls] (fun [n] -> + SL.mk_star [ + SL.mk_distinct [x; y]; + SL.mk_pto x n; + + SL.mk_or [ + SL.mk_eq [n; y]; + SL.mk_exists' [SL_builtins.loc_ls] (fun [n2] -> + SL.mk_star [ + SL.mk_distinct [n; y]; + SL.mk_pto n n2; + SL.mk_eq [n2; y]; + ])] + ])] + in + SL.check_equal actual expected + +let unfold_test4 () = + let id = InductiveDefinition.map IntroduceIfThenElse.apply ls in + let sid = InductiveDefinition.ID_map.of_list [("ls", id)] in + let actual = Simplifier.simplify @@ InductiveDefinition.unfold sid id [x; y] 0 in + let expected = SL.mk_eq [x; y] in + SL.check_equal actual expected + +let unfold_test5 () = + let id = InductiveDefinition.map IntroduceIfThenElse.apply ls in + let sid = InductiveDefinition.ID_map.of_list [("ls", id)] in + let actual = Simplifier.simplify @@ InductiveDefinition.unfold sid id [x; y] 1 in + let expected = + SL.mk_ite + (SL.mk_eq [x; y]) + (SL.emp) + (SL.mk_exists' [SL_builtins.loc_ls] (fun [n] -> + SL.mk_star [ + SL.mk_pto x n; + SL.mk_eq [n; y] + ])) + in + SL.check_equal actual expected + let () = run "Inductive definitions" [ "instantiate", [ test_case "no refresh" `Quick instantiate_test1; test_case "refresh" `Quick instantiate_test2; ]; + "unfold", [ + test_case "unfold ls, depth: 0" `Quick unfold_test1; + test_case "unfold ls, depth: 1" `Quick unfold_test2; + test_case "unfold ls, depth: 2" `Quick unfold_test3; + test_case "unfold ls-ite, depth: 0" `Quick unfold_test4; + test_case "unfold ls-ite, depth: 1" `Quick unfold_test5; + ]; ] diff --git a/tests/testables/Builder.ml b/tests/testables/Builder.ml index bdf1724..0dff2a1 100644 --- a/tests/testables/Builder.ml +++ b/tests/testables/Builder.ml @@ -20,7 +20,7 @@ module Make (T : TESTABLE_BASE) = struct Alcotest.check' (module T' : TESTABLE with type t = t) ~msg ~actual ~expected let check_distinct lhs rhs = - let msg = Format.asprintf "Objects %s and %s are equal (expected to be distinct)" (T.show lhs) (T.show rhs) in + let msg = Format.asprintf "Objects\n%s\n\nand\n\n%s\n are equal (expected to be distinct)" (T.show lhs) (T.show rhs) in Alcotest.check' Alcotest.bool ~msg ~actual:(T.equal lhs rhs) ~expected:false let check_apply ~input ~expected fn =