המצגת נטענת. אנא המתן

המצגת נטענת. אנא המתן

Logic Programming: Introduction

מצגות קשורות


מצגת בנושא: "Logic Programming: Introduction"— תמליל מצגת:

1 Logic Programming: Introduction
A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). A logic program: a set of procedures (predicates), defining relations in the program domain. arity procedure predicate % Signature: parent (Parent, Child) /2 % Purpose: Parent is a parent of Child parent (erik, jonas). parent (lena, jonas). % Signature: male(Person) /1 % Purpose: Person is a male male (erik). % Signature: father (Dad, Child) /2 % Purpose: Dad is father of Child father (Dad, Child) :- parent(Dad, Child) , male (Dad). program fact axioms rule אקסיומות: עובדות וכללים. פרוצדורה (פרדיקט): קבוצה סדורה של אקסיומות בעלות חתימה זהה, הכוללת את שם הפרדיקט ואת מספר הפרמטרים (arity). תכנית: קבוצה לא סדורה של פרוצדורות (פרדיקטים), המגדירות יחסים בתחום ההגדרה של התוכנית. האינטרפרטר של פרולוג עובד בלולאת read-eval-print. בהינתן שאילתה, הוא מנסה להוכיח אותה על סמך התוכנית: אם נכשל, הוא עונה false. אם הצליח והשאילתה אינה כוללת משתנים, הוא עונה true. אם הצליח והשאילתה כוללת משתנים, הוא נותן את כל ההצבות האפשריות למשתנים שבשאילתה אשר נמצאו בבניית ההוכחה. if and The relation father holds between Dad and Child parent holds Dad is male

2 Logic Programming: Introduction
The prolog interpreter operates in a read-eval-print loop. Given a query, it attempts to prove it based on the program: If it fails, it answers false. Else, if the query has no variables, it answers true. Else, it outputs all possible variables assignments found during proof process. % Signature: parent (Parent, Child) /2 % Purpose: Parent is a parent of Child parent (erik, jonas). parent (lena, jonas). % Signature: male(Person) /1 % Purpose: Person is a male male (erik). % Signature: father (Dad, Child) /2 % Purpose: Dad is father of Child father (Dad, Child) :- parent(Dad, Child), male (Dad). ? - father (X,Y). X=erik, Y=jonas ? - parent (X,jonas). X=erik ; X=lena query Next possible assignment בשפות תכנות מוכרות אנו רגילים לרעיון לפיו פרוצדורה מחזירה ערך. בתכנות לוגי, הפעלת התכנית נעשית על ידי שאילתות המשתמשות במערכת האקסיומות שהוגדרה. כאן, לא "חוזר ערך", אלא מתקבלים כתשובות (תוך תהליך הוכחה) הסימבולים שיש להציב במקום משתנים בשאילתא על מנת שערכה יהיה true. בהיעדר משתנים, התשובות האפשריות הן true או false.

3 Logic Programming: Example 1 – logic circuits
An electronic logic circuit combines: Electric component: resistor, transistor, connect between different connection points. Connection points: power, ground, or a point connecting one logic gate to another. A program that models electric logic circuits: Connection points: are individual constants. Electric components: are relations on constants. Connection point Resistor (symmetric) Ground Transistor (asymmetric) Power N1 N2 N3 N4 N5 רכיב חשמלי הוא התקן במערכת שמחבר מספר נקודות חיבור. נקודת חיבור היא נקודה שניתן לחבר אותה אל רכיב חשמלי: n1,…,n5, power, ground. רזיסטור הוא רכיב בעל שני חיבורים. הוא סימטרי ולכן אין משמעות לסדר החיבורים. טרנזיסטור הוא רכיב בעל שלושה חיבורים, ישנה חשיבות לסדר החיבורים: ימין, למטה, למעלה. מעגל לוגי מממש פונקציה לוגית מורכבת יותר בהתבסס על רכיבים חשמליים. התרשים מימין מתאר כיצד נמדל את המעגל באמצעות יחסים בתכנות לוגי: את נקודות החיבור נייצג באמצעות terms (קבועים ומשתנים) בתוכנית. את הרכיבים החשמליים נייצג באמצעות יחסים בתוכנית המוגדרים על אותם terms.

4 Logic Programming: Example 1 – logic circuits
An electronic logic circuit combines: Electric component: resistor, transistor, connect between different connection points. Connection points: power, ground, or a point connecting one logic gate to another. A program that models electric logic circuits: Connection points: are individual constants. Electric components: are relations on constants. % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1 transistor(n2,ground,n1). 2 transistor(n3,n4,n2). 3 transistor(n5,ground,n4). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2 resistor(power, n2). 3 resistor(n1, power). 4 resistor(n2, power). Note: In contrast to resistor, the order of arguments in transistor is important. Each has a different role. 

5 Logic Programming: Example 1 – logic circuits
% Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2 resistor(power, n2). 3 resistor(n1, power). 4 resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose End1,End2 1 resistor(power, n1). 2 resistor(power, n2). 3 resistor(n1, power). Reminders… A procedure begins with a contract. Constants start with lowercase characters. A predicate name is also a constant, which defines a relation between its arguments. variables start with uppercase characters (‘_’ for wildcard). Atomic formulas are either true, false or of the form predicate(t1 , ..., tn), ti is a term. A rule is a formula defining a relation that depends on certain conditions. A fact is an atomic formula which is unconditionally true. terms Constants – individual symbols Predicate symbol – predicate name Variables – variable symbols

6 Logic Programming: Example 1 – logic circuits
% Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2 resistor(power, n2). 3 resistor(n1, power). 4 resistor(n2, power). % Purpose End1,End2 Reminders… A query is a sequence of atomic formulas: ?- resistor(power, n1),resistor(n2, power). true; false No more answers המשתנים בשאילתה מכומתים על ידי "קיים" (existentially quantified). תזכורת: אין כאן פרוצדורה שמחזירה ערך במובן המוכר. האינטרפרטר מנסה להוכיח את השאילתא ומוצא את הערכים המתאימים שיש להציב במשתני השאילתא כדי שהיא תתקיים. עם זאת, אפשר להתייחס לערכים שנמצאים תוך תהליך ההוכחה כפלט ואז, רעיונית, לחשוב על השאילתא כהפעלת פרוצדורה. “Does an X exist such that resistor holds for (power, X)?” ?- resistor(power, X). X = n1 ; X = n2; false Is there another answer?

7 Logic Programming: Example 1 – logic circuits
Combining logic gates to create a NOT logic circuit: The resistor and transistor relations are based on facts. They can be combined by a rule to determine whether the not_circuit relation stands for some Input and Output. not circuit power ground resistor Output transistor Input תיארנו את היחסים resistor ו-transistor על ידי אוסף של עובדות. מעגל not מתקיים כאשר resistor ו-transistor משולבים יחד באופן מסוים. כדי לאפשר שאילתות הבודקות היכן מתקיים מעגל not במעגל החשמלי המקורי, נוכל להשתמש בכלל. הערה: n2 היא נקודת הקלט למעגל ה – not. n1 היא נקודת הפלט.

8 Logic Programming: Example 1 – logic circuits
Combining logic gates to create a NOT logic circuit: % Signature: not_circuit(Input, Output)/2 % Purpose: not logic circuit. 1. not_circuit(Input, Output) :- transistor(Input, ground, Output) , resistor(power, Output). Rule head: an atomic formula with variables Rule body power ground resistor not circuit ?- not_circuit(X,Y). X=n2, Y=n1; false Output transistor Input המשתנים בראש הכלל מכומתים על ידי "לכל" (universally quantified). ניתן להתייחס למשתנים המופיעים רק בגוף הכלל כמשתנים המכומתים על ידי "קיים" (existentially quantified). אפשר להתייחס לעובדה כמקרה פרטי של כלל: ראש הכלל הוא העובדה וגוף הכלל הוא הנוסחה האטומית true. כיוון שגוף הכלל תמיד נכון, true, לא כותבים אותו. Read: “For all Input and Output, not_circuit holds if: transistor holds for (Input, ground, Output) and resistor holds for (power, Output).”

9 Logic Programming: Example 1 – logic circuits
Combining logic gates to create a NAND logic circuit: % Signature: nand_circuit(Input1, Input2, Output)/3 % Purpose: nand logic circuit nand_circuit(Input1, Input2, Output) :- transistor(Input1, X, Output), transistor(Input2, ground, X), resistor(power, Output). ?- not_circuit(X, Y), nand_circuit(In1, In2, X). X = n2, Y = n1, In1 = n3, In2 = n5; false Connection point Ground Power N1 N2 N3 N4 N5 nand circuit הערה: תחום ההגדרה של המשתנה X הוא גוף הכלל בלבד. בתוך גוף הכלל הוא משתמש כדי לקשור בין אותה נקודת חיבור הנושאת תפקיד שונה בשני טרנזיסטורים.

10 Semantics: Unification algorithm
A program execution is triggered by a query in attempt to prove its goals: To find a possible proof, the answer-query algorithm is used. It makes multiple attempts to apply rules on a selected goal. This is done by applying a unification algorithm, Unify, to the rule head and the goal. The operational semantics is based on two mechanisms: (1) Unification: deals with making atoms identical. Performed by substituting variables by expressions. (2) Search and backtracking.

11 Semantics: Unification algorithm
Definitions: binding: a non-circular expression, X=t, where X is a variable and t is a term not including X. Substitution: a function from a finite set of variables to a finite set of terms (or bindings). Application of a sub. S to an atomic formula A, replaces vars in A with corresponding terms in S: S = {I=X} A = not_circuit(I, I) , A º S = not_circuit(X, X) B = not_circuit(X,Y) , B º S = not_circuit(X, Y) the result is an instance of A Unifier: a substitution S is called a unifier of formulas A and B if A º S = B º S. For example: S = {I=X} º {X=Y} = {I=Y, X=Y} A º S = not_circuit(Y, Y) B º S = not_circuit(Y, Y) תזכורת: האות הראשונה של משתנה היא אות גדולה ושל קבוע - קטנה. term הוא קבוע או משתנה. משתנה המופיע בצד שמאל של binding אינו יכול להופיע בצד ימין של ה-binding. substitution היא פונקציה ולכן כל משתנה ממופה לערך יחיד. כל משתנה מופיע פעם אחת בלבד:{X=3, Z=2, X=4} אינה חוקית. unifier כללי ביותר פירושו שכל Unifier אחר יכול להתקבל מהרכבה של הכללי ביותר עם הצבה כלשהיא. נשים לב: אין משמעות לערך של ביטוי מספרי בקוד. 5 הוא סמל (שם, בדיוק כמו erik) ולא מספר. Unify receives two atomic formulas and returns their most general unifier S = {I=5, X=5, Y=5} is a unifier for A and B, but not the most general one.

12 Semantics: Proof trees
Executing answer-query: The interpreter searches for a proof for a given query, a conjunction of goals. The search is done by building and traversing a proof tree where all possibilities are examined. The possible outcome is one of the following: The algorithm finishes, and possible values of the query variables are given. The algorithm finishes, but there is no proof for the query (false). The proof attempt loops and never ends.

13 Semantics: Proof trees
The tree structure depends on Prolog's goal selection and rule selection policies: Query goals (G1,…,Gn) are at the root of the proof tree. Gsel: select current goal (leftmost order). Rename: the variables in every rule and fact in the program. Rsel: get all rules R such that head(R) can be unified with the goal (program order). Whenever unification succeeds: A new child node is created. The goal is replaced with body(R). A leaf may be created if the goal list is empty (success), or if the goal cannot be proven (failure). Backtracking: When a leaf is reached, the search travels up to the first parent node where another rule can be matched. ?- G1, G2, G3 R1 :- A1…, B1…, : R2 :- A2…, B2…, root G1, G2, G3 תיאור האלגוריתם מופיע באופן מלא בספר ובאופן חלקי בתרגול. כאן מוצג האלגוריתם באופן כללי. הערה: כל goal בשאילתה הוא נוסחה אטומית. A1, B1, G2, G3 A2, B2, G2, G3

14 a finite success tree with one success path.
Semantics: Example 2 – A generated proof tree ?- nand_circuit(In1, In2, Out). nand_circuit(Input1,Input2,Output) :- transistor(Input1,X,Output), transistor(Input2,ground,X), resistor(power, Output). nand_circuit(In1, In2, Out) { Input1_1=In1, Input2_1= In2, Output_1= Out} Rule 1 mgu transistor(n2,ground,n1). transistor(n3,n4,n2). transistor(n5,ground,n4). transistor(In1, X_1, Out), transistor(In2, ground, X_1), resistor(power, Out) resistor(power, n1). resistor(power, n2). resistor(n1, power). resistor(n2, power). { In1=n2, X_1=ground, Out=n1} Fact 1 – transistor { In1=n3, X_1=n4, Out=n2} Fact 2 – transistor { In1=n5, X_1=ground, Out=n4} Fact 3 – transistor transistor(In2,ground,ground), resistor(power, n1) transistor(In2,ground,n4), resistor(power, n2) transistor(In2,ground,ground), resistor(power, n4) [] ענף ההצלחה מהווה תשובה שמתקבלת על ידי הרכבת כל ההצבות על המסלול המוביל אל העלה בו הוא מסתיים. [] לאחר ההרכבה, בוחרים רק את המשתנים המופיעים בשאילתה ואלו ניתנים כתשובה. [] הענף השלישי ייווצר כאשר נבקש תשובה נוספת. נשים לב: ביוניפיקציה הראשונה, ה – terms שבראש הכלל "מקבלים" את ה – terms שבשאילתא (input_1 "מקבל" את In1). ביוניפיקציה הבאה אין משתנים בראש הכלל, כל ה – terms הם קבועים. במקרה כזה המשתנים בשאילתא "מקבלים" את ה – terms שבראש הכלל. ברגע שהצלחנו לבצע יוניפיקציה עם עובדה, היא לא תופיע בילד הבא בעץ (בפועל, עובדה היא כלל שהגוף שלו הוא true . הגוף אמנם מחליף את העובדה בילד הבא, אבל rsel "מדלג" עליו) {In2=n2} Fact 1 – transistor fail fail { In2=n3} Fact 2 – transistor fail { In2=n5} Fact 3 – transistor true resistor(power, n2) { In2=n2} Fact 1 – transistor fail { In2=n3} Fact 2 – { In2=n5} Fact 3 – a finite success tree with one success path.

15 Semantics: proof trees
Possible types of proof trees: A success tree has at least one success path in it. A failure tree is one in which every path is a failure path. A proof tree is an infinite tree if it contains an infinite path. Otherwise, it is a finite tree. Example: An infinite tree is generated by repeatedly applying the following rule (left recursion): p(X):-p(Y),q(X,Y) ?-p(A) To avoid the infinite path, we could rewrite the rule as follows (tail recursion): p(X):- q(X,Y),p(Y) השאילתא, לדוגמא, P(A), תיכנס ללולאה אינסופית בהתאם לסדר הבחירה של כללים בתהליך ההוכחה. במקרה השני, קיימת אפשרות שהתהליך ייעצר בעקבות יוניפיקציה של q(X,Y) שתייחס את Y לקבוע כך ש – p(Y) יתקיים לפי עובדה כלשהי. לדוגמא: אם נוסיף את העובדות p(6) ו – q(3,6). במקרה הראשון, לעומת זאת, גם אם נניח שעבור p מוגדרות גם כמה עובדות, עדיין ניכנס ללולאה אינסופית כאשר נמצה אותן. כלומר, בהכרח ניכנס ללולאה אינסופית בשלב כלשהו, בעוד במקרה השני זה לא כך. שאלה: איך ישפיע שינוי סדר בחירת הכללים או בחירת ה-goals או הכללים על הרקורסיה הנ"ל?

16 An example: Relational logic programming & SQL operations.
Relational logic programming has no ability to describe composite data, only atoms. Logic programming is equipped with functors to describe composite data. Q: In the following procedure, which symbols are predicates? Which are functors? % Signature: tree_member(Element,Tree)/ 2 % Purpose: Checks if Element is an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). Predicate A Functor applied to three data items. Q: how to tell a predicate from a functor? A predicate appears as an identifier of an atomic formula. A functor is way to construct a term, which is part of an atomic formula. A functor can be nested, a predicate cannot. NOTE: The same name may be used for both a predicate and a functor!

17 An example: Relational logic programming & SQL operations.
Logic Programming: Unification with functors An example: Relational logic programming & SQL operations. Unification is more complex with functors. Here is an execution of the Unify algorithm, step by step: Unify(A,B) where A = tree_member (tree (X, 10, f(X)), W) ; B = tree_member (tree (Y, Y, Z), f(Z)). Initially, s={} Disagreement-set = {X=Y} X does not occur in Y  s=s  {X=Y} = {X=Y} As= tree_member (tree (X, 10, f(Y)), W ) Bs= tree_member (tree (Y, Y , Z ), f(Z)) As ≠ Bs Disagreement-set = {Y=10}  s=s  {Y=10} = {X=10, Y=10} As= tree_member (tree (Y, 10, f(Y)), W ) Bs= tree_member (tree (Y, Y ,Z ), f(Z)) As ≠ Bs  Disagreement-set = {Z=f(10)}  s=s  {Z=f(10)} = {X=10, Y=10, Z=f(10)} As= tree_member (tree (10, 10, f(10)), W ) Bs= tree_member (tree (10, 10, Z ), f(Z)) As ≠ Bs Disagreement-set = {W=f(f(10))} s={X=10, Y=10, Z=f(10), W=f(f(10))} As= tree_member (tree (10, 10, f(10)), W ) Bs= tree_member (tree (10, 10, f(10)), f(f(10))) As ≠ Bs As= tree_member (tree (10, 10, f(10)), f(f(10)) ) Bs= tree_member (tree (10, 10, f(10)), f(f(10))) Q: Why check for occurence?

18 An example: Relational logic programming & SQL operations.
Logic Programming: Unification with functors An example: Relational logic programming & SQL operations. A: Consider the following two atomic formulas: A = tree_member (tree (X, Y, f(X)), X ) B = tree_member (tree (Y, Y, Z ), f(Z)) Applying Unify(A,B) will result in a loop: X=Y, Z=f(Y)… A = tree_member (tree (Y, Y, f(Y)), Y ) B = tree_member (tree (Y, Y, f(Y) ), f(f(Y)) ) A = tree_member (tree (f(f(Y)), f(f(Y)), f(f(f(Y)))), f(f(Y)) ) B = tree_member (tree (f(f(Y)), f(f(Y)), f(f(f(Y)))), f(f(f(f(Y)))) ) … the substitution cannot be successfully solved. במקרה שהצד הימני בהצבה מכיל את הצד השמאלי, ניכנס ללולאה אינסופית. במקרה זה, נמשיך עם Y=f(f(Y)) מבלי שהתהליך יסתיים.

19 An example: Relational logic programming & SQL operations.
Logic Programming: Using fanctors An example: Relational logic programming & SQL operations. % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). ?- tree_member(1, tree(1,nil, nil)). true ?- tree_member(2,tree(1,tree(2,nil,nil), tree(3,nil, nil))). true. ?- tree_member(1, tree(3,1, 3)). false. ?- tree_member(X,tree(1,tree(2,nil,nil), tree(3,nil, nil))). X=1; X=2; X=3; דוגמא לשימוש בפנקטורים - חיפוש איבר בעץ בינארי: נייצג קדקוד בצורה tree(Node, Left, Right). עלה הוא קדקוד ששני הענפים שלו לא קיימים (nil). תיאור הפרדיקט: העובדה הראשונה מכסה את המקרה בו האיבר שאנו מחפשים נמצא בשורש העץ: X והעץ ששורשו X עומדים ביחס tree_member. אם לא הצלחנו להוכיח את העובדה הראשונה, ננסה את הכללים הבאים: X והעץ הנתון עומדים ביחס אם X ותת העץ השמאלי עומדים ביחס. כנ"ל עבור תת-העץ השמאלי. דוגמאות: במקרה זה נתבצעת יוניפיקציה יחידה עם העובדה הראשונה, אשר מצליחה וההצבה ל-X היא 1. היוניפיקציה עם העובדה הראשונה לא מצליחה ואנו פונים אל הכלל הבא שדורש להוכיח ש-X נמצא בתת-העץ השמאלי. ההוכחה מצליחה כמו במקרה הקודם. היוניפיקציה עם העובדה הראשונה נכשלת. בכלל הבא ננסה להוכיח כי 1 נמצא בעץ 3, אשר כלל אינו עץ. היוניפיקציה הבאה תיכשל.

20 Full Logic Programming: An example proof tree
?- tree_member(X, tree(1, tree(2, nil, nil), tree(3, nil, nil))). tree_member(X, tree(1, tree(2, nil, nil), tree(3, nil, nil))) {X_1 = 1, X = 1 Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} {X_1 = X, Y_1 = 1, Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} {X_1 = X, Y_1 = 1, Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} true {X=1} tree_member (X, tree(2,nil,nil)) tree_member (X, tree(3,nil,nil)) {X_2 = 2, X = 2 Left_2 = nil, Right_2 = nil} {X_2 = X, Y_2 = 2, Left_2 = nil, Right_2 = nil} {X_2 = X, Y_2 = 2, Left_2 = nil, Right_2 = nil} {X_2 = 3, X = 3 Left_2 = nil, Right_2 = nil} {X_2 = X, Y_2 = 3, Left_2 = nil, Right_2 = nil} {X_2 = X, Y_2 = 3, Left_2 = nil, Right_2 = nil} tree_member (X, nil) tree_member (X, nil) true {X=2} fail fail % Signature: tree_member(Element,Tree)/ 2 % … tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). בהתאם ל-mgu שמתקבל מהיוניפיקציה הראשונה: ה – disagreement set בהתחלה הוא X=X_1 לאחר מכן X_1=1 ולכן מתקבל X=1 true {X=3} tree_member (X, nil) tree_member (X, nil) fail fail

21 Full Logic Programming: An example proof tree
% Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). ?- tree_member(1, T). T = tree(1, _G445, _G446) ; T = tree(_G444, tree(1, _G449, _G450), _G446) ; T = tree(_G444, tree(_G448, tree(1, _G453, _G454), _G450), _G446) ; ... Looking for all trees in which 1 is a member, we get an infinite success tree with partially instantiated answers (containing variables). We use a rule that requires a defined input, but our input is a variable. Possible answers are generated by the proof algorithm. In this case we call the rule a generator rule.

22 Logic Programming (cont’d): Lists
Lists in Prolog are represented by a functor (similar to cons in Scheme): The empty list is represented by the constant - [ ]. The pipe symbol ‘|’ : [X | Xs] is a term which represent a list where X is the first element and Xs is a list which includes the rest elements For example: [3 | [5 | [] ] ] Lists of known length can be represented as a list of comma-separated expressions: For example: [ X, 2, f(Y) ] or [3 , 5]

23 Logic Programming (cont’d): Lists
Example 1: Implementing a Context Free Grammar in PROLOG: Representation in PROLOG: NP VP Det N V NP | V a | the woman | man saw S NP VP  Det N V s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([saw]). Q: What language does this CFG represent? ?- s([a,woman,saw,a,man]). true ?-s(X). X = [the,woman,saw,the,woman]; X = [the,woman,saw,the,man]; X = [the,woman,saw,a,woman]; X = [the,woman,saw,a,man]; X = [the,woman,saw] … ?-s([the,man|X]). X = [saw,the,woman]; X = [saw,the,man]; X = [saw,a,woman] … [] את כלל הגזירה הראשון נוכל לקרוא באופן הבא: "לכל Z, Z נגזר מהדקדוק החל מהסימבול S, אם: קיימים X ו-Y כך ש-X נגזר מ – np וכן Y נגזר מ-vp. Z הוא שרשור של X ושל Y. [] את היחס append נקרא: שלוש רשימות L1,L2,L3 עומדות ביחס אם L3 היא שרשור של L1 ושל L2. היחס מוגדר רקורסיבית באופן הבא: הרשימה הריקה ושתי רשימות זהות, Xs עומדות ביחס: כל רשימה היא שרשור של הרשימה הריקה עם עצמה. זוג כלשהו של רשימות עומד ביחס עם רשימה שלישית אם: בראש הרשימה השלישית נמצא ראש הרשימה הראשונה. שארית הרשימה השלישית היא שרשור של זנב הרשימה הראשונה והרשימה השנייה. [] כאשר השאילתה כוללת רק משתנים, תהליך ההוכחה מייצר את כל התשובות האפשריות. במקרה הזה, מדובר בכל המשפטים הקיימים בשפה. % Signature: append(L1,L2,L3)/3 % Purpose: L3 is the concatenation of L1, L2 append([],Xs,Xs). append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs).

24 Logic Programming (cont’d): Lists
Example 1: Implementing a Context Free Grammar in PROLOG: Representation in PROLOG: S NP Adj VP  Det N V NP VP Det N | Det Adj N vicious | marvelous V NP | V a | the woman | man saw s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([shoots]). s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). np(Z):- det(X),adj(W),n(Y),append([X,W,Y],Z). adj([vicious]). adj([marvelous]). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([shoots]). Q: How would we implement the modifications above? [] השתמשנו כאן בפרדיקט append/2 של PROLOG, השונה מהפרדיקט append/3. הפרמטר הראשון הוא רשימה של רשימות והשני – רשימה בה כל אותן רשימות משורשרות.


הורד את "ppt "Logic Programming: Introduction

מצגות קשורות


מודעות Google