cs344 introduction to cs344 introduction to artificial
play

CS344: Introduction to CS344: Introduction to Artificial - PowerPoint PPT Presentation

CS344: Introduction to CS344: Introduction to Artificial Intelligence g Pushpak Bhattacharyya Pushpak Bhattacharyya CSE Dept., IIT Bombay IIT Bombay Lecture 11Prolog Introduction PROgramming in LOGic Emphasis on what rather than


  1. CS344: Introduction to CS344: Introduction to Artificial Intelligence g Pushpak Bhattacharyya Pushpak Bhattacharyya CSE Dept., IIT Bombay IIT Bombay Lecture 11–Prolog

  2. Introduction � PROgramming in LOGic � Emphasis on what rather than how � Emphasis on what rather than how Problem in Declarative Form Logic Machine Basic Machine Basic Machine

  3. Prolog’s strong and weak points � Assists thinking in terms of objects and entities entities � Not good for number crunching � Useful applications of Prolog in Useful applications of Prolog in � Expert Systems (Knowledge Representation and Inferencing) Representation and Inferencing) � Natural Language Processing � Relational Databases R l ti l D t b

  4. A Typical Prolog program Compute_length ([],0). Compute_length ([Head|Tail], Length):- Compute_length (Tail,Tail_length), Length is Tail_length+ 1. High level explanation: The length of a list is 1 plus the length of the tail of the list, obtained by removing the first t il f th li t bt i d b i th fi t element of the list. This is a declarative description of the This is a declarative description of the computation.

  5. Fundamentals Fundamentals (absolute basics for writing Prolog Programs) g )

  6. Facts � John likes Mary � like(john,mary) � Names of relationship and objects must begin with a lower-case letter. � Relationship is written first (typically the Relationship is written first (typically the predicate of the sentence). � Objects are written separated by commas � Objects are written separated by commas and are enclosed by a pair of round brackets. � The full stop character ‘.’ must come at the p end of a fact.

  7. More facts Predicate Predicate I nterpretation I nterpretation valuable(gold) Gold is valuable. owns(john,gold) (j h ld) J h John owns gold. ld father(john mary) father(john,mary) John is the father of John is the father of Mary gives (john book mary) gives (john,book,mary) John gives the book to John gives the book to Mary

  8. Questions � Questions based on facts � Answered by matching y g Two facts match if their predicates are same (spelt the same way) and the arguments ( p y) g each are same. � If matched, prolog answers yes , else no . � No does not mean falsity. y

  9. Prolog does theorem proving � When a question is asked, prolog tries to match transitively. to match transitively. � When no match is found, answer is no. � This means not provable from the given This means not provable from the given facts.

  10. Variables � Always begin with a capital letter � ?- likes (john X) � ? likes (john,X). � ?- likes (john, Something). � But not But not � ?- likes (john,something)

  11. Example of usage of variable Facts: likes(john,flowers). likes(john mary) likes(john,mary). likes(paul,mary). Question: ?- likes(john,X) ? l k ( h ) Answer: X= flowers and wait ; mary ; no

  12. Conjunctions � Use ‘,’ and pronounce it as and . � Example p � Facts: � likes(mary,food). � likes(mary,tea). � likes(john,tea). � likes(john,mary) (j , y) � ?- � likes(mary,X),likes(john,X). � Meaning is anything liked by Mary also liked by John?

  13. Backtracking (an inherent property Backtracking (an inherent property of prolog programming) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john tea) likes(john,tea) likes(john,mary) 1. First goal succeeds. X=food 2. Satisfy likes(john,food) y (j )

  14. Backtracking (continued) R t Returning to a marked place and trying to resatisfy is i t k d l d t i t ti f i called Backtracking likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john tea) likes(john,tea) likes(john,mary) 1. Second goal fails 2. Return to marked place p and try to resatisfy the first goal

  15. Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john tea) likes(john,tea) likes(john,mary) 1. First goal succeeds again, X=tea 2. Attempt to satisfy the likes(john,tea) p y (j )

  16. Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john tea) likes(john,tea) likes(john,mary) 1. Second goal also suceeds 2. Prolog notifies success and waits for a reply g p y

  17. Rules � Statements about objects and their relationships � Expess � If-then conditions � I use an umbrella if there is a rain � I use an umbrella if there is a rain use(i, umbrella) :- occur(rain). � � Generalizations � All men are mortal All t l mortal(X) :- man(X). � � Definitions � An animal is a bird if it has feathers bird(X) :- animal(X), has_feather(X). �

  18. Syntax � < head> :- < body> � Read ‘:-’ as ‘if’ � Read :- as if . � E.G. � likes(john,X) :- likes(X,cricket). lik (j h X) lik (X i k t) � “John likes X if X likes cricket”. � i.e., “John likes anyone who likes cricket”. � Rules always end with ‘.’.

  19. Another Example sister_of (X,Y):- female (X), parents (X M F) parents (X, M, F), parents (Y, M, F). X is a sister of Y is X is a female and X and Y have same parents X and Y have same parents

  20. Question Answering in presence Q g p of rules � Facts � male (ram) � male (ram). � male (shyam). � female (sita) � female (sita). � female (gita). � parents (shyam, gita, ram). parents (shyam gita ram) � parents (sita, gita, ram).

  21. Question Answering: Y/N type: is sita the sister of shyam? sister of shyam? ?- sister_of (sita, shyam) parents(sita,M,F) parents(shyam,M,F) female(sita) parents(shyam,gita,ram) parents(sita,gita,ram) p ( ,g , ) success

  22. Question Answering: wh-type: whose sister is sita? sister is sita? ?- ?- sister_of (sita, X) parents(sita,M,F) parents(Y,M,F) female(sita) parents(Y,gita,ram) p parents(sita,gita,ram) ( ,g , ) parents(shyam,gita,ram) Success Success Y=shyam

  23. Exercise 1. From the above it is possible for somebody to be her own sister. How somebody to be her own sister. How can this be prevented?

  24. An example Prolog Program An example Prolog Program

  25. Shows path with mode of conveyeance from city C 1 to city C 2 :-use_module(library(lists)). go(C1,C2) :- travel(C1,C2,L), � � show_path(L). byCar(auckland,hamilton). � travel(C1,C2,L) :- travel(C1 C2 L) :- � � b C byCar(hamilton,raglan). (h ilt l ) � direct_path(C1,C2,L). byCar(valmont,saarbruecken). � byCar(valmont,metz). � travel(C1,C2,L) :- � direct_path(C1,C3,L1),travel(C p ( , , ), ( byTrain(metz,frankfurt). byTrain(metz frankfurt) � 3,C2,L2),append(L1,L2,L). byTrain(saarbruecken,frankfurt � ). direct_path(C1,C2,[C1,C2,' by � byTrain(metz,paris). � car']):- byCar(C1,C2). byTrain(saarbruecken,paris). byTrain(saarbruecken paris) � � direct path(C1 C2 [C1 C2 ' by direct_path(C1,C2,[C1,C2,' by � train']):- byTrain(C1,C2). byPlane(frankfurt,bangkok). direct_path(C1,C2,[C1,C2,' by � � plane']):- byPlane(C1,C2). byPlane(frankfurt,singapore). � byPlane(paris losAngeles) byPlane(paris,losAngeles). � � show_path([C1,C2,M| T]) :- � byPlane(bangkok,auckland). � write(C1),write(' to byPlane(losAngeles,auckland). � '),write(C2),write(M),nl,show_p ath(T).

  26. Rules � Statements about objects and their relationships � Expess � If-then conditions � I use an umbrella if there is a rain � I use an umbrella if there is a rain use(i, umbrella) :- occur(rain). � � Generalizations � All men are mortal All t l mortal(X) :- man(X). � � Definitions � An animal is a bird if it has feathers bird(X) :- animal(X), has_feather(X). �

  27. P Prolog Program Flow, l P Fl BackTracking and Cut BackTracking and Cut Controlling the program flow

  28. Prolog’s computation � Depth First Search � Pursues a goal till the end � Pursues a goal till the end � Conditional AND; falsity of any goal prevents satisfaction of further prevents satisfaction of further clauses. � Conditional OR; satisfaction of any C diti l OR ti f ti f goal prevents further clauses being e al ated evaluated.

  29. Control flow (top level) Given g:- a b c g:- a, b, c. (1) (1) g:- d, e, f; g. (2) If prolog cannot satisfy (1), control will automatically fall through to (2).

  30. Control Flow within a rule Taking (1), g:- a, b, c. g: a, b, c. If a succeeds, prolog will try to satisfy b, succeding which c will be tried. succeding which c will be tried. For ANDed clauses, control flows forward till the ‘.’, iff the current clause is true. till the . , iff the current clause is true. For ORed clauses, control flows forward till the ‘.’, iff the current clause till the . , iff the current clause evaluates to false.

  31. � REDO the immediately preceding di What happens on failure l di i h goal.

  32. Fundamental Principle of prolog p p g programming � Always place the more general rule l l h l l AFTER a specific rule.

  33. CUT � Cut tells the system that I F YOU HAVE COME THI S FAR DO NOT BACKTRACK EVEN I F YOU FAI L SUBSEQUENTLY. ‘CUT’ WRI TTEN AS ‘!’ ALWAYS SUCCEEDS.

  34. Fail � This predicate always fails. � Cut and Fail combination is used to � Cut and Fail combination is used to produce negation. � Since the LHS of the neck cannot Since the LHS of the neck cannot contain any operator, A � ~ B is implemented as implemented as B :- A, !, Fail.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend