1 / 122

Artificial Intelligence programming and Prolog Language Tutorial

Artificial Intelligence programming and Prolog Language Tutorial. Contents. What is logic programming? Small Examples The Basics Another Example: Towers of Hanoi Other Examples. What is Logic Programming?. What is Logic Programming?.

ovid
Download Presentation

Artificial Intelligence programming and Prolog Language Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Artificial Intelligence programming and Prolog Language Tutorial

  2. Contents • What is logic programming? • Small Examples • The Basics • Another Example: Towers of Hanoi • Other Examples

  3. What is Logic Programming?

  4. What is Logic Programming? • “The use of mathematical logic for computer programming.” (Wikipedia) • “A declarative, relational style of programming based on first-order logic.” (Dictionary.com)

  5. History of Prolog • Developed in 1972 by Alain Colmerauer and Philippe Roussel • Name comes from “PROgramming in LOGic” • A solution to the debate about which kinds of logic to use

  6. History of Logic Programming • Came about in 1960s and 1970s due to debates about using declarative or procedural representations in AI • Stanford and Edinburgh – declarative • MIT - procedural • Developed Planner in 1969 (first language in the proceduralistic paradigm).

  7. Systems for Logic Programming • ALF • CLP • ECLiPSe • Elf • Fish • Flang • Gödel • KLIC • LIFE • MONA • Oz System • RELFUN • SAMPLE • XSB Just to name a few….

  8. Systems for Logic Programming, cont. • Prolog is the most common system • Prolog has many variations: • &-Prolog, And-parallel Prolog. • ACE, And-Or-parallel Prolog. • Actor Prolog • Andorra-I, an Or- and (deterministic) and-parallel Prolog. • Aurora, Or-parallel Prolog. • cu-Prolog, a constraint logic programming language • lambda Prolog • LeanTaP, a small theorem prover written in SICStus Prolog. • Logtalk, an extension for Object-Oriented Programming in Prolog. • Mixtus, an automatic partial evaluator for full Prolog. • Muse, Or-parallel Prolog.

  9. Prolog Structure

  10. Prolog Structure • Prolog facts – a database of predicates and associations. • Prolog rules – define new predicates by using Prolog facts. • Note: • Prolog considers capital letters to denote variables, not predicates.

  11. Prolog Structure – Queries • A query searches the database for the first fact that satisfies its goal. • Ifa fact is found thenit either unifies the variable with a constant orPrologreturns yes. • Ifa fact is not found that meets that condition thenProlog returns no.

  12. Prolog Structure – disjunction and conjunction. • Use a semi-colon to request subsequent answers. • In other words, a semi-colon signifies disjunction.  • A comma signifies conjunction.

  13. How Prolog Works: Example 1 Example of data base: instructor (perkowski, ee271) instructor (perkowski, ee171) instructor (perkowski, ee478) enrolled (alan-cheng, ee475) enrolled (matthew, ee171) enrolled (alan-cheng,ee171) enrolled (alan-cheng,ee271) enrolled (chris-clark,ee271) enrolled (edison-tsai, ee171) enrolled (chris-clark, ee171) This is the database of Prolog facts.

  14. How Prolog Works, cont. Prolog rules: teaches (P,S) :- instructor (P,C), enrolled (S,C) This is to say that an instructor only teaches if he teaches a class and students are enrolled in that class. teaches (Professor, Student) :- instructor (Professor, Class), enrolled (Student,Class) teaches (Professor, Student)  instructor (Professor, Class), enrolled (Student,Class) instructor (Professor, Class), enrolled (Student,Class)  teaches (Professor, Student)

  15. How Prolog Works, cont. • Prolog answers queries based off of the database that has been given. ?enrolled (chris-clark, ee271) yes ?enrolled (X, ee271) alan-cheng chris-clark ?teaches (X, alan-cheng) perkowski ?teaches (X, chris-clark) Perkowski Jeske greenwood

  16. Expanding Database in Prolog • Imagine what happens if we expand the database: • instructor (perkowski, ee271) • instructor (perkowski, ee171) • instructor (perkowski, ee478) • enrolled (jeske, ee171) • enrolled (greenwood, ee171) • enrolled (alan-chen,ee171) • enrolled (alan-chen,ee271) • enrolled (chris-clark,ee271) • enrolled (edison-tsai, ee171) • enrolled (chris-clark, ee171) • instructor (bebis, cs365) • instructor (looney, cs311) • instructor (yuksel, cs446) • instructor (helfand, cs493) • instructor (quint, math486) • enrolled (ben, cs365) • enrolled (bill, cs365) • enrolled (bill, cs446) • enrolled (brian, cs311) • enrolled (brian, cs365) • enrolled (brittney, cs311) • enrolled (brittney, cs365) • enrolled (brittney, cs446) • enrolled (cody, cs311) • enrolled (cody, cs365) • enrolled (danielle, cs365) • enrolled (danielle, cs446) • enrolled (danielle, cs493) • enrolled (david, cs365) • enrolled (javier, cs365) • enrolled (jeffrey, cs365) • enrolled (jessica, cs311) • enrolled (jessica, cs446) • enrolled (jessica, math486) • enrolled (joel, cs365) • enrolled (joseph, cs311) • enrolled (joseph, cs365) • enrolled (joseph, cs446) • enrolled (joseph, cs493) • enrolled (joseph, math486) • enrolled (kellen, cs365) • enrolled (matts, cs311) • enrolled (matts, cs365) • enrolled (mattw, cs311) • enrolled (mattw, cs365) • enrolled (mattw, cs446) • enrolled (miran, cs365) • enrolled (ryan, cs365) • enrolled (samuel, cs365) • enrolled (shane, cs311) • enrolled (shane, cs365) • enrolled (shane, cs446) • enrolled (tiffany, cs311) • enrolled (tiffany, cs365) • enrolled (tiffany, cs446)

  17. How Prolog Works, asking questions to data base ?enrolled (X, cs365) ben bill brian brittney cody danielle david javier jeffrey joel joseph kellen matts mattw miran ryan samuel shane tiffany • This list now gives us the entire roster of students in CS 365.

  18. How Prolog Works, more complicated querries Queries can be more complicated to compare more data: classmates (S1, S2) :- enrolled (S1, C), enrolled (S2, C) ?classmates (joseph, danielle) yes ?classmates (joseph, jessica) yes ?classmates (jessica, danielle) no class c c enrolled enrolled enrolled enrolled classmates s1 s1 s2 s2

  19. How Prolog Works, more complicated querries classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C) ?classmates (joseph, danielle, C) cs365 cs446 cs493 no ?classmates (joseph, jessica, C) math ?classmates (jessica, danielle, C) no

  20. Family Data base

  21. parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandparent(X,Z) :- parent(X,Y), parent(Y,Z). ancestor(X,Z) :- parent(X,Z). ancestor(X,Y) :- parent(X,Y), ancestor(Y,Z). sibling(X,Y) :- mother(M,X), mother(M,Y), father(F,X), father(F,Y), X \= Y. cousin(X,Y) :- parent(U,X), parent(V,Y), sibling(U,V). father(albert, jeffrey). mother(alice, jeffrey). father(albert, george). mother(alice, george). father(john, mary). mother(sue, mary). father(george, cindy). mother(mary, cindy). father(george, victor). mother(mary, victor).

  22. ?- [kinship]. % kinship compiled 0.00 sec, 3,016 bytes Yes ?- ancestor(X, cindy), sibling(X, jeffrey). X = george Yes ?- grandparent(albert, victor). Yes ?- cousin(alice, john). No ?- sibling(A,B). A = jeffrey, B = george ;  A = george, B = jeffrey ;  A = cindy, B = victor ;  A = victor, B = cindy ;  No SWI Prolog

  23. Unification

  24. Prolog Structure - Unification • A query resolves by unifying all of its elements. • A constant unifies with itself and any variable. • Scopeis limited to the rule in which a variable occurs. • When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

  25. PROLOG STRUCTURE - UNIFICATION • Process of making one predicate same as another. • A query resolves by unifying all of its elements. • A constant unifies with itself and any variable. • Scope is limited to the rule in which a variable occurs. • When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

  26. Example of Unification

  27. Prolog Structure - Backtracking • In more complex examples, Prolog uses backtracking to find possible solutions. • Prolog will attempt to resolve the first fact of its rule, unifying any variables with the first constant that satisfies that fact • It then attempts to resolve the rest of that rules facts. • If it is unable to do so under those conditions it “backs up” and tries again with the next available unifying constant.

  28. assertions (database) questions Clauses • Programs are constructed from A number of clauses: <head> :- <body> • Clauses have three forms: • hypotheses (facts) • conditions (rules) • goals • Both <head> and <body> are composed of relationships(also called predicationsor literals)

  29. Relationships • Represent properties of and relations among the individuals • A relationship is application of a predicate to one or more terms • Terms: • atoms(or constants): john, 25, … • variables (begin with uppercase letters): X, … • compounds • Horn clause form: At most one relationship in <head>

  30. World of Toys

  31. World of Toys - Example 2 • Let us consider the following description of a “system”; • Ann likes every toy she plays with. • A doll is a toy. • A train is a toy. • Ann plays with trains. • John likes everything Ann likes. • To express this in Prolog we must: • Identify the entities, or actual things, mentioned in the description • Identify the types of properties that things can have, as well as the relations that can hold between these things • Figure out which properties/relations hold for which entities • There is really no unique way of doing this; • we must decide the best way to structure our data • (based on what we want to do with it).

  32. A Small Example World of Toys - Example 2 • We will choose the following: • Things: • Ann, Sue, doll, train • Properties: • “... is a toy” • Relations: • “... likes ...”, “... plays with ...” • Constructing our knowledge base then consists of writing down: • which properties hold for which things • which relationships hold for which things

  33. A Small Example World of Toys - Example 2 • We write: likes(ann,X) :- toy(X), plays(ann,X). toy(doll). toy(train). plays(ann,train). likes(john,Y) :- likes(ann,Y).

  34. A Small Example – What It Means World of Toys - Example 2 • There are three important logical symbols :- if , and ; or • X and Y are variables • ann, john, doll and train are constants • likes, toy and plays are predicate symbols

  35. A Small Example – What It Means World of Toys - Example 2 • A variable represents some unspecified element of the system • A constant represents a particular, known, member of the system • A predicate represents some relation or property in the system. • Note that: • Variables always start with an upper-case letter or an underscore • Predicates and constants always start with a lower-case letter or digit

  36. A Small Example – What It Means World of Toys - Example 2 • Each line in a Prolog program is called a clause • There are two types of clauses - facts and rules • Rules are clauses which contain the “:-” symbol • Facts are clauses which don't • Each fact consists of just one predicate • Each rule consists of a predicate, followed by a “:-” symbol, followed by a list of predicates separated by “,” or “;” • Every clause is terminated by a “.” (full-stop). • In a rule, the predicate before the “:-” is called the head of the rule • The predicates coming after the ``:-'' are called the body

  37. A Small Example – What It Means World of Toys - Example 2 • For example: likes(ann,X) :- toy(X), plays(ann,X). <---Head---> <-------Body------->

  38. World of Toys - Example 2 A Small Example – What It Means • We “define a predicate” by writing down a number of clauses which have that predicate at their head • The order in which we write these down isimportant • Any predicates mentioned in the body must either: • be defined somewhere else in the program, or • be one of Prolog's “built-in” predicates. • Defining a predicate in Prolog corresponds roughly to defining a procedure • Predicates occurring in the body of a clause correspond roughly to procedure calls • Note also that: • Constants and variables will never appear “on their own” in a clause. They can only appear as the “arguments” to some predicate. • Predicates will (almost) never appear as arguments to another predicate

  39. World of Toys - Example 2 What It Says: • So, after all that, what does our little program say? • Having all the relations expressed as a predicate followed by arguments is not particularly intuitive, so with some suitable swapping-around we get: • For any X, (ann likes X) if (X is-a-toy) and (ann plays-with X). • (doll is-a-toy). • (train is-a-toy). • (ann plays-with train). • For any Y, (john likes Y) if (ann likes Y).

  40. World of Toys - Example 2 Running It • So how do we run it? • We run it by giving Prolog a query to prove • A query has exactly the same format as a clause-body: one or more predicates, separated by “,” or “;”, terminated by a full-stop • Thus, we might enter in the following as a query: • likes(john,Z). • Logically, this can be interpreted as • “is there a Z such that john likes Z?” • From a relational point of view, we can read it as: • “List all those Z's that john likes”

  41. World of Toys - Example 2 • In general terms we call the query our “goal”, and say that Prolog is being asked to (find ways to) “satisfy” the goal • This process is also known as inferencing: • Prolog has to infer the solution to the query from the knowledge base • Note that solving a query results in either: • failure, in which case “no” is printed out, or • success, in which case all sets of values for the variables in the goal (which cause it to be satisfied) are printed out

  42. World of Toys - Example 2 How It Works • So how does Prolog get an answer? • We have to solve likes(john, Y), so we must examine all the clauses which start with the predicate likes. • The first one is of no use at this point, since it only tells us what ann likes. • The second rule for likes tells us that in order to find something that john likes, we need only to find something which ann likes. So now we have a new goal to solve - likes(ann,Z). • To solve this we again examine all the rules for likes. This time the first rule matches (and the second doesn't), and so we are told that in order to find something which ann likes, we must find something which is a toy, and which annplays with.

  43. World of Toys - Example 2 • So first of all we try to find a toy. • To do this we examine the clauses with toy at their head. • There are two possibilities here: a toy is either a doll or train. • We now take these two toys, and test to see which one ann plays with; • that is, we generate two new sub-goals to solve: plays(ann,doll) and plays(ann,train). • In general, to solve these, we must look at the clauses for plays. • There is only one: since it is for train, we conclude with the answer: Z = train.

  44. World of Toys - Example 2 Exercises • Example: toys.pl • Does Ann like dolls? • Who likes trains? • What does John like? • Who plays with trains?

  45. A Small Example - Exercises • Translate the following sentences into Prolog: • John eats all kinds of food. Apples are food. Oysters are food. Anything anyone eats is food. Tom eats snakes. Sue eats everything that Tom eats. Save the program in a file called food.pl. Now read them into Prolog, and formulate queries to find out: • What John eats • What Sue eats • If there is anything which both John and Sue eat. • Who eats snakes

  46. Lists

  47. Prolog Lists: concept • Lists are a collection of terms inside [ and ] • [ chevy, ford, dodge] • loc_list([apple, broccoli, crackers], kitchen). • loc_list([desk, computer], office). • loc_list([flashlight, envelope], desk). • loc_list([stamp, key], envelope). loc_list(['washing machine'], cellar). • loc_list([nani], 'washing machine'). • loc_list([], hall)

  48. Prolog Lists: unification • Unification works on lists just as it works on other data structures. • loc_list(X, kitchen). X = [apple, broccoli, crackers] ?- [_,X,_] = [apples, broccoli, crackers]. X = broccoli • The patterns won't unify unless both lists have the same number of elements.

  49. Prolog Lists: member and append • List functions • [H|T] • separate list into head and tail • member • test if X is a member of a list • append • append two lists to form a third list

  50. Prolog Lists: head and tail • Head and Tail of a List • Syntax [H|T] • Examples • ?- [a|[b,c,d]] = [a,b,c,d]. %We check identity yes • ?- [a|b,c,d] = [a,b,c,d]. no

More Related