How to solve the Monkey & Banana Prolog Problem?

					 %  Monkey World simple  2023 to be discussed in L6, L7

move(state(middle, onbox, middle, hasnot),
     grasp,      state( middle,onbox,middle,has)).
 /*  move(state(P, onbox, P, H),             %unc1
     unclimb,      state( P,onfloor,P,H)).    */
move(state( P, onfloor, P, H),
     climb,      state( P,onbox,P,H)).
/*  move(state( P, onbox, P, H),             %unc2
     unclimb,      state( P,onfloor,P,H)). */
move(state( P, onfloor, P, H),
     push( P,P1),      state( P1,onfloor,P1,H)).
move(state(P, onfloor, B, H),
     walk( P,P1),      state( P1,onfloor,B,H)).
 /*    move(state( P, onbox, P, H),          %unc3
     unclimb,      state( P,onfloor,P,H)).   */
canget(state(_,_,_,has)).               % The hunger!
canget(S1):- move( S1,_M,S2 ),canget( S2 ).	% The HUNGER!!

     %1 Can the monkey get the banana from the input state?
     %2 Are there states from which the monkey will not get the b?
     %3 What happens if you change the order of the clauses?
     %4 Note that the order of clauses is roughly opposite to
     %    the sequence of events. Why?
     %5 Shouldn't the poor monkey be allowed to climb down?
     %6 Or *is* the monkey already allowed to climb down (in a sense)?
     %7 Can you think of a way to make the program notify you
     %    when and where and how the monkey gets on the box / the banana?
     %8 What if you start the monkey from position 'inouterspace', say?

cangetw( state( _,_,_,has ) ).
cangetw(S1):- writeln( S1 ),
                   move( S1,_M,S2 ), cangetw( S2 ).

% Output (writing) from cangetw is slightly unsatisfactory.
% What does the monkey actually do? Improve it.
% What happens on ; (looking for more solutions) ?
% What do you think of the variables in the output of cangetw?
% Does a monkey have variables in his/her head? Do you?
% Does canget model the *behaviour* of a monkey?
% Rather than just writing out 'where the monkey is', one could
% store the states in a list (another variable to canget) and...


a) Give a state(_,_,_,hasnot) from which the monkey cannot get [i.e. canget is false] the banana.

b) Give a state(_,_,_,hasnot) from which the monkey can get [i.e. canget is true] the banana.

For the following use the predicate cangetw, which gives some idea of ‘what the monkey does’ as it searches according to Prolog. Feel free to improve it, but give a different name, e.g. cangetw_hanako, to your modification if you do so.

c) What does the monkey do if you ask the program for more solutions (via ; ) Find out the monkey’s favorite moves. Tracing may also be helpful.

d) Check (using cangetw or/and trace) what happens if you allow the unc (unclimb) moves in the program.

e) Make a suggestion how a predicate cangetunc might be defined to change the way prolog searches, so that the suggested unclimb moves do not lead to loopy behavior. You do not have implement it. Extra points if you do implement it.

2) Use the Prolog handout (term arithmetic), that is sTOnn/2, nnTOs/2 and pluss/3 to define mulss/3 (i.e. multiplication) for successor terms. Check that the multiplication works. 

Compute a few cubes for small numbers, such as (s(s(zero)) three times mulss-ed by itself, and finally check out at what number the computation of mulsscubes runs out of stack. 

For this write a mulsscubes(N, Cube) predicate that uses nnTOs and sTOnn, for input and output and try out ?- mulsscubes(20,C) ?- mulsscubes(33,C) etc., etc. [If you don’t run out of stack, but the computer starts smoking, take a picture with your kee-tai and hand it in as completed homework.]

3) Look at ‘’ from moodle and answer questions i-iii found inside).



% cutalysis for non-chemists
c(X,Y,Z,U,W) :- abc1(X),ab2(Y),!,ab3(Z),ab4(U),abc5(W),writeln([X,Y,Z,U,W]).
abc1(0) .
ab2(0) :- !.  % Does this cut do anything?
ab2(1) :- !.  % Does this cut do anything?
ab4(0) :- !.
abc5(1) :- !.

% HW_06 Problem 3: i) How many solutions does
% the query ?- c(A,B,C,D,E). produce?
% ii) Remove all cuts and count the solutions via ;
% iii) A add new cuts in
% this program so you get exactly 9 solutions.
% Hint: note that there is a cut in the first line
% A simpler program for cutting...
c :- a, writeln(['YES, because of a']).
c :- b, writeln(['YES, because of b']),!.
c :- d, writeln(['YES, because of d']).

%  Add cuts in various other places and see what happens.
%  If you think about it, it kinda makes sense.
%  The cut definitely is useful if you want to 'tame'
%  a program like the old oshimai, where
%  a number added (probably) should not be treated as a default
%  and ignored atom on a second try, which will give 'smaller answers'
%  Nor should 'oshimai' be treated as an ignored atom on a later
%  try, so you continue adding after 'oshimai' and you get
%  'bigger answers'.


5*) For a little further *extra credit*, convert as much as you can stand from the first homework set into clausal form (using de Morgan’s laws, etc, as appropriate) and apply resolution to prove that unsatisfiable propositions and the negations of tautologies resolve to ‘box’, or read off satisfying assignments from the resolution tree.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *