.Open CS320 Prolog Programming . Check out New things on the Course Web Page .See http://cse.csusb.edu/dick/cs320/index.html . Goals -- Now it is your turn to write some Prolog Your task in this laboratory is to create a new example of Prolog and place it one your web site. Your lab page should link to a page that describes what it does and what you learned plus a link to the (public) source code. 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. You can build your data base and predicates to model any world that you are interested in. I'm looking for a small "proof of concept". Keep it simple! I do not require a perfect working Prolog program -- just a prototype that could be improved to something useful. You need a source code file to link to your page. Develop this in your favorite editor with extension ".plg". You can compile and test it using 'swipl' or 'Q' on our lab systems. . A Simple Example of how Prolog works 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. .As_is % --------- month(Number, LongName). --------- .As_is month(1, january). .As_is month(2, february). .As_is month(3, march). .As_is ... 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 .As_is swipl and ask prolog to compile your file: .As_is consult('months.plg'). (don't forget that period!) List the information about `month`: .As_is listing(month). You can now use this data in many ways. First, it can test to see if something is a particular month: .As_is month(1,january). .As_is month(2,january). Second, it can test whether a number or atom is ANY month. Because `_` matches any argument value at all: .As_is month(1, _). .As_is month(12,_). .As_is month(13,_). .As_is month(_,january). .As_is 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: .As_is month(1, X). .As_is month(2, X). .As_is month(13,X). OR Vice versa you can generate the number from the name: .As_is month(N, february). .As_is month(N, march). OR, even be used to generate a whole series of months: .As_is month(Number, Name). .As_is (tap ';' after each pair) OR, a series of names, with no numbers: .As_is month(_, Name). OR, a series of Numbers, with no Names: .As_is month(N, _). How about the summer months: .As_is month(N,X), N>3, N<6. Or a pair of months: .As_is (X=january; X=march), month(N,X). Notice the leverage: .As_is 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? .Open 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 .As_is retract( Query ) looks up a matching piece of data in the usual way, but then deletes it from the data base. The predicate .As_is 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: .As_is dynamic(functor/arity). . A Student Records database Down load and examine the following file: .See http://www/dick/cs320/prolog/students.plg Can you see how it records some information on 3 students? Notice the clever use of Prolog operators to encode grades: .As_is cs320=a: A grade of A in CS320 .As_is cs320+b: A grade of B+ in CS320 .As_is 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`: .See http://www/dick/cs320/prolog/ar.plg It defines the following commands: .As_is load_students. .As_is list_students. .As_is save_students. .As_is admit(Student_number, Student_name). .As_is dismiss(Student_number, Student_name). Notice it also needs you to have download .See http://www/dick/cs320/prolog/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: .See http://www/dick/cs320/prolog/grading.plg Save this program. You will also need your `students.plg` and `ar.plg` file. Start up the `grading.plg` program and use .As_is consult('ar.plg'). .As_is load_students. to get the data online. Add student with number 9999 and name 'Joe Coyote'. Hint. .See Admissions and Records above. Give him the grade of a in cs125. .As_is 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. .Close A Prototype Student Data Base . More on data bases Also see .See UML to Prolog below. .Open More Examples 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: .As_is 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: .As_is cones(0). To count a cone we: .Box Extract the old value out of the data base (cones(Old)) Add one to the Old number we found Re-assert the new value of the number of cones. .Close.Box Like this .As_is 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: .As_is cone(A,B, C), count, fail. .As_is cones(Number). . Prolog List Processing See Sebesta Section 16.6.7 for examples of handling lists. Ask Prolog for a listing of .As_is append Study it. Test it out. Trace it. Describe in English how 'append' works. . Problems and programs 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. .Close CS320 Prolog Examples . 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: .See http://www/dick/samples/prolog.html Also see a selection of messages from the SWI-Prolog mailing list .See http://www/dick/cs320/prolog/mbox The SWI manual (with some GIFs missing) is in .See http://cse.csusb.edu/dick/cs320/prolog/SWI-prolog/ and the Gnu Prolog manual is at .See http://cse.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... .Set 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. .As_is End of line does not count. Prolog is highly case sensitive. Variables all have a capital letter first. .As_is X is a variable - with an unknown (as yet?) value. .As_is 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 .As_is swipl You compile a program inside Prolog by .As_is consult('name_of_file'). .Close.Set . 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` .As_is EDIT=name_of_your_favorite_editor .As_is VISUAL=$EDIT .As_is EDITOR=$EDIT .As_is 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 arity::= `the number of arguments in a compound term.`. 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`. .Close CS320 Prolog Programming