A small collection of Prolog programs illustrating facts, rules, and constraint solving.
Install SWI-Prolog (version 10+):
# Windows (winget)
winget install SWI-Prolog.SWI-Prolog# macOS
brew install swi-prolog
# Ubuntu/Debian
sudo apt install swi-prologAfter installation, swipl will be on your PATH (you may need to restart your terminal).
Load a file into the interactive interpreter and type queries at the ?- prompt:
swipl friends.pl?- friend(wallace, grommit).
true.
?- halt.Use -g to run a goal non-interactively and -t halt to exit when done:
swipl -q -l friends.pl -g "friend(wallace, grommit), write(true)" -t haltThe -q flag suppresses the startup banner.
Defines likes/2 facts and a friend/2 rule using negation-as-failure (\+).
likes(wallace, cheese).
likes(grommit, cheese).
likes(wendolene, sheep).
friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).Interactive queries:
?- friend(wallace, grommit).
true.
?- friend(grommit, wallace).
true.
?- friend(wallace, wendolene).
false.
?- likes(wallace, What).
What = cheese.Note: Querying
friend(X, Y).with unbound variables returns no results because\+(X = Y)fails when bothXandYare uninstantiated (they unify with each other). Bind the variables throughlikes/2first if you want to enumerate all pairs:?- likes(X, _), likes(Y, _), friend(X, Y).
Demonstrates multi-step inference: food_type + flavor facts → food_flavor rule.
food_type(velveeta, cheese). food_flavor(X, Y) :- food_type(X, Z), flavor(Y, Z).
flavor(savory, cheese).
flavor(savory, meat).
flavor(sweet, dessert).
flavor(sweet, soda).Interactive queries:
?- food_flavor(spam, What).
What = savory.
?- food_flavor(twinkie, What).
What = sweet.
?- food_flavor(What, sweet).
What = jolt ;
What = twinkie.
?- food_flavor(X, Y).
X = velveeta, Y = savory ;
X = spam, Y = savory ;
X = sausage, Y = savory ;
X = jolt, Y = sweet ;
X = twinkie, Y = sweet.
ritzhas no flavor becauseflavor(_, cracker)is not defined in the knowledge base.
Uses Prolog's search to find a valid 3-coloring of five neighboring US states.
different(red, green). different(red, blue).
different(green, red). different(green, blue).
different(blue, red). different(blue, green).Interactive queries:
?- coloring(Alabama, Mississippi, Georgia, Tennessee, Florida).
Alabama = blue,
Mississippi = red,
Georgia = red,
Tennessee = green,
Florida = green ;
...Press ; to see the next solution, or . to stop.
Illustrates unification (=) and generating combinations.
cat(lion). cat(tiger).
dorothy(X, Y, Z) :- X = lion, Y = tiger, Z = bear.
twin_cats(X, Y) :- cat(X), cat(Y).Interactive queries:
?- dorothy(X, Y, Z).
X = lion,
Y = tiger,
Z = bear.
?- twin_cats(X, Y).
X = lion, Y = lion ;
X = lion, Y = tiger ;
X = tiger, Y = lion ;
X = tiger, Y = tiger.
?- cat(lion).
true.