<!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>Corresponding author.
$ stepan.starosta@fit.cvut.cz (Š. Starosta)
 https://users.fit.cvut.cz/~staroste/ (Š. Starosta)</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>Producing symmetrical facts for lists induced by the list reversal mapping in Isabelle/HOL</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Martin Raška</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Štěpán Starosta</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Faculty of Information Technology, Czech Technical University in Prague</institution>
          ,
          <addr-line>Thákurova 9, 160 00 Praha 6</addr-line>
          ,
          <country country="CZ">Czech Republic</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Faculty of Mathematics and Physics, Charles University</institution>
          ,
          <addr-line>Ke Karlovu 2027/3, 121 16 Praha 2</addr-line>
          ,
          <country country="CZ">Czech Republic</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2021</year>
      </pub-date>
      <volume>000</volume>
      <fpage>0</fpage>
      <lpage>0002</lpage>
      <abstract>
        <p>Many facts possess symmetrical counterparts that often require a separate formal proof, depending on the nature of the involved symmetry. We introduce a method in Isabelle/HOL which produces such a symmetrical fact for the list datatype and the symmetry induced by the list reversal mapping. The method is implemented as an attribute and its result is based on user-declared symmetry rules. Besides general rules, we provide rules that are aimed to be applied in the domain of Combinatorics on Words.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;combinatorics on words</kwd>
        <kwd>mathematics formalizations</kwd>
        <kwd>reversal symmetry</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        While formalizing a piece of mathematical knowledge, one probably hopes that some part of
the tedious work will be done by the machine. One such mechanical tasks are proofs that follow
“by symmetry” which can be seen as a variation of “without loss of generality” [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. One such “by
symmetry” usually stands for a proper description of the symmetry involved and the procedure
of how lemmas involving the symmetry should be used to obtain the symmetrical claim.
      </p>
      <p>
        In this article, we exhibit a partial, yet quite useful, solution to “by symmetry” in the case of
lists and the reversal mapping in the proof assistant Isabelle/HOL [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. The reversal, or mirror
mapping, is the mapping reversing the order of elements in a list. This mapping interconnects
many pairs of definitions over lists in the spirit of the following duality: the list  is a prefix of
the list  if and only if the reversal of  is a sufix of the reversal of . We situate this solution
in the context of Combinatorics on words, a mathematical domain which studies words, i.e.,
lists and their various properties including equations on words.
      </p>
      <p>
        First, we give a short overview of mathematical context along with examples of the symmetry
in question. In Section 3, we shortly describe possible approaches to the solution and then we
describe our solution which is part of the ongoing project of formalization of Combinatorics
on Words [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. We conclude by describing the limits of the current solution in Section 4 and
conclude by final remarks in Section 5
      </p>
    </sec>
    <sec id="sec-2">
      <title>2. Mathematical context and examples of the symmetry</title>
      <p>We work with words, which are finite sequences ()=0 with  ∈  with  usually being a
ifnite set. The set of all words over  is denoted * (where * is the Kleene star). The reversal
mapping, denoted rev, is a mapping * → * which maps the word  = ()=0 to the word
rev () = (− )=0, or simply put, it reads the letters of the word in the reverse order. The
reversal mapping is an involutive antimorphism with respect to the operation of concatenation
of two words, that is, rev ∘ rev = id and rev ( · ) = rev () · rev () where · is the binary
operation of concatenation. It follows that rev is also a bijection.</p>
      <p>
        In our ongoing project [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] of formalization of Combinatorics on Words, we formalize many
elementary preparatory lemmas dealing with a handful of notions. Many of these notions have
a symmetrical counterpart, and many facts are symmetrical, and their proof is just copy and
paste of the proof of the original lemma. We continue with examples that exhibit this symmetry.
2.1. Example 1
If a word  can be written as a concatenation of two words  and , i.e.,  =  · , we say
that  is a prefix of  and  is a sufix . An elementary example of a symmetrical pair of claims
involving prefix and sufix is the following.
      </p>
      <p>Lemma 1. If  is a prefix of , then  a prefix of  · .</p>
      <p>Proof. If  is a prefix of , there exists a word  such that  =  · . Hence,  ·  =  ·  · , and
 is a prefix of  · .</p>
      <p>By the symmetrical counterpart of Lemma 1 we mean the following claim.</p>
      <p>Lemma 2. If  is sufix of , then  is a sufix of  · .</p>
      <p>Its proof can be done as the presented proof of Lemma 1, however, stating in follows “by
symmetry” from Lemma 1 would be no exception in literature.</p>
      <p>In order to formally exploit the symmetry in a full proof, we have to make the intended
symmetry between a prefix and a sufix explicit:
Lemma 3. The word  is a prefix of  if and only if rev () is a sufix of rev ().</p>
      <p>A full proof of Lemma 2, by symmetry, is as follows.</p>
      <p>Proof of Lemma 2. Fix  and assume that  is a sufix of . Let ′, ′, and ′ be the words such
that
As the reversal is an involution, it follows that
′ = rev (), ′ = rev ()
 = rev (︀ ′)︀ ,  = rev (︀ ′)︀
and
and
′ = rev ().
 = rev (︀ ′)︀ .</p>
      <p>As  is a sufix of , the word rev (′) is a sufix of rev (′). By Lemma 3, ′ is a prefix of ′.
Using Lemma 1, ′ is a prefix of ′ · ′. Again, by Lemma 3, rev (′) is a sufix of rev (′ · ′).
Since</p>
      <p>rev (︀ ′ · ′)︀ = rev (︀ ′)︀ · rev (︀ ′)︀ =  · ,
we conclude that  is a sufix of  · .
2.2. Example 2
Since the next examples are in the framework of Isabelle/HOL, we first recall our setting. A
word is represented by the datatype of list, which is is specified via 2 constructors: Nil (denoted
[]), the empty list/word, and Cons (denoted #), the recursive constructor allowing to add an
element to the list at its beginning. The reversal mapping is represented by the function rev:
primrec rev :: ′a list ⇒ ′a list where
rev [] = [] |
rev (x # xs) = rev xs @ [x]
with @ being the notation for list append, i.e., concatenation of two words.</p>
      <p>The predicates for prefix and sufix are already part of the Isabelle distribution in the theory
HOL-Library.Sublist:
definition prefix :: ′a list ⇒ ′a list ⇒ bool where prefix xs ys →←
definition sufix :: ′a list ⇒ ′a list ⇒ bool where sufix xs ys</p>
      <p>The second example is constituted by the pair of symmetric definitions of the first and the
last letter of a word, i.e., element of a list. In Isabelle/HOL, the first element of a list is its head,
realized as one of two selectors, named hd, of the list constructor Cons. The last letter is the
recursive function last:
primrec (nonexhaustive) last :: ′a list ⇒ ′a where</p>
      <p>last (x # xs) = (if xs = [] then x else last xs)
given in the main theory List. To obtain a simple enough symmetry rule for hd and last, it
sufices to notice that they behave the same way on the empty list.</p>
      <p>lemma hd_last_Nil: hd [] = last [] unfolding hd_def last_def by simp
lemma hd_rev_last: hd(rev xs) = last xs by (induct xs, simp add: hd_last_Nil, simp)</p>
      <sec id="sec-2-1">
        <title>The pair of symmetrical claims is the following.</title>
        <p>lemma example2: u ̸= [] =⇒ prefix u v =⇒ hd u = hd v
lemma example2_sym: u ̸= [] =⇒ sufix u v</p>
        <p>=⇒ last u = last v</p>
        <p>The goal is to obtain example2_sym from example2 by symmetry. We proceed analogously
to the proof of Lemma 2 above using standard methods in Isabelle/HOL:
example2[of rev u rev v, unfolded rev_is_Nil_conv sufix_to_prefix
[symmetric] hd_rev_last]
where rev_is_Nil_conv is (rev xs = []) = (xs = []) and
suffix_to_prefix[symmetric] is prefix (rev xs) (rev ys) = suffix xs ys. That
is, we instantiate every variable of example2 by its reversal to obtain
rev u ̸= [] =⇒ prefix (rev u) (rev v) =⇒ hd (rev u) = hd (rev v),
and then rewrite the terms using appropriate symmetry rules, via the unfolded attribute
(which is analogous to what was done in the proof of Lemma 2 above). We end up with
example2_sym and the proof by symmetry is done.
2.3. Example 3
The next example is the following pair of symmetric facts.
Applying the same strategy as for example2 fails, since trying to obtain example3_sym from
example3[of rev u rev p rev w rev q, unfolded symmetry_rules],
where symmetry_rules is a list of appropriate symmetry rules, leaves us with
which is not yet in the form of example3_sym. This is not unexpected, the claim
contains a bound variable r. To finish the conversion, it sufices to realize that (∃.  ()) ↔
(∃.  (rev )) holds. Thus, we may replace the last two occurrences of r with rev r, and
apply appropriate symmetric rules.</p>
        <p>The next section addresses our realization of automatic production of symmetric rules,
preceded by a discussion on the use of existing tools.</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>3. Automated production of symmetrical claims</title>
      <p>Before describing our solution to the automation of producing symmetrical claims, we discuss
if and how might our task be achieved using tools for theorem reuse available in Isabelle/HOL.
We have not found any ready made tool in Isabelle/HOL that could achieve our objectives. We
shall briefly discuss two existing tools that achieve a similar task, namely reusing of a theory in
a homomorphic setting.</p>
      <p>
        The first tool are locales. Locale is a mechanism for abstraction via interpretation and locale
expressions [
        <xref ref-type="bibr" rid="ref4 ref5">4, 5</xref>
        ]. We could see the “by symmetry” argument as two instantiations of the same
claim: first in lists, and second in reversed lists. It would require to prove all claims about lists
in an abstract setting, and then apply it to lists and reversed lists. The abstract setting would
mean some kind of “axiomatic theory of lists”, that is, of free monoids, as in [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. While this may
be the correct idea mathematically, we do not see how to naturally recreate it in Isabelle/HOL
using locales.
      </p>
      <p>
        The second tool is the infrastructure of transfer [
        <xref ref-type="bibr" rid="ref7 ref8">7, 8</xref>
        ]. Its main purpose is to transfer facts
between two datatypes, e.g., from natural integers to integers, via user specified transfer rules.
Although, in principle, it should be possible to use this powerful tool, we encountered several
problems using it and we did not find a way how to employ it for our purposes without
producing undesired limitations. For example, it is not clear how to specify whether in the case
of transferring a fact containing ′a list list the transfer rule should be applied to ′a list
or ′a list list = ′b list.
      </p>
      <p>Since it seems from the above discussion that there is no direct way how to achieve the desired
automation of the symmetry, we propose a “lightweight” solution which closely mimics the
simple reproving of each individual claim “on the fly” as indicated by the examples in Section 2.
Our solution is very simple but at the same time it proves to be very practical and suficiently
versatile.</p>
      <p>It is created as a single attribute called “reversed”. The symmetry rules are collected as a list
of theorems called “reversal_rule”, i.e., a user can add and remove them any time. By default,
rules are required to eliminate reversal images, thus the reversal images are supposed to be
on the left side of the equalities serving as rules. For instance, the symmetry rule Lemma 3 is
stored in this form
sufix (rev p) (rev w) = prefix p w.</p>
      <p>The execution follows examples of Sections 2.2 and 2.3: first, all schematic variables of type
list of the fact being reversed are instantiated by their reversals. Before the application of the
symmetry rules, bound variables need to be treated. Let us indicate this procedure on example3
of Section 2.3 which contains one bound variable. As indicated above, the idea is to use the
equivalence (∃.  ()) ↔ (∃.  (rev )). We introduce a helper (private) definition and 2
claims as follows:
definition Ex_rev_wrap :: ( ′a list ⇒ bool) ⇒ bool</p>
      <p>where Ex_rev_wrap P = (∃ x. P (rev x))
lemma Ex_rev_wrapI: ∃ x. P x ≡ Ex_rev_wrap P
lemma Ex_rev_wrapE: Ex_rev_wrap ( x. P x) ≡ ∃
x. P (rev x)</p>
      <sec id="sec-3-1">
        <title>The application of these claims can be seen as</title>
        <p>example3[of rev u rev p rev w rev q,unfolded Ex_rev_wrapI],
which yields
The next step is
resulting in
example3[of rev u rev p rev w rev q,unfolded Ex_rev_wrapI, unfolded Ex_rev_wrapE]
prefix (rev u) (rev p @ rev w @ rev q) =⇒ length (rev p) ≤ length (rev u) =⇒
length (rev u) ≤ length (rev p @ rev w) =⇒ ∃ r. rev u = rev p @ rev r ∧ prefix (rev r) (rev
w).</p>
        <p>Note that the name of the bound variable is preserved in this step. It is due to ( x. P x)
being present in Ex_rev_wrapE rather than just P.</p>
        <p>This two step rewriting using the definition Ex_rev_wrap in the intermediate step is to
prevent an infinite loop of rewriting while trying to go directly from ∃ x. P x to ∃ x. P (rev
x).</p>
        <p>The last form is ready for the application of symmetry rules, and we almost obtain our goal,
example3_sym. The remaining diference is the order of application of @, i.e., the arrangement
of parentheses. The operation @ is associative and this final adjustment is left to be done
manually, if desirable.</p>
        <p>
          The implementation deals with other types of bound variables in a similar manner using a
definition analogous to Ex_rev_wrap and its two associated wrapping and unwrapping rules.
In a similar spirit, a special care for the constructors Nil and Cons is also part of the reversing
process. The described implementation is available at [
          <xref ref-type="bibr" rid="ref9">9</xref>
          ].
        </p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>4. Limits of the approach</title>
      <p>To show the current limits, consider the following pair of symmetric claims:
lemma example4: prefix ps ws =⇒ prefix (concat ps) (concat ws)
lemma example4_sym: sufix ps ws</p>
      <p>=⇒ sufix (concat ps) (concat ws)</p>
      <sec id="sec-4-1">
        <title>Applying the attribute reversed on example4 produces:</title>
        <p>sufix ps ws</p>
        <p>=⇒ prefix (concat (rev ps)) (concat (rev ws))</p>
        <p>The problem here is that we are dealing with variables of type ′a list list, representing
factorizations or decomposition of words. As they are of type ′b list, the reversing happens
only on this level, whereas to produce example4_sym one would need the reversing to act on
′a list = ′b. Namely, the additional required action of the symmetry on ps and ws is the
application of map rev. The reason that this represents a current limit is that the choice of
correct reversal rules for variables of type ′a list list becomes crucial and it is no more
clear what are the correct reversal rules.</p>
      </sec>
    </sec>
    <sec id="sec-5">
      <title>5. Concluding remarks</title>
      <p>
        Although the implemented attribute seems to be very simple, together with many delicately
selected reversal rules it is very useful in our current project of formalization of Combinatorics
on Words [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
      </p>
      <p>As the attribute is a part of a living project, and the time period between the acceptance and
publication of this article was noticeable, the obstacle exhibited in the previous section has been
already surmounted in a way to suit the needs of the project. However, the goal to properly deal
with variables of any type remains. In order to do that, our tentative model of the symmetry in
question needs to be generalized and validated.</p>
      <p>Acknowledgements
The authors acknowledge support by the Czech Science Foundation grant GAČR 20-20621S.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>J.</given-names>
            <surname>Harrison</surname>
          </string-name>
          ,
          <article-title>Without loss of generality</article-title>
          , in: S. Berghofer,
          <string-name>
            <given-names>T.</given-names>
            <surname>Nipkow</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Urban</surname>
          </string-name>
          , M. Wenzel (Eds.),
          <source>Theorem Proving in Higher Order Logics</source>
          , Springer Berlin Heidelberg, Berlin, Heidelberg,
          <year>2009</year>
          , pp.
          <fpage>43</fpage>
          -
          <lpage>59</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>T.</given-names>
            <surname>Nipkow</surname>
          </string-name>
          , M. Wenzel, L. C. Paulson (Eds.), Isabelle/HOL, Springer Berlin Heidelberg,
          <year>2002</year>
          . doi:
          <volume>10</volume>
          .1007/3-540-45949-9.
        </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>C.</given-names>
            <surname>Ballarin</surname>
          </string-name>
          ,
          <article-title>Tutorial to locales and locale interpretation</article-title>
          , in: L.
          <string-name>
            <surname>Lambán</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Romero</surname>
          </string-name>
          , J. Rubio (Eds.), Contribuciones Científicas en Honor de Mirian Andrés Gómez,
          <string-name>
            <surname>Servicio de Publicaciones de la Universidad de La Rioja</surname>
          </string-name>
          , Logroño, Spain,
          <year>2010</year>
          .
          <article-title>Also part of the Isabelle user documentation</article-title>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>C.</given-names>
            <surname>Ballarin</surname>
          </string-name>
          ,
          <article-title>Locales: A module system for mathematical theories</article-title>
          ,
          <source>Journal of Automated Reasoning</source>
          <volume>52</volume>
          (
          <year>2014</year>
          )
          <fpage>123</fpage>
          -
          <lpage>153</lpage>
          . URL: https://doi.org/10.1007/s10817-013-9284-7. doi:
          <volume>10</volume>
          .1007/s10817-013-9284-7.
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>Š.</given-names>
            <surname>Holub</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Verof</surname>
          </string-name>
          ,
          <article-title>Formalizing a fragment of combinatorics on words</article-title>
          , in: J.
          <string-name>
            <surname>Kari</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          <string-name>
            <surname>Manea</surname>
          </string-name>
          , I. Petre (Eds.),
          <source>Unveiling Dynamics and Complexity</source>
          , Springer International Publishing, Cham,
          <year>2017</year>
          , pp.
          <fpage>24</fpage>
          -
          <lpage>31</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>B.</given-names>
            <surname>Hufman</surname>
          </string-name>
          ,
          <string-name>
            <given-names>O.</given-names>
            <surname>Kunčar</surname>
          </string-name>
          ,
          <article-title>Lifting and transfer: A modular design for quotients in isabelle/hol</article-title>
          , in: G. Gonthier, M. Norrish (Eds.),
          <source>Certified Programs and Proofs</source>
          , Springer International Publishing, Cham,
          <year>2013</year>
          , pp.
          <fpage>131</fpage>
          -
          <lpage>146</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>O.</given-names>
            <surname>Kunčar</surname>
          </string-name>
          , Types, Abstraction and Parametric Polymorphism in Higher-Order
          <string-name>
            <surname>Logic</surname>
          </string-name>
          ,
          <source>Ph.D. thesis</source>
          , Technischen Universität München, Germany,
          <year>2016</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <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, et al.,
          <article-title>Combinatorics on words formalized: Reversal symmetry</article-title>
          , https://gitlab.com/formalcow/combinatorics-on-words-formalized/-/tree/ Archive-Reversal-Symmetry,
          <year>2021</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>