Skip to main contentCal State San Bernardino
>> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CS320 Course Materials] [Text Version] lab/20.html Tue Jan 9 10:19:22 PST 2007
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]

Contents


    CS320 Prolog Programming

      Introduction

        Check out New things on the Course Web Page

        [ index.html ]

        Goals

        Your task in this laboratory is to create a new example of Prolog in action and place it one your web site.

        Keep your Prolog notes open beside your keyboard so you can look for help when things do not work as you expect. Also see the Hints at the end of this lab.

        A Simple Predicate

        In Prolog, a program is a set of definitions of predicates. Here is start of a file that defines the relationship between the number of a month and the name of the month (in lowercase letters). As in all good programs the first line is a short comment.
         	% --------- month(Number, LongName). ---------
         	month(1, january).
         	month(2, february).
         	month(3, march).
         	...
        Carefully prepare a file called months.plg that has the above lines plus nine (9) more months (4,5,6,7,8,9,10,11,12). Check to make sure that there is a period at the end of each definition.

        Start up prolog

         	pl
        and ask prolog to compile your file:
         	consult('months.plg').
        (don't forget that period!)

        List the information about month:

         	listing(month).
        You can now use this data in many ways.

        First, it can test to see if something is a particular month:

         	month(1,january).
         	month(2,january).

        Second, it can test whether a number or atom is ANY month. Because _ matches any argument value at all:

         	month(1, _).
         	month(12,_).
         	month(13,_).
         	month(_,january).
         	month(_,janember).

        Third, month can be used to generate the name given the number, as long as you give it a variable like X to put it in:

         	month(1, X).
         	month(2, X).
         	month(13,X).
        OR Vice versa you can generate the number from the name:
         	month(N, february).
         	month(N, march).
        OR, even be used to generate a whole series of months:
         	month(Number, Name).
         			(tap ';' after each pair)
        OR, a series of names, with no numbers:
         	month(_, Name).
        OR, a series of Numbers, with no Names:
         	month(N, _).

        How about the summer months:

         	month(N,X), N>3, N<6.
        Or a pair of months:
         	(X=january; X=march), month(N,X).

        Notice the leverage:

         	One set of data, many ways to use it.

        Logic Puzzles

        If you have a logic puzzle and can see how to modify the examples in the last lab.... have a go at it.

        Combinatorics

        Can you extend the snow-cones example or perhaps modify it to calculate combinations of pizza toppings?

        A Prototype Student Data Base

          A common programming technique in Prolog is to read in a data base and modify it before returning it to the data base. The predicate
           		retract( Query )
          looks up a matching piece of data in the usual way, but then deletes it from the data base. The predicate
           		assert( Fact )
          puts the query into the data base. Here are some examples of how this is done.

          Again in modern Prolog systems we have to set up the dynamic predicates in advance:

           		dynamic(functor/arity).

          A Student Records database

          Down load and examine the following file: [ students.plg ]

          Can you see how it records some information on 3 students?

          Notice the clever use of Prolog operators to encode grades:

           		cs320=a:	A grade of A in CS320
           		cs320+b:	A grade of B+ in CS320
           		cs320-c:	A grade of C- in CS320
          (This may not be a good idea... but it shows you that in Prolog you can make expressions mean anything you want).

          Note: If we don't use a list of grades in the record we have to have a separate set of facts linking students, courses and grades. For more on data base design see CSci480.

          Admissions and Records

          Here is a very simple application that manipulates the student records in students.plg: [ ar.plg ] It defines the following commands:
           		load_students.
           		list_students.
           		save_students.
           		admit(Student_number, Student_name).
           		dismiss(Student_number, Student_name).
          Notice it also needs you to have download [ students.plg ] first.

          Run the ar.plg program, load the students, list them, add a new student with number 9999 and name 'Joe Coyote', list the result, and save the students. Quit Prolog and look at the result.

          Study how the program is written. Notice the unfriendly user interface. Notice how simple the resulting code is. Hence a possible prototype to check out the algorithms and data structures, not a finished program.

          Grades.

          Look at the program to record new grades in student records: [ grading.plg ] Save this program. You will also need your students.plg and ar.plg file. Start up the grading.plg program and use
           		consult('ar.plg').
           		load_students.
          to get the data online.

          Add student with number 9999 and name 'Joe Coyote'. Hint. [ Admissions and Records ] above.

          Give him the grade of a in cs125.

           		grade(9999, cs125=a).
          List the student records. Give Joe a grade of B+ in cs201 (cs201+b), and list the result.

          Give Joe a grade of A- in CS320, and check the result...

          If this works, study the code and then try some other experiments.

        . . . . . . . . . ( end of section A Prototype Student Data Base) <<Contents | End>>

        More on data bases

        Also see [ UML to Prolog ] below.

        If you have Time

          Counting Ice-cream Cones from First Principles

          Suppose we wanted to count how many cones there are, but don't want to store each possible cone before we count. We want something like this:
           	cone(A,B,C), count, fail.
          The fail forces Prolog to backtrack and find another cone. We need the right definition for count. We want count to increase some hidden item of data each time is called, and we don't want it to undo anything when backtracking from the fail.

          Prolog variables only have one value so we can not use them for counting (except recursively - one symbol but many variables!). We can't use a variable so we use the Prolog database: Initially we have no cones:

           		cones(0).
          To count a cone we:
          1. Extract the old value out of the data base (cones(Old))
          2. Add one to the Old number we found
          3. Re-assert the new value of the number of cones.

          Like this
           	count:-retract(cones(Old)), New is Old+1, assert(cones(New)).
          (Prolog uses the jargon word retract to mean `find the first matching rule and remove it from the data base`.)

          Put together the definition of cone, scoop, cones, and count, in your cones.plg file and then test:

           	cone(A,B, C), count, fail.
          	cones(Number).

          Prolog List Processing

          See Sebesta Section 16.6.7 for examples of handling lists. Ask Prolog for a listing of
           	append
          Study it. Test it out. Trace it. Describe in English how 'append' works.

          Problems and programms From the Book

          Sebesta has some example problems and programing exercises at th end of the chapter. Some of these are in our SWI-Prolog already.

          For more on Prolog

          Schnupp and Bernard have written an excellent introduction. Their book is in the CSUSB library.

          If you want to learn the techniques used for expert systems, simulations, and half a dozen other purposes... see T Van Le's text book.

          I have a page of pointers to Prolog information: [ prolog.html ]

          Also see a selection of messages from the SWI-Prolog mailing list [ mbox ]

          The SWI manual (with some GIFs missing) is in [ http://www.csci.csusb.edu/dick/cs320/prolog/SWI-prolog/ ] and the Gnu Prolog manual is at [ http://www.csci.csusb.edu/dick/cs320/prolog/gnu/ ] and describes how to use gprolog and all the predicates it defines.

          Hints

          Study these hints if unexpected things happen...
          • Prolog is a combination of compiler and interpreter. The compiler reads in the "program" made up of data and rules. It produces a running program which reads and interprets your input according to the compiled data.

          • In Prolog you must finish each statement with a period.
             	End of line does not count.

          • Prolog is highly case sensitive. Variables all have a capital letter first.
             	X is a variable - with an unknown (as yet?) value.
             	x is the atom x

          • You get out of Prolog by inputting CTRL/D or 'halt.' on a new line.

          • You run Prolog with the command
             		pl
          • You compile a program inside Prolog by
             		consult('name_of_file').

          Default Editor

          If you have a favorite editor and want to have invoked from other UNIX programs (like mail and Prolog) then add these lines to your .profile
           		EDIT=name_of_your_favorite_editor
           		VISUAL=$EDIT
           		EDITOR=$EDIT
           		export EDIT VISUAL EDITOR

          The next time you login most UNIX programs will know what editor you like to use.

          UML to Prolog

          Speaking of data bases.... You can often do a very quick translation from a UML class diagram into a prototype Prolog data base. Each box becomes a predicate. The chemistry example had a predicate called element which would come from the UML class Element with 8 public attributes: name:atom, symbol:string, etc. . The operations would typically be written as rules. Operations that change the state of the object will need to retract the old attribute values and assert the new ones in place of the old ones. Relationships between classes can be code as: a predicate relating the two classes, a list stored as an parameter of a predicate in a class, or a single parameter identifying a unique corresponding object in the other calls. Inheritance is harder but you can write Prolog rules that attempt to find information in one predicate, and if it fails goes else where. You can also write rules that will search a series of predicates to find which one needs changing.

          Glossary

        1. arity::= the number of arguments in a compound term..
        2. functor::= the identifier of a structure in a term. It can appear immediately before a bracketed list of arguments, or be infixed between two arguments. It can also be prefixed and post-fixed.

          Bigger Hint

           	element(Name, _, Number1), element(Name, _, Number2), Number1=\=Number2.

        . . . . . . . . . ( end of section CS320 Prolog Examples) <<Contents | End>>

      End