Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).

Similar presentations


Presentation on theme: "Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain)."— Presentation transcript:

1 Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain). ◦ Generalizations  All men are mortal  mortal(X) :- man(X). ◦ Definitions  An animal is a bird if it has feathers  bird(X) :- animal(X), has_feather(X).

2 Matching Artificial Intelligence Programming in Prolog LAB 4 2

3 the =/2 predicate tests whether its two arguments match. For example, if we pose the query: =(mia,mia). =(mia,vincent). Prolog will respond ‘no’. mia = mia. yes

4 4 The central ideas of Prolog MATCHING –any two data items can be compared for similarity, and values can be bound to variables in order to allow a match to succeed.  If term1 and term2 are constants.  If term1 is a variable and term2 is any type of term, then term1 and term2 match, and term1 is instantiated to term2. And vica versa.

5  If term1 and term2 are complex terms, then they match if and only if: (a) They have the same functor and arity. (b) All their corresponding arguments match (c) and the variable instantiations are compatible. (I.e. it is not possible to instantiate variable X to mia, when matching one pair of arguments, and to then instantiate X to vincent, when matching another pair of arguments.)

6 Let’s try an example with a variable: mia = X. X = mia yes

7 Now, let’s look at an example involving complex terms: kill(shoot(gun),Y) = kill(X,stab(knife)). X = shoot(gun) Y = stab(knife) yes

8 ?- X=mia, X=vincent. no  ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes

9 ?- loves(X,X) = loves(marsellus,mia). no

10 10 Structure unification 2 structures will unify if ◦ the functors are the same, ◦ they have the same number of components, ◦ and all the components unify. | ?- person(Nm,london,Age) = person(bob,london,48). Nm = bob, Age = 48? yes | ?- person(Someone,_,45) = person(harry,dundee,45). Someone = harry ? yes (A plain underscore ‘_’ is not bound to any value. By using it you are telling Prolog to ignore this argument and not report it.)

11 11 Structure unification (2) A structure may also have another structure as a component. |?-addr(flat(4),street(‘Home Str.’),postcode(eh8_9lw)) = addr(flat(Z),Yy,postcode(Xxx)). Z = 4, Yy = street(‘Home Str.’), Xxx = eh8_9lw ? yes Unification of nested structures works recursively: ◦ first it unifies the entire structure, ◦ then it tries to unify the nested structures. Remember to close brackets! Reported variables are ordered according to number of characters in the variable name.

12 12 Structures = facts? The syntax of structures and facts is identical but: ◦ Structures are not facts as they are not stored in the database as being true (followed by a period ‘.’); ◦ Structures are generally just used to group data; ◦ Functors do not have to match predicate names. However predicates can be stored as structures command(X):- X. | ?- X = write(‘Passing a command’), command(X). Passing a command X = write('Passing a command') ? yes By instantiating a variable with a structure which is also a predicate you can pass commands.

13 Exercises

14 SEARCH TREE

15

16

17

18

19

20 Another Example

21 21 Summary Prolog’s proof strategy can be represented using AND/OR trees. Tree representations allow us trace Prolog’s search for multiple matches to a query. They also highlight the strengths and weaknesses of recursion (e.g. economical code vs. infinite looping). Recursive data structures can be represented as structures or lists. Structures can be unified with variables then used as commands. Lists can store ordered data and allow its sequential processing through recursion.


Download ppt "Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain)."

Similar presentations


Ads by Google