Presentation is loading. Please wait.

Presentation is loading. Please wait.

1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) 1990-2000 University.

Similar presentations


Presentation on theme: "1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) 1990-2000 University."— Presentation transcript:

1 1Lecture 12 Introduction to Prolog SWI-Prolog

2 2Lecture 12 Introduction to Prolog Welcome 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-Prolog You 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 mode You 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 bytes Yes ?- 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 Prolog listing. 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 Prolog trace. notrace. Or simply n. spy(likes/2). nospy Tracing your program

9 9Lecture 12 Introduction to Prolog Call: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tom Y = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, john) ? creep X = mary Y = john ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, mouse) ? creep X = tom Y = mouse ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tom Y = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(jerry, cheeze) ? creep ?- trace. Yes [trace] ?- likes(X,Y).

10 10Lecture 12 Introduction to Prolog X = jerry Y = cheeze ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, fruit) ? creep X = mary Y = fruit ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(john, book) ? creep X = john Y = book ; Fail: (7) likes(_G332, _G333) ? creep No

11 11Lecture 12 Introduction to Prolog Structures

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 Prolog Arithmetic

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 = 5 B = 4 C = 10 D = 2.5 E = 2 +additionX + Y - subtractionX - Y *multiplicationX * Y /divisionX / Y //division (integer)X // Y modmodulo (remainder)X mod Y **powerX ** Y isequalsZ is X + Y Arithmetic Operators

15 15Lecture 12 Introduction to Prolog population(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 = us Y = 91.6667 Yes ?- concen(X,Y). X = us Y = 91.6667 ; X = china Y = 315.5 ; X = nz Y = 16 ; X = india Y = 304.136 ; No

17 17Lecture 12 Introduction to Prolog Comparison

18 18Lecture 12 Introduction to Prolog X \= Y True if X not equal to Y X < Y True if X is less than Y X > Y True if X is greater than Y X =< Y True if X is less than or equal to Y X >= Y True if X is greater than or equal to Y Example: 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 interchanged greatest common devisor of 6 and 6 is 6 greatest common devisor of 27 and 9 is 9 greatest common devisor of 20 and 15 is 5 X = Y True if X equals to Y

19 19Lecture 12 Introduction to Prolog gcd(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, as gcd(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 stack gcd(X,X,X).

20 20Lecture 12 Introduction to Prolog fact(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 bytes Yes ?- fact(6,X). X = 720 Yes ?- fact(3,X). X = 6 Yes Another Example n*(n-1)…2*1 n=1,2,3… n!= 1 n=0

21 21Lecture 12 Introduction to Prolog Lists

22 22Lecture 12 Introduction to Prolog The 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 Prolog Heads 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 = a Tail = [b, c] ?- [the,[boy,run]]=[Head|Tail]. Head = the Tail = [[boy, run]] ?- [a, b, c] = [X, Y | Tail]. X = a Y = b Tail = [c] ?- [a, b, c] = [X|Y,Z]. No

25 25Lecture 12 Introduction to Prolog Converting 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 Prolog member(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= c X= ada; X= haskell; No Recursion and Lists

27 27Lecture 12 Introduction to Prolog Other Examples change(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 Prolog asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)).

29 29Lecture 12 Introduction to Prolog :-dynamic fib fib(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 Prolog F(6) + f(5)f(4) + f(3) f(2) f(2) f(1) + 1 + f(4) f(3) + f(3) f(2) 1 + f(2) f(1) + 1 1 1 1 1


Download ppt "1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) 1990-2000 University."

Similar presentations


Ads by Google