ws1 Prolog

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Logical Analysis and Problem Solving

Prolog Basics Prolog is often used to create applications that are based on logic. This worksheet helps you to create a simple program, based on a family tree to show relationships between the people within the tree. Family Tree A family tree shows how people are related to each other. The simplest relation that can describe all the people within a family tree is to say that one person is the parent of another person. For example, in the family tree shown below, John is the parent of Jim and Jim is the parent of Mary and also the parent of a!e.

'ou are going to create a Prolog program that creates a series of facts based on the family tree shown abo!e and the parent relation described pre!iously. (. )lick on menu option File, New. 'ou will be asked to enter a filename, type in family !pl and click the "# button. An additional window should be displayed for editing Prolog programs, usually using the *otePad text editor. +. Type in the following code, $% Prol o g pro g r a m t& a t repr e s e n t s a fa mil y tre e ! %$ par e n t '( a n , rob) ! par e n t ' r o b , bill)! par e n t ' b i ll , to n y ) ! par e n t ' b i ll , l*cy ) ! par e n t '( o & n , (im)! par e n t '(i m , mary ) ! par e n t '(i m , dav e ) ! par e n t ' m a r y , ton y ) ! par e n t ' m a r y , l*cy) ! par e n t ' d a v e , +elly) ! par e n t ' d a v e , (ac+) ! -. Save the file, using menu option File, Save and exit the editor, using File, ,-it.

*ote, for clarity, the full family tree is not shown

"n terms of logic, we might represent John is the parent of Jim as parent#John, Jim$. The term parent is said to be a relation and in this relation, John is the parent of Jim. Prolog Program for a Family Tree %ogin and download the program SWI Prolog from the module web site. The initial SWI Prolog console screen should be displayed, where &ueries of Prolog programs are performed, similar to the screen shown below.

*ote, !alues within relations in Prolog m*st be must be in lower case and there should be a f*ll stop after each Prolog relation, as shown abo!e. )urrently, you ha!e put in a number of Prolog statements in an editor, but they are not yet a!ailable for use within the ./" Prolog console screen. The easiest way to enable the Prolog statements to be used, is to consult the file, where the file is compiled to check that all lines of code are correct, and then you can then &uery the Prolog statements. 0. /ithin the ./" Prolog console, select menu option File, .ons*lt and select file family !pl. "f the Prolog program has been successfully compiled, the console screen should state that the program has been cons*lted, which means that Prolog statements can now be &ueried as re&uired. .ee message shown below for example successful compile message.

.M1+(+203-4(.doc

Page

of /

Logical Analysis and Problem Solving

*ote, 'ou can 5consult6 a Prolog file without using the editor, by using menu option File, .ons*lt. At present, a large comment has been added at the start of the program using $% for the start of comments and %$ for the end of comments. )omments can be placed after Prolog statements using the 7 symbol, whereby all text typed after the 7 is ignored. At present the Prolog program simply lists a number of facts. /e now wish to answer a number of &ueries, as outlined below. 0*erying t&e Family Tree Program There are a number of &ueries that are re&uired for the Family Tree Program, such as, (. "s John the father of Jim8 +. "s Jim the father of 9ill8 -. /ho is 9ill the father of8 0. /ho is the parent of :elly8 3. /ho are the parents of %ucy8 /e can answer the &ueries as follows, To answer &uestion ( we type at the 12 prompt, parent'(o&n,(im)! and press enter . The reply yes should be displayed. Prolog processes each Prolog statement attempting to match ;ohn with the relation parent and then ;im and if the statement is tr*e, then yes is reported back. *ote how the names in the database all start with lowercase characters, this is deliberate, as uppercase characters represent !ariables, which we will see being used in a later &uestion, are used for &uerying a database. *ote also how a Prolog statement always re&uires a f*ll stop at the end of the clause. <uestion + would be &ueried by typing parent'(im,bill)! For which no should be displayed as expected, gi!en the data within the Prolog system. Prolog would ha!e processed all parent clauses until no more clauses are a!ailable and then report the &uery as false and report back no. For &uestion - type in parent'bill,.&ildren)! and press enter . The reply should be similar to that shown below,

"f you press enter after )hildren > tony then no alternati!es will be output, but if you put a semi?colon 3 after )hildren > tony then lucy will also be output. The semi?colon is the symbol for or in Prolog. 'our output should be similar to that shown below.

*ote, a !ariable name can start with any uppercase character, or underscore character, and can be one letter only, if re&uired. "nstead of )hildren you could ha!e used @, or ', but it is good practice to use names that relate to the problem in hand. For &uestion 0 type in parent'Parent, +elly)! and press enter. Parent > da!e should be output, and if you press A for an alternati!e, the reply no will output, as shown below, as no alternati!es are a!ailable within the database &ueried. 'ou may be thinking that kelly must ha!e had a mother, but in our system that fact was not made a!ailable. This highlights a characteristic of logic, called the closed world assumption , whereby only the facts gi!en are used to infer other facts.

*ote, how relation parent is treated differently than Parent, due to the initial capital. .o Prolog is case?sensiti!e, which is often a problem when de!eloping Prolog systems, so be carefulB ,-ercise )omplete the &uery for &uestion 3, using a suitable !ariable and the or symbol, where appropriate. Adding 4ender 5etails "n the family tree shown abo!e, we ha!e not indicated whether a person within the tree is male or female. /e would say, in Cnglish that Jim is male, which could be written in Prolog as male'(im)! remembering that lowercase characters must be used for facts. ,-ercise Add the gender of all the people, using relations male and female where appropriate, within the family tree, to the file family(.pl, sa!e the file as family6!pl and consult the program, using menu option File, .ons*lt, as before.

At present only tony is displayed, but bill is also the father of %ucy. /hy isn6t lucy also output8 Prolog has found the first clause where bill is specified as the parent and copied tony with the variable )hildren = it is a !ariable because the first letter is capitalised.
.M1+(+203-4(.doc

Page 6 of /

Logical Analysis and Problem Solving

7ore .omple- 0*eries "t would be useful to &uery the family tree database with gender included within the &ueries. For example, we would like to answer the following &ueries, (. /ho the male child of Jim is8 +. /ho the female parent of %ucy is8 To answer &uestion ( put parent'(im, .&ild), male'.&ild)! and press enter. The output produced should be similar to that shown below.

The &uery is asking to find a child of Jim AN5 ensure that the )hild is male. *ote that to include an A* condition we use the comma , symbol. *ote also that the !ariable )hild is the same !alue for both parent'(im, .&ild) and male'.&ild) when matching is carried out throughout the &uery. For &uestion+, type in parent'Parent, l*cy), female'Parent)! and press enter. The output should be similar to that shown below.

Thus within Prolog we can create complex &ueries, using DE and A* connecti!es as re&uired. /ithin future worksheets you will see how rules can be used to simplify complex &ueries for the user of a Prolog system. ,-ercise Fow might we ask for the brother of lucy, as a &uery using relations parent and male8

.M1+(+203-4(.doc

Page / of /

You might also like