Skip to content

Instantly share code, notes, and snippets.

@lakshayg
Created March 5, 2024 22:36

Revisions

  1. lakshayg created this gist Mar 5, 2024.
    23 changes: 23 additions & 0 deletions knights_and_knaves.prolog
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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)).