rules statements about objects and their relationships expess ◦ if-then conditions i use an...

21
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).

Upload: godwin-long

Post on 13-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 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)

RulesStatements about objects and their

relationshipsExpess

◦ 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).

Page 2: 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)

Matching

Artificial Intelligence Programming in PrologLAB 4

2

Page 3: 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)

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

Page 4: 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)

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.

Page 5: 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)

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.)

Page 6: 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)

Let’s try an example with a variable:

mia = X.X = miayes

Page 7: 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)

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

Page 8: 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)

?- 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

Page 9: 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)

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

no

Page 10: 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)

10

Structure unification2 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.)

Page 11: 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)

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.

Page 12: 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)

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.

Page 13: 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)

Exercises

Page 14: 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)

SEARCH TREE

Page 15: 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)
Page 16: 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)
Page 17: 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)
Page 18: 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)
Page 19: 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)
Page 20: 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)

Another Example

Page 21: 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)

21

SummaryProlog’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.