1. Trang chủ
  2. » Cao đẳng - Đại học

Bài tập trí tuệ nhân tạo prolog

9 63 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 109,78 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 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 DOWNLOAD and INSTALLATION 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! 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 | ?- abc = abc Yes | ?- abc = cba No | ?- abc = ABC ABC=abc Yes | ?- ABC = XYZ ABC=_12345 XYZ=_12345 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 | ?- [H|T] = [a,b,c,d] H=a T = [b,c,d] Yes Table 1: BProlog term unification First term Atom 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)) List List Variable Non-variable Variable 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 Ex: X = & N = Heads unify and tails unify Binding (Variable is replaced by the nonvariable) Share bindings 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) CuuDuongThanCong.com https://fb.com/tailieudientucntt TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 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 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)] For more information on control structures, student can read session 2.3 in BProlog User Manual 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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: compile(Lab1) load(Lab1) Do not forget the ending symbol of ‘.’ 3.3 Query your program After loading your program, you can query upon the predicate you have written Here are some examples: before(a,b) before(b,a) before(a,d) before(a,X) 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 CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 Consider the World_Cup.pl file This file defines a database of facts of the follow in format (Note that anything following '%' is treated as a comment in Prolog): % host(X)

Ngày đăng: 15/12/2021, 10:52

TỪ KHÓA LIÊN QUAN

w