Difference between pages "Structured Types" and "The Use of Theories in Code Generation"

From Event-B
(Difference between pages)
Jump to navigationJump to search
imported>WikiSysop
 
imported>Andy
 
Line 1: Line 1:
==Modelling Structured Types==
+
= Defining Translations Using The Theory Plug-in =
 +
The theory plug-in is used to add mathematical extensions to Rodin. The theories are created, and deployed, and can then be used in any models in the workspace. When dealing with implementation level models, such as in Tasking Event-B, we need to consider how to translate newly added types and operators into code. We have augmented the theory interface with a Translation Rules section. This enables a user to define translation rules that map Event-B formulas to code.
 +
== Translation Rules==
 +
<div id="fig:Translation Rules">
 +
<br/>
 +
[[Image:TheoryCGRules.png|center||caption text]]
 +
<center>'''Figure 1''': Translation Rules</center>
 +
<br/>
 +
</div>
  
The Event-B mathematical language currently does not support a syntax for the direct definition of structured types such as records or class structures.
+
Figure 1 shows the interface, and some translations rules of the mapping to Ada.
Nevertheless it is possible to model structured types using projection functions to represent the fields/attributes.  For example,
 
suppose we wish to model a record structure ''R'' with fields ''e'' and ''f'' (with type ''E'' and ''F'' respectively).
 
Let us use the following syntax for this (not part of Event-B syntax):
 
  
<center>
+
The theory is given a name, and may import some other theories. Type parameters can be added, and we use them here to type the meta-variables. The meta-variable ''a'' is restricted to be an integer type, but meta-variable ''c'' can be any type. Meta-variables are used in the translator rules for pattern matching.
{{SimpleHeader}}
 
|<math> \begin{array}{lcl}
 
\textbf{RECORD}~~~~ R &::&  e\in E\\
 
    &&  f \in F
 
\end{array}
 
</math>
 
|}
 
</center>
 
  
We can model this structure in Event-B by introducing (in a context) a carrier set ''R'' and two
+
Translator rules are templates, which are used in pattern matching. Event-B formulas are defined on the left hand side of the rule, and the code to be output (as text) appears on the right hand side of the matching rule. During translation an abstract syntax tree (AST) representation of the formula is used. The theory plug-in attempts to match the formulas in the rules with each syntactic element of the AST. As it does so it builds the textual output as a string, until the whole AST has been successfully matched. When a complete tree is matched, the target code is returned. If the AST is not matched, a warning is issued, and a string representation of the original formula is returned.
functions ''e'' and ''f'' as constants as follows:
 
  
<center>
+
== Type Rules for Code Generation ==
{{SimpleHeader}}
 
|<math> \begin{array}{l}
 
\textbf{SETS}~~ R\\
 
\textbf{CONSTANTS}~~ e,~ f\\
 
\textbf{AXIOMS}\\
 
~~~~\begin{array}{l}
 
  e \in  R \tfun E\\
 
  f \in  R \tfun F\\
 
\end{array}
 
\end{array}
 
</math>
 
|}
 
</center>
 
  
 +
The type rules section, shown in Figure 1, is where the relationship is defined, between Event-B types and the type system of the implementation.
  
Now, given an element <math>r\in R</math> representing a record structure, we write <math>e(r)</math> for the ''e'' component of ''r'' and <math>f(r)</math> for the ''f'' component of ''r''.
+
= Adding New (implementation-level) Types =
 +
When we are working at abstraction levels close to the implementation level, we may make an implementation decision which requires the introduction of a new type to the development. We give an example of our approach, where we add a new array type, shown in Figure 2, and then define its translation to code.
  
''E'' and ''F'' can be any  type definable in Event-B, including a type representing a record structure.
+
== An Array Type Definition ==
 +
<div id="fig:Extension with an Array Type">
 +
<br/>
 +
[[Image:ArrayDef.png|center||caption text]]
 +
<center>'''Figure 2''': Array Definition</center>
 +
<br/>
 +
</div>
  
==Constructing Structured Values==
+
The array operator notation is defined in the expression array(s: P(T)); and the semantics is defined in the direct definition. arrayN constrains the arrays to be of fixed length. Array lookup, update, and constructor operators are subsequently defined. In the next step we need to define any translations required to implement the array in code.
  
Suppose we have a variable ''v'' in a machine whose type is the structure ''R'' defined above:
+
== Translation Rules ==
<center>
 
<math>\textbf{VARIABLE}~~ v\in R</math>
 
</center>
 
We wish to assign a structured value to ''v'' whose ''e'' field has some value ''e1'' and whose ''f'' field has some value ''f1''.
 
This can be achieved by specifying the choice of an event parameter ''r'' whose fields are constrained by appropriate guards
 
and assigning parameter ''r'' to the machine variable ''v''.  This is shown in the following event:
 
  
<center><math>
+
<div id="Translation Rules for the Array Type">
\begin{array}{l}
+
<br/>
\textbf{ANY}~~ r ~~\textbf{WHERE} \\
+
[[Image:ArrayTrans.png|center||caption text]]
~~~~ r \in R  \\
+
<center>'''Figure 3''': Translation Rules for the Array Type</center>
~~~~ e( r ) = e1 \\
+
<br/>
~~~~ f( r ) = f1 \\
+
</div>
\textbf{THEN} \\
 
~~~~ v := r \\
 
\textbf{END}
 
\end{array}
 
</math></center>
 
  
If we only wish to update some fields and leave others changed, this needs to be done by specifying explicitly that some fields
+
Figure 3 shows the Ada translation; beginning with the meta-variable definitions that are used for pattern matching in the translation rules. Each of the operators; ''newArray'', and ''update'', and an expression using the ''lookup'' operator, are mapped to their implementations on the right hand side of the rule. The ''Type Rules'' section describes the implementation's description of the ''arrayN'' type.
remain unchanged.  This is shown in the following example where only the ''e'' field is modified:
 
 
 
<center><math>
 
\begin{array}{l}
 
\textbf{ANY}~~ r ~~\textbf{WHERE} \\
 
~~~~ r \in R  \\
 
~~~~ e( r ) = e1 \\
 
~~~~ f( r ) = f(v) \\
 
\textbf{THEN} \\
 
~~~~ v := r \\
 
\textbf{END}
 
\end{array}
 
</math></center>
 
 
 
If we don't care about the value of some field (e.g., ''f''), we simply omit any guard on that field as follows:
 
 
 
<center><math>
 
\begin{array}{l}
 
\textbf{ANY}~~ r ~~\textbf{WHERE} \\
 
~~~~ r \in R  \\
 
~~~~ e( r ) = e1 \\
 
\textbf{THEN} \\
 
~~~~ v := r \\
 
\textbf{END}
 
\end{array}
 
</math></center>
 
 
 
 
 
Sometimes we will wish to model a set of structured elements as a machine variable, e.g.,
 
<center>
 
<math>\textbf{VARIABLE}~~ vs\subseteq R</math>
 
</center>
 
 
 
We can add a structured element to this set using the following event:
 
<center><math>
 
\begin{array}{l}
 
\textbf{ANY}~~ r ~~\textbf{WHERE} \\
 
~~~~ r \in R  \\
 
~~~~ e( r ) = e1 \\
 
~~~~ f( r ) = f1 \\
 
\textbf{THEN} \\
 
~~~~ vs := vs \cup \{r\} \\
 
\textbf{END}
 
\end{array}
 
</math></center>
 
 
 
==Extending Structured Types==
 
 
 
==Sub-Typing==
 
 
 
==Recursive Structured Types==
 
 
 
==Structured Variables==
 

Revision as of 15:51, 15 May 2012

Defining Translations Using The Theory Plug-in

The theory plug-in is used to add mathematical extensions to Rodin. The theories are created, and deployed, and can then be used in any models in the workspace. When dealing with implementation level models, such as in Tasking Event-B, we need to consider how to translate newly added types and operators into code. We have augmented the theory interface with a Translation Rules section. This enables a user to define translation rules that map Event-B formulas to code.

Translation Rules


caption text
Figure 1: Translation Rules


Figure 1 shows the interface, and some translations rules of the mapping to Ada.

The theory is given a name, and may import some other theories. Type parameters can be added, and we use them here to type the meta-variables. The meta-variable a is restricted to be an integer type, but meta-variable c can be any type. Meta-variables are used in the translator rules for pattern matching.

Translator rules are templates, which are used in pattern matching. Event-B formulas are defined on the left hand side of the rule, and the code to be output (as text) appears on the right hand side of the matching rule. During translation an abstract syntax tree (AST) representation of the formula is used. The theory plug-in attempts to match the formulas in the rules with each syntactic element of the AST. As it does so it builds the textual output as a string, until the whole AST has been successfully matched. When a complete tree is matched, the target code is returned. If the AST is not matched, a warning is issued, and a string representation of the original formula is returned.

Type Rules for Code Generation

The type rules section, shown in Figure 1, is where the relationship is defined, between Event-B types and the type system of the implementation.

Adding New (implementation-level) Types

When we are working at abstraction levels close to the implementation level, we may make an implementation decision which requires the introduction of a new type to the development. We give an example of our approach, where we add a new array type, shown in Figure 2, and then define its translation to code.

An Array Type Definition


caption text
Figure 2: Array Definition


The array operator notation is defined in the expression array(s: P(T)); and the semantics is defined in the direct definition. arrayN constrains the arrays to be of fixed length. Array lookup, update, and constructor operators are subsequently defined. In the next step we need to define any translations required to implement the array in code.

Translation Rules


caption text
Figure 3: Translation Rules for the Array Type


Figure 3 shows the Ada translation; beginning with the meta-variable definitions that are used for pattern matching in the translation rules. Each of the operators; newArray, and update, and an expression using the lookup operator, are mapped to their implementations on the right hand side of the rule. The Type Rules section describes the implementation's description of the arrayN type.