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.
Complete Code
% ============================================================
% Monkey-Banana Problem in Prolog
% Demonstrates goal chaining: the monkey must complete all
% four steps in sequence to reach the banana.
% ============================================================
domains
X = string
predicates
take(X, X) % take(Animal, Object) - can Animal take Object?
move(X, X) % move(Animal, Object) - can Animal move Object?
get_on(X, X) % get_on(Animal, Object) - can Animal get on Object?
hit(X, X, X) % hit(Animal, Tool, Fruit)- can Animal hit Fruit with Tool?
go % entry point
clauses
% Main goal: monkey succeeds if all four steps are confirmed
go :-
take("Monkey", "Stick"),
move("Monkey", "Chair"),
get_on("Monkey", "Chair"),
hit("Monkey", "Stick", "Banana"),
write("The monkey has hit the banana.").
% Fallback: at least one step was denied
go :-
write("The monkey couldn't reach the banana.").
% Ask if the animal can take the object
take(Animal, Object) :-
write("Does the ", Animal, " take the ", Object, "?(y/n)"),
readchar(Reply),
Reply = 'y'.
% Ask if the animal can move the object
move(Animal, Object) :-
write("Does the ", Animal, " move the ", Object, "?(y/n)"),
readchar(Reply),
Reply = 'y'.
% Ask if the animal can get on the object
get_on(Animal, Object) :-
write("Does the ", Animal, " get on ", Object, "?(y/n)"),
readchar(Reply),
Reply = 'y'.
% Ask if the animal can hit the fruit with the tool
hit(Animal, Tool, Fruit) :-
write("Does the ", Animal, " hit the ", Fruit, " with the ", Tool, "?(y/n)"),
readchar(Reply),
Reply = 'y'.
Explanation of the Code
domainsblock — Declares the type aliasX = string. This allows all predicates to accept string arguments uniformly (used in Turbo Prolog / Visual Prolog).go/0— Goal Chaining — The firstgoclause chains all four steps with,(logical AND). Prolog evaluates them left to right. If every predicate succeeds (user answersy), the finalwritefires. If any step fails, Prolog abandons this clause and tries the secondgoclause which prints the failure message.- Action Predicates (
take,move,get_on,hit) — Each predicate writes a question, reads a single character withreadchar/1, and succeeds only if the reply is'y'. A reply of'n'causes the predicate to fail, which triggers backtracking in the callinggoclause. - Backtracking Behaviour — This is the key insight of the Prolog solution. You do not write explicit
if-elselogic. Instead, you define what must be true (gosucceeds if all steps succeed) and let Prolog’s backtracking automatically handle the failure path. The secondgoclause is a catch-all that fires whenever the first fails.
Sample Run 1 — Monkey Succeeds
Goal: go
Does the Monkey take the Stick?(y/n) y
Does the Monkey move the Chair?(y/n) y
Does the Monkey get on Chair?(y/n) y
Does the Monkey hit the Banana with the Stick?(y/n) y
The monkey has hit the banana.
Explanation: The user confirms every step. All four predicates succeed in sequence, so the final write in the first go clause fires and the goal is satisfied with one solution.
Sample Run 2 — Monkey Fails Mid-Plan
Goal: go
Does the Monkey take the Stick?(y/n) y
Does the Monkey move the Chair?(y/n) n
The monkey couldn't reach the banana.
Explanation: The monkey picks up the stick (take succeeds) but fails to move the chair (move fails because the user answers n). Prolog immediately abandons the first go clause and falls through to the second, printing the failure message. Note that Prolog does not ask about the remaining steps — once a conjunction fails, the rest of the chain is skipped.
See Also
Conclusion
The Monkey-Banana problem elegantly demonstrates the power of Prolog’s goal-directed execution. A plan that would require explicit conditional logic in Java or C becomes a simple conjunction of sub-goals in Prolog. Adding a new step to the plan is as simple as adding one more predicate call inside go — no control flow changes needed. This same pattern is used in more sophisticated AI planning systems such as STRIPS and PDDL planners. For a related example of interactive yes/no reasoning in Prolog, see the Diagnosis Problem post.
error while running..
?- [‘L:/MANOJ/8TH SEM/AI/PROLOG TRY/Monkey_B2.pl’].
ERROR: l:/manoj/8th sem/ai/prolog try/monkey_b2.pl:1:8: Syntax error: Operator expected
Warning: l:/manoj/8th sem/ai/prolog try/monkey_b2.pl:18:
Redefined static procedure go/0
Previously defined at l:/manoj/8th sem/ai/prolog try/diagnosis.pl:15
true.
error occurred
we are havin AI in 7th sem..