









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










30/09/^
2
-^ 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.^
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
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
(equals),^ ==
-^ Equality is different from unification= checks if two terms unify=:= compares the arithmetic value of two expressions^ ?- X=Y.^
yes^ Instantiation error
X=4, Y=3? yes
30/09/^
6 | ?- X is 5+4*2.X = 13 ?yes
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?^
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
|?- 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
|?- 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,
The bigger number is 5yes|?-
Reaches full-stop= clause succeeds
30/09/^
12
|?- 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
|?- 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
bigger(X,Y,Answer):- X>Y.bigger(X,Y,Answer):- X=<Y.
, Answer = X., Answer = Y.