WM WM trí tuệ nhân tạo cao hoàng trứ chương ter prolog sinhvienzone com

37 45 0
WM WM trí tuệ nhân tạo cao hoàng trứ chương ter prolog sinhvienzone com

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

e co m Ho Chi Minh City University of Technology Faculty of Computer Science and Engineering Vi en Z on INTRODUCTION TO PROLOG Si nh Huỳnh Tấn Đạt Email: htdat@cse.hcmut.edu.vn Home Page: http://www.cse.hcmut.edu.vn/~htdat/ SinhVienZone.com https://fb.com/sinhvienzonevn References Si nh Vi en Z on e co m [1] Ivan Bratko (1990), Prolog Programming For Artificial Intelligence, Addition-Westley [2] Nguyễn Thanh Tùng, Nguyễn Thị Trúc Viên (2007), Thực Hành Ngơn Ngữ Lập Trình, (my web site) [3] B-Prolog – User‟s Guide SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Prolog m  PROgramming in LOGic e co  Alain Colmerauer & Philippe Roussel, 1972 on  A declarative programming language to describe Si nh Vi en Z problems SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Prolog Vi en Z on e co m  Irrefutable reasoning processes (Aristotle): – Socrates is a man man(socrates) – All men are mortal mortal(X) :- man(X) => Socrates is mortal robber(jerry) childof(tom, john) rich(john) rich(X) :- childof(X, Y), rich(Y) rich(X) :- robber(X) Si nh – Jerry is a robber – Tom is a child of John – John is rich – X is rich if X‟s father is rich or X is a robber SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Predicate Predicate m Prolog program e co Clause Predicate Si nh Vi … Predicate Clause en Z on Predicate … Predicate  A predicate always returns true or false SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Clause en Z on e co m  Two types of clause: – Fact – Rule  Clauses are separated by „.‟ character Si nh Vi robber(jerry) childof(tom, john) rich(john) rich(X) :- childof(X, Y), rich(Y) rich(X) :- rober(X) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT Facts Rules https://fb.com/sinhvienzonevn Slide sub-clauses on e co rich(X) :- childof(X, Y), rich(Y) rich(X) :- rober(X) m Rule en Z Predicate name and parameters Vi  A rule returns true if all sub-clauses return true Si nh  The clauses of a predicate should be consecutive SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Clause en Z on e co m  Literals, or atoms, start with lower-case letters, or enclose in quotes „ „  Whereas variables start with Upper-case letters man(socrates) mortal(X) :- man(X) Si nh Vi  The arguments in sub-clauses must be variables, not expressions  Example: factorial(N – 1, R1) % Wrong!!! factorial(0, 1) factorial(N, R1) :- N1 is N – 1, factorial(N1, R1), R is R1 * N SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide How Prolog answers questions? Si nh Vi en Z on e co m  A question to Prolog is always a sequence of one or more goals  Prolog tries to satisfy all the goals by matching clauses  Create the associations (unifications) between arguments in the question and parameters declared in the head of clauses  Execute the sub-clauses of the current clauses  If all sub-clauses are satisfied, the variables in question will be bound to values => We have got a solution SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide e co Si nh ?- happy(xeda) ?- happy(Y) Vi en Z on  Example: man(socrates) man(xeda) king(xeda) happy(X) :- man(X), king(X) m How Prolog answers questions? SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 10 ?- happy(Y) Y = xeda e co X = Y = xeda Si nh Vi en Z on  Example: man(socrates) man(xeda) king(xeda) happy(X) :- man(X), king(X) m How Prolog answers questions? SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 23 Controlling backtracking Vi ?- p(X) Si nh p(X) :- q(X) p(3) q(1) q(2) en Z on e co m  Prolog automatically backtrack if this is necessary for satisfying a goal  The results may be different if we change the order of clauses of a predicate  Example: SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 24 Controlling backtracking p(X) :- q(X), ! p(3) q(2) q(1) Vi Si nh p(X) :- q(X), ! p(3) q(1) q(2) en Z on e co m  Uncontrolled backtracking may cause inefficiency in a program  Cut mechanism is used to prevent the backtracking of prolog at the point indicated by „CUT‟ p(3) p(X) :- q(X), ! q(1) q(2) ?- p(X) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 25 Controlling backtracking Si nh Vi en Z on e co m  Example: – If X < then Y = f(X, 0) :- X < f(X, 2) :- =< X, X < – If Prolog uses backtracking mechanism to try two useless alternatives (rule no and 3) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 26 Controlling backtracking Si nh Vi en Z on e co m  The rules about f relation are mutually exclusive, as soon as one rule succeeds, we not need to try the other rules ?- f(1, Y), < Y f(X, 0) :- X < 3, ! Rules no and are not matched f(X, 2) :- =< X, X < 6, ! f(X, 4) :- =< X  The third version of the program: f(X, 0) :- X < 3, ! f(X, 2) :- X < 6, ! f(X, 4) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 27 Problems with CUT Si nh Vi en Z on e co m  Advantages – We can improve the efficiency of the program – We can specify „mutually exclusive rules p:- c, !, s1 p:- s2  Disadvantages – We can lose the valuable correspondence between the declarative and procedural meaning of programs – A change in the order of clauses may affect the declarative meaning => We can get different results SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 28 Problems with CUT Si nh Vi en Z on e co m  Example p :- a, b % p :- a, b ; c p  (a and b) or c p :- c Insert a CUT: p :- a, !, b p  (a and b) or (not a and c) p :- c If we swap the clauses: p :- c p  c or (a and b) p :- a, !, b  When we use the CUT facility we have to pay more attention to the procedural aspects SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 29 Fail e co m  fail is a special sug-goal, which always fails Si nh Vi different(X, Y): different(X, X) :- !, fail different(X, Y) en Z on “Mary likes all animals but snakes”: like(mary, X) :- snake(X), !, fail like(mary, X) :- animal(X) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 30 NOT Predicate e co m  Many Prolog implementations support this predicate If not, we can define NOT ourselves en Z on not(P) :- P, !, fail not(_) Si nh Vi  The two examples can be rewritten with NOT as: likes(mary, X) :- animal(X), not(snake(X)) different(X, Y) :- not(X == Y) % (X =\= Y) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 31 Closed World Assumption Si nh Vi en Z on e co m  When processing a not(P) goal, Prolog does not try to prove this goal directly Instead, it tries to prove the opposite And if the opposite cannot be proved then Prolog assumes that the not(P) goal succeeds  Such reasoning is based on the so-called closed world assumption  The world is closed in the sense that everything that exists is in the program or can be derived from a program  If something is not in the program (or cannot be derived from it) then it is not true and consequently its negation is true SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 32 Closed World Assumption en Z on e co m  Example – If we not explicitly enter the following clause into our program human(mary) Vi then Proglog will answer „no‟ for this question Si nh ?- human(mary) no There is not enough information in the program to prove that Mary is human SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 33 List en Z on e co m  A list is sequence of any number of items – Example: [ann, tennis, tom, skiing]  An empty list: [ ]  An non-empty list: [ann, tennis, tom, skiing] Vi [The first item (head) | The remaining part of the list (tail)] Si nh  Prolog uses „|‟ to separate the head and tail of a list  Tail itself is a list  The elements of a list can be objects of any kind They can be lists SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 34 List Si nh Vi en Z on e co m  Prolog provides notations for lists: – [Item1, Item2, ] – [Head | Tail] – [Item1, Item2, | Others] SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 35 Example of Operations on List on e co m  Length of a list: – length(L, R): R is the length of the list L length([ ], 0) length([_ | T], R) :- length(T, R1), R is R1 + Si nh Vi en Z – Anonymous variable: _  Membership: – member(X, L): return true if X occurs in L – Example: member(b, [a, b, c]) => true member(d, [a, b, c]) => false member(H, [H | _]):- ! member(H, [_ | Tail]) :- member(H, Tail) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 36 Exercises Si nh Vi en Z on e co m  Get the first element of a list L  Insert an element into the first position of a list L  Get the nth element of a list L  Append two lists L1 and L2 into L3  Write count1(L, R) predicate where L is a list of integers and the output R is the number of positive numbers in L – Example: count1([1, 2, -1, 3, 2], R) => R =  Write count2(LL, R) predicate where LL is 2-dimentional list of integers and the output R is the number of positive numbers in LL – Example: count2([1, 2, -1, 3, 2], [ ], [2]], R) => R = SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 37 ... https://fb.com/sinhvienzonevn Slide Clause en Z on e co m  Literals, or atoms, start with lower-case letters, or enclose in quotes „ „  Whereas variables start with Upper-case letters man(socrates) mortal(X) :- man(X)... SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide How Prolog answers questions? Si nh Vi en Z on e co m  A question to Prolog is always a sequence... Y => Y = and the sub-goal < fails => Prolog uses backtracking mechanism to try two useless alternatives (rule no and 3) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn

Ngày đăng: 30/01/2020, 23:11

Tài liệu cùng người dùng

Tài liệu liên quan