Introduction to Prolog Prolog • "Programming with Logic" • Declarative • Very different from other (procedural) programming languages • Good for knowledge-rich tasks History of Prolog First Prolog interpreter by Colmerauer and Roussel 1972 1977 1980 1980s/1990s 2005 History of Prolog implementation of DEC10 compiler by Warren 1972 1977 1980 1980s/1990s 2005 History of Prolog Definite Clause Grammars implementation by Pereira and Warren 1972 1977 1980 1980s/1990s 2005 History of Prolog Prolog grows in popularity especially in Europe and Japan 1972 1977 1980 1980s/1990s 2005 History of Prolog Prolog used to program natural language interface in International Space Station by NASA 1972 1977 1980 1980s/1990s 2005 Consequences • Think declaratively, not procedurally – Challenging – Requires a different mindset • High-level language – Not as efficient as, say, C – Good for rapid prototyping – Useful in many AI applications Deficiencies of Prolog Resolution Order Control 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 and rules. This can be very inefficient and hinder optimization. By contrast, pure logic examines the prepositions in a non-deterministic manner, leaving out facts or rules which are less likely to be called upon. Deficiencies of Prolog. Closed-World Assumption 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 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 every possibility while writing a Prolog program. Deficiencies of Prolog. Intrinsic Limitations Despite the versatility and power of logic programming, there are some sets of problems that it cannot solve without resorting to procedural programming concepts, such as sorting lists. The programmer must still define a procedure for the system to follow in these cases. Application Area of Prolog implementation of RDBMS In DBS, the query language SQL is used to execute queries. SQL is a nonprocedural language, which makes it similar to logic programming languages such as Prolog. The table which stores information in the database system can be described as a set of structures in a logic programming language, and the relationships between the tables can be described by rules of inference. Application Area of Prolog Expert Systems. Expert systems, also called knowledge-based systems, are programs that behave as an expert for some small set of problems. They are capable of solving problems that require some knowledge built into the system. They are designed to offer solutions to problems for which the set of initial information is inconsistent and/or incomplete. Application Area of Prolog Natural Language Processing. A form of logical programming can be used to describe the language syntax just as a context-free grammar would. The semantic of a natural language can be made clear by modeling the language with logic programming. Basic idea of Prolog Describe the situation of interest Ask a question Prolog logically deduces new facts about the situation we described Prolog gives us its deductions back as answers Using Prolog SWI Prolog • Freely available Prolog interpreter • Works with – Linux, – Windows, or – Mac OS • There are many more Prolog interpreters • Not all are ISO compliant Using Prolog First, write your program (away from computer!). Then, type it into a file, with a .pl extension. Any text editor will do. Then, start prolog system You will be presented with the Prolog prompt |?- Using Prolog Then, `consult' your file (omitting the .pl): |?- consult(YourFileName). Or |?- [YourFileName]. The entire content of your file is then stored in the memory of the Prolog interpreter. You can see what is consulted by typing | ?- listing. Then you can ask questions (queries) of your database. Using Prolog If you edit your program file (e.g. to correct something), be sure to consult it again afterwards! To exit from Prolog, type |?- halt. or press Control/D Using Prolog The Prolog comment characters: – Single line comments:% % This is a comment This not a comment, but an error – Multiple line comments: /* /* This is a multi-line comment which must be closed with a */ Using Prolog Prolog & English Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- woman(mia). yes ?- Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?-playsAirGuitar(jody). Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?-playsAirGuitar(jody). Yes ?- Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- playsAirGuitar(mia). No ? Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). no ?- Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?- Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?-party. Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?-party. Yes ?- Knowledge Base 1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party. ?-rockConcert. No ?- Knowledge Base 2 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):listens2music(yolanda). Knowledge Base 2 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?- Knowledge Base 2 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). ?- playsAirGuitar(mia). yes ?-playsAirGuitar(yolanda). yes Knowledge Base 2 fact happy(yolanda). fact listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). Knowledge Base 2 fact rule happy(yolanda). rule fact listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). rule Basic Elements of Prolog Prolog program is a database of facts and rules. Some are always true (facts): father( john, jim). Some are dependent on others being true (rules): parent( Person1, Person2 ) :father( Person1, Person2 ). To run a program, we ask questions (query about the database Knowledge Base 2 Predicate happy(yolanda). name Predicate: is the name given to the word occurring before the bracket in a fact or rule. Both facts and rules are predicate definitions. By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true. Knowledge Base 2 Predicate name father( john, jim). Arguments A predicate head consists of a predicate name and sometimes some arguments contained within brackets and separated by commas. Knowledge Base 2 start:sum,nl. sum:write('X= '),read(X), write('Y= '),read(Y), S is X+Y, write('Sum is '),write(S). A predicate may have no arguments Knowledge Base 2 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). head body Clause A clause consists of a head (fact) and sometimes a body (rule). Clauses playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). goal goal Head body Clause A body can be made up of any number of subgoals (calls to other predicates) Clauses happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). There are five clauses in this kb: two facts, (fact is a rule with an empty body) three rules. The end of a clause is marked with ‘.’ Clauses happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). There are three predicates (procedure) in this kb: happy listens2music playsAirGuitar (1 clause/rule) (2 clauses/rules) (2 clauses/rules) Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). The comma “," expresses conjunction in Prolog Q. How many facts & rules in the kb? Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(vincent). no ?- Knowledge Base 3 happy(vincent). listens2music(butch). playsAirGuitar(vincent):listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). ?- playsAirGuitar(butch). yes ?- Expressing Conjunction happy(vincent). listens2music(butch). playsAirGuitar(vincent):listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). Expressing Disjunction happy(vincent). Two rules with listens2music(butch). the same head playsAirGuitar(vincent):- listens2music(vincent), Ξ or happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch). happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch); listens2music(butch). Prolog and Logic • Clearly Prolog has something to do with logic • Operators – Implication :– Conjunction , (the comma means and) – Disjunction ; (Prolog symbol for or) • Use of modus ponens Modus Ponens The rule of logic stating that if a conditional statement (“if p then q ”) is accepted, and the antecedent ( p ) holds, then the consequent ( q ) may be inferred. Example: If you have a current password, then you can log on to the network: "You have a current password“ Therefore: "You can log on to the network" This has the form: p→q p ∴q Modus Ponens If you have a current password, then you can log on to the network: “You can't log into the network” "You don't have a current password" This has the form: ¬q p→q ∴ ¬p Therefore: Knowledge Base 4 woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). Q. How many facts & rules in the kb? Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- woman(X). X=mia; X=jody; X=yolanda; no Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(marsellus,X), woman(X). X=mia yes ?- Prolog Variables woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). ?- loves(pumpkin,X), woman(X). no ?- Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). Q. How many facts & rules in the kb? four facts - one rule – 3 variables. Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W). W=vincent ?- Prolog Syntax What exactly are facts, rules and queries built out of? There are three types of term: 1. Constants. These can either be atoms (such as vincent) or numbers (such as 24). 2. Variables. 3. Complex terms. These have the form: functor(term_1,...,term_n). Atoms A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter Examples: butch, big_kahuna_burger, playGuitar An arbitrary sequence of characters enclosed in single quotes Examples: 'Vincent', 'Five dollar shake', '@$' A sequence of special characters Examples: @= and ====> and ; and :- Numbers Integers: 12, -34, 22342 Floats: 34573.3234 Variables A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with either an uppercase letter or an underscore Examples: X, Y, Variable, Vincent, _tag The variable _ is special. Is called the anonymous variable Anonymous Variable A variable that stands in for some unknown object Stands for some objects about which we don’t care Several anonymous variables in the same clause need not be given consistent interpretation written as _ in Prolog for example ?- composer(X, _, _). X = beethoven; X = mozart; We are interested in the names of composers but not their birth and death years Complex Terms Atoms, numbers and variables are building blocks for complex terms Complex terms are often called structures. Complex terms are built of a functor directly followed by a sequence of arguments Arguments are put in round brackets, separated by commas The functor must be an atom and cannot be a variable. Examples of complex terms Examples as seen before: – playsAirGuitar(jody) – loves(vincent, mia) – jealous(marsellus, W) Complex terms inside complex terms: – hide(X,father(father(father(butch)))) Arity The number of arguments a complex term has is called its arity Examples: - woman(mia) is a term with arity 1 - loves(vincent,mia) has arity 2 - father(father(butch)) arity 1 Arity is important In Prolog you can define two predicates with the same functor but with different arity. Prolog treat this as two different predicates: love(vincent, mia). love(vincent, marcellus, mia). Arity is important In Prolog documentation arity of a predicate is usually indicated with the suffix "/" followed by a number to indicate the arity This knowledge base defines: –happy/1 –listens2music/1 –playsAirGuitar/1 Example of Arity happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):listens2music(yolanda). • This knowledge base defines: – happy/1 – listens2music/1 – playsAirGuitar/1 quiz 1- Which of the following sequences of characters are atoms, which are variables, and which are neither? atom 1. vINCENT variable 2. Footmassage atom 3. variable23 atom 4. variable2000 atom 5. big_kahuna_burger atom 6. ’big kahuna burger’ neither 7. big kahuna burger atom 8. ’Jules’ variable 9. _Jules atom 10. ’_Jules’ quiz 2- Which of the following sequences of characters are atoms, which are variables, which are complex terms, and which are not terms at all? Give the functor and arity of each complex term. 1. loves(Vincent,mia) 2. ’loves(Vincent,mia)’ 3. Butch(boxer) 4. boxer(Butch) 5. and(big(burger),kahuna(burger)) 6. and(big(X),kahuna(X)) 7. _and(big(X),kahuna(X)) 8. (Butch kills Vincent) 9. kills(Butch,Vincent) 10. kills(Butch,Vincent complex term, functor: loves, arity: 2 atom neither complex term, functor: boxer, arity: 1 complex term, functor: and, arity: 2 complex term, functor: and, arity: 2 neither neither complex term, functor: kills, arity: 2 neither quiz 3- How many facts, rules, clauses, and predicates are there in the following knowledge base? What are the heads of the rules, and what are the goals they contain? facts: 3 woman(vincent). rules: 4 woman(mia). clauses: 7 man(jules). predicates: 5 person(X) :- man(X); woman(X). loves(X,Y) :- knows(Y,X). father(Y,Z) :- man(Y), son(Z,Y). father(Y,Z) :- man(Y). quiz 4- How many facts, rules, clauses, and predicates are there in the following knowledge base? What are the heads of the rules, and what are the goals they contain? woman(vincent). woman(mia). man(jules). person(X) :- man(X); woman(X). loves(X,Y) :- knows(Y,X). father(Y,Z) :- man(Y), son(Z,Y). father(X,Y,Z) :- man(Y). quiz 5. Here are simple rules: Formulate the following queries? has(jack,apples). has(ann,plums). has(dan,money). fruit(apples). fruit(plums). - what has Jack? - does Jack have something? - who has apples and who has plums? - does someone have apples and plums? - has Dan fruits? quiz Answer ?- has(jack,X). % what has Jack? ?- has(jack,_). % does Jack have something? ?- has(X,apples),has(Y,plums). % who has apples and who has plums? ?- has(X,apples),has(X,plums). % does someone have apples and plums? ?- has(dan,X),fruit(X). %has Dan fruits? quiz 6. Here are simple facts. likes(mary,food). likes(mary,wine). likes(john,wine). likes(john,mary). Formulate the following queries? 1. John likes anything that Mary likes 2. John likes anyone who likes wine 3. John likes anyone who likes themselves quiz Answer likes(mary,food). likes(mary,wine). likes(john,wine). likes(john,mary). likes(john,X),likes(mary,X). % John likes anything that Mary likes Likes(john,X),likes(X,wine). % John likes anyone who likes wine Likes(john,X),likes(X,X). % John likes anyone who likes themselves quiz 7- Bible tree Write the appropriate clauses (facts) which represents the following family tree? abraham ismael isaac esau iacob quiz Answer parent(abraham,ismael). parent(abraham,isaac). parent(isaac,esau). parent(isaac,iacob). grandfather(B,N):- parent(B,P),parent(P,N). % B is grandfather of N brother(F1,F2):- parent(P,F1),parent(P,F2),\F1=F2. % F1 brother of F2 descendent(X,Y):- parent(X,Y). % X descendant of Y descendent(X,Y):- parent(X,Z),descendent(Z,Y). quiz 8- Family tree Write the appropriate clauses (facts) which represents the following family tree? James I Elizabeth Charles I Sophia Catherine Charles II James II George I quiz Formulate the following queries: Was George I the parent of Charles I? Who was Charles I's parent? Who were the children of Charles I? Express the following rules: - M is the mother of X if she is a parent of X and is female - F is the father of X if he is a parent of X and is male - X is a sibling of Y if they both have the same parent. Furthermore add rules defining: "sister", "brother", "aunt", "uncle", "grandparent", "cousin" quiz Answer male(james1). male(charles1). male(charles2). male(james2). male(george1). female(catherine). female(elizabeth). female(sophia). parent(charles1, james1). parent(elizabeth, james1). parent(charles2, charles1). parent(catherine, charles1). parent(james2, charles1). parent(sophia, elizabeth). parent(george1, sophia). quiz Answer ?- parent(charles1, george1). % Was George I the parent of Charles I? ?- parent(charles1,X). % Who was Charles I's parent? ?- parent(X,charles1). % Who were the children of Charles I? quiz Answer mother(X,M):- parent(X,M), female(M). % M is the mother of X if she is a parent of X and is female father(X,F):- parent(X,M), male(M). % F is the father of X if he is a parent of X and is male sibling(X,Y):- parent(X,Z), parent(Y,Z). % X is a sibling of Y if they both have the same parent. quiz Answer Furthermore add rules defining: "sister", "brother", "aunt", "uncle", "grandparent", "cousin" sister(Y,X):- female(X), parent(X,M), parent(Y,M). % X is a sister of Y brother(Y,X):- male(X), parent(X,M), parent(Y,M). % X is a brother of Y parent female X M sister parent Y parent male X M brother parent Y quiz Answer Furthermore add rules defining: "sister", "brother", "aunt", "uncle", "grandparent", "cousin" aunt(Y,X):- female(X), sister(X,M), mother(Y,M). aunt(Y,X):- female(X), brother(X,M), father(Y,M). % X is an aunt of Y M father brother female X M aunt Y mother sister female X aunt Y quiz Answer Furthermore add rules defining: "sister", "brother", "aunt", "uncle", "grandparent", "cousin" uncle(Y,X):- male(X), sister(X,M), mother(Y,M). uncle(Y,X):- male(X), brother(X,M), father(Y,M). % X is an uncle of Y M father brother male X M uncle Y mother sister male X uncle Y quiz Answer Furthermore add rules defining: "sister", "brother", "aunt", "uncle", "grandparent", "cousin" grandparent(Y,X):- male(X), father(X,M), parent(M,Y). grandparent(Y,X):- female(X), mother(X), parent(X,M). % X is a grandparent of Y M parent father male M X Y grandparent parent mother female X Y grandparent quiz 9- Here are six English words: (abalone, abandon, anagram, connect, elegant, enhance) They are to be arranged in a crossword puzzle like fashion in the given grid given. Write prolog program to solve this crossword word(abalone,a,b,a,l,o,n,e). word(abandon,a,b,a,n,d,o,n). word(enhance,e,n,h,a,n,c,e). word(anagram,a,n,a,g,r,a,m). word(connect,c,o,n,n,e,c,t). word(elegant,e,l,e,g,a,n,t). crossword(H1,H2,H3,V1,V2,V3) :word(H1,_,A,_,B,_,C,_), word(H2,_,D,_,E,_,F,_), word(H3,_,G,_,H,_,I,_), word(V1,_,A,_,D,_,G,_), word(V2,_,B,_,E,_,H,_), word(V3,_,C,_,F,_,I,_). quiz ?crossword(H1,H2,H3,V1,V2,V3). RECALL Basic Elements of Prolog The Prolog comment characters: – Single line comments:% % This is a comment This not a comment, but an error – Multiple line comments: /* /* This is a multi-line comment which must be closed with a */ © Patrick Blackburn, Johan Bos & Kristina Striegnitz Basic Elements of Prolog happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):listens2music(yolanda). Basic Elements of Prolog Prolog program is a database of facts and rules. Some are always true (facts): father( john, jim). Some are dependent on others being true (rules): parent( Person1, Person2 ) :father( Person1, Person2 ). To run a program, we ask questions (query about the database Basic Elements of Prolog playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). goal goal Head body Clause Basic Elements of Prolog fact rule happy(yolanda). rule fact listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda). Fact clause without body rule Basic Elements of Prolog Predicate Name Argument happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). Summary • A Prolog program consists of predicate definitions. • A predicate denotes a property or relationship between objects. • Definitions consist of clauses. • A clause has a head and a body (Rule) or just a head (Fact). • A head consists of a predicate name and arguments. • A clause body consists of a conjunction of terms. • Terms can be constants, variables, or compound terms. • We can set our program goals by typing a command that unifies with a clause head. • A goal unifies with clause heads in order (top down). • Unification leads to the instantiation of variables to values. • If any variables in the initial goal become instantiated this is reported back to the user.