Download presentation

Presentation is loading. Please wait.

Published byArturo Westmoreland Modified about 1 year ago

1
George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial Intelligence, 5 th edition. © Pearson Education Limited,

2
PROLOG What is True What needs to be Proved Not How to do it like in other Programming languages 2

3
PROLOG Edinburgh Syntax Programming in PROLOG –Clocksin and Mellish,

4
PROLOG EXAMPLES 4

5
EXAMPLE 1 Set of facts: parent(ali,ahmad). parent(ahmad,salem). parent(ali,fatema). parent(fatema,osama). male(ali). male(ahmad). male(salem). male(osama). female(fatema). 5 aliahmadsalemfatemaosama

6
EXAMPLE 1 Every sentence ends with a period “.” Names starts with lower case, constants 6

7
EXAMPLE 1 ? parent(ali, ahmad). By unifying this statement with the axioms (assertions) in order. The above query (goal) also ends with a period Ans: yes ?parent(ali,X). The above statement asks about all ali’s child Semicolon asks for more answers Ans: X=ahmad; Note: by unifying X with ahmad X=fatema; no 7

8
EXAMPLE 1 ?parent(X,Y). X=ali Y=ahmad; X=ahmad Y=salem; X=ali Y=fatema; X=fatema Y=osama; no 8

9
EXAMPLE 1 ?parent(X,ahmad), male(X). Comma “,” means and Who is ahmad’s father It first unifies the first part, i.e parent(X,ahmad). It finds that X could take the value of ali. It then attempts to prove male(ali) Ans: X=ali; No Backtracking on two levels to find other answers 9

10
EXAMPLE 1 Add the following rule (considered rule not fact): father(X,Y):- parent(X,Y), male(X). father(X,Y)called head parent(X,Y), male(X)called body ?father(X,ahmad). Ans: X=ali; No ?father(fatema,Y). no Because fatema is unified with X and there is no way to prove that male(fatema) 10

11
EXAMPLE 1 sibling(X,Y):- parent(Z,X), parent(Z,Y). ?sibling(X,Y) X=ahmad, Y=ahmad; X=ahmad, Y=fatema; X=salem, Y=salem; X=fatema, Y=ahmad; X=fatema, Y=fatema; X=osama, Y=osama; no 11

12
EXAMPLE 1 sibling(X,Y):- parent(Z,X), parent(Z,Y), X\==Y. ?sibling(X,Y) X=ahmad, Y=fatema; X=fatema, Y=ahmad; no 12

13
EXAMPLE 1 Define uncle relation, uncle(X,Y). uncle(X,Y):- parent(Z,Y), parent(G,Z), parent(G,X), X\==Z. 13

14
HW: Define the following rules: mother son daughter sister sibling grandfather(X,Y):-parent(X,Z),parent(Z,Y), male(X). grandmother cousin(X,Y) 14

15
Built-in mathematical predicates X=:=Ytrue when X equals Y X=\=Ytrue when X does not equal to Y X

16
Built-in Mathematical predicates ?6 = : = yes ?8 = : = 4*2. yes ?7= : = 4+6. no ?6 =:= 6. yes 16

17
Built-in logical predicates X= =Y true when X can be unified with Y X\= =Y true when X is not the same as Y X=Y attempts to unify X with Y 17

18
Built-in logical predicates ?5= =5. yes ?X= = Y. no ?5+1 == 5+1. yes ?5+1 = = 1+5. no 18

19
Built-in logical predicates ?X= ali. X=ali ?X= 1/2. X=1/2 ? f(X)= f(ali). X=ali ?f = g. no 19

20
Built-in logical predicates ?5 \= 4. yes yes ? 'Basem'. yes ? X=1+2. X=1+2 20

21
Built-in logical predicates ? X is 1+2. X=3 ? X is 4*2. X=8 ?X is 7//3. X=2 ? X is 5, X is 3+3. no 21

22
Mathematical operations +Addition -Subtraction *Multiplication /Real division //Integer division modmodulus ^Exponent 22

23
Negation as failure home(X):-not out(X). out(ali). ?home(ali). no ?home(X). no ?home(zaki). yes ?out(zaki). no 23

24
Horn Clauses P1 ^ P2 ^ P3 ^ P4 R R:- P1, P2, P3, P4. 24

25
Recursion A program that calls itself Assume the following relations and attempt to find predecessor parent(ali,ahmad). parent(ahmad,fatema). parent(fatema,osama). 25

26
Recursion A non recursion version predecessor(X,Y):- parent (X,Y). predecessor(X,Y):- parent (X,Z), parent(Z,Y). predecessor(X,Y):- parent (X,Z1), parent (Z1,Z2), parent(Z2,Y). The above program is applicable to a limited number of generations. 26

27
Recursion Recursion version predecessor(X,Y):- parent (X,Y). predecessor(X,Y):- parent (X,Z), predecessor(Z,Y). 27

28
Recursion ? predecessor(ali,ahmad). yes ? predecessor(ali,fatema). yes ? predecessor(ali,osama). yes. ? predecessor(ali,X). X=ahmad 28

29
Recursion Factorial 29

30
Recursion Factorial fact(0,1). fact(N,X):- N>0, N1 is N-1, fact(N1,X1), X is N*X1. ?fact(0,X). X=1. ?fact(1,X). X=1 30

31
Recursion HW: Write a program to calculate the sum of the numbers: 1, 2, 3,..., N 31

32
List Processing Empty lists are denoted as [] [H | T] list of : –head H representing the first element. –tail T represents the rest of the elements. –[5, 2, 7, 10] H is 5, T is [2, 7, 10] 32

33
List Processing ?[H | T]=[5, 2, 7, 10]. H=5 T=[2, 7, 10] ?[a,b,c,d]=[X,Y | T]. X=a Y=b T=[c, d] 33

34
List Processing ?[H | T]=[ali]. H=ali T=[] ?[H | T]=[]. no 34

35
List Processing - membership Test for a membership in a list member(X,[X | T]). member(X,[ _ | T]):- member(X,T). ?member(5,[5, 7]). yes ?member(5,[1, 5]). yes 35

36
List Processing - membership To get all members of a list you can use: ?member(X,[a,b,c,d]). X=a; X=b; X=c; X=d; no 36

37
List Processing - count To count how many members in a list count([],0). count([X | T],C):- count(T,C1), C is C1+1. ?count([2,4,7],C). C=3 37

38
List Processing - sum To calculate the sum of the elements in a list sum([],0). sum([X | T],S):- sum(T, S1), S is S1+X. ?sum([2,4,7],S). S=13 38

39
List Processing - append To append two lists and produce a list containing the elements of the first list then the elements of the second list append([], L, L). append([H | T1], L, [H | T]): append(T1, L, T). 39

40
List Processing - append ?append([r,t],[a,b,c],L). L=[r,t,a,b,c]. ?append(L1,L2,[a,b,c]). L1=[] L2=[a,b,c]; L1=[a] L2=[b,c]; L1=[a,b] L2=[c]; L1=[a,b,c] L2=[]; no 40

41
List Processing - split To divide a list into two parts positive and negative positive([],[],[]). positive([X|Y],[X|L1], L2) :- X >= 0, positive(Y,L1, L2). positive([X|Y],L1, [X|L2]) :- positive(Y,L1, L2). 41

42
List Processing – write elements To print the elements of a list write_a_list([]). write_a_list([H|T]):- write(H), nl, write_a_list(T). ? write_a_list([5, 6, 7])

43
Cut operator To find a maximum of two numbers, you might write: max(X,Y,X):- X>=Y. max(X,Y,Y). ?max(5,3,M). M=5; M=3; no 43

44
Cut operator To find a maximum of two numbers, you might write: max(X,Y,X):- X>=Y,!. max(X,Y,Y). ?max(5,3,M). M=5; no 44

45
Cut operator Add an element to a list only if that element does not exist already in the list add(X, L, L):- member(X,L), !. add(X, L, [X|L]). 45

46
Self test list exercises 1.Calculate the max/min value in a list. ? min_in_list([5,7,3,7,9], A). A=3 ? max_in_list([8,4,9,4],X). X=9 46

47
Self test list exercises 2.Calculate how many times a specific number appears in a list. 3.Take a list as input and return its reverse in another list. 4.Take two lists as input and return their union, intersection, and difference in another list. 5.Delete one number from a list. 47

48
Homework 1.Write a program to calculate the absolute value of X, absval(X,Y) where X is the input and Y is the output. 2.Write a program to define the even(X) which is true only if X is even. 3.Write a program write_reverse to print the elements of a list in a reverse order 4.Write a program that simulates repeat assuming repeat does not exist. 48

49
PROLOG Self test examples ?concatenate([2, 3, 5], [7, 9, 5], B). B=[2, 3, 5, 7, 9, 5] ?order([2, 8, 3, 5], B). B=[2, 3, 5, 8] 49

50
The fail predicate country(jordan). country(egypt). country(england). print_countries:- country(X), write(X), nl, fail. print_countries. 50

51
The fail predicate ?print_countries. jordan egypt england yes 51

52
The repeat command do:- repeat, read(X), square(X). do. square(stop):-!. square(X):- Y is X*X, write(Y), nl, fail. 52

53
The repeat command ?do stop. yes 53

54
The repeat command do:- read(X), square(X). square(stop). square(X):- X\==stop, Y is X*X, write(Y), nl, do. 54

55
Input / output procedures - write ?X=5, write(X). 5 yes ?write([a,b,5,6]). [a,b,5,6] yes 55

56
Input / output procedures - write ?write(date(16,11,2008)). date(16,11,2008) yes 56

57
Input / output procedures - read ?read(X). a. X=a. ?read(stop). test no 57

58
Dynamic programs Programs that are able to modify themselves. Modification means the ability to add/ remove facts at runtime. Such procedures include: –assert(C) –retract(C) –abolish(C) 58

59
Dynamic programs - assert assert(C) ?parent(zaid, faris). no ?assert(parent(zaid, faris)). yes ?parent(zaid, faris). yes 59

60
Dynamic programs - assert asserta(C) adds the new statement C to the beginning of the program. assertz(C) adds the new statement C to the end of the program. 60

61
Dynamic programs - retract retract removes a statement from the program ?retract(parent(zaid, faris)). yes ?parent(zaid, faris). no 61

62
Dynamic programs - abolish ?abolish(parent,2). yes removes all statements defined as parent with arity 2. 62

63
Structures employee(Name,House_no,Street_name,City_name, Day,Month,Year). employee(ali,5,salt_street,amman,12,10,1980). ?employee(ali,No,S,C,D,M,Y). No=5 S=salt_street C=amman D=12 M=10 Y=

64
Structures employee(ali,address(5,salt_street,amman),date(12,10,1980)). employee(hasan,address(12,university_street,zarqa),date(7,3,1985)). employee(samer,address(9,madina_street,amman),date(2,9,1987)). ?employee(hasan,A,D). A=address(12,university_street,zarqa) D=date(7,3,1985) ?employee(Name,address(No,St,amman),_). Name=ali No=5 St=salt_street; Name=samer No=9 St=madina_street; false no 64

65
Structures ?employee(Name,A,date(_,_,1985)). Name=hasan A=address(12,university_street,zarqa); no ?employee(ali,A,date(Day, Month, Year)), Year>25. A=address(5,salt_street,amman) Day=12 Month=10 Year=1980; no 65

66
Structures – Binary Trees b a c a Example 3 Example 2 Example 1

67
Structures – Binary Trees Binary trees when empty are called nil. It may contain 3 components: –Left subtree –Root –Right subtree It can be represented using the following structure: bin_tree(Left_subtree, Root, Right_subtree). 67

68
Structures – Binary Trees bin_tree(nil,a,nil). 68 a Example 1

69
Structures – Binary Trees bin_tree(bin_tree(nil,b,nil),a, bin_tree(nil,c,nil)). 69 Example 2 b a c

70
Structures – Binary Trees bin_tree(bin_tree(nil,2,nil),5, bin_tree(bin_tree(nil,7,nil),10, bin_tree(nil,9, bin_tree(nil,3,nil)))). 70 Example

71
Structures – Binary Trees - Count count(nil,0). count(bin_tree(Left,Root,Right),C):- count(Left,C1), count(Right,C2), C is C1+C

72
Structures – Binary Trees – Count V2 count(nil,0). count(bin_tree(Left, _,Right),C):- count(Left,C1), count(Right,C2), C is C1+C

73
Structures – Binary Trees - Sum sum(nil,0). sum(bin_tree(Left,Root,Right),S):- sum(Left,S1), sum(Right,S2), S is S1+S2+Root. 73

74
Structures – Binary Trees - depth depth(nil,0). depth(bin_tree(Left,Root,Right),D):- depth(Left,D1), depth(Right,D2), D1>=D2, D is D1+1. depth(bin_tree(Left,Root,Right),D):- depth(Left,D1), depth(Right,D2), D2>D1, D is D

75
Structures – Binary Trees – depth V2 depth(nil,0). depth(bin_tree(Left,Root,Right),D):- depth(Left,D1), depth(Right,D2), max(D1,D2,M), D is M+1. max(X,Y,X):- X>=Y,!. max(X,Y,Y):- X

76
Binary Tree - Hw Write a PROLOG program max_tree to find the maximum value in a binary tree (bin_tree) assuming all values are numeric. Write a PROLOG program to add a new value to an ordered binary tree. Write a PROLOG program to display the values stored in an ordered binary tree in ascending order. 76

77
PROLOG Resources olog_tutorial/2_7.html 77

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google