Skip to content

Commit

Permalink
SMT2: implement cond
Browse files Browse the repository at this point in the history
This adds support for cond_exprt, via a lowering to nested ?:, to the SMT
backend.
  • Loading branch information
kroening committed Dec 17, 2024
1 parent fa299c5 commit 19958fe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/solvers/smt2/smt2_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,11 @@ void smt2_convt::convert_expr(const exprt &expr)
out << ')';
}
}
else if(expr.id() == ID_cond)

Check warning on line 2493 in src/solvers/smt2/smt2_conv.cpp

View check run for this annotation

Codecov / codecov/patch

src/solvers/smt2/smt2_conv.cpp#L2493

Added line #L2493 was not covered by tests
{
// use the lowering
convert_expr(to_cond_expr(expr).lower());

Check warning on line 2496 in src/solvers/smt2/smt2_conv.cpp

View check run for this annotation

Codecov / codecov/patch

src/solvers/smt2/smt2_conv.cpp#L2496

Added line #L2496 was not covered by tests
}
else
INVARIANT_WITH_DIAGNOSTICS(
false,
Expand Down
29 changes: 29 additions & 0 deletions src/util/std_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,32 @@ exprt binding_exprt::instantiate(const variablest &new_variables) const
values.push_back(new_variable);
return instantiate(values);
}

exprt cond_exprt::lower() const

Check warning on line 264 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L264

Added line #L264 was not covered by tests
{
INVARIANT(

Check warning on line 266 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L266

Added line #L266 was not covered by tests
operands().size() % 2 == 0, "cond must have even number of operands");

exprt result = nil_exprt();

Check warning on line 269 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L269

Added line #L269 was not covered by tests

auto &operands = this->operands();

Check warning on line 271 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L271

Added line #L271 was not covered by tests

// functional version -- go backwards
for(std::size_t i = operands.size(); i != 0; i -= 2)

Check warning on line 274 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L274

Added line #L274 was not covered by tests
{
INVARIANT(

Check warning on line 276 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L276

Added line #L276 was not covered by tests
i >= 2,
"since the number of operands is even if i is nonzero it must be "
"greater than two");

const exprt &cond = operands[i - 2];
const exprt &value = operands[i - 1];

Check warning on line 282 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L281-L282

Added lines #L281 - L282 were not covered by tests

if(result.is_nil())
result = value;

Check warning on line 285 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L284-L285

Added lines #L284 - L285 were not covered by tests
else
result = if_exprt{cond, value, std::move(result)};

Check warning on line 287 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L287

Added line #L287 was not covered by tests
}

return result;

Check warning on line 290 in src/util/std_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/std_expr.cpp#L290

Added line #L290 was not covered by tests
}
3 changes: 3 additions & 0 deletions src/util/std_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,9 @@ class cond_exprt : public multi_ary_exprt
operands().push_back(condition);
operands().push_back(value);
}

// a lowering to nested if_exprt
exprt lower() const;
};

template <>
Expand Down

0 comments on commit 19958fe

Please sign in to comment.