<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <journal-title-group>
        <journal-title>Ievgen Ivanov, Artur Korniłowicz, and Mykola Nikitchenko. Partial correctness of GCD algorithm.
Formalized Mathematics</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>Formal verification of the correctness of chosen algorithms in Mizar</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Adrian Jaszczak</string-name>
          <email>a.jaszczak@uwb.edu.pl</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Institute of Informatics University of Bialystok</institution>
          ,
          <country country="PL">Poland</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2019</year>
      </pub-date>
      <volume>26</volume>
      <issue>2</issue>
      <fpage>8</fpage>
      <lpage>12</lpage>
      <abstract>
        <p>In this paper we continue development of formal verification tools for algorithms using the framework of nominative data and Floyd-Hoare logic with partial pre- and postconditions in the Mizar proof assistant. We define operations of sequential composition of several programs, formalize and show soundness of new inference rules which can be used to prove partial correctness of programs involving these operations in the context of partial pre- and postconditions. A process of verification of the partial correctness of exemplary algorithms is described.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>The framework is intended to be used for practical verification of the correctness of programs. One of its
implementations is done in the Mizar proof assistant [BBG+15, GKN15]. Nominative data with simple names
and complex values and basic operations on them are defined in [INKK17]. An algebra of programs over partial
predicates and binominative functions on nominative data including among others operators of a programming
language (e.g. assignment, skip, conditional if statement and a while loop) are defined in [KIN18, IKN18b,
IKN18c]. An inference system of an extension of Floyd-Hoare logic for partial predicates and soundness of the
rules are formalized in [IKN18a]. An example – the subtraction-based version of Euclid’s algorithm computing
the greatest common divisor of natural numbers – how to use all this stuff is presented in [IKN18d].</p>
      <p>In this paper we report on our continuation [KKNI17a, KKNI17b, KKNI17c] of formalization of the framework
in Mizar including three more inference rules for unconditional composition of 3, 4, and 5 programs and proofs of
the correctness of two algorithms: computing the natural power of a complex number and factorial of a natural
number.
2</p>
    </sec>
    <sec id="sec-2">
      <title>Mizar Formalization</title>
      <p>Having mathematical tools mentioned above we have proved in Mizar the correctness of two algorithms: the
natural power of a complex number [Jas19] and factorial of a natural number [JK19] with the following
pseudocodes:</p>
      <p>Pseudocode of factorial algorithm:
i := val.1; j := val.2;
n := val.3; s := val.4;
while ( i &lt;&gt; n )
i := i + j;
s := s * i;
return s;</p>
      <p>Pseudocode of power algorithm:
i := val.1; j := val.2;
b := val.3; n := val.4; s := val.5;
while ( i &lt;&gt; n )
i := i + j;
s := s * b;
return s;</p>
      <p>Both algorithms consist of three parts: the initialization of variables, the main while loop, and the result
returning statement. Moreover, in factorial we have only one input value and in power two input values: base
and exponent. At the end we return s as the results of algorithms.</p>
      <p>Formalization of the algorithms in Mizar has been divided into few separate parts. Because both algorithms
are similar, we can take a closer look into just one of them, let us choose the power algorithm. To formulate
it we introduced several Mizar functors representing various components of the algorithm: the initialization of
variables:
definition let V,A,loc,val;
func power_var_init(A,loc,val) -&gt; SCBinominativeFunction of V,A equals
PP_composition(</p>
      <p>SC_assignment(denaming(V,A,val.1), loc/.1), SC_assignment(denaming(V,A,val.2), loc/.2),
SC_assignment(denaming(V,A,val.3), loc/.3), SC_assignment(denaming(V,A,val.4), loc/.4),
SC_assignment(denaming(V,A,val.5), loc/.5) );
end;
the loop body:
end;
the entire loop:
definition let V,A,loc;
func power_loop_body(A,loc) -&gt; SCBinominativeFunction of V,A equals</p>
      <p>PP_composition(</p>
      <p>SC_assignment(addition(A,loc/.1,loc/.2),loc/.1),</p>
      <p>SC_assignment(multiplication(A,loc/.5,loc/.3),loc/.5) );
definition let V,A,loc;
func power_main_loop(A,loc) -&gt; SCBinominativeFunction of V,A equals</p>
      <p>PP_while(PP_not(Equality(A,loc/.1,loc/.4)),power_loop_body(A,loc));
end;
the initialization composed with the loop:
definition let V,A,loc,val;
func power_main_part(A,loc,val) -&gt; SCBinominativeFunction of V,A equals</p>
      <p>PP_composition(power_var_init(A,loc,val),power_main_loop(A,loc));
and finally the entire program where the returning statement is added:
definition let V,A,loc,val,z;
func power_program(A,loc,val,z) -&gt; SCBinominativeFunction of V,A equals</p>
      <p>PP_composition(power_main_part(A,loc,val), SC_assignment(denaming(V,A,loc/.5),z));</p>
      <p>Because we work in the paradigm of nominative data those definitions take sets V and A as names and values,
respectively. Moreover, V-valued functions loc represents formally locations in memory where variables are
stored, and functions val keep values of variables.</p>
      <p>The partial correctness of programs in our framework is expressed as the validity of semantic Floyd-Hoare
triples [IKN18a] of the form &lt;*p,f,r*&gt; is SFHT of D, where p represents a precondition, f represents a
program, and r represents a postcondition, all defined over a set D. In the case of the power algorithm it takes the
form:
&lt;* valid_power_input(V,A,val,b0,n0),
power_program(A,loc,val,z),
valid_power_output(A,z,b0,n0) *&gt; is SFHT of ND(V,A)
where valid_power_input is the precondition representing the valid input and valid_power_output is the
postcondition representing the valid output, both involving the complex base b0 and the natural exponent n0.
All components of the triple are defined over the set ND(V,A) of all nominative data over sets V and A.</p>
      <p>The discussed algorithm contains a while loop, which verification requires an invariant. For this purpose we
defined the predicate:
definition let V,A,loc,b0,n0,d;
pred power_inv_pred A,loc,b0,n0,d means
ex d1 being NonatomicND of V,A st
d = d1 &amp; { loc/.1, loc/.2, loc/.3, loc/.4, loc/.5 } c= dom d1 &amp;
d1.(loc/.2) = 1 &amp; d1.(loc/.3) = b0 &amp; d1.(loc/.4) = n0 &amp;
ex S being Complex, I being Nat st I = d1.(loc/.1) &amp; S = d1.(loc/.5) &amp; S = b0|^I;
end;
which describes that every state d1 includes all required memory locations, initial values 1, b0 and n0 are always
in the same locations, and S is always equal to b0I. It is used to prove that initialization of variables and each
iteration of the loop fulfill the condition.</p>
      <p>With this structure we could start proving the correctness of the algorithms. Detailed proofs are available in
the Mizar Mathematical Library [BBG+18, Jas19, JK19].</p>
      <p>Apart from the formal verification of the correctness of mentioned algorithms we also defined operations
for composition of 3, 4 and 5 instructions. Moreover, we defined inference rules describing how to prove the
correctness of programs involving these operations and proved their soundness. In the case of the composition
of 3 instructions the rule is:
fpg f1 fqg; fqg f2 frg; f qg f2 frg; frg f3 fsg; f rg f3 fsg</p>
      <p>fpg f1 f2 f3 fsg
As one can notice, the rule above is different than in the standard Floyd-Hoare logic would be. The reason of
that is because we also consider a case where results of programs f1 and f2 do not belong to the domains of the
partial conditions q and r, respectively. In the standard case all pre- and postconditions would be total.</p>
      <p>The operations and rules allowed us to use one composition instead of several binary compositions.
3</p>
    </sec>
    <sec id="sec-3">
      <title>Conclusions and Future Work</title>
      <p>In this paper we have shown how to verify the correctness of algorithms in the Mizar proof assistant using
nominative data and a variant of Floyd-Hoare logic on the example of algorithms computing the natural power
of a complex number and factorial of a natural number. On these simple examples it can be observed that
various algorithms have almost same structure and in many cases there are very similar formalization steps. In
the future we want to detect and define more general structures of algorithms written in our environment and to
use particular Mizar constructions, like structures and schemes, to formulate the algorithms and make reasoning
on them. For example, we can define a Mizar structure containing the input, output, constant values, the main
program, and possibly other components of programs as separate fields of the structure.</p>
      <p>Another way of extension of our framework is to define new instructions in the language and new inference
rules about the instructions within our variant of logic. For example, we can introduce an instruction for
the composition of arbitrary n instructions instead of compositions of two, three, four and more instructions
separately, and for loop instruction and adequate inference rules. It will make easier writing algorithms with
sequences of compositions and make shorter proofs of properties of the algorithms.</p>
      <p>The ultimate goal of the project is to build a complete formal tool for verifying the correctness of complex
algorithms. These algorithms could be then implemented, for example, in some safety-critical software. Soon,
almost every aspect of our life will be connected with computer programs, so it is important to make sure that
algorithms that they will be using are designed and implemented correctly.
[Flo67]</p>
      <p>Adam Grabowski, Artur Korniłowicz, and Adam Naumowicz. Four decades of Mizar. Journal of
Automated Reasoning, 55(3):191–198, 2015. https://doi.org/10.1007/s10817-015-9345-1
V. M. Glushkov. Automata theory and formal microprogram transformations. Cybernetics, 1(5):1–8,
Sep 1965.</p>
      <p>C.A.R. Hoare. An axiomatic basis for computer programming. Commun. ACM, 12(10):576–580,
1969.
[IKN18a] Ievgen Ivanov, Artur Korniłowicz, and Mykola Nikitchenko. An inference system of an extension
of Floyd-Hoare logic for partial predicates. Formalized Mathematics, 26(2):159–164, 2018. https:
//doi.org/10.2478/forma-2018-0013
[IKN18b] Ievgen Ivanov, Artur Korniłowicz, and Mykola Nikitchenko. On algebras of algorithms and
specifications over uninterpreted data. Formalized Mathematics, 26(2):141–147, 2018. https://doi.org/
10.2478/forma-2018-0011</p>
      <p>Ievgen Ivanov, Artur Korniłowicz, and Mykola Nikitchenko. On an algorithmic algebra over
simplenamed complex-valued nominative data. Formalized Mathematics, 26(2):149–158, 2018. https:
//doi.org/10.2478/forma-2018-0012
[JK19]</p>
      <p>Adrian Jaszczak. Partial correctness of a power algorithm. Formalized Mathematics, 27(2), 2019.
Accepted for publication.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [BBG+15]
          <string-name>
            <surname>Grzegorz</surname>
            <given-names>Bancerek</given-names>
          </string-name>
          , Czesław Byliński, Adam Grabowski, Artur Korniłowicz, Roman Matuszewski, Adam Naumowicz, Karol Pąk, and
          <string-name>
            <given-names>Josef</given-names>
            <surname>Urban</surname>
          </string-name>
          . Mizar:
          <article-title>State-of-the-art and beyond</article-title>
          . In Manfred Kerber, Jacques Carette, Cezary Kaliszyk, Florian Rabe, and Volker Sorge, editors,
          <source>Intelligent Computer Mathematics</source>
          , volume
          <volume>9150</volume>
          of Lecture Notes in Computer Science, pages
          <fpage>261</fpage>
          -
          <lpage>279</lpage>
          . Springer International Publishing,
          <year>2015</year>
          . http://dx.doi.org/10.1007/978-3-
          <fpage>319</fpage>
          -20615-8_
          <fpage>17</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [BBG+18]
          <string-name>
            <surname>Grzegorz</surname>
            <given-names>Bancerek</given-names>
          </string-name>
          , Czesław Byliński, Adam Grabowski, Artur Korniłowicz, Roman Matuszewski, Adam Naumowicz, and
          <string-name>
            <given-names>Karol</given-names>
            <surname>Pąk</surname>
          </string-name>
          .
          <article-title>The role of the Mizar Mathematical Library for interactive proof development in Mizar</article-title>
          .
          <source>Journal of Automated Reasoning</source>
          ,
          <volume>61</volume>
          (
          <issue>1</issue>
          ):
          <fpage>9</fpage>
          -
          <lpage>32</lpage>
          ,
          <year>2018</year>
          . https://doi.org/10. 1007/s10817-017-9440-6
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>[KIN18]</mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          <string-name>
            <surname>Mathematics</surname>
          </string-name>
          ,
          <volume>27</volume>
          (
          <issue>2</issue>
          ),
          <year>2019</year>
          .
          <article-title>Accepted for publication</article-title>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          <source>Formalized Mathematics</source>
          ,
          <volume>26</volume>
          (
          <issue>1</issue>
          ):
          <fpage>11</fpage>
          -
          <lpage>20</lpage>
          ,
          <year>2018</year>
          . https://doi.org/10.2478/forma-2018
          <source>-0002</source>
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [KKNI17a]
          <string-name>
            <given-names>Artur</given-names>
            <surname>Korniłowicz</surname>
          </string-name>
          , Andrii Kryvolap, Mykola Nikitchenko, and
          <string-name>
            <given-names>Ievgen</given-names>
            <surname>Ivanov</surname>
          </string-name>
          .
          <article-title>An approach to formalization of an extension of Floyd-Hoare logic</article-title>
          . In Vadim Ermolayev, Nick Bassiliades,
          <string-name>
            <surname>Hans-Georg</surname>
            <given-names>Fill</given-names>
          </string-name>
          , Vitaliy Yakovyna, Heinrich C. Mayr, Vyacheslav Kharchenko, Vladimir Peschanenko, Mariya Shyshkina, Mykola Nikitchenko, and Aleksander Spivakovsky, editors,
          <source>Proceedings of the 13th International Conference on ICT in Education, Research and Industrial Applications</source>
          . Integration, Harmonization and
          <string-name>
            <given-names>Knowledge</given-names>
            <surname>Transfer</surname>
          </string-name>
          , Kyiv, Ukraine, May
          <volume>15</volume>
          -18,
          <year>2017</year>
          , volume
          <volume>1844</volume>
          <source>of CEUR Workshop Proceedings</source>
          , pages
          <fpage>504</fpage>
          -
          <lpage>523</lpage>
          . CEUR-WS.org,
          <year>2017</year>
          . http://ceur-ws.
          <source>org/</source>
          Vol-
          <volume>1844</volume>
          /10000504.pdf
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [KKNI17b]
          <string-name>
            <given-names>Artur</given-names>
            <surname>Korniłowicz</surname>
          </string-name>
          , Andrii Kryvolap, Mykola Nikitchenko, and
          <string-name>
            <given-names>Ievgen</given-names>
            <surname>Ivanov</surname>
          </string-name>
          .
          <article-title>Formalization of the algebra of nominative data in Mizar</article-title>
          . In Maria Ganzha, Leszek A.
          <string-name>
            <surname>Maciaszek</surname>
          </string-name>
          , and Marcin Paprzycki, editors,
          <source>Proceedings of the 2017 Federated Conference on Computer Science and Information Systems, FedCSIS</source>
          <year>2017</year>
          , Prague, Czech Republic,
          <source>September 3-6</source>
          ,
          <year>2017</year>
          , pages
          <fpage>237</fpage>
          -
          <lpage>244</lpage>
          ,
          <year>2017</year>
          . https://doi.org/10.15439/2017F301
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [KKNI17c]
          <string-name>
            <given-names>Artur</given-names>
            <surname>Korniłowicz</surname>
          </string-name>
          , Andrii Kryvolap, Mykola Nikitchenko, and
          <string-name>
            <given-names>Ievgen</given-names>
            <surname>Ivanov</surname>
          </string-name>
          .
          <article-title>Formalization of the nominative algorithmic algebra in Mizar</article-title>
          . In Leszek Borzemski, Jerzy Świątek, and Zofia Wilimowska, editors,
          <source>Information Systems Architecture and Technology: Proceedings of 38th International Conference on Information Systems Architecture</source>
          and Technology
          <string-name>
            <surname>- ISAT 2017 - Part</surname>
            <given-names>II</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Szklarska</surname>
            <given-names>Poręba</given-names>
          </string-name>
          , Poland,
          <source>September 17-19</source>
          ,
          <year>2017</year>
          , volume
          <volume>656</volume>
          <source>of Advances in Intelligent Systems and Computing</source>
          , pages
          <fpage>176</fpage>
          -
          <lpage>186</lpage>
          . Springer,
          <year>2017</year>
          . https://doi.org/10.1007/978-3-
          <fpage>319</fpage>
          -67229-8_
          <fpage>16</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>[KNS13]</mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>[NN92]</mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>[SNI14]</mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>gies in Education, Research, and Industrial Applications: 9th International Conference, ICTERI</mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          2013, Kherson, Ukraine, June 19-22,
          <year>2013</year>
          , Revised Selected Papers, pages
          <fpage>355</fpage>
          -
          <lpage>378</lpage>
          . Springer Inter-
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          <source>national Publishing</source>
          ,
          <year>2013</year>
          . https://doi.org/10.1007/978-3-
          <fpage>319</fpage>
          -03998-5_
          <fpage>18</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          John Wiley &amp; Sons, Inc., New York, NY, USA,
          <year>1992</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          <string-name>
            <surname>Education</surname>
          </string-name>
          , Research, and
          <string-name>
            <surname>Industrial</surname>
            Applications - 10th International Conference,
            <given-names>ICTERI</given-names>
          </string-name>
          <year>2014</year>
          ,
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          <string-name>
            <surname>Kherson</surname>
          </string-name>
          , Ukraine, June 9-12,
          <year>2014</year>
          , Revised Selected Papers, volume
          <volume>469</volume>
          of Communications in
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          <string-name>
            <surname>Computer</surname>
          </string-name>
          and Information Science, pages
          <fpage>117</fpage>
          -
          <lpage>138</lpage>
          . Springer,
          <year>2014</year>
          . https://doi.org/10.1007/
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>