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.”