1.FACTS : Basically what is known. Axioms about some objects and their relationship.
- Johnny is fat.
- The dog is brown.
- Suzie likes bobby.
fat(johnny).
brown(dog).
likes(suzie,bobby).
Some of one argument , some of them are 2 arguments.It is normal. Arity is number of arguments.
2.RULES : Extends facts.
likes(ryan,britney).
likes(britney,ryan).
likes(dan,josh).
Now lets define a rule for dating.
dating(X,Y):- likes(X,Y),likes(Y,X).
and ,
or ;
if :-
NOT not
3.QUERIES : Complex form of facts and rules.
VARIABLES
wthr.pl
>/*weather(City, Season, Temp).*/
weather(phoenix, summer, hot).
weather(la, summer, warm).
weather(phoenix, winter, warm).
- We write the program.Now for Prolog
- When we get true answer we can move.We want to write cities hot in summer.For this
?- weather(City, summer, hot).
City = phoenix.
- Now we want to write cities that is warm no matter what season is.
City = la
City = phoenix .
- We want to write a city hot in summer and warm in winter.
City = phoenix.
- Now we add some codes to whtr.pl.
write(' is warmer than '), write(C2), nl.
nl means new line.
For running this write:
warmer_than(phoenix,la).
We get :
phoenix is warmer than la
true.
STRUCTS :
For a course we can write name of the course, date of the course, time of the course etc.
course(cse110,mon,wed,11,12,11,12,holton,bryce,coor105,coor321).
But this so long and complicated.In order to do this we can write these by using structs.
course(
cse110,
date(mon,wed),
time(11,12),
prof(holton,bryce),
coor105
).
This is for monday's course.Now for wednesday's course:
course(
cse110,
date(mon,wed),
time(11,12),
prof(holton,bryce),
coor321
).
This is more suitable :)
- For text editor and compiler we can use SWISH. Here is the link.http://pengines.swi-prolog.org/apps/swish/index.html
[Head|Tail].
Ex:
[Head|Tail] = [john,josh,eat(cheese)].
Head=>john
Tail=>josh,eat(cheese).
Ex: If we want to print put first two item of the list.
We should choose seperation part correctly.
[X, Y | W] = [[],ryan, josh,eat(cheese)].
X=[]
Y=ryan
W=josh,eat(cheese).
NESTED LIST:
_ means we do not care about that element.
[_,_,[_|X]|_] = [[],dead(z),[2,[b,c]],[],z,[2,[b,c]]].
If we write this we get : [[b,c]]
MEMBER FUNCTION:
member(X,[X|T]). /* If X is the head of the list stop, do nothing.Returns true.*/
/* if not */
member(X,[H|T]) :-
member(X,T). /*If not now do the function for tail of the list.Recursive*/
For example
- member(john, [john,jan,judy]). /* this returns true.Because john is the head of the list.*/
- member(troy,[john,judy,troy]). /*this returns true.Because of recursion function evetually find troy in the list.*/
- member(troy,[john,jan,judy]). /* This returns false.Because there is no troy in the list.*/
- member(X,[23,25,67,12,25,19,9,6] ), Y is X*X, Y<400. /*it shows the nımbers whose square is smaller than 400.*/
X=19,Y=361
X=9,Y=81
X=6,Y=36
man(adam).
man(peter).
man(paul).
woman(mary).
woman(eve).
parent(adam,peter).
parent(eve,peter).
parent(mary,paul).
father(F,C) :- man(F), parent(F,C).
mother(M,C) :- woman(M), parent(M,C).
is_father(F):- father(F,_).
is_mother(M):- mother(M,_).
is_son(X,Y):-man(X), parent(Y,X).
is_daughter(X,Y):-woman(X), parent(Y,X).
siblings(A,B):-parent(X,A),parent(X,B), A\=B.
uncle(U,A):- man(U), siblings(X,U),parent(X,A).
aunt(X,Y):-woman(X), siblings(Z,X),parent(Z,X).
grand_parent(G,N):-parent(X,N), parent(G,X).
APPEND FUNCTION IN LISTS
For concatinating two lists.For example
>append([1,2,3],[a,b,c],[1,2,3,a,b,c]).
If we write this we get true.
>append([1,2,3],[a,b,c],[a,b,c,1,2,3]).
If we write thiswe get false.
If we want to write function of append:
> append([],L,L).
append([H|T],L2,[H|L3]) :- append(T,L2,L3).
This is the function of append.If we want to process we should look at below.
append([a, b, c], [1, 2, 3], _G518)
append([b, c], [1, 2, 3], _G587)
append([c], [1, 2, 3], _G590)
append([], [1, 2, 3], _G593)
append([], [1, 2, 3], [1, 2, 3])
append([c], [1, 2, 3], [c, 1, 2, 3])
append([b, c], [1, 2, 3], [b, c, 1, 2, 3])
append([a, b, c], [1, 2, 3], [a, b, c, 1, 2, 3])
X = [a, b, c, 1, 2, 3]
yes
Ex:?- append(X,Y,[a,b,c,d]).
As we see from this we concatinate X and Y lists and we get [a,b,c,d].If we run this :
X = [],
Y = [a, b, c, d]
Y = [a, b, c, d]
X = [a],
Y = [b, c, d]
Y = [b, c, d]
X = [a, b],
Y = [c, d]
Y = [c, d]
X = [a, b, c],
Y = [d]
Y = [d]
X = [a, b, c, d],
Y = []
falseY = []
- The prefixes of [a,b,c,d] are [] , [a] , [a,b] , [a,b,c] , and [a,b,c,d] .
?- prefix(X,[a,b,c,d]).
X = [] ;
X = [a] ;
X = [a,b] ;
X = [a,b,c] ;
X = [a,b,c,d] ;
false
- The suffixes of [a,b,c,d] are [] , [d] , [c,d] , [b,c,d] , and [a,b,c,d] .
?- suffix(X,[a,b,c,d]).
X = [a,b,c,d] ;
X = [b,c,d] ;
X = [c,d] ;
X = [d] ;
X = [] ;
false
- The sublists of [a,b,c,d] are [] , [a] , [b] , [c] , [d] , [a,b] , [b,c] , [c,d] , [a,b,c] , [b,c,d] , and [a,b,c,d] .
BACTRACKING:
Consider a piece-wise function:
- if x < 3 then y = 0
- if x >= 3 and x < 6 then y =2
- if x >= 6 then y= 4
Lets encode this in Prolog
f(X,0) :- X <3.
f(X,2) :- 3 =< X, X < 6.
f(X,4) :- 6 =< X.
Consider f(1,Y), 2<Y.
- First it looks f(X,0). Y is instantiated to 0 . But second part fails 2 < Y. So Prolog bactracks and tries the other predicates. But the first predicate was true , the others will always fail !
- We want to tell Prolog that if the first one succeds, there is no need to try the others.
- We do this with a cut .
f(X,0) :- X <3, !.
f(X,2) :- 3 =< X, X < 6, !.
f(X,4) :- 6 =< X.
- The cut (!) prevents Prolog from backtracking backwards through the cut.
- If the first predicate fails, we know that x >= 3.So we don't have to check it in the second one.Simirlarly with x>=6 for the second and third predicates.
f(X,2) :- X < 6, !.
f(X,4) .
- What if we remove cuts.
f(X,2) :- X < 6.
f(X,4) .
?-f(1,X).
The result will (0,2,4).
Ex: Maximum of two values without a cut:
max(X,Y,X) :- X >= Y.
max(X,Y,Y) :- X < Y.
Ex: Maximum of two values with a cut :
max(X,Y,X) :- X >= Y, !.
max(X,Y,Y) .
Hiç yorum yok:
Yorum Gönder