-
Notifications
You must be signed in to change notification settings - Fork 274
Parsing and type-checking loop variants (aka ranking functions) #6183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
int main() | ||
{ | ||
int i = 0; | ||
int N = 10; | ||
while(i != N) | ||
// clang-format off | ||
__CPROVER_loop_invariant(0 <= i && i <= N) | ||
__CPROVER_decreases(int x = 0) | ||
// clang-format on | ||
{ | ||
i++; | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
SaswatPadhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
activate-multi-line-match | ||
^main.c.*error: syntax error before 'int'\n.*__CPROVER_decreases\(int x = 0\)$ | ||
^PARSING ERROR$ | ||
^EXIT=(1|64)$ | ||
^SIGNAL=0$ | ||
-- | ||
-- | ||
This test fails because the loop variant is a statement (more precisely, | ||
an assignment) rather than an expression (more precisely, an ACSL binding | ||
expression). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
int main() | ||
{ | ||
int i = 0; | ||
int N = 10; | ||
while(i != N) | ||
// clang-format off | ||
__CPROVER_loop_invariant(0 <= i && i <= N) | ||
__CPROVER_decreases(N - i, 42) | ||
// clang-format on | ||
{ | ||
i++; | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
activate-multi-line-match | ||
^main.c.*error: syntax error before ','\n.*__CPROVER_decreases\(N - i, 42\)$ | ||
^PARSING ERROR$ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to see some negative tests! |
||
^EXIT=(1|64)$ | ||
^SIGNAL=0$ | ||
-- | ||
-- | ||
This test fails because the loop variant has two arguments instead of just one. | ||
Currently, we only support loop variants of single arguments - we do not | ||
support loop variants of tuples (yet). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -484,7 +484,8 @@ void c_typecheck_baset::typecheck_for(codet &code) | |
typecheck_code(code); // recursive call | ||
} | ||
|
||
typecheck_spec_expr(code, ID_C_spec_loop_invariant); | ||
typecheck_spec_loop_invariant(code); | ||
typecheck_spec_decreases(code); | ||
} | ||
|
||
void c_typecheck_baset::typecheck_label(code_labelt &code) | ||
|
@@ -772,7 +773,8 @@ void c_typecheck_baset::typecheck_while(code_whilet &code) | |
break_is_allowed=old_break_is_allowed; | ||
continue_is_allowed=old_continue_is_allowed; | ||
|
||
typecheck_spec_expr(code, ID_C_spec_loop_invariant); | ||
typecheck_spec_loop_invariant(code); | ||
typecheck_spec_decreases(code); | ||
} | ||
|
||
void c_typecheck_baset::typecheck_dowhile(code_dowhilet &code) | ||
|
@@ -805,16 +807,28 @@ void c_typecheck_baset::typecheck_dowhile(code_dowhilet &code) | |
break_is_allowed=old_break_is_allowed; | ||
continue_is_allowed=old_continue_is_allowed; | ||
|
||
typecheck_spec_expr(code, ID_C_spec_loop_invariant); | ||
typecheck_spec_loop_invariant(code); | ||
typecheck_spec_decreases(code); | ||
} | ||
|
||
void c_typecheck_baset::typecheck_spec_expr(codet &code, const irep_idt &spec) | ||
void c_typecheck_baset::typecheck_spec_loop_invariant(codet &code) | ||
{ | ||
if(code.find(spec).is_not_nil()) | ||
if(code.find(ID_C_spec_loop_invariant).is_not_nil()) | ||
{ | ||
exprt &constraint = static_cast<exprt &>(code.add(spec)); | ||
exprt &invariant = static_cast<exprt &>(code.add(ID_C_spec_loop_invariant)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything else we could check at this point? For instance, the number of operands? @SaswatPadhi // this comes with 1 operand, which is a Boolean statement
if(code.operands().size()!=1)
{
error().source_location = code.source_location();
error() << "decreases expected to have 1 operand" << eom;
throw 0;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good idea, but that's already taken care of by the parsing rule, so we don't need to add it to the type checker. For instance, take a look at Also note that in future this restriction would be relaxed to allow multi-dimensional ranking functions. I'm not sure if we'll make |
||
|
||
typecheck_expr(constraint); | ||
implicit_typecast_bool(constraint); | ||
typecheck_expr(invariant); | ||
implicit_typecast_bool(invariant); | ||
} | ||
} | ||
|
||
void c_typecheck_baset::typecheck_spec_decreases(codet &code) | ||
{ | ||
if(code.find(ID_C_spec_decreases).is_not_nil()) | ||
{ | ||
exprt &variant = static_cast<exprt &>(code.add(ID_C_spec_decreases)); | ||
|
||
typecheck_expr(variant); | ||
implicit_typecast_arithmetic(variant); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to have a
__CPROVER_decreases
without a loop invariant?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is possible. The default loop invariant is
true
. The code for that is already in my local repository, and I will file a second PR soon.