<!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>Fifth Workshop on Formal Mathematics for Mathematicians at CICM</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>Computing the Border Array in Isabelle/HOL</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Štěpán Holub</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Department of Algebra, Faculty of Mathematics and Physics, Charles University</institution>
          ,
          <addr-line>Sokolovská 83, 186 75 Prague</addr-line>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2021</year>
      </pub-date>
      <volume>30</volume>
      <fpage>30</fpage>
      <lpage>31</lpage>
      <abstract>
        <p>An evaluable function computing the border array of a list in Isabelle/HOL is presented. The correctness of the function is verified in a straightforward lightweight manner, and it is applied to computation of other important properties of lists. A border of a word  (word is used as a preferred synonym for 'list' or 'string' in this text) is a word  (possibly empty, that is, of length 0) such that  ̸=  and  is both prefix and sufix of . The maximal border of  is its longest border. The concept of the border is complementary to the concept of a periodic root. If  =  ·  where  is a border of , then  is a periodic root of , that is,  is a prefix of  ·  ·  · · · (Here · denotes the concatenation, which is motivated by the fact words with concatenation form a monoid). Obviously, a border of  is determined by its length. The border array BA = [0, . . . , ||− 1] of  is the list (of the same length as , which is denoted here by ||) of natural numbers such that  is the length of the maximal border of the prefix of  of length  + 1. In particular, the last element of BA is the length of the maximal border of . The border array, possibly slightly modified and with diferent terminology, is a well known structure. In particular, the border array of the searched pattern plays a crucial role in the Knuth-Morris-Pratt text search algorithm (cf. the function  defined on p. 327 of [ 1]). Moreover, establishing the maximal border is itself a search task: the maximal border of  corresponds to (the beginning of) the first repeated occurrence of  in itself. It follows that using the eficient Knuth-Morris-Pratt algorithm for the computation of the maximal border of a word is equivalent to the first part of the algorithm, namely to the computation of the whole border array.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;border array</kwd>
        <kwd>Isabelle/HOL</kwd>
        <kwd>Knuth-Morris-Pratt algorithm</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
    </sec>
    <sec id="sec-2">
      <title>2. Motivation</title>
      <p>
        My interest in formalization of diferent aspects of periods of words, and therefore in (maximal)
borders, is motivated by the project formalizing combinatorics on words, see [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] and [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. The
maximal border can be used for characterization of several other important properties od the
word, for example for establishing its primitivity. The word is primitive if it is its own (trivial)
power only. In other words, a word is imprimitive if it is a power of a shorter word. For example,
 is imprimitive, while  is primitive. Denote the shortest periodic root of  by  ().
That is,  () is the shortest word such that  is a prefix of  () ·  () ·  () · · · . The following
lemma holds:
Lemma 1. The word  is imprimitive if and only if  () ̸=  and  () ·  =  ·  ().
Proof sketch. Let  = . Then both  and  () are periods of , which implies | ()| ≤ | |
by the minimality of  (). The proof is based on the fact (which is a weak version of the
Periodicity Lemma, see [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ])) that if || + | ()| ≥ | |, then  and  () are powers of the same
word (which is then also a periodic root of ). This implies that  () =  if  ≥ 2.
      </p>
      <p>On the other hand, words commute if and only if they are powers of the same word. The rest
is easy.</p>
      <p>If we denote the maximal border of  by  (), we have  =  () ·  (). Therefore, being
able to compute the maximal border yields an efective test of the primitivity of the word.</p>
    </sec>
    <sec id="sec-3">
      <title>3. The algoritm</title>
      <p>For convenience and future reference, I briefly and informally review a concrete version of the
well known algorithm computing the border array. Let  be the word whose maximal border
we want to compute. Fix the elements of  as  = [, − 1, . . . , 1, 0], where || =  + 1.
Following the order of construction of lists we shall process  from right to left, and construct
gradually its sufix border array , that is, a list [||, ||− 1, . . . , 1] of natural numbers where 
is the length of the maximal border of , which is the sufix of  of length .</p>
      <p>The construction uses, in addition to , three variables arr, pos and bord with the following
meaning. The integer pos indicates the currently processed position of , while arr is the
sufix border array of the already processed part. More precisely, we have  = 1 · 2, where
pos = |1| and |arr| = |2|. The list arr = [|2|, . . . , 1] of natural numbers is the sufix
border array of 2 (and a sufix of the computed sufix border array of ). The initial value of
bord (for a new pos) is |2|.</p>
      <p>The algorithm terminates with pos = 0. If pos ̸= 0, let  be the last element of 1. The
algorithm is currently looking for the maximal border of #2 (where # denotes insertion of
an element at the beginning of a list), and considers # as a candidate, where  is the prefix of
2 of length bord. Moreover  (or bord) has two additional properties:
•  is a border of 2;
• bord + 1 is an upper bound on the length of the maximal border of #2.
These conditions reflect the basic idea of the algorithm: if # is the maximal border of #2,
then  is a border of 2, and (in view of the second condition) also a border of , which is also
a sufix of 2. This dictates the next step: the algorithm compares  and bord.
• If  = bord, then # is the maximal border of #2. Then arr can be updated, and
the algorithm proceeds to pos − 1;
• If  ̸= bord, then there are two possibilities:
– if  is empty, then #2 is unbordered (its maximal border is empty), and we can
again proceed to pos − 1;
– otherwise, the next candidate is #′ where ′ is the maximal border of . The
length of ′ is stored in arr, namely, it is bord.</p>
    </sec>
    <sec id="sec-4">
      <title>4. Implementation and related work</title>
      <p>My formalization of the above described algorithm in Isabelle/HOL is realized by three functions:
kmp_arr, kmp_bord and kmp_pos which map the four parameters , arr, bord and pos to
updated values corresponding to the situation after one step of the algorithm. The algorithm
kmp is then a straightforward recursive call which is exited when pos = 0. The termination is
obtained easily by the lexicographic order on the pair (pos, bord).</p>
      <p>The invariant properties of variables described above are captured by the predicate
kmp_valid. The key task is then to prove lemma kmp_valid_step which shows that the
properties are indeed preserved by the above mentioned functions. That is, if (, arr, bord, pos)
satisfies the predicate, then also the quadruple</p>
      <p>︀( , kmp_arr( arr bord pos), kmp_bord( arr bord pos), kmp_pos( arr bord pos))︀
does. A fully structured commented proof in Isar language is given. Together with obvious
validity of the predicate for the initial quadruple (, [0], 0, || − 1), this establishes the
correctness proof, which is explicitly reformulated for the border_array (which simply inverts the
sufix border array yielded by kmp).</p>
      <p>Summarizing, our main goal is achieved by a pair of theorems. The correctness theorem
bord_array shows that the function border_array indeed computes the desired length of
maximal borders:
theorem bord_array: assumes Suc k ≤ | |</p>
      <p>w
shows (border_array w)!k = |max_border (take (Suc k) w)|
The function can be evaluated. For example,</p>
      <p>value border_array [5,4,5,3,5,5,5,4,5::nat]
yields</p>
      <p>
        [
        <xref ref-type="bibr" rid="ref1 ref1 ref1 ref1 ref2 ref3">0, 0, 1, 0, 1, 1, 1, 2, 3</xref>
        ]
This trivially leads to the code equation generating theorem max_border_comp that computes
the maximal border of .
      </p>
      <p>
        theorem max_border_comp [code]: max_border w = take ((border_array w)!(|w|− 1)) w
As can be seen, the described formalization is a handmade version of a verification process
which can be nowadays heavily automatized by the Isabelle Refinement Framework (IRF) by
Peter Lammich (see [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ], [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ], [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]). Moreover, this very framework has been used to
formalize the full Knuth-Morris-Pratt algorithm by Fabian Hellauer [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]. Specifically, our function
border_array (which is essentially the above mentioned function  of [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]) is defined (again
in a slightly modified form) in [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] via its specification, computed by means of the Refinement
Framework, and its correctness is then proved using tools of refinement automation (for example
18 gooals out of 22 in the correctness proof are discharged by the method vc_solve taking
several seconds). The theory [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] is well written, and it contains, unsurprisingly, many theorems
about borders that closely match some of my theorems and that I could easily reuse.
      </p>
      <p>Therefore, in view of the above work, the value of the formalization presented here is open
to judgment. I want to make two comments. First, using IRF brings about a significant overhead
(in terms of the bootstrapping time for example) which discourages me from making my theory
depend on it. In particular since the theory of borders is relatively simple layer of our larger
development. Second, it is not clear (to me) how easily the exported code can be used in
subsequent proofs in the way we need as indicated in Section 2, and described in the next
section.</p>
    </sec>
    <sec id="sec-5">
      <title>5. Towards the intended application</title>
      <p>Using appropriate code equations we are now able to evaluate several important properties of
particular words, like the maximal border, the minimal periodic root and the minimal period,
as well as predicates bordered and primitive. For example, primitivity can be tested using
Lemma 1 and the equality  =  () ·  () by the following four pieces of code leading to the
function KMP described in the previous section:
lemma primitive_if [code]: primitive w →← w ̸=  ∧ ( w = w ∨  w · w ̸= w ·  w)
lemma min_per_root_take [code]:  w = take (|w| − |max_border w|) w
lemma max_border_comp [code]: max_border w = take ((border_array w)!(|w|− 1)) w
fun border_array :: ′a list ⇒ nat list where</p>
      <p>border_array  = 
| border_array (a#w) = rev (KMP (rev (a#w)) [0] 0 (|a#w|− 1))
An obvious drawback is that the evaluation is available only for types that are of class equal,
which does not apply to general lists of type ’a list. In order to avoid this limitation, we
encode  into a binary alphabet (which is of class equal) by a simple function bin_encode:
fun bin_encode :: ′b ⇒ ′b ⇒ ′b ⇒ Enum.finite_2</p>
      <p>where bin_encode x y = ( z. (if z = x then Enum.finite_2 .a1 else Enum.finite_2 .a2))
Decoding function bin_decode is analogous. We can now prove that the encoded word is
primitive if and only if the original one is:
lemma prim_bin_enc_if : assumes x ̸= y and ws ∈ lists {x,y}</p>
      <p>shows primitive ws →← primitive (map (bin_encode x y) ws)</p>
      <sec id="sec-5-1">
        <title>Theories</title>
        <p>
          An archive version of the formalization described in this paper is available at [
          <xref ref-type="bibr" rid="ref9">9</xref>
          ] and consists of
four theories:
• CoWBasic.thy introduces large amount of properties of words extending further the
deafult theory List.thy and its extension Sublist.thy in Isabelle’s HOL-Library.
• Reverse_Symmetry.thy is an auxiliary theory for CoWBasic.thy automating generation
of reverse-symmetrical facts.
• Border_Array.thy contains the main material described in this paper.
• Spehner.thy illustrates a particular use of the primitivity predicate and the encoding
into the binary alphabet.
        </p>
        <p>
          A current (and possibly significantly modified) version of these theories is maintained as part of
our large development [
          <xref ref-type="bibr" rid="ref3">3</xref>
          ].
        </p>
        <p>Note added in proof: Recently, we implemented an alternative method of proving primitivity
of a word, which makes this particular application of the border array calculation slightly
outdated. We have also already formalized the theorem spehner, which was used without
proof (with sorry) for sake of illustration in the theory Spehner.thy (see [10]).</p>
      </sec>
      <sec id="sec-5-2">
        <title>Acknowledgements</title>
        <p>The author acknowledge support by the Czech Science Foundation grant GAČR 20-20621S.
[10] Š. Holub, M. Raška, Š. Starosta, Binary codes that do not preserve primitivity, IJCAR 2022,
accepted.</p>
      </sec>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>D. E.</given-names>
            <surname>Knuth</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. James H.</given-names>
            <surname>Morris</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V. R.</given-names>
            <surname>Pratt</surname>
          </string-name>
          ,
          <article-title>Fast pattern matching in strings</article-title>
          ,
          <source>SIAM Journal on Computing</source>
          <volume>6</volume>
          (
          <year>1977</year>
          )
          <fpage>323</fpage>
          -
          <lpage>350</lpage>
          . doi:
          <volume>10</volume>
          .1137/0206024.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>Š.</given-names>
            <surname>Holub</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Raška</surname>
          </string-name>
          , Š. Starosta, Combinatorics on words basics,
          <source>Archive of Formal Proofs</source>
          (
          <year>2021</year>
          ). https://isa-afp.org/entries/Combinatorics_Words.html,
          <source>Formal proof development.</source>
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>Š.</given-names>
            <surname>Holub</surname>
          </string-name>
          , Š. Starosta, et al.,
          <source>Combinatorics on words formalized</source>
          , https://gitlab.com/ formalcow/combinatorics-on
          <article-title>-words-</article-title>
          <string-name>
            <surname>formalized</surname>
          </string-name>
          ,
          <year>2021</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>N. J.</given-names>
            <surname>Fine</surname>
          </string-name>
          ,
          <string-name>
            <given-names>H. S.</given-names>
            <surname>Wilf</surname>
          </string-name>
          ,
          <article-title>Uniqueness theorems for periodic functions</article-title>
          ,
          <source>Proceedings of the American Mathematical Society</source>
          <volume>16</volume>
          (
          <year>1965</year>
          )
          <fpage>109</fpage>
          -
          <lpage>109</lpage>
          . URL: http://dx.doi.org/10.1090/ S0002-9939-1965-0174934-9. doi:
          <volume>10</volume>
          .1090/S0002-9939-1965-0174934-9.
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>P.</given-names>
            <surname>Lammich</surname>
          </string-name>
          ,
          <article-title>The imperative refinement framework, Archive of Formal Proofs (</article-title>
          <year>2016</year>
          ). https://isa-afp.org/entries/Refine_Imperative_HOL.html,
          <source>Formal proof development.</source>
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>P.</given-names>
            <surname>Lammich</surname>
          </string-name>
          , Refinement to imperative/hol, in: C.
          <string-name>
            <surname>Urban</surname>
            ,
            <given-names>X.</given-names>
          </string-name>
          Zhang (Eds.),
          <source>Interactive Theorem Proving - 6th International Conference, ITP</source>
          <year>2015</year>
          , Nanjing, China,
          <source>August 24-27</source>
          ,
          <year>2015</year>
          , Proceedings, volume
          <volume>9236</volume>
          of Lecture Notes in Computer Science, Springer,
          <year>2015</year>
          , pp.
          <fpage>253</fpage>
          -
          <lpage>269</lpage>
          . URL: https://doi.org/10.1007/978-3-
          <fpage>319</fpage>
          -22102-1_
          <fpage>17</fpage>
          . doi:
          <volume>10</volume>
          .1007/ 978-3-
          <fpage>319</fpage>
          -22102-1\_
          <fpage>17</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>P.</given-names>
            <surname>Lammich</surname>
          </string-name>
          ,
          <article-title>Refinement based verification of imperative data structures</article-title>
          , in: J.
          <string-name>
            <surname>Avigad</surname>
            ,
            <given-names>A</given-names>
          </string-name>
          . Chlipala (Eds.),
          <source>Proceedings of the 5th ACM SIGPLAN Conference on Certified Programs and Proofs</source>
          , Saint Petersburg, FL, USA, January
          <volume>20</volume>
          -
          <issue>22</issue>
          ,
          <year>2016</year>
          , ACM,
          <year>2016</year>
          , pp.
          <fpage>27</fpage>
          -
          <lpage>36</lpage>
          . URL: https://doi.org/10.1145/2854065.2854067. doi:
          <volume>10</volume>
          .1145/2854065.2854067.
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>F.</given-names>
            <surname>Hellauer</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Lammich</surname>
          </string-name>
          ,
          <article-title>The string search algorithm by Knuth, morris and pratt</article-title>
          ,
          <source>Archive of Formal Proofs</source>
          (
          <year>2017</year>
          ). https://isa-afp.org/entries/Knuth_Morris_Pratt.html,
          <source>Formal proof development.</source>
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>Š.</given-names>
            <surname>Holub</surname>
          </string-name>
          , Š. Starosta, et al.,
          <source>Combinatorics on words formalized</source>
          , https://gitlab.com/ formalcow/combinatorics-on-words-formalized/-/tree/Archive-Border-Array,
          <year>2021</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>