際際滷

際際滷Share a Scribd company logo
Introduction to
Prolog Programming

                                     2010.10.02
            ???(http://kazuya.springnote.com)
       ???(http://cafe.naver.com/architect1.cafe)
Table of Contents
 ?   ???? ??
 ?   ?? ?? ??
 ?   ???? ??
 ?   ???? ???? with Demo
 ?   ?? : ????? ?? ??
???? ??
? ????? ???? ?? ??? ????.
 C ??? ???? ??~~~ ???
 C C ? ??, ???(=), ???(if), ???(for) ???
 C ???? ?? ?? ???? ?? ? ? ?? ?? ??
?? ?? ??
? SWI Prolog for Windows
   C http://www.swi-prolog.org
   C ??? ????.




? ??? ??? ??
   C http://en.wikipedia.org/wiki/Comparison_of_Prolog_implementatio
     ns
???? ??
? prologue?
  C ???! Prolog. Programming in Logic ? ??


? ?? ??
  C ????? Prolog ? ?? ????? ??? ? ?? ?? ??
  C ???? ????? ????


? ???
  C Symbolic Programming
  C Declarative Programming
  C Logic Programming
  ? ?? ???? ? ????.
???? ??
? Symbolic Programming
  C vs. Numeric Computation
  C ????? ??? ? ???? ??? ?? ??. ??? ??? ?? ??
  C ?? ?? ?? ???? ?? ???? ???? ?? ?? ???? ??
    ? ??????? ??? ? ???? ??? ????
? Declarative Programming
  C vs. Procedural Programming
  C ???? ??? ???? ?? ????? ??? ??????, ?? ?
    ?? ?? ?? ?? ?? ??? ???? ???, ????? ??? ?
    ? ??? ? ?? ??? ??? ?? ?? ? ??
? Logic Programming
  C vs. Functional Programming
  C ? ???, ??? ????? ??? ????
???? ????
? ???? ?? ???? ???? ?,
                         pam         tom
? ?? ??? ?? ?? ????
  ????? ??? ??
  C   bob ? ????
  C   pat ? ?????              bob         liz
  C   ... ? ??????
  C   ... ? ????
  C   ... ? ????         ann         pat
  C   ... ? ????
  C   ... ? ????
                               jim
???? ????
? ???? #1
  parent(pam,bob). % pam is parent of bob. pam         tom
  parent(tom,bob).
  parent(tom,liz).
  parent(bob,ann).                               bob         liz
  parent(bob,pat).
  parent(pat,jim).
                                          ann          pat
? ???
  ?-   parent(bob,pat).
  ?-   parent(liz,pat).                          jim

  ?-   parent(tom,ben).
  ?-   parent(X,liz).
???? ?? ??
? clause
   C ???? ???.
   C parent(pam,bob). % parent ? relation ??? ??.
? atom
   C ???? ??
   C parent, pam, bob
? variable
   C ???? ??
   C X, Y
? goals
   C ???? ??
   C ?? ??? clause ? ???? goal? ???? ?? ???.
???? ????
? ???? #2
  C   female(pam). % pam is female.               pam         tom
  C   male(tom).
  C   male(bob).
  C   female(liz).                                      bob         liz
  C   female(ann).
  C   female(pat).
  C   male(jim).                                  ann         pat
  C   offstring(X,Y) :- parent(Y,X).
        % If Y is parent of X, X is offspring of Y.
                                                        jim
???? ?? ??
? rule
   C offspring(X,Y) :- parent(Y,X).
   C 'head' :- 'body'.

   C P :- Q, R. % If Q and R then P.
   C P :- Q; R. % If Q or R, then P.
???? ????
? ???? #3
  C mother(X,Y) :-                        pam         tom
              parent(X,Y), female(X).
  C grandparent(X,Z) :-
              parent(X,Y), parent(Y,Z).         bob         liz
  C sister(X,Y) :-
              parent(Z,X),
              parent(Z,Y),                ann         pat
              female(X).


                                                jim
???? ????
? ???? #4
  C predecessor(X,Z) :-
             parent(X,Z).
  C predecessor(X,Z) :-
             parent(X,Y),
             predecessor(Y,Z).
  C haschid(X) :- parent( X, _ ).
???? ?? ??
? List
   C   [   ]
   C   [   A ] = .( A, [] )
   C   [   A, B, C ] = .( A, .( B, .( C, [] ) ) )
   C   [   Head | Tail ]
   C   [   A, B, C ] = [ A | [ B, C ] ] = [ A, B | [C] ]
????? ??
? Facts
   C 5?? ?? ?? ??? ?? ??
   C ? ??? ?? ??? ??? ??
   C ? ????? ?? ???? ???, ?? ??? ??, ?? ?????
     ???
? Details
   C   ???? ?? ?? ??
   C   ????? ?? ???
   C   ????? ??? ???
   C   ???? ??? ?? ??? ??
   C   ??? ??? ??? ???
   C   ?? ??? ?? ??? ?? ???
   C   ???? ??? ?? ??? ???
????? ??
? Details
   C   ???? ?? ??? ??? ???
   C   ?????? ??? ?? ??
   C   ??? ??? ??? ??? ???? ??? ?? ??? ??
   C   ?? ??? ??? ?? ??? ??? ?? ??? ??
   C   ????? ??? ??? ??? ??? ???
   C   ???? ??? ??? ???
   C   ?????? ???? ??? ??
   C   ??? ??? ??? ??? ?? ??? ?? ??? ??



? ??. ???? ??? ??? ?????
   C ???...
????? ??
? ??? ??
   C house( Color, Nationality, Beverage, Cigarette, Pet ).
   C [ House1, House2, ... , House5 ]


? Relation
   C   member( Item, [ Item | _ ] ).
   C   member( Item, [ _ | Rest ] ) :- member( Item, Rest).
   C   order( A, B, [ A, B | _ ] ).
   C   order( A, B, [ _ | Tail ] ) :- order( A, B, Tail ).
   C   near( A, B, List ) :- order( A, B, List) ; order( B, A, List ).
????? ??
? ?? ?? ??
  C ???? ?? ??? ??? ???
  C ?????? ??? ?? ??
  C position(
     [ house(_, norway, _, _, _),
       house(_, _, _, _, _),
       house(_, _, milk, _, _),
       house(_, _, _, _, _),
       house(_, _, _, _, _) ] ).
????? ??
? ?? ??? ?? ??
  C   ???? ?? ?? ??
  C   member(house(red, england, _, _, _), HouseList).
  C   member(house(_, sweden, _, _, dog), HouseList).
  C   member(house(_, denmark, tea, _, _), HouseList).
  C   member(house(green, _, coffee, _, _), HouseList).
  C   member(house(_, _, _, pallmall, bird), HouseList).
  C   member(house(yellow, _, _, dunhill, _), HouseList).
  C   member(house(_, _, beer, bluemaster, _), HouseList).
  C   member(house(_, german, _, prince, _), HouseList).
????? ??
? ?? ??
  C   order(house(green, _, _, _, _), house(white, _, _, _, _), List).
  C   near(house(_, _, _, blend, _), house(_, _, _, _, cat), List).
  C   near(house(_, _, _, _, horse), house(_, _, _, dunhill, _), List).
  C   near(house(_, norway, _, _, _), house(blue, _, _, _, _), List).
  C   near(house(_, _, _, blend, _), house(_, _, water, _, _), List).
????? ??
????? ??
? ?? ???? ?? ????
????
? Prolog Programming for Artificial Intelligence

? http://nlp.kaist.ac.kr/~cs370/
   C Symbolic Programming Course


? http://en.wikipedia.org/wiki/Prolog
? http://en.wikipedia.org/wiki/Einstein's_Puzzle
   C a.k.a "Zebra Puzzle"
?
QnA

More Related Content

Introduction to prolog

  • 1. Introduction to Prolog Programming 2010.10.02 ???(http://kazuya.springnote.com) ???(http://cafe.naver.com/architect1.cafe)
  • 2. Table of Contents ? ???? ?? ? ?? ?? ?? ? ???? ?? ? ???? ???? with Demo ? ?? : ????? ?? ??
  • 3. ???? ?? ? ????? ???? ?? ??? ????. C ??? ???? ??~~~ ??? C C ? ??, ???(=), ???(if), ???(for) ??? C ???? ?? ?? ???? ?? ? ? ?? ?? ??
  • 4. ?? ?? ?? ? SWI Prolog for Windows C http://www.swi-prolog.org C ??? ????. ? ??? ??? ?? C http://en.wikipedia.org/wiki/Comparison_of_Prolog_implementatio ns
  • 5. ???? ?? ? prologue? C ???! Prolog. Programming in Logic ? ?? ? ?? ?? C ????? Prolog ? ?? ????? ??? ? ?? ?? ?? C ???? ????? ???? ? ??? C Symbolic Programming C Declarative Programming C Logic Programming ? ?? ???? ? ????.
  • 6. ???? ?? ? Symbolic Programming C vs. Numeric Computation C ????? ??? ? ???? ??? ?? ??. ??? ??? ?? ?? C ?? ?? ?? ???? ?? ???? ???? ?? ?? ???? ?? ? ??????? ??? ? ???? ??? ???? ? Declarative Programming C vs. Procedural Programming C ???? ??? ???? ?? ????? ??? ??????, ?? ? ?? ?? ?? ?? ?? ??? ???? ???, ????? ??? ? ? ??? ? ?? ??? ??? ?? ?? ? ?? ? Logic Programming C vs. Functional Programming C ? ???, ??? ????? ??? ????
  • 7. ???? ???? ? ???? ?? ???? ???? ?, pam tom ? ?? ??? ?? ?? ???? ????? ??? ?? C bob ? ???? C pat ? ????? bob liz C ... ? ?????? C ... ? ???? C ... ? ???? ann pat C ... ? ???? C ... ? ???? jim
  • 8. ???? ???? ? ???? #1 parent(pam,bob). % pam is parent of bob. pam tom parent(tom,bob). parent(tom,liz). parent(bob,ann). bob liz parent(bob,pat). parent(pat,jim). ann pat ? ??? ?- parent(bob,pat). ?- parent(liz,pat). jim ?- parent(tom,ben). ?- parent(X,liz).
  • 9. ???? ?? ?? ? clause C ???? ???. C parent(pam,bob). % parent ? relation ??? ??. ? atom C ???? ?? C parent, pam, bob ? variable C ???? ?? C X, Y ? goals C ???? ?? C ?? ??? clause ? ???? goal? ???? ?? ???.
  • 10. ???? ???? ? ???? #2 C female(pam). % pam is female. pam tom C male(tom). C male(bob). C female(liz). bob liz C female(ann). C female(pat). C male(jim). ann pat C offstring(X,Y) :- parent(Y,X). % If Y is parent of X, X is offspring of Y. jim
  • 11. ???? ?? ?? ? rule C offspring(X,Y) :- parent(Y,X). C 'head' :- 'body'. C P :- Q, R. % If Q and R then P. C P :- Q; R. % If Q or R, then P.
  • 12. ???? ???? ? ???? #3 C mother(X,Y) :- pam tom parent(X,Y), female(X). C grandparent(X,Z) :- parent(X,Y), parent(Y,Z). bob liz C sister(X,Y) :- parent(Z,X), parent(Z,Y), ann pat female(X). jim
  • 13. ???? ???? ? ???? #4 C predecessor(X,Z) :- parent(X,Z). C predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z). C haschid(X) :- parent( X, _ ).
  • 14. ???? ?? ?? ? List C [ ] C [ A ] = .( A, [] ) C [ A, B, C ] = .( A, .( B, .( C, [] ) ) ) C [ Head | Tail ] C [ A, B, C ] = [ A | [ B, C ] ] = [ A, B | [C] ]
  • 15. ????? ?? ? Facts C 5?? ?? ?? ??? ?? ?? C ? ??? ?? ??? ??? ?? C ? ????? ?? ???? ???, ?? ??? ??, ?? ????? ??? ? Details C ???? ?? ?? ?? C ????? ?? ??? C ????? ??? ??? C ???? ??? ?? ??? ?? C ??? ??? ??? ??? C ?? ??? ?? ??? ?? ??? C ???? ??? ?? ??? ???
  • 16. ????? ?? ? Details C ???? ?? ??? ??? ??? C ?????? ??? ?? ?? C ??? ??? ??? ??? ???? ??? ?? ??? ?? C ?? ??? ??? ?? ??? ??? ?? ??? ?? C ????? ??? ??? ??? ??? ??? C ???? ??? ??? ??? C ?????? ???? ??? ?? C ??? ??? ??? ??? ?? ??? ?? ??? ?? ? ??. ???? ??? ??? ????? C ???...
  • 17. ????? ?? ? ??? ?? C house( Color, Nationality, Beverage, Cigarette, Pet ). C [ House1, House2, ... , House5 ] ? Relation C member( Item, [ Item | _ ] ). C member( Item, [ _ | Rest ] ) :- member( Item, Rest). C order( A, B, [ A, B | _ ] ). C order( A, B, [ _ | Tail ] ) :- order( A, B, Tail ). C near( A, B, List ) :- order( A, B, List) ; order( B, A, List ).
  • 18. ????? ?? ? ?? ?? ?? C ???? ?? ??? ??? ??? C ?????? ??? ?? ?? C position( [ house(_, norway, _, _, _), house(_, _, _, _, _), house(_, _, milk, _, _), house(_, _, _, _, _), house(_, _, _, _, _) ] ).
  • 19. ????? ?? ? ?? ??? ?? ?? C ???? ?? ?? ?? C member(house(red, england, _, _, _), HouseList). C member(house(_, sweden, _, _, dog), HouseList). C member(house(_, denmark, tea, _, _), HouseList). C member(house(green, _, coffee, _, _), HouseList). C member(house(_, _, _, pallmall, bird), HouseList). C member(house(yellow, _, _, dunhill, _), HouseList). C member(house(_, _, beer, bluemaster, _), HouseList). C member(house(_, german, _, prince, _), HouseList).
  • 20. ????? ?? ? ?? ?? C order(house(green, _, _, _, _), house(white, _, _, _, _), List). C near(house(_, _, _, blend, _), house(_, _, _, _, cat), List). C near(house(_, _, _, _, horse), house(_, _, _, dunhill, _), List). C near(house(_, norway, _, _, _), house(blue, _, _, _, _), List). C near(house(_, _, _, blend, _), house(_, _, water, _, _), List).
  • 22. ????? ?? ? ?? ???? ?? ????
  • 23. ???? ? Prolog Programming for Artificial Intelligence ? http://nlp.kaist.ac.kr/~cs370/ C Symbolic Programming Course ? http://en.wikipedia.org/wiki/Prolog ? http://en.wikipedia.org/wiki/Einstein's_Puzzle C a.k.a "Zebra Puzzle"
  • 24. ? QnA