Skip to content

Commit 7192de0

Browse files
committed
Add SMT2 core theory "distinct"
1 parent b6106ba commit 7192de0

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/solvers/smt2_incremental/smt_core_theory.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,26 @@ void smt_core_theoryt::equalt::validate(
4444

4545
const smt_function_application_termt::factoryt<smt_core_theoryt::equalt>
4646
smt_core_theoryt::equal{};
47+
48+
const char *smt_core_theoryt::distinctt::identifier()
49+
{
50+
return "distinct";
51+
}
52+
53+
smt_sortt
54+
smt_core_theoryt::distinctt::return_sort(const smt_termt &, const smt_termt &)
55+
{
56+
return smt_bool_sortt{};
57+
}
58+
59+
void smt_core_theoryt::distinctt::validate(
60+
const smt_termt &lhs,
61+
const smt_termt &rhs)
62+
{
63+
INVARIANT(
64+
lhs.get_sort() == rhs.get_sort(),
65+
"\"distinct\" may only be applied to terms which have the same sort.");
66+
}
67+
68+
const smt_function_application_termt::factoryt<smt_core_theoryt::distinctt>
69+
smt_core_theoryt::distinct{};

src/solvers/smt2_incremental/smt_core_theory.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class smt_core_theoryt
2323
static void validate(const smt_termt &lhs, const smt_termt &rhs);
2424
};
2525
static const smt_function_application_termt::factoryt<equalt> equal;
26+
27+
struct distinctt final
28+
{
29+
static const char *identifier();
30+
static smt_sortt return_sort(const smt_termt &lhs, const smt_termt &rhs);
31+
static void validate(const smt_termt &lhs, const smt_termt &rhs);
32+
};
33+
static const smt_function_application_termt::factoryt<distinctt> distinct;
2634
};
2735

2836
#endif // CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_CORE_THEORY_H

unit/solvers/smt2_incremental/smt_core_theory.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,39 @@ TEST_CASE("SMT core theory \"=\".", "[core][smt2_incremental]")
5353
smt_bit_vector_constant_termt{2, 8}, smt_bool_literal_termt{true}));
5454
}
5555
}
56+
57+
TEST_CASE("SMT core theory \"distinct\".", "[core][smt2_incremental]")
58+
{
59+
SECTION("Bool sorted term comparison")
60+
{
61+
const smt_bool_literal_termt true_term{true};
62+
const smt_bool_literal_termt false_term{false};
63+
const auto bool_comparison =
64+
smt_core_theoryt::distinct(true_term, false_term);
65+
CHECK(bool_comparison.get_sort() == smt_bool_sortt{});
66+
REQUIRE(bool_comparison.arguments().size() == 2);
67+
REQUIRE(bool_comparison.arguments()[0].get() == true_term);
68+
REQUIRE(bool_comparison.arguments()[1].get() == false_term);
69+
}
70+
71+
SECTION("Bit vector sorted term comparison")
72+
{
73+
const smt_bit_vector_constant_termt two{2, 8};
74+
const smt_bit_vector_constant_termt three{3, 8};
75+
const auto bit_vector_comparison = smt_core_theoryt::distinct(two, three);
76+
CHECK(bit_vector_comparison.get_sort() == smt_bool_sortt{});
77+
REQUIRE(bit_vector_comparison.arguments().size() == 2);
78+
REQUIRE(bit_vector_comparison.arguments()[0].get() == two);
79+
REQUIRE(bit_vector_comparison.arguments()[1].get() == three);
80+
}
81+
82+
SECTION("Mismatch sort invariant")
83+
{
84+
cbmc_invariants_should_throwt invariants_throw;
85+
CHECK_THROWS(smt_core_theoryt::distinct(
86+
smt_bit_vector_constant_termt{2, 8},
87+
smt_bit_vector_constant_termt{2, 16}));
88+
CHECK_THROWS(smt_core_theoryt::distinct(
89+
smt_bit_vector_constant_termt{2, 8}, smt_bool_literal_termt{true}));
90+
}
91+
}

0 commit comments

Comments
 (0)