Prolog Syntax
Visual Prolog ¿¡¼ ¹®¹ýÀ» ¼³¸íÇÑ ¿¹Á¦ÀÌ´Ù. Visual Prolog Tutorial ÀÇ ÀϺÎÀÌ´Ù.
Anonymous Variables (¹«¸í º¯¼ö)
Compound goals (º¹ÇÕ ¸ñÇ¥) : conjunctions ¿Í disjunctions (and, or)
Prolog ÀÇ ±âº» program section
Multiple arity ( ¿©·¯°³ÀÇ Àμö¸¦ °¡Áú ¶§ )
Relentless search for solution (¹«Â÷º°ÇÑ Å½»ö)
Cut ÀÇ »ç¿ë¹ý : rule¿¡¼ ÀÌÀüÀÇ subgoal ·Î backtrackÀ» ¹æÁö
Cut ÀÇ »ç¿ë¹ý : ´ÙÀ½ clause(¶Ç´Â rule) ·ÎÀÇ backtrackÀ» ¹æÁö
ÀÏ´Ü prolog ¿¡ ÀÏ·ÃÀÇ fact ¸¸ ºÎ¿©ÇÏ¸é ±× fact ¿Í °ü·ÃÇÏ¿© Áú¹®À» ÇÒ¼ö°¡ ÀÖ´Ù,À̰ÍÀ» Querying the prolog system À̶ó ÇÑ´Ù.
PREDICATES /* Predicates = Relations */
nondeterm likes(symbol,symbol)
CLAUSES
likes(ellen,tennis). /* ellen, tennis ´Â °´Ã¼, likes ´Â °ü°è */
likes(john,football). /* john, football Àº Àμö, likes ´Â predicate */
likes(tom,baseball). /* "tom Àº baseballÀ» likes ÇÑ´Ù" ¶ó´Â fact */
likes(eric,swimming). /* °´Ã¼¿Í °ü°è´Â ¼Ò¹®ÀÚ, */
likes(mark,tennis). /* ¸ðµç fact ¿Í rule Àº ¸¶Ä§Ç¥(.) ·Î ³¡³´Ù */
likes(bill,Activity):- likes(tom, Activity).
/* tom ÀÌ ¾î¶² Activity¸¦ likes ÇÑ´Ù¸é billµµ ±× Activity¸¦ likeÇÑ´Ù
´Â rule Ç¥Çö, º¯¼ö´Â ´ë¹®ÀÚ */
GOAL
likes(bill, baseball).
Ãâ·Â
yes
PREDICATES /* clauses ¼½¼Ç¿¡¼ »ç¿ëµÉ predicate ¼±¾ð */
nondeterm can_buy(symbol, symbol)
nondeterm person(symbol)
nondeterm
car(symbol)
likes(symbol,
symbol)
for_sale(symbol)
CLAUSES /* fact (°´Ã¼¿Í ±×µéÀÇ °ü°è) ¿Í rule ·Î ±¸¼º */
can_buy(X,Y):- /* rule ÀÇ °á·ÐºÎ */
person(X), /* rule ÀÇ Á¶°ÇºÎ */
car(Y), /* Á¶°ÇºÎ´Â Çϳª ÀÌ»óÀÇ AND³ª OR·Î ¿¬°áµÈ ºÎ¸ñÇ¥¸¦ °¡Áü */
likes(X,Y), /* prolog¿¡¼ , ´Â AND À̰í ; ´Â OR ÀÌ´Ù */
for_sale(Y).
person(kelly).
person(judy).
person(ellen).
person(mark).
car(lemon).
car(hot_rod).
likes(kelly,
hot_rod).
likes(judy, pizza).
likes(ellen,
tennis).
likes(mark, tennis).
for_sale(pizza).
for_sale(lemon).
for_sale(hot_rod).
GOAL /* prolog ¿¡ ´ëÇÑ Áú¹® (query) */
can_buy(Who,What). /* ±â¾ïµÈ fact ¿Í Áú¹®ÀÌ ÀÏÄ¡ÇÑ ³»¿ëÀÌ ÀÖ´ÂÁö °Ë»ö */
/* prolog¿¡¼ goalÀ» Á¦°øÇÏ´Â ¹æ¹ýÀº program ¿¡ Goal ¼½¼ÇÀ» µÎ´Â ¹æ¹ý°ú program ½ÇÇà½Ã ÇÊ¿äÇÑ ¸ñÇ¥¸¦ dialog window¿¡ Á¦°øÇÏ´Â ¹æ¹ýÀÌ ÀÖ´Ù */
Ãâ·Â
Who=kelly, What=hot_rod
1 solution
prolog ´Â ÇҴ翬»êÀÚ( assignment statement ) °¡ ¾ø´Ù. À̰ÍÀÌ ´Ù¸¥ ¾ð¾î¿ÍÀÇ Áß¿äÇÑ Â÷ÀÌ´Ù. prolog¿¡¼ º¯¼ö´Â fact ³ª rule¿¡¼ÀÇ »ó¼ö( constant )¿Í match µÊÀ¸·Î½á °ªÀ» ¾ò´Â´Ù. º¯¼ö°¡ °ªÀ» ¾ò±â Àü±îÁö¸¦ free ¶ó°íÇÏ°í °ªÀ» ¾òÀ¸¸é bound µÇ¾ú´Ù°í ÇÑ´Ù. query¿¡¼ one solutionÀ» ¾ò±âÀ§ÇØ ÇÊ¿ä·Î µÇ´Â ½Ã°£±îÁö¸¸ bound »óÅ·ΠÀÖ°í ,one solutionÀ» ãÀ¸¸é unbind µÇ°í back up µÇ¾î ´Ù¸¥ solutionÀ» ã´Â´Ù.Áï º¯¼ö¿¡ °ªÀ» ºÎ¿©ÇÏ¿© Á¤º¸¸¦ ÀúÀåÇÒ¼ö ¾ø´Ù. º¯¼ö´Â Á¤º¸ÀúÀå ¿ëµµ°¡ ¾Æ´Ï¶ó pattern matching process ÀÇ ÀϺκÐÀ¸·Î »ç¿ëµÈ´Ù
PREDICATES
nondeterm likes(symbol,symbol)
CLAUSES
likes(ellen,reading).
likes(john,computers).
likes(john,badminton).
likes(leonard,badminton).
likes(eric,swimming).
likes(eric,reading).
GOAL
likes(Person, reading),
likes(Person, swimming).
/* óÀ½¿¡ query ÀÇ Ã³À½ part¿¡ ´ëÇØ top down ¹æÇâÀ¸·Î clauses¸¦ Ž»ö »çÀÛ-> º¯¼ö PersonÀº free»óÅ (free variable, ÀÚÀ¯º¯¼ö) -> µÎ¹øÂ° Àμö reading °ú match µÇ´Â fact¸¦ ¹ß°ß -> Person Àº ellen À¸·Î bind µÇ°í (bound variable, ÇÑÁ¤º¯¼ö) µ¿½Ã¿¡ fact list »ó¿¡ pointer¸¦ À§Ä¡½ÃÅ´ -> query ÀÇ µÎ¹øÂ° part ¼öÇà, Áï likes(ellen, swimming)À» óÀ½ºÎÅÍ Ã£´Â´Ù, Person is ellen¿¡ ´ëÇØ not true -> Person Àº unbind µÇ°í ´Ù½Ã free, query ÀÇ Ã³À½ part¿¡ ´ëÇØ ´Ù¸¥ solutionÀ» ã´Â´Ù -> ¶Ç´Ù¸¥ fact¸¦ ã±âÀ§ÇØ pointer À§Ä¡·Î ´Ù½Ã µ¹¾Æ°£´Ù, À̰ÍÀ» backtracking À̶ó ÇÑ´Ù -> Person Àº ´Ù½Ã ericÀ¸·Î bound µÇ°í µÎ¹øÂ° part¿¡¼ likes(eric,swimming) ¶ó´Â fact¸¦ ¹ß°ßÇÏ¿© matching -> query°¡ ¸¸Á·µÇ¾î °á°ú¸¦ ¸®ÅÏ */
Ãâ·Â
Person=eric
1 solution
query¿¡¼ ¾î¶² Á¤º¸¸¦ ÇÊ¿ä·Î ÇÏÁö¸¸ ¾î¶² °ªÀ» °¡ÁöµçÁö »ó°ü¾ø´Ù¸é Anonymous Varialbles¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. Anonymous Varialbles ´Â underscore ("_") ·Î¼ Ç¥ÇöµÈ´Ù. Anonymous Variables´Â ¾î¶² º¯¼ö ´ë½Å¿¡µµ »ç¿ëÇÒ ¼ö´Â ÀÖÁö¸¸ °áÄÚ °ªÀ» °¡Áú ¼ö´Â ¾ø´Ù. ¾î¶² »ç¶÷µéÀÌ parents ÀÎÁö¸¦ ¾Ë·Á°í ÇÑ´Ù¸é ±×µéÀÇ ÀÚ½ÄÀÌ ´©±¸ÀÎÁö¸¦ ¾Ë ÇÊ¿ä´Â ¾ø´Ù. À̶§¿¡ underscore¸¦ »ç¿ëÇÏ¿© º¯¼ö °ø°£ÀÇ °ªÀ» ¹«½ÃÇÑ´Ù. Áï ´ë°³ »ç¿ëÀÚ°¡ °ü½ÉÀ» °®Áö ¾Ê´Â ´ë»óÀÇ º¯¼ö´Â underlineÀ» »ç¿ëÇÏ¿© º¯¼ö ´ë½Å »ç¿ëÇÑ´Ù
PREDICATES
male(symbol)
female(symbol)
nondeterm parent(symbol,
symbol)
CLAUSES
male(bill).
male(joe).
female(sue).
female(tammy).
parent(bill,joe).
parent(sue,joe).
parent(joe,tammy).
GOAL
parent(Parent, _).
Ãâ·Â
Parent=bill
Parent=sue
Parent=joe
3 solutions
º¹ÇÕ ¸ñÇ¥ Áï subgoal A and subgoal B (=conjunction, and)À» Ç¥ÇöÇϱâÀ§ÇØ comma (,)¸¦ »ç¿ëÇϰųª subgoal A or subgoal B (=disjunction, or)À» Ç¥ÇöÇϱâÀ§ÇØ semicolon(;)À» »ç¿ëÇÑ´Ù
PREDICATES
car(symbol,long,integer,symbol,long)
truck(symbol,long,integer,symbol,long)
nondeterm vehicle(symbol,long,integer,symbol,long)
CLAUSES
car(chrysler,130000,3,red,12000).
car(ford,90000,4,gray,25000).
car(datsun,8000,1,red,30000).
truck(ford,80000,6,blue,8000).
truck(datsun,50000,5,orange,20000).
truck(toyota,25000,2,black,25000).
vehicle(Make,Odometer,Age,Color,Price):-
car(Make,Odometer,Age,Color,Price);
truck(Make,Odometer,Age,Color,Price).
GOAL
car(Make,
Odometer, Years_on_road, Body, 25000).
/* °ªÀÌ Á¤È®ÇÏ°Ô 25000 ÀÎ Â÷¸¦ ã¾Æ³½´Ù. Á»´õ ÀÚ¿¬½º·´°Ô 25000 ºÒ ÀÌÇÏÀÇ Â÷¸¦ ã¾Æ³»·Á¸é car(Make, Odometer, Years_on_road, Body, Cost), Cost < 25000. °ú °°ÀÌ subgoal A and subgoal B ·Î Ç¥ÇöÇØ¾ß ÇÑ´Ù. $25,000 ÀÌÇÏÀÇ car À̰ųª $20,000 ÀÌÇÏÀÇ truckÀº ? ¸¦ Ç¥ÇöÇÏ·Á¸écar(Make,Odometer,Years_on_road,Body,Cost), Cost<25000 ; truck(Make,Odometer,Years_on_road,Body,Cost), Cost < 20000. ¿Í °°ÀÌ subgoal A or subgoal B ·Î Ç¥ÇöÇØ¾ß ÇÑ´Ù. */
Ãâ·Â
Make=ford, Odometer=90000, Years_on_road=4, Body=gray
1 solution
DOMAINSÀº prologÀÇ standard domainÀÌ ¾Æ´Ñ »ç¿ëÀÚ°¡ »ç¿ëÇÏ´Â domainÀ» ¼±¾ðÇÑ´Ù. domains¿Í types ´Â °°Àº ÀǹÌÀÌ´Ù. PREDICATES´Â predicate¸¦ ¼±¾ðÇϰí ÀμöÀÇ domains(types)¸¦ ¼±¾ðÇÑ´Ù. prolog¿¡¼ Á¦°øÇÏ´Â built-in predicate¸¦ ¼±¾ðÇÒ ÇÊ¿ä´Â ¾ø´Ù. CLAUSES´Â ½ÉÀåºÎ¿¡ ÇØ´çÇÑ´Ù. Áï goalÀ» ¸¸Á·½Ã۱â À§ÇÑ fact¿Í ruleÀÌ À§Ä¡ÇÑ´Ù.
DOMAINS
product,sum
= integer
PREDICATES
add_em_up(sum,sum,sum)
multiply_em(product,product,product)
CLAUSES
add_em_up(X,Y,Sum):-
Sum=X+Y.
multiply_em(X,Y,Product):-
Product=X*Y.
/* °ü°è¿¬»êÀÚ ·Î¼ °°´Ù´Â
ÀǹÌÀÌ´Ù (C ¾ð¾îÀÇ ÇÒ´ç ¿¬»êÀÚ = ¿Í ´Ù¸£´Ù*/
GOAL
add_em_up(32,54,Sum).
Ãâ·Â
Sum=86
1 solution
standard domainÀº symbol, string, integer, real, charµîÀÌ ÀÖ´Ù.
symbol Àº sequence of character ·Î¼ string °ú ¹®¹ýÀº °°´Ù. ±×·¯³ª strings¸¦ Æ÷ÇÔÇϰí ÀÖ´Â hashed symbol-table ¿¡ ´ëÇÑ pointer·Î¼ ±¸ÇöµÈ´Ù. symbol, string´Â Å« Àǹ̷Π»óÈ£ interchangeableÇÏ´Ù. ±×·¯³ª ÀúÀ广½ÄÀÌ ´Ù¸£´Ù. symbolÀº ÁÖ¼Ò¸¦ °¡Áö´Â look-up tableÀ» °¡Áö°í¼ objectµéÀ» ã¾Æ°¡¹Ç·Î ¸Å¿ì ºü¸£°Ô match°¡ µÃ´Ù.ÇϳªÀÇ symbolÀÌ program¿¡¼ ¹Ýº¹ÇÏ¿© ³ª¿À¸é compactÇÏ°Ô ÀúÀåÇÒ¼ö ÀÖ´Ù.¹Ý¸é string´Â tableÀÌ ¾ø¾î match µÇ´ÂÁö¸¦ º¸±âÀ§Çؼ ¹®ÀÚ ÇϳªÇϳª¸¦ ´Ù °Ë»çÇØ¾ß ÇÑ´Ù.
DOMAINS
brand,color = symbol
age = byte /*
byte ÇüÀº 8-bit unsigned int (0~255) */
price, mileage = ulong /* ulong ÇüÀº 32-bit unsigned int */
PREDICATES
nondeterm
car(brand,mileage,age,color,price)
CLAUSES
car(chrysler,130000,3,red,12000).
car(ford,90000,4,gray,25000).
car(datsun,8000,1,black,30000).
GOAL
car(renault, 13,
40000, red, 12000). /* age ÀÇ byte type error */
car(ford, 90000, gray, 4, 25000). /* age, color type error */
Ãâ·Â
error
±âº»ÀûÀ¸·Î goal sectionÀº ruleÀÇ body¿Í °°´Ù (¿©·¯°³ÀÇ subgoalÀÌ ÀÖÀ»¼ö ÀÖ°í ¿©·¯°³ÀÇ conditionÀÌ ÀÖÀ»¼ö ÀÖ´Ù´Â Àǹ̿¡¼) . ÀÏ·ÃÀÇ subgoalÀÇ ¸ðÀÓÀÌ¸ç µÎ°¡Áö Á¡¿¡¼ rule°ú ´Ù¸£´Ù. ù°´Â :- ¿¬»êÀÚ°¡ ÇÊ¿ä¾ø°í µÑ°´Â program¼öÇà½Ã ÀÚµ¿À¸·Î goalÀÌ ¼öÇàµÈ´Ù. ¼öÇàÁß subgoal ÀÌ ¸ðµÎ ¸¸Á·Çϸé programÀº ¼º°øÀûÀ¸·Î Á¾·áÇÏÁö¸¸ ÇϳªÀÇ subgoalÀÌ¶óµµ failÇϰԵǸé programÀº failµÇ¾ú´Ù°í ¸»ÇØÁø´Ù.
nowarnings % Special compiler directive; ignore for the moment
PREDICATES
nondeterm
run
CLAUSES
run:-
write("***************
Hello World Program **********************"),nl,
write("Hello
World (first)"),nl,
readchar(_).
run:-
write("Hello
World (second)"),nl,
readchar(_).
GOAL
run. /*
This is an internal goal. */
Ãâ·Â
*************** Hello World Program **********************
Hello World (first)
yes
DOMAINS ¼½¼ÇÀÌ ÇÊ¿ä¾ø´Ù. ¿Ö³ÄÇϸé À¯ÀÏÇÑ standard domain ¸¸ÀÌ »ç¿ëµÇ±â ¶§¹®ÀÌ´Ù.
PREDICATES
nondeterm
phone_number(symbol,symbol)
CLAUSES
phone_number("Albert","EZY-3665").
phone_number("Betty","555-5233").
phone_number("Carol","909-1010").
phone_number("Dorothy","438-8400").
phone_number("Kim","438-8400")
GOAL
phone_number(Who,
"438-8400").
Ãâ·Â
Who=Dorothy
Who=Kim
2 solutions
´Ù¸¥ arity (ÀμöÀÇ °¹¼ö°¡ ´Ù¸¥) ¸¦ °¡Áö¸é¼ °°Àº À̸§À» °¡Áø predicate ´Â ¿ÏÀüÈ÷ ´Ù¸£°Ô Ãë±ÞµÇ¾î¾ß ÇÑ´Ù.
DOMAINS
person
= symbol
PREDICATES
nondeterm
father(person) /*
ÀÌ person Àº father ÀÌ´Ù */
nondeterm
father(person, person) /* ÇÑ person Àº ´Ù¸¥ personÀÇ
father */
CLAUSES
father(Man):-
father(Man,_).
father(adam,seth).
father(abraham,isaac).
GOAL
father(X).
Ãâ·Â
x=adam
x=abraham
2 solutions
DOMAINS
title,author = symbol
pages =
unsigned
PREDICATES
book(title, pages)
nondeterm written_by(author,
title)
nondeterm
long_novel(title)
CLAUSES
written_by(fleming, "DR NO").
written_by(melville,
"MOBY DICK").
book("MOBY
DICK", 250).
book("DR NO", 310).
long_novel(Title):-
written_by(_, Title),
/* Àμö match¸¦ À§ÇØ ÇÁ·Î±×·¥ top->down Ž»ö
*/
book(Title,
Length), /* match½Ã free º¯¼ö¿¡ value¸¦ bind */
Length
> 300.
GOAL
long_novel(X).
Ãâ·Â
x = DR NO
1 solution
/* goalÀ» ¸¸Á·½Ã۱â À§ÇØ °¢ÀÚÀÇ subgoalÀ» ¸¸Á·½ÃÄÑ¾ß ÇÏ°í ¶ÇÇÑ °¢ Àμö¿Í matchµÇ´Â clause¸¦ ã±âÀ§ÇØ ÇÁ·Î±×·¥ÀÇ À§¿¡¼ ¾Æ·¡·Î searchÇÏ°Ô µÈ´Ù. goal°ú matchµÇ´Â clause¸¦ ãÀ¸¸é goal°ú clause°¡ identicalÇØÁöµµ·Ï free variable¿¡ °ªÀÌ bindµÈ´Ù. À̶§ goalÀº clause¿¡ unifyµÇ¾ú´Ù°í ¸»ÇØÁö°í ÀÌ·¯ÇÑ matching°úÁ¤À» unificationÀ̶ó ÇÑ´Ù */
/* Çö½Ç ¹®Á¦¸¦ Ç®¶§ ³í¸®ÀûÀÎ °á·Ð¿¡ À̸£´Â path¸¦ ã¾Æ¾ß ÇÑ´Ù. °á·ÐÀÌ Ã£´Â ´äÀ» ÁÖÁö ¸øÇÒ ¶§ ´Ù¸¥ path¸¦ ¼±ÅÃÇÏ°Ô µÈ´Ù.prolog´Â ÁÖ¾îÁø goal¿¡ ¸¸Á·ÇÏ´Â ¸ðµç ´äÀ» ã¾Æ¾ß ÇÑ´Ù. À̶§ goalÀ» ¸¸Á·½Ãų °æ¿ì´Â ´Ù¸¥ ´äÀ» ã±â À§ÇØ, ¸¸Á·½ÃŰÁö ¸øÇÒ °æ¿ì´Â ´äÀ» ã±âÀ§ÇØ database¸¦ óÀ½ºÎÅÍ Å½»öÇØ ³ª°£´Ù. À̸¦ backtrackingÀ̶óÇÏ´Â backing-up-and-trying-again method ÀÌ´Ù. goal¿¡ ´ëÇÑ ÇϳªÀÇ solutionÀ» ã±â ½ÃÀÛÇÏ¸é¼ branching spot ( = backtracking point )À» marker·Î¼ Ç¥½ÃÇϰí first subgoalÀÌ fail ÇÏ¸é ±× point·Î µÇµ¹¾Æ°¡ ´Ù¸¥ subgoalÀ» try ÇÑ´Ù */
PREDICATES
nondeterm likes(symbol,symbol)
tastes(symbol,symbol)
nondeterm food(symbol)
CLAUSES
likes(bill,X):- /* What ˼ variable
X¿¡ unified , rule ÀÇ head°¡
matchingµÊ*/
food(X),
/* ruleÀÇ body¿¡¼ ù¹øÂ° subgoal È£Ãâ(call)
*/
tastes(X,good).
/* tastes(brussels_sprouts,good)¸¦ ã±âÀ§ÇØ topÀ¸·ÎºÎÅÍ Å½»ö. callÀÌ failÇϰí ÀÚµ¿ÀûÀ¸·Î backtracking ¼öÇàµÈ´Ù */
tastes(pizza,good).
/* ù¹øÂ° subgoal ÀÇ ¸¸Á·¿©ºÎ¸¦ À§ÇØ top¿¡¼ºÎÅÍ search */
tastes(brussels_sprouts,bad).
food(brussels_sprouts).
/* callµÈ fact ´ÙÀ½¿¡ backtracking point¸¦ µÐ´Ù, X ´Â brussels_sprouts
·Î bind */
food(pizza).
/* Çѹø bindµÈ º¯¼ö´Â backtracking¿¡ ÀÇÇØ¼¸¸ free ÇÏ°Ô µÈ´Ù. X´Â pizza¿¡ bind µÇ°í tastes(pizza,good) °¡ »õ·ÎÀÌ È£Ãâ (call) µÈ´Ù. ´Ù½Ã topÀ¸·ÎºÎÅÍ Å½»öÀÌ µÇ°í match°¡ ÀÌ·ç¾îÁ® goalÀº ¼º°øÀûÀ¸·Î ¸®ÅϵȴÙ. likes rule¿¡¼ WhatÀº X¿Í unifyµÇ°í X´Â °ª pizza¿Í bind µÇ¾ú±â ¶§¹®¿¡ What Àº °ª pizza ¿Í »õ·ÎÀÌ bind µÇ¾î ´äÀ» ³½´Ù */
GOAL
likes(bill, What).
Ãâ·Â
What = pizza
1 solution
/* ruleÀÇ headºÎ match¸¦ À§ÇÑ search´Â programÀÇ top¿¡¼ºÎÅÍ ½ÃÀÛÇÑ´Ù
»õ·Î¿î callÀÌ ÀÖÀ» ¶§ ±×¿¡ matchµÇ´Â °Íµµ top¿¡¼ºÎÅÍ searchÇÑ´Ù
ÇϳªÀÇ callÀÌ ¼º°øÀûÀ¸·Î match µÇ¸é ´Ù¸¥ subgoalÀ» Â÷·Ê·Î ¼öÇàÇÑ´Ù
clause¿¡¼ Çѹø bindµÈ º¯¼ö´Â backtracking¿¡ ÀÇÇØ¼¸¸ free ÇÏ°Ô µÈ´Ù */
/* prolog´Â ¹®Á¦¿¡ ´ëÇÑ ÇϳªÀÇ solution¸¸À» ã´Â °ÍÀÌ ¾Æ´Ï°í °¡´ÉÇÑ ¸ðµç solutionµéÀ» ÀüºÎ ã´Â´Ù. ±×°ÍÀÌ backtracking ÀÇ ÀÕÁ¡ÀÌ´Ù.*/
DOMAINS
child = symbol
age =
integer
PREDICATES
nondeterm player(child,
age)
CLAUSES
player(peter,9).
player(paul,10).
player(chris,9).
player(susan,9).
GOAL
/* compound goal */
player(Person1,
9),
player(Person2,
9),
Person1
<> Person2.
/* ³ªÀ̰¡ 9»ìÀÌ¸é¼ ´Ù¸¥ »ç¶÷ÀÎ Person1 °ú Person2À» ã¾Æ¶ó */
Ãâ·Â
Person1 = peter, Person2 = chris
Person1 = peter, Person2 = susan
Person1 = chris, Person2 = peter
Person1 = chris, Person2 = susan
Person1 = susan, Person2 = peter
Person1 = susan, Person2 = chris
6 solutions
backtracking ÀÇ 4°¡Áö ±âº»¿øÄ¢
1. subgoalÀº À§¿¡¼ ¾Æ·¡·Î ¼ø¼´ë·Î ¸¸Á·µÇ¾î¾ß ÇÑ´Ù
2. predicate clause´Â ÇÁ·Î±×·¥¿¡ ³ªÅ¸³ª´Â ¼ø¼´ë·Î À§¿¡¼ ¾Æ·¡·Î testµÇ¾î¾ß ÇÑ´Ù
3. subgoalÀÌ ruleÀÇ head(°á·Ð) °ú matchµÇ¸é ´ÙÀ½¿¡ ruleÀÇ body(ÀüÁ¦)°¡ ¸¸Á·µÇ¾î¾ß ÇÑ´Ù. ±×¶§ ruleÀÇ body°¡ ¸¸Á·µÇ´Â subgoalÀÇ »õ·Î¿î ÁýÇÕÀ» ±¸¼ºÇÑ´Ù. À̰ÍÀº goal tree¸¦ ¸¸µç´Ù
4. goal treeÀÇ extremities (leaves)°¢ÀÚ¿¡ ´ëÇØ matching fact°¡ ¹ß°ßµÇ¸é ºñ·Î¼Ò goalÀÌ ¸¸Á·µÇ´Â °ÍÀÌ´Ù
DOMAINS
name,thing = symbol
PREDICATES
likes(name, thing)
reads(name)
is_inquisitive(name)
CLAUSES
likes(john,wine):-!. /* subgoal likes(X,wine)
¿Í match, X´Â john°ú bind*/
likes(lance,skiing):-!.
likes(lance,books):-!.
likes(lance,films):-!.
likes(Z,books):- reads(Z), is_inquisitive(Z).
/* likes(X,books)¿Í match,º¯¼ö Z´Â X¿Í match °¡´É, unify . ruleÀÇ body¸¦ ¸¸Á·½ÃÄѾß...»õ·Î¿î subgoalÁýÇÕÀ¸·Î tree±¸¼º*/
reads(john). /* »õ·Î¿î subgoal tree¸¦ ¸¸Á·½ÃÅ´ */
is_inquisitive(john). /* extremity °¢ÀÚ¿¡ ´ëÇÑ matching fact */
GOAL
likes(X,wine), likes(X,books)
/* 2°³ÀÇ subgoal·Î ±¸¼ºµÇ´Â goal tree */
Ãâ·Â
X = john
1 solution
PREDICATES
nondeterm type(symbol,
symbol)
nondeterm
is_a(symbol, symbol)
lives(symbol,
symbol)
nondeterm
can_swim(symbol)
CLAUSES
type(ungulate,animal). /* type(X,animal) °ú match, X´Â
ungulate¿¡ bind */
type(fish,animal). /*
backtracking point, redo : X´Â fish¿¡ bind */
is_a(zebra,ungulate).
/* is_a(Y,X) ¿Í match, Y´Â zebra¿Í bind */
is_a(herring,fish).
/* redo : is_a(Y,ungulate) ¿Í no match (fail),
Y´Â herring¿¡ bind*/
is_a(shark,fish).
/* " */ /* backtracking point, redo : Y´Â shark¿¡ bind */
lives(zebra,on_land).
lives(frog,on_land).
lives(frog,in_water).
lives(shark,in_water).
can_swim(Y):-
/*
head of rule match */
type(X,animal),
/* call */
is_a(Y,X),
/* is_a(Y,ungulate)¸¦ call, is_a(Y,fish)¸¦ call */
lives(Y,in_water).
/* lives(zebre,in_water)¸¦ call , no match (fail),
lives(herring,in_water)¸¦ call, no match,
lives(shark,in_water)¸¦
call, match */
GOAL
can_swim(What),
write("A ",What,"
can swim\n").
Ãâ·Â
A shark can swim
What = shark
1 solution
backtracking mechanismÀº ºÒÇÊ¿äÇÑ Å½»öÀ» ÇÏ¿© ºñ´É·üÀ» ÃÊ·¡ÇÒ¼ö ÀÖ´Ù. Áï ´Ü ÇϳªÀÇ (unique)´ä¸¸À» ã´Â °æ¿ì ¶óµç°¡ ¹Ý´ë·Î ¿©·¯°³ÀÇ ´äÀ» ã±âÀ§ÇØ Ãß°¡ÀûÀΠŽ»öÀ» °¿äÇÏ´Â °æ¿ì°¡ ÀÖ´Ù. À̸¦ À§Çؼ backtracking process¸¦ Á¦¾îÇÒ Çʿ䰡 ÀÖ´Ù. À̸¦ À§Çؼ´Â 2°¡Áö ¹æ¹ýÀ» »ç¿ëÇÑ´Ù.
1. backtrackingÀ» °¿ä (force)ÇÏ´Â fail predicate.
2. backtrackingÀ» ¹æÁö (prevent)ÇÏ´Â cut ( ! ).
ÇϳªÀÇ callÀÌ ½ÇÆÐÇϸé backtrackingÇÏ°Ô µÇ´Âµ¥ ¾î¶² °æ¿ì¿¡´Â ºÎ°¡ÀûÀÎ ´äÀ» ã±âÀ§Çؼ backtrackingÀ» °Á¦ÇÒ °æ¿ì°¡ ÀÖ´Ù. fail ¹®ÀåÀº failure¸¦ °Á¦ÇÏ°í ±×·³À¸·Î½á backtrackingÀ» ¾ß±âÇÑ´Ù. fail ¹®ÀåÀÇ È¿°ú´Â 2 = 3 °ú °°Àº impossible subgoal ÀÇ È¿°ú¿Í °°´Ù. ´ÙÀ½Àº fail ¹®ÀåÀÇ ¿¹ÀÌ´Ù.
DOMAINS
name = symbol
PREDICATES
nondeterm father(name,
name)
everybody
CLAUSES
father(leonard,katherine).
father(carl,jason).
father(carl,marilyn).
everybody:- /* father(X,Y) ÀÇ ¶Ç´Ù¸¥ ´äÀ» À§ÇØ
backtracking */
father(X,Y),
write(X," is
",Y,"'s father\n"),
fail.
/* °áÄÚ ¸¸Á·µÉ¼ö
¾ø´Â Ç×»ó fail, backtracking °¿ä*/
everybody. /*
fail ÀÌ ¾ø´Ù¸é backtrackÇÒ ÀÌÀ¯°¡ ¾ø´Ù */
/* fail Àº last call±îÁö backtrack ÇÑ´Ù */
GOAL
everybody. /* father(X,
Y) º¸´Ùµµ ´õ ¸íÈ®ÇÑ ´äÀ» º¸¿©ÁØ´Ù*/
/* prolog¿¡¼ ¿©·¯°³ÀÇ ´äÀ» ³¾¼ö ÀÖµµ·Ï ¸¶Áö¸· call±îÁö backtrackÇϰԵǴµ¥ ÀÌ·¯ÇÑ callÀ» non-deterministic call À̶ó ÇÑ´Ù. ´ÜÁö ÇϳªÀÇ ´ä¸¸À» ³¾¼ö ÀÖ´Â callÀ» deterministic callÀ̶ó ÇÑ´Ù*/
Ãâ·Â
leonard is katherine's father /*fail¿¡ ÀÇÇØ¼ °¡´ÉÇÑ ¸ðµç ´äÀ» ³½´Ù*/
carl is jason's father
carl is marilyn's father
yes
backtrackingÀ» ¸·±âÀ§Çؼ exclamation mark (!)¸¦ »ç¿ëÇÏ´Â °ÍÀ» cut À̶ó ÇÑ´Ù. cutÀ» °¡·ÎÁú·¯ backtrackÇÏ´Â °ÍÀº ºÒ°¡´ÉÇÏ´Ù. ruleÀÇ body¿¡ subgoalÀ» À§Ä¡½ÃŰ´Â °Í°ú °°Àº ¹æ½ÄÀ¸·Î cutÀ» À§Ä¡½ÃŲ´Ù. cut À¸·ÎÀÇ È£ÃâÀº Áï½Ã ¼º°øÇÏ°í ´ÙÀ½ subgoalÀÌ È£ÃâµÈ´Ù. ÀÏ´Ü cut ÀÌ Áö³ª°¡¸é cut ÀÌÀüÀÇ subgoal·Î backtrackÇÏ´Â °ÍÀº ºÒ°¡´ÉÇÏ´Ù. ¶ÇÇÑ cutÀ» Æ÷ÇÔÇÑ predicate¸¦ Á¤ÀÇÇϰí ÀÖ´Â ´Ù¸¥ predicate·ÎÀÇ backtrackµµ ºÒ°¡´ÉÇÏ´Ù. cut ÀÇ ¿ëµµ´Â Å©°Ô 2°¡ÁöÀÌ´Ù.
1. ÀǹÌÀÖ´Â ´äÀÌ ³ª¿Ã °¡´É¼ºÀÌ ÀüÇô ¾ø´Ù´Â °ÍÀ» ¹Ì¸® ¾Ë°í ÀÖÀ» ¶§ ´Ù¸¥ ´äÀ» ±¸ÇÏ´Â °ÍÀº ½Ã°£°ú memoryÀÇ ³¶ºñÀÌ´Ù. ÀÌ »óȲ¿¡¼ cutÀ» »ç¿ëÇϸé ÇÁ·Î±×·¥Àº ´õ »¡¸® ½ÇÇàµÇ°í memory¸¦ ´ú »ç¿ëÇÑ´Ù. À̰ÍÀ» green cut À̶ó ºÎ¸¥´Ù
2. ÇÁ·Î±×·¥ÀÇ logicÀÌ cutÀ» ¿ä±¸ÇÒ ¶§, Áï Ãß°¡ÀÇ subgoal ÀÇ °í·Á¸¦ ¹æÁöÇϱâ À§Çؼ »ç¿ëÇÑ´Ù. À̰ÍÀ» red cut À̶ó ÇÑ´Ù
r1 :- a, b, !, c.
À§ÀÇ ¹®Àå¿¡¼ subgoal a, b ¿¡ ´ëÇÑ ´äÀÌ ±¸ÇØÁøÈÄ¿¡ c ¿¡ ´ëÇÑ ´äÀÌ backtrack¿¡ ÀÇÇØ ¿©·¯°³ ±¸ÇØÁö´õ¶óµµ call a, b ¿¡ ´ëÇÑ Ãß°¡ÀûÀÎ ÇØ¸¦ ±¸Çϱâ À§ÇÑ backtrack´Â Çã¿ëµÇÁö ¾Ê´Â´Ù. ¶ÇÇÑ predicate r1À» Á¤ÀÇÇÑ ¶Ç´Ù¸¥ clause·ÎÀÇ backtrackµµ Çã¿ëµÇÁö ¾Ê´Â´Ù.
PREDICATES
buy_car(symbol,symbol)
nondeterm car(symbol,symbol,integer)
colors(symbol,symbol)
CLAUSES
buy_car(Model,Color):-
car(Model,Color,Price),
/*first subgoal*/
colors(Color,sexy),!,
/*cutÀ» ¸¸³ª´Â ¼ø°£ bindµÇ¾ú´ø º¯¼ö°ªµé µ¿°á*/
Price > 25000.
/*
test succeed*/
car(maserati,green,25000).
car(corvette,black,24000). /* match,Color
´Â black·Î bind */
car(corvette,red,26000).
/*backtrack, match, Color´Â red·Î
bind,
Price´Â 26000*/
car(porsche,red,24000).
colors(red,sexy).
/* sexy °¡ ¸ÞÄ¡µÊ*/
colors(black,mean). /* ¼±ÅõÈ
Â÷°¡ sexy colorÀÎÁö Å×½ºÆ®, fail*/
colors(green,preppy).
GOAL
buy_car(corvette,
Y).
/* À§¿¡¼ Price < 25000 À̶ó¸é test ´Â fail, ¾ÕÂÊÀÇ subgoal ·Î backtrack ÇÒ¼ö ¾ø¾î fjnal subgoal ÀÎ Price ¹®Á¦ÀÇ ÇØ°áÀ» À§ÇÑ ¹æ¹ýÀº ¾ø°í goal Àº failure ·Î ³¡³´Ù */
Ãâ·Â
Y = red
1 solution
r(1) :- !, a, b, c.
r(2) :- !, d.
r(3) :- !, c.
r(_) :- write ("This is a catchall clause.").
cutÀ» »ç¿ëÇÏ¿© predicate rÀ» deterministicÇÏ°Ô ¸¸µç´Ù. r(1), r(2), r(3), r(_) Áß¿¡¼ Çϳª¶óµµ body of ruleÀÌ ¼º°øÇϸé cutÀº backtracking point¸¦ Á¦°ÅÇÏ¿© ¶Ç´Ù¸¥ r clause·ÎÀÇ backtracking °¡´É¼ºÀ» ¾ø¾Ö¼ ´Ü ÇϳªÀÇ r¸¸ ¼öÇàµÇ°Ô ÇÑ´Ù. ´Ù¸¥ r ÀÇ ¾î¶² conditionµµ match°¡ µÇÁö ¾ÊÀ» ¶§ ¸¶Áö¸·ÀÇ error message ¹®ÀåÀÌ ¼öÇàµÈ´Ù
PREDICATES
friend(symbol,symbol)
girl(symbol)
likes(symbol,symbol)
CLAUSES
friend(bill,jane):-
girl(jane),
likes(bill,jane),!.
friend(bill,jim):-
likes(jim,baseball),!.
/* cutÀÌ ¾ø´Ù¸é ´äÀº 2°³ */
friend(bill,sue):-
girl(sue).
girl(mary).
girl(jane).
girl(sue).
likes(jim,baseball).
likes(bill,sue).
GOAL
friend(bill,Who).
Ãâ·Â
Who = jim
1 solution
/* non-deterministic clause¸¦ body of rule ¿¡ cutÀ» »ðÀÔÇÔÀ¸·Î½á deterministic clause·Î ¸¸µé¼ö ÀÖ´Ù. À§¿¡¼ friend predicate´Â Çϳª¸¦ ¸®ÅÏÇÏ¿© ¿ÀÁ÷ ÇϳªÀÇ solution¸¸À» °¡Áö¹Ç·Î deterministic ÇÏ´Ù.*/
not Àº subgoalÀÌ true¶ó°í Áõ¸íµÉ¼ö ¾øÀ» ¶§ ¼º°øÇÑ´Ù
DOMAINS
name = symbol
gpa = real
PREDICATES
nondeterm honor_student(name)
nondeterm student(name,
gpa)
probation(name)
CLAUSES
honor_student(Name):-
student(Name,
GPA),
GPA>=3.5,
not(probation(Name)).
student("Betty
Blue", 3.5).
student("David Smith",
2.0).
student("John Johnson", 3.7).
probation("Betty
Blue").
probation("David Smith").
GOAL
honor_student(X).
Ãâ·Â
X = John Johnson
1 solution
/* probation : º¸È£ °üÂûÁßÀÎ »óÅ */
not Àº ºÎÀûÀýÇÏ°Ô »ç¿ë½Ã error message¸¦ ³»°Å³ª logic»óÀÇ error¸¦ ¹ß»ý½ÃŲ´Ù. ´ÙÀ½Àº not ÀÇ ÀûÀýÇÑ »ç¿ë¿¹ÀÌ´Ù.
PREDICATES
nondeterm likes_shopping(symbol)
nondeterm has_credit_card(symbol,symbol)
bottomed_out(symbol,symbol)
CLAUSES
likes_shopping(Who):-
has_credit_card(Who,Card),
not(bottomed_out(Who,Card)),
write(Who,"
can shop with the ",Card, " credit card.\n").
has_credit_card(chris,visa).
has_credit_card(chris,diners).
has_credit_card(joe,shell).
has_credit_card(sam,mastercard).
has_credit_card(sam,citibank).
bottomed_out(chris,diners).
bottomed_out(sam,mastercard).
bottomed_out(chris,visa).
GOAL
likes_shopping(Who).
Ãâ·Â
joe can shop with the shell credit card.
Who = joe
sam can shop with the citibank credit card.
Who = sam
2 solutions
´ÙÀ½Àº backtracking ¿¡ °üÇÑ ¿¬½ÀÀÌ´Ù.
Bert ´Â motive(µ¿±â)¸¦ °¡Áö°í ÀÖ°í Èñ»ýÀÚ¿Í °°Àº stuff (À½½Ä ¶Ç´Â ¿À¹°)·Î smeared_in (´õ·´ÇôÁ³±â) ¶§¹®¿¡ À¯ÁË´Ù.
trace
DOMAINS
name,sex,occupation,object,vice,substance
= symbol
age=integer
PREDICATES
nondeterm person(name,
age, sex, occupation)
nondeterm
had_affair(name, name)
killed_with(name,
object)
killed(name)
nondeterm killer(name)
motive(vice)
smeared_in(name,
substance)
owns(name,
object)
nondeterm
operates_identically(object, object)
nondeterm
owns_probably(name, object)
nondeterm
suspect(name)
/* *
* »ìÇØ »ç°Ç°ú °ü·ÃµÈ »ç½Ç * * */
CLAUSES
person(bert,55,m,carpenter).
person(allan,25,m,football_player).
person(allan,25,m,butcher).
person(john,25,m,pickpocket).
had_affair(barbara,john).
had_affair(barbara,bert).
had_affair(susan,john).
killed_with(susan,club).
killed(susan).
motive(money).
motive(jealousy).
motive(righteousness).
smeared_in(bert,
blood).
smeared_in(susan, blood).
smeared_in(allan,
mud).
smeared_in(john, chocolate).
smeared_in(barbara,chocolate).
owns(bert,wooden_leg).
owns(john,pistol).
/* * * ¹è°æ Áö½Ä * * */
operates_identically(wooden_leg,
club).
operates_identically(bar, club).
operates_identically(pair_of_scissors,
knife).
operates_identically(football_boot, club).
owns_probably(X,football_boot):-
person(X,_,_,football_player).
owns_probably(X,pair_of_scissors):-
person(X,_,_,hairdresser).
owns_probably(X,Object):-
owns(X,Object).
/* *
* * * * * * * * * * * * * * * * * * * * *
* Susan ÀÌ »ìÇØµÉ
¸¸ÇÑ ¹«±â¸¦ °¡Áø »ç¶÷À» ÀÇ½É *
* * * * * * * * * * * * * * * * * * * * * * */
suspect(X):-
killed_with(susan,Weapon)
,
operates_identically(Object,Weapon)
,
owns_probably(X,Object).
/* *
* * * * * * * * * * * * * * * * * * * * * * * *
* Susan
°ú °ü°è¸¦ °¡Áø ³²ÀÚ¸¦ ÀÇ½É *
* * * *
* * * * * * * * * * * * * * * * * * * * * */
suspect(X):-
motive(jealousy),
person(X,_,m,_),
had_affair(susan,X).
/* *
* * * * * * * * * * * * * * * * * * *
* SusanÀ» ¾Æ´Â ¾î¶²
»ç¶÷°ú °ü°è¸¦ °¡Áø ¿©¼ºÀ» ÀÇ½É *
* * * * *
* * * * * * * * * * * * * * * */
suspect(X):-
motive(jealousy),
person(X,_,f,_),
had_affair(X,Man),
had_affair(susan,Man).
/* *
* * * * * * * * * * * * * * * * * * * * * * * * *
* µ·À»
³ë¸° ¼Ò¸ÅÄ¡±â¸¦ ÀÇ½É *
* * *
* * * * * * * * * * * * * * * * * * * * * * * */
suspect(X):-
motive(money),
person(X,_,_,pickpocket).
killer(Killer):-
person(Killer,_,_,_),
killed(Killed),
Killed <>
Killer, /* It is not a suicide */
suspect(Killer),
smeared_in(Killer,Goo),
smeared_in(Killed,Goo).
GOAL
killer(X).
Ãâ·Â
X = bert
1 solution