Skip to content

Commit 0e422b8

Browse files
committed
Add core theory if then else
1 parent 7192de0 commit 0e422b8

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/solvers/smt2_incremental/smt_core_theory.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,32 @@ void smt_core_theoryt::distinctt::validate(
6767

6868
const smt_function_application_termt::factoryt<smt_core_theoryt::distinctt>
6969
smt_core_theoryt::distinct{};
70+
71+
const char *smt_core_theoryt::if_then_elset::identifier()
72+
{
73+
return "ite";
74+
}
75+
76+
smt_sortt smt_core_theoryt::if_then_elset::return_sort(
77+
const smt_termt &,
78+
const smt_termt &then_term,
79+
const smt_termt &)
80+
{
81+
return then_term.get_sort();
82+
}
83+
84+
void smt_core_theoryt::if_then_elset::validate(
85+
const smt_termt &condition,
86+
const smt_termt &then_term,
87+
const smt_termt &else_term)
88+
{
89+
INVARIANT(
90+
condition.get_sort() == smt_bool_sortt{},
91+
"Condition term must have bool sort.");
92+
INVARIANT(
93+
then_term.get_sort() == else_term.get_sort(),
94+
"\"ite\" must have \"then\" and \"else\" terms of the same sort.");
95+
}
96+
97+
const smt_function_application_termt::factoryt<smt_core_theoryt::if_then_elset>
98+
smt_core_theoryt::if_then_else{};

src/solvers/smt2_incremental/smt_core_theory.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ class smt_core_theoryt
3131
static void validate(const smt_termt &lhs, const smt_termt &rhs);
3232
};
3333
static const smt_function_application_termt::factoryt<distinctt> distinct;
34+
35+
struct if_then_elset final
36+
{
37+
static const char *identifier();
38+
static smt_sortt return_sort(
39+
const smt_termt &condition,
40+
const smt_termt &then_term,
41+
const smt_termt &else_term);
42+
static void validate(
43+
const smt_termt &condition,
44+
const smt_termt &then_term,
45+
const smt_termt &else_term);
46+
};
47+
static const smt_function_application_termt::factoryt<if_then_elset>
48+
if_then_else;
3449
};
3550

3651
#endif // CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_CORE_THEORY_H

unit/solvers/smt2_incremental/smt_core_theory.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,20 @@ TEST_CASE("SMT core theory \"distinct\".", "[core][smt2_incremental]")
8989
smt_bit_vector_constant_termt{2, 8}, smt_bool_literal_termt{true}));
9090
}
9191
}
92+
93+
TEST_CASE("SMT core theory if then else.", "[core][smt2_incremental]")
94+
{
95+
const smt_bool_literal_termt false_term{false};
96+
const smt_bit_vector_constant_termt two{2, 8};
97+
const smt_bit_vector_constant_termt three{2, 8};
98+
const auto if_then_else =
99+
smt_core_theoryt::if_then_else(false_term, two, three);
100+
CHECK(if_then_else.get_sort() == smt_bit_vector_sortt{8});
101+
REQUIRE(if_then_else.arguments().size() == 3);
102+
CHECK(if_then_else.arguments()[0].get() == false_term);
103+
CHECK(if_then_else.arguments()[1].get() == two);
104+
CHECK(if_then_else.arguments()[2].get() == three);
105+
cbmc_invariants_should_throwt invariants_throw;
106+
CHECK_THROWS(smt_core_theoryt::if_then_else(two, two, three));
107+
CHECK_THROWS(smt_core_theoryt::if_then_else(false_term, false_term, three));
108+
}

0 commit comments

Comments
 (0)