1. Trang chủ
  2. » Giáo án - Bài giảng

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

37 56 0

Đ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

Nội dung

ne C om Ho Chi Minh City University of Technology Faculty of Computer Science and Engineering nh Vi en Zo INTRODUCTION TO PROLOG Huỳnh Tấn Đạt Si 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 Zo ne C om [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 om  PROgramming in LOGic C  Alain Colmerauer & Philippe Roussel, 1972 ne  A declarative programming language to describe Si nh Vi en Zo problems SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Prolog nh Vi en Zo ne C om  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 – 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 om Prolog program ne C Predicate … Clause nh Vi en … Zo Predicate Clause Predicate Si Predicate  A predicate always returns true or false SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Clause Zo ne C om  Two types of clause: – Fact – Rule  Clauses are separated by „.‟ character Si nh Vi en 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 ne C rich(X) :- childof(X, Y), rich(Y) rich(X) :- rober(X) om Rule nh Vi en Zo Predicate name and parameters  A rule returns true if all sub-clauses return true Si  The clauses of a predicate should be consecutive SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide Clause Zo ne C om  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 en  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 Zo ne C om  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 .C ne nh Vi en Zo  Example: man(socrates) man(xeda) king(xeda) happy(X) :- man(X), king(X) om How Prolog answers questions? Si ?- happy(xeda) ?- happy(Y) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 10 .C ne X = Y = xeda Si ?- happy(Y) Y = xeda nh Vi en Zo  Example: man(socrates) man(xeda) king(xeda) happy(X) :- man(X), king(X) om How Prolog answers questions? SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 23 Controlling backtracking ?- p(X) Si p(X) :- q(X) p(3) q(1) q(2) nh Vi en Zo ne C om  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) Si nh Vi en p(X) :- q(X), ! p(3) q(1) q(2) Zo ne C om  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 Zo ne C om  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 Zo ne C om  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 Zo ne C om  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 Zo ne C om  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 nh Vi en Zo ne C “Mary likes all animals but snakes”: like(mary, X) :- snake(X), !, fail like(mary, X) :- animal(X) om  fail is a special sug-goal, which always fails Si different(X, Y): different(X, X) :- !, fail different(X, Y) SinhVienZone.com Faculty of Computer Science and Engineering – HCMUT https://fb.com/sinhvienzonevn Slide 30 NOT Predicate ne Zo nh Vi en not(P) :- P, !, fail not(_) .C om  Many Prolog implementations support this predicate If not, we can define NOT ourselves Si  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 Zo ne C om  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 Zo ne C om  Example – If we not explicitly enter the following clause into our program human(mary) nh Vi en then Proglog will answer „no‟ for this question Si ?- 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 nh Vi en Zo ne C om  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] [The first item (head) | The remaining part of the list (tail)] Si  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 Zo ne C om  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 ne C om  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 Zo – 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 Zo ne C om  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 Zo ne C om  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 Zo ne C om  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:10

TỪ KHÓA LIÊN QUAN