User Tools

Site Tools



  • Gain experience with logic programming.
  • Become familiar with Prolog.
  • Gain understanding of the declarative nature of prolog.


For this class, we use SWI-Prolog. You can download binaries for a Windows or macOS system here.

Also, there is an unofficial tutorial for installation here.

For Mac with Homebrew installed, you can do this command (info here):

brew install swi-prolog

For Ubuntu, you can do these commands:

sudo add-apt-repository ppa:swi-prolog/stable
sudo apt-get update
sudo apt-get install swi-prolog


You will turn in two prolog programs (in separate files) that solve the following logic puzzles:

To help you start, we've provided a sample solution to It's a tie. Use this as a model for your own Prolog solutions.


  • There are links above to the solutions (not the Prolog code) for these two puzzles so that you can double-check the results produced by your code. We strongly encourage taking a few minutes to solve these puzzles by hand so that you get a feel for how to solve them and the “facts” you're extracting from the clues in the puzzle. This will give you a better feel for what you need to encode in your Prolog solution as well as giving you a feel for what Prolog's trying to do under the hood. (And it's fun!)
  • Just copy and paste the all_different function. It simply checks to make sure each item in the list is unique.
  • Check spelling errors. There is no such thing as misspelled atoms. So speigel and spiegel are both valid but different.
  • Once you have a solution, check it against the solutions we provide for the puzzles. If you're getting something different, you might not have sufficiently constrained the solution by extracting all of the information from the puzzle. (Or you might have just miscoded one of the facts.)
  • If you don't constrain the solution enough, you can get multiple possible answers. Even if you get lucky and happen to get the right answer as your first answer, especially if you only look at the first one found, that doesn't mean your Prolog code is correct. There is exactly one solution to each puzzle.
  • Logic tips:
    • “A implies B” or “if A then B” is equivalent to “(A and B) or (not A)”.
    • “if A then B; otherwise C” is equivalent to “(A and B) or ((not A) and C)”.
  • These problems make extensive use of the “not” operator, \+.
    • \+ works as you expect when all variables are completely bound.
    • If E contains unbound variables, \+E will try to find bindings to that will make E true. If Successful \+ will fail.
  • This lab is taken from this site. There may be additional resources there.
sc330_f2016/prolog1.txt · Last modified: 2018/10/25 19:23 by morse