Artificial Intelligence CS370D Prolog programming Introduction to Prolog
Syntax of Prolog: Terms. Facts and Rules. Programs. Queries.
Syntax of Prolog: Terms A- Constants: 1- Identifiers: sequences of letters, digits, or underscore “_” that start with lower case letters.Ex: noura, x25, x_25, alpha_beta. 2- Numbers: Integers and real numbers:1.001, 2, 3.03 3- Strings enclosed in single quotes: ‘Noura A’ Note: can start with upper case letter, number, spaces or special characters like (==, <=,>=… etc)
Syntax of Prolog: Terms B- Variables: Sequence of letters digits or underscore that start with an upper case letter or the underscore. Ex: _x, Amal, Successor_State, Undescore by itself is the special “anonymous” variable.
Defining relations by rules. Rule is: relation can be defined by making use of the fact. structure with functor ' :- ' and two arguments: head and body. Each rule has two parts: Condition. (The right hand side) (head) Conclusion. (The left hand side) (body)
Defining relations by rules. The differences between rule and fact: Fact: is a something that always, unconditionally true. Rule: specify things that are true if some condition is satisfied.
Defining relations by rules. From previous example (parent relation): offspring(X,Y):-parent(Y,X). offspring(X,Y) -> is a conclusion part. parent(X,Y) -> is a condition part. For all X and Y,Y is an offspring of X if X is a parent of Y.
Defining relations by rules. More complex rules: Let’s add the following facts: female(leyla). male(omar). female(nour). male(khaled). female(meriam). male(ali). female(zahra). Now, let’s define the relation mother: For all X and Y ,X is the mother of Y if X is female and X is parent of Y.
Defining relations by rules. The corresponding Prolog rule is: Mother(X,Y):-female (X) , parent(X,Y). Head (conclusion part): mother (X,Y) Body (condition part): female (X) , parent(X,Y).
Defining relations by rules. female X X X offspring parent mother parent parent grandparent Y Y Y parent Z Nodes correspond to objects: arguments of relations. Arcs between nodes correspond to binary relations. Arcs are oriented so as to point from the first to the second argument. Unary relations are represented by marking the corresponding node by the name of the relation.
Defining relations by rules. Question: define the relation sister: Z parent parent X Y sister For any X and Y X is a sister of Y if Both X and Y have the same parent X is a female sister(X,Y):- female(X), parent(Z,X), parent(Z,Y).
Defining relations by rules. Let’s ask the question: is meriam sister of khaled? ?-sister(meriam,khaled). Another question: who is khaled’s sister? ?-sister(X,khaled). Think about ?-sister(X,meriam).
Recursive rule Let’s describe the predecessor relation , this relation will be defined in term of parent relation. For all X and Z X is a predecessor of Z if X is a parent of Z predecessor( X , Z ) :- parent( X , Z ).
Recursive rule x z Y1 y2 Y3 What about indirect predecessor? The rule will be as the following: predecessor(X,Z):- parent(X,Y), parent(Y,Z). parent(X,Y1), parent(Y1,Y2), parent(y2,Z). ……. parent Y1 parent y2 Predecessor parent Y3 parent z
Recursive rule x z For all X and Z there is a Y such that Y1 X is the parent of Y , and Y is a predecessor of Z. predecessor( X , Z ):- parent( X , Y ), predecessor( Y , Z ). parent Y1 parent y2 Predecessor parent Y3 Predecessor parent z
Summary Prolog programs can be extended by simply adding new clauses. Prolog clauses are of three types: facts, rules and questions. Facts declare things that are always, unconditionally, true. Rules declare things that are true depending on a given condition. By means of questions the user can ask the program what things are true. Prolog clauses consist of the head and the body. The body is a list of goals separated by commas. Commas are understood as conjunctions. Facts are clauses that have a head and the empty body. Questions have only the body. Rules have a head and a body. Variables are instantiated when the system answers questions. Variables are assumed to be universally quantified (for all).