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 whereSUM
is assigned the sum ofA
andB
.SUB(A, B, DIF) :- DIF = A - B.
→ This defines subtraction whereDIF
is the difference betweenA
andB
.MUL(A, B, MUL) :- MUL = A * B.
→ This defines multiplication whereMUL
is the product ofA
andB
.DIV(A, B, DIV) :- DIV = A / B.
→ This defines division whereDIV
is the quotient ofA
divided byB
.
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!
this is not working
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).