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

trí tuệ nhân tạo cao hoàng trứ lab 1 prolog sinhvienzone com

9 33 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 9
Dung lượng 107,84 KB

Nội dung

TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính LAB SESSION PROLOG A MATCHING, UNIFICATION and QUERY on B-PROLOG OBJECTIVE om The objectives of part A are (1) to get acquainted with B-Prolog; (2) to understand unification and control structures; and (3) to write some first simple predicates with BProlog .C DOWNLOAD and INSTALLATION Zo ne B-Prolog is a variation in the Prolog programming language family A version of BProlog is available for download in the course site Nothing is simpler than the so-called installation of B-Prolog Copy the whole directory to the root directory of the C drive and run the bp.bat file Welcome to the world of programming logic! nh Vi en UNIFICATION & CONTROL STRUCTURE Unification A question to Prolog is always a sequence of one or more goals and Prolog tries to satisfy all the goals by finding a clause whose head matches the goal and then trying to prove the sub-clauses, if any, in the clause's body (matching clauses) The act of matching two Prolog terms is called unification and is described by the table Example: Two terms in BProlog can be unified using the “=” predicate Si | ?- abc = abc Yes | ?- abc = cba No | ?- abc = ABC ABC=abc Yes | ?- ABC = XYZ ABC=_12345 XYZ=_12345 SinhVienZone.com https://fb.com/sinhvienzonevn TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính Yes | ?- ABC = XYZ, ABC = abc, xyz =XYZ No om | ?- [H|T] = [a,b,c,d] H=a T = [b,c,d] Yes Table 1: BProlog term unification Second term Atom Number Number Structure (including name and list of parameters Ex fibonacci(X,N)) Structure (including name and list of parameters Ex fibonacci(3,5)) en Zo ne C First term Atom List nh Variable Vi List Variable Ex: X = & N = Heads unify and tails unify Binding (Variable is replaced by the nonvariable) Share bindings Si Variable Non-variable Unify Unify if they are the same Unify if they are the same value Unify if they have same name and corresponding parameters unify Control Structures Cut mechanism: Used to prevent the backtracking of prolog at the point indicated by “!” Cut is always true and acting as the gate When matching process passes over the gate (cut), it can not backtrack to the clause on the left of the gate, but the clause on the left of the clause which chooses the clause containing cut Example: In the following example, when the undo process is applied to find more solution (type “;”), BProlog backtrack on q(X), not at r(X) SinhVienZone.com https://fb.com/sinhvienzonevn TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính ne C om p(X) :- q(X),r(X) p(4) q(1) q(2) r(X):- X1 is X + 1,q(X1),! r(X):- writeln(“r”) | ?- p(X) X = ?; X = ?; X=4 Yes Vi en Zo All solutions BProlog provides us build-in predicates that find all solutions named findall(Term,Goal,List) This predicate succeeds if List is the list of instances of Term such that Goal succeeds Example: ?-findall(X,member(X,[(1,a),(2,b),(3,c)]),Xs) Xs=[(1,a),(2,b),(3,c)] nh For more information on control structures, student can read session 2.3 in BProlog User Manual Si EXPERIMENT 3.1 Write your program Here is your first program, or, predicate You can use any text editor to create it before(a,b) before(b,c) before(A,C) :- before(A,B), before(B,C) Listing – Your first program SinhVienZone.com https://fb.com/sinhvienzonevn TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính Save your “program” as a file, let’s say Lab1.pl 3.2 Compile and load Recall something you have learnt from the PPL course: in order to be executed, a program must be first compiled and then loaded to become a process In the B-Prolog window, type the following commands to have the Lab1.pl compiled and loaded: om compile(Lab1) load(Lab1) .C Do not forget the ending symbol of ‘.’ ne 3.3 Query your program Zo After loading your program, you can query upon the predicate you have written Here are some examples: Vi en before(a,b) before(b,a) before(a,d) before(a,X) Si nh In the last case, we ask B-Prolog to find the value of X to make the predicate true After B-Prolog produces the first answer, you may want to type ‘;’ to ask it to find more However, not push it so hard, or your B-Prolog may suffer an overflow error 3.5 Trace the matching process Student read “Debugging Chapter” in Prolog manual for the detail of debugging 3.4 Anonymous variable Reopen your Lab1.pl and write on additional predicate as follows notfirst(X):- before(_,X) The meaning of this predicate is comprehensible An item X is not the first item if there is another item before it The idea is we not necessarily need to know who the very item SinhVienZone.com https://fb.com/sinhvienzonevn TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính before X is As long as such an item exists, X is not the first item In that case, we use the anonymous variable, denoted by ‘_’, as observed You can test the new predicate yourselves CLASS EXERCISES C % host(X)

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