Subject: Re: [SWIPL] Logic Puzzle Solver (fwd) again

From: Jan Wielemaker <[email-masked]>
Date: Tue, 8 May 2001 10:26:18 +0200

On Tue, 08 May 2001, [email-masked] wrote:
>
>
>I forgot to mention that I traced it to the solve 
>function giving me an Exception:(12).
>----------------------  Forwarded Message:  ---------------------
>From:    [email-masked]
>To:      [email-masked]
>Subject: Logic Puzzle Solver
>Date:    Tue, 08 May 2001 03:46:56 +0000
>
>To Whom It May Concern:
>
>The Attached Program puzzle.pl gives me an error in SWI 
>Prolog, when I try to run it.  The error is as follows:
>
>ERROR: Arguments are not sufficiently instantiated
>
>To run the program, compile (consult) the file than at 
>the prompt type:
>
>test_puzzle(test,X).
>
>This same program ran under Quintus Prolog on a RISC 
>6000.  Could there be something that I am doing wrong?  
>Any help would be greatly appreciated.
>
>Sincerely,

Seems there is a problem in your program.  SICStus gave the
same error.  I managed to track it down to the write_ln, that
indicates A is unbound.  I'll use this program to improve the
debugger as it was quite hard to find this.  No clue why Quintus
answered it without error.

	--- Jan

nextto(X,Y):-X is Y+1.
nextto(X,Y):-Y is X+1.

solve_puzzle(puzzle(Clues,Queries,Solution),Solution) :-
	solve(Clues),solve(Queries).

solve([Clue|Clues]) :-
	Clue,
	solve(Clues).
solve([]).

test_puzzle(Name,Solution) :-
	structure(Name,Structure), 
	clues(Name,Structure,Clues),
	queries(Name,Structure,Queries,Solution),
	solve_puzzle(puzzle(Clues,Queries,Solution),Solution).

structure(test,
	  [ man(House1,Color1,National1,Pet1,Drink1,Smoke1), 
	    man(House2,Color2,National2,Pet2,Drink2,Smoke2), 
	    man(House3,Color3,National3,Pet3,Drink3,Smoke3), 
	    man(House4,Color4,National4,Pet4,Drink4,Smoke4), 
	    man(House5,Color5,National5,Pet5,Drink5,Smoke5)
	  ]). 
					 
houseposition(man(H,C,N,P,D,S),H).
house(man(H,C,N,P,D,S),C).
nationality(man(H,C,N,P,D,S),N).
drinks(man(H,C,N,P,D,S),D).
pet(man(H,C,N,P,D,S),P).
smokes(man(H,C,N,P,D,S),S).

clues(test, Man,
      [ (house(Man1Clue1,red),nationality(Man1Clue1,english)),
	(nationality(Man1Clue2,spanish),pet(Man1Clue2,dog)),
	(house(Man1Clue3,green),drinks(Man1Clue3,coffee)),
	(nationality(Man1Clue4,ukrainian),drinks(Man1Clue4,tea)),
	(house(Man1Clue5,green),house(Man2Clue5,ivory),
	 houseposition(Man1Clue5,A),houseposition(Man2Clue5,B),
	 write_ln(A),			% JW
	 B is A+1),
	(smokes(Man1Clue6,winston),pet(Man1Clue6,snails)),
	(house(Man1Clue7,yellow),smokes(Man1Clue7,kools)),
	(drinks(Man1Clue8,milk),houseposition(Man1Clue8,3)),
	(nationality(Man1Clue9,norwegian),houseposition(Man1Clue9,1)),
	(smokes(Man1Clue10,chesterfields),pet(Man2Clue10,fox),
	 houseposition(Man1Clue10,P),houseposition(Man2Clue10,Q),nextto(P,Q)),
	(smokes(Man1Clue11,kools),pet(Man2Clue11,horse),
	 houseposition(Man1Clue11,R),houseposition(Man2Clue11,S),nextto(R,S)),
	(smokes(Man1Clue12,luckystrike),drinks(Man1Clue12,orangejuice)),
	(nationality(Man1Clue13,japanese),smokes(Man1Clue13,parliaments)),
	(nationality(Man1Clue14,norwegian),house(Man2Clue14,blue),
	 houseposition(Man1Clue14,I),houseposition(Man2Clue14,J),
	 nextto(I,J))
      ]).

queries(test, Man,
	[ member(Q1, Man), nationality(Q1,N1), pet(Q1,zebra),
	  member(Q2, Man), nationality(Q2,N2), drink(Q2,water)
	],
	[ [N1, ' owns the zebra'],
	  [N2,' drinks water']
	]).