2005 PROLOG Yonsei Univ 1 Prolog Terms Constants

  • Slides: 20
Download presentation
2005. 인공지능 PROLOG Yonsei Univ. 인공지능 1

2005. 인공지능 PROLOG Yonsei Univ. 인공지능 1

목차 § Prolog 소개 § Terms – Constants, Variables, Compound Terms § 기본문 –

목차 § Prolog 소개 § Terms – Constants, Variables, Compound Terms § 기본문 – Facts, Rules § SWI-Prolog § Operators § Lists § findall, bagof, setof § Problem solving § 숙제 인공지능 2

Constants § Integer constants(정수) : 1, 2, 3, … § Real constants(실수) : 1.

Constants § Integer constants(정수) : 1, 2, 3, … § Real constants(실수) : 1. 23, … § Atom(원자) : 알파벳, 숫자, 특수문자로 구성 – 소문자 알파벳으로 시작 w ex) john, book, mjk 0206, … – 특수문자로만 구성 w ex) *, . , =, @#$, … – 인용부호(‘ ’)를 이용하여사용가능 w ex) ‘george-smith’ – Atom은 Variable이 아님 : not bound to anything, never equal to anything else (string constants에 더 가까움) 인공지능 Terms 4

Compound Terms § Terms 구성 성분 혹은 요소(Component)들을 모아 단일 대상으로 나타냄 – Structured

Compound Terms § Terms 구성 성분 혹은 요소(Component)들을 모아 단일 대상으로 나타냄 – Structured data라고 생각하면 됨 § 함수(술어)와 요소(구성 성분)에 의해 기술 – structure-name ( attribute, . . . , attribute ) – Ex) 자동차를 표현하는 예 w Maker, Age, Price 의 3가지 특성 포함 w car(ford, 3, 5000) § 활용 예 has(joe, car(toyota, 5, 1000)). has(mick, car(ford, 2, 2000)). ? - has(mick, car(ford, Age, Price)) Age와 Price는 변수이므로 첫 글자가 대문자! Answer: Age=2, Price=2000 인공지능 6

Rules § 재귀적 Rules ancestor(X, Y) : - parent(X, Y). ancestor(X, Y) : parent(Z,

Rules § 재귀적 Rules ancestor(X, Y) : - parent(X, Y). ancestor(X, Y) : parent(Z, Y), ancestor(X, Z). § 다른 Rules를 사용한 Rules grandparent(GP, GC) : parent(GP, P), parent(P, GC). greatgrandparent(GGP, GGC) : grandparent(GGP, P), parent(P, GGC). 인공지능 8

SWI-Prolog Welcome to SWI-Prolog (Multi-threaded, Version 5. 4. 7) Copyright (c) 1990 -2003 University

SWI-Prolog Welcome to SWI-Prolog (Multi-threaded, Version 5. 4. 7) Copyright (c) 1990 -2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http: //www. swi-prolog. org for details. For help, use ? - help(Topic). or ? - apropos(Word). ? - § Prompting for a query with ‘? -’ § 질문, 결과출력 의 반복 수행 인공지능 9

relations. pl parent(kim, holly). /* comment */ parent(margaret, kim). % comment SWI-Prolog parent(margaret, kent).

relations. pl parent(kim, holly). /* comment */ parent(margaret, kim). % comment SWI-Prolog parent(margaret, kent). parent(esther, margaret). parent(herbert, jean). greatgrandparent(GGP, GGC) : parent(GGP, GP), parent(GP, P), parent(P, GGC). 인공지능 10

Interpreter의 사용 § SWI-Prolog 파일 읽기 ? - consult(relations). % relations compiled 0. 00

Interpreter의 사용 § SWI-Prolog 파일 읽기 ? - consult(relations). % relations compiled 0. 00 sec, 1, 420 bytes Yes ? ? - ['relations']. % relations compiled 0. 00 sec, 1, 420 bytes Yes ? - § 간단한 질문 ? - parent(margaret, kent). Yes ? - 인공지능 11

SWI-Prolog 변수를 이용한 질문 ? - parent(margaret, Child). Child = kim 사용자의 입력 을

SWI-Prolog 변수를 이용한 질문 ? - parent(margaret, Child). Child = kim 사용자의 입력 을 기다림. Child = kim ; Child = kent ; Yes No ? - § 변수는 질문의 어떤 위치에도 나올 수 있음 w parent(Parent, jean) w parent(esther, Child) § ‘Enter’ 대신에 ‘; ’를 입력하여 Prolog system에게 추가 답을 요청 § 질문을 ‘, ’로 결합가능 w parent(margaret, X), parent(X, holly). 인공지능 12

Rule을 이용한 질문 SWI-Prolog ? - greatgrandparent(esther, Grate. Grandchild). Grate. Grandchild = holly Yes

Rule을 이용한 질문 SWI-Prolog ? - greatgrandparent(esther, Grate. Grandchild). Grate. Grandchild = holly Yes ? - greatgrandparent(esther, Great. Grandchild) GGP는 esther, GGC는 Great. Grand. Child parent(esther, GP), parent(GP, P), parent(P, Great. Grandchild) GP는 margaret parent(margaret, P), parent(P, Great. Grandchild) P는 kim parent(kim, Great. Grandchild) Great. Grandchild는 holly 인공지능 13

List Predicates § append(X, Y, Z) ? - append([1, 2], [3, 4], Z). Z

List Predicates § append(X, Y, Z) ? - append([1, 2], [3, 4], Z). Z = [1, 2, 3, 4] ? - append(X, Y, [1, 2, 3]). X = [] Y = [1, 2, 3] ; Yes X = [1] Y = [2, 3] ; ? - append(X, [3, 4], [1, 2, 3, 4]). X = [1, 2] Y = [3] ; X = [1, 2] Yes Lists X = [1, 2, 3] Y = [] ; No § member(X, Y) – List Y가 X를 포함하는지 확인 인공지능 16

findall, bagof, setof ? -findall(C, parent(john, C), L). L = [peter, paul, mary] ?

findall, bagof, setof ? -findall(C, parent(john, C), L). L = [peter, paul, mary] ? -findall(C, parent(P, C), L). L = [peter, paul, mary, davy, dee, dozy] parent(john, peter). parent(john, paul). parent(john, mary). parent(mick, davy). parent(mick, dee). parent(mick, dozy). ? -bagof(C, parent(P, C), L). P = john L = [peter, paul, mary]; P = mick ? -bagof(C, P^parent(P, C), L). L = [peter, paul, mary, davy, dee, dozy] L = [davy, dee, dozy] setof/3 : bagof/3 과 비슷하지만 결과를 중복없이 sort해서 보여줌 인공지능 17

Problem solving (predicate) § Prolog의 주된 control structure 는 recursion § 예) partition/4 predicate:

Problem solving (predicate) § Prolog의 주된 control structure 는 recursion § 예) partition/4 predicate: partition(L, N, Littles, Bigs) § Step 1. declarative specification % partition(L, N, Littles, Bigs) % list L중에서 N보다 작은수는 Littles에, 큰 수는 Bigs에 포함 § Step 2. recursion과 출력 arguments를 identify (코드 골격 작성) partition([ ], N, [ ]). partition([Head|Tail], N, ? Littles, ? Bigs): /* do something with Head */ partition(Tail, N, Littles, Bigs). 인공지능 18

Problem solving (predicate) (cont. ) § Step 3. 코드 body 작성 partition([], N, []).

Problem solving (predicate) (cont. ) § Step 3. 코드 body 작성 partition([], N, []). partition([Head|Tail], N, ? Littles, ? Bigs): Head < N, partition(Tail, N, Littles, Bigs), ? Littles = [Head|Littles], ? Bigs = Bigs. partition([Head|Tail], N, ? Littles, ? Bigs): Head >= N, partition(Tail, N, Littles, Bigs), ? Littles = Littles, ? Bigs = [Head |Bigs]. § Step 4. 출력 argument 채우기 partition([], N, []). partition([Head|Tail], N, [Head|Littles], Bigs): Head < N, partition(Tail, N, Littles, Bigs). partition([Head|Tail], N, Littles, [Head|Bigs]): Head >= N, partition(Tail, N, Littles, Bigs). 인공지능 19

숙제 Tom Arnold Nick Nicole Kitty Michael Jack § Julia Ann Mike Sue 위의

숙제 Tom Arnold Nick Nicole Kitty Michael Jack § Julia Ann Mike Sue 위의 가계도를 보고 male, female, father, mother의 fact를 작성하고, 이를 이용하여 다음 predicate을 구현하시오. 1. brother_and_sister(X, Y). %X, Y는 남매인가? 2. grandparent(X, Y). %X는 Y의 조부모(혹은 외조부모)인가? 3. ancestor(X, Y). %X는 Y의 조상인가? 인공지능 20