If412 09

573 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
573
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

If412 09

  1. 1. IF412 – Konsep Bahasa Pemrograman Pertemuan : 09 KONTROL PADA PROLOG • Algoritma = logic + control • Control dalam Prolog ada dua : o Goal order : pilih sub goal paling kiri o Rule order : pilih rule pertama yang dapat diterapkan • Algoritma untuk mencapai goal : current goal (sesuai dengan query) while current goal ≠ 0 do pilih goal paling kiri if ada rule yang memenuhi pilih rule pertama yang me menuhi bentuk goal baru else backtrack end if end while Contoh : append ([], Y, Y). append ([H|X], Y, [H|Z]) :- append(X, Y, Z). prefix(X, Z) :- append(X, Y, Z). suffix(Y,Z) :- append (X, Y, Z). appen2(H|X], Y, [H|Z]) :- appen2 (X, Y, Z). appen2([], Y, Y). • Pengaruh susunan goal ?- prefix(X,[a,b,c]), suffix([e],X). no ?- suffix([e],X), prefix(X,[a,b,c]). [infinite computation ] • Pengaruh susunan Rule ?- append (X,[c], Z). X = [] Z = [c]; X = [_1] Z = [_1,c] X = [_1,_2] Z = [_1,_2,c] Yes ?- appen2(X, [c], Z). [ infinite computation ] BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  2. 2. IF412 – Konsep Bahasa Pemrograman Pertemuan : 09 • Perbaikan algoritma goal start dengan query sebagai current goal while current goal ≠ 0 do current goal G1, G2, G3 ........,Gk pilih goal paling kiri G1 if rule berlaku untuk G1 pilih rule A :- B1, ......, Bj j ≥ 0 τ ← unifier G1 dan A current goal ← B1τ, B2τ, ...., Bjτ, G2τ, ......Gkτ else backtrack end if end while • Contoh : ?- suffix([a], L), prefix(L,[a, b, c]). L = [a] Query di atas dihitung tanpa backtracking, caranya : suffix([a], L), prefix(L, [a, b, c]) suffix([a], L) if append (_1,[a], L). append(_1, [a], L), prefix(L, [a, b, c]) {_1 → [], L → [a]} append([], [a], [a]) prefix([a], [a, b, c]) prefix([a],[a, b, c,]) if append([a], _2.[a, b, c]). append([a],_2,[a,b,c]) append([a],_2,[a,b,c]) if append([],_2,[b,c]). append([],_2,[b,c]) {_2 → [b,c]} append([],[b,c],[b,c]). yes Prolog Search Tree • Perhatikan query untuk search tree : BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  3. 3. IF412 – Konsep Bahasa Pemrograman Pertemuan : 09 ?- suffix([b], L), prefix(L, [a, b, c]). L = [a, b] suffix([b], L), prefix(L, [a, b, c]) append(_1, [b], L), prefix(L, [a, b, c]) {_1→ [ ], L → [b]} {_1 → [_3|_4], L → [_3||_5]} prefix([b], [a, b, c]) append([b],_2,[a, b, c]) backtrack append(_4,[b],_5), prefix([_3|_5], [a,b,c]) {_4 → [ ], _5 → [b]} prefix([_3, b], [a, b, c]) ..... append([_3,b],_6,[a, b, c]) { _3 → a} append([b], _6, [b, c] ) append( [], _6, [c] ) {_6 → [c]} yes Susunan Goal Mempengaruhi Solusi ?- suffix ([a], L), prefix(L,[a, b, c]). L=[a] [ infinite computation ] ?- prefix(L,[a, b, c]), suffix([a], L) L = [a] no prefix(L, [a, b, c]), suffix([a], L) append(L, _1, [a, b, c]), suffix([a], L]) {L → [ ], _1 → [a, b, c]} suffix([a], [ ]) {L → [a | _3]} append(_2, [a], [ ]) BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  4. 4. IF412 – Konsep Bahasa Pemrograman Pertemuan : 09 backtrack append(_3, _1, [b, c]), suffix([a], [a|_3]) {_3 → [ ], _1 → [b, c]} suffix([a], [a]) ..... append(_4, [a], [a]) { _3 → [ ]} yes Effect of changing goal order. Susunan Rule Mempengaruhi Searching • Contoh : append( [ ],Y, Y). append([H|X], Y, [H|Z]) :- append(X, Y, Z). appen2([H|X], Y, [H|Z]) :- appen2(X, Y, Z). appen2( [ ],Y, Y). append(X, [c], Z) appen2(X, [c], Z) {X → [ ], Z → [c]} {X → [ ], Z → [c]} yes {X→ [_1 | _2 |, {X→ [_1 | _2 |, yes X=[ ], Z=[c] Z → [_1 | _3]} Z → [_1 | _3]} X=[ ], Z=[c] append(_2, [c], _3) append(_2, [c], _3) {_2→[ ], _3→[c]} {_2→[ ], _3→[c]} yes yes X=[_1], Z=[_1, c] X=[_1], Z=[_1, c] ... ... Rule order determines the order of the children of a node CUT • Bentuk umum : B :- C1, C2, ......,Cj-1, ! , Cj, Cj+1......,Cn Yang dilakukan : BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  5. 5. IF412 – Konsep Bahasa Pemrograman Pertemuan : 09 a. Jawaban yang OK sampai dengan Cj-1 b. backtrack hanya boleh pada Cj, Cj+1, ....,Cn c. rule lain dari B jangan diperhatikan lagi Contoh: b b:- c b:- d b:- e c d e b b:- c b:- ! d Cut b:- e c d e BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998

×