Download presentation
Presentation is loading. Please wait.
1
A closer look to prolog San Nabaty
2
Contents Syntax Characters Operators Equality and Unification
Arithmetic Summary of Satisfying Goals
3
syntax The syntax of a language describes how we are allowed to fit words together. In English, the syntax of the sentence "I see a zebra" is correct, but the syntax of "zebra see I a" is not correct. Prolog programs are built from terms. A term is either a constant, a variable, or a structure. We saw each of these terms in the previous chapter, but we did not know them by these names. Each term is written as a sequence of characters. Characters are divided into four categories as follows: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z + - * / \ ~ A < > : . # $ 8. The first row consists of upper-case letters. The second row consists of lower-case letters. The third row consists of digits. The fourth row consists of sign characters.
4
Constants Constants names specific objects or specific relationships. There are two kinds of constants: atoms, and numbers. Examples of atoms : likes , mary , john , book , owns. The following is example of atom: a void = 'george-smith' --> george_smith ieh2304. The following is not example of atom: 2304ieh george-smith Void _alpha. The special symbols that Prolog uses to denote questions "?-" and rules ":-" are also atoms. The "e" notation is used to denote a power of 10. So, for example, the number -2.67e2 is x 102 or just -267; 6.02e-23 is 6.02x 10"2 3 .
5
Variables The second kind of term used in Prolog is the variable.
Variables look like atoms, except they have names beginning with a capital letter or an underline sign "_". A variable should be thought of as standing for some object that we are unable or unwilling to name at the time we write the program. In the example Prolog clauses we have seen so far, we have used variables with names such as X, Y, and Z. However, the names can be as long as you like, for example: Answer Input Gross_Pay _3_blind_mice A_very_long_variable_name Sometimes one needs to use a variable, but its name will never be used. For example, if we want to find out if anyone likes John, but we do not need to know just who it is, we can use the anonymous variable. The anonymous variable is written as a single underline character. Our example is written in Prolog as: ?- likes(_, john).
6
Structures Structures are useful when there is a common kind of object, of which many may exist. Books, for example. In Chapter 1 we discussed the fact owns(john, book) . to denote that John owns some particular book. If we later said owns(mary, book). this means that Mary owns the same object that John owns,because it has the same name. There is no other way of telling objects apart, except by their name. We could say: owns(john,wuthering_heights) owns(mary, moby_dick) Structures help to organize the data in a program, because they permit a group of related information to be treated as a single object (a library card) instead of as separate entities. The way that you decompose data into components depends on what problem you want to solve. Inside the owns fact we have a structure by the name of book, which has two compo-nents, a title and an author. Since the book structure appears inside the fact as one of the fact's arguments, it is acting as an object, taking part in a relationship. If we like, we can also have another structure for the author's name, because there were three Bronte writers we wish to distinguish: owns(john, book(wuthering_heights, author(emily, bronte))).
7
Structures Structures may participate in the process of question-answering using variables. For example, we may ask if John owns any book by any of the Bronte sisters: ?- owns(john, book(X, author(Y, bronte))) If this is true, X will then be instantiated to the title that was found, and Y will be instantiated to the first name of the author. Or, we may not need to use the variables, so we can use anonymous ones: ?- owns(john, book(_, author(_, bronte))) We could improve the book structure by adding another argument indicating which copy the book is. For example, a third argument, where we would insert an integer, would provide a way of uniquely identifying a book: owns(john, book(ulysses, author(james, joyce), 3129)). which we could use to represent John owns the 3,129th copy of Ulysses, by James Joyce.
8
Characters Prolog recognizes two kinds of characters: printing characters and non-printing characters. Printing characters cause a symbol to appear on your computer terminal's display. Non-printing characters do not cause a symbol to appear, but cause an action to be carried out. Such actions include printing a blank space, beginning new lines of text, or perhaps making a beeping sound. The following are all the printing characters that can be used : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z ! " # $ % & ' ( ) = - ~ A | \ { } [ ] _ + ; * : < > , . ? / Some of the characters have special meanings. For example, the round brackets are used to enclose the components of a structure. However, we shall see in later chapters that all the characters may be treated as information by Prolog programs. Characters may be printed, read from the keyboard, compared, and take part in arithmetic operations.
9
Operators When we write the arithmetic expression "x+y*z", we call the "plus" sign and the "multiply" sign operators. If we had to write the arithmetic expression "x+y*z" in the normal way for structures, it would look like this: +(x,*(y,z)), and this would be a legal Prolog term. The operators are sometimes easier to use, however, because we have grown accustomed to using them in arithmetic expressions ever since our schooldays. Also, the structure form requires that round brackets be placed around the factor's components, which may be awkward at times. The precedence of an operator is used to indicate which operation is carried out first. Each operator that is used in Prolog has a precedence class associated with it. The precedence class is an integer that is associated with an operator. This means that expressions like "8/4/4" are read as "(8/4)/4". Also, "5+8/2/2" is read as "5+((8/2)/2) “.
10
Equality and Unification
One noteworthy predicate is equality, which is an infix operator written as "=". When an attempt is made to satisfy the goal ?- X = Y (pronounced "X equals Y"), Prolog attempts to unify X and Y, and the goal succeeds if they unify. We can think of this act as trying to make X and Y equal. The equality predicate is built-in, which means that it is already defined in the Prolog system. Within a use of some clause, X always equals X, and we exploit this property when defining the equality predicate in the way shown. Given a goal of the form X=Y, where X and Y are any two terms which are permitted to contain instantiated variables, the rules for deciding whether X and Y are equal are as follows: If X is an instantiated variable, and if Y is instantiated to any term, then X and Y are equal. Also, X will become instantiated to whatever Y is. For example, the following question succeeds, causing X to be instantiated to the structure rides(student, bicycle) ?- rides(student, bicycle) = X
11
Equality and Unification
Two structures are equal if they have the same factor and number of components, and all the corresponding components are equal. For example, the following goal succeeds, and causes X to be instantiated to bicycle: rides(student, bicycle) = rides(student, X) Look closely: this is not a question about rides; it is a question about =. Structures can be "nested" one inside another to any depth. If such nested structures are tested for equality, the test may take more time to carry out, because there is more structure to test. Exercise : Say whether the following goals would succeed, and which variables, if any, would be instantiated to what values: pilots(A, London) = pilots(London, pans) point(X, Y, Z) = point(Xl, Yl, Zl) letter(C) = word(letter) noun(alpha) = alpha ‘student’= student f(X, X) = f(a, b) f(X, a(b, c)) = f(Z, a(Z, c))
12
Arithmetic In Prolog we are allowed to write arithmetic as infix operators: X =:=Y X and Y stand for the same number X =\=Y X and Y stand for different numbers X < Y X is less than Y X > Y X is greater than Y X =< Y X is less than or equal to Y X >= Y X is greater than or equal to Y Note that the "less than or equal to" symbol is not written as "<=" as in many pro-gramming languages. As these comparison operators are predicates, one might think it possible to write a Prolog fact as follows : 2 > 3 in order to assert that 2 is actually greater than 3. A fact like this one is perfectly well-formed in Prolog, However, Prolog will not allow further facts to be added to predicates that are "built in" to Prolog. This prevents you from changing the meaning of built-in predicates in unexpected ways.
13
Arithmetic Consider the following database about the population and area of various countries We will use the predicate pop to represent the relationship between a country and its population. pop(usa, 203). pop(india, 548). pop(china, 800). pop(brazil, 108). area(usa, 3). area(india, 1). area(china, 4). area(brazil, 3).
14
Arithmetic Now to find the population density of a country, we must use the rule that the density is the population divided by the area. This can be represented as the predicate density, where the goal density(X,Y) succeeds for country X having Y as the population density of that country. A Prolog rule for this is: density(X, Y) :- pop(X, P), area(X, A), Y is P / A. The rule is read as follows: The population density of country X is Y, if: The population of X is P, and The area of X is A, and Y is calculated by dividing P by A.
15
Summary of Satisfying Goals
Note// We need to use the "is" predicate any time we require to evaluate an arithmetic expression. Getting back to the population density example, it is not hard now to see that typical questions and their answers are: ?- density(china, X). X=200 yes ?- density(turkey, X). no In the first question, the X=200 is Prolog's answer, meaning 200 people per square mile. The second question failed, because the population of Turkey could not be found in our example database.
16
Summary of Satisfying Goals
Prolog attempts to satisfy the goals in a conjunction, whether they appear in a rule body or in a question, in the order they are written (left to right). This means that Prolog will not attempt to satisfy a goal until its neighbor on the left has been satisfied. And, when it has been satisfied, Prolog will attempt to satisfy its neighbor on the right. Consider the following simple program about family relations: female(mary). parent(C, M, F) :- mother(C, M), father(C, F). mother(john, ann). mother(mary, ann). father(mary, fred). father(john, fred). Let us look at the sequence of events that leads to answering the question: ?- female(mary), parent(mary, M, F), parent(john, M, F). This question is to find whether mary is a sister of john.
17
Thank you for your attention
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.