Skip to content

Commit 3b062c6

Browse files
committed
Add lambda_exprt to provide an API for exprt(ID_lambda)
We have support for ID_lambda expressions in some places, but did not yet have a proper API for them.
1 parent 230e525 commit 3b062c6

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/util/std_expr.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,4 +4460,79 @@ inline cond_exprt &to_cond_expr(exprt &expr)
44604460
return ret;
44614461
}
44624462

4463+
/// \brief Expression to define a mapping from an argument (index) to elements.
4464+
/// This enables constructing an array via an anonymous function.
4465+
class lambda_exprt : public binary_exprt
4466+
{
4467+
public:
4468+
explicit lambda_exprt(symbol_exprt arg, exprt body, array_typet _type)
4469+
: binary_exprt(std::move(arg), ID_lambda, std::move(body), std::move(_type))
4470+
{
4471+
}
4472+
4473+
const array_typet &type() const
4474+
{
4475+
return static_cast<const array_typet &>(binary_exprt::type());
4476+
}
4477+
4478+
array_typet &type()
4479+
{
4480+
return static_cast<array_typet &>(binary_exprt::type());
4481+
}
4482+
4483+
const symbol_exprt &arg() const
4484+
{
4485+
return static_cast<const symbol_exprt &>(op0());
4486+
}
4487+
4488+
symbol_exprt &arg()
4489+
{
4490+
return static_cast<symbol_exprt &>(op0());
4491+
}
4492+
4493+
const exprt &body() const
4494+
{
4495+
return op1();
4496+
}
4497+
4498+
exprt &body()
4499+
{
4500+
return op1();
4501+
}
4502+
};
4503+
4504+
template <>
4505+
inline bool can_cast_expr<lambda_exprt>(const exprt &base)
4506+
{
4507+
return base.id() == ID_lambda;
4508+
}
4509+
4510+
inline void validate_expr(const lambda_exprt &value)
4511+
{
4512+
validate_operands(value, 2, "'Lambda' must have two operands");
4513+
}
4514+
4515+
/// \brief Cast an exprt to a \ref lambda_exprt
4516+
///
4517+
/// \a expr must be known to be \ref lambda_exprt.
4518+
///
4519+
/// \param expr: Source expression
4520+
/// \return Object of type \ref lambda_exprt
4521+
inline const lambda_exprt &to_lambda_expr(const exprt &expr)
4522+
{
4523+
PRECONDITION(expr.id() == ID_lambda);
4524+
const lambda_exprt &ret = static_cast<const lambda_exprt &>(expr);
4525+
validate_expr(ret);
4526+
return ret;
4527+
}
4528+
4529+
/// \copydoc to_lambda_expr(const exprt &)
4530+
inline lambda_exprt &to_lambda_expr(exprt &expr)
4531+
{
4532+
PRECONDITION(expr.id() == ID_lambda);
4533+
lambda_exprt &ret = static_cast<lambda_exprt &>(expr);
4534+
validate_expr(ret);
4535+
return ret;
4536+
}
4537+
44634538
#endif // CPROVER_UTIL_STD_EXPR_H

0 commit comments

Comments
 (0)