Implementing Calculator in Prolog

Prolog is a logic programming language widely used in artificial intelligence and symbolic computing. Unlike imperative languages, Prolog is declarative, meaning it defines relationships rather than a step-by-step procedure. In this blog, we will implement a simple calculator in Prolog that can perform basic arithmetic operations: addition, subtraction, multiplication, and division.

Complete Code

PREDICATES
ADD(integer,integer,integer)
SUB(integer,integer,integer)
MUL(integer,integer,integer)
DIV(integer,integer,integer)

CLAUSES
ADD(A,B,SUM):-
SUM=A+B.
SUB(A,B,DIF):-
DIF=A-B.
MUL(A,B,MUL):-
MUL=A*B.
DIV(A,B,DIV):-
DIV=A/B.

// output
// Goal: ADD(5,4,SUM)
// SUM=9
// 1 solution
// Goal: SUB(9,4,SUB)
// SUB=5
// 1 solution
// Goal: MUL(4,5,MUL)
// MUL=20
// 1 solution
// Goal: DIV(10,2,DIV)
// DIV=5
// 1 solution

Understanding the Code

The Prolog code defines four arithmetic operations using predicates. Each predicate takes two input numbers and produces an output, representing the result of the computation.

Predicates Definition

PREDICATES
ADD(integer, integer, integer)
SUB(integer, integer, integer)
MUL(integer, integer, integer)
DIV(integer, integer, integer)

These lines define the arithmetic predicates that our calculator will use. Each predicate takes three arguments:

  • The first two are input integers.
  • The third one stores the result of the operation.

Clauses Implementation

CLAUSES
ADD(A, B, SUM):-
    SUM = A + B.
SUB(A, B, DIF):-
    DIF = A - B.
MUL(A, B, MUL):-
    MUL = A * B.
DIV(A, B, DIV):-
    DIV = A / B.

Each clause implements a mathematical operation:

  • ADD(A, B, SUM) :- SUM = A + B. → This defines the addition operation where SUM is assigned the sum of A and B.
  • SUB(A, B, DIF) :- DIF = A - B. → This defines subtraction where DIF is the difference between A and B.
  • MUL(A, B, MUL) :- MUL = A * B. → This defines multiplication where MUL is the product of A and B.
  • DIV(A, B, DIV) :- DIV = A / B. → This defines division where DIV is the quotient of A divided by B.

Executing the Code and Understanding Output

Let’s run some queries to see how the Prolog calculator works.

Addition Query

Goal: ADD(5, 4, SUM)

Output:

SUM = 9
1 solution

Explanation: The query ADD(5, 4, SUM) asks Prolog to compute the sum of 5 and 4. The result is stored in SUM, which equals 9.

Subtraction Query

Goal: SUB(9, 4, SUB)

Output:

SUB = 5
1 solution

Explanation: The query SUB(9, 4, SUB) requests the difference between 9 and 4. The result is stored in SUB, which equals 5.

Multiplication Query

Goal: MUL(4, 5, MUL)

Output:

MUL = 20
1 solution

Explanation: The query MUL(4, 5, MUL) computes the product of 4 and 5. The result is stored in MUL, which equals 20.

Division Query

Goal: DIV(10, 2, DIV)

Output:

DIV = 5
1 solution

Explanation: The query DIV(10, 2, DIV) performs the division of 10 by 2. The result is stored in DIV, which equals 5.


This simple Prolog calculator demonstrates how logic programming can be used to perform arithmetic operations. By defining relationships between numbers, Prolog efficiently computes results based on queries. You can expand this calculator by adding more operations like modulus, exponentiation, or even complex mathematical functions.

I hope this blog helped you understand the basics of Prolog arithmetic operations. Happy coding!

2 thoughts on “Implementing Calculator in Prolog”

    1. try this
      PREDICATES
      ADD(integer,integer,integer)
      SUB(integer,integer,integer)
      MUL(integer,integer,integer)
      DIV(integer,integer,integer)

      CLAUSES
      ADD(A,B,SUM):-
      SUM=A+B.
      SUB(A,B,DIF):-
      DIF=A-B.
      MUL(A,B,MUL):-
      MUL=A*B.
      DIV(A,B,DIV):-
      DIV=A/B.

      GOAL
      ADD(5,4,SUM),
      SUB(5,4,DIF),
      MUL(5,4,MUL),
      DIV(5,4,DIV).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.