[CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CSci620] >> lab18 [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Thu May 27 08:56:29 PDT 2004

# CSci620 Programming Languages -- More, Prolog

## Process

Running examples and taking notes.

Work in pairs!

## Deliverable

Two or three pages of notes on what you have learned. These notes can be on paper or online. They can be put on the web if you want.

End of lab

## Harry Potter and the Puzzle Potions

Here is a logic puzzle. I solve it by modeling the situation as a list of elements and trying out various permutations to find those that match the problem.

In the first Harry Potter book, Harry and Hermione have to solve a puzzle involving 7 potions. See [ Potions.htm ]

You can download the source code here: [ potion1.plg ] and then test it out. Note if you are running gprolog then you need [ potion1.pro ] instead.

## From the Book

Try out some examples in the book on pages 211.. 218.

## Some of my Examples

### Classical Algorithm

Although Prolog is not designed to express algorithms or to run them efficiently, it can do so. A first rough version of the Binary search algorithm is defined in [ binary.plg ] You can test to see if it calculated the square root of 49 correctly:
`          go.`

### A Simple Data Base

I have rapidly prototyped a data base of chemical elements. I typed in a database describing the chemical and physical properties of the elements...(most of them). Each element has a record structure (called element) that lists
Net
1. Its name - an atom
2. It's chemical symbol - an atomic string with a capital letter and and optional lower case letter
3. It's Atomic number
4. Whether it is 'radioactive' or 'not radioactive'
5. What type of element is it: metal, nonmetal, alkali, rare_earth,...
6. What its normal state is (at 0C, and normal pressure...)
7. Its Melting point(MP) in Centigrade
8. Its Boiling point(BP) in Centigrade

(End of Net)
I also define some abbreviations for getting an elements Name, or to get its element's name and symbol, or to relate name, symbol, and number:
`          element(Name).`
`          element(Name, Symbol).`
`          element(Name, Symbol, Number).`
I also define some properties of the atomic numbers: Is the element metallic or non-metallic, and what group and period in the periodic table is assigned to the element. There also three utility commands that print information about an element: period(Atomic_number), group(Atomic_Number), and print_element(Name_of_an_element).

The data and program are in [ chem.plg ] save/download a copy of this file. Load it into Prolog and try a query:

`          print_element(boron).`
To see how this works list the prolog program:
`          listing(print_element).`
Now try the following simple searches/queries of our knowledge base:
`          element(boron, Symbol, Number).`
`          element(Name, 'F', Number).`
`          element(Name, Symbol, 14).`
`          element(barium, 'Ba', 56).`
`          element(hydrogen, 'H', 24).`
`          element(hydrogen, 'H', Nbr, Rad, Class,Normally,Melt, Boil).`
`          element(Name, _, _, radioactive, _,_,_, _).`
The last of these above has several answers... tap ';' to get each radioactive element in turn. Notice the wild-card variable _.

You will have a chance to fix the problems in this data base in the next lab.

### How to Write Bad Science Fiction

Find, download and test out the 1950-style sci-fi pulp-fiction processor that is in [ story.plg ] Note... do not take this seriously. They are intended to be jokes parodying the popular magazines and movies of a less enlightened century. Here [ http://www.badmovies.org/movies/ ] are the kind of bad movies that this little program writes whenever you type the go. command.

How does it do it? Try

`  listing(go).`
Using 'help', 'listing', and some guessing can you see how I encoded the "structure" of these stories as a grammar?

### A Cryptarithm Puzzle

The following is intended to dazzle you with the sheer cleverness of Prolog. I don't expect you to duplicate this technique in this class. But I can't resist sharing it with you.

Prolog can be used to solve puzzles where you are given a sum and all the numbers have been coded as letters. The file [ crptarth.plg ] shows a clever way to this by using the fact that Prolog variables are rather like algebraic variables. They stand for blanks that need to filled in. All you have to do is generate all the possibilities and reject the ones that do not fit the rules.

Download it and use pl to compile the program:

`  ...\$ pl`
`  ?- consult('crptarth.plg').`
`  ....`
`  ?- listing.`
`  ?- go.`

## Prolog Source Code

The time has come for you to see an industrial size C program.... the Prolog Source Code. Explore this a little... before you leave the lab: [ http://www.csci.csusb.edu/dick/cs320/prolog/src/ ] This is precisely what I downloaded, uncompressed, unarchived, compiled, and installed so that you could do these labs.

Note.

Don't try to understand a large program like this. It would take you several weeks full time work to understand it all. More importantly, working with it does not mean understanding how the compiler stores the syntax tree! Even maintaining a program this big means that you only work on a small part at a time. In a well designed program, you can understand it one small part at a time.

## Hints

Look at the notes from the last lab and my handout [ Prolog.html ] and also Prolog's "help" and "trace" predicates.

. . . . . . . . . ( end of section CSci620 Programming Languages -- More, Prolog) <<Contents | Index>>

# Glossary

1. BNF::="Backus-Naur Form", for syntax and grammar, developed by Backus and Naur.
2. EBNF::="Extended " BNF.
3. HTML::= "HyperText Markup Language", used on the WWW.
4. HTML_page::syntax= "<HTML>" head body.
5. Java::="An " OO " Language from Sun".
6. LISP::= "LISt Processing Language".
7. LRM::="Language Reference Manual".
8. OO::="Object-Oriented".
9. Prolog::="Programming in Logic".
10. TBA::="To Be Announced".
11. UML::="Unified Modeling Language".
12. URL::=Universal_Resource_Locator,
13. Universal_Resource_Locator::syntax= protocol ":" location, where
Net
1. protocol::= "http" | "ftp" | "mailto" | ... ,
2. location::= O( "//" host) O(pathname).

(End of Net)
14. WWW::= See http://www.csci.csusb.edu/dick/cs620/, index to web site for this class.
15. XBNF::="eXtreme" BNF, developed by the teacher from EBNF, designed to ASCII input of syntax, semantics, and other formal specifications.