home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / PROLOG_T.ZIP / PROLOG.TUT
Encoding:
Text File  |  1987-05-19  |  28.0 KB  |  934 lines

  1.              Copyright Robert Morein and Automata Design Associates
  2.  
  3.                                   1570 Arran Way
  4.                                 Dresher, Pa. 19025
  5.                                   (215)-646-4894
  6.  
  7.     ...................................................................
  8.  
  9.  
  10.                                  Prolog Tutorial          
  11.  
  12.  
  13.                                   Introduction
  14.  
  15.  
  16.  
  17.              Probably  you  have heard of the language PROLOG within  the 
  18.         last year or so. You probably wondered the following things:
  19.  
  20.         1) What does the name stand for? Names of computer languages are 
  21.         almost always acronyms.
  22.  
  23.         2) What is it good for?
  24.  
  25.         3) Why now?
  26.  
  27.         4) Can I get a copy to play with?
  28.  
  29.              Congratulations! You obviously know the answer to the fourth 
  30.         question. We now respond to the other three.
  31.              
  32.         1)  The  name  stands for "programming in logic." This  we  shall 
  33.         elaborate on in depth later on.
  34.  
  35.         2) PROLOG is good for writing question answering systems.  It  is 
  36.         also   good   for  writing  programs  that  perform   complicated 
  37.         strategies  that  compute the best or worst way to  accomplish  a 
  38.         task, or avoid an undesirable result.
  39.  
  40.         3) PROLOG was virtually unknown in this country until researchers 
  41.         in  Japan announced that it was to be the core language  of  that 
  42.         country's fifth generation computer project.  This is the project 
  43.         with  which  Japan hopes to achieve a domainant position  in  the 
  44.         world information industry of the 1990's. 
  45.  
  46.              PROLOG  is  one of the most unusual computer languages  ever 
  47.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  48.         BASIC.  The facilities complement,  rather than replace those  of 
  49.         conventional  languages.  Although  it  has great  potential  for 
  50.         database  work,  it  has  nothing in  common  with  the  database 
  51.         languages used on microcomputers.
  52.  
  53.              Perhaps  the  best point to make is that while  conventional 
  54.         languages are prescriptive, PROLOG is descriptive. A statement in 
  55.         a conventional language might read:
  56.  
  57.              if( car_wheels = TRUE ) then
  58.                begin
  59.                  (some sort of procedure)
  60.                  X = X + 1;
  61.                end 
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.         A statment in PROLOG could just be a statment of fact about  cars 
  70.         and wheels. There are many relationships that hold. For instance,
  71.  
  72.              has( car, wheels ).
  73.  
  74.              has( car, quant(wheels, four) ).
  75.  
  76.              round( wheels ).
  77.  
  78.         Each  of  these statments is an independent fact  relating  cars, 
  79.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  80.         independent, they can be put into a PROLOG program by programmers 
  81.         working separately. The man who is a specialist on car bodies can 
  82.         say  his thing,  the wheel specialist can have his say,  and  the 
  83.         participants can work with relative independence. And this brings 
  84.         to light a major advantage of PROLOG:
  85.  
  86.  
  87.                              PARALLEL PROGRAMMING!!!
  88.                             
  89.  
  90.         With  conventional  programming languages projects can  still  be 
  91.         "chunked",  or  divided between programmers.  But efficiency on a 
  92.         team  project  drops  drastically below that  of  the  individual 
  93.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  94.         participants    grows   the   need   for   communication    grows 
  95.         geometrically. The time spent communicating can exceed that spent 
  96.         programming! 
  97.              Although   PROLOG   does   not  eliminate   the   need   for 
  98.         task  coordination,  the problem is considerably  simplified.  It 
  99.         also provides the ability to answer questions in a "ready to  eat 
  100.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  101.         statements about cars and wheels previously given,  could you ask 
  102.         it the following question?   
  103.  
  104.                        
  105.               has( car, X ), round( X ).
  106.  
  107.              Does  a  car  have anything which  is  round?  The  question 
  108.         instructs the PROLOG interpreter to consider all the objects that 
  109.         it  knows are possessed by a car and find those which are  round. 
  110.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  111.         of a smart database searcher.  It can not only find the facts but 
  112.         selectively find them and interpret them.
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.              Consider the problem of a fault tree, as exemplified by this 
  136.         abbreviated one:
  137.  
  138.  
  139.  
  140.         {Car won't start}
  141.              | 
  142.              | 
  143.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  144.                 (Yes)                                       v
  145.                  |                                  {Check battery}
  146.                  |
  147.         [Smell gasoline](yes) --> {Try full throttle cranking}
  148.                  |                       (failure)
  149.         /--------/                           |
  150.  
  151.                             (details omitted)
  152.  
  153.  
  154.  
  155.              The fault tree is easily programmed in BASIC. Later we shall 
  156.         show  that  PROLOG supplies a superior replacement for the  fault 
  157.         tree.  Though the tree is capable of diagnosing only the  problem 
  158.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  159.         appropriate  tree from facts and rules you have provided.  PROLOG 
  160.         is not limited to answering one specific question.  Given  enough 
  161.         information,  it  will attempt to find all deductive solutions to 
  162.         any problem.
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                                   PROLOG PRIMER
  203.  
  204.         I.                       Rules and Facts     
  205.  
  206.  
  207.  
  208.              This  is  where you should start if you know  nothing  about 
  209.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  210.  
  211.         1)   has( car, wheels ).
  212.  
  213.         This  statement  is a "fact.  The word "has" in this statment  is 
  214.         known  either  as a functor or predicate.  It is a name  for  the 
  215.         relationship  within the parenthesis.  It implies that a car  has 
  216.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  217.         arbitrary and established by you. You could just as easily say:
  218.  
  219.         2)   has( wheels, car ).
  220.  
  221.         and if you wrote this way consistently,  all would be  well.  The 
  222.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  223.         "car" are constants. 
  224.              
  225.         A   database   of  facts  can  illustrate  the   data   retrieval 
  226.         capabilities of PROLOG. For instance:
  227.  
  228.         3)   has( car, wheels ).
  229.              has( car, frame ).
  230.              has( car, windshield ).
  231.              has( car, engine ).
  232.  
  233.         You could then ask PROLOG the question:
  234.  
  235.         4)   has( car, Part ).
  236.  
  237.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  238.         will make Part equal to whatever constant is required to make the 
  239.         question match one of the facts in the database. Thus PROLOG will 
  240.         respond:
  241.  
  242.              Part = wheels.
  243.              
  244.              More?(Y/N):
  245.  
  246.         If you type "y" the next answer will appear:
  247.  
  248.              Part = frame.
  249.  
  250.              More?(Y/N):
  251.  
  252.         If you continue, PROLOG will produce the answers Part = windshield 
  253.         and Part = engine. Finally, you will see:
  254.  
  255.              More?(Y/N):y
  256.  
  257.              No.
  258.          
  259.         indicating that PROLOG has exhausted the database.  Incidentally, 
  260.         when  a  variable is set equal to a constant or  other  variable, 
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.         it is said to be instantiated to that object.
  268.  
  269.              Notice  that  PROLOG searches the database forwards  and  in 
  270.         this case,  from the beginning.  The forward search path is built 
  271.         into PROLOG and cannot be changed. An author of a program written 
  272.         in  a  prescriptive language is quite conscious of the  order  of 
  273.         execution  of  his program,  while in PROLOG it is  not  directly 
  274.         under his control.
  275.              
  276.              The other major element is the rule which is a fact which is 
  277.         conditionally true. In logic this is called a Horn clause: 
  278.  
  279.  
  280.         5)   has( X, wheels ) :- iscar( X ).
  281.  
  282.         The  fact iscar( car ) and the above rule are equivalent to
  283.  
  284.         6)   has( car, wheels).
  285.  
  286.         The  symbol :- is the "rule sign." The expression on the left  of 
  287.         :-is the "head" and on the right is the body.  The variable X has 
  288.         scope  of the rule,  which means that it has meaning only  within 
  289.         the rule.  For instance,  we could have two rules in the database 
  290.         using identically named variables.
  291.  
  292.  
  293.         7)   has( X,  transportation ) :- 
  294.                            has( X,  car ), has( license, X ).
  295.  
  296.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  297.  
  298.         The  variables  X in the two expressions are completely  distinct 
  299.         and have nothing to do with each other.
  300.  
  301.         The comma between has( X, car ) and has( license, X ) means "and" 
  302.         or logical conjuction.  The rule will not be true unless both the 
  303.         clauses has(X, car) and has( license, X ) are true.
  304.  
  305.  
  306.              On the other hand if there is a rule
  307.              
  308.         9)   has( license, X ) :- passedexam( X ).
  309.  
  310.         consider what PROLOG will do in response to the question:
  311.  
  312.         10)  has( harry, transportation ).
  313.  
  314.         (Notice  that  harry has not been capitalized because we  do  not 
  315.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  316.         enclosed in single quotes.)
  317.  
  318.              It  will scan the database and use (7),  in which X will  be 
  319.         instantiated to harry. The rule generates two new questions:
  320.  
  321.         11)  has( harry, car ).
  322.  
  323.         12)  has( license, harry ).
  324.  
  325.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  326.         satisfied and the database is scanned for a match to (12). PROLOG 
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  334.         question is now posed:
  335.  
  336.         13)  passedexam( harry ).
  337.  
  338.              If there is a fact:
  339.  
  340.              passedexam( harry ).
  341.  
  342.         in  the database then all is well and harry  has  transportation. 
  343.         If there is not, then PROLOG will succinctly tell you:
  344.  
  345.              No.
  346.  
  347.         But  suppose Harry has money and can hire a chauffer as any  good 
  348.         programmer  can.  That  could be made part of the program in  the 
  349.         following way.
  350.  
  351.              The rule which PROLOG tried to use was:
  352.  
  353.         14)  has( X,  transportation ) :- 
  354.                            has( X,  car ), has( license, X ).
  355.  
  356.         At any point following it there could be included another rule:
  357.  
  358.         15)  has( X, transportation ) :- has( X, money ).
  359.  
  360.         or simply the bald fact:
  361.  
  362.         16)  has( harry, transportation ).
  363.  
  364.              These  additional  rules  or  facts would  be  used  in  two 
  365.         circumstances.  If at any point a rule does not yield a solution, 
  366.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  367.         applicable  one.  This process is known as "backtracking  search" 
  368.         and can be quite time consuming.
  369.  
  370.  
  371.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  372.         search  for an additional distinct solution.  It will attempt  to 
  373.         find an alternate rule or fact for the last rule or fact used. If 
  374.         that  fails,  it  will back up to the antecedent rule and try  to 
  375.         find  an alternate antecedent.  And it will continue to  back  up 
  376.         until  it  arrives at the question you asked,  at which point  it 
  377.         will say:
  378.  
  379.              No.
  380.  
  381.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  382.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  383.         question (16).
  384.  
  385.  
  386.  
  387.  
  388.         II.                          Grammar
  389.  
  390.              It is a boring subject, but it must be discussed. All PROLOG 
  391.         statements are composed of valid terms, possibly a rule sign (":-
  392.         "),  commas representing conjunction ("and"), and a period at the 
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.         very end.
  400.              A term is a structure, constant, variable, or number.
  401.          
  402.              What is a structure? It is a kind of grouping:
  403.  
  404.              1) Structures consist of a functor, and a set of objects or
  405.                 structures in parenthesis.
  406.  
  407.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  408.                 which we have not discussed yet.
  409.  
  410.              3)  A  constant or functor must be a string of  letters  and 
  411.                 numbers, beginning with a lower case letter, unless
  412.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  413.                 pardner'  ),  in  which  case you are  freed  from  these 
  414.                 restrictions.
  415.              4) A  variable  must be a string of  letters  and  numbers 
  416.                 beginning with a capital letter.
  417.              
  418.              5) A  functor  may optionally have  arguments  enclosed  in 
  419.                 parenthesis , as in: hascar( X ) or hascar. 
  420.  
  421.         An example:  "has( X, transportation )." is a structure.
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.         III.                     Input / Output      
  466.  
  467.              You   now   know  enough  to  write  simple  databases   and 
  468.         interrogate   them  profitably.   But  before  we  examine   more 
  469.         sophisticated  examples,  it  will be necessary to add input  and 
  470.         output to the language. There are built in functions which appear 
  471.         as rules which are satisfied once. Thus the statment:
  472.  
  473.              write( 'Hello world.' ).
  474.  
  475.         can be included on the right side of a rule:
  476.  
  477.  
  478.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  479.         can  also write "write( X )" where X is some variable.  Note that 
  480.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  481.         which denote a constant, are required. Double quotes would denote 
  482.         a list, which is another thing entirely.
  483.  
  484.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  485.         function  "write"  is  executed and the message  printed  to  the 
  486.         screen.
  487.              The  builtin  read( X ) reads a "structure" that  you  input 
  488.         from the keyboard. More formally, we have
  489.  
  490.              read( <variable> or <constant> )
  491.              write( <variable> or <constant> )
  492.  
  493.         If you write read( Input ),  then the variable "keyboard" will be 
  494.         assigned to whatever is typed at the keyboard,  provided that the 
  495.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  496.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  497.         is a constant,  and the user at the keyboard did not type exactly 
  498.         "baloney." 
  499.  
  500.         When you input a structure in response to a "read" statement,  be 
  501.         sure to end it with a period and an <ENTER>. 
  502.  
  503.              There  is  a convenient way of putting the cursor on  a  new 
  504.         line. This is the builtin "nl". For example:
  505.  
  506.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  507.  
  508.         would result in:
  509.  
  510.              line 1
  511.              line 2
  512.  
  513.              There  is  also a primitive form of input/output for  single 
  514.         characters. It will be discussed later.
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.         IV.                   A Fault Tree Example
  532.  
  533.              Consider the "won't start" fault tree for an automobile:
  534.  
  535.         {Car won't start}
  536.              | 
  537.              | 
  538.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  539.                 (Yes)                                       v
  540.                  |                                  {Check battery}
  541.                  |
  542.         [Smell gasoline](yes) --> {Try full throttle cranking}
  543.                  |                       (failure)
  544.         /--------/                           |
  545.         |           /------------------------/ 
  546.         |           |                       
  547.         |           |
  548.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  549.         |          (no)
  550.         |           |
  551.         |           |
  552.         |  [Check for defective carburator](yes)--\
  553.         |          (no)                           v
  554.         |                                {Repair carburator}
  555.         \----\
  556.              |
  557.              |
  558.         [Is spark present](no)-->[Do points open and close](no)-\
  559.              |                             (yes)                v
  560.         /----/                               |          {Adjust points}
  561.         |           /------------------------/
  562.         |           |
  563.         |  [Pull distributor wire, observe spark](blue)--\
  564.         |        (orange)                                v
  565.         |           |                           {Check plug wires & cap}
  566.         |           |
  567.         |  [Measure voltage on coil primary](not 12V)--\
  568.         |         (12V)                                v
  569.         |           |              {Check wiring, ballast resistor}
  570.         |           |
  571.         |  [Check condenser with ohmmeter](conducts)--\
  572.         |    (no conduction)                          v
  573.         |           |                         {Replace condenser}
  574.         |           |
  575.         |  [Open and close points](voltage not 0 - 12)--\
  576.         |   (voltage swings 0 - 12)                     v
  577.         |           |                         {Fix primary circuit}
  578.         |           |
  579.         |  {Consider hidden fault, swap components]
  580.         |
  581.         |
  582.         \-------{Call a tow truck!!}
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.              A PROLOG program to  implement this is simple. Each statment 
  598.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  599.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  600.         solution. 
  601.  
  602.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  603.                             affirm, nl,
  604.                             write( 'Check battery' ).
  605.  
  606.         'car wont start' :- write( 'Smell gasoline?' ), 
  607.                             affirm, nl,
  608.                             'fuel system'.
  609.  
  610.         'fuel system'    :- write( 'Try full throttle cranking' ).
  611.  
  612.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  613.                             affirm, nl,
  614.                             write( 'Replace fuel line.' ).
  615.  
  616.         'fuel system'    :- write( 'Check carburator' ).
  617.  
  618.         'car wont start' :- write( 'Is spark present?' ),
  619.                             not( affirm ), nl,
  620.                             'no spark'.
  621.  
  622.         'no spark'       :- write( 'Do points open and close?' ),
  623.                             not( affirm ), nl,
  624.                             write( 'Adjust or replace points.' ).
  625.  
  626.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  627.                             affirm,
  628.                             write( 'Check plug wires and cap.' ).
  629.  
  630.         'no spark'       :- write( 'What is the voltage on the primary
  631.                              of the coil: ' ), 
  632.                             read( Volts ), 
  633.                             Volts < 10,
  634.                             nl,
  635.                             write('Check wiring and ballast resistor.').
  636.  
  637.         'no spark'       :- write( 'Does the capacitor leak?' ),
  638.                             affirm,
  639.                             write( 'Replace the capacitor.' ).
  640.  
  641.         'no spark'       :- not( 'primary circuit' ).
  642.  
  643.         'primary circuit' 
  644.                          :- write( 'Open the  points.  Voltage  across 
  645.                               coil?:'), nl,
  646.                             read( Openvolts ), Openvolts < 1, 
  647.                             write(  'Close the points.  Voltage  across 
  648.                               coil?:'),
  649.                             read( Closevolts ), Closevolts > 10, nl,
  650.                             write( 'Primary circuit is OK.' ). 
  651.  
  652.         'no spark'       :- write( 'Consider a hidden fault. Swap
  653.                               cap, rotor,points,capacitor.' ).
  654.  
  655.  
  656.         'Car wont start' :- write( 'Get a tow truck!!' ).
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.                                  --End program--
  666.  
  667.  
  668.              The  above  is  a  simple example of  an  expert  system.  A 
  669.         sophisticated  system would tell you exactly the method by  which 
  670.         it  has reached a conclusion.  It would communicate by a  "shell" 
  671.         program  written  in PROLOG which would accept a wider  range  of 
  672.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  673.         interpreter directly.
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.         V.                            Lists               
  730.  
  731.              Consider  a  shopping list given you by your wife.  It is  a 
  732.         piece of paper with items written on it in an order that probably 
  733.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  734.         followed by carrots, hamburger, and finally a flea collar for the 
  735.         dog, if you can find one. In PROLOG such a list would be written:
  736.  
  737.         1)   [eggs, carrots, hamburger, fleacollar]
  738.  
  739.         The  order of a list is important so that eggs and carrots cannot 
  740.         be reversed and PROLOG be uncaring.
  741.  
  742.         Let us put the list in a structure:
  743.  
  744.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  745.  
  746.         Then  if you wished to isolate the head of the list you could ask 
  747.         the question:
  748.  
  749.              shopping( [ Mostimportant | Rest ] ).
  750.  
  751.         and PROLOG would respond:
  752.  
  753.              Mostimportant   =  eggs,   
  754.              Rest   =   [carrots,   hamburger, fleacollar].
  755.  
  756.         The vertical bar "|" is crucial here. It is the string extraction 
  757.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  758.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  759.         separate the head of the list from the rest, or tail.
  760.  
  761.  
  762.              You  may have gained the impression that PROLOG is a  rather 
  763.         static language capable of answering simple questions,  but it is 
  764.         far  more powerful than that.  The string extraction operator  is 
  765.         the  key.  It permits PROLOG to whittle a complex expression down 
  766.         to the bare remainder.  If the rules you have given it permit  it 
  767.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  768.         achieved. An example of this is the definition of "append."
  769.  
  770.              Let  us suppose you have not yet done yesterday's  shopping, 
  771.         let alone today's. You pull it out of your wallet and sootch tape 
  772.         it to the list your wife just gave you. Yesterday's list was:
  773.  
  774.              [tomatoes, onions, ketchup]
  775.  
  776.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  777.  
  778.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  779.  
  780.         To  take one list and to attach it to the tail of another list is 
  781.         to  "append"  the first to the second.  The PROLOG definition  of 
  782.         append is:
  783.  
  784.  
  785.  
  786.         Rule1:     append( [], L, L ).
  787.  
  788.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.                       append( List1, List2, List3 ].
  796.  
  797.         The  general  scheme is this:  
  798.  
  799.         The definition consists of one rule and one fact.  The rule  will 
  800.         be used over and over again until what little is left matches the 
  801.         fact.  The [] stands for empty list,  which is like a bag without 
  802.         anything in it. This is an example of a recursive definition.
  803.              Suppose we ask:
  804.  
  805.              append( [a,b,c], [d,e,f], Whatgives ).
  806.  
  807.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  808.         2. Rule 2 is invoked again with arguments:
  809.              ( [b,c], [d,e,f], List3 ).
  810.         3. Rule 2 is invoked again with arguments:
  811.              ( [b], [d,e,f], List3 ).
  812.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  813.             matches. End.
  814.  
  815.         How does this cause a list to be constructed? The key is to watch 
  816.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  817.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  818.         2. Now lets trace this as rule two is successivly invoked: 
  819.  
  820.  
  821.                 Whatgives                                                
  822.                    |                                                     
  823.                    |                                                     
  824.                    |                                                     
  825.                    v                                                     
  826.         Rule2:  [X|List3] (List1 = [b,c])                                
  827.                  |  \                                                    
  828.                  |   \                                                   
  829.                  |    \                                                  
  830.                  v     \                                                 
  831.         Rule2:   a   [X'|List3'] (List1' = [c])                          
  832.                       |    \                                             
  833.                       |     \                                            
  834.                       |      \                                           
  835.                       v       \                                          
  836.         Rule2:        b     [X''|List3''] (List1'' = [], ie., empty set.)
  837.                               |    \                                     
  838.                               |     \                                    
  839.                               |      \                                   
  840.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  841.                                                                          
  842.         End.
  843.  
  844.  
  845.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  846.         2 never alters List2. It supplies it to whatever rule it invokes. 
  847.         So L in rule 1 is the original List2, or [a,b,c].
  848.  
  849.              This example would not have worked if the order of rules one 
  850.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  851.         attempts to use the the first rule encountered. You could imagine 
  852.         it as always reading your program from the top down in attempting 
  853.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  854.         an  unpleasant  thing  would  have happened  if  the  order  were 
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.         reversed. The program would loop forever.
  862.              
  863.  
  864.  
  865.  
  866.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  867.         appetite. You should now purchase the book
  868.  
  869.              Programming In Prolog
  870.              W.F. Clocksin and C.S. Mellish
  871.              Springer - Verlag
  872.              Berlin,Heidelberg,New York. 1981,1984
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926. ve gained the impression that PROLOG is a  rather 
  927.         static language capable of answering simple questions,  but it is 
  928.         far  more powerful than that.  The string extraction operator  is 
  929.         the  key.  It permits PROLOG to whittle a complex expression down 
  930.         to the bare remainder.  If the rules you have given it permit  it 
  931.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  932.         achieved. An example of this is the definition of "append."
  933.  
  934.              Let  us suppose you