Skip to content

Instantly share code, notes, and snippets.

@lakshayg
Created March 5, 2024 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshayg/80f6a7479539eca71ca79f04cdd0865e to your computer and use it in GitHub Desktop.
Save lakshayg/80f6a7479539eca71ca79f04cdd0865e to your computer and use it in GitHub Desktop.
A prolog program to solve the knight and knaves logic puzzles
person(knight).
person(knave).
persons(List) :- maplist(person, List).
claim(knight, Stmt) :- Stmt.
claim(knave, Stmt) :- \+Stmt.
% A puzzle taken from https://dmackinnon1.github.io/knaves/
% You have met a group of 6 islanders. Their names are Justin, Samuel, Ira, Frank, Beatrix, and Pamela.
%
% Samuel says: Ira is a knight.
% Samuel says: Justin lies.
% Beatrix says: Frank never lies.
% Beatrix says: Pamela tells the truth.
% Ira says: Frank is a knight or I am a knave.
puzzle(J, S, I, F, B, P) :-
persons([J, S, I, F, B, P]),
claim(S, I = knight),
claim(S, J = knave),
claim(B, F = knight),
claim(B, P = knight),
claim(I, (F = knight; I = knave)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment