Docsity

Unification-Artificial Intelligence-Lecture Slides, Slides of Artificial Intelligence

Toggle sidebar
bg1
30/09/04 2
Unification
• When two terms match we say that they unify.
– Their structures and arguments are compatible.
• This can be checked using =/2
|?- brother(rasheed,X) = brother(Y,hafeez).
X = hafeez, unification leads to instantiation
Y = rasheed?
yes
Terms that unify Outcome
fred = fred. yes.
‘Hey you’ = ‘Hey you’. yes
fred=X. X=fred.
X=Y. Y = X.
foo(X) = foo(bar). X=bar.
foo(N,N) = foo(bar,X). N=bar, X=bar.
foo(foo(bar)) = foo(X) X = foo(bar)
Terms that don’t unify
fred = jim.
‘Hey you’ = ‘Hey me’.
frou(frou) = f(frou).
foo(bar) = foo(bar,bar).
foo(N,N) = foo(bar,rab).
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Unification-Artificial Intelligence-Lecture Slides and more Slides Artificial Intelligence in PDF only on Docsity!

30/09/^

2

Unification

-^ When two terms match we say that they unify.– Their structures and arguments are compatible.•^ This can be checked using =/2^ |?- brother(rasheed,X) = brother(Y,hafeez).X = hafeez,

^ unification leads to instantiation Y = rasheed?^  yes

Terms that unify

Outcome fred = fred.^

yes. ‘Hey you’ = ‘Hey you’.

yes fred=X.^

X=fred. X=Y.^

Y = X.

foo(X) = foo(bar).

X=bar. foo(N,N) = foo(bar,X). N=bar, X=bar.foo(foo(bar)) = foo(X)

X = foo(bar)

Terms that don’t unifyfred = jim.‘Hey you’ = ‘Hey me’.frou(frou) = f(frou).foo(bar) = foo(bar,bar).foo(N,N) = foo(bar,rab).

30/09/^

3

Asking questions of the

database

We can ask about facts directly:^ |?- mother(X,alan).X = jane?Yes Or we can define rules that prove if aproperty or relationship holds given the factscurrently in the database.^ |?-^ parent(jane,X).X = alan?yes

mother(jane,alan).father(john,alan).parent(Mum,Child):-mother(Mum,Child).parent(Dad,Child):-father(Dad,Child).

30/09/^

5

Arithmetic Operators• Operators for arithmetic and value comparisons arebuilt-in to Prolog= always accessible / don’t need to be written• Comparisons: <, >, =<, >=, =:=

(equals),^ ==

(not equals)

-^ Equality is different from unification= checks if two terms unify=:= compares the arithmetic value of two expressions^ ?- X=Y.^

?- X=:=Y.^

?-X=4,Y=3, X+2 =:= Y+3.

yes^ Instantiation error

X=4, Y=3? yes

30/09/^

6 | ?- X is 5+4*2.X = 13 ?yes

Arithmetic Operators (2)• Arithmetic Operators: +, -, *, /– Need to use

is^ to access result of the arithmetic expression otherwise it is treated as a term:^ |?- X = 5+4.

|?- X is 5+4. X = 5+4?^

X = 9?

yes^

yes (Can X unify with 5+4?)

(What^ is the result of 5+4?)

-^ Mathematical precedence is preserved: /, *,

before^ +,-

-^ Can make compound sums using round brackets– Impose new precedence– Inner-most brackets first

| ?- X is (5+4)*2. X = 18 ?yes

30/09/^

8

Backtracking

|?- bigger(5,4).bigger(N,M):-^ N < M,write(‘The bigger number

is ‘), write(M). bigger(N,M):-N > M,write(‘The bigger number

is ‘), write(N). bigger(N,M):-N =:= M,write(‘Numbers are the same‘).

30/09/^

9

Backtracking

|?- bigger(5,4).bigger(5,4):-5 < 4,

^ fails write(‘The bigger number

is ‘), write(M). bigger(N,M):-N > M,write(‘The bigger number

is ‘), write(N). bigger(N,M):-N =:= M,write(‘Numbers are the same‘).

Backtrack

30/09/^

11

|?- bigger(5,4).^ bigger(N,M):-N < M,write(‘The bigger number

is ‘), write(M). bigger(5,4):-5 > 4,

^ succeeds, go on with body. write(‘The bigger number is ‘), write(5).

The bigger number is 5yes|?-

Backtracking

Reaches full-stop= clause succeeds

30/09/^

12

Backtracking

|?- bigger(5,5).

^ If our query only matches the final clause bigger(N,M):-N < M,write(‘The bigger number

is ‘), write(M). bigger(N,M):-N > M,write(‘The bigger number

is ‘), write(N). bigger(5,5):-5 =:= 5,^ write(‘Numbers are the same‘).

^ Is already known as the first two clauses failed.

30/09/^

14

Reporting Answers

|?- bigger(5,4).

^

Question is asked The bigger number is 5

^ Answer is written to terminal yes^

^ Succeeds but answer is lost

-^ This is fine for checking what the code is doing but not for usingthe proof. |?- bigger(6,4), bigger(Answer,5).Instantiation error! •^ To report back answers we need to–^ put an uninstantiated variable in the query,–^ instantiate the answer to that variable when the query succeeds,–^ pass the variable all the way back to the query.

30/09/^

15

Passing Back Answers• To report back answers we need to1. put an uninstantiated variable in the query, | ?- bigger(6,4,Answer),bigger(Answer,5,New_answer). 2. instantiate the answer to that variable when the querysucceeds,3. pass the variable all the way back to the query.

bigger(X,Y,Answer):- X>Y.bigger(X,Y,Answer):- X=<Y.

, Answer = X., Answer = Y.