Skip to main content

Implementing Calculator in Prolog

In this post, we implement a simple calculator in Prolog that performs basic arithmetic operations: addition, subtraction, multiplication, and division. Prolog is a logic programming language widely used in artificial intelligence and symbolic computing. Unlike imperative languages (Java, C), Prolog is declarative — you describe what relationships hold, not how to compute them step by step. The Prolog engine then uses unification and backtracking to derive answers.

What is a Prolog Predicate?

In Prolog, a predicate defines a relationship between its arguments. When you query a predicate, Prolog attempts to unify the query with the predicate’s head and then evaluates the body. For arithmetic, the is operator (or direct unification with =) binds a variable to the result of an expression. In our calculator, ADD(A, B, SUM) :- SUM = A + B. means: “SUM holds if SUM equals A plus B.”


Complete Code

% ============================================================
% Simple Calculator in Prolog
% Defines four arithmetic predicates: ADD, SUB, MUL, DIV
% Each predicate takes two inputs and unifies the third with the result.
% ============================================================

PREDICATES
  ADD(integer, integer, integer)  % ADD(A, B, Result)
  SUB(integer, integer, integer)  % SUB(A, B, Result)
  MUL(integer, integer, integer)  % MUL(A, B, Result)
  DIV(integer, integer, integer)  % DIV(A, B, Result)

CLAUSES
  % Addition: SUM is unified with A + B
  ADD(A, B, SUM) :-
    SUM = A + B.

  % Subtraction: DIF is unified with A - B
  SUB(A, B, DIF) :-
    DIF = A - B.

  % Multiplication: PRODUCT is unified with A * B
  MUL(A, B, PRODUCT) :-
    PRODUCT = A * B.

  % Division: QUOTIENT is unified with A / B
  DIV(A, B, QUOTIENT) :-
    QUOTIENT = A / B.

Understanding the Code

The program is divided into two Prolog sections: PREDICATES (declarations) and CLAUSES (rules).

Predicates Declaration

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

Each predicate is declared with three integer arguments: the first two are inputs, and the third will be unified with the computed result when the predicate is queried.

Clauses (Rules)

CLAUSES
  ADD(A, B, SUM)     :- SUM = A + B.
  SUB(A, B, DIF)     :- DIF = A - B.
  MUL(A, B, PRODUCT) :- PRODUCT = A * B.
  DIV(A, B, QUOTIENT):- QUOTIENT = A / B.
  • ADD(A, B, SUM) :- SUM = A + B. — Prolog unifies SUM with the arithmetic expression A + B. When queried as ADD(5, 4, SUM), Prolog substitutes A=5 and B=4, then unifies SUM with 9.
  • SUB(A, B, DIF) :- DIF = A - B. — Computes the difference. SUB(9, 4, DIF) gives DIF = 5.
  • MUL(A, B, PRODUCT) :- PRODUCT = A * B. — Computes the product. MUL(4, 5, PRODUCT) gives PRODUCT = 20.
  • DIV(A, B, QUOTIENT) :- QUOTIENT = A / B. — Computes integer division. DIV(10, 2, QUOTIENT) gives QUOTIENT = 5.

Running Queries and Understanding Output

In Prolog, you interact with the program by issuing Goal queries. The Prolog engine tries to satisfy each goal by matching it with a clause head and evaluating the body.

Addition

Goal: ADD(5, 4, SUM)
SUM = 9
1 solution

Prolog unifies A=5, B=4, then evaluates SUM = 5 + 4, binding SUM to 9. There is exactly one matching clause, so “1 solution” is reported.

Subtraction

Goal: SUB(9, 4, DIF)
DIF = 5
1 solution

Prolog evaluates DIF = 9 - 4 and binds DIF to 5.

Multiplication

Goal: MUL(4, 5, PRODUCT)
PRODUCT = 20
1 solution

Prolog evaluates PRODUCT = 4 * 5 and binds PRODUCT to 20.

Division

Goal: DIV(10, 2, QUOTIENT)
QUOTIENT = 5
1 solution

Prolog evaluates QUOTIENT = 10 / 2 and binds QUOTIENT to 5. Note that dividing integers in Prolog typically yields an integer result when both operands are integers.


See Also

Conclusion

This simple Prolog calculator illustrates the declarative nature of logic programming: you define what a relationship is, and Prolog figures out how to satisfy it. You can extend this calculator by adding predicates for modulus (MOD), power (POW), or even handling division-by-zero gracefully using Prolog’s built-in exception mechanism. For more complex AI applications, refer to our Diagnosis Problem in Prolog post which shows how Prolog’s pattern matching and backtracking can model expert system reasoning.

2 Replies to “Implementing Calculator in Prolog”

  1. Navdeep kaur suseta Oct 31st at 1:52 pm

    this is not working

    • Adelaja Oluwaseun Mar 12th at 3:16 am

      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.