Presentation is loading. Please wait.

Presentation is loading. Please wait.

인공지능언어 : PROLOG 3-1 PROLOG stands for programming in logic - an idea emerged in the early 1970s to use logic as a programming language. PROLOG PROLOG 는.

Similar presentations


Presentation on theme: "인공지능언어 : PROLOG 3-1 PROLOG stands for programming in logic - an idea emerged in the early 1970s to use logic as a programming language. PROLOG PROLOG 는."— Presentation transcript:

1 인공지능언어 : PROLOG 3-1 PROLOG stands for programming in logic - an idea emerged in the early 1970s to use logic as a programming language. PROLOG PROLOG 는 대상 (Object) 과 대상간의 관계 (Relationship) 를 포함하고 있는 문제를 해결하는데 사용되는 언어이다. PROLOG 프로그래밍은 다음과 같은 사항들로 구성 된다. 1. 대상과 그들간의 관계에 대한 사실 (Facts) 선언 2. 대상과 그들간의 관계에 대한 규칙 (Rules) 의 정의 3. 대상과 그들간의 관계에 대한 물음 (Questions) 논리지향적 언어이며, 술어논리 (Predicate Logic) 에 기반을 두고 있다 병렬탐색에 적합, 병렬처리가 주요요소가 될 미래의 컴퓨터에 적합 대형 시스템 개발에는 부적절,

2 3-2 사실 (Facts) “John 은 Mary 를 좋아한다 ” 는 사실은 PROLOG 에서는 다음과 같이 표현한다. likes(john, mary). Object : john, mary Relationship: likes PROLOG 주의사항 대상과 관계의 명칭은 반드시 알파벳 소문자로 시작한다. 관계를 쓴 후에 대상들이 콤머 (,) 로 구분해서 열거되며, 대상들은 소 괄호 안에 열거된다. 사실 표현의 끝에는 마침표를 찍는다.  소괄호 안에 열거된 대상들을 인수 (Arguments) 라하고, 괄호 앞의 관계를 술어 (Predicate) 라 한다. 관계는 임의의 개수의 인수를 가질 수 있다.

3 3-3 사실 (Facts) 의 예 valuable(gold). female(yonghee). male(changsoo). owns(changsoo, gold). father(changsoo, yonghee). daughter(yonghee, changsoo). gives(changsoo, book, yonghee). play(changsoo, yonghee, football). play(jane, jim, badminton). % This is a comment /* This is also a comment */ PROLOG

4 3-4 물음 (Questions) PROLOG 에서는 물음은 사실과 비슷하지만 다음과 같이 표현한다. ?- owns(mary, book). 이 물음에서 mary 를 “Mary” 라 부르는 사람으로 해석하고, Book 을 어떤 특정의 책을 의미한다. 이 물음은 “Mary 는 그 책을 가지고 있는가 ?.” 라는 물음이다. PROLOG likes(joe, fish). likes(joe, mary). likes(joe, book). likes(john, book). ?- likes(joe, money). no ?- likes(mary, joe). no ?- likes(joe, book). yes

5 3-5 변수 (Variables) “John 은 Mary 를 좋아합니까 ?.” 의 질문 대신에 “John 은 X 를 좋아합니까 ?.” 라고 묻는다면 이 경우 X 는 변수라 부른다. 변수는 알파벳 대문자로 표현한다. PROLOG 변수를 사용하는 경우 instantiate 되거나 not instantiate 되거나 한다. PROLOG likes(jiho, flowers). likes(john, mary). likes(jisuk, book). likes(john, book). ?- likes(jiho, X). % jiho 가 좋아하는 것은 무엇입니까 ?. X = flowers ?- likes(X, book). % book 을 좋아하는 대상은 무엇입니까 ?. X=jisuk; % 첫 번재 답 다음 ; 을 치면 X=john; % 두 번재 답 다음 ; 을 치면 no ?- likes(john, X).

6 3-6 논리적 ( 論理積, Conjunctions) 질문 : “jiho 와 jisuk 은 서로 좋아합니까 ?.” PROLOG likes(jiho, food). likes(jiho, wine). likes(jisuk, wine). likes(jisuk, jiho). 위 질문은 “jiho 는 jisuk 을 좋아합니까 ?. 그리고 jisuk 은 jiho 를 좋아 합니까 ?.” 와 같은 의미이므로 두 개의 goal 을 동시에 만족시켜야 한다. ?- likes(jisuk, jiho), likes(jiho, jisuk). no “jiho 와 jisuk 은 모두 무엇을 좋아합니까 ?.“ ?- likes(jiho, X), likes(jisuk, X).

7 3-7 다음의 절차로 답을 찾아 낸다. PROLOG likes(jiho, food). likes(jiho, wine). likes(jisuk, wine). likes(jisuk, jiho). 1. 첫번째 목표는 만족되고, X 는 food 로 instantiate 된다. 2. 이어 두번째 목표를 만족시키고자 한다. ?- likes(jiho, X), likes(jisuk, X). likes(jiho, food). likes(jiho, wine). likes(jisuk, wine). likes(jisuk, jiho). 3. 두번째 목표는 실패하고, 4. 이어 backtracking 이 일어나다 : X 의 바로 전의 값을 지우고, 첫번째 목표를 다시 만족시키기 위한 시도를 한다. ?- likes(jiho, X), likes(jisuk, X).

8 3-8 PROLOG likes(jiho, food). likes(jiho, wine). likes(jisuk, wine). likes(jisuk, jiho). 5. 첫번째 목표는 다시 만족되고, X 는 wine 로 instantiate 된다. 6. 이어 두번째 목표를 만족시키고자 한다. ?- likes(jiho, X), likes(jisuk, X). likes(jiho, food). likes(jiho, wine). likes(jisuk, wine). likes(jisuk, jiho). 7. 두번째 목표가 성공한다. 8. PROLOG 는 성공을 알리고 다음 반응을 기다린다. ?- likes(jiho, X), likes(jisuk, X).

9 3-9 규칙 (Rules) We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no more than a stored query. Its syntax is head :- body. Where head a predicate definition (just like a fact) :- the neck symbol, sometimes read as "if" body one or more goals (a query) PROLOG where_food(X,Y) :- location(X,Y), edible(X). Example 1) “There is something X to eat in room Y if located in Y, and X is edible.”

10 3-10 Example 2) PROLOG If X likes wine John likes X. likes (john, X) :- likes(X, wine). Example 3) X is female, Y is female, X has mother M and father A, and Y has the same mother and father as X does. sister_of (X,Y) :- female(X), female(Y), parents(X, M, F), parents(Y, M, F).

11 PROLOG 3-11 A Recursive Rule Definition PROLOG predecessor(X, Z) :- parent(X, Z). (a) X is direct predecessor of Z X Z parent predecessor X Y1 parent Y2 parent Z predecessor (b) X is an indirect predecessor of Z Rule formulation (a): For all X and Z, X is predecessor of Z if X is a parent of Z. predecessor(X, Z) :- parent(X, Y1), parent(Y1,Y2), parent(Y2, Z).

12 PROLOG 3-12 A Recursive Rule Definition PROLOG X Y parent Z predecessor Recursive formulation of the predecessor relation For all X and Z, X is predecessor of Z if there is a Y such that (1) X is a parent of Y and (2) Y is a predecessor of Z. predecessor(X, Z) :- parent(X, Z). predecessor(X, Z) :- parent(X,Y), predecessor(Y, Z).... predecessor

13 PROLOG 3-13 PROLOG parent(pam, bob). %Pam is a parent of bob parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). female(pam). %Pam is a female male(tom). male(bob). female(ann). female(pat). female(liz). offspring(Y, X) :- parent(X, Y). mother(X, Y) :- parent(X, Y), female(X). %X is the mother of Y if X is a parent of Y and X % is female grandparent(X, Z) :- parent(X, Y), parent(Y, Z). sister(X, Y) :- parent(Z, X), parent(Z, Y), female(X), female(Y). % rule1 predecessor(X, Z) :- parent(X,Z). % rule2 predecessor(X, Z) :- parent(X,Y), predecessor(Y, Z). ?- predecessor(pam, X). X = bob; X = ann; X = pat; X = jim

14 PROLOG 3-14 PROLOG ?- predecessor(tom, pat). 의 수행과정 predecessor(tom, pat) parent(tom, pat) by rule1 parent(tom, Y) predecessor(Y, pat) by rule2 predecessor(bob, pat) parent(bob, pat) by parent(tom, bob) by rule1 Y = bob no yes

15 3-15 기본요소 Characters: A~Z, a~z, _, 0~9, ; ! % + - * / /* */ :-. ~ | ?- = Data objects: PROLOG Atoms can be constructed in three ways: (1) string of letters, digits and ‘_’, starting with a lower-case letter: x_2 x23 x___y I_miss_you (2) string of special characters: ====> ….:. Note: do not use a predetermined meaning :- ?- (3) strings of characters enclosed in single quotes: ‘Tom’ ‘South_Korea’ ‘Gun Ho Lee’ Data objects simple objects structures constants variables atoms numbers

16 3-16 Variables: consist of strings of letter, digit and underscore characters. Note that starting with an upper-case letter or an underscore characters: X, Y, Z, Result, Objects, Shopping_mall, _x23, __x900 PROLOG hasachild(X) :- parent(X, Y). hasachild(X) :- parent(X, _). somebody_has_child :- parent(_, _). somebody_has_child :- parent(X, Y). If we are interested in people who have children, but not in the name of the children, then we can ask: ?- parent(X, _).

17 Structures: are objects that have several components. The compoents themselves can, in turn, be structures. 3-17 PROLOG date sept. 22 1998 Date(22, spet, 1998) arguments functor (a) as it is represented as a tree (b) as it is written in Prolog P2=(2,3) P1=(1, 1) (4, 2) (6, 4) (7, 1) point for points, seg for line segments,and triangle for triangles. Example Then the object can be represented as next: P1 = point(1, 1) P2 = point(2, 3) S = seg(P1, P2) = seg(point(1, 1), point(2, 3)) T = triangle(point(4, 2), point(6, 4), point(7, 1)) S T 3 차원 좌표라면 또 다른 functor, point3 사용 : point3(X, Y, Z) point(X, Y) 와 point(X, Y, Z) 의 모두 사용가능

18 Structures: 3-18 PROLOG par: a functor of the parallel compositions of resistors seg: a functor the sequential compositions of resistors r1 r2 r1 r2 r1 r2 r3 r1 r2 r3 r4 seg(r1, r2) par(r1, r2) par(r1, par(r2, r3)) par(r1, seq(par(r2, r3), r4)) seg r1 r2 par r1 r2 par r1 par r2 r3 par r1 seq r4 par r2 r3

19 3-19 Matching Given two terms, we say that they are match if: (1) they are identical, or (2) the variables in both terms can be instantiated to objects in such a way that after the substitution of variables by these objects the terms become identical. PROLOG Match terms, date(D, M, 1998) and date(D1, may, Y1). - D is instantiated to D1 - M is instantiated to may - Y1 is instantiated to 1998 Prolog outputs results: D = D1 M = may Y1 = 1998 How to match two terms, date(D, M, 1998) and date(D1, M1, 1999) ?. How to match two terms, date(X, Y, Z) and point(X, Y, Z) ?.

20 3-20 Example) PROLOG ?. vertical(seg(point(1, 1), point(1, 2))). yes ?. vertical(seg(point(1, 1), point(2, Y))). no ?. horizontal(seg(point(1, 1), point(2, Y))). Y = 1 ?. vertical(seg(point(2, 3), P). P = point(2, Y) ?. vertical(S), horizontal(S). S = seg(point(X, Y), point(X, Y)) vertical(seg(point(X, Y), point(X, Y1))). horizontal(seg(point(X, Y), point(X1, Y))). point(X, Y1) point(X, Y) point(X1, Y)point(X, Y) Illustration of vertical and horizontal line segments The following conversion is possible with the above two factors:

21 member(H,[H|T]). % rule 1 member(X,[H|T]) :- member(X,T). % rule 2 ?- member(a, [c, d, a]). 의 질문에 대한 추론과정 member(a, [c, d, a]) by rule 1 No member(a, [c, d, a]) member(a, [c|[d, a]]) by rule 2 member(a, [d, a]]) :- member(a, [d| [a] ]) :- member(a, [a]) member(a, [a|[]]) by rule 1 Yes 1 2 3 4 56 7

22 member(a, [c, d, a]) 이 참인가 거짓인가를 판별하기 위해 먼저 1 과 같이 rule 1 을 적용시켜본 결과 실패한다. rule 2 에 의해 만족하는지 확인 한다. member(a, [c, d, a]) 는 사실상 member(a, [c|[d, a]]) 과 같고 rule 2 로 member(a, [c|[d, a]]) 이 만족되기 위해 가정 member(a, [d, a]]) 이 참 이여야 한다. member(a, [d, a]]) = member(a, [d|[a]]) 이므로 member(a, [d|[a]]) 이 참이기 위해서는 rule 2 에 의해 member(a, [a]) 이 만족되어야 한다. member(a, [a]) = member(a, [a|[]]) 이므로, member(a, [a|[]]) 는 rule 1 에 의해 참임 이 명백하다. 다시 정리하면 1. member(a, [a|[]]) 가 rule 1 에 의해 참이고 2. member(a, [a|[]]) = member(a, [a]) 이므로 3. member(a, [d|[a]]) :- member(a, [a]) 가 만족된다. 4. member(a, [c|[d,a]]) :- member(a, [d,a]) 도 만족하여 5. member(a, [c, d, a]) 가 참이다.

23 add([ ],X,X). % rule 1 add([H|T1],X,[H|T2]) :- add(T1,X,T2). % rule 2 ?- add([a,b],[c,d,e], [a,b,c,d,e]). 의 질문에 대한 추론과정 add([a,b],[c,d,e], [a,b,c,d,e]) by rule 1 No add([a,b],[c,d,e], [a,b,c,d,e]) add([a|[b]],[c,d,e],[a|[b,c,d,e]]) by rule 2 :- by rule 1 Yes 1 2 3 4 56 add([b],[c,d,e], [b,c,d,e]) add([b|[]],[c,d,e], [b|[c,d,e]])add([],[c,d,e], [c,d,e])

24 add([a,b],[c,d,e], [a,b,c,d,e]). 이 참인가 거짓인가를 판별하기 위해 먼저 1 과 같 이 rule 1 을 적용시켜본 결과 실패한다. 다음은 rule 2 에 의해 만족하는지 확인한 다. add([a,b],[c,d,e], [a,b,c,d,e]) 는 사실상 add([a|[b]],[c,d,e],[a|[b,c,d,e]]) 과 같고 rule 2 을 충족시키기 위해서는, 즉, add([a|[b]],[c,d,e],[a|[b,c,d,e]]) 이 만족되기 위해 가정부분 add([b],[c,d,e], [b,c,d,e]) 이 참 이여야 한다. add([b],[c,d,e], [b,c,d,e]) = add([b|[]],[c,d,e], [b|[c,d,e]]) 이므로 add([b|[]],[c,d,e], [b|[c,d,e]]) 이 참이기 위해서는 rule 2 에 의해 add([],[c,d,e], [c,d,e]) 이 만족되어야 한다. add([],[c,d,e], [c,d,e]) 는 rule 1 에 의해 참임이 명백하다. 1. add([],[c,d,e], [c,d,e]) 가 rule 1 에 의해 참이므로 2. add([b|[]],[c,d,e], [b|[c,d,e]]) :- add([],[c,d,e], [c,d,e]) 가 참 3. add([b|[]],[c,d,e], [b|[c,d,e]]) = add([b],[c,d,e], [b,c,d,e]) 이므로 4. add([a|[b]],[c,d,e],[a|[b,c,d,e]]) :- add([b],[c,d,e], [b,c,d,e]) 가 참 5. add([a|[b]],[c,d,e],[a|[b,c,d,e]]) = add([a,b],[c,d,e], [a,b,c,d,e]) 이므로 add([a,b],[c,d,e], [a,b,c,d,e]) 는 참이다.

25 loc_list([apple, broccoli, crackers], kitchen). loc_list([desk, computer], office). loc_list([flashlight, envelope], desk). loc_list([stamp, key], envelope). loc_list(['washing machine'], cellar). loc_list([nani], 'washing machine'). member(H,[H|T]). member(X,[H|T]) :- member(X,T). append([ ],X,X). append([H|T1],X,[H|T2]) :- append(T1,X,T2). location(X, Y) :- loc_list(List, Y), member(X, List). add_thing(NewThing, Container, NewList) :- loc_list(OldList, Container), append([NewThing], OldList, NewList). add_thing2(NewThing, Container, NewList) :- loc_list(OldList, Container), NewList = [NewThing|OldList]. add_thing3(NewThing, Container, [NewThing|OldList]) :- loc_list(OldList, Container).

26 del(X, [X|T], T). del(X, [Y|T], [Y|T], [Y|T1]) :- del(X, T, T1). ?- del(a, [a, b, c], [b, c]). 이 참인가 ? ?- del(a, [b, c, a], [b, c]). 이 참인가를 추론하는 과정 del(a, [b, c, a], [b, c]) = del(a, [b|[c, a], [b|[c]]) 이므로 del(a, [b|[c, a], [b|[c]]) 이 만족 하기 위해 del(a, [c, a], [c]) 가 만족하면 된다. 즉, rule2 에 의해 del(a, [b|[c, a], [b|[c]]) :- del(a, [c, a], [c]) del(a, [c, a], [c]) = del(a, [c|[a]], [c|[] ]). del(a, [c|[a]], [c|[] ]) :- del(a, [a], [] ) by rule 2 del(a, [a], [] ) = del(a, [a|[]], [] ) 이므로 del(a, [a|[]], [] ) 은 rule 1 에 의해 참이다.

27 del(X, [X|T], T). % rule 1 del(X, [Y|T], [Y|T1]) :- del(X, T, T1). % rule 2 ?- del(a, [b, c, a], [b, c]). 이 참인가를 추론하는 과정 del(a, [b|[c, a], [b|[c]]) :- del(a, [c, a], [c]). 이므로 del(a, [c|[a]], [c|[] ]) :- del(a, [a], [] ) del(a, [a|[]], [] ) by rule 1 yes by rule 2 Since del(a, [b, c, a], [b, c]) = del(a, [b|[c, a], [b|[c]]) Since del(a, [c, a], [c]) = del(a, [c|[a]], [c|[] ]). Since del(a, [a], [] ) = del(a, [a|[]], [] )


Download ppt "인공지능언어 : PROLOG 3-1 PROLOG stands for programming in logic - an idea emerged in the early 1970s to use logic as a programming language. PROLOG PROLOG 는."

Similar presentations


Ads by Google