Presentation is loading. Please wait.

Presentation is loading. Please wait.

KU NLP Artificial Intelligence1 Ch 14. An Introduction to Prolog q an Implementation of logic as a programming language(PROgramming in LOGig) q many interesting.

Similar presentations


Presentation on theme: "KU NLP Artificial Intelligence1 Ch 14. An Introduction to Prolog q an Implementation of logic as a programming language(PROgramming in LOGig) q many interesting."— Presentation transcript:

1

2 KU NLP Artificial Intelligence1 Ch 14. An Introduction to Prolog q an Implementation of logic as a programming language(PROgramming in LOGig) q many interesting contributions to AI problem solving  declarative semantics  built-in unification  high-powered techniques for pattern matching and search

3 KU NLP Artificial Intelligence2 14.1 Syntax for Predicate Calculus Programming q 14.1.1 Representing Facts and Rules q 14.1.2 Creating, Changing, and Monitoring the PROLOG Environment q 14.1.3 Lists and Recursion in PROLOG q 14.1.4 Recursive Search in PROLOG q 14.1.5 The Use of Cut

4 KU NLP Artificial Intelligence3 14.1.1 Representing Facts and Rules(1) q English Predicate Calculus PROLOG and , or  ; only if  :- not  not q “Everyone likes Susie.”  likes(X, susie).orlikes(Everyone, susie). q The set of people that are liked by George AND Susie  likes(george, Y), likes(susie, Y). q “George likes Kate OR George likes Susie.”  likes(george, kate); likes(george, susie).

5 KU NLP Artificial Intelligence4 14.1.1 Representing Facts and Rules(2) q “George likes Susie if George likes Kate.”  likes(george, susie) :- likes(george, kate). q “Kate does not like Susie.”  not(likes(kate, susie)). q A Model for the database  Database - a set of specifications describing the objects and relations in a problem domain  Queries to the database are patterns in the same logical syntax as the database entries.  Prolog interpreter uses pattern-directed search to find whether these queries logically follow from the contents of the database.

6 KU NLP Artificial Intelligence5 14.1.1 Representing Facts and Rules(3) q Prolog exmaple likes(george, kate).likes(susie, wine). likes(george, susie).likes(kate, gin). likes(george, wine).likes(kate, susie). ?- likes(george, kate). yes ?- likes(kate, susie). yes ?- likes(george, X). X = kate ; X = susie ; X = wine ; no ?- likes(george, beer). no

7 KU NLP Artificial Intelligence6 14.1.1 Representing Facts and Rules(4)  Prolog interpertations  The prompts with (;) forces a backtrack on the most recent result. Continued prompts force Prolog to find all possible solutions to the query  Closed world assumption or Negations as failure  assumes that “anything is false if it is not known to be true.”  Horn clause logic  the left hand side(conclusion) of an implication must be a single positive literal.  An example of a rule friends(X, Y) :- likes(X, Z), likes(Y, Z). ?- friends(george, susie). yes  Prolog doesn’t have global variables, the scope of X, Y, and Z is limited to friends rule.  Values bound to, X, Y, Z are consistent across the entire expression.

8 KU NLP Artificial Intelligence7 14.1.2 Creating, Changing, and Monitoring(1) q assert - add a new predicate to the current DB.  ?- assert(likes(david, sarah)).  asserta(p) asserts the predicate P at the beginning.  assertz(p) adds p at the end. q retract(p) remove p from the DB q place the file in the DB  ?- consult(myfile)  ?- [myfile] q read(X) takes the next term from the current input stream and binds it to X. q write(X) puts X in the output stream.

9 KU NLP Artificial Intelligence8 14.1.2 Creating, Changing, and Monitoring(2) q see(X) opens the file X and defines the current input stream as originating in X. q tell(X) opens a file for the output stream. q seen(X) and told(X) close the files q listing(predicate_name) - all the clauses with that predicate name in the DB are returned. q trace allows the user to monitor the progress of the Prolog interpreter. q when a more selective trace is required, the goal spy is useful.

10 KU NLP Artificial Intelligence9 14.1.3 Lists and Recursion (1)  Recursion is the primary control mechanism for PROLOG programming  Recursion is the natural way to process the list structure  List is a data structure consisting of ordered sets of elements.  List elements are enclosed by [] and separated by commas. [tom,dick,harry,fred]  Using the vertical bar operator and unification, we can break a list into its components  [tom,dick,harry,fred][X | Y]  X=tom and Y=[dick,harry,fred]  [tom,dick,harry,fred][X,Y | Z]  X=tom, Y=dick, Z=[harry, fred]

11 KU NLP Artificial Intelligence10 14.1.3 Lists and Recursion (2) q Definition of member 1: member(X, [X|T]). 2: member(X, [Y|T]) :- member(X,T). ?- member(c, [a,b,c]). call 1. fail, since c  a call 2. X=c, Y=a, T=[b,c], member(c,[b,c])? call 1. fail, since c  b call 2. X=c, Y=b, T=[c], member(c,[c])? call 1. success, c=c yes (to second call 2.) yes(to first call 2.) yes

12 KU NLP Artificial Intelligence11 14.1.3 Lists and Recursion (3) q Anonymous variables - variables used solely for pattern-matching process  member(X,[X|_]) the content of the tail of the list is unimportant. q Writing one element of the list on each line. Writelist([]). writelist([H|T]) :- write(H), nl, writelist(T). q Writing out a list in reversed order reverse - writelist([]). reverse - writelist([H|T]) :- reverse_writelist(T), write(H), nl.

13 KU NLP Artificial Intelligence12 14.1.4 Recursive Search in PROLOG(1) q Knight’s tour problem  a knight can move two squares either horizontally or vertically followed by one square in an orthogonal direction as long as it does not move off the board.  3*3 chessboard with move rules move(1,6) move(3,4) move(6,7) move(8,3) move(1,8) move(3,8) move(6,1) move(8,1) move(2,7) move(4,3) move(7,6) move(9,4) move(2,9) move(4,9) move(7,2) move(9,2)  attempt to find a series of legal moves in which the knight lands on each square of the chessboard exactly once. 1 2 3 4 5 6 7 8 9

14 KU NLP Artificial Intelligence13 14.1.4 Recursive Search in PROLOG(2) q Definition of Path using PROLOG path(Z,Z) path(X,Y) :- move(X,W), not(been(W)),assert(been(W)),path(W,Y).  To determine whether the desired goal state is over reached (termination condition of recursion), path(Z,Z) is needed.  been predicate is used to record previously visited states and avoid loops.

15 KU NLP Artificial Intelligence14 14.1.4 Recursive Search in PROLOG(3) q Depth-first graph search with backtracking algorithm of Path. 1. path (Z, Z, L). 2. path (X, Y, L) :- move(X,Z), not(member(Z,L)), path(Z,Y,[Z|L]). ?-path(1,3,[1]). path(1,3,[1]) attempts to match 1. Fail 1  3. path(1,3,[1]) matches 2. X is 1, Y is 3, L is [1] move(1,Z) matches Z as 6, not(member(6,[1])) is true, call path(6,3[6,1]) path(6,3,[6,1]) attempts to match 1. Fail 6  3. path(6,3,[6,1]) matches 2. X is 6, Y is 3, L is [6,1]. move(6,Z) matches Z as 7, not(member(7,[6,1])) is true, path(7,3,[7,6,1])

16 KU NLP Artificial Intelligence15 14.1.4 Recursive Search in PROLOG(4) path(7,3,[7,6,1]) attempts to match 1. Fail 7  3. path(7,3,[7,6,1]) matches 2. X is 7, Y is 3, L is [7,6,1]. move(7,Z) is Z=6, not(member(6,[7,6,1])) fails, backtrack! move(7,Z) is Z=2, not(member(2,[7,6,1])) true, path(2,3,[2,7,6,1]) path call attempts 1, fail, 2  3 path matches 2, X is 2, Y is 3, L is [2,7,6,1] move matches Z as 7, not(member(…)) fails, backtrack! move matches Z as 9, not(member(…)) fails, path(9,3,[9,2,7,6,1]) path fails 1, 9  3 path matches 2, X is 9, Y is 3, L is [9,2,7,6,1] move is Z=4, not(member(…)) true, path(4,3,[4,9,2,7,6,1]) path fails 1, 4  3 path matches 2, X is 4, Y is 3, L is [4,9,2,7,6,1] move Z=3, not(member(…)) true, path(3,3,[3,4,9,2,7,6,1]) path attempts 1, true, 3=3, yes

17 KU NLP Artificial Intelligence16 14.1.5 The use of Cut to Control Search in PROLOG(1)  Cut is represented by !  Side effects  when originally encountered it always succeeds.  If it is “failed back to” in the normal course of backtracking, it comes the entire goal to fail.  Example path2(X,Y):- move(X,Z), move(Z,Y). move(1,6). move(1,8). move(6,7). move(6,1). move(8,3). move(8,1). ?-path2(1,W). W=7; W=1; W=3; W=1; no path3(X,Y):-move(X,Z), !, move(Z,Y). ?-path3(1,W) W=7; W=1; no

18 KU NLP Artificial Intelligence17 14.1.5 The use of Cut to Control Search in PROLOG(2) q Usage of cut in programming  allows the programmer to control the shape of the search tree, when further search is not required, the tree can be explicitly pruned at that point  Controls recursion path(Z,Z,L) path(X,Z,L):- move(X,Y), not(member(Y,L)), path(Y,Z,[Y|L]),!. the addition of cut means that at most one solution is produced.

19 KU NLP Artificial Intelligence18 The farmer, wolf, goat and cabbage Problem (P 620)  A farmer with his wolf, goat, and cabbage come to the edge of a river they wish to cross.  There is a boat at the river’s edge, but of course, only the farmer can row.  The boat also can carry only two things (including the rower) at a time.  If the wolf is ever left alone with the goat, the wolf will eat the goat;  Similarly, if the goat is left alone with the cabbage, the goat will eat the cabbage.  Devise a sequence of crossings of the river so that all four characters arrive safely on the other side of the river.

20 KU NLP Artificial Intelligence19 Sample crossing for FWGC problem

21 KU NLP Artificial Intelligence20 State Space Representation of FWGC problem

22 KU NLP Artificial Intelligence21 FWGC problem in PROLOG q test:- go(state(w,w,w,w), state(e,e,e,e)).  go(Start, Goal) :- empty_stack(Empty_been_stack), stack(Start, Empty_been_stack, Been_stack) path(Start, Goal, Been_stack).  path(State, Goal, Been_stack) :- ……..  move(state(….), state(….)) :- ……..  unsafe, opp, empty_stack, stack, member_stack, reverse_print_stack, write_list, etc.

23 KU NLP Artificial Intelligence22 A parse Tree

24 KU NLP Artificial Intelligence23 A parser in PROLOG utterance(X) :- sentence(X, [ ]). sentence(Start, End) :- nounphrase(Start, Rest), verbphrase(Rest, End). nounphrase([Noun|End], End) :- noun(Noun). nounphrase([Article, Noun|End], End) :- article(Article), noun(Noun). verbphrase([Verb|End], End) :- verb(Verb). verbphrase([Verb|End], End) :- verb(Verb), nounphrase(Rest, End). article(a). article(the). noun(man). noun(dog). verb(likes). verb(bites).

25 KU NLP Artificial Intelligence24 ELIZA Person: Men are all alike. ELIZA: In what way? Person: They’re always bugging us about something or other. ELIZA: Can you think of a specific example? Person: Well, my boyfriend made me come here. ELIZA: Your boyfriend made you come here. Person: He says I’m depressed much of the time. ELIZA: I am sorry to hear you are depressed. Person: It’s true. I am unhappy. ELIZA: Do you think coming here will help you not to be unhappy? Person: I need some help, that much seems certain. ELIZA: What would it mean to you if you got some help? Person: Perhaps I could learn to get along with my mother. ELIZA: Tell me more about your family.  (X you Y)  (Why do remember X just now?)  (Who else in your family is Y?  (Tell me more about your family) (X me Y) (I remember Y) (My {family-member} is Y) (X {family-member} Y)

26 KU NLP Artificial Intelligence25 Prolog Homework q FWCG problem (exercise, p623) q Eliza (exercise) q A parser in PROLOG  Read 14.9.2 (p 667) recursive parser  Write a grammar for parsing sentences in the own dialog  Produce a parse tree as follows:  ?- utterance([The, man, bites, the, dog]). [sentence, [nounphrase, [article, the], [noun, man]], [verbphrase, [verb, bites], [nounphrase, [article, the], [noun, dog]]]].


Download ppt "KU NLP Artificial Intelligence1 Ch 14. An Introduction to Prolog q an Implementation of logic as a programming language(PROgramming in LOGig) q many interesting."

Similar presentations


Ads by Google