Скачать презентацию Prolog Unification and clause order The notion Скачать презентацию Prolog Unification and clause order The notion

87dfab6addfa5b2c9bddcd2fa1f074d4.ppt

  • Количество слайдов: 21

Prolog Unification and clause order Prolog Unification and clause order

The notion of Unification n n Unification is when two things “become one” Unification The notion of Unification n n Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like equality in algebra Unification is mostly like pattern matching Example: n n loves(john, X) can unify with loves(john, mary) and in the process, X gets unified with mary 2

Unification I n n The explicit unification operator is = Any value can be Unification I n n The explicit unification operator is = Any value can be unified with itself. n n n A variable can be unified with another variable. n n sunny = sunny weather(sunny) = weather(sunny) X = Y A variable can be unified with any Prolog value. n n Topic = weather(sunny) We say that the variable is instantiated to the value 3

Unification II n Two different structures can be unified if their constituents can be Unification II n Two different structures can be unified if their constituents can be unified. n n n female(X) = female(jane) mother(mary, X) = mother(Y, father(Z)) A variable can be unified with a structure containing that same variable. n n X = f(X) This is usually a Bad Idea and should be avoided. 4

Unification III n n n The explicit unification operator is = Unification is symmetric: Unification III n n n The explicit unification operator is = Unification is symmetric: Steve = father(isaac) means the same as father(isaac) = Steve In well-written code, most unification happens implicitly, as a result of parameter matching. n n n In database: loves(john, mary). As a query: ? - loves(X, mary). Consequence: X and mary are unified. Poor style: ? - loves(X, Y), X = mary. If you find yourself using =, try to replace it with parameter matching. 5

Scope of Names n n The scope of a variable is the single clause Scope of Names n n The scope of a variable is the single clause in which it appears. The scope of the “anonymous” (“don’t care”) variable, _, is itself. n n loves(_, _) = loves(john, mary) A variable that only occurs once in a clause is useless, and is called a singleton; you should either: n n Replace it with the anonymous variable, or Put an underscore as the first character of the variable name; this tells Prolog you don’t intend to use it more than once 6

Writing Prolog Programs n Suppose the database contains loves(chuck, X) : - female(X), rich(X). Writing Prolog Programs n Suppose the database contains loves(chuck, X) : - female(X), rich(X). female(jane). and we ask who Chuck loves, ? - loves(chuck, Woman). n female(X) finds a value for X , say, jane n rich(jane) then tests whether Jane is rich n (At this point, X is jane) 7

Clauses as Cases n A predicate consists of multiple clauses, each of which represents Clauses as Cases n A predicate consists of multiple clauses, each of which represents a “case” grandson(X, Y) : - son(X, Z), son(Z, Y). grandson(X, Y) : - son(X, Z), daughter(Z, Y). abs(X, Y) : - X < 0, Y is -X. abs(X, X) : - X >= 0. 8

Ordering n n Clauses are always tried in order buy(X) : - good(X). buy(X) Ordering n n Clauses are always tried in order buy(X) : - good(X). buy(X) : - cheap(X). cheap('Java 2 Complete'). good('Thinking in Java'). n What will buy(X) choose first? 9

Ordering II n Try to handle more specific cases (those having more variables instantiated) Ordering II n Try to handle more specific cases (those having more variables instantiated) first. dislikes(john, bill). dislikes(john, X) : - rich(X). dislikes(X, Y) : - loves(X, Z), loves(Z, Y). 10

Ordering III n Some “actions” cannot be undone by backtracking over them: n n Ordering III n Some “actions” cannot be undone by backtracking over them: n n write, nl, assert, retract, consult Do any necessary tests before you take any undoable actions: n take(Thing) : holding(Thing), write('You're already holding it!'), nl. take(Thing) : - next clause… 11

Recursion Handle the base cases first ancestor(X, Y) : - child(Y, X). (X is Recursion Handle the base cases first ancestor(X, Y) : - child(Y, X). (X is an ancestor of Y is a child of X. ) Recur only with a simpler case ancestor(X, Y) : child(Z, X), ancestor(Z, Y). (X is an ancestor of Y if Z is a child of X and Z is an ancestor of Y). 12

Basic and derived clauses n You can often choose which facts you want to Basic and derived clauses n You can often choose which facts you want to be “basic” and which derived son(isaac, steven). child(X, Y) : - son(X, Y). male(isaac). child(isaac, steven). son(X, Y) : - male(X), child(X, Y). 13

Cut and Cut-fail n The cut, !, is a commit point. It commits to: Cut and Cut-fail n The cut, !, is a commit point. It commits to: n n n Example: n n n the clause in which it occurs (can’t try another) everything up to that point in the clause loves(chuck, X) : - female(X), !, rich(X). Chuck loves the first female in the database, but only if she is rich. Cut-fail, (!, fail), means give up now and don't even try for another solution. 14

Negation n n The negation operator is + (or in some Prolog dialects, not(…)) Negation n n The negation operator is + (or in some Prolog dialects, not(…)) Consider for example, +female(X). n If there is some female, say, female(jane), then female(X) will succeed, and X will be tentatively unified with jane n n Then the success is negated, so +female(X) fails; the unification is cancelled, and backtracking occurs This will be repeated for each female If there is no female in the database, female(X) fails without doing any unifications, then +female(X) succeeds Hence, negation can never result in a unification n Consequently, negation is seldom useful 15

What you can’t do in Prolog n n n There are no functions, only What you can’t do in Prolog n n n There are no functions, only predicates Prolog is programming in logic, therefore there are few control structures There are no assignment statements; the state of the program is what’s in the database 16

Workarounds I n n There are few control structures in Prolog, but… You don’t Workarounds I n n There are few control structures in Prolog, but… You don’t need an if statement because you can use multiple clauses with “tests” in them You don’t need loops because you have recursion You can, if necessary, construct a “fail loop” 17

Fail Loops notice_objects_at(Place) : at(X, Place), write('There is a '), write(X), write(' here. '), Fail Loops notice_objects_at(Place) : at(X, Place), write('There is a '), write(X), write(' here. '), nl, fail. notice_objects_at(_). n Use fail loops sparingly, if at all. 18

Workarounds II n n There are no functions, only predicates, but… A call to Workarounds II n n There are no functions, only predicates, but… A call to a predicate can instantiate variables: female(X) can either n n n look for a value for X that satisfies female(X), or if X already has a value, test whether female(X) can be proved true By convention, “output” variables (ones that you would like to get instantiated) are put last 19

Workarounds III n n There are no assignment statements, but… the Prolog database keeps Workarounds III n n There are no assignment statements, but… the Prolog database keeps track of program state n n n assert(at(fly, bedroom)) bump_count : retract(count(X)), Y is X + 1, assert(count(Y)). Don’t get carried away and misuse this! 20

The End 21 The End 21