<!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 />
    <article-meta>
      <title-group>
        <article-title>Integration of SMT-LIB Support into Maple</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Stephen A. Forrest</string-name>
          <email>sforrest@maplesoft.com</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Maplesoft Europe Ltd.</institution>
          ,
          <addr-line>Cambridge</addr-line>
          ,
          <country country="UK">UK</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>The SC2 project arose out of the recognition that the Symbolic Computation and Satis ability Checking communities mutually bene t from the sharing of results and techniques. An SMT solver can pro t from the inclusion of computer algebra techniques in its theory solver, while a computer algebra system can pro t from dispatching SAT or SMT queries which arise as sub-problems during computation to a dedicated external solver; many existing implementations of both of these may be found. Here we describe on-going work in the second category: an API in Maple for dispatching computations to, and processing results from, an SMT solver supporting the SMT-LIB 2 format.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>1.1</p>
    </sec>
    <sec id="sec-2">
      <title>Introduction</title>
      <p>
        SC2, Maple, and SMT-LIB
The SC2 project [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] was established with a mandate to promote the
establishment of bridges between the Symbolic Computation and Satis ability Checking
communities in the form of common platforms and roadmaps. Applying this
principle to the software tools themselves, it is well understood that SMT solvers
can bene t substantially from incorporating computer algebra techniques such
as symbolic simpli cation or quanti er elimination in their theory solvers. [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]
This approach is realized in the implementation of several SMT solvers, such as
veriT [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] and SMT-RAT [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>
        The opposite task of incorporating SAT or SMT solving techniques into a
computer algebra system also has signi cant prior art. One such example from
the computer algebra system Redlog is the integration of learning strategies from
CDCL-based SMT solving into real quanti er elimination [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>
        We describe here a interface between Maple[
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] and an arbitrary SMT solver
implementing the SMT-LIB 2.0 [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] format. Maple is a computer algebra system
originally developed by members of the Symbolic Computation Group in the
Faculty of Mathematics at the University of Waterloo. Since 1988, it has been
developed and commercially distributed by Maplesoft (formally Waterloo Maple
Inc.), a company based in Waterloo, Ontario, Canada, with ongoing
contributions from a liated research centres. The core Maple language is implemented
in a kernel written in C++ and much of the computational library is written
in the Maple language, though the system does employ external libraries such
as LAPACK and the GNU Multiprecision Library (GMP) for special-purpose
computations.
      </p>
      <p>The SMT-LIB 2.0 standard de nes a language for writing terms and formulas
in a sorted version of rst-order logic, specifying background theories and logics,
and interacting with SMT solvers in order to impose and retract assertions and
inquire about their satis ability.
1.2</p>
      <p>
        SMT-like Queries in Maple
Consistent with Maple's roots as a computer algebra system, its solvers (such as
solve, dsolve, int) generally seek to provide the user with a general solution
to a posed problem which is both compact and useful. Further transformation
or simpli cation of such solutions using simpli ers based on heuristic methods
[
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] is often necessary.
      </p>
      <p>Nevertheless the approach of posing queries as questions about satis ability
or requests for a satisfying witness is not unknown in Maple, and its core routines
make regular use of satis ability queries in the course of symbolic simpli cation.</p>
      <p>
        The existing general-purpose commands in Maple for querying universal and
existential properties about a given expression are named is and coulditbe
respectively. [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ] The is command accepts an expression p and asks if p evaluates
to the value true for every possible assignment of values to the symbols in p. The
coulditbe command accepts identical syntax but asks if there is any assignment
of values to the symbols in p which could cause p to evaluate to true.
      </p>
      <p>Both is and coulditbe return results in ternary logic: true, false, or FAIL.
Both also make use of the \assume facility", which is a system for associating
Boolean properties with symbolic variables. This provides limits on the range
of possible assignments considered by is and coulditbe and is roughly
analogous to a type declaration. For example, the expression is(x^2&gt;=0) evaluates
to false because there are many possible values of x which do not evaluate
to nonnegative real numbers: it may be a symbol with no numeric value, an
expression of arbitrary size containing such a symbol, or simply the imaginary
unit p 1. By contrast, the expression is(x^2&gt;=0) assuming x::real returns
true because the range of possible values of x is limited to those corresponding
to real numbers.</p>
      <p>An illustrative example can be found with the Maple command product.
In the evaluation of the expression product(f(n),n=a..b), the system seeks
to compute a symbolic formula for the product Qbn=a f (n). As one can verify
by inspecting the source code with showstat(product), the implementation of
product computes a set of roots of f (n) and, if neither a nor b is in nite, checks
whether there exists a root r such that r is an integer and a r b. If so, it
returns zero as the result of the product. (Similar logic is applied if either of a
or b is in nite.)</p>
      <p>This is an example of what is essentially an SMT instance appearing as a
subproblem in the course of symbolic computation. Many examples of such queries
may be found in the Maple library; a common pattern is to pose an is query
(that is, verify that a speci ed condition holds universally) as a precondition to
applying a certain transformation.</p>
      <p>As evidence of the ubiquity of such queries, Table 1 summarizes the distinct
invocations of is and coulditbe encountered during a complete run through
Maplesoft's internal test suite for the Maple library performed on 26 July 2017.
This includes both instances in which the test case explicitly calls is/coulditbe
and instances in which is/coulditbe are invoked by other library functions such
as product, as shown previously.</p>
      <p>In total, 24085 distinct is and 5771 distinct coulditbe queries were issued
during the course of the test run. The inputs vary considerably in size and in the
complexity of the underlying theory, and for both is and coulditbe
approximately 11% of queries cannot be decided (i.e. return FAIL rather than true or
false). A complete list of queries encountered may be viewed at
https://doi.org/10.5281/zenodo.943349.</p>
      <p>It is probable that a signi cant subset of these queries are expressible in
the SMT-LIB 2 format. Explicitly dispatching such queries from Maple to an
external SMT solver could o er performance improvements and permit a broader
class of queries to be decided (i.e. return answers other than FAIL) than is
possible with analogous existing tools in Maple. More generally, the scope of
these queries serves to provide evidence that posing existential questions about
the satis ability of problems over the integers and real numbers in the style of
SMT is, in fact, a natural activity when doing computer algebra.
2</p>
    </sec>
    <sec id="sec-3">
      <title>Challenges</title>
      <p>The Maple language is loosely-typed and permits identi ers which have not been
previously de ned to be freely used in algebraic expressions, with the
understanding that such identi ers represent symbolic indeterminates. Maple
therefore places no requirements on the user to provide an advance declaration of the
mathematical domain associated with or theory underlying the input expression.
Maple does possess a facility with which additional properties about symbols
may be speci ed using the commands assume or assuming. In general however
the e ective interpretation of symbols is imposed by the particular command
being invoked: for example, coeffs(x2^+3,x) interprets x as a transcendental
element while evalc((x+I)2^) interprets x as a real number.</p>
      <p>This overall exibility presents a signi cant obstacle to translating an
arbitrary algebraic expression from Maple to SMT-LIB: we must either oblige a user
to specify the SMT-LIB logic underlying the expression and the type of each
symbol explicitly, or attempt to detect them.
3</p>
    </sec>
    <sec id="sec-4">
      <title>Results</title>
      <p>We present a work-in-progress Maple package, SMTLIB, designed to facilitate
interaction with an SMT solver supporting the SMT-LIB standard. This package
o ers three commands: ToString, Satisfiable, and Satisfy.</p>
      <p>The rst of these, ToString, accepts a Maple expression and returns a string
output containing an SMT-LIB 2.0 script. By default, this simply asserts the
truth of the expression corresponding to the Maple input and requests a satis
ability check (i.e. (check-sat)). It does not explicitly invoke an SMT solver, but
merely returns the input which would be sent to one if Satisfiable or Satisfy
were invoked.</p>
      <p>The SMT-LIB logic may be explicitly speci ed or inferred. In the following
example, we ask about the sati ability of x2+1 = 0 while instructing ToString to
use the SMT-LIB logic QF LRA (quanti er-free linear real arithmetic), implicitly
forcing the variable x to be real. (Note that the input line is preceded by &gt; and
output lines follow afterwards.)
&gt; SMTLIB:-ToString( x^2+1=0, logic="QF_LRA" );
"(set-logic QF_LRA)
(declare-fun x () Real)
(assert (= (+ (* x x) 1) 0))
(check-sat)
(exit)"</p>
      <p>
        In the event that the logic is not speci ed, ToString will attempt to choose
the \smallest" SMT-LIB logic in which the input can be represented, according
to the partial order on SMT-LIB logics described in [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ]. That is it will choose
a logic which is su cient to represent the input expression, and which will be a
sub-logic of any logic in which the input can be represented.
      </p>
      <p>If we repeat the previous command while leaving the logic unspeci ed, ToString
defaults to using the logic QF NIA (quanti er-free nonlinear integer arithmetic)
because that is the minimal logic in which both the integer addition and the
square term can be represented.
&gt; SMTLIB:-ToString( x^2+1=0 );
"(set-logic QF_NIA)
(declare-fun x () Int)
(assert (= (+ (* x x) 1) 0))
(check-sat)
(exit)"</p>
      <p>
        The Satisfiable and Satisfy commands simply generate SMT-LIB scripts
(which request a satis ability check and a satisfying witness, respectively) and
dispatch the query to an SMT solver. By specifying the path to the executable for
the SMT solver, an arbitrary SMT-LIB compliant solver may be used, though the
default implementation uses Z3 [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ]. The output of the SMT solver is parsed and
returned as a corresponding Maple object: a Boolean result (for Satisfiable)
or either a satisfying assignment or the value NULL.
      </p>
      <p>The following examples rst con rm that a satisfying assignment exists and
then returns a satisfying assignment for the equation w3 +x3 = y3 +z3 in positive
integers where w 6= y; w 6= z:
The SMTLIB Maple package presents a concrete example of a computer algebra
system e ectively harnessing the power of an SMT solver. The fact that its
interface is su ciently generic to be uncoupled from any particular SMT solver
stands as testimony to the bene t of the widespread adoption of the SMT-LIB
standard by implementors of SMT solvers.</p>
      <p>This implementation nevertheless currently leaves a considerable portion of
the functionality de ned in the SMT-LIB 2 standard unexploited, including
definition of new theories and use of the command language for interacting with
assertions via stack push/pop operations.
5</p>
    </sec>
    <sec id="sec-5">
      <title>Future Work</title>
      <p>The inclusion of the SMTLIB package in Maple provides a facility for users
explicitly interested in interacting with an SMT solver. In future, we aim to
examine the utility of using as a general-purpose tool for solving SMT instances
which arise during evaluation of symbolic expressions.</p>
      <p>An important factor in this assessment will be whether this implementation
o ers better performance and meaningful answers (not FAIL) for a larger class
of such queries than existing tools in Maple.</p>
      <p>We also hope to nd ways of meaningfully incorporating other parts of the
SMT-LIB 2 speci cation into the Maple interface. At present, in the case of
unsatis ability users of the interface must accept a mere false or NULL; they would
bene t from access to concrete evidence of unsatis ability (e.g. an unsatis able
core).</p>
      <p>The interface would also bene t from the addition of support for other
existing SMT-LIB types, including
{ Uninterpreted functions (QF UF and logics which extend it)
{ Arrays
{ Bit vectors
{ Floating-point arithmetic</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>E.</given-names>
            <surname>Abraham</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Abbott</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Becker</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.M.</given-names>
            <surname>Bigatti</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Brain</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Buchberger</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Cimatti</surname>
          </string-name>
          ,
          <string-name>
            <surname>J.H. Davenport M. England</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          <string-name>
            <surname>Fontaine</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          <string-name>
            <surname>Forrest</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Griggio</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          <string-name>
            <surname>Kroening</surname>
            ,
            <given-names>W.M.</given-names>
          </string-name>
          <string-name>
            <surname>Seiler</surname>
            , and
            <given-names>T.</given-names>
          </string-name>
          <string-name>
            <surname>Sturm</surname>
          </string-name>
          . SC2:
          <article-title>Satis ability Checking Meets Symbolic Computation</article-title>
          . In: M.
          <string-name>
            <surname>Kohlhase</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Johansson</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          <string-name>
            <surname>Miller</surname>
          </string-name>
          , L. de Moura, F. Tompa, eds.,
          <source>Intelligent Computer Mathematics (Proceedings of CICM 2016)</source>
          , pp.
          <fpage>28</fpage>
          -
          <lpage>43</lpage>
          , (Lecture Notes in Computer Science,
          <volume>9791</volume>
          ). Springer International Publishing,
          <year>2016</year>
          . http://www.sc-square.org/Papers/CICM16.pdf.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>Erika</given-names>
            <surname>Abraham</surname>
          </string-name>
          .
          <year>2015</year>
          .
          <article-title>Building Bridges between Symbolic Computation and Satisability Checking</article-title>
          .
          <source>In Proceedings of the 2015 ACM on International Symposium on Symbolic and Algebraic Computation (ISSAC</source>
          <year>2015</year>
          ). ACM, New York, NY, USA,
          <fpage>1</fpage>
          -
          <lpage>6</lpage>
          . doi:
          <volume>10</volume>
          .1145/2755996.2756636.
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>Thomas</given-names>
            <surname>Bouton</surname>
          </string-name>
          , Diego Caminha B. de Oliveira, David Deharbe,
          <string-name>
            <surname>Pascal Fontaine.</surname>
          </string-name>
          (
          <year>2009</year>
          ).
          <article-title>veriT: An Open, Trustable and E cient SMT-Solver</article-title>
          .
          <fpage>151</fpage>
          -
          <lpage>156</lpage>
          . doi:
          <volume>10</volume>
          .1007/978-3-
          <fpage>642</fpage>
          -02959-2
          <fpage>12</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>Florian</given-names>
            <surname>Corzilius</surname>
          </string-name>
          , Ulrich Loup,
          <string-name>
            <given-names>Sebastian</given-names>
            <surname>Junges</surname>
          </string-name>
          , and
          <string-name>
            <given-names>Erika</given-names>
            <surname>Abraham</surname>
          </string-name>
          .
          <year>2012</year>
          .
          <article-title>SMTRAT: an SMT-compliant nonlinear real arithmetic toolbox</article-title>
          .
          <source>In Proceedings of the 15th international conference on Theory and Applications of Satis ability Testing (SAT'12)</source>
          ,
          <source>Alessandro Cimatti and Roberto Sebastiani (Eds.)</source>
          . Springer-Verlag, Berlin, Heidelberg,
          <fpage>442</fpage>
          -
          <lpage>448</lpage>
          . doi:
          <volume>10</volume>
          .1007/978-3-
          <fpage>642</fpage>
          -31612-8
          <fpage>35</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>Konstantin</given-names>
            <surname>Korovin</surname>
          </string-name>
          , Marek Kosta, Thomas Sturm.
          <article-title>Towards Con ict-Driven Learning for Virtual Substitution</article-title>
          . Vladimir P. Gerdt, Wolfram Koepf,
          <string-name>
            <surname>Werner M. Seiler</surname>
          </string-name>
          ,
          <string-name>
            <surname>Evgenii</surname>
            <given-names>V.</given-names>
          </string-name>
          <string-name>
            <surname>Vorozhtsov</surname>
          </string-name>
          . Computer Algebra in Scienti c Computing - 16th
          <source>International Workshop, CASC</source>
          <year>2014</year>
          ,
          <year>2014</year>
          , Warsaw, Poland. Springer,
          <volume>8660</volume>
          , pp.
          <fpage>256</fpage>
          -
          <lpage>270</lpage>
          ,
          <year>2014</year>
          , Lecture Notes in Computer Science. doi:
          <volume>10</volume>
          .1007/978-3-
          <fpage>319</fpage>
          -10515-4
          <fpage>19</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>Maple</given-names>
            <surname>Programming</surname>
          </string-name>
          <string-name>
            <surname>Guide</surname>
          </string-name>
          , Toronto: Maplesoft,
          <article-title>a division of Waterloo Maple Inc</article-title>
          .,
          <fpage>2005</fpage>
          -
          <lpage>2016</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Clark</surname>
            <given-names>Barrett</given-names>
          </string-name>
          , Pascal Fontaine, and
          <string-name>
            <given-names>Cesare</given-names>
            <surname>Tinelli</surname>
          </string-name>
          .
          <article-title>The Satis ability Modulo Theories Library (SMT-LIB)</article-title>
          , http://www.smt-lib.org,
          <year>2016</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>Jacques</given-names>
            <surname>Carette</surname>
          </string-name>
          .
          <year>2004</year>
          .
          <article-title>Understanding Expression Simpli cation</article-title>
          .
          <source>In Proceedings of the 2004 International Symposium on Symbolic and Algebraic Computation</source>
          , Santander,
          <source>Spain (ISSAC</source>
          <year>2004</year>
          ), ACM, New York, NY, USA,
          <fpage>72</fpage>
          -
          <lpage>79</lpage>
          . doi:
          <volume>10</volume>
          .1145/1005285.1005298.
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          <article-title>9. The Assume Facility in Maple, Maple Online Help : The Assume Facility</article-title>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>10. Logics in SMT-LIB, http://smtlib.org/logics.shtml.</mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <surname>de Moura</surname>
            ,
            <given-names>L. M.</given-names>
          </string-name>
          , and Bj rner,
          <string-name>
            <surname>N.</surname>
          </string-name>
          <article-title>Z3: an e cient SMT solver</article-title>
          .
          <source>In TACAS</source>
          (
          <year>2008</year>
          ), vol.
          <volume>4963</volume>
          of Lecture Notes in Computer Science, Springer, pp.
          <fpage>337340</fpage>
          . https://github.com/Z3Prover/z3.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>