<!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>On the development of a logic calculator: a novel tool to perform logical operations</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Oscar Chavez-Bosquez</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Pilar Pozos-Parra</string-name>
          <email>pilar.pozosg@ujat.mx</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Kevin McAreavey</string-name>
          <email>kevin.mcareavey@qub.ac.uk</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>School of Electronics, Electrical Engineering and Computer Science, Queen's University Belfast</institution>
          ,
          <addr-line>Northern</addr-line>
          <country country="IE">Ireland</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Universidad Juarez Autonoma de Tabasco, Division Academica de Informatica y Sistemas. Cunduacan</institution>
          ,
          <addr-line>Tabasco</addr-line>
          ,
          <country country="MX">Mexico</country>
        </aff>
      </contrib-group>
      <fpage>9</fpage>
      <lpage>16</lpage>
      <abstract>
        <p>In this paper we propose a Logic Calculator with three operation modes: evaluation of logical formulae; logical entailment and conversion of a formula to Disjunctive Normal Form (DNF) and Conjunctive Normal Form (CNF). It is worth noting there are no existing automated tools for these three computations integrated into one tool providing a graphical calculator format. Logic Calculator is available at https://sourceforge.net/p/logiccalculator.</p>
      </abstract>
      <kwd-group>
        <kwd>propositional logic</kwd>
        <kwd>logical entailment</kwd>
        <kwd>disjunctive normal form</kwd>
        <kwd>conjunctive normal form</kwd>
        <kwd>open source tool</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>In this paper we describe a helpful tool developed for the automatic
computation of logic operations: the Logic Calculator. We show the development of the
calculator and describe its functionality: (i) Evaluation of logical formulae. In
this mode, it evaluates the basic Boolean operations: negation; conjunction;
disjunction; conditional and bi-conditional. The user can insert the logical formula
and the Logic Calculator displays the truth table along with the models of the
formula. (ii) Logical entailment. In this mode, the user can insert a number of
premises followed by a number of conclusions, so the Logic Calculator displays
the truth table of each premise/conclusion and the result of whether or not these
premises logically entail the given conclusions. (iii) Conversion to Disjunctive
Normal Form (DNF) and Conjunctive Normal Form (CNF). In this mode, the
user inserts a logical formula and the Logic Calculator outputs its representation
in both DNF and CNF. The main bene t of the Logic Calculator is the
automatic evaluation of complex logic operations where manual calculations may be
too di cult and take a considerable time, while there may also be a lack of
con dence concerning the logic results obtained by hand.</p>
    </sec>
    <sec id="sec-2">
      <title>Preliminaries</title>
      <p>In this paper we consider the language L of propositional logic formed from
the nite set of P24 = fa; b; : : : ; t; w; x; y; zg variables, where 24 denotes the
maximum available number of variables in the Logic Calculator.</p>
      <p>The set = f:; ^; _; !; $g denotes valid logical operators.</p>
      <p>
        A Well Formed Formula (WFF), also called formula or sentence, is a
propositional formula containing a subset of variables from P24 and a subset of
operators from . Parentheses are used in the traditional way [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ].
      </p>
      <p>We can de ne a WFF as follows:
{ An atomic formula (atom or variable) is a formula.
{ If is a formula, then : is a formula.
{ If and are formulas, and is a binary connective, then</p>
      <p>Where 2 n f:g is a binary connectives.
is a formula.</p>
      <p>
        A truth table is a list of all possible truth assignments (possible worlds) for
the set of variables. Such a table typically contains several rows and columns,
with the header representing the variables and the WFF. The truth table is
composed of one column for each input variable and one nal column for all possible
results of the logical operation that the table represents [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. Rows represent every
possible interpretation and the truth-values that would occur. Each one of these
combinations is called an interpretation, such as an assignment of truth-values
to all of the variables in the formula. [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>A model of a propositional formula is a possible interpretation where is
true under the interpretation in the classical truth functional manner. If there
is a model for , then is said to be satis able.</p>
      <p>
        When we want to know whether a set of sentences is a logical conclusion of
another set of sentences, this is known as logical entailment [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]. A set of sentences
logically entails a sentence if and only if every truth assignment that satis es
also satis es . That is, the models of are also models of . This relation
is denoted as j= .
      </p>
      <p>
        A literal is an atom or negation of an atom. A formula is in Conjunctive
Normal Form (CNF) if it is a conjunction of clauses, where a clause is a
disjunction of literals. A formula is in Disjunctive Normal Form (DNF) if it is a
disjunction of terms, where a term is a conjunction of literals. As in CNF, the
only propositional operators in DNF are ^, _, and :. The : operator can only
appear as part of a literal, which means that it can only precede an atom [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
3
      </p>
    </sec>
    <sec id="sec-3">
      <title>Implementing the Logic Calculator</title>
      <p>In this section we describe the modules and functionality of the Logic Calculator.
We explain the main algorithms implemented and show some examples of using
the three Logic Calculator modes.
3.1</p>
      <sec id="sec-3-1">
        <title>Logical Formula Evaluator mode</title>
        <p>The rst mode developed was the evaluation of logical formulae, since this is the
basic functionality and is also used by the Logical entailment mode.</p>
        <p>
          The rst step is to translate the input in x formula into a Reverse Polish
Notation (RPN) (post x notation) using the Algorithm 1, based on Dijkstra's
Shunting-yard algorithm [
          <xref ref-type="bibr" rid="ref8">8</xref>
          ]. Post x notation, while less common than in x
notation in written mathematics, is much more explicit in the sense that the operands
belonging to a particular operation can be expressed without the need for
brackets or operator precedence.
        </p>
        <p>Algorithm 1: Shunting-yard algorithm adapted for logical operators.</p>
        <p>input : WFF
output: RPN WFF
1 foreach token in turn in the input WFF do
2 if token is a variable 2 P24 then
3 Append to the post x output
4 else if token is `(' then
5 push `(' onto the stack
6 else if token is an operator
7 while there is an operator
the stack do</p>
        <p>Pop o the stack
Append to the post x output
2 then</p>
        <p>of higher or equal precedence than
end</p>
        <p>Push onto the stack
else if token is `)' then
repeat</p>
        <p>Pop token x o the stack
if x 2 then</p>
        <p>Append x to the post x output</p>
        <p>
          To parse the expression, the operator precedence must also be de ned. The
list of valid operators in order of precedence (highest to lowest) is: [
          <xref ref-type="bibr" rid="ref1">1</xref>
          ]
Negation (:), [
          <xref ref-type="bibr" rid="ref2">2</xref>
          ] Conjunction (^), [
          <xref ref-type="bibr" rid="ref3">3</xref>
          ] Disjunction (_), [
          <xref ref-type="bibr" rid="ref4">4</xref>
          ] Implication (!) and [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ]
Bi-implication ($). Where operators have equal precedence, their associativity
indicates in which order they are applied.
        </p>
        <p>For example, the WFF 1 is processed by Algorithm 1, producing the RPN
WFF 2.</p>
        <p>p _ q $ (q ! r) ^ :s
p q _ q r ! s : ^ $
(1)
(2)</p>
        <p>The second step consists in setting up the truth table for the formula
variables. If we consider the case where n is the number of distinct variables in the
formula, then we need to set up a truth table with n columns and 2n rows, as
follows: column under the rst variable should alternate 2n 1 true's with 2n 1
false's; column under the second variable should alternate 2n 2 true's with 2n 2
false's; continuing until we reach the last column variable.</p>
        <p>The third step consists of evaluating the RPN WFF for each interpretation
of the generated truth table. The explicitness of the RPN WFF obtained by
Algorithm 1 allows us to implement the algorithm for evaluating post x notation
in a straightforward manner. Algorithm 2 shows the RPN parser developed using
a stack as a helper data structure.</p>
        <p>For example given the RPN Formula 2 and the interpretation p = 1, q = 0,
r = 1 and s = 0, Algorithm 2 outputs 1 as the result of the evaluation. There
are 24 calls to Algorithm 2 since there are 4 variables in this formula.
3.2</p>
      </sec>
      <sec id="sec-3-2">
        <title>Logical Entailment mode</title>
        <p>In this mode we ca determine whether a set of premises logically entails a
conclusion (or set of conclusions). The logical symbol j= allows us to represent a logical
entailment, separating the premises from the conclusion, with associativity from
left to right. Premises are separated by commas (;). Parentheses are used in the
classical way, usually to override the normal precedence of operators. Premises
and conclusions are WFFs.</p>
        <p>First step is to save the premises in a list and store the conclusion in separate
lists. After evaluating each WFF from the premises part (using the modules
developed for the Logical formula evaluator mode) and evaluating each WFF
from the conclusion, a nal truth table is used to determine whether the logical
entailment holds (see Algorithm 3).</p>
        <p>Algorithm 3: Logical entailment processor.</p>
        <p>input : Premises list, conclusions list
output: Whether logical entailment holds
1 foreach WFF in premises list do
2 Evaluate truth table of
3 Add truth table to nal table
4 end
5 foreach WFF in conclusions list do
6 Evaluate truth table of
7 Add truth table to nal table
8 end
9 Evaluate</p>
        <p>For example, given the following case:
p _ q;
q ! r;
:s
j=
q _ r
(3)</p>
        <p>We have the problem of determining whether fq _ rg (the conclusion) is
logically entailed from fp _ q; q ! r; :sg (the premises). So, the Logic Calculator
prints the nal truth table and the result, in this case: \Premises do not logically
entail the conclusion".
3.3</p>
      </sec>
      <sec id="sec-3-3">
        <title>Normal Form Converter mode</title>
        <p>The conversion of a WFF to its DNF and CNF is made in two steps. First, a
binary tree is created from parsing the WFF. Operator ! is used to build the
tree from the minor priority operator. For example, the binary tree shown in
Figure 1 is created from Formula 4 with respect to the priority of operators.
a ! b ^ c _ d ! e
(4)</p>
        <p>
          Second, the WFF conversion to CNF (resp. DNF) is based on the laws
governing CNF and DNF conversion [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ].
        </p>
        <p>For example, walking through the binary tree t and generating the tree shown
in Figure 2, resulting in the CNF shown in Formula 5. Similarly, the tree shown
in Figure 3 corresponds to the DNF formula shown in Formula 6.
:b :c</p>
        <p>^
_</p>
        <p>_
e :d e
a</p>
        <p>_
^</p>
        <p>^
:b :d</p>
        <p>_
a
^</p>
        <p>^
:c :d
e
(a ^ :b ^ :d) _ (a ^ :c ^ :d) _ e
(5)
(6)</p>
        <p>
          It is worth clarifying that the Logic Calculator converts to an equivalent
CNF formula rather than an equisatis able CNF formula (via the insertion of
new variables [
          <xref ref-type="bibr" rid="ref2">2</xref>
          ]). The equisatis ability technique is more common in terms of
existing tools. Source code of this module is based on the JavaScript code from
the Theorem Proving in Propositional Logic (WFFs) web site [
          <xref ref-type="bibr" rid="ref1">1</xref>
          ].
3.4
        </p>
      </sec>
      <sec id="sec-3-4">
        <title>Logic Calculator GUI</title>
        <p>One of the goals of the Logic Calculator is to provide a user-friendly User
Interface (UI) similar to an arithmetic calculator. Figure 4 shows the GUI of the
Logic Calculator in Logical formula evaluator mode. The output box is a text
area for convenience, allowing easy copying and pasting of formulae. Debug area
outputs debug details and log information.
4</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>Performance tests</title>
      <p>We stress-tested the Logic Calculator on an Alienware M17x laptop, with an
Intel Core i7-2670QM CPU @2.20GHz 8, 8 GB in RAM, 500 GB in HDD,
Ubuntu Linux 12.04 64-bit and OpenJDK 7 64-bit.</p>
      <p>Table 1 (column Op1) shows processing time plus printing time of the truth
table. Memory is an issue when printing a truth table of 23 variables because
223 = 8388608 interpretations, so the Logic Calculator runs out of memory
at printing 4206949 lines. This runtime exception is thrown depending on the
amount of virtual memory that the JVM has access to. But we can perform
operations with up to 24 variables without memory problems. For example,
Table 1 (column Op2) shows processing time but only printing the number of
models without printing the truth table, and Table 1 (column Op3) shows time
performing conversions of WFF to their CNF and DNF. We can see that
computing formulae conversions is quite quick, even for 24 variables formula.</p>
    </sec>
    <sec id="sec-5">
      <title>Conclusion</title>
      <p>The main bene t of the Logic Calculator is the automatic evaluation of complex
logic operations. For example, to evaluate complex logical formulae with at most
24 variables and any number of parentheses, or to determine if a set of highly
elaborate premises logically entails a set of highly elaborate conclusions, or to
translate complex logical formulae to CNF or DNF, are highly complex tasks.
Currently there are no tools which can solve these problems e ectively, in a
userfriend manner. Logic Calculator addresses these issues, so it can be an e ective
tool for new methods of reasoning.</p>
      <p>
        The Logic Calculator was developed under the Open Java Development Kit
(OpenJDK) 7 but source code using Java 5 as target, therefore it needs Java
Runtime Environment version 5 or higher. OpenJDK platform has the same
attributes of the o cial JDK, plus the bene ts of being free software [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ].
      </p>
      <p>Multiplatform support is also an advantage, thus it can be executed over
recent versions of Microsoft Windows, most Linux distributions, most UNIX
variants and modern versions of Mac OS. Future work considers improve the
e ciency of algorithms, manage more variables and a possible implementation
in a hardware device.</p>
      <p>Logic Calculator source code is downloadable from Sourceforge repositories at
https://sourceforge.net/p/logiccalculator. A number of downloads up to
2016 is available at https://sourceforge.net/projects/logiccalculator/
files/stats/timeline?dates=2013-09-27+to+2016-12-31.</p>
    </sec>
    <sec id="sec-6">
      <title>Acknowledgments</title>
      <p>To CONACYT for supporting the doctoral program in Computer Science at the
Universidad Juarez Autonoma de Tabasco.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Allison</surname>
            ,
            <given-names>L. Theorem</given-names>
          </string-name>
          <article-title>Proving in Propositional Logic (WFFs)</article-title>
          . http://www. allisons.org/ll/Logic/Propositional/Wff/,
          <year>2015</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Aaron</surname>
            <given-names>R.</given-names>
          </string-name>
          <string-name>
            <surname>Bradley</surname>
            and
            <given-names>Zohar</given-names>
          </string-name>
          <string-name>
            <surname>Manna</surname>
          </string-name>
          .
          <article-title>The Calculus of Computation: Decision Procedures with Applications to Veri cation</article-title>
          . Springer Publishing, USA, 1st edition,
          <year>2007</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>H. B.</given-names>
            <surname>Enderton</surname>
          </string-name>
          . A Mathematical Introduction to Logic. Academic Press, USA, 2nd edition,
          <year>2001</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>M.R.</given-names>
            <surname>Genesereth</surname>
          </string-name>
          and
          <string-name>
            <given-names>E.</given-names>
            <surname>Kao</surname>
          </string-name>
          . Introduction to Logic. Morgan &amp; Claypool Publishers.
          <source>Synthesis Lectures on Computer Science, USA, 2nd edition</source>
          ,
          <year>2013</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>M.</given-names>
            <surname>Hazewinkel</surname>
          </string-name>
          .
          <source>Encyclopedia of Mathematics</source>
          , volume
          <volume>10</volume>
          . Springer Publishing, USA,
          <year>2002</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>E.</given-names>
            <surname>Mendelson</surname>
          </string-name>
          . Introduction to Mathematical Logic. CRC Press, USA, 5th edition,
          <year>2009</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <given-names>J.</given-names>
            <surname>Russell</surname>
          </string-name>
          and
          <string-name>
            <given-names>R.</given-names>
            <surname>Cohn</surname>
          </string-name>
          .
          <source>OpenJDK. Book on Demand, USA, 1st edition</source>
          ,
          <year>2012</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>R.</given-names>
            <surname>Wilson</surname>
          </string-name>
          .
          <article-title>An Introduction to Dynamic Data Structures</article-title>
          .
          <source>McGraw Hill, USA, 1st edition</source>
          ,
          <year>1988</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>