Skip to content

Commit

Permalink
Simplify Valid_Context predicate and generating code
Browse files Browse the repository at this point in the history
* Now for each field, only specify First/Size for this field, don't
  express anything about successor fields
* Use existing Field_First_Internal/Field_Size_Internal functions
* bump timeout for ping
* add unit test for more coverage

For eng/recordflux/recordflux#1382
  • Loading branch information
kanigsson committed Nov 6, 2023
1 parent ee47a40 commit aa0abdf
Show file tree
Hide file tree
Showing 31 changed files with 254 additions and 370 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Syntax for passing repeated `-i` and `-v` options to `rflx validate` (eng/recordflux/RecordFlux#1441)
- Simplified setter code and removed internal `Successor` function (eng/recordflux/RecordFlux#1448)
- Simplified `Valid_Context` predicate (eng/recordflux/RecordFlux#1382)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions examples/apps/ping/ping.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ project Ping is
package Prove is
for Proof_Switches ("Ada") use Defaults.Proof_Switches;
for Proof_Switches ("rflx-rflx_arithmetic.adb") use ("--prover=Z3,altergo,cvc5,colibri", "--timeout=120");
for Proof_Switches ("rflx-icmp-message.adb") use ("--timeout=120");
end Prove;

end Ping;
133 changes: 49 additions & 84 deletions rflx/generator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ def type_conversion(expression: expr.Expr) -> expr.Expr:

def message_structure_invariant(
message: model.Message,
prefix: str,
embedded: bool = False,
) -> Expr:
"""
Expand All @@ -338,97 +337,64 @@ def message_structure_invariant(
def prefixed(name: str) -> expr.Expr:
return expr.Selected(expr.Variable("Ctx"), name) if not embedded else expr.Variable(name)

def link_property(link: model.Link, unique: bool) -> Expr:
field_type = message.types[link.target]
condition = link.condition.substituted(substitution(message, prefix, embedded)).simplified()
size = (
field_type.size
if isinstance(field_type, model.Scalar)
else link.size.substituted(
substitution(message, prefix, embedded, target_type=const.TYPES_BIT_LENGTH),
).simplified()
)
first = (
prefixed("First")
if link.source == model.INITIAL
else link.first.substituted(
substitution(message, prefix, embedded, target_type=const.TYPES_BIT_INDEX),
)
.substituted(
mapping={
expr.UNDEFINED: expr.Add(
expr.Selected(
expr.Indexed(
prefixed("Cursors"),
expr.Variable(link.source.affixed_name),
def field_property(fld: model.Field) -> Expr:
return AndThen(
Equal(
Add(
Sub(
Selected(
Indexed(
prefixed("Cursors").ada_expr(),
Variable(fld.affixed_name),
),
"Last",
),
expr.Number(1),
Selected(
Indexed(
prefixed("Cursors").ada_expr(),
Variable(fld.affixed_name),
),
"First",
),
),
},
)
.simplified()
)
precond = (
AndThen(
Number(1),
),
Call(
"Well_Formed",
[Indexed(Variable("Cursors"), Variable(link.source.affixed_name))],
"Field_Size_Internal",
[
Variable("Cursors"),
Variable("First"),
Variable("Verified_Last"),
Variable("Written_Last"),
Variable("Buffer"),
*[Variable(fld.name) for fld in message.parameter_types],
Variable(fld.affixed_name),
],
),
condition.ada_expr(),
)
if link.source != model.INITIAL and not unique
else TRUE
)

return If(
[
(
precond,
AndThen(
Equal(
Add(
Sub(
Selected(
Indexed(
prefixed("Cursors").ada_expr(),
Variable(link.target.affixed_name),
),
"Last",
),
Selected(
Indexed(
prefixed("Cursors").ada_expr(),
Variable(link.target.affixed_name),
),
"First",
),
),
Number(1),
),
size.ada_expr(),
),
Equal(
Selected(
Indexed(
prefixed("Cursors").ada_expr(),
Variable(link.target.affixed_name),
),
"First",
),
first.ada_expr(),
),
),
Equal(
Selected(
Indexed(
prefixed("Cursors").ada_expr(),
Variable(fld.affixed_name),
),
"First",
),
],
Call(
"Field_First_Internal",
[
Variable("Cursors"),
Variable("First"),
Variable("Verified_Last"),
Variable("Written_Last"),
Variable("Buffer"),
*[Variable(fld.name) for fld in message.parameter_types],
Variable(fld.affixed_name),
],
),
),
)

def field_property(fld: model.Field) -> Expr:
incoming = message.incoming(fld)
unique = len(incoming) == 1
return AndThen(*[link_property(link, unique) for link in incoming])

def map_invariant(fld: model.Field) -> Expr:
return If(
[
Expand All @@ -447,7 +413,6 @@ def map_invariant(fld: model.Field) -> Expr:

def context_predicate(
message: model.Message,
prefix: str,
) -> Expr:
def invalid_successors_invariant() -> Expr:
"""
Expand Down Expand Up @@ -529,7 +494,7 @@ def invalid_successors_invariant() -> Expr:
],
),
invalid_successors_invariant(),
message_structure_invariant(message, prefix, embedded=True),
message_structure_invariant(message, embedded=True),
)


Expand Down
1 change: 0 additions & 1 deletion rflx/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ def _create_message(self, message: Message) -> dict[ID, Unit]: # noqa: PLR0912
self._executor.submit(
message_generator.create_valid_context_function,
message,
self._prefix,
),
self._executor.submit(message_generator.create_context_type, message),
self._executor.submit(message_generator.create_initialize_procedure, message),
Expand Down
3 changes: 1 addition & 2 deletions rflx/generator/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,6 @@ def fld_first_expr(fld: Field) -> expr.Expr:

def create_valid_context_function(
message: Message,
prefix: str,
) -> UnitPart:
specification = FunctionSpecification(
"Valid_Context",
Expand Down Expand Up @@ -832,7 +831,7 @@ def create_valid_context_function(
),
ExpressionFunctionDeclaration(
specification,
common.context_predicate(message, prefix),
common.context_predicate(message),
[Postcondition(TRUE)],
),
Pragma(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ private
and then ((if
Well_Formed (Cursors (F_Data))
then
(Cursors (F_Data).Last - Cursors (F_Data).First + 1 = RFLX_Types.Bit_Length (Written_Last - First + 1)
and then Cursors (F_Data).First = First))))
Cursors (F_Data).Last - Cursors (F_Data).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Data)
and then Cursors (F_Data).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Data))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,13 @@ private
and then ((if
Well_Formed (Cursors (F_Data))
then
(Cursors (F_Data).Last - Cursors (F_Data).First + 1 = RFLX_Types.Bit_Length (Length) * 8
and then Cursors (F_Data).First = First))
Cursors (F_Data).Last - Cursors (F_Data).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, Length, Extended, F_Data)
and then Cursors (F_Data).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, Length, Extended, F_Data))
and then (if
Well_Formed (Cursors (F_Extension))
then
(Cursors (F_Extension).Last - Cursors (F_Extension).First + 1 = RFLX_Types.Bit_Length (Length) * 8
and then Cursors (F_Extension).First = Cursors (F_Data).Last + 1))))
Cursors (F_Extension).Last - Cursors (F_Extension).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, Length, Extended, F_Extension)
and then Cursors (F_Extension).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, Length, Extended, F_Extension))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ private
and then ((if
Well_Formed (Cursors (F_Value))
then
(Cursors (F_Value).Last - Cursors (F_Value).First + 1 = 8
and then Cursors (F_Value).First = First))))
Cursors (F_Value).Last - Cursors (F_Value).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Value)
and then Cursors (F_Value).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Value))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,13 +621,13 @@ private
and then ((if
Well_Formed (Cursors (F_A))
then
(Cursors (F_A).Last - Cursors (F_A).First + 1 = 32
and then Cursors (F_A).First = First))
Cursors (F_A).Last - Cursors (F_A).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_A)
and then Cursors (F_A).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_A))
and then (if
Well_Formed (Cursors (F_B))
then
(Cursors (F_B).Last - Cursors (F_B).First + 1 = 32
and then Cursors (F_B).First = Cursors (F_A).Last + 1))))
Cursors (F_B).Last - Cursors (F_B).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_B)
and then Cursors (F_B).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_B))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,13 +621,13 @@ private
and then ((if
Well_Formed (Cursors (F_C))
then
(Cursors (F_C).Last - Cursors (F_C).First + 1 = 32
and then Cursors (F_C).First = First))
Cursors (F_C).Last - Cursors (F_C).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_C)
and then Cursors (F_C).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_C))
and then (if
Well_Formed (Cursors (F_D))
then
(Cursors (F_D).Last - Cursors (F_D).First + 1 = 32
and then Cursors (F_D).First = Cursors (F_C).Last + 1))))
Cursors (F_D).Last - Cursors (F_D).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_D)
and then Cursors (F_D).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_D))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,18 +664,18 @@ private
and then ((if
Well_Formed (Cursors (F_X_A))
then
(Cursors (F_X_A).Last - Cursors (F_X_A).First + 1 = 32
and then Cursors (F_X_A).First = First))
Cursors (F_X_A).Last - Cursors (F_X_A).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_X_A)
and then Cursors (F_X_A).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_X_A))
and then (if
Well_Formed (Cursors (F_X_B))
then
(Cursors (F_X_B).Last - Cursors (F_X_B).First + 1 = 32
and then Cursors (F_X_B).First = Cursors (F_X_A).Last + 1))
Cursors (F_X_B).Last - Cursors (F_X_B).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_X_B)
and then Cursors (F_X_B).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_X_B))
and then (if
Well_Formed (Cursors (F_Y))
then
(Cursors (F_Y).Last - Cursors (F_Y).First + 1 = 32
and then Cursors (F_Y).First = Cursors (F_X_B).Last + 1))))
Cursors (F_Y).Last - Cursors (F_Y).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Y)
and then Cursors (F_Y).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Y))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,18 +773,18 @@ private
and then ((if
Well_Formed (Cursors (F_Message_Type))
then
(Cursors (F_Message_Type).Last - Cursors (F_Message_Type).First + 1 = 8
and then Cursors (F_Message_Type).First = First))
Cursors (F_Message_Type).Last - Cursors (F_Message_Type).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Message_Type)
and then Cursors (F_Message_Type).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Message_Type))
and then (if
Well_Formed (Cursors (F_Length))
then
(Cursors (F_Length).Last - Cursors (F_Length).First + 1 = 8
and then Cursors (F_Length).First = Cursors (F_Message_Type).Last + 1))
Cursors (F_Length).Last - Cursors (F_Length).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Length)
and then Cursors (F_Length).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Length))
and then (if
Well_Formed (Cursors (F_Data))
then
(Cursors (F_Data).Last - Cursors (F_Data).First + 1 = RFLX_Types.Bit_Length (Cursors (F_Length).Value) * 8
and then Cursors (F_Data).First = Cursors (F_Length).Last + 1))))
Cursors (F_Data).Last - Cursors (F_Data).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Data)
and then Cursors (F_Data).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Data))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,13 @@ private
and then ((if
Well_Formed (Cursors (F_Length))
then
(Cursors (F_Length).Last - Cursors (F_Length).First + 1 = 16
and then Cursors (F_Length).First = First))
Cursors (F_Length).Last - Cursors (F_Length).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Length)
and then Cursors (F_Length).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Length))
and then (if
Well_Formed (Cursors (F_Data))
then
(Cursors (F_Data).Last - Cursors (F_Data).First + 1 = RFLX_Types.Bit_Length (Cursors (F_Length).Value) * 8
and then Cursors (F_Data).First = Cursors (F_Length).Last + 1))))
Cursors (F_Data).Last - Cursors (F_Data).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Data)
and then Cursors (F_Data).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Data))))
with
Post =>
True;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,18 +740,18 @@ private
and then ((if
Well_Formed (Cursors (F_Tag))
then
(Cursors (F_Tag).Last - Cursors (F_Tag).First + 1 = 8
and then Cursors (F_Tag).First = First))
Cursors (F_Tag).Last - Cursors (F_Tag).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Tag)
and then Cursors (F_Tag).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Tag))
and then (if
Well_Formed (Cursors (F_Length))
then
(Cursors (F_Length).Last - Cursors (F_Length).First + 1 = 16
and then Cursors (F_Length).First = Cursors (F_Tag).Last + 1))
Cursors (F_Length).Last - Cursors (F_Length).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Length)
and then Cursors (F_Length).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Length))
and then (if
Well_Formed (Cursors (F_Value))
then
(Cursors (F_Value).Last - Cursors (F_Value).First + 1 = RFLX_Types.Bit_Length (Cursors (F_Length).Value) * 8
and then Cursors (F_Value).First = Cursors (F_Length).Last + 1))))
Cursors (F_Value).Last - Cursors (F_Value).First + 1 = Field_Size_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Value)
and then Cursors (F_Value).First = Field_First_Internal (Cursors, First, Verified_Last, Written_Last, Buffer, F_Value))))
with
Post =>
True;
Expand Down
Loading

0 comments on commit aa0abdf

Please sign in to comment.