# Jason Daniels, Sean Grean, and Scott Scudder's Prolog Project

Jump to navigation
Jump to search

## Description of the Problem

The Three Suspects

We got the problem from this site (Thanks to Ted's and Chris's Wiki for inspiration).

Kyle, Neal, and Grant were rounded up by their mother yesterday, because one of them was suspected of having grabbed a few too many cookies from the cookie jar. The three brothers made the following statements under very intensive questioning:

* Kyle: I'm innocent. * Neal: I'm innocent. * Grant: Neal is the guilty one.

If only one of these statements was true, who took the cookies?

## Description of the Database

Our database consists of the three facts we are given about the problem.

Kyle: I'm innocent. Neal: I'm innocent. Grant: Neal is the guilty one.

If only one of these facts can be true, then which fact would cause the other two facts to be false?

## Solution

Looking through the three facts let's first assume Grant took the cookie.

Then that makes both Kyle's and Neal's statement true, and there can only be one true statement so assuming Grant doesn't work. Kyle: I'm innocent. T Neal: I'm innocent. T Grant: Neal is the guilty one. F

Now let's assume Neal took the cookie.

Then Kyle's statement, "I'm innocent" still holds true. Also Grant's statement, "Neal is the guilty one." is true, therefore Neal can't be the one who's guilty either. Kyle: I'm innocent. T Neal: I'm innocent. F Grant: Neal is the guilty one. T

Lastly, we'll assume Kyle took the cookie.

Neal's statement is true, Grant's statement is false, because Kyle would be guilty. In addition Kyle's statement is false. Therefor we have are answer. Kyle took the cookie! Kyle: I'm innocent. F Neal: I'm innocent. T Grant: Neal is the guilty one. F

## The Code

% Jason Daniels, Sean Green, Scott Scudder % February 19, 2009 % MAT 385 % Dr. Andy Long % %The Three Suspects %http://www.edcollins.com/logic/logic21-30.htm %Database------------------------------------ innocent(kyle). %what each are implied as innocent(neal). guilty(neal). says(neal, innocent(neal)). %what each says says(kyle, innocent(kyle)). says(grant, guilty(neal)). %Database------------------------------------ %and rule used to join 2 different statements into 1. and(L,R) :- L,R. % Assume Neal took the cookie implies that: assume_(neal) :- not(and(says(kyle, innocent(kyle)), says(grant, guilty(neal)))). %Assume Kyle took the cookie, which implies that: assume_(kyle) :- not(and(says(neal, innocent(neal)), not( says(grant, guilty(neal))))). %Assume Grant took the cookie, implies that: assume_(grant) :- not(and(says(kyle, innocent(kyle)), says(neal, innocent(neal)))). %case statement to trigger a certain call. case(X) :- X == 1, (test(assume_(kyle))); X == 2, (test(assume_(neal))); X == 3, (test(assume_(grant))). %writes the correct message at the end of the loop endLoop(X) :- X == 4, setof(Y,assume_(Y),Y), write(Y), !; X < 4, write('Good Job!'). %returns the value of the Term test(Term) :- Term, !; not(Term), false, !. %loops the question until it's answered right, or user says Uncle! guess :- write('\nTry to solve who took the extra cookie!\n'), repeat, write('\nWho do you think took the cookie:\n'), write('[1] Kyle\n[2] Neal\n[3] Grant\n[4] Uncle!\n'), write('pick a number ==> '), read(Sus), (case(Sus), true, !; Sus == 4, !), %determine if user guessed correctly or called Uncle! endLoop(Sus).

## Example Output

1 ?- assume_(neal). false. 2 ?- assume_(grant). false. 3 ?- assume_(kyle). true. 4 ?- setof(X,assume_(X),Y). Y = [kyle]. 5 ?- guess. Try to solve who took the extra cookie! Who do you think took the cookie: [1] Kyle [2] Neal [3] Grant [4] Uncle! pick a number ==> 3. Who do you think took the cookie: [1] Kyle [2] Neal [3] Grant [4] Uncle! pick a number ==> 2 Who do you think took the cookie: [1] Kyle [2] Neal [3] Grant [4] Uncle! pick a number ==> 4. [kyle] true. 6 ?- guess. Try to solve who took the extra cookie! Who do you think took the cookie: [1] Kyle [2] Neal [3] Grant [4] Uncle! pick a number ==> 1. Good Job! true. 7 ?-