CS 214: Programming Languages
Spring 2009

Home|Syllabus|Schedule
<<|>>|Prolog templates

Basic Prolog
Prolog, Iteration 1

Logic Programming

Prolog is a language for logic programming. This is not to say that programming in other languages is illogical (with the possible exceptions of Visual Basic and FALSE). It is to say that Prolog is built almost entirely on logic and not control flow—conditional execution is implicit in the logic and iteration comes from recursion (which is just a logical relationship).

Technical Stuff

Run SWI-Prolog at a command line in a directory you've set aside for this assignment:

unix-%  pl

Use any text editor to type in your Prolog code.

Simple Examples from Learn Prolog Now!

Work through Chapter 1 of Learn Prolog Now! (LNP!).

Of course, it's not quite as simple as that.

Working with SWI-Prolog

Each subsection of Section 1.1 starts with a knowledge base. Using the book's terminology, you can create files kb1.pl, kb2.pl, etc. For each of these files, also create a file for unit tests: kb1.plt, kb2.plt, etc.

Load a knowledge base into SWI-Prolog with this command:

?- ['kb1.pl', 'kb1.plt'].

All of the interactive commands listed in LNP! can be entered in SWI-Prolog after you load the file.

Writing and Running Tests

Here's a test file for kb1.plt:

:- begin_tests(kb1).

test(woman) :- woman(mia).
test(playsAirGuitar) :- playsAirGuitar(jody).
test(playsAirGuitar, [fail]) :- playsAirGuitar(mia).
test(playsAirGuitar, [fail]) :- playsAirGuitar(vincent).
test(tatooed, [fail]) :- tatooed(jody).

:- end_tests(kb1).

Each test file will begin and end the same way (tweaking kd1 appropriately). Each of the tests here correspond to the interactive coding described in Section 1.1.1 of LNP!.

To run these tests, load the files with computation code and unit tests, then run:

?- run_tests.