Kĩ thuật lập trình - Chapter 15: Logic programming

Prolog programs are made from terms, which can be: Variables Constants Structures Variables begin with a capital letter, like Bob. Constants are either integers, like 24, or atoms, like the, zebra, ‘Bob’, and ‘.’. Structures are predicates with arguments, like: n(zebra), speaks(Y, English), and np(X, Y) The arity of a structure is its number of arguments (1, 2, and 2 for these examples).

ppt20 trang | Chia sẻ: huyhoang44 | Lượt xem: 538 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Kĩ thuật lập trình - Chapter 15: Logic programming, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Programming Languages 2nd edition Tucker and NoonanChapter 15Logic ProgrammingQ: How many legs does a dog have if you call its tail a leg?A: Four. Calling a tail a leg doesn’t make it one. Abraham LincolnContents15.1 Logic and Horn Clauses15.2 Logic Programming in Prolog15.2.1 Prolog Program Elements15.2.2 Practical Aspects of Prolog15.3 Prolog Examples15.3.1 Symbolic Differentiation15.3.2 Solving Word Puzzles15.3.3 Natural Language Processing15.3.4 Semantics of Clite15.3 5 Eight Queens Problem15.1 Logic and Horn ClausesA Horn clause has a head h, which is a predicate, and a body, which is a list of predicates p1, p2, , pn. It is written as: h  p1, p2, , pnThis means, “h is true only if p1, p2, , and pn are simultaneously true.”E.g., the Horn clause: snowing(C)  precipitation(C), freezing(C) says, “it is snowing in city C only if there is precipitation in city C and it is freezing in city C.”Horn Clauses and PredicatesAny Horn clause h  p1, p2, , pncan be written as a predicate: p1  p2   pn  hor equivalently: (p1  p2   pn)  hBut not every predicate can be written as a Horn clause. E.g., literate(x)  reads(x)  writes(x)Resolution and UnificationIf h is the head of a Horn clause h  terms and it matches one of the terms of another Horn clause: t  t1, h, t2 then that term can be replaced by h’s terms to form: t  t1, terms, t2 During resolution, assignment of variables to values is called instantiation.Unification is a pattern-matching process that determines what particular instantiations can be made to variables during a series of resolutions.ExampleThe two clauses: speaks(Mary, English) talkswith(X, Y)  speaks(X, L), speaks(Y, L), XYcan resolve to: talkswith(Mary, Y)  speaks(Mary, English), speaks(Y, English), MaryYThe assignment of values Mary and English to the variables X and L is an instantiation for which this resolution can be made.15.2 Logic Programming in PrologIn logic programming the program declares the goals of the computation, not the method for achieving them.Logic programming has applications in AI and databases.Natural language processing (NLP)Automated reasoning and theorem provingExpert systems (e.g., MYCIN)Database searching, as in SQL (Structured Query Language)Prolog emerged in the 1970s. Distinguishing features: Nondeterminism Backtracking15.2.1 Prolog Program ElementsProlog programs are made from terms, which can be:VariablesConstantsStructuresVariables begin with a capital letter, like Bob.Constants are either integers, like 24, or atoms, like the, zebra, ‘Bob’, and ‘.’.Structures are predicates with arguments, like: n(zebra), speaks(Y, English), and np(X, Y)The arity of a structure is its number of arguments (1, 2, and 2 for these examples).Facts, Rules, and ProgramsA Prolog fact is a Horn clause without a right-hand side. Its form is (note the required period .): term.A Prolog rule is a Horn clause with a right-hand side. Its form is (note :- represents  and the required period .): term :- term1, term2, termn.A Prolog program is a collection of facts and rules.Example Programspeaks(allen, russian).speaks(bob, english).speaks(mary, russian).speaks(mary, english).talkswith(X, Y) :- speaks(X, L), speaks(Y, L), X \= Y.This program has four facts and one rule. The rule succeeds for any instantiation of its variables in which all the terms on the right of := are simultaneously true. E.g., this rule succeeds for the instantiation X=allen, Y=mary, and L=russian.For other instantiations, like X=allen and Y=bob, the rule fails.Searching for Success: QueriesA query is a fact or rule that initiates a search for success in a Prolog program. It specifies a search goal by naming variables that are of interest. E.g., ?- speaks(Who, russian). asks for an instantiation of the variable Who for which the query speaks(Who, russian) succeeds.A program is loaded by the query consult, whose argument names the program. E.g., ?- consult(speaks). loads the program named speaks, given on the previous slide.Answering the Query: UnificationTo answer the query: ?- speaks(Who, russian).Prolog considers every fact and rule whose head is speaks. (If more than one, consider them in order.)Resolution and unification locate all the successes: Who = allen ; Who = mary ; NoEach semicolon (;) asks, “Show me the next success.”Search TreesFirst attempt to satisfy the query ?- talkswith(Who, allen).Fig 15.2Database Search - The Family Tree Fig 15.4Prolog ProgramFig 15.3mother(mary, sue).mother(mary, bill).mother(sue, nancy).mother(sue, jeff).mother(jane, ron).parent(A,B) :- father(A,B).parent(A,B) :- mother(A,B).grandparent(C,D) :- parent(C,E), parent(E,D).father(john, sue).father(john, bill).father(bob, nancy).father(bob, jeff).father(bill, ron).Some Database QueriesWho are the parents of jeff? ?- parent(Who, jeff). Who = bob; Who = sueFind all the grandparents of Ron. ?- grandparent(Who, ron). What about siblings? Those are the pairs who have the same parents. ?- sibling(X, Y) :- parent(W, X), parent(W, Y), X\=Y.ListsA list is a series of terms separated by commas and enclosed in brackets.The empty list is written [].The sentence “The giraffe dreams” can be written as a list: [the, giraffe, dreams]A “don’t care” entry is signified by _, as in[_, X, Y] A list can also be written in the form: [Head | Tail]The functions append joins two lists, and member tests for list membership.append Functionappend([], X, X).append([Head | Tail], Y, [Head | Z]) :- append(Tail, Y, Z).This definition says: 1. Appending the empty list to any list (X) returns an unchanged list (X again). 2. If Tail is appended to Y to get Z, then a list one element larger [Head | Tail] can be appended to Y to get [Head | Z].Note: The last parameter designates the result of the function. So a variable must be passed as an argument.member Functionmember(X, [X | _]).member(X, [_ | Y]) :- member(X, Y).The test for membership succeeds if either: 1. X is the head of the list [X | _] 2. X is not the head of the list [_ | Y] , but X is a member of the list Y.Notes: pattern matching governs tests for equality. Don’t care entries (_) mark parts of a list that aren’t important to the rule.More List FunctionsX is a prefix of Z if there is a list Y that can be appended to X to make Z. That is: prefix(X, Z) :- append(X, Y, Z).Similarly, Y is a suffix of Z if there is a list X to which Y can be appended to make Z. That is: suffix(Y, Z) :- append(X, Y, Z).So finding all the prefixes (suffixes) of a list is easy. E.g.: ?- prefix(X, [my, dog, has, fleas]). X = []; X = [my]; X = [my, dog];

Các file đính kèm theo tài liệu này:

  • pptch15a_2379.ppt
Tài liệu liên quan