Tag Archives: Prolog

Implementation of Monkey-Banana Problem in Prolog

In this post, we implement the Monkey-Banana Problem in Prolog — a classic AI planning problem used to demonstrate goal-directed reasoning. The scenario: a monkey is in a room with a banana hanging from the ceiling. There is also a stick and a chair in the room. To reach the banana, the monkey must pick up the stick, move the chair under the banana, climb on the chair, and then hit the banana with the stick. The Prolog program models each required action as an interactive predicate and chains them together to achieve the final goal.

How Does the Prolog Solution Work?

Each step in the plan is modelled as a Prolog predicate (take, move, get_on, hit). Each predicate asks the user a yes/no question. The main goal go chains all four predicates together using Prolog’s conjunction operator (,). If every step is confirmed by the user, the goal succeeds and the monkey reaches the banana. If any step fails, Prolog backtracks to the second go clause and reports failure.

Continue reading Implementation of Monkey-Banana Problem in Prolog

Implementing Diagnosis Problem in Prolog

In this post, we implement a medical diagnosis expert system in Prolog. The program asks the user a series of yes/no questions about a patient’s symptoms and attempts to diagnose a disease based on the answers. This is a classic demonstration of how Prolog’s pattern matching and backtracking can model the reasoning process of a rule-based expert system.

How Does the Diagnosis Work?

The Prolog program defines a hypothesis predicate that maps a combination of symptoms to a disease. A symptom predicate interactively queries the user for each symptom. If all required symptoms are confirmed, Prolog unifies the disease variable and reports the diagnosis. If no hypothesis can be satisfied, a fallback clause prints a failure message.

In this example, the only disease defined is common cold, which requires the patient to have both a headache and sneezing. You can easily extend this to cover more diseases by adding more hypothesis and symptom clauses.

Continue reading Implementing Diagnosis Problem in Prolog

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

Continue reading Implementing Calculator in Prolog