home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / prolog / pdprolog / cardfile.pro < prev    next >
Text File  |  1986-05-05  |  5KB  |  143 lines

  1. /* -----------------------   message    ----------------------------->
  2. CARDFILE                              January 1986
  3.  
  4. author:  George Planansky
  5.          11 Varnum St.,
  6.          Arlington, MA 02174
  7.  
  8. written using A.D.A. PD PROLOG.
  9.  
  10. This little program demonstrates how functor, arg, and assertz may be
  11. to construct, store, & access data-bearing PROLOG structures.
  12.  
  13. First, notice that lines #1 & #2 of get_book are functionally equivalent,
  14. and may be substituted for each other.  IF the predicate book had many
  15. arguments, instead of only the 2 used here, line # 1 would be more convenient
  16. and definitive than, say,  book(T,_,_,_,_,_,_,_,_,_,_,_,_,_).
  17. Clocksin & Mellish point this out while discussing the predicate arg.
  18.  
  19. Second, notice that there are basically two ways that a value, or a
  20. value-bearing structure, may be accessed in PROLOG:
  21.  
  22. 1. by being invoked, passed, and shared as an argument of structures ...
  23.    e.g., the argument Y of get_book(Y,T,A).
  24. 2. by being in a structure in the database, which structure may be matched,
  25.    deleted with retract, and emplaced with asserta or assertz.
  26.    e.g., the lack of an argument Y in alt_get_book(T,A).
  27.  
  28. Method 1 is used by the program alt_card_file,
  29. method 2 is used by card_file.
  30.  
  31. Notice that using a conjunction of the type:
  32.  
  33.           retract(Y), process(Y,Component_Yn), assertz(Y)
  34.  
  35. makes a DESTRUCTUVE reassignment to the value of Y (regarding one of its
  36. components) in the database.  PROLOG can so use its (global) constants
  37. to emulate the global variables of languages like BASIC and Pascal.
  38.  
  39. The program squares in file SQR_GAME uses the variable Gr as a "ubiquitous"
  40. variable, and so Gr is among the arguments of many of the predicates there.
  41. The structure square(Id,Si,Ad,En,Ow) there is a "global" variable.
  42.  
  43.  
  44. TO RUN:
  45.  
  46. CARDFILE.PRO and PDPROLOG.EXE must be in the active MS/PC -DOS directory.
  47.  
  48. Invoke the interpreter with the DOS command:
  49. >"PDPROLOG <Return>"
  50.  
  51. Command the interpreter to load and run cardfile:
  52. ?-"consult(cardfile). <Return>"
  53.  
  54. Note that the query ending CARDFILE.PRO instructs the interpreter to print
  55. a message suggesting the clause "card_file" as a response.
  56. <------------------------------------------------------------------- */
  57.  
  58. is_a_book(X) :- functor(X,book,2).
  59.  
  60. title(X,T) :- is_a_book(X), arg(1,X,T).
  61.  
  62. author(X,A) :- is_a_book(X), arg(2,X,A).
  63.  
  64. get_book(T,A) :-
  65.                  is_a_book(Y), title(Y,T), author(Y,A), Y,    /* #1 */
  66.                  /*  book(T,A), */                            /* #2 */
  67.                  print('\n\ntitle: ',T,' \nauthor:  ',A),
  68.                  print('\nGet more books (y/n)?>>'),
  69.                  read(Yes),
  70.                  Yes \= y,
  71.                  !, fail.
  72. get_book(T,A) :- print('Out of books'),
  73.                  !, fail.
  74.  
  75. enter_book(T,A) :- is_a_book(Y),
  76.                    title(Y,T), author(Y,A),
  77.                    assertz(Y).
  78.  
  79. card( in) :- print('\nenter title>>'),
  80.              read(T),
  81.              print('\nenter author>>'),
  82.              read(A),
  83.              enter_book(T,A),
  84.              !, fail.
  85. card(out) :- print('\nenter title>>'),
  86.              read(T),
  87.              print('\nenter author>>'),
  88.              read(A),
  89.              get_book(T,A).
  90.  
  91. card_file :- repeat,
  92.              print('\nCardfile:  enter task (in/out) >>'),
  93.              read(Task),
  94.              card(Task).
  95.  
  96. alt_get_book(Y,T,A) :- Y,
  97.                        print('\n\ntitle: ',T,' \nauthor:  ',A),
  98.                        print('\nGet more books (y/n)?>>'),
  99.                        read(Yes),
  100.                        Yes \= y,
  101.                        !, fail.
  102. alt_get_book(Y,T,A) :- print('Out of books'),
  103.                        !, fail.
  104.  
  105. alt_enter_book(Y) :- assertz(Y).
  106.  
  107. alt_card( in) :- print('\nenter title>>'),
  108.                  read(T),
  109.                  print('\nenter author>>'),
  110.                  read(A),
  111.                  is_a_book(Y),
  112.                  title(Y,T), author(Y,A),
  113.                  alt_enter_book(Y),
  114.                  !, fail.
  115. alt_card(out) :- print('\nenter title>>'),
  116.                  read(T),
  117.                  print('\nenter author>>'),
  118.                  read(A),
  119.                  is_a_book(Y), title(Y,T), author(Y,A),
  120.                  alt_get_book(Y,T,A).
  121.  
  122. alt_card_file :- repeat,
  123.                  print('\nCardfile:  enter task (in/out) >>'),
  124.                  read(Task),
  125.                  alt_card(Task).
  126. /**/
  127. ?- nl,
  128. print('\nThis is cardfile.  You can enter books into, and seek books from, the '),
  129. print('\ndatabase IN MEMORY.  Do this by choosing a task and then entering '),
  130. print('\nthe specified data.  Data entries follow a format:'),
  131. print('\n   either " onewordnoncap ", '),
  132. print('\n       or " \'two or more words\' ", '),
  133. print('\n       or " \'Onecapitalizedword\' ".'), nl,
  134. print('\nThat is, anything but a single word uncapitalized entry must be put '),
  135. print('\nin single quotes.'), nl,
  136. print('\nAlso, terminate each entry with a period and a <Return>.'),nl,
  137. print('\nSeek a book by entering KNOWN data exactly as above.'),
  138. print('\nSpecify UNKOWN or "wildcard" data with the underline key: "_".'),
  139. print('\nLibrary will attempt matches of data to the database.'),
  140. print('\n\nStart cardfile with:  ?- "card_file. <Return>" .'),
  141. print('\nYou may quit library by pressing <Esc>, and you may quit PROLOG to MS-DOS'),
  142. print('\nwith:  ?- "exitsys. <Return>"  .').
  143. /**/