CS 3723
 Programming Languages 
   Prolog Basics  


Elementary Introduction to Prolog:

    Prolog is a non-procedural programming language. This means that Prolog does not specify step-by-step the sequences of operations and functions to be carried, but instead the programmer uses Prolog to specify the logical consequences and requirements, and leaves it to Prolog to achieve those consequences. The word "Prolog" refers to "programming in logic". Here is an initial introduction to prolog, starting with Prolog facts and rules. This application also illustrates the similarities between Prolog and a relational database management system.
    Prolog knowledgebase example

Using Prolog on the Elk System:

    Right now Prolog is available on the elk machines, so one must first do an ssh to elk0n, n=1,6. Here is a sample prolog source file, stored using the filename hanoi.pl and a sample session showing the use of this file (boldface is user input):

      % ssh wagner@elk01.cs.utsa.edu
      password: (give password here)
      elk01% apropos prolog
      plld (1)  - Create emb. executable
      plrc (1)  - SWI-Prolog res. arch.
      prolog (1)- SWI-Prolog 5.6.59
      swipl (1) - SWI-Prolog 5.6.59
      xpce (1)  - Prolog with GUI
      
      elk01:~> cat hanoi.pl
      % move all discs onto the center pole
      % discs start piled in order at left
      hanoi(N) :- move(N, left, center, right).
      
      move(0, _, _, _) :- !.
      move(N, A, B, C) :-
         M is N-1,
         move(M, A, C, B),
         inform(N, A, B),
         move(M, C, B, A).
      
      inform(I, X, Y) :- write('move disc '),
         write(I), write(' from '),
         write(X), write(' to '), write(Y), nl.
      
      elk01% prolog
      Welcome to SWI-Prolog ...
        ... blah, blah ...
      ?- consult(hanoi).
      % hanoi compiled 0.00 sec, 1,640 bytes
      true.
      
      ?- hanoi(1).
      move disc 1 from left to center
      true.
      
      ?- hanoi(2).
      move disc 1 from left to right
      move disc 2 from left to center
      move disc 1 from right to center
      true.
      
      ?- hanoi(3).
      move disc 1 from left to center
      move disc 2 from left to right
      move disc 1 from center to right
      move disc 3 from left to center
      move disc 1 from right to left
      move disc 2 from right to center
      move disc 1 from left to center
      true.
      ?- halt.
      


Prolog Resources:


A simple Prolog database example:

  • Suppliers and supplies example: .html .


The "Company" Database in Prolog:

  • The company database (from Elmasri/Navathe Fundamentals of Database Systems 2nd Ed.) here (in PDF).
  • The same company database in prolog company.pl (text).
  • Some rules for this database in prolog rules.pl (text).
  • Sample interactive session in prolog session.html (in HTML).


UTSA CS Courses in Prolog:

    Here is raw data in the form of Prolog facts for the upper-division CS elective courses:

      spr03u(cs2213,  '001', 1900-mw,  maltrud).
      spr03u(cs2213,  '002', 0930-tr,  maltrud).
      spr03u(cs2413,  '001', 1000-mwf, maynard).
      spr03u(cs2513,  '001', 1200-mwf, key).
      spr03u(cs2733,  '001', 1300-mwf, wagner).
      spr03u(cs3233,  '001', 1230-tr,  tian).
      spr03u(cs3343,  '001', 1530-tr,  kwek).
      spr03u(cs3723,  '001', 1200-mwf, wagner).
      spr03u(cs3733,  '001', 1230-tr,  srobbins).
      spr03u(cs3743,  '001', 1900-tr,  staff).
      spr03u(cs3773,  '001', 1730-tr,  lo).
      spr03u(cs4313,  '001', 1400-mw,  bylander).
      spr03u(cs4363,  '001', 1000-mwf, wagner).
      spr03u(cs4383,  '001', 1300-mwf, maynard).
      spr03u(cs4753,  '001', 1730-mw,  yum).
      spr03u(cs4913,  '000', arranged, staff).
      spr03u(cs4953,  '001', 0930-tr,  srobbins).
      spr03u(cs4993,  '000', arranged, staff).
      
      semesteru(X, Y, Z, W) :- spr03u(X, Y, Z, W).
      year('2003').
      season('Spring').

    These facts need to be combined with many other facts and rules to produce a reasonable listing. Some of the facts are names of courses, full names of instructors, better representation for times, and others. These additional facts and rules do not change (much) from semester to semester, so they are more-or-less universal.

    • Database of upper-division CS courses. This is an initial very simple version to illustrate how the system works.
      • The Prolog file for courses for Spring 2003 (4.3 kbytes): spring03.pl.
      • Interactive session illustrating the program (3.5 kbytes): spring03.html.
    • Database of upper-division CS courses. This is also a simplified version to help illustrate the principles.
      • The courses for Spring 2003 (0.8 kbytes): spring03.pl.
      • The file of fixed facts and rules that are independent of the semester (9.7 kbytes): general.pl.
      • The result of "running" this program, to produce a .html file (7.8 kbytes): spring03.html.
    • The complete database for Spring 2003 courses. This is the full database of all courses. It is essentially the same as the previous example, but there are many additional details. This expanded version is about 5 times as large as the previous one.
      • Spring 2003 courses in Prolog (4.6 kbytes): spr03.pl.
      • The large file that handles all semesters (37.6 kbytes): general3.pl.
      • Resulting .html (32.7 kbytes): spr04.html.


Example Prolog Sessions:

  • Example of rulers of England: rulers.pl
  • Example of Happy Valley Food Coop: food.pl

Revision date: 2014-12-02. (Please use ISO 8601, the International Standard Date and Time Notation.)