Skip to content

Commit 3599b1d

Browse files
committed
Add core theory "if then else" operation
1 parent 2601e6a commit 3599b1d

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,23 @@ TEST_CASE("SMT core theory \"distinct\".", "[core][smt2_incremental]")
104104
smt_bit_vector_constant_termt{2, 8}, smt_bool_literal_termt{true}));
105105
}
106106
}
107+
108+
TEST_CASE("SMT core theory if then else.", "[core][smt2_incremental]")
109+
{
110+
const smt_bool_literal_termt false_term{false};
111+
const smt_bit_vector_constant_termt two{2, 8};
112+
const smt_bit_vector_constant_termt three{2, 8};
113+
const auto if_then_else =
114+
smt_core_theoryt::if_then_else(false_term, two, three);
115+
CHECK(if_then_else.get_sort() == smt_bit_vector_sortt{8});
116+
CHECK(
117+
if_then_else.function_identifier() ==
118+
smt_identifier_termt{"ite", smt_bit_vector_sortt{8}});
119+
REQUIRE(if_then_else.arguments().size() == 3);
120+
CHECK(if_then_else.arguments()[0].get() == false_term);
121+
CHECK(if_then_else.arguments()[1].get() == two);
122+
CHECK(if_then_else.arguments()[2].get() == three);
123+
cbmc_invariants_should_throwt invariants_throw;
124+
CHECK_THROWS(smt_core_theoryt::if_then_else(two, two, three));
125+
CHECK_THROWS(smt_core_theoryt::if_then_else(false_term, false_term, three));
126+
}

0 commit comments

Comments
 (0)