logic programming language prolog

23
Logic Programming Language Logic Programming Language PROLOG PROLOG Presented by: Presented by: Jeffrey Coleman & Desi Jeffrey Coleman & Desi Doncheva Doncheva

Upload: cole-obrien

Post on 30-Dec-2015

104 views

Category:

Documents


2 download

DESCRIPTION

Logic Programming Language PROLOG. Presented by: Jeffrey Coleman & Desi Doncheva. Concepts of Logical Programming and Prolog. I. Overview of Logic Programming Concepts. A. Declarative programming paradigm. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Logic  Programming  Language  PROLOG

Logic Programming Language Logic Programming Language

PROLOGPROLOG

Presented by:Presented by:

Jeffrey Coleman & Desi DonchevaJeffrey Coleman & Desi Doncheva

Page 2: Logic  Programming  Language  PROLOG

Concepts of Logical Programming and Prolog Concepts of Logical Programming and Prolog

I. Overview of Logic Programming Concepts.I. Overview of Logic Programming Concepts. A. Declarative programming paradigm. A. Declarative programming paradigm.

B. The role of Predicate Calculus in Logic Programming.B. The role of Predicate Calculus in Logic Programming. C. Characteristics of Logic Programming Languages - declarative semantic.C. Characteristics of Logic Programming Languages - declarative semantic. II. Prolog.II. Prolog.

A. Origins and OSI Standard for Prolog.A. Origins and OSI Standard for Prolog. B. Basic Elements in Prolog.B. Basic Elements in Prolog. C. Definition of Program Construction in Prolog.C. Definition of Program Construction in Prolog.

D. Prolog Resolution.D. Prolog Resolution. E. Deficiencies of Prolog.E. Deficiencies of Prolog.

III. Applications.III. Applications. A. Use of Logic Programming in implementation of RDBMS.A. Use of Logic Programming in implementation of RDBMS. B. Expert Systems.B. Expert Systems. C. Language Processing.C. Language Processing. IV. Prolog Language Variances. IV. Prolog Language Variances. A. ObjLog.A. ObjLog. B. TuProlog.B. TuProlog. C. VAP Prolog.C. VAP Prolog. D. Lambda Prolog.D. Lambda Prolog. E. Visual Prolog. E. Visual Prolog. V. Conclusion – the importance of Logic Programming Language Prolog.V. Conclusion – the importance of Logic Programming Language Prolog.

Page 3: Logic  Programming  Language  PROLOG

I. Overview of Logic Programming Concept.I. Overview of Logic Programming Concept.

Logic Programming uses a significantly different approach Logic Programming uses a significantly different approach from functional and imperative programming in the process of from functional and imperative programming in the process of developing the program and producing the result of the developing the program and producing the result of the execution .execution .

Programs are expressed in terms of symbolic logic and logical Programs are expressed in terms of symbolic logic and logical inferences. Logical Programming is built over first-order inferences. Logical Programming is built over first-order predicate calculus.predicate calculus.

Logic programs are declarative - the program only describes Logic programs are declarative - the program only describes what should be accomplished rather than the algorithm the what should be accomplished rather than the algorithm the program must follow in order to produce the desired result. program must follow in order to produce the desired result.

What is needed by the program to provide the capability of What is needed by the program to provide the capability of logic programming is the relevant information and the method logic programming is the relevant information and the method of inference for computing the result.of inference for computing the result.

Page 4: Logic  Programming  Language  PROLOG

Predicate calculus defines terms such as atomic propositions, Predicate calculus defines terms such as atomic propositions, compound terms, antecedents and consequents of a causal form compound terms, antecedents and consequents of a causal form proposition, resolution of sequence of propositions that are used proposition, resolution of sequence of propositions that are used to define relations by Facts or by Rules in Logic Programming to define relations by Facts or by Rules in Logic Programming syntax.syntax.

Logic Programming Languages have declarative semantics. Logic Programming Languages have declarative semantics. Every program consists of declarations (propositions) rather than Every program consists of declarations (propositions) rather than assignments and control flow statements.assignments and control flow statements.

Declarative semantics is simpler than the semantics of the Declarative semantics is simpler than the semantics of the

imperative languages. For example, the meaning of a proposition imperative languages. For example, the meaning of a proposition in declarative language can be determined from the proposition in declarative language can be determined from the proposition itself. In contrast, a simple assignment statement in imperative itself. In contrast, a simple assignment statement in imperative language requires variable type checks, examination of local language requires variable type checks, examination of local declarations, and the scope rules of the language. declarations, and the scope rules of the language.

Page 5: Logic  Programming  Language  PROLOG

II. Prolog. II. Prolog.

A.A. Origins and OSI Standard for Prolog.Origins and OSI Standard for Prolog.

Developed by Alain Colmerauer, a French computer scientist, with Developed by Alain Colmerauer, a French computer scientist, with the assistance of Robert Kowalski at University of Edinburgh in the assistance of Robert Kowalski at University of Edinburgh in 1972.1972.

Few dialects of Prolog are available because two independent Few dialects of Prolog are available because two independent European universities research on the development and the use of European universities research on the development and the use of the language.the language.

Prolog gained popularity when Japanese government launched a Prolog gained popularity when Japanese government launched a research project on developing Fifth Generation Computer Systems research project on developing Fifth Generation Computer Systems in 1981 and Prolog was chosen as a basis language for research. in 1981 and Prolog was chosen as a basis language for research.

The widely available syntax of Prolog is the so-called Edinburgh The widely available syntax of Prolog is the so-called Edinburgh syntax, which in 1996 was officially accepted as OSI standard for syntax, which in 1996 was officially accepted as OSI standard for Prolog. Prolog.

Page 6: Logic  Programming  Language  PROLOG

B.B. Basic Elements.Basic Elements. There are only a few data types called data objects in Prolog but they can be There are only a few data types called data objects in Prolog but they can be

constructed to be very complex.constructed to be very complex. The type of an object is recognized by its syntactic form. The type of an object is recognized by its syntactic form.

DATA OBJECTS

SIMPLE OBJECTS STRUCTURES

CONSTANTS VARIABLES

ATOMS NUMBERS

Page 7: Logic  Programming  Language  PROLOG

Simple Data Objects.Simple Data Objects. ConstantsConstants are concrete objects in are concrete objects in

form of Atom or Integer.form of Atom or Integer. AtomsAtoms areare symbolic values symbolic values

represented by strings of letters, represented by strings of letters, digits and the underscore character digits and the underscore character starting with a lower-case letter. starting with a lower-case letter.

Ex. anna, x25, x_, x_AB Ex. anna, x25, x_, x_AB IntegersIntegers in Prolog include real and in Prolog include real and

integer numbers. But numbers are integer numbers. But numbers are not heavily used because the not heavily used because the language primarily uses symbolic language primarily uses symbolic non-numeric computation. non-numeric computation.

VariablesVariables are strings of letters, digits are strings of letters, digits and an underscore character, which and an underscore character, which must start with an upper-case letter. must start with an upper-case letter. The lexical scope of the variable The lexical scope of the variable name is the clause the variable name is the clause the variable appears in. The process of binding a appears in. The process of binding a value to a variable is called value to a variable is called instantiation. instantiation.

Ex. X, Result, _23, Object2 Ex. X, Result, _23, Object2

Structures.Structures. StructuresStructures are objects that have are objects that have

several components. Structures several components. Structures can be nested—a component of a can be nested—a component of a structure can be another structure. structure can be another structure. The form of a structure is as The form of a structure is as follows:follows:

functor( parameters list)functor( parameters list) Ex. Ex. data( 1, may, 2001) – all data( 1, may, 2001) – all

components are constants. components are constants. data (Day , may, 2001) – Day is data (Day , may, 2001) – Day is a variable. a variable.

A structure object can be pictured A structure object can be pictured as a tree. The root of the tree is the as a tree. The root of the tree is the functor and the children of the root functor and the children of the root are the parameters in the structure are the parameters in the structure declaration. If one of the declaration. If one of the parameters is also a structure, then parameters is also a structure, then we define a subtree for that tree . we define a subtree for that tree .

Page 8: Logic  Programming  Language  PROLOG

C.C. Definition of Program Construction in Prolog. Definition of Program Construction in Prolog.

A program written in Prolog starts with information assumed to be always A program written in Prolog starts with information assumed to be always true, then we define a theorem or a set of theorems, and finally, we ask if the true, then we define a theorem or a set of theorems, and finally, we ask if the theorem is satisfied for some set of objects. theorem is satisfied for some set of objects.

Prolog programs consists of FACT statements, RULE statements and GOAL Prolog programs consists of FACT statements, RULE statements and GOAL statements. statements.

In proving theorems, we always use a set of information that we know is In proving theorems, we always use a set of information that we know is always true. Each statement in this set or database of assumed information is always true. Each statement in this set or database of assumed information is called a FACT in Prolog.called a FACT in Prolog.

FACTS are defined with the use of structure data objects and a list of atoms FACTS are defined with the use of structure data objects and a list of atoms or integers as parameters. Each statement ends with a period. The form of or integers as parameters. Each statement ends with a period. The form of FACT statements corresponds to the headless Horn clause from predicate FACT statements corresponds to the headless Horn clause from predicate calculus.calculus.

Ex.1Ex.1

parent ( tom, bob). Tom is Bob’s parentparent ( tom, bob). Tom is Bob’s parent

female ( linda). Linda is a female.female ( linda). Linda is a female. Prolog is suited for solving problems that define relations between objects. Prolog is suited for solving problems that define relations between objects.

The functor part of the statement actually defines the relation between the The functor part of the statement actually defines the relation between the objects in the arguments list. objects in the arguments list.

Page 9: Logic  Programming  Language  PROLOG

RULE statements have an ‘if’ and a ‘then’ part. They correspond to headed RULE statements have an ‘if’ and a ‘then’ part. They correspond to headed Horn clauses.Horn clauses.

The ‘then’ part, also called the consequent, is a single statement or term that The ‘then’ part, also called the consequent, is a single statement or term that needs to be satisfied.needs to be satisfied.

The ‘if’ part, which is called the antecedent, can be constructed from a single The ‘if’ part, which is called the antecedent, can be constructed from a single statement or a set of statements. statement or a set of statements.

Ex 2. Consider the following logical statement:Ex 2. Consider the following logical statement:

For all X and Y,For all X and Y,

Y is a child of X ifY is a child of X if

X is parent of Y.X is parent of Y.

Using Prolog RULE statements, we can construct the following Prolog Using Prolog RULE statements, we can construct the following Prolog clause:clause:

child (Y, X) :- parent ( X, Y)., child (Y, X) :- parent ( X, Y).,

where X and Y can be instantiated to any object. And if the parent (X, Y) term where X and Y can be instantiated to any object. And if the parent (X, Y) term is satisfied or true then the child (Y, X) term is also true.is satisfied or true then the child (Y, X) term is also true.

We can think of X and Y as being universal quantifiers or objects.We can think of X and Y as being universal quantifiers or objects. If there are some instances of X and Y that satisfy the antecedent part of the If there are some instances of X and Y that satisfy the antecedent part of the

RULE statement, then the consequent term is also satisfied for those same RULE statement, then the consequent term is also satisfied for those same instances of X and Y. instances of X and Y.

Page 10: Logic  Programming  Language  PROLOG

If we define a rule with more then one statement in the antecedent part, the If we define a rule with more then one statement in the antecedent part, the comma separator (,) acts as a conjunction or logical AND.comma separator (,) acts as a conjunction or logical AND.

Ex. 3 For all X and Y, Ex. 3 For all X and Y,

X is mother of Y ifX is mother of Y if

X is a female and X is parent of Y.X is a female and X is parent of Y.

Prolog rule:Prolog rule:

mother (X , Y) :- mother (X , Y) :-

female (X), female (X),

parent (X, Y).parent (X, Y).

In the second example BOTH female (X) AND parent (X, Y) must be true for In the second example BOTH female (X) AND parent (X, Y) must be true for some instances of X and Y in order for the rule mother (X, Y) to be satisfied for some instances of X and Y in order for the rule mother (X, Y) to be satisfied for the same X and Y. the same X and Y.

Ex. 4Ex. 4

siblings (X, Y) : -siblings (X, Y) : -

mother(M, X), mother(M, Y),mother(M, X), mother(M, Y),

father(F, X), father(F, Y).father(F, X), father(F, Y). So far we have seen how known facts and rules that define relationships among So far we have seen how known facts and rules that define relationships among

facts are described in Prolog, but it’s still not clear what the output of a Prolog facts are described in Prolog, but it’s still not clear what the output of a Prolog program would look like. program would look like.

Page 11: Logic  Programming  Language  PROLOG

After we define the facts and the rules of inference between objects, we can After we define the facts and the rules of inference between objects, we can simply ask if certain rules are true for certain sets of objects.simply ask if certain rules are true for certain sets of objects.

This type of Prolog statement is the GOAL statement. This type of Prolog statement is the GOAL statement. The syntactic form of a Prolog GOAL statement is identical to a headless The syntactic form of a Prolog GOAL statement is identical to a headless

Horn Clause.Horn Clause.

factor ( parameters list).factor ( parameters list).

where factor is the name of the relationship that will be proved to be either where factor is the name of the relationship that will be proved to be either true or false for the parameters in the parameter list. true or false for the parameters in the parameter list.

Ex. 5 The following clause will be either true or false. Ex. 5 The following clause will be either true or false.

female( linda ).female( linda ).

The system will display YES if the predicate female is satisfied for the The system will display YES if the predicate female is satisfied for the atom linda, either by fact reference or by rule reference, or NO if the predicate atom linda, either by fact reference or by rule reference, or NO if the predicate is not satisfied.is not satisfied.

Prolog GOAL statements can include a combination of variables and atoms. Prolog GOAL statements can include a combination of variables and atoms. In this case, the system not only displays the result of the satisfaction of the In this case, the system not only displays the result of the satisfaction of the GOAL statement, but also the values of the variable or variables for which the GOAL statement, but also the values of the variable or variables for which the GOAL statement is satisfied. GOAL statement is satisfied.

Ex. 6 GOAL statement: father (X, bob).Ex. 6 GOAL statement: father (X, bob).

Result: X = tom yes Result: X = tom yes

Page 12: Logic  Programming  Language  PROLOG

D.D. Prolog Resolution.Prolog Resolution.

How does the program decide if a GOAL statement is true or false? How does the program decide if a GOAL statement is true or false? When the programmer ask a question, the system uses the process of When the programmer ask a question, the system uses the process of

inference to satisfy each of the propositions when the GOAL is a compound inference to satisfy each of the propositions when the GOAL is a compound proposition (more then one statement must be satisfied).proposition (more then one statement must be satisfied).

The inferencing process must find a chain of inference rules and/or facts in the The inferencing process must find a chain of inference rules and/or facts in the database that connects the GOAL to one or more facts in the same database.database that connects the GOAL to one or more facts in the same database.

If Q is the GOAL then either Q must be found in the facts database OR If Q is the GOAL then either Q must be found in the facts database OR

the inferencing process must find a fact P1 or a sequence of facts the inferencing process must find a fact P1 or a sequence of facts

P1, P2, P3,…, PN, such thatP1, P2, P3,…, PN, such that

P2 : - P1, P2 : - P1,

P3 : - P2,P3 : - P2,

… …....

Q : - PN. Q : - PN.

The process of finding the Ps is complicated when we have compound The process of finding the Ps is complicated when we have compound propositions in the antecedent part of the GOAL statement. This process is propositions in the antecedent part of the GOAL statement. This process is also called matching because the system basically compares and matches the also called matching because the system basically compares and matches the rules with each other. rules with each other.

Page 13: Logic  Programming  Language  PROLOG

Bottom-up resolution ( Forward chaining).Bottom-up resolution ( Forward chaining). The system begins with facts and the rules in the database and attempts to The system begins with facts and the rules in the database and attempts to

find a sequence of matches that lead to the GOAL. It’s suitable for large find a sequence of matches that lead to the GOAL. It’s suitable for large numbers of possible correct answers. Not used by Prolog system of numbers of possible correct answers. Not used by Prolog system of inferencing.inferencing.

Top-down resolution ( Backtrack chaining).Top-down resolution ( Backtrack chaining). The system begins with the goal and attempts to find a sequence of The system begins with the goal and attempts to find a sequence of

matching prepositions that lead to the same set of original facts in the matching prepositions that lead to the same set of original facts in the database. The backtrack chaining works when a small set of candidate database. The backtrack chaining works when a small set of candidate answers are possible. Prolog system uses the backtrack chaining because of answers are possible. Prolog system uses the backtrack chaining because of the large class of problems that are suitable to be solved with top-down the large class of problems that are suitable to be solved with top-down resolution. resolution. Ex. 7 Database of facts and rules: Ex. 7 Database of facts and rules:

father (bob).father (bob). man (X) : - father (X).man (X) : - father (X).

GOAL: GOAL: man (bob).man (bob).

Forward resolution? Forward resolution? Backtracking resolution?Backtracking resolution?

Page 14: Logic  Programming  Language  PROLOG

What does the system do when a subgoal fails?What does the system do when a subgoal fails?

Prolog system uses the backtracking mechanism to backtrack to a certain Prolog system uses the backtracking mechanism to backtrack to a certain subgoal and reexamines all the possible answers. Multiple solutions result subgoal and reexamines all the possible answers. Multiple solutions result from different instantiations to the variables in the subgoal. The from different instantiations to the variables in the subgoal. The backtracking mechanism for finding solutions requires a lot of memory and backtracking mechanism for finding solutions requires a lot of memory and time because it finds every possible solution for each subgoal. time because it finds every possible solution for each subgoal.

Page 15: Logic  Programming  Language  PROLOG

E.E. Deficiencies of Prolog.Deficiencies of Prolog.

Resolution Order ControlResolution Order ControlThe Prolog resolution technique is sequential—it starts with the first The Prolog resolution technique is sequential—it starts with the first predicate in the database and continues until it has examined all of the facts predicate in the database and continues until it has examined all of the facts and rules. This can be very inefficient and hinder optimization. By contrast, and rules. This can be very inefficient and hinder optimization. By contrast, pure logic examines the prepositions in a non-deterministic manner, leaving pure logic examines the prepositions in a non-deterministic manner, leaving out facts or rules which are less likely to be called upon.out facts or rules which are less likely to be called upon.

Closed-World AssumptionClosed-World AssumptionThe Prolog system knows nothing about the world other than what is in its The Prolog system knows nothing about the world other than what is in its database, and anything that cannot be proven true from inference of that database, and anything that cannot be proven true from inference of that database is assumed to be false. Thus, Prolog is actually a true/fail rather database is assumed to be false. Thus, Prolog is actually a true/fail rather than a true/false system. This means that a programmer must account for than a true/false system. This means that a programmer must account for every possibility while writing a Prolog program.every possibility while writing a Prolog program.

Intrinsic LimitationsIntrinsic LimitationsDespite the versatility and power of logic programming, there are some sets Despite the versatility and power of logic programming, there are some sets of problems that it cannot solve without resorting to procedural of problems that it cannot solve without resorting to procedural programming concepts, such as sorting lists. The programmer must still programming concepts, such as sorting lists. The programmer must still define a procedure for the system to follow in these cases.define a procedure for the system to follow in these cases.

Page 16: Logic  Programming  Language  PROLOG

III. ApplicationsIII. Applications

A. Use of Logic Programming in implementation of RDBMSA. Use of Logic Programming in implementation of RDBMSIn DBS, the query language SQL is used to execute queries. SQL In DBS, the query language SQL is used to execute queries. SQL is a nonprocedural language, which makes it similar to logic is a nonprocedural language, which makes it similar to logic programming languages such as Prolog. The table which stores programming languages such as Prolog. The table which stores information in the database system can be described as a set of information in the database system can be described as a set of structures in a logic programming language, and the relationships structures in a logic programming language, and the relationships between the tables can be described by rules of inference. between the tables can be described by rules of inference. Therefore, the use of logic programming to implement RDBMS Therefore, the use of logic programming to implement RDBMS systems is advantageous in terms of using a single language to systems is advantageous in terms of using a single language to input, output, and process data, and in the capability to deduce input, output, and process data, and in the capability to deduce information not built into the database. (Conventional databases information not built into the database. (Conventional databases store facts or data without inference rules.)store facts or data without inference rules.)

B. Expert Systems.B. Expert Systems. Expert systems, also called knowledge-based systems, are Expert systems, also called knowledge-based systems, are programs that behave as an expert for some small set of problems. programs that behave as an expert for some small set of problems. They are capable of solving problems that require some knowledge They are capable of solving problems that require some knowledge built into the system. They are designed to offer solutions to built into the system. They are designed to offer solutions to problems for which the set of initial information is inconsistent and/or problems for which the set of initial information is inconsistent and/or incomplete. Defining an expert system involves the presentation of incomplete. Defining an expert system involves the presentation of the knowledge and creation of a user-friendly interface in a manner the knowledge and creation of a user-friendly interface in a manner by which proper reasoning can be applied to arrive at a solution.by which proper reasoning can be applied to arrive at a solution.

Page 17: Logic  Programming  Language  PROLOG

C. Natural Language ProcessingC. Natural Language Processing

A form of logical programming can be used to describe the A form of logical programming can be used to describe the language syntax just as a context-free grammar would. The language syntax just as a context-free grammar would. The semantic of a natural language can be made clear by modeling semantic of a natural language can be made clear by modeling the language with logic programming.the language with logic programming.

Page 18: Logic  Programming  Language  PROLOG

IV. Prolog Language VariantsIV. Prolog Language Variants

KL1—An experimental AND-parallel version of the sequential KL1—An experimental AND-parallel version of the sequential logic language KL0.logic language KL0.

ObjLog—A frame-based language which combines objects with ObjLog—A frame-based language which combines objects with Prolog predicates.Prolog predicates.

TuProlog—A Java-based lightweight Prolog engine, used for TuProlog—A Java-based lightweight Prolog engine, used for research at the University of Bologna, Italy. The core is a tiny research at the University of Bologna, Italy. The core is a tiny Java object which contains only the essential Prolog predicates Java object which contains only the essential Prolog predicates such as input and output, and requires the Java Virtual machine to such as input and output, and requires the Java Virtual machine to run.run.

Visual Prolog—Distributed by Borland, Visual Prolog is a strongly Visual Prolog—Distributed by Borland, Visual Prolog is a strongly object-oriented extension of Prolog. It can be used to build object-oriented extension of Prolog. It can be used to build Microsoft Windows GUI applications, console applications, DLLs, Microsoft Windows GUI applications, console applications, DLLs, and CGI. It combines the best features of logic and object-and CGI. It combines the best features of logic and object-oriented programming.oriented programming.

VAP Prolog—An open-source Prolog engine compatible with ISO VAP Prolog—An open-source Prolog engine compatible with ISO Prolog standard.Prolog standard.

Page 19: Logic  Programming  Language  PROLOG

Lambda-Prolog—Developed at the University of Minnesota, it Lambda-Prolog—Developed at the University of Minnesota, it features high-ordered programming, using functions as arguments features high-ordered programming, using functions as arguments to other functions, as in lambda calculus.to other functions, as in lambda calculus.

Strawberry Prolog—A dialect of Prolog whose syntax is close to Strawberry Prolog—A dialect of Prolog whose syntax is close to that of ISO Prolog with numerous extensions.that of ISO Prolog with numerous extensions.

Page 20: Logic  Programming  Language  PROLOG

ConclusionConclusion

We have only covered a fraction of Prolog and concepts of logic We have only covered a fraction of Prolog and concepts of logic programming, but we can see that it is very different from the programming, but we can see that it is very different from the functional and procedural programming that we are more familiar functional and procedural programming that we are more familiar with. Logic programming is very powerful for defining relations with. Logic programming is very powerful for defining relations between objects. We have seen how easy it is to define familial between objects. We have seen how easy it is to define familial relations simply by using facts and rules, without having to know how relations simply by using facts and rules, without having to know how the system interprets and derives solutions to questions such as the system interprets and derives solutions to questions such as “Who are Ivan’s siblings?” More advanced data structures, such as “Who are Ivan’s siblings?” More advanced data structures, such as lists, are used to represent sets , trees, and graphs; such data lists, are used to represent sets , trees, and graphs; such data structures are useful in creating games, solving scheduling problems, structures are useful in creating games, solving scheduling problems, finding paths and other functions involving graphs, and so on, in a finding paths and other functions involving graphs, and so on, in a much clearer way than procedural programming. Because of its much clearer way than procedural programming. Because of its small number of primitive constructs, many more complex constructs small number of primitive constructs, many more complex constructs can be designed in a way that is easy for the programmer to write can be designed in a way that is easy for the programmer to write and for an outside observer to read.and for an outside observer to read.

Page 21: Logic  Programming  Language  PROLOG

Simple ProgramSimple Program FACTS Declaration:FACTS Declaration:

parent(tom, bob).parent(tom, bob). parent(tom, liz).parent(tom, liz).

parent(bob, ann).parent(bob, ann). parent(bob, pat).parent(bob, pat). parent(pat, jim).parent(pat, jim). female(pam).female(pam). female(liz).female(liz). female(ann).female(ann). female(pat).female(pat).

male(tom).male(tom). male(bob).male(bob). male(jim).male(jim).

RULES Declaration:RULES Declaration:

offspring(Y, X):- parent(X, Y).offspring(Y, X):- parent(X, Y). mother(X, Y):- parent(X, Y), female(X).mother(X, Y):- parent(X, Y), female(X). grandparent(X,Z):-parent(X, Y), parent(Y, Z).grandparent(X,Z):-parent(X, Y), parent(Y, Z). ssister(X, Y):-parent(Z, X), parent(Z, Y), female(X), \==(X, Y).ister(X, Y):-parent(Z, X), parent(Z, Y), female(X), \==(X, Y). predecessor(X, Z):-parent(X, Z).predecessor(X, Z):-parent(X, Z). predecessor(X, Z):-parent(X, Y), predecessor(Y, Z).predecessor(X, Z):-parent(X, Y), predecessor(Y, Z).

Page 22: Logic  Programming  Language  PROLOG

Simple Queries that Prolog engine can answer based on FACTs and RULEs previously Simple Queries that Prolog engine can answer based on FACTs and RULEs previously declared:declared:

parent( bob, pat).parent( bob, pat).

parent( tom, ann).parent( tom, ann).

child( ann, bob).child( ann, bob).

child( liz, bob).child( liz, bob).

predecessor( X, liz).predecessor( X, liz).

predecessor( X, jin). predecessor( X, jin).

predecessor( pam, Z). predecessor( pam, Z).

predecessor( X, Z).predecessor( X, Z).

The free GNU Prolog interpreter and compiler can be downloaded from The free GNU Prolog interpreter and compiler can be downloaded from www.gprolog.orgwww.gprolog.org. It’s supported on Linux and Windows platforms as well as many other . It’s supported on Linux and Windows platforms as well as many other architectures. A comprehensive manual is also available. GNU Prolog compiler architectures. A comprehensive manual is also available. GNU Prolog compiler complies with ISO Prolog standard and extend the language specification by providing complies with ISO Prolog standard and extend the language specification by providing many other useful in practice predicates.many other useful in practice predicates.

Page 23: Logic  Programming  Language  PROLOG

ReferencesReferences Sebesta R., Concepts of Programming Languages, 7Sebesta R., Concepts of Programming Languages, 7 thth Ed., Ed.,

2006, Pearson Education, Inc2006, Pearson Education, Inc Bratko I., Prolog – Programming for Artificial Intelligent, 3Bratko I., Prolog – Programming for Artificial Intelligent, 3rdrd Ed., Ed.,

2000, Eddison Wesley 2000, Eddison Wesley GNU Prolog Compiler Manual - GNU Prolog Compiler Manual - www.gprolog.orgwww.gprolog.org