Skip to content

Instantly share code, notes, and snippets.

@andrewjjenkins
Created March 1, 2022 22:04
Show Gist options
  • Save andrewjjenkins/e0abde425d5efa84b1f59d3f59c8ab30 to your computer and use it in GitHub Desktop.
Save andrewjjenkins/e0abde425d5efa84b1f59d3f59c8ab30 to your computer and use it in GitHub Desktop.
A Prolog problem to solve a Knights-and-Knaves example from a teambuilding exercise
% Constraint Logic Programming
:- use_module(library(clpb)). % Boolean constraints
animals(1, [Horse, Cow, Chicken, Pig, Sheep, Goat, Swan, Cat]) :-
sat(card([6],[Horse, Cow, Chicken, Pig, Sheep, Goat, Swan, Cat])),
% Swan: "I'm the only honest bird"
sat(Swan =:= ~Chicken),
% Cow: "You can't trust the cat"
sat(Cow =:= ~Cat),
% Chicken: "The goat is honest"
sat(Chicken =:= Goat),
% Pig: "The horse is supremely trustworthy"
sat(Pig =:= Horse),
% Horse: "I wouldn't lie, but the cow would"
sat(Horse =:= ~Cow),
% Sheep: "One of my hooved brethren is a liar"
sat(Sheep =:= card([2],[Horse, Cow, Pig])),
% Cat: not a liar.
sat(Cat =:= Cat),
% Goat: Lying four-legged == lying birds
sat(Goat =:= (
card([2],[Chicken, Swan])*card([6],[Horse,Cow,Pig,Sheep,Goat,Cat]) +
card([1],[Chicken, Swan])*card([5],[Horse,Cow,Pig,Sheep,Goat,Cat]) +
card([0],[Chicken, Swan])*card([4],[Horse,Cow,Pig,Sheep,Goat,Cat])
)),
labeling([Horse, Cow, Chicken, Pig, Sheep, Goat, Swan, Cat]).
/** <examples> Your example queries go here, e.g.
?- animals(1, [Horse, Cow, Chicken, Pig, Sheep, Goat, Swan, Cat]).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment