Logic Programming Prolog 2
اسلاید 1: 1Lecture 12 Introduction to PrologSWI-Prolog
اسلاید 2: 2Lecture 12 Introduction to PrologWelcome to SWI-Prolog (Version 4.0.9) Copyright (c) 1990-2000 University of Amsterdam. Copy policy: GPL-2 (see www.gnu.org) For help, use ?- help(Topic). or ?- apropos(Word). ?-SWI-PrologYou may query Prolog now. Use the built-in Predicate “halt” to exit Prolog.
اسلاید 3: 3Lecture 12 Introduction to Prolog?- append([abc,def],[ghi,lmn],L).L = [abc, def, ghi, lmn] Yes?- append([abc,def],L,[abc,def,ghi,lmn]).L = [ghi, lmn] Yes?- Query mode
اسلاید 4: 4Lecture 12 Introduction to Prolog?- [user]. |:Do not forget the period at the end of your input. At the “|:” prompt, type in what you want entered into your database.male(john).female(mary).father(john,mary).great(massey).has_a_job(mary).After the last fact is entered and at the “|:” prompt, enter a ctrl-D to exit the consult mode. An example is shown on the next slide.Consult modeYou can enter consult mode by typing in “[user]” at the “?-” Prompt:
اسلاید 5: 5Lecture 12 Introduction to Prolog?- [user].|: likes(tom,jerry).|: likes(mary,john).|: likes(tom,mouse).|: likes(tom,jerry).|: likes(jerry,cheeze).|: likes(mary,fruit).|: likes(john,book).|: knows(mary,book).|: knows(tom,john).|: % user compiled 0.01 sec, 64 bytesYes?- listing. likes(tom, jerry).likes(mary, john).likes(tom, mouse).likes(tom, jerry).likes(jerry, cheeze).likes(mary, fruit).likes(john, book).knows(mary,book).knows(tom,john).Yes
اسلاید 6: 6Lecture 12 Introduction to Prologlisting.listing(likes/2).Listing your predicates
اسلاید 7: 7Lecture 12 Introduction to Prolog?- listing(likes/2). /* we could have said “listing(likes).” in this case*/likes(tom, jerry).likes(mary, john).likes(tom, mouse).likes(tom, jerry).likes(jerry, cheeze).likes(john,book).?- listing(knows). knows(mary,book).knows(tom,john).
اسلاید 8: 8Lecture 12 Introduction to Prologtrace.notrace.Or simply n.spy(likes/2).nospyTracing your program
اسلاید 9: 9Lecture 12 Introduction to Prolog Call: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tomY = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, john) ? creepX = maryY = john ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, mouse) ? creepX = tomY = mouse ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creepX = tomY = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(jerry, cheeze) ? creep?- trace.Yes[trace] ?- likes(X,Y).
اسلاید 10: 10Lecture 12 Introduction to PrologX = jerryY = cheeze ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, fruit) ? creepX = maryY = fruit ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(john, book) ? creepX = johnY = book ;Fail: (7) likes(_G332, _G333) ? creepNo
اسلاید 11: 11Lecture 12 Introduction to PrologStructures
اسلاید 12: 12Lecture 12 Introduction to Prolog?- owns(john,book(prolog,author( _ ),year( X ),edition( _ ))),X >1990.owns(john,book(prolog,author(clocksin_and_mellish),year(1994),edition(4))).owns(victoria,book(prolog,author(bratko),year(2001),edition(3))).owns(john,book(prolog,clocksin_and_mellish)).owns(victoria,book(prolog,bratko)).owns(john, book).owns(victoria, book).owns(george,book).
اسلاید 13: 13Lecture 12 Introduction to PrologArithmetic
اسلاید 14: 14Lecture 12 Introduction to Prolog?- A is 2+3, B is A-1, C is A*2, D is C/B, E is C // B.A = 5B = 4C = 10D = 2.5E = 2 +additionX + Y- subtractionX - Y*multiplicationX * Y/divisionX / Y//division (integer)X // Ymodmodulo (remainder)X mod Y**powerX ** YisequalsZ is X + YArithmetic Operators
اسلاید 15: 15Lecture 12 Introduction to Prologpopulation(us,275000).population(china,1262000).population(nz,4000).Population(india,1000000).land(us,3000).land(china,4000).land(nz,250).land(india,3288).concen(X,Y):-population(X,P),land(X,L),Y is P / L.
اسلاید 16: 16Lecture 12 Introduction to Prolog?- concen(X,Y).X = usY = 91.6667 Yes?- concen(X,Y).X = usY = 91.6667 ;X = chinaY = 315.5 ;X = nzY = 16 ;X = indiaY = 304.136 ;No
اسلاید 17: 17Lecture 12 Introduction to PrologComparison
اسلاید 18: 18Lecture 12 Introduction to PrologX = Y True if X not equal to YX < Y True if X is less than YX > Y True if X is greater than YX =< Y True if X is less than or equal to YX >= Y True if X is greater than or equal to YExample: Given two positive integers X and Y, their greatest common devisor “D” is found according to the following: if X = Y then D = X if X < Y then D = greatest common devisor of X and Y – X if X > Y then D = do the same as above with X and Y interchangedgreatest common devisor of 6 and 6 is 6greatest common devisor of 27 and 9 is 9greatest common devisor of 20 and 15 is 5X = Y True if X equals to Y
اسلاید 19: 19Lecture 12 Introduction to Prologgcd(X,Y,D) :- X < Y, Z is Y – X, gcd(X, Z, D).gcd(X,Y,D) :- Y < X, gcd(Y, X, D).We could have written the last clause above, asgcd(X,Y,D) :- Y < X, Z is X – Y,gcd(Z, Y, D).?- gcd(20,15,D).D = 5 Yes?- gcd(0,15,D).ERROR: Out of local stackgcd(X,X,X).
اسلاید 20: 20Lecture 12 Introduction to Prologfact(0,1).fact(N,F) :- N > 0, N1 is N – 1, fact(N1, F1), F is N * F1.?- edit(fact).% d:/prolog/fact.pl compiled 0.00 sec, 64 bytesYes?- fact(6,X).X = 720 Yes?- fact(3,X).X = 6 YesAnother Examplen*(n-1)…2*1 n=1,2,3…n!= 1 n=0
اسلاید 21: 21Lecture 12 Introduction to PrologLists
اسلاید 22: 22Lecture 12 Introduction to PrologThe list is the main data structure in Prolog. A list may be empty:[ ]or it may contain one or more terms (constants, variables or structures).[a,b,c,d,e][5,8,3,9,7][the, boy, run]A list can be split into a head and a tail: [H|T].grades(john, [70,87,90,58]).?- grades(john, [H|T]). H = 70 T = [87,90,58]
اسلاید 23: 23Lecture 12 Introduction to PrologHeads and tails of lists List Head Tail [1,2,3]1[2,3][red,blue,[green,yellow]]red[blue,[green,yellow]][ X+Y, a-b]X+Y[a-b][the,[boy,run]]the[[boy,run]][[a],b,c,d][a][b,c,d][ ]____
اسلاید 24: 24Lecture 12 Introduction to Prolog?- [a, b, c] = [Head | Tail].Head = aTail = [b, c] ?- [the,[boy,run]]=[Head|Tail].Head = theTail = [[boy, run]] ?- [a, b, c] = [X, Y | Tail].X = aY = bTail = [c] ?- [a, b, c] = [X|Y,Z].No
اسلاید 25: 25Lecture 12 Introduction to PrologConverting a structure to a list?- likes(john,mary) =.. X.X = [likes, john, mary] Anything in double is equivalent to a list of ASCII values:?- abc=X.X = [97, 98, 99]
اسلاید 26: 26Lecture 12 Introduction to Prologmember(X,[Y| _ ] ) :- X = Y.member(X, [ _ | Y]) :- member(X, Y).It would be easier to write this as:member(X,[X| _ ]).member(X, [ _ | Y]) :- member(X, Y).?- member(1, [3,4,5,8,1,9]).Yes?- member(X, [prolog, c, ada, haskell]).X= prolog;X= cX= ada;X= haskell;NoRecursion and Lists
اسلاید 27: 27Lecture 12 Introduction to PrologOther Exampleschange(you, i).change(are, [am, not]).change(french, australian).change(do, no).change(X, X). /* catchall */alter([ ], [ ]).alter([H|T], [X|Y]) :- change(H, X), alter(T,Y).?- alter([you,are,french],R).R = [i, [am, not], australian] Yes?- alter([you,are,a,computer],R).R = [i, [am, not], a, computer] Yes?-
اسلاید 28: 28Lecture 12 Introduction to Prologasserta/1.assertz/1assert/1retract/1retractall/1Example:assertz(fib(N,F)).
اسلاید 29: 29Lecture 12 Introduction to Prolog:-dynamic fibfib(1,1).fib(2,1).fib(N,F) :-N > 2,N1 is N-1, fib(N1,F1),N2 is N-2, fib(N2,F2),F is F1 + F2,asserta(fib(N,F)).?- fib(8, F).F = 21 ?- fib(6,F).F = 8
اسلاید 30: 30Lecture 12 Introduction to PrologF(6)+f(5)f(4)+f(3) f(2)f(2) f(1)+1 1+f(4) f(3)+f(3) f(2)1 +f(2) f(1)+f(2) f(1)1 1 1 1 1
نقد و بررسی ها
هیچ نظری برای این پاورپوینت نوشته نشده است.