Created
August 13, 2024 13:26
-
-
Save yackx/8aab9ab1f885a5457bcd430d8a1fc176 to your computer and use it in GitHub Desktop.
Detective solving a murder case in Prolog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% You are a detective trying to solve a murder case. | |
% There are three suspects - Art, Burt, and Carl. | |
% They are also the only three witnesses. | |
% | |
% Here are their statements: | |
% | |
% Art: | |
% Burt was the victim's friend, but the victim and carl were deadly | |
% enemies. | |
% | |
% Burt: | |
% I was out of town when it happened, and on top of that I didn't even | |
% know the guy. | |
% | |
% Carl: | |
% I'm innocent. I don't know who did it. I saw Art and Burt driving | |
% around town then. | |
% | |
% Determine who is lying. | |
% | |
% Source | |
% https://github.com/Anniepoo/prolog-examples/blob/master/newdetective.pl | |
testimony(art, friend(burt)). | |
testimony(art, enemy(carl)). | |
testimony(burt, out_of_town(burt)). | |
testimony(burt, stranger(burt)). | |
testimony(carl, in_town(art)). | |
testimony(carl, in_town(burt)). | |
testimony(carl, in_town(carl)). % testimony implies he was in town | |
inconsistent(friend(X), enemy(X)). | |
inconsistent(friend(X), stranger(X)). | |
inconsistent(enemy(X), stranger(X)). | |
inconsistent(in_town(X), out_of_town(X)). | |
% Witnesses are inconsistent if two testimonies belonging to | |
% two different witnesses are inconsistent. | |
inconsistent_testimonies(W) :- | |
% X and Y are two different witnesses | |
member(X, W), member(Y, W), X \= Y, | |
% One of their testimonies is inconsistent | |
testimony(X, XT), testimony(Y, YT), inconsistent(XT, YT). | |
% Witnesses are consistent if there is no inconsistent testimony | |
% among them. | |
consistent_testimonies(W) :- \+inconsistent_testimonies(W). | |
% Solve the case. | |
murderer(M) :- | |
% Pick a suspect | |
member(M, [art, burt, carl]), | |
% Ignore their testimonies by removing them from the witnesses | |
select(M, [art, burt, carl], W), | |
% If witnesses are consistent, M is the murderer | |
consistent_testimonies(W). |
Author
yackx
commented
Aug 13, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment