home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / pdpro / expert / rules. < prev   
Encoding:
Text File  |  1994-02-22  |  219.5 KB  |  7,478 lines

  1.  
  2.                   NYACC MEMBERSHIP APPLICATION
  3.               New York Amateur Computer CLub, Inc.
  4.                            PO Box 106
  5.             Church Street Station, New York NY 10008
  6.  
  7.  
  8.          [ ] New Member    [ ] Renewal    Dues are $15.00
  9.                Please make checks payable to NYACC
  10.  
  11. First name:__________________  Last name: _______________________
  12.  
  13. Mailing Address: ________________________________________________
  14.  
  15. City: ___________________ State: _____ Zip: ______ Country: _____
  16.  
  17. Business phone: (___) ___-____          Home phone: (___)___-____
  18.  
  19. What topics would you like covered at club meetings:
  20.  
  21.  
  22.  
  23. Would you like to speak at a club meeting? [ ] yes [ ] no
  24. Topic(s):
  25.  
  26.  
  27.  
  28. Can you help the club? [ ] general   [ ] newsletter  [ ] flea market
  29. [ ] meetings  [ ] user group
  30.  
  31.  
  32.  
  33. Comments:
  34.  
  35.  
  36.  
  37. MZ[ß  ┤!brÇ═-?dâ╛╓F╤·· 0HX┐[¿ 
  38.          
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.                            AUTOMATA DESIGN ASSOCIATES
  52.  
  53.  
  54.  
  55.                                                           1570 Arran Way
  56.                                                           Dresher, Pa.
  57.                                                           19025
  58.                                                           (215)-646-4894
  59.                                                           December 14,1985
  60.         Dear PD user:
  61.  
  62.              Here's version 1.8 of PD PROLOG. It's better, of course. And 
  63.         now I must ask a favor of you.
  64.  
  65.              The   AAAI   fifth   national   conference   on   artificial 
  66.         intelligence is on August 11 to 15th, 1986 in Philadelphia. Since 
  67.         I live next to Philly,  I'd love to exhibit there. The problem is 
  68.         that  the rate is $2200 for a 10' x 10' booth,  with a $600  rate 
  69.         for publishers.
  70.  
  71.              I  am  appealing to the coordinator of the  conference,  Ms. 
  72.         Claudia   Mazzetti,   for  a  publishers  rate  break  with   the 
  73.         justification  of my contribution to the public  domain.  I  have 
  74.         sent  to Ms.  Mazzetti a financial statement which shows  clearly 
  75.         that  I  cannot afford the standard rate.  Ms.  Mazzetti was  not 
  76.         particularly   impressed  with  my  request,   but  perhaps  some 
  77.         substantiation of my contribution will change her mind.
  78.  
  79.              I  therefore  request that you  write  Ms.  Mazzetti  before 
  80.         February  at the below address,  and inform her of what I've done 
  81.         for mankind, the Byte readership, etc:
  82.  
  83.                               Ms. Claudia Mazzetti
  84.                               Director, AAAI-86
  85.                               445 Burgess Drive
  86.                               Menlo Park, CA 94025-3496
  87.  
  88.                                 Sincerely yours,
  89.  
  90.  
  91.                                    Bob Morein
  92.                               Author, A.D.A. PROLOG
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.                      A.D.A PROLOG Documentation Version 1.80
  110.                   for the Educational and Public Domain Versions
  111.  
  112.                                   December 14, 1985
  113.  
  114.               Copyright Robert Morein and Automata Design Associates
  115.  
  116.                                  1570 Arran Way
  117.                                Dresher, Pa. 19025
  118.  
  119.                                  (215)-646-4894
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.                                       News
  171.  
  172.              Release 1.80 is further debugged. There are no new features. 
  173.         However,  there  are  some new sample programs by  Tom  Sullivan. 
  174.  
  175.              There  is  also a very interesting natural language  parsing 
  176.         system by Lou Schumacher of Future Dimensions in directory "ATN". 
  177.         Lou's system, known as an augmented transition network parser, is 
  178.         very well documented,  since the paper he wrote on the subject is 
  179.         also included.  You'll need Volkswriter to print it out, but it's 
  180.         readable as is.  The system requires ED PROLOG to run,  since  it 
  181.         makes use of the grammar rule syntax.
  182.  
  183.              Simon   Blackwell's   "PIE"  Truth  Maintenance  System   is 
  184.         presented in revised, debugged, and enlarged form. This system is 
  185.         found  in  the  directory  "expert"  and  augments  the  strictly 
  186.         deductive  capabilities  of raw Prolog with additional  forms  of 
  187.         reasoning.  PIE  has a syntax that resembles colloquial  English. 
  188.         Wait till you see the backwards quote marks!
  189.  
  190.              The predicates "batch" and "nobatch" are introduced to allow 
  191.         execution  of batch files without confusing messages and  prompts 
  192.         appearing on the screen. I've put one in Simon's "KOPS" file.
  193.  
  194.  
  195.  
  196.                                 Copyright Notice
  197.  
  198.              The  public domain PD PROLOG system has been contributed  to 
  199.         the  public domain for unrestricted use with one  exception:  the 
  200.         object  code  may not be  disassembled  or  modified.  Electronic 
  201.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  202.         software the widest possible distribution.
  203.  
  204.              This documentation may be reproduced freely,  but it may not 
  205.         be  included in any other documentation without the permission of 
  206.         the author.
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.                                   Introduction
  237.                 
  238.         We  are pleased to present the third major version of PD  PROLOG, 
  239.         version 1.7.  Version 1.7 continues to refine "problems" and adds 
  240.         the  entertaining  feature of IBM PC video  screen  support.  The 
  241.         memory requirements are somewhat greater than the original, since 
  242.         it   is  uses  the  large  memory  model.   It   compensates   in 
  243.         thoroughness.  The memory requirement is about 210K bytes of TPA, 
  244.         and it will benefit from up to 253k bytes. The availalble 
  245.         workspace is 100K bytes.
  246.  
  247.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  248.         afford  you exposure to THE fifth generation language at the cost 
  249.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  250.         explicable:  We  want you to think of Automata Design  Associates 
  251.         for  fifth  generation  software.  It also gives us a  nice  warm 
  252.         feeling.
  253.  
  254.         The  minimum  memory  requirement is 200 k of  transient  program 
  255.         area,  plus  whatever  space is needed to execute  programs  from 
  256.         within PROLOG.  DOS or MSDOS 2.0 are required.  The program  does 
  257.         not  require  IBM PC compatibility to run,  although  the  screen 
  258.         access routines do require compatibility.
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.                      Products by Automata Design Associates
  303.  
  304.              Automata  Design  Associates  specializes  in  software  for 
  305.         artificial  intelligence  and  robotic  applications.   A  PROLOG 
  306.         language  system is available in various configurations.  A  LISP  
  307.         interpreter will be introduced in March of 1985.
  308.  
  309.  
  310.              There  are five versions of PROLOG available  from  Automata 
  311.         Design  Associates.  All  of  them run under the MSDOS  or  PCDOS 
  312.         operating systems. Other environments will be supported soon.
  313.  
  314.  
  315.              .Public Domain PROLOG
  316.  
  317.         This  serves to further the general awareness of the public about 
  318.         PROLOG.  It  also is an excellent adjunct to anyone learning  the 
  319.         language.  Most  of  the core PROLOG described  by  Clocksin  and 
  320.         Mellish  in  the book Programming In  PROLOG  is  implemented.  A 
  321.         complete  IBM PC video screen support library is included in this 
  322.         and  all other A.D.A.  prologs.  Trace predicates are  not.  This 
  323.         version is available from us for $10.00 postage paid.
  324.  
  325.  
  326.              .Educational PROLOG
  327.  
  328.         At  extremely modest cost this affords an educational institution 
  329.         or  individual  a  PROLOG  system  which  provides  the   maximum 
  330.         available  programming  area  available  within  the  8086  small 
  331.         programming model.  Tracing,  a debugging aid,  allows monitoring 
  332.         a program as it runs.  User settable spy points selectively allow 
  333.         this.  Exhaustive  tracing  is also  available.  I/O  redirection 
  334.         gives some file ability.
  335.  
  336.              An  "exec"  function allows the execution of  a  program  or 
  337.         editor  from  within  PROLOG,  thus  encouraging  an  interactive 
  338.         environment.
  339.  
  340.              An  "interrupt"   menu is added,  permitting the control  of 
  341.         tracing, toggling the printer, and screen printing.
  342.  
  343.              Definite clause grammar support is now included.
  344.              
  345.              The cost of Educational PROLOG is $29.95.
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.              .FS PROLOG
  369.  
  370.              A  small  increment  in price adds full random  access  file 
  371.         capability. Character and structure I/O are allowed.
  372.  
  373.              The  "asserta and "assertz" predicates are expanded and work 
  374.         with a clause indexing ability.  One can assert clauses  anywhere 
  375.         in the database under precise pattern matching control. 
  376.  
  377.              A tree structured lexical scoping system and floating  point 
  378.         arithmetic are other enhancements.
  379.  
  380.              The cost of FSM PROLOG is $49.95
  381.  
  382.  
  383.  
  384.              .VMI PROLOG -- Virtual Memory (Replaces type VMS)
  385.  
  386.  
  387.              At reasonable cost the addition of virtual memory gives  an 
  388.         expansion of capabilities of an order of magnitude. 
  389.  
  390.              The  database on disk is treated transparently.  No  special 
  391.         provisions  need  be  made  by the  user.  Virtual  and  resident 
  392.         databases  may be mixed.  A unique updating algorithim  preserves 
  393.         the format of the database as typed by the user while making only 
  394.         those changes necessary to make it equivalent to the database  in 
  395.         central memory.
  396.  
  397.              The cost of VMI PROLOG is $99.95
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.              .VML PROLOG Large model Virtual Memory System
  435.  
  436.              A.D.A.  PROLOG is a remarkable fifth generation developement 
  437.         tool  for  the  implementation  of  intelligent  strategies   and 
  438.         optimized  control.  It  is both the kernel for  applications  of 
  439.         virtually  unlimited scope and a sophisticated developement  tool 
  440.         that multiplies the productivity of the programmer many times. 
  441.  
  442.              With  a  cost/performance ratio exceeding that of any  other 
  443.         product  and  a  compatibility  insured  by  compliance  to   the 
  444.         Edinburgh syntax, performance is enhanced by numerous extensions, 
  445.         many of them invisible to the user.
  446.  
  447.              A quick overview of some of the features discloses:
  448.  
  449.              1)  Invisible  compilation  to  a  semantic  network 
  450.              preserves the flexibility of the interpreted mode and 
  451.              the speed of a compiler.
  452.  
  453.              The  programmer can compile and recompile any portion 
  454.              of a module at any time.  The edit/compile/test cycle 
  455.              is short and free of strain. An interface is provided 
  456.              to an editor of choice.
  457.  
  458.  
  459.              2) Floating point arithmetic with a full  complement 
  460.              of  input  and  output  methods,  transcendental  and 
  461.              conversion functions.
  462.  
  463.  
  464.              3)  Virtual  memory.  Module  size  and  number  are 
  465.              unrestricted,   with  a  total  capacity  of  several 
  466.              hundred megabytes.  Resident and virtual modules  may 
  467.              be co-resident. Compilation is incremental. The cache 
  468.              algorithim  is  sophisticated.  Changes made  in  the 
  469.              database can be updated to disk by a single command.
  470.  
  471.  
  472.              4) A powerful exec function and acceptance of  stream 
  473.              input make  integration into  applications practical.
  474.  
  475.  
  476.              5)  Global polymorphic variables  retain  information 
  477.              that  would  otherwise  require  the  "assertion"  of 
  478.              facts.
  479.  
  480.  
  481.              6)  A  quoted  variable class,  borrowed  from  LISP, 
  482.              permits  referencing variables as objects as well  as 
  483.              by value.
  484.  
  485.              7) Multidimensional arrays,  dynamically created  and 
  486.              destroyed,  efficiently  store numeric and nonnumeric 
  487.              structures. Arrays are ideal for representing spatial 
  488.              and ordinal relationships.
  489.              8)  Debugging facilities let you see your program run 
  490.              without any additional generation steps.
  491.  
  492.  
  493.              9)   Totally   invisible  and   incremental   garbage 
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.              collection.   There   is  NEVER  any  wait  for  this 
  501.              function.
  502.  
  503.  
  504.              10)  A  tree  structured,   dynamically  configurable 
  505.              lexical scoping system. The work of many  programmers 
  506.              can  be coupled together in the form of libraries and 
  507.              nested domains. 
  508.  
  509.              Each lexically scoped domain is a hidden space  which 
  510.              communicates  with the parent domain via exports  and 
  511.              imports. Domains can be linked together under program 
  512.              control to achieve any desired configuration.
  513.  
  514.  
  515.              11) The Grammar Rule Notation is an integral feature.
  516.  
  517.  
  518.              12) Keyword redefinition makes porting code easy.
  519.  
  520.  
  521.         The  cost  of this system is $200 for the MSDOS version.
  522.  
  523.  
  524.  
  525.         .VMA PROLOG Large model Virtual Memory System
  526.  
  527.         This  system  has  additional forms of  virtual  memory.  It  was 
  528.         intended  for  deep  reasoning  problems.  Contact  us  for  more 
  529.         information.
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.                                  Upgrade Policy
  567.  
  568.              Half the cost of any A.D.A. PROLOG system may be credited to 
  569.         the purchase of a higher level version.      The full cost of VMS 
  570.         prolog  may  be  applied to the purchase of VMI  or  VML  PROLOG. 
  571.         Updates to a particular level product vary from $15.00 to $35.00.
  572.  
  573.  
  574.                                 Run-time Packages
  575.  
  576.              Software  developers wishing to integrate an A.D.A.  product 
  577.         into  their  system  should inquire  about  specialized  run-time 
  578.         packages available at reasonable cost.
  579.  
  580.  
  581.  
  582.                               Technical Information
  583.  
  584.              Technical information may be obtained at (215) - 646- 4894
  585.  
  586.              Perhaps we can answer the following questions in advance:
  587.  
  588.              There is no support for:  APPLE II, Atari, Commodore, or  
  589.         CPM 80 .  Other machines from these manufactures may be supported 
  590.         in the future.
  591.  
  592.              The MSDOS products are available on 5" and 8" diskettes.
  593.  
  594.  
  595.  
  596.                               To Place Your Order:
  597.  
  598.              You may place your order at the following number:
  599.  
  600.                         (215)-646-4894   - day and night.
  601.  
  602.  
  603.  
  604.                                      Returns
  605.  
  606.              The  software may be returned within 30 days of purchase for 
  607.         a full refund.  This applies whether or not the software has been 
  608.         used.  We do ask that manuals, disks and packaging be returned in 
  609.         excellent condition.
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.                                   Installation
  633.  
  634.              You will need an IBM PC compatible machine with a minimum of 
  635.         256 kbytes of memory. ED PROLOG benefits from up to 245 kbytes of 
  636.         program  memory (TPA).  This means in practice that a machine with  320 
  637.         kbytes of memory is optimal for ED PROLOG.
  638.  
  639.              To  determine the amount of TPA your machine  has,  run  the 
  640.         "chkdsk" program which is supplied with DOS. The last line reads:
  641.  
  642.                                 XXXXXX bytes free
  643.  
  644.         where XXXXXX is a six digit number.
  645.  
  646.         If  this  number  is greater than 200000,  ED  PROLOG  will  have 
  647.         reduced workspace.  If it's over 245000,  the amount of memory is 
  648.         optimal. If this is not the case, there are two possibilities:
  649.  
  650.         1) The machine doesn't have enough memory.
  651.  
  652.         2)  Something  else is removing memory from TPA,  such as  a  co-
  653.         resident  program,  a  ramdisk,  a large dos driver,  or a  large 
  654.         number of file or disk buffers.
  655.  
  656.         If  you're  short of memory,  make sure that no  other  programs, 
  657.         ramdisks,  or drivers besides DOS are running in the machine. You 
  658.         may find it helps to eliminate (by renaming) the config.sys  file 
  659.         when you intend to run ED PROLOG.
  660.  
  661.  
  662.                       How to run the Demonstration Programs 
  663.                         without Knowing What You're Doing
  664.  
  665.              We strongly advise that you purchase the book Programming in 
  666.         PROLOG by Clocksin and Mellish,  publisher Springer Verlag, 1981. 
  667.         For  the  impatient we give some advice.  Type the  demonstration 
  668.         program you wish to run.  There must be at least one entry  point 
  669.         within  the  program.  
  670.  
  671.         Note:  Please  understand that these are demonstrations programs. 
  672.         Regarding  user  interface,  they are poorly  written.  You  will 
  673.         probably have to read Clocksin and Mellish to appreciate that the 
  674.         following examples of input are not equivalent: "yes." , "yes" .
  675.  
  676.  
  677.         The animals program - "animal"
  678.          
  679.              Most  of the examples require C & M for  comprehension.  The 
  680.         program "animals", however, can be appreciated by anyone. It is a 
  681.         traditional   example of an expert system.
  682.              
  683.              We had hoped to include the animals program on disk,  but we 
  684.         have  found  to  our  dismay that the version which  we  used  is 
  685.         allegedly copyrighted by the implementors of PROLOG 86.  Don't be 
  686.         surprised - even "happy birthday" is copyrighted.  We will simply 
  687.         point out that the November '84 issue of Robotics Age included  a 
  688.         version  of  the  animals game,  which you can,  at the  risk  of 
  689.         copyright infringement,  type in.  There is only one change  that 
  690.         need  be  made.  The "concat" function used in that  program  has 
  691.         arguments of the form:
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.                       concat( [atom1, atom2,...], result ).
  700.  
  701.         In order to make the concat definition more closely resemble that 
  702.         of  "name",  which  is  described by Clocksin  and  Mellish,  the 
  703.         argments have been reversed:
  704.  
  705.                       concat( result, [atom1, atom2,...] )
  706.  
  707.         Assuming  that you have typed in the program and made the  change 
  708.         just noted, the following steps are required to run it:
  709.              
  710.              Run the prolog.exe file.  The prompt "?-" will appear.  Type 
  711.         "consult( 'animals' ).<CR>".  Here <CR> indicates you are to type 
  712.         a  carriage  return.  The PROLOG system will load  "animals"  and 
  713.         compile  it into an internal form.  When the "?-" prompt  appears 
  714.         PROLOG is ready to run the "animals" guessing game. The object of 
  715.         the program is to deduce the animal you are thinking of. To start 
  716.         it  off  type  "help.<CR>".  PROLOG  will  respond  by  asking  a 
  717.         question. 
  718.              Because of the way the animals program is written,  you must 
  719.         respond in a rigid format.  You may type "yes<CR>",  "no<CR>", or 
  720.         "why<CR>". 
  721.              Eventually the program will terminate with either a guess as 
  722.         to what animal you are thinking of,  or a remark that the  animal 
  723.         is  not within its domain of knowledge.  The program has learned, 
  724.         however.  You  may  run  the  program again to  see  what  effect 
  725.         additional knowledge has on the program's behavior. 
  726.  
  727.              The program fragment "console" shows how you may improve the 
  728.         console input routines of any of these programs.
  729.              
  730.  
  731.         The Hematology Diagnosis Program - "hemat"
  732.  
  733.              Although  the  logical structure is not as sophisticated  as 
  734.         that of "animals", it is interesting for several reasons:
  735.  
  736.              1)  The  program  evaluates numerical data to  arrive  at  a 
  737.         diagnosis.
  738.  
  739.              2) Although inaccurate, it demonstrates that useful question 
  740.         answering systems are not difficult to write in PROLOG.
  741.  
  742.              3)  There  are  some mistakes in  the  program,  which  only 
  743.         slightly impede its usefulness. 
  744.  
  745.              This  program  uses  structure  input.  Terminate  all  your 
  746.         answers with a period, as in "y.<CR>", or "no.<CR>".
  747.         The starting point is "signs.<CR>".  PROLOG will prompt  you 
  748.         for  signs  of  anemia.  The  program attempts  to  diagnose  two 
  749.         varieties of a hemolytic anemia.
  750.              The program could use a good working over by a  hematologist 
  751.         and we would be delighted to collaborate.
  752.  
  753.  
  754.         Prime Number Generator - "sieve"
  755.  
  756.              This  program demonstrates that anything can be programed in 
  757.         PROLOG if one tries hard enough.  Asking the  question   
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.           "primes( 50, L ).<CR>" causes a list of prime numbers less than 
  765.         50  to be printed out.  "Sieve" is heavily recursive and  quickly 
  766.         exhausts the stack space of the small model interpreters.
  767.  
  768.  
  769.         Grrules
  770.  
  771.              This  is  an  example  of the use  of  the  definite  clause 
  772.         grammer  notation.  PD  PROLOG  does not  have  this  translation 
  773.         facility,  but ED PROLOG and  all of our other versions do. It is 
  774.         possible  to  perform  the  translations  by  hand  if  you  have 
  775.         thoroughly  read  C  & M.  Then you would have  the  pleasure  of 
  776.         asking:
  777.  
  778.                  ?-sentence( X, [every,man,loves,a,woman], [] ).
  779.  
  780.         and  having the meaning elucidated as a statment in the predicate 
  781.         calculus.
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.                                Special Offer  # 1
  831.  
  832.              For  some  inexplicable reason,  demonstration programs  are 
  833.         hard to come by. We are too busy writing PROLOG fill this gap. We 
  834.         will  reward the contribution of "cute" sample programs with  the 
  835.         following:
  836.  
  837.              1) A free  copy of type VMI virtual memory PROLOG
  838.  
  839.              2) The sample program will be published as an intact file 
  840.              together with whatever comments or advertisments the author
  841.              may see fit to include, on our distribution disks. 
  842.  
  843.              3) Exceptional contributions may merit a copy of type VML 
  844.              large  model virtual memory PROLOG which now incorporates  a 
  845.              UNIX1 style tree structured domain system.
  846.  
  847.  
  848.                                 Special Offer # 2
  849.  
  850.  
  851.              If  you are a hardware manufacturer and would like a  PROLOG 
  852.         language  for your system,  the solution is simple.  Just send us 
  853.         one  of  your machines!  Provided your system  implements  a  "C" 
  854.         compiler, it will be ported in no time flat.
  855.  
  856.  
  857.         ______
  858.              1. Trademark of AT & T.
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.                          Writing Programs For ED PROLOG
  897.  
  898.              You do not type in programs at the "?-" prompt.  There is no 
  899.         built-in  editor.  The command "consult( user )" is accepted  but 
  900.         does not cause PROLOG to enter an editing mode.  We feel that the 
  901.         most universally acceptable editing method is for the user to use 
  902.         a text editor of choice,  which can be invoked from within PROLOG 
  903.         by the "exec" predicate.
  904.  
  905.              Use  Wordstar  or your customary editor to write a  program. 
  906.         Then  run  PD  PROLOG and use the consult function  to  load  the 
  907.         program.
  908.  
  909.              In  all  cases except PD PROLOG,  you can  run  your  editor 
  910.         without leaving PROLOG by use of the "exec" predicate.
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.                              Running the Interpreter
  963.  
  964.         COMMANDS: Give commands in lower case.
  965.  
  966.         TO RUN:
  967.              Invoke PROLOG.EXE. After the "?-" prompt appears,
  968.                type "consult( <filename><CR> )", where <filename> is the 
  969.                desired database.
  970.              To exit, type "exitsys.<CR>"
  971.  
  972.         TO ENTER A FACT:
  973.              Don't do it except with the "assert" predicates. This is the 
  974.              most  frequently misunderstood aspect of A.D.A.  Prolog.  If 
  975.              you want to enter a bunch of facts,  put them in a file  and 
  976.              "consult" them using the "consult" predicate.
  977.  
  978.         TO ASK A QUESTION:
  979.              At the prompt, type "<expression>.<CR>", where 
  980.              <expression>  is  a  question as described by  Clocksin  and 
  981.              Mellish. Be sure to terminate the question with a period.
  982.              The question may be up to 500 characters long.
  983.  
  984.         TO INPUT A STRUCTURE AT THE KEYBOARD:
  985.              The structure may be up to 500 characters in length. Be sure 
  986.              to terminate with a period.
  987.  
  988.         TO ASK FOR ANOTHER SOLUTION:
  989.              If a solution has been provided, the PROLOG interpreter will 
  990.              ask  "More?  (Y/N):".  Only  if  a "y"  is  typed  will  the 
  991.              interpreter perform a search.
  992.  
  993.         TO ABORT A SEARCH:
  994.              Simply   type   the  escape  key.   The   interpreter   will      
  995.              respond  with  "Interrrupted.",  and return to  the  command      
  996.              prompt.
  997.  
  998.         TO LOAD ANOTHER DATABASE:
  999.              Type  "consult(<filename>).<CR>" The file name must have the 
  1000.              extension  ".PRO".  It  is  not  necessary  to  include  the 
  1001.              extension in the argument of consult. The file name as given 
  1002.              must not be the same as a predicate name in the file or  any 
  1003.              file which will be loaded.
  1004.  
  1005.         TO TRACE:
  1006.              When the system displays the prompt "?-", type "trace.<CR>".  
  1007.              The display will likely move too rapidly for you to read. To 
  1008.              stop the display,  type Control S.  To restart the  display, 
  1009.              type  Control S.  To turn the trace      display  off,  type 
  1010.              "notrace<CR>"  at  the  prompt  "?-".   The  interrupt  menu 
  1011.              contains  additional  options,  such  as sending  all  trace 
  1012.              output to a file, as well as display at the console.
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.         TO INTERRUPT A PROGRAM:
  1029.              See  the  section  entitled  "The  Interrupt  Menu"  for   a 
  1030.              description  of the flexible options.  Basically,  one types 
  1031.              ESC  to terminate a program,  while Control V or  Control  I 
  1032.              interrupt a program. 
  1033.  
  1034.         TO RECONSULT A FILE:
  1035.              The   predicate  "recon"  is  identical  to  the   Edinburgh 
  1036.              predicate "reconsult."
  1037.  
  1038.         TO REMOVE A DATABASE:
  1039.              Type "forget(<filename>).<CR>"
  1040.  
  1041.         TO EXIT TO THE OPERATING SYSTEM:
  1042.              Type "exitsys.<CR>"
  1043.  
  1044.              The system is totally interactive; any commands the operator 
  1045.         gives  are and must be valid program statements.  Statements must 
  1046.         terminate with a period.      All commands which take a file name 
  1047.         also  accept a path name.  Any name which is not a  valid  PROLOG 
  1048.         atom (refer to C & M) must be enclosed in single quotes. Thus one 
  1049.         could say
  1050.  
  1051.              consult( expert )
  1052.  
  1053.         but one would need single quotes with
  1054.  
  1055.              consult( 'b:\samples\subtype\expert' ).
  1056.  
  1057.  
  1058.         To exit the system, type "exitsys.<CR>"
  1059.  
  1060.         Atoms  may contain MSDOS pathnames if they are enclosed by single 
  1061.         quotes, ie.,  '\b:\samples\animal' .
  1062.  
  1063.         You may consult more than one file at a time.  However, all names 
  1064.         are public and name conflicts must be avoided. The order in which 
  1065.         modules are loaded may,  in cases of poor program design,  affect 
  1066.         the behavior.
  1067.  
  1068.  
  1069.  
  1070.                              Command Line Arguments
  1071.  
  1072.              ED and PD PROLOG accept one command line argument,  which is 
  1073.         the name of a "stream" which replaces the console for input.  The 
  1074.         "stream"  in  MSDOS is a pipe or file which supplies input  until 
  1075.         end-of-file is reached. Control then reverts back to the console. 
  1076.         To avoid noisy parser error messages when end-of-file is reached, 
  1077.         the  last command in the file should be "see( user )." See  Simon 
  1078.         Blackwell's PIE program for an example of this.
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.                                A Reference of Note
  1095.  
  1096.  
  1097.              With  minor  exceptions,  the syntax is a superset  of  that 
  1098.         described  by  Clocksin and Mellish in the  book  Programming  in 
  1099.         Prolog by W.F.  Clocksin and C.S.  Mellish, published by Springer 
  1100.         Verlag in Berlin, Heidelberg, New York, and Tokyo. We shall refer 
  1101.         to this book as "C & M".
  1102.  
  1103.  
  1104.              There   are  very  few   syntactical   differences,   mostly 
  1105.         unrecognized and/or minor.
  1106.              When  an operator is declared using the "op" statement,  the 
  1107.         operator must be enclosed in single quotes in the "op"  statement 
  1108.         itself,  if  it would not otherwise be a legal Edinburgh functor. 
  1109.         Subsequently,  however,  the parser will recognize it for what it 
  1110.         is,  except  in  the "unop" statement,  where it  must  again  be 
  1111.         enclosed in single quotes.
  1112.  
  1113.              Variable numbers of functor paramaters are supported.
  1114.  
  1115.              A  goal  may be represented by a  variable,  which  is  less 
  1116.         restrictive  than  the  C  &  M requirement  that  all  goals  be 
  1117.         functors.  The  variable must be instantiated to a  functor  when 
  1118.         that goal is pursued.
  1119.  
  1120.              Rules which appear inside other expressions must be enclosed 
  1121.         in  parenthesis  if  the "," operator is to be  recognized  as  a 
  1122.         logical connective.
  1123.  
  1124.              All  infix  operators described by C & M,  and user  defined 
  1125.         infix,  prefix, and postfix operators with variable associativity 
  1126.         and precedence are supported exactly as in C & M.
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.                          The Built In Predicate Library
  1161.  
  1162.                         Available Operators in PD and ED PROLOG
  1163.  
  1164.  
  1165.         Column 1 gives the function symbol. 
  1166.  
  1167.         Column 2 gives the precedence. The range of precedence is 1 to 255.
  1168.         A zero in the precedence column indicates the symbol is parsed as 
  1169.         a functor, and precedence is meaningless in this case.
  1170.  
  1171.         Column 3 gives the associativity. 
  1172.         A zero in the associativity column indicates the symbol is parsed
  1173.         as a functor, and associativity is meaningless in this case.
  1174.  
  1175.         Column 4 indicates which version the function is available in. 
  1176.         Unless otherwise noted, the function is available in all versions.
  1177.         Nonstandard predicates are indicated by "NS".
  1178.  
  1179.  
  1180.         op/pred      precedence    associativity         availability
  1181.  
  1182.         "!"             0             0
  1183.         "|"             0             0
  1184.         "="             40,           XFX                   
  1185.         "=="            40,           XFX                   
  1186.         "\\="           40,           XFX                   
  1187.         "\\=="          40,           XFX       
  1188.         "/"             21,           YFX       
  1189.         "@="            40,           XFX       
  1190.         ">="            40,           XFX       
  1191.         "=<"            40,           XFX       
  1192.         ">"             40,           XFX       
  1193.         "<"             40,           XFX       
  1194.         "-"             31,           YFX        
  1195.         "*"             21,           YFX
  1196.         "+"             31,           YFX
  1197.         "=.."           40,           XFX
  1198.         "-->"           255,          YFY       (not  in  PD PROLOG)
  1199.         "?-"            255,          FY  
  1200.               
  1201.  
  1202.  
  1203.         "arg"           0,            0,                                   
  1204.         "asserta"       0,            0,                                   
  1205.         "assertz"       0,            0,                                   
  1206.         "atom"          0,            0,                                   
  1207.         "atomic"        0,            0,                                   
  1208.         "batch"         0,            0
  1209.         "clause"        0,            0,                                   
  1210.         "clearops"      0,            0,                                   
  1211.         "cls"           0,            0,       NS
  1212.         "concat"        0,            0,                                   
  1213.         "consult"       8,            FX,                                  
  1214.         "crtgmode"      0,            0,       NS
  1215.         "crtset"        0,            0,       NS
  1216.         "curset"        0,            0,       NS
  1217.         "curwh"         0,            0,       NS
  1218.         "debugging      0,            0,                                   
  1219.         "dir"           0,            0,                                   
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.         "display"       0,            0,                                   
  1227.         "dotcolor"      0,            0,       NS
  1228.         "drawchar"      0,            0,       NS
  1229.         "drawdot"       0,            0,       NS
  1230.         "drawline"      0,            0,       NS
  1231.         "exec"          0,            0,                                   
  1232.         "exitsys"       0,            0,       NS                          
  1233.         "forget"        0,            0,       NS                          
  1234.         "functor"       0,            0,                                   
  1235.         "get0"          8,            FX,                                  
  1236.         "integer"       0,            0,                                   
  1237.         "is"            40,           XFX,                                 
  1238.         "listing"       0,            0,                                   
  1239.         "memleft"       0,            0,      NS
  1240.         "mod"           11,           XFX,                                 
  1241.         "name"          0,            0,                                   
  1242.         "nl"            0,            0,                                   
  1243.         "nodebug"       0,            0,       (not in PD PROLOG)
  1244.         "nonvar"        0,            0,                                   
  1245.         "nospy"         50,           FX,      (not in PD PROLOG)
  1246.         "not"           60            FX                                   
  1247.         "notrace"       0,            0,       (not in PD PROLOG)
  1248.         "op"            0,            0,                                   
  1249.         "popoff"        0,            0,       NS
  1250.         "popoffd"       0,            0,       NS
  1251.         "popon"         0,            0,       NS
  1252.         "popond"        0,            0,       NS
  1253.         "print"         0,            0,                                   
  1254.         "prtscr"        0,            0,       NS
  1255.         "put"           0,            0,                                   
  1256.         "ratom"         0,            0,                                   
  1257.         "read"          0,            0,                                   
  1258.         "recon"         0,            0, (Note: this is "reconsult")
  1259.         "repeat"        0,            0,                                   
  1260.         "retract"       0,            0                                    
  1261.         "rnum"          0,            0,                                   
  1262.         "see"           0,            0,       
  1263.         "seeing"        0,            0,       
  1264.         "seen"          0,            0,       
  1265.         "skip"          0,            0,       (not in PD PROLOG)
  1266.         "spy"           50,           FX,      
  1267.         "tab"           0,            0,                                   
  1268.         "tell"          0,            0,       
  1269.         "telling"       0,            0,       
  1270.         "told"          0,            0,       
  1271.         "trace"         0,            0,       (not in PD PROLOG)
  1272.         "true"          0,            0,                                   
  1273.         "unop"          0,            0,                                   
  1274.         "var"           0,            0,                                   
  1275.         "write"         0,            0,                                   
  1276.                         Description of the Modifications.
  1277.              
  1278.  
  1279.         I/O Redirection
  1280.  
  1281.         I/O  redirection is a feature described by Clocksin and  Mellish. 
  1282.         The predicates "see",  "seeing",  "seen",  "tell", "telling", and 
  1283.         "told" are used to select the streams used for input and output. 
  1284.  
  1285.              The  predicates  "seen" and "told" require as arguments  the 
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.         name of the stream that is to be closed.  This enables the system 
  1293.         to  remember the indices of several streams and switch  back  and 
  1294.         forth between them.
  1295.  
  1296.              The  predicate "batch",  when inserted at the beginning of a 
  1297.         stream file, has the following properties:
  1298.  
  1299.              1)  The normal prompt,  "?-",  and advisory messages do  not 
  1300.              appear at the screen.
  1301.  
  1302.              2)  It is self cancelling if the input stream is  reassigned 
  1303.              to the console.
  1304.  
  1305.              3) It may also be cancelled by the predicate "batch".
  1306.  
  1307.  
  1308.  
  1309.         call( <goal> )
  1310.  
  1311.              The  predicate as defined in C & M is obsolete.  The purpose 
  1312.         was  to permit a goal search where the goal name was  a  variable 
  1313.         instantiated  to  some functor name.  A.D.A.  permits writing  of 
  1314.         goals with such names,  so the mediation of the "call" clause  is 
  1315.         no longer necessary.
  1316.  
  1317.              The  "call"  predicate  may  be  trivially  implemented  for 
  1318.         compatibility with the PROLOG definition
  1319.  
  1320.                                  call( X ) :- X.
  1321.  
  1322.  
  1323.  
  1324.         clause
  1325.  
  1326.              The  function  clause(  X,  Y ) has the  new  optional  form 
  1327.         clause(  X,  Y,  I  ).  If the third variable is written,  it  is 
  1328.         instantiated  to the current address of a clause in  memory.  The 
  1329.         only  use of the result is with succeeding assertfa and  assertfz 
  1330.         statements.
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.         debugging
  1359.  
  1360.              "Debugging"  prints a list of the current  spypoints.  After 
  1361.         each name a sequence of numbers may appear, indicating the number 
  1362.         of  arguments that is a condition of the trace.  The  word  "all" 
  1363.         appears  if  the  number of arguments is not a condition  of  the 
  1364.         trace.
  1365.  
  1366.         op( <prec>, <assoc>, <functor> )
  1367.  
  1368.              Defines  the  user  definable  grammar  of  a  functor.  The 
  1369.         definition conforms to that in C & M. We mention here a minor but 
  1370.         important point.  If <functor> is not a valid PROLOG atom it must 
  1371.         be   enclosed  in  single  quotes  when  declared  in  the   "op" 
  1372.         declaration.  It  is  not necessary or legal to do this when  the 
  1373.         functor is actually being used as an operator.  In version 1.6, a 
  1374.         declared  or built-in operator can be used either as an  operator 
  1375.         or as a functor. For example, 
  1376.  
  1377.                                  +(2,3) = 2 + 3.
  1378.  
  1379.         is a true statement.
  1380.  
  1381.              Declared  operators  are annotated in the directory  display 
  1382.         with their precedence and associativity.
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.         Output predicates
  1425.  
  1426.            display
  1427.            write
  1428.            print
  1429.            put
  1430.  
  1431.              These  functions  have  been  modified  to  accept  multiple 
  1432.         arguments in the form:
  1433.  
  1434.                        print( <arg1>, <arg2>, <arg3>,... )
  1435.  
  1436.         Thus, "put( a, b, c )" would result in the display of "abc".
  1437.              The  names  of  some  PROLOG atoms that may  occur  are  not 
  1438.         accepted  by  the  PROLOG scanner  unless  surrounded  by  single 
  1439.         quotes.  This only applies when such an atom is read in, not when 
  1440.         it is internally generated. Nevertheless, this presents us with a 
  1441.         problem:  We  would  like to be capable of writing  valid  PROLOG 
  1442.         terms to a file. In some cases, it is necessary to add the single 
  1443.         quotes.  In other cases,  such as human oriented output, they are 
  1444.         an irritant. The modified definitions of the following predicates 
  1445.         are an attempt at a solution:
  1446.  
  1447.              display
  1448.              Operator  precedence  is ignored,  all functors are  printed 
  1449.              prefix and single quotes are printed if needed or they  were 
  1450.              supplied if and when the atom was originally input.
  1451.  
  1452.              write
  1453.              Operator  precedence is taken into account and operators are 
  1454.              printed according to precedence.  Single quotes are  printed 
  1455.              under the same conditions as for "display."
  1456.  
  1457.              print
  1458.              Operator  precedence is taken into account and operators are 
  1459.              printed  according to precedence.  Single quotes  are  never 
  1460.              displayed.  This  is  a  human oriented form of  output  and 
  1461.              should  never  be used for writing of terms for  the  PROLOG 
  1462.              scanner.
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.         get0
  1491.         read
  1492.  
  1493.              The  functions  "get0"  and "read"  have  been  extended  to 
  1494.         support input from a stream other than the one currently selected 
  1495.         by "see". To direct output to a file or other stream, an optional 
  1496.         argument  is used.  For example,  "get0( char,  <file name> )" or 
  1497.         "get0( char,  user )" would cause input to come from <file  name> 
  1498.         or the console.  If the file has not already been opened,  "get0" 
  1499.         will fail.
  1500.  
  1501.  
  1502.  
  1503.              Atoms enclosed by single quotest, eg. '\nthis is a new line' 
  1504.         can contain the escape sequences
  1505.  
  1506.              '\n', '\r', '\t' and '\''.
  1507.  
  1508.              If these atoms are printed by "display" or "write" they  are 
  1509.         printed  just  as they are.  If they are printed by  the  "print" 
  1510.         clause they are translated as follows:
  1511.  
  1512.         '\n'  results  in  the printing of a carriage return and  a  line 
  1513.         feed.
  1514.         '\r' results in the printing of a carriage return only.
  1515.         '\t' results in the printing of a tab character.
  1516.         '\'' allows the printing of a single quote within a quoted atom.
  1517.  
  1518.              The "portray" feature is not presently implemented.
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.  
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.  
  1532.  
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.                         Description of the New Predicates
  1557.  
  1558.  
  1559.         clearops-
  1560.  
  1561.              Nullify  the  operator  status  of  every  operator  in  the 
  1562.              database.
  1563.  
  1564.         concat( (<variable> | <functor>), <List> )
  1565.  
  1566.              A  list  of functors or operators is concatenated  into  one 
  1567.         string,  which becomes the name of a new atom to which <variable> 
  1568.         or <functor> must match or be instantiated.
  1569.  
  1570.  
  1571.         dir( option )
  1572.  
  1573.              Provide  an  alphabetized listing to the console  of  atoms, 
  1574.         constants,   or   open  files.   Without  options,   simply  type 
  1575.         "dir.<CR>". Options are:
  1576.  
  1577.              dir( p ) - list clause names only.
  1578.              dir( c ) - list consulted files only.
  1579.  
  1580.         Consulted files are prefixed by "S:".
  1581.  
  1582.  
  1583.         exitsys
  1584.  
  1585.              Exit to the operating system.
  1586.  
  1587.  
  1588.  
  1589.         forget( <file name> )
  1590.              Make  a database unavailable for use and reclaim the storage 
  1591.         it occupied.
  1592.  
  1593.  
  1594.  
  1595.         ratom( <arg>, <stream> )-
  1596.              Read an atom from the input stream,  to which <arg>  matches 
  1597.         or  is  instantiated.  <stream> is optional.  If <stream> is  not 
  1598.         given, the input stream defaults to the standar input.
  1599.              Input is terminated by a CR or LF, which are not included in 
  1600.         the stream.
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.                              Arithmetic Capabilities
  1623.  
  1624.              Integer  arithmetic is supported.  Numbers are 32 bit signed 
  1625.         quantities. The following arithmetic operators are supported:
  1626.  
  1627.                       "+", "-", "*", "/", <, <=, >, >=, mod.
  1628.  
  1629.         Arithmetic operators must never be used as goals,  although  they 
  1630.         may be part of structures. It is legal to write:
  1631.  
  1632.                                     X = a + b
  1633.              
  1634.         which  results in the instantiation of X to the struture (a + b). 
  1635.         But the following is not legal:
  1636.  
  1637.                        alpha( X, Y ) :- X + Y, beta( Y ).
  1638.  
  1639.  
  1640.         Evaluation  of  an arithemtic expression is mediated by the  "is" 
  1641.         and inequality predicates.  For instance,  the following would be 
  1642.         correct:
  1643.  
  1644.                          alpha( X, Y, Z ) :- Z is X + Y.
  1645.  
  1646.                          beta( X, Y ) :- X + 2 < Y + 3.
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.                             Memory Metric Predicate
  1689.  
  1690.              The purpose of this predicate is to give the prolog system 
  1691.         a  sense  of  how much memory remains so  that  expensive  search 
  1692.         strategies  can  be  controlled.  It is not possible  to  exactly 
  1693.         quantify how much memory remains.  At the lowest level, there are 
  1694.         two  types of memory - the stack and the heap.  The stack expands 
  1695.         down  from  high  memory,  while  the heap  tends  to  expand  at 
  1696.         unpredictable intervals upwards.  If the stack and heap meet, the 
  1697.         prolog  system  must abort the search and return to  the  prompt. 
  1698.         Judicious  use  of  the  memory  metric  predicates  reduces  the 
  1699.         probability of this happening.
  1700.  
  1701.              The  stack is easy to quantify because it expands  downwards 
  1702.         in  a  predictable  way with recursion.  The symbol  space  is  a 
  1703.         "heap".  For  those  interested,  the structure of  the  heap  is 
  1704.         determined  by  the C compiler under which Prolog  was  compiled. 
  1705.         There  is  a function internal to Prolog known as  the  allocator 
  1706.         searches  the  heap for enough contiguous memory to create a  new 
  1707.         symbol.  The  heap resembles a piece of Swiss cheese;  the  holes 
  1708.         represent symbols and already allocated memory while the remained 
  1709.         is  searched  by the allocator for a piece of  contiguous  memory 
  1710.         large enough to satisfy a request.  If one cannot be  found,  the 
  1711.         uppermost  bound of the heap is expanded upwards,  and that bound 
  1712.         is  the  number  which we measure for an  estimate  of  remaining 
  1713.         memory.
  1714.  
  1715.              The  sqace between the top of the heap,  and  the top of the 
  1716.         stack,  which we call "gap",  serves as a rough guide to how much 
  1717.         memory  remains.  The  demands  of the system  are  not  entirely 
  1718.         predictable,  however.  For example, the creation of a new symbol 
  1719.         larger  than "gap" would cause an abort.  The user must  use  the 
  1720.         numbers  supplied  by  these  functions  as  a  heuristic  guide, 
  1721.         tempered  by  experience,  to  minimize  the  possibility  of  an 
  1722.         unexpected abort.
  1723.  
  1724.         "Gap" is measured in 256 byte pages.
  1725.  
  1726.         memleft( X )
  1727.  
  1728.         If  the argument is an integer,  this is satisfied if the size of 
  1729.         "gap" is greater than "X".
  1730.  
  1731.         If the argument is a variable,  it is instantiated to the  amount 
  1732.         of "gap" remaining.
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.                          IBM PC Video Display Predicates
  1755.  
  1756.  
  1757.         A high level method is provided for drawing and displaying on the 
  1758.         screen of IBM PC and compatible computers. 
  1759.  
  1760.  
  1761.  
  1762.         cls
  1763.  
  1764.         Clear  the screen and position the cursor at the upper left  hand 
  1765.         corner.
  1766.  
  1767.  
  1768.  
  1769.         crtgmode( X )
  1770.  
  1771.         Matches  the  argument to the mode byte of the display  which  is 
  1772.         defined as follows:
  1773.  
  1774.                   mode           meaning
  1775.  
  1776.                    0             40 x  25  BW (default)
  1777.                    1             40 x  25  COLOR
  1778.                    2             80 x  25  BW
  1779.                    3             80 x  25  COLOR
  1780.                    4            320 x 200  COLOR
  1781.                    5            320 x 200  BW
  1782.                    6            640 x 200  BW
  1783.                    7             80 x  25  monochrome  display card
  1784.  
  1785.  
  1786.  
  1787.         crtset( X )
  1788.          
  1789.         This  sets the mode of the display.  The argument must be one  of 
  1790.         the modes given above. 
  1791.  
  1792.  
  1793.  
  1794.         curset( <row>, <column>, <page> )
  1795.  
  1796.         Sets the cursor to the given row, column, and page. The arguments 
  1797.         must be integers.
  1798.  
  1799.  
  1800.  
  1801.         curwh(  <row>, <column> )
  1802.  
  1803.         Reports the current position of the cursor.  The argument must be 
  1804.         an integer or variable. The format is:
  1805.  
  1806.              1) page zero is assumed.
  1807.              2) The row is in the range 0 to 79, left to right.
  1808.              3) The column is in the range 0 to 24, bottom to top.
  1809.         dotcolor( <row>, <column>, <color> )
  1810.  
  1811.         The  argument  <color> is matched to the color of  the  specified 
  1812.         dot. The monitor must be in graphics mode.
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.         drawchar( <character>, <attribute> )
  1823.  
  1824.         Put a character at the current cursor position with the specified 
  1825.         attribute.  The  arguments  <character> and <attribute>  must  be 
  1826.         integers.  Consult  the IBM technical reference manual  regarding 
  1827.         attributes.
  1828.  
  1829.  
  1830.  
  1831.         drawdot( <row>, <column>, <color> )
  1832.  
  1833.         Put  a dot at the specified position.  The monitor must be in the 
  1834.         graphics  mode.  The  arguments  must be  integer.  The  argument 
  1835.         <color> is mapped to integers by default in the following manner:
  1836.  
  1837.  
  1838.  
  1839.         drawline( <X1>, <Y1>, <X2>, <Y2>, <color> )
  1840.  
  1841.         Draw a line on the between the coordinate pairs. The monitor must 
  1842.         be in the graphics mode and the arguments are integer.
  1843.  
  1844.  
  1845.  
  1846.         prtscr
  1847.  
  1848.         Print  the  screen  as it currently appears.  Be  sure  that  the 
  1849.         printer  is  on  line and ready before invoking  this  predicate, 
  1850.         since otherwise, the system may lock up or abort.
  1851.  
  1852.  
  1853.  
  1854.         The  integer argument <color> referred to in the above predicates 
  1855.         is represented as follows:
  1856.  
  1857.              COLOR          PALETTE 0           PALETTE 1
  1858.              
  1859.                0            background          background
  1860.                1            green               cyan
  1861.                2            red                 magenta
  1862.                3            brown               white
  1863.  
  1864.         To change the palette and the background,  see the IBM  Technical 
  1865.         Reference Bios listings for more information.
  1866.  
  1867.  
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875.  
  1876.  
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885.  
  1886.                                    Trace Files
  1887.  
  1888.                                  (type ED only)
  1889.  
  1890.              You  can  now dump your trace to disk,  instead  of  (groan) 
  1891.         wasting  reams of printer paper.  This option is described in the 
  1892.         next section.
  1893.  
  1894.  
  1895.                                The Interrupt Menu
  1896.  
  1897.                                  (type ED only)
  1898.  
  1899.              This  menu  has been modified.  It was formerly  called  the 
  1900.         ESCAPE   menu,  but  the  meaning  of the  ESCAPE  key  has  been 
  1901.         redefined.  It  is  no  longer necessary to display the  menu  to 
  1902.         perform  one of the menu functions.  This reduces the  amount  of 
  1903.         display which is lost by scrolling off the screen.
  1904.  
  1905.              At any time while searching, printing, or accepting keyboard 
  1906.         input,  you can break to this menu.  It is generally not possible 
  1907.         to  do  this  during disk access,  since control  passes  to  the 
  1908.         operating  system  at this time.  Two keys cause  this  break  to 
  1909.         occur:
  1910.  
  1911.              ^V:  The menu is displayed and a command is accepted at the 
  1912.                   prompt  "INTERRUPT>".  After  a command,  the  menu  is 
  1913.                   redisplayed  until  the user selects  a  command  which 
  1914.                   causes an exit. 
  1915.  
  1916.              ^I:  The menu is not displayed.  Command is accepted at the 
  1917.                   prompt  "INTERRUPT>" until the user selects  a  command 
  1918.                   which causes an exit.
  1919.  
  1920.              ESC: Typing this key  causes a  termination of  the  PROLOG      
  1921.                   search  and  control returns to the user command  level      
  1922.                   with a prompt of "?-".  Notice that previously, the ESC      
  1923.                   key invoked this menu. 
  1924.  
  1925.  
  1926.  
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.              As the resulting menu indicates, the following functions are 
  1953.         possible:
  1954.  
  1955.              A: Abort the search and return to the prompt.
  1956.  
  1957.              O  Open  a trace file.  The user is prompted for  the  file 
  1958.                 name.  The file receives all trace output.  If a file is 
  1959.                 already opened it is closed with all output preserved.
  1960.  
  1961.              C  Close the trace file if one is open.  Otherwise there is 
  1962.                 no effect.
  1963.  
  1964.             ^C: Immediately  exit PROLOG without closing files.  This is 
  1965.                 not advised.
  1966.  
  1967.             ^P: Typing <Control>P toggles the printer. If the printer is 
  1968.                 on,  all  input  and output will also be  routed  to  the 
  1969.                 printer.
  1970.  
  1971.              S: If  the machine in use is an IBM PC compatible  machine, 
  1972.                 the  currently displayed screen will be printed.  If  the 
  1973.                 machine  is  not an IBM PC compatible,  do not  use  this 
  1974.                 function.
  1975.  
  1976.              T: If  trace  is in use,  most of the trace output  can  be 
  1977.                 temporarily turned off by use of this function,  which is 
  1978.                 a toggle.
  1979.  
  1980.              R:  Entering  another  ESC causes a return to the  current 
  1981.                 activity  (keyboard  input or search)  with  no  residual 
  1982.                 effect from the interruption.
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010.  
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.                              Conserving memory space
  2020.  
  2021.  
  2022.                Success popping is controlled by the predicates "popond",
  2023.         "popoffd",  "popon",  and  "popoff".  Success popping is means of 
  2024.         reclaiming  storage which is used on backtracking to  reconstruct 
  2025.         how a particular goal was satisfied.  If it is obvious that there 
  2026.         is no alternative solution to a goal this PROLOG system is  smart 
  2027.         enough to reclaim that storage. 
  2028.  
  2029.              In  this system,  succees popping is an expensive operation. 
  2030.         Therefore,  there  is a tradeoff of memory versus  time.  On  the 
  2031.         other hand, discrete use of success popping can actually speed up 
  2032.         a program by recreating structures in a more accessible form.
  2033.  
  2034.              The  definitions of the control predicates is given in  this 
  2035.         manual  and  their use is totally  optional.  The  modulation  of 
  2036.         success popping has no effect on program logic (read solution.)
  2037.              
  2038.              The  "cut"  can  save  substantial  time  and  computational 
  2039.         overhead  as well as storage.  Although the execution of the  cut 
  2040.         costs  time,  you can design your program to use cuts in critical 
  2041.         places  to  avoid unnecessary backtracking.  Thus  the  execution 
  2042.         speed of the program can actually increase. 
  2043.              
  2044.              Anyone  who  has  read Clocksin and  Mellish  is  aware,  of 
  2045.         course, that the "cut" has a powerful logical impact which is not 
  2046.         always desirable.
  2047.  
  2048.  
  2049.         popoff 
  2050.  
  2051.         See the below definition.
  2052.  
  2053.  
  2054.  
  2055.         popon
  2056.  
  2057.         The  inference  engine  does complete success popping  for  goals 
  2058.         which appear after "popon".  Consider this example: 
  2059.  
  2060.           goal :-  a, popon, b, c, popoff, d.
  2061.  
  2062.         If  no alternative solutions exist for b,  then  success  popping 
  2063.         will  reclaim storage by removing unnecessary records  describing 
  2064.         how  "b"  was  satisfied.  If the Prolog system cannot  rule  out 
  2065.         possible additional solutions,  success popping will never occur, 
  2066.         regardless of your use of "popon". 
  2067.              Since goal "d" occurs after "popoff",  success popping  will 
  2068.         never occur.
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.         popoffd
  2085.  
  2086.         If  no  "popon" or "popoff" declarations occur in a  clause,  the 
  2087.         default  action  is  determined by  "popoffd"  and  "popond".  If 
  2088.         "popoffd" has been invoked,  the default is that success  popping 
  2089.         will not occur.
  2090.  
  2091.  
  2092.  
  2093.         popond
  2094.  
  2095.         The inverse of "popoffd". Turns on default success popping.
  2096.         printf( <stream>, <term1>,<term2>,... )
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.  
  2120.  
  2121.  
  2122.  
  2123.  
  2124.  
  2125.  
  2126.  
  2127.  
  2128.  
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134.  
  2135.  
  2136.  
  2137.  
  2138.  
  2139.  
  2140.  
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.                                  Prolog Tutorial          
  2151.  
  2152.  
  2153.                                   Introduction
  2154.  
  2155.  
  2156.  
  2157.  
  2158.              Probably  you  have heard of the language PROLOG within  the 
  2159.         last year or so. You probably wondered the following things:
  2160.  
  2161.         1) What does the name stand for? Names of computer languages are 
  2162.         almost always acronyms.
  2163.  
  2164.         2) What is it good for?
  2165.  
  2166.         3) Why now?
  2167.  
  2168.         4) Can I get a copy to play with?
  2169.  
  2170.              Congratulations! You obviously know the answer to the fourth 
  2171.         question. We now respond to the other three.
  2172.              
  2173.         1)  The  name  stands for "programming in logic." This  we  shall 
  2174.         elaborate on in depth later on.
  2175.  
  2176.         2) PROLOG is good for writing question answering systems.  It  is 
  2177.         also   good   for  writing  programs  that  perform   complicated 
  2178.         strategies  that  compute the best or worst way to  accomplish  a 
  2179.         task, or avoid an undesirable result.
  2180.  
  2181.         3) PROLOG was virtually unknown in this country until researchers 
  2182.         in  Japan announced that it was to be the core language  of  that 
  2183.         country's fifth generation computer project.  This is the project 
  2184.         with  which  Japan hopes to achieve a domainant position  in  the 
  2185.         world information industry of the 1990's. 
  2186.  
  2187.              PROLOG  is  one of the most unusual computer languages  ever 
  2188.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  2189.         BASIC.  The facilities complement,  rather than replace those  of 
  2190.         conventional  languages.  Although  it  has great  potential  for 
  2191.         database  work,  it  has  nothing in  common  with  the  database 
  2192.         languages used on microcomputers.
  2193.  
  2194.              Perhaps  the  best point to make is that while  conventional 
  2195.         languages are prescriptive, PROLOG is descriptive. A statement in 
  2196.         a conventional language might read:
  2197.  
  2198.              if( car_wheels = TRUE ) then
  2199.                begin
  2200.                  (some sort of procedure)
  2201.                  X = X + 1;
  2202.                end 
  2203.  
  2204.  
  2205.  
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.  
  2212.  
  2213.  
  2214.  
  2215.  
  2216.         A statment in PROLOG could just be a statment of fact about  cars 
  2217.         and wheels. There are many relationships that hold. For instance,
  2218.  
  2219.              has( car, wheels ).
  2220.  
  2221.              has( car, quant(wheels, four) ).
  2222.  
  2223.              round( wheels ).
  2224.  
  2225.         Each  of  these statments is an independent fact  relating  cars, 
  2226.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  2227.         independent, they can be put into a PROLOG program by programmers 
  2228.         working separately. The man who is a specialist on car bodies can 
  2229.         say  his thing,  the wheel specialist can have his say,  and  the 
  2230.         participants can work with relative independence. And this brings 
  2231.         to light a major advantage of PROLOG:
  2232.  
  2233.  
  2234.                              PARALLEL PROGRAMMING!!!
  2235.                             
  2236.  
  2237.         With  conventional  programming languages projects can  still  be 
  2238.         "chunked",  or  divided between programmers.  But efficiency on a 
  2239.         team  project  drops  drastically below that  of  the  individual 
  2240.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  2241.         participants    grows   the   need   for   communication    grows 
  2242.         geometrically. The time spent communicating can exceed that spent 
  2243.         programming! 
  2244.              Although   PROLOG   does   not  eliminate   the   need   for 
  2245.         task  coordination,  the problem is considerably  simplified.  It 
  2246.         also provides the ability to answer questions in a "ready to  eat 
  2247.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  2248.         statements about cars and wheels previously given,  could you ask 
  2249.         it the following question?   
  2250.  
  2251.                        
  2252.               has( car, X ), round( X ).
  2253.  
  2254.              Does  a  car  have anything which  is  round?  The  question 
  2255.         instructs the PROLOG interpreter to consider all the objects that 
  2256.         it  knows are possessed by a car and find those which are  round. 
  2257.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  2258.         of a smart database searcher.  It can not only find the facts but 
  2259.         selectively find them and interpret them.
  2260.  
  2261.  
  2262.  
  2263.  
  2264.  
  2265.  
  2266.  
  2267.  
  2268.  
  2269.  
  2270.  
  2271.  
  2272.  
  2273.  
  2274.  
  2275.  
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281.  
  2282.              Consider the problem of a fault tree, as exemplified by this 
  2283.         abbreviated one:
  2284.  
  2285.  
  2286.  
  2287.         {Car won't start}
  2288.              | 
  2289.              | 
  2290.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  2291.                 (Yes)                                       v
  2292.                  |                                  {Check battery}
  2293.                  |
  2294.         [Smell gasoline](yes) --> {Try full throttle cranking}
  2295.                  |                       (failure)
  2296.         /--------/                           |
  2297.  
  2298.                             (details omitted)
  2299.  
  2300.  
  2301.  
  2302.              The fault tree is easily programmed in BASIC. Later we shall 
  2303.         show  that  PROLOG supplies a superior replacement for the  fault 
  2304.         tree.  Though the tree is capable of diagnosing only the  problem 
  2305.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  2306.         appropriate  tree from facts and rules you have provided.  PROLOG 
  2307.         is not limited to answering one specific question.  Given  enough 
  2308.         information,  it  will attempt to find all deductive solutions to 
  2309.         any problem.
  2310.  
  2311.  
  2312.  
  2313.  
  2314.  
  2315.  
  2316.  
  2317.  
  2318.  
  2319.  
  2320.  
  2321.  
  2322.  
  2323.  
  2324.  
  2325.  
  2326.  
  2327.  
  2328.  
  2329.  
  2330.  
  2331.  
  2332.  
  2333.  
  2334.  
  2335.  
  2336.  
  2337.  
  2338.  
  2339.  
  2340.  
  2341.  
  2342.  
  2343.  
  2344.  
  2345.  
  2346.  
  2347.  
  2348.  
  2349.                                   PROLOG PRIMER
  2350.  
  2351.         I.                       Rules and Facts     
  2352.  
  2353.  
  2354.  
  2355.              This  is  where you should start if you know  nothing  about 
  2356.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  2357.  
  2358.         1)   has( car, wheels ).
  2359.  
  2360.         This  statement  is a "fact.  The word "has" in this statment  is 
  2361.         known  either  as a functor or predicate.  It is a name  for  the 
  2362.         relationship  within the parenthesis.  It implies that a car  has 
  2363.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  2364.         arbitrary and established by you. You could just as easily say:
  2365.  
  2366.         2)   has( wheels, car ).
  2367.  
  2368.         and if you wrote this way consistently,  all would be  well.  The 
  2369.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  2370.         "car" are constants. 
  2371.              
  2372.         A   database   of  facts  can  illustrate  the   data   retrieval 
  2373.         capabilities of PROLOG. For instance:
  2374.  
  2375.         3)   has( car, wheels ).
  2376.              has( car, frame ).
  2377.              has( car, windshield ).
  2378.              has( car, engine ).
  2379.  
  2380.         You could then ask PROLOG the question:
  2381.  
  2382.         4)   has( car, Part ).
  2383.  
  2384.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  2385.         will make Part equal to whatever constant is required to make the 
  2386.         question match one of the facts in the database. Thus PROLOG will 
  2387.         respond:
  2388.  
  2389.              Part = wheels.
  2390.              
  2391.              More?(Y/N):
  2392.  
  2393.         If you type "y" the next answer will appear:
  2394.  
  2395.              Part = frame.
  2396.  
  2397.              More?(Y/N):
  2398.  
  2399.         If you continue, PROLOG will produce the answers Part = windshield 
  2400.         and Part = engine. Finally, you will see:
  2401.  
  2402.              More?(Y/N):y
  2403.  
  2404.              No.
  2405.          
  2406.         indicating that PROLOG has exhausted the database.  Incidentally, 
  2407.         when  a  variable is set equal to a constant or  other  variable, 
  2408.  
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414.         it is said to be instantiated to that object.
  2415.  
  2416.              Notice  that  PROLOG searches the database forwards  and  in 
  2417.         this case,  from the beginning.  The forward search path is built 
  2418.         into PROLOG and cannot be changed. An author of a program written 
  2419.         in  a  prescriptive language is quite conscious of the  order  of 
  2420.         execution  of  his program,  while in PROLOG it is  not  directly 
  2421.         under his control.
  2422.              
  2423.              The other major element is the rule which is a fact which is 
  2424.         conditionally true. In logic this is called a Horn clause: 
  2425.  
  2426.  
  2427.         5)   has( X, wheels ) :- iscar( X ).
  2428.  
  2429.         The  fact iscar( car ) and the above rule are equivalent to
  2430.  
  2431.         6)   has( car, wheels).
  2432.  
  2433.         The  symbol :- is the "rule sign." The expression on the left  of 
  2434.         :-is the "head" and on the right is the body.  The variable X has 
  2435.         scope  of the rule,  which means that it has meaning only  within 
  2436.         the rule.  For instance,  we could have two rules in the database 
  2437.         using identically named variables.
  2438.  
  2439.  
  2440.         7)   has( X,  transportation ) :- 
  2441.                            has( X,  car ), has( license, X ).
  2442.  
  2443.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  2444.  
  2445.         The  variables  X in the two expressions are completely  distinct 
  2446.         and have nothing to do with each other.
  2447.  
  2448.         The comma between has( X, car ) and has( license, X ) means "and" 
  2449.         or logical conjuction.  The rule will not be true unless both the 
  2450.         clauses has(X, car) and has( license, X ) are true.
  2451.  
  2452.  
  2453.              On the other hand if there is a rule
  2454.              
  2455.         9)   has( license, X ) :- passedexam( X ).
  2456.  
  2457.         consider what PROLOG will do in response to the question:
  2458.  
  2459.         10)  has( harry, transportation ).
  2460.  
  2461.         (Notice  that  harry has not been capitalized because we  do  not 
  2462.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  2463.         enclosed in single quotes.)
  2464.  
  2465.              It  will scan the database and use (7),  in which X will  be 
  2466.         instantiated to harry. The rule generates two new questions:
  2467.  
  2468.         11)  has( harry, car ).
  2469.  
  2470.         12)  has( license, harry ).
  2471.  
  2472.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  2473.         satisfied and the database is scanned for a match to (12). PROLOG 
  2474.  
  2475.  
  2476.  
  2477.  
  2478.  
  2479.  
  2480.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  2481.         question is now posed:
  2482.  
  2483.         13)  passedexam( harry ).
  2484.  
  2485.              If there is a fact:
  2486.  
  2487.              passedexam( harry ).
  2488.  
  2489.         in  the database then all is well and harry  has  transportation. 
  2490.         If there is not, then PROLOG will succinctly tell you:
  2491.  
  2492.              No.
  2493.  
  2494.         But  suppose Harry has money and can hire a chauffer as any  good 
  2495.         programmer  can.  That  could be made part of the program in  the 
  2496.         following way.
  2497.  
  2498.              The rule which PROLOG tried to use was:
  2499.  
  2500.         14)  has( X,  transportation ) :- 
  2501.                            has( X,  car ), has( license, X ).
  2502.  
  2503.         At any point following it there could be included another rule:
  2504.  
  2505.         15)  has( X, transportation ) :- has( X, money ).
  2506.  
  2507.         or simply the bald fact:
  2508.  
  2509.         16)  has( harry, transportation ).
  2510.  
  2511.              These  additional  rules  or  facts would  be  used  in  two 
  2512.         circumstances.  If at any point a rule does not yield a solution, 
  2513.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  2514.         applicable  one.  This process is known as "backtracking  search" 
  2515.         and can be quite time consuming.
  2516.  
  2517.  
  2518.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  2519.         search  for an additional distinct solution.  It will attempt  to 
  2520.         find an alternate rule or fact for the last rule or fact used. If 
  2521.         that  fails,  it  will back up to the antecedent rule and try  to 
  2522.         find  an alternate antecedent.  And it will continue to  back  up 
  2523.         until  it  arrives at the question you asked,  at which point  it 
  2524.         will say:
  2525.  
  2526.              No.
  2527.  
  2528.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  2529.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  2530.         question (16).
  2531.  
  2532.  
  2533.  
  2534.  
  2535.         II.                          Grammar
  2536.  
  2537.              It is a boring subject, but it must be discussed. All PROLOG 
  2538.         statements are composed of valid terms, possibly a rule sign (":-
  2539.         "),  commas representing conjunction ("and"), and a period at the 
  2540.  
  2541.  
  2542.  
  2543.  
  2544.  
  2545.  
  2546.         very end.
  2547.              A term is a structure, constant, variable, or number.
  2548.          
  2549.              What is a structure? It is a kind of grouping:
  2550.  
  2551.              1) Structures consist of a functor, and a set of objects or
  2552.                 structures in parenthesis.
  2553.  
  2554.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  2555.                 which we have not discussed yet.
  2556.  
  2557.              3)  A  constant or functor must be a string of  letters  and 
  2558.                 numbers, beginning with a lower case letter, unless
  2559.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  2560.                 pardner'  ),  in  which  case you are  freed  from  these 
  2561.                 restrictions.
  2562.              4) A  variable  must be a string of  letters  and  numbers 
  2563.                 beginning with a capital letter.
  2564.              
  2565.              5) A  functor  may optionally have  arguments  enclosed  in 
  2566.                 parenthesis , as in: hascar( X ) or hascar. 
  2567.  
  2568.         An example:  "has( X, transportation )." is a structure.
  2569.  
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585.  
  2586.  
  2587.  
  2588.  
  2589.  
  2590.  
  2591.  
  2592.  
  2593.  
  2594.  
  2595.  
  2596.  
  2597.  
  2598.  
  2599.  
  2600.  
  2601.  
  2602.  
  2603.  
  2604.  
  2605.  
  2606.  
  2607.  
  2608.  
  2609.  
  2610.  
  2611.  
  2612.         III.                     Input / Output      
  2613.  
  2614.              You   now   know  enough  to  write  simple  databases   and 
  2615.         interrogate   them  profitably.   But  before  we  examine   more 
  2616.         sophisticated  examples,  it  will be necessary to add input  and 
  2617.         output to the language. There are built in functions which appear 
  2618.         as rules which are satisfied once. Thus the statment:
  2619.  
  2620.              write( 'Hello world.' ).
  2621.  
  2622.         can be included on the right side of a rule:
  2623.  
  2624.  
  2625.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  2626.         can  also write "write( X )" where X is some variable.  Note that 
  2627.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  2628.         which denote a constant, are required. Double quotes would denote 
  2629.         a list, which is another thing entirely.
  2630.  
  2631.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  2632.         function  "write"  is  executed and the message  printed  to  the 
  2633.         screen.
  2634.              The  builtin  read( X ) reads a "structure" that  you  input 
  2635.         from the keyboard. More formally, we have
  2636.  
  2637.              read( <variable> or <constant> )
  2638.              write( <variable> or <constant> )
  2639.  
  2640.         If you write read( Input ),  then the variable "keyboard" will be 
  2641.         assigned to whatever is typed at the keyboard,  provided that the 
  2642.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  2643.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  2644.         is a constant,  and the user at the keyboard did not type exactly 
  2645.         "baloney." 
  2646.  
  2647.         When you input a structure in response to a "read" statement,  be 
  2648.         sure to end it with a period and an <ENTER>. 
  2649.  
  2650.              There  is  a convenient way of putting the cursor on  a  new 
  2651.         line. This is the builtin "nl". For example:
  2652.  
  2653.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  2654.  
  2655.         would result in:
  2656.  
  2657.              line 1
  2658.              line 2
  2659.  
  2660.              There  is  also a primitive form of input/output for  single 
  2661.         characters. It will be discussed later.
  2662.  
  2663.  
  2664.  
  2665.  
  2666.  
  2667.  
  2668.  
  2669.  
  2670.  
  2671.  
  2672.  
  2673.  
  2674.  
  2675.  
  2676.  
  2677.  
  2678.         IV.                   A Fault Tree Example
  2679.  
  2680.              Consider the "won't start" fault tree for an automobile:
  2681.  
  2682.         {Car won't start}
  2683.              | 
  2684.              | 
  2685.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  2686.                 (Yes)                                       v
  2687.                  |                                  {Check battery}
  2688.                  |
  2689.         [Smell gasoline](yes) --> {Try full throttle cranking}
  2690.                  |                       (failure)
  2691.         /--------/                           |
  2692.         |           /------------------------/ 
  2693.         |           |                       
  2694.         |           |
  2695.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  2696.         |          (no)
  2697.         |           |
  2698.         |           |
  2699.         |  [Check for defective carburator](yes)--\
  2700.         |          (no)                           v
  2701.         |                                {Repair carburator}
  2702.         \----\
  2703.              |
  2704.              |
  2705.         [Is spark present](no)-->[Do points open and close](no)-\
  2706.              |                             (yes)                v
  2707.         /----/                               |          {Adjust points}
  2708.         |           /------------------------/
  2709.         |           |
  2710.         |  [Pull distributor wire, observe spark](blue)--\
  2711.         |        (orange)                                v
  2712.         |           |                           {Check plug wires & cap}
  2713.         |           |
  2714.         |  [Measure voltage on coil primary](not 12V)--\
  2715.         |         (12V)                                v
  2716.         |           |              {Check wiring, ballast resistor}
  2717.         |           |
  2718.         |  [Check condenser with ohmmeter](conducts)--\
  2719.         |    (no conduction)                          v
  2720.         |           |                         {Replace condenser}
  2721.         |           |
  2722.         |  [Open and close points](voltage not 0 - 12)--\
  2723.         |   (voltage swings 0 - 12)                     v
  2724.         |           |                         {Fix primary circuit}
  2725.         |           |
  2726.         |  {Consider hidden fault, swap components]
  2727.         |
  2728.         |
  2729.         \-------{Call a tow truck!!}
  2730.  
  2731.  
  2732.  
  2733.  
  2734.  
  2735.  
  2736.  
  2737.  
  2738.  
  2739.  
  2740.  
  2741.  
  2742.  
  2743.  
  2744.              A PROLOG program to  implement this is simple. Each statment 
  2745.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  2746.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  2747.         solution. 
  2748.  
  2749.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  2750.                             affirm, nl,
  2751.                             write( 'Check battery' ).
  2752.  
  2753.         'car wont start' :- write( 'Smell gasoline?' ), 
  2754.                             affirm, nl,
  2755.                             'fuel system'.
  2756.  
  2757.         'fuel system'    :- write( 'Try full throttle cranking' ).
  2758.  
  2759.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  2760.                             affirm, nl,
  2761.                             write( 'Replace fuel line.' ).
  2762.  
  2763.         'fuel system'    :- write( 'Check carburator' ).
  2764.  
  2765.         'car wont start' :- write( 'Is spark present?' ),
  2766.                             not( affirm ), nl,
  2767.                             'no spark'.
  2768.  
  2769.         'no spark'       :- write( 'Do points open and close?' ),
  2770.                             not( affirm ), nl,
  2771.                             write( 'Adjust or replace points.' ).
  2772.  
  2773.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  2774.                             affirm,
  2775.                             write( 'Check plug wires and cap.' ).
  2776.  
  2777.         'no spark'       :- write( 'What is the voltage on the primary
  2778.                              of the coil: ' ), 
  2779.                             read( Volts ), 
  2780.                             Volts < 10,
  2781.                             nl,
  2782.                             write('Check wiring and ballast resistor.').
  2783.  
  2784.         'no spark'       :- write( 'Does the capacitor leak?' ),
  2785.                             affirm,
  2786.                             write( 'Replace the capacitor.' ).
  2787.  
  2788.         'no spark'       :- not( 'primary circuit' ).
  2789.  
  2790.         'primary circuit' 
  2791.                          :- write( 'Open the  points.  Voltage  across 
  2792.                               coil?:'), nl,
  2793.                             read( Openvolts ), Openvolts < 1, 
  2794.                             write(  'Close the points.  Voltage  across 
  2795.                               coil?:'),
  2796.                             read( Closevolts ), Closevolts > 10, nl,
  2797.                             write( 'Primary circuit is OK.' ). 
  2798.  
  2799.         'no spark'       :- write( 'Consider a hidden fault. Swap
  2800.                               cap, rotor,points,capacitor.' ).
  2801.  
  2802.  
  2803.         'Car wont start' :- write( 'Get a tow truck!!' ).
  2804.  
  2805.  
  2806.  
  2807.  
  2808.  
  2809.  
  2810.  
  2811.  
  2812.                                  --End program--
  2813.  
  2814.  
  2815.              The  above  is  a  simple example of  an  expert  system.  A 
  2816.         sophisticated  system would tell you exactly the method by  which 
  2817.         it  has reached a conclusion.  It would communicate by a  "shell" 
  2818.         program  written  in PROLOG which would accept a wider  range  of 
  2819.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  2820.         interpreter directly.
  2821.  
  2822.  
  2823.  
  2824.  
  2825.  
  2826.  
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832.  
  2833.  
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.  
  2848.  
  2849.  
  2850.  
  2851.  
  2852.  
  2853.  
  2854.  
  2855.  
  2856.  
  2857.  
  2858.  
  2859.  
  2860.  
  2861.  
  2862.  
  2863.  
  2864.  
  2865.  
  2866.  
  2867.  
  2868.  
  2869.  
  2870.  
  2871.  
  2872.  
  2873.  
  2874.  
  2875.  
  2876.         V.                            Lists               
  2877.  
  2878.              Consider  a  shopping list given you by your wife.  It is  a 
  2879.         piece of paper with items written on it in an order that probably 
  2880.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  2881.         followed by carrots, hamburger, and finally a flea collar for the 
  2882.         dog, if you can find one. In PROLOG such a list would be written:
  2883.  
  2884.         1)   [eggs, carrots, hamburger, fleacollar]
  2885.  
  2886.         The  order of a list is important so that eggs and carrots cannot 
  2887.         be reversed and PROLOG be uncaring.
  2888.  
  2889.         Let us put the list in a structure:
  2890.  
  2891.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  2892.  
  2893.         Then  if you wished to isolate the head of the list you could ask 
  2894.         the question:
  2895.  
  2896.              shopping( [ Mostimportant | Rest ] ).
  2897.  
  2898.         and PROLOG would respond:
  2899.  
  2900.              Mostimportant   =  eggs,   
  2901.              Rest   =   [carrots,   hamburger, fleacollar].
  2902.  
  2903.         The vertical bar "|" is crucial here. It is the string extraction 
  2904.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  2905.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  2906.         separate the head of the list from the rest, or tail.
  2907.  
  2908.  
  2909.              You  may have gained the impression that PROLOG is a  rather 
  2910.         static language capable of answering simple questions,  but it is 
  2911.         far  more powerful than that.  The string extraction operator  is 
  2912.         the  key.  It permits PROLOG to whittle a complex expression down 
  2913.         to the bare remainder.  If the rules you have given it permit  it 
  2914.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  2915.         achieved. An example of this is the definition of "append."
  2916.  
  2917.              Let  us suppose you have not yet done yesterday's  shopping, 
  2918.         let alone today's. You pull it out of your wallet and sootch tape 
  2919.         it to the list your wife just gave you. Yesterday's list was:
  2920.  
  2921.              [tomatoes, onions, ketchup]
  2922.  
  2923.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  2924.  
  2925.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  2926.  
  2927.         To  take one list and to attach it to the tail of another list is 
  2928.         to  "append"  the first to the second.  The PROLOG definition  of 
  2929.         append is:
  2930.  
  2931.  
  2932.  
  2933.         Rule1:     append( [], L, L ).
  2934.  
  2935.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  2936.  
  2937.  
  2938.  
  2939.  
  2940.  
  2941.  
  2942.                       append( List1, List2, List3 ].
  2943.  
  2944.         The  general  scheme is this:  
  2945.  
  2946.         The definition consists of one rule and one fact.  The rule  will 
  2947.         be used over and over again until what little is left matches the 
  2948.         fact.  The [] stands for empty list,  which is like a bag without 
  2949.         anything in it. This is an example of a recursive definition.
  2950.              Suppose we ask:
  2951.  
  2952.              append( [a,b,c], [d,e,f], Whatgives ).
  2953.  
  2954.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  2955.         2. Rule 2 is invoked again with arguments:
  2956.              ( [b,c], [d,e,f], List3 ).
  2957.         3. Rule 2 is invoked again with arguments:
  2958.              ( [b], [d,e,f], List3 ).
  2959.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  2960.             matches. End.
  2961.  
  2962.         How does this cause a list to be constructed? The key is to watch 
  2963.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  2964.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  2965.         2. Now lets trace this as rule two is successivly invoked: 
  2966.  
  2967.  
  2968.                 Whatgives                                                
  2969.                    |                                                     
  2970.                    |                                                     
  2971.                    |                                                     
  2972.                    v                                                     
  2973.         Rule2:  [X|List3] (List1 = [b,c])                                
  2974.                  |  \                                                    
  2975.                  |   \                                                   
  2976.                  |    \                                                  
  2977.                  v     \                                                 
  2978.         Rule2:   a   [X'|List3'] (List1' = [c])                          
  2979.                       |    \                                             
  2980.                       |     \                                            
  2981.                       |      \                                           
  2982.                       v       \                                          
  2983.         Rule2:        b     [X''|List3''] (List1'' = [], ie., empty set.)
  2984.                               |    \                                     
  2985.                               |     \                                    
  2986.                               |      \                                   
  2987.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  2988.                                                                          
  2989.         End.
  2990.  
  2991.  
  2992.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  2993.         2 never alters List2. It supplies it to whatever rule it invokes. 
  2994.         So L in rule 1 is the original List2, or [a,b,c].
  2995.  
  2996.              This example would not have worked if the order of rules one 
  2997.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  2998.         attempts to use the the first rule encountered. You could imagine 
  2999.         it as always reading your program from the top down in attempting 
  3000.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  3001.         an  unpleasant  thing  would  have happened  if  the  order  were 
  3002.  
  3003.  
  3004.  
  3005.  
  3006.  
  3007.  
  3008.         reversed. The program would loop forever.
  3009.              
  3010.  
  3011.  
  3012.  
  3013.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  3014.         appetite. You should now purchase the book
  3015.  
  3016.              Programming In Prolog
  3017.              W.F. Clocksin and C.S. Mellish
  3018.              Springer - Verlag
  3019.              Berlin,Heidelberg,New York. 1981,1984
  3020.  
  3021.  
  3022.  
  3023.  
  3024.  
  3025.  
  3026.  
  3027.  
  3028.  
  3029.  
  3030.  
  3031.  
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039.  
  3040.  
  3041.  
  3042.  
  3043.  
  3044.  
  3045.  
  3046.  
  3047.  
  3048.  
  3049.  
  3050.  
  3051.  
  3052.  
  3053.  
  3054.  
  3055.  
  3056.  
  3057.  
  3058.  
  3059.  
  3060.  
  3061.  
  3062.  
  3063.  
  3064.  
  3065.  
  3066.  
  3067.  
  3068.  
  3069.  
  3070.  
  3071.  
  3072.  
  3073. read_in([W|Ws]) :- get0(C), readword(C,W,C1), restsent(W,C1,Ws).
  3074.  
  3075. restsent( W,_,[]) :- lastword(W), !.
  3076. restsent(W,C,[W1|Ws]) :- readword(C,W1,C1), restsent(W1,C1,Ws).
  3077.  
  3078. readword(C,W,C1) :- single_character(C), !, name(W,[C]), get0(C1).
  3079. readword(C,W,C2) :-
  3080.     in_word(C,NewC), !,
  3081.     get0(C1),
  3082.     restword(C1,Cs,C2),
  3083.     name(W,[NewC|Cs]).
  3084. readword(C,W,C2) :- get0(C1), readword(C1,W,C2).
  3085.  
  3086. restword(C,[NewC|Cs],C2) :-
  3087.     in_word(C,NewC), !,
  3088.     get0(C1),
  3089.     restword(C1,Cs,C2).
  3090. restword(C,[],C).
  3091.  
  3092. single_character(44).  /* , */
  3093. single_character(59).  /* ; */
  3094. single_character(58).  /* : */
  3095. single_character(63).  /* ? */
  3096. single_character(33).  /* ! */
  3097. single_character(46).  /* . */
  3098.  
  3099. in_word(C,C) :- C>96, C<123.                /* a b..z */
  3100. in_word(C,L) :- C>64, C<91, L is C+32.      /* A,B..Z */
  3101. in_word(C,C) :- C>47, C<58.                 /* 1,2,..9 */
  3102. in_word(39,39).                             /*  ' */
  3103. in_word(45,45).                             /* - */
  3104.  
  3105. lastword( '.' ).
  3106. lastword( '!' ).
  3107. lastword( '?' ).
  3108.  
  3109. answer(A) :- ratom( X ), conv( X, A ), !.
  3110.  
  3111. conv(X,I) :- atoi( X, I ), 0 < I, I < 4, !.
  3112. conv(X,A) :- name( X, String ), valid_resp( String, A ), !.
  3113.  
  3114.  
  3115. valid_resp( [H|T], A ) :- type_ans( H, A ).
  3116.  
  3117. type_ans( X, A ) :- ([X] = "h"; [X] = "H"), A = help.
  3118. type_ans( X, A ) :- ([X] = "w"; [X] = "W"), A = why.
  3119.  
  3120. valid_resp( [], A ) :-
  3121.     print('\nPlease try to give me a H,W, or number for an answer.'),
  3122.     answer( A ), !.
  3123.  
  3124. valid_resp( [H|T], A ) :- valid_resp( T, A ).
  3125.  
  3126.  
  3127.  
  3128. /* Makes new atoms, one at a time. Do not expect a repeat solution. 
  3129. You must ask each time you want an atom. It starts with some root,
  3130. and appends an incrementing number onto it. 
  3131.  
  3132. Ask: ?-gensym( student, X ).      get: X = student1.
  3133.      ?-gensym( student, X ).      get: X = student2.
  3134.      ?-gensym( student, X ).      get: X = student3.
  3135.  
  3136.     and ad infinitum. */
  3137.  
  3138. gensym( Root, Atom ) :-
  3139.     get_num( Root, Num ),
  3140.     name( Root, Name1 ),
  3141.     integer_name( Num, Name2 ),
  3142.     append( Name1, Name2, Name ),
  3143.     name( Atom, Name ).
  3144.  
  3145. get_num( Root, Num ) :-
  3146.     retract( current_num( Root, Num1 )), !,
  3147.     Num is Num1 + 1,
  3148.     asserta( current_num( Root, Num)).
  3149.  
  3150. get_num( Root, 1 ) :- asserta( current_num( Root, 1 )).
  3151.  
  3152. integer_name( Int, List ) :- integer_name( Int, [], List ).
  3153. integer_name( I, Sofar, [C|Sofar] ) :-
  3154.     I < 10, !, C is I + 48.
  3155. integer_name( I, Sofar, List ) :-
  3156.     Tophalf is I/10,
  3157.     Bothalf is I mod 10,
  3158.     C is Bothalf + 48,
  3159.     integer_name( Tophalf, [C|Sofar], List ).
  3160.  
  3161.  
  3162. append( [], L, L ).
  3163. append( [Z|L1], L2, [Z|L3] ) :- append( L1, L2, L3 ).
  3164.  
  3165. printstring( [] ).
  3166. printstring( [] ).
  3167. printstring( [H|T] ) :- put( H ), printstring( T ).
  3168.  
  3169. /*
  3170. .PN1
  3171. .PO0 */
  3172.  
  3173. signs :- nl, 
  3174.   print( 'Does the patient exhibit any of the following signs:' ), nl,
  3175.   print( 'weakness, lightheadedness, syncope, cardiac awareness,
  3176.   pallor, tachycardia, jaundice.' ), 
  3177.   affirm,
  3178.   syndrome( 1 ).
  3179.  
  3180. syndrome( 1 ) :- 'undist anemia'.
  3181.  
  3182. 'undist anemia' :-
  3183.    anemia( RBC ), 
  3184.    print( 'Patient has anemia. We now try to diagnose the specific type.' ),
  3185.   'anemia subtype'( RBC ).
  3186.  
  3187. 'anemia subtype'( RBC ) :-  'congenital hemolytic anemia'( RBC ).
  3188. 'anemia subtype'( RBC ) :-  'acquired hemolytic anemia'.
  3189.  
  3190. 'acquired hemolytic anemia' :-  
  3191.    ldh( high ),  nl,
  3192.    print('Based upon a diagnosis of anemia and ' ),  
  3193.    print(' high LDH we obtain acquired hemolytic anemia.' ).
  3194.  
  3195. 'congenital hemolytic anemia'( low )  :-
  3196.    'congenital hemolytic history', 
  3197.    'congenital hemolytic determinant', nl,
  3198.    print('Based upon a diagnosis of anemia and '), 
  3199.    print( 'the just named symptom we diagnose congenital hemolytic anemia.' ).
  3200.  
  3201. 'deficiency anemia' :- nl,
  3202.   print( 'Diagnosis is a deficiency anemia.' ).
  3203.  
  3204.  
  3205. 'congenital hemolytic history' :- jaundice.
  3206. 'congenital hemolytic history' :- gallstones.
  3207. 'congenital hemolytic history' :- sphenomegally.
  3208. 'congenital hemolytic history' :- hepatomegally.
  3209. 'congenital hemolytic history' :- 'bony malformations'.
  3210. 'congenital hemolytic history' :- 'mental retardation'.
  3211.  
  3212. 'congenital hemolytic determinant' :- microcytosis.
  3213. 'congenital hemolytic determinant' :- eliptocytosis.
  3214. 'congenital hemolytic determinant' :- spherocytosis.
  3215. 'congenital hemolytic determinant' :- anisopoikilocytosis.
  3216. 'congenital hemolytic determinant' :- 'anemia related to food'.
  3217.  
  3218. microcytosis :- labfindings( microcytosis ).
  3219. eliptocytosis :- labfindings( eliptocytosis ).
  3220. anisopoikilocytosis :- labfindings( anisopoikilocytosis ).
  3221. 'anemia related to food' :- evidence( 'anemia related to food' ).
  3222. spherocytosis :- nl,
  3223.      print( 'Is the % of spherocytosis > 50%' ), affirm.
  3224.  
  3225. anemia( RBC ) :- symptom( anemic ), rbc( RBC ).
  3226.  
  3227. symptom( anemic ) :- hemoglobin( low ).
  3228. symptom( anemic ) :- hematocrit( low ).
  3229.  
  3230. evidence( X ) :- nl,
  3231.   print('Has the patient evidence of '),
  3232.   print( X ), affirm.
  3233.  
  3234. labfindings( X ) :- nl,
  3235.   print('Are there laboratory findings of ' ),
  3236.   print( X ), affirm.
  3237.  
  3238. jaundice :- evidence( jaundice ).
  3239. gallstones :- evidence( gallstones ).
  3240. sphenomegally :- evidence( sphenomegally ).
  3241. hepatomegally :- evidence( hepatomegally ).
  3242. 'bony malformations' :- evidence( 'bony malformations' ).
  3243. 'mental retardation' :- evidence(  'mental retardation' ).
  3244. 'retarded growth and development' :-
  3245.      evidence( 'retarded growth and development' ).
  3246. 'crisis of viscera, bones' :-
  3247.      evidence( 'crisis of viscera, bones' ).
  3248.  
  3249.  
  3250. /* Laboratory measurements: */
  3251.  
  3252. rbc( HLN ) :- rbcmeas( RBC ), rbccat( RBC, HLN ).
  3253. rbccat( RBC, low ) :- RBC < 4.
  3254. rbccat( RBC, high) :- RBC > 6.
  3255. rbccat( RBC, norm ) :- RBC = 5.
  3256. rbcmeas(RBC) :- nl,
  3257.      print( 'Input the RBC in millions/microliter:' ),
  3258.      read( RBC ).
  3259.  
  3260. hematocrit( HLN ) :- hematocrtmeas( HEMAT ), hematcat( HEMAT, HLN ).
  3261. hematcat( HEMAT, low ) :- HEMAT < 36.
  3262. hematocrtmeas( HEMAT ) :- nl,
  3263.      print( 'What is the hematocrit level % per deciliter?:' ),
  3264.      read( HEMAT ).
  3265.  
  3266. mcv( low  ) :- mcv1( low ).
  3267. mcv( high ) :- mcv1( high ), not( arct( high ) ).
  3268.  
  3269.  
  3270. mcv1( HLN ) :- mcvmeas( MCV ), mcvcat( MCV, HLN ).
  3271. mcvcat( MCV, high) :- MCV > 96.
  3272. mcvcat( MCV, low ) :- MCV < 85.
  3273. mcvmeas( MCV ) :- nl,
  3274.   print( 'What is the level of MCV in cubic microns:' ),
  3275.   read( MCV ).
  3276.  
  3277. ldh( LDH ) :- nl,
  3278.   print( 'What is the level of LDH (high,low, or norm)?: ' ),
  3279.   read( LDH ).
  3280.  
  3281. arct( HLN ) :- arctmeas( ARCT ), arctcat( ARCT, HLN ).
  3282. arctmeas( ARCT ) :- nl,
  3283.   print( 'What is the absolute reticulocyte count in units of thousands:'),
  3284.   nl,
  3285.   read( ARCT ).
  3286.  
  3287. affirm :- nl, print( '(y./n.) ?:- ' ),  read( ANS ), nl, yes( ANS ).
  3288.  
  3289. yes( y ).
  3290. /* This is a truely silly program, since it is based on my own
  3291. medical knowledge. 
  3292. Ask: ?-itch.    or ?-lesion.    or ?-help. to get it started. */
  3293.  
  3294.  
  3295. help :- print( 'Diagnose the following topics:' ), nl, 
  3296. print( 'Itch.' ), nl, print( 'lesion' ).
  3297.  
  3298.  
  3299. itch :- print( 'Is the atmosphere dry?: ' ), 'say yes',
  3300.   print( 'Do not take so many showers. Use vaseline.' ).
  3301.  
  3302. itch :- print( 'Does the patient have an allergic history?: '), 
  3303.   'say yes', not(fever),  print( 'Consider atopic dermatitis.' ).
  3304.  
  3305. fever  :- 
  3306.   print( 'Does the patient have a fever?' ), 'say yes'.
  3307.  
  3308. 'non infective' :- acne, 'severe acne'.
  3309. 'non infective' :- acne, 'cystic acne'.
  3310. 'non infective' :- acne.
  3311. 'non infective' :- 'severe acne rosacea'.
  3312. 'non infective' :- 'rosacea'.
  3313.  
  3314. lesion :- not( fever ), 'non infective'.
  3315.  
  3316.  
  3317. acne :- 
  3318.   print( 'Is the skin oily?' ), 'say yes',
  3319.   print( 'Are there lots of pimples?' ), 'say yes',
  3320.   print( 'Condition is probably acne.' ). 
  3321.  
  3322. 'cystic acne' :- 
  3323.   print( 'Are there many yellowish cysts?' ), 'say yes', 
  3324.   print( 'Condition is cystic acne.' ).
  3325.  
  3326. 'severe acne' :- 
  3327.   print( 
  3328.     'Are there large elevated bluish abscesses with disfiguring scars?' ), 
  3329.   'say yes'.
  3330.  
  3331. 'rosacea' :- print( 'Is the patient  a woman?' ), 'say yes', 
  3332.              'acne rosacea'.
  3333.  
  3334.  
  3335. 'acne rosacea' :-  'severe'.
  3336. 'acne rosacea' :-  'mild'.
  3337.  
  3338. 'severe' :- 
  3339.   print( 'Does the patient have an enlarged nose, with growths?' ), 'say yes',
  3340.   print( 'Diagnosis is severe acne rosacea.' ).
  3341.  
  3342. 'mild' :- 
  3343.   print( 'Is the skin oily, with a tendency towards seborrhea?' ), 'say yes',
  3344.   print( 'Are there pustules surrounded by a reddish area?' ), 'say yes',
  3345.   print( 'But are they larger than ordinary acne eruptions?' ), 'say yes',
  3346.   print( 'Diagnosis is acne rosacea.' ).
  3347.  
  3348.  
  3349. 'say yes' :- read( Ans ), yes( Ans ), nl.
  3350.  
  3351.  
  3352. yes( yes ).
  3353. yes( y ).
  3354. yes( yea ).
  3355. yes( yup ).
  3356. /* 
  3357. Text substitution game.
  3358. Ask: ?-alter( [you, are, a, computer], Z ).
  3359.  
  3360. */
  3361.  
  3362. alter( [], [] ).
  3363. alter( [H|T], [X|Y] ) :- change( H, X ), alter( T, Y ).
  3364.  
  3365. change( you, i ).
  3366. change( are, [am, not ] ).
  3367. change( french, german ).
  3368. change( do, no ).
  3369. change( X, X ).
  3370.  
  3371. /* This is an unfinished expert system for rock blasting. It was orig-
  3372. inally written in another dialect and I have not had time to convert it.
  3373.  Go to work!. */
  3374.  
  3375.  
  3376.  
  3377. rock_blasting :- 
  3378.   print( 'Pleas answer each of the following questions by entering
  3379.      \na number in the form "1" or "2" or "3". etc., whichever appropriate.
  3380.      \nFor further information enter "help." or "why".' ),
  3381. (decision(X) ; X = error), !,
  3382. print( 'Based on your answers to the questions,' ),
  3383. meseg( X ).
  3384.  
  3385.  
  3386. ask( NQ, NA ) :- question( NQ ),
  3387.       answer( NA ),!,
  3388.       (NA = help, help( NQ), retry( ask( NQ, NA) ) );
  3389.       (NA = why, why( NQ ),  retry( ask(NQ,NA)   ) ).
  3390.  
  3391.  
  3392. meseg(1) :- print( 'Presplitting is feasible and recommended' ).
  3393. meseg(2) :- print( 'Presplitting is feasible but not recommended.' ).
  3394. meseg(3) :- print( 'Smooth blasting is recommended.' ).
  3395. meseg(4) :- print( 'Conventional blasting is recommended' ).
  3396. meseg(5) :- print( 'Presplitting is feasible but some experimentation     
  3397.     \nis necessary to obtain design parameters.' ).
  3398.  
  3399. meseg(error) :- print(
  3400.     '\nThere is an error in the answer to one of the questions.
  3401.     \nTo restart, type rock_blasting. Respond to each question with a 
  3402.     \nnumber in the range mentioned.' ).
  3403.  
  3404. question(1) :- print(
  3405.     '\nIs it critical to have a smooth rock surface and/or maintain the
  3406.     \nintegrity of the boundary rock?
  3407.     \n 1) Yes     2) No.' ).
  3408. help(1) :- print( 'if you don\'t care, you can just blast away.').
  3409. why(1)  :- print( 'If yes, more elaborate information is required before a 
  3410.     \ndecision can be made.' ).
  3411.  
  3412. question(2) :- print( 'Where is the blast?
  3413.     \n 1) On the surface    2) Underground' ).
  3414.         
  3415. help(2) :- print( 'Look up! Do you see sky?' ).
  3416.  
  3417. why(2) :- print( 'if on the surface, there are many options available.' ).
  3418.  
  3419. question(3) :-
  3420.     print( 'is the rock
  3421.       \n1) Hard( compressive strength > 100,000 MPa )?
  3422.       \n2) Soft( compressive strength < 100,000 MPa )?' ).
  3423.  
  3424. help(3) :- print( 'Measure the rock strength in MegaPascals.' ).
  3425. why(3) :- print( 'I don\'t know' ).
  3426.  
  3427.  
  3428. question(4) :- print( 'Is the bench height( or blasthole length ) < 50 feet?
  3429.     \n1) Yes   2) No.' ).
  3430.  
  3431. help(4) :- print( 'How deep did you make the hole?' ).
  3432. why(4) :- print( 'I don\'t know.' ).
  3433.  
  3434. question(5) :- print('Is the borehole drill capable of drilling a 2" to 4"
  3435.     \n blasthole?
  3436.     \n1) Yes      2) No.' ).
  3437.  
  3438. help(5) :- print( 'Go check the drill.' ).
  3439. why(5) :- print( 'I don\'t know' ).
  3440.  
  3441.  
  3442.  
  3443. question(6) :- print( 'Is the charge density of the explosive in the
  3444.     \nblasthole\n 1)High(>1.1 g/cc)?     2)Low(< 1.1g/cc)?' ).
  3445.  
  3446. help(6) :- print( 'go check while I hide behind a rock.' ).
  3447. why(6) :- print( 'I don\'t know.' ).
  3448.  
  3449. question(7) :- print( 'Is the rock mass
  3450.     \n1) Stratified with the proposed face parallel to the plane of
  3451.     \n the dominant fabric elements, or not
  3452.     \nstratified but heavily jointed?' ),
  3453.     print( 
  3454.     '\n\n2) Stratified and/or heavily jointed with the proposed face not
  3455.     \nparallel to the plane of the dominant fabric element?
  3456.  
  3457.     \n\n3)Jointed or fractured such that the blasting will create loose, 
  3458.     \nblocky conditions on theface?' ).
  3459.     
  3460. help(7) :- print( 'That is indeed a tough question!' ).
  3461. why(7) :- print( 'I don\'t know.' ).
  3462.  
  3463.  
  3464. question(8) :- print('Are the static field stresses
  3465.     \n1)Low( < 10 MPa ) with the principle stresses parallel,
  3466.     \nto the proposed face?
  3467.     \n\nLow( < 10MPa) but with the principle stresses parallel to
  3468.     \nthe proposed face?
  3469.     \n\n3)High( > 10 MPa )?' ).
  3470. help(8) :- print( 'That\'s a tough question' ).
  3471. why( 8) :- print( 'I don\'t know.' ).
  3472.  
  3473. decision(4) :- ask( 1,2 ).
  3474. decision(3) :- ask(1,1),ask(3,2).
  3475. decision(5) :- ask(1,1),ask(2,1),ask(3,2).
  3476. decision(4) :- ask(1,1),ask(2,1),ask(3,2).
  3477. decision(5) :- ask(1,1),ask(2,1),ask(3,1),ask(4,1),ask(5,2).
  3478. at1 :- ask(1,1),ask(2,1),ask(3,1),ask(4,1),ask(5,1).
  3479. decision(5) :- at1, ask(6,1).
  3480. decision(1) :- at1,ask(6,2),ask(7,1),ask(8,1).
  3481. decision(2) :- at1,ask(6,2),ask(7,1),ask(8,2).
  3482. decision(3) :- at1,ask(6,2),ask(7,1),ask(8,3).
  3483. decision(4)  :- at1,ask(6,2),ask(7,2).
  3484. decision(3) :- at1,ask(6,2),ask(7,3).
  3485.  
  3486.  
  3487. answer( A ) :- rnum( A ).
  3488. /*
  3489. answer(A) :- ratom( X ), conv( X, A ), !.
  3490.  
  3491. conv(X,I) :- atoi( X, I ), 0 < I, I < 4, !.
  3492. conv(X,A) :- name( X, String ), valid_resp( String, A ), !.
  3493.  
  3494.  
  3495. valid_resp( [H|T], A ) :- type_ans( H, A ).
  3496.  
  3497. type_ans( X, A ) :- ([X] = "h"; [X] = "H"), A = help.
  3498. type_ans( X, A ) :- ([X] = "w"; [X] = "W"), A = why.
  3499.  
  3500. valid_resp( [], A ) :-
  3501.     print('\nPlease try to give me a H,W, or number for an answer.'),
  3502.     answer( A ), !.
  3503.  
  3504. valid_resp( [H|T], A ) :- valid_resp( T, A ).
  3505. */
  3506.  
  3507.  
  3508.  
  3509.  
  3510.  
  3511. /* 
  3512. Note: Paula did not claim the prize of a free type VMI PROLOG
  3513. for a demonstration program. However, she is working on making
  3514. Pooh smarter, and I anticipate that she shortly will. (R.M). 
  3515. */
  3516.  
  3517.  
  3518. /* --------------- POOH : A PRO FOR THE AMATEUR --------------- */
  3519. /* -------------- Copyright 1985 Paula McKerall --------------- */
  3520.  
  3521. /* To wake pooh up, type: hello.<CR> when prompted "?-"         */
  3522. /* (Of course, type: consult(pooh).<CR> to get to his house!)   */
  3523.  
  3524. hello :- print( '\nHello! -- pooh is a program of very little brain,' ),
  3525.      print( '\nso please answer with: yes.<CR> if you mean "yes" -' ),
  3526.      print( '\nor with: no.<CR> if you mean "no" -' ),
  3527.      print( '\nor pooh will get confused!' ), nl,
  3528.      print( '\n(If you learn Prolog, you can make him smarter!)' ), nl,
  3529.      ask_want.
  3530. ask_want :- print( '\nDo you want advice about Prolog?' ), ((yes,
  3531.      ask_fun); (sorry; confused)).
  3532. ask_fun :- print( '\nDo you like to have fun?' ), ((learn; too_bad);
  3533.      confused).
  3534. learn :- yes, print( '\nThen please learn Prolog; you will like it.' ),
  3535.      print( '\nAnd then you can have a good time with pooh!' ), nl, naptime.
  3536. sorry :- no, print( '\nSorry, pooh can only give advice about Prolog.' ),
  3537.      print( '\nIf you learned Prolog, you could teach him other things!' ),
  3538.      nl, print( '\n(Besides, it is lonely in here; pooh needs a friend!)' ),
  3539.      nl, naptime.
  3540. too_bad :- no, print( '\nToo bad, because Prolog is fun.' ),
  3541.      print( '\nYou might change your mind if you try it!' ), nl,
  3542.      print( '\n(Besides, pooh is hungry, and if you learned Prolog,' ),
  3543.      print( '\nyou could teach him how to ask for honey!)' ), nl, naptime.
  3544. confused :- print( '\nPooh is most definitely confused.'  ), nl, naptime.
  3545. yes :- print( '\nPlease tell pooh: yes. or no. ?- ' ), read( yes );
  3546.      print( '\nThat is a strange answer. Do you mean yes?- '), read( yes ).
  3547. no :- print( '\nCuriouser and curiouser. Do you mean no?- ' ), read( yes );
  3548.      print( '\nPooh is getting confused. Do you mean yes?- '), read( no ).
  3549. naptime :- print( '\nPooh has to take a nap now.' ),
  3550.      print( '\nTo wake him up say: hello.<CR>' ),
  3551.      print( '\nOr to quit say: exitsys.<CR>' ).
  3552.  
  3553. /* This is really a silly little game; pooh behaves himself if you
  3554.    give him a chance, but I think it's fun to confuse him. Just don't
  3555.    make any errors in the "rules" or you'll get the Prolog error
  3556.    messenger, who is smarter than pooh but not as cuddly; unlike the
  3557.    error messenger, pooh can't say "No." *//*
  3558. Concatentate strings, using append. If the below is asked, 
  3559.  
  3560. ?-append( "ABC", "DEF", X ), append( "123", X, Y ), append( Y, "XYZ", Z ),
  3561.   printstring( Z ).
  3562.  
  3563.   you will get: Z = "123ABCDEFXYX", printed out as a list of ASCII codes. */
  3564.  
  3565. append( [], L, L ).
  3566. append( [Z|L1], L2, [Z|L3] ) :- append( L1, L2, L3 ).
  3567. printstring( [] ).
  3568. printstring( [H|T] ) :- put( H ), printstring( T ).
  3569.  
  3570.  
  3571. lips(L) :- rev( [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,20,
  3572.               21,22,23,24,25,26,27,28,29,30], L ).
  3573.  
  3574. rev( [], [] ).
  3575. rev( [H|T], L ) :- rev( T,Z), append( Z, [H], L ).
  3576. /* Improved bicycle program. Ask: ?-partlist( bike ). */
  3577.  
  3578.  
  3579. basicpart( rim ).
  3580. basicpart( rearframe ).
  3581. basicpart( gears ).
  3582. basicpart( nut ).
  3583. basicpart( spoke ).
  3584. basicpart( handles ).
  3585. basicpart( bolt ).
  3586. basicpart( fork ).
  3587.  
  3588. assembly( bike, [quant( wheel, 2 ), quant( frame, 1 )] ).
  3589. assembly( wheel, [quant( spoke, 20 ), quant( rim, 1 ), quant( hub, 1)] ).
  3590. assembly( frame, [quant( rearframe, 1), quant( frontframe, 1 ) ] ).
  3591. assembly( frontframe, [quant( fork, 1 ), quant( handles, 1 )] ).
  3592. assembly( hub, [quant( gears, 1 ), quant( bolt, 7 ), quant( axle, 1 ) ] ).
  3593. assembly( axle, [quant( bolt, 1 ), quant( nut, 2) ] ).
  3594.  
  3595. partlist( T ) :- partsof( 1, T, P ), collect( P, Q ), 
  3596.   printpartlist( Q ).
  3597.  
  3598. partsof( N, X, P ) :- assembly( X, S ), partsoflist( N, S, P ).
  3599. partsof( N,X,[quant(X,N)]) :- basicpart( X ).
  3600.  
  3601. partsoflist( _, [], [] ).
  3602. partsoflist( N, [quant( X, Num) | L ], T ) :-
  3603.   M is N * Num,
  3604.   partsof( M, X, Xparts ),
  3605.   partsoflist( N, L, Restparts ),
  3606.   append( Xparts, Restparts, T ).
  3607.  
  3608. collect( [], [] ).
  3609. collect( [quant(X, N )|R], [quant( X, Ntotal)|R2] ) :-
  3610.   collectrest( X, N, R, O, Ntotal ),
  3611.   collect( O, R2 ).
  3612.  
  3613. collectrest( _, N, [], [], N ).
  3614. collectrest( X, N, [quant( X, Num)|Rest ], Others, Ntotal ) :-
  3615.   !,
  3616.   M is N + Num,
  3617.   collectrest( X, M, Rest, Others, Ntotal ).
  3618. collectrest( X,N,[Other|Rest],[Other|Others],Ntotal ) :-
  3619.   collectrest( X, N, Rest, Others, Ntotal ).
  3620.  
  3621. printpartlist( [] ).
  3622. printpartlist( [quant( X, N )|R] ) :- nl,
  3623.   print( '    ' ), print( N ), print( '     ' ), 
  3624.   print( X ), printpartlist( R ).
  3625.  
  3626. append( [], L, L ).
  3627. append( [X|L1], L2, [X|L3] ) :- append( L1, L2, L3 ).
  3628.  
  3629. /* Ask ?-aless( 2, 3 ). Get: Yes.
  3630.    Ask ?-aless( 3, 2 ). Get: No. */
  3631.  
  3632.  
  3633. aless( X, Y ) :- name( X, L ), name( Y, M ), alessx( L, M ).
  3634. alessx( [], [_|_] ).
  3635. alessx( [X|_], [Y|_] ) :- X < Y.
  3636. alessx( [P|Q], [R|S] ) :- P = R, alessx( Q, S )./* Describe the parts required to make a bicycle. Firt the elementary parts
  3637. are given (basicpart). Then a description of various subassemblies.
  3638. Ask: ?-partsof( hub, P ). to get all the basic parts required to make a hub.
  3639. Ask: ?-partsof( bike, P ). for the whole bike. */
  3640.  
  3641.  
  3642. basicpart( rim ).
  3643. basicpart( rearframe ).
  3644. basicpart( gears ).
  3645. basicpart( nut ).
  3646. basicpart( spoke ).
  3647. basicpart( handles ).
  3648. basicpart( bolt ).
  3649. basicpart( fork ).
  3650.  
  3651. assembly( bike, [quant( wheel, 2 ), quant( frame, 1 )] ).
  3652. assembly( wheel, [quant( spoke, 20 ), quant( rim, 1 ), quant( hub, 1)] ).
  3653. assembly( frame, [quant( rearframe, 1), quant( frontframe, 1 ) ] ).
  3654. assembly( frontframe, [quant( fork, 1 ), quant( handles, 1 )] ).
  3655. assembly( hub, [quant( gears, 1 ), quant( axle, 1 ) ] ).
  3656. assembly( axle, [quant( bolt, 1 ), quant( nut, 2) ] ).
  3657.  
  3658. partsof( X, [X] ) :- basicpart( X ).
  3659. partsof( X, P ) :- assembly( X, Subparts ),
  3660.                    partsoflist( Subparts, P ).
  3661.  
  3662. partsoflist( [], [] ).
  3663. partsoflist( [quant( X,N ) | Tail ], Total ) :-
  3664.              partsof( X, Headparts ),
  3665.              partsoflist( Tail, Tailparts ),
  3666.              append( Headparts, Tailparts, Total ).
  3667.  
  3668. append( [], L, L ).
  3669. append( [X|L1], L2, [X|L3] ) :- append( L1, L2, L3 ).
  3670. /* To analyze the family structure of the family of Queen Victoria.
  3671. English friend of mine notes there wasn't a Harry. I put him in.
  3672.  
  3673. Answers the compelling question: Who is X the sister of?
  3674.  
  3675. Ask: ?-sisterof( alice, X ). or ?-sisterof( alice, harry ).
  3676.  
  3677.   or ?-sisterof( alice, X ), loves( X, wine ). as an example of a
  3678.   complex question. 
  3679.  
  3680. or even:
  3681.  
  3682. sisterof( alice, X ), loves( X, wine ), loves( alice, wine ).
  3683.  
  3684. */
  3685.  
  3686. sisterof( X, Y ) :- parents( X, M, F ), 
  3687.                     female( X ),
  3688.                     parents( Y, M, F ).
  3689.  
  3690. parents( edward, victoria, albert ).
  3691. parents( harry, victoria, albert ).
  3692. parents( alice, victoria, albert ).
  3693. female( alice ).
  3694.  
  3695. loves( harry, wine ).
  3696. loves( alice, wine ).
  3697. /* Recursive member of list definition. 
  3698.  
  3699. Ask: Member( X, [a,b,c,d,e,f] ) to get successively all
  3700. the members of the given list. */
  3701.  
  3702.  
  3703. member( Y, [Y|_] ).
  3704.  
  3705. member( B, [_|C] ) :-  member( B, C ).
  3706.  
  3707.  
  3708.  
  3709. /*  
  3710. For a similar program, see Clocksin & Mellish page 165.
  3711.  
  3712. Plan a trip from place to place.
  3713.  
  3714. An appropriate question would be:
  3715.  
  3716. ?-go( darlington, workington, X ).
  3717. */
  3718.  
  3719.   append( [], L, L ).
  3720.   append( [Z|L1], L2, [Z|L3] ) :- append( L1, L2, L3 ).
  3721.   printstring( [] ).
  3722.   printstring( [H|T] ) :- put( H ), printstring( T ).
  3723.  
  3724.  
  3725.   rev( [], [] ).
  3726.   rev( [H|T], L ) :- rev( T,Z), append( Z, [H], L ).
  3727.  
  3728. /* Recursive member of list definition. 
  3729.  
  3730. Ask: Member( X, [a,b,c,d,e,f] ) to get successively all
  3731. the members of the given list. */
  3732.  
  3733.  
  3734. member( Y, [Y|_] ).
  3735.  
  3736. member( B, [_|C] ) :-  member( B, C ).
  3737.  
  3738.  
  3739. pp([H|T],I)  :-  !, J is I+3, pp(H,J), ppx(T,J), nl.
  3740. pp(X,I)  :- tab(I), print(X), nl.
  3741. ppx([],_).
  3742. ppx([H|T],I)  :-  pp(H,I),ppx(T,I).
  3743.  
  3744. /* see page 163 of CM  */
  3745.  
  3746. findall(X,G,_) :-
  3747.       asserta(found(mark)),
  3748.       G,
  3749.       asserta(found(X)),
  3750.       fail.
  3751. findall(_,_,L)  :-  collect_found([],M),!, L  =  M.
  3752. collect_found(S,L)  :-  getnext(X), !, collect_found([X|S],L).
  3753. collect_found(L,L).
  3754. getnext(X)  :-  retract(found(X)), !, X \== mark.
  3755.  
  3756. a(newcastle,carlisle,58).
  3757. a(carlisle,penrith,23).
  3758. a(darlington,newcastle,40).
  3759. a(penrith,darlington,52).
  3760. a(workington, carlisle,33).
  3761. a(workington, penrith,39).
  3762.  
  3763. /*  does ; work properly ? */
  3764.  
  3765. legalnode(X,Trail,Y) :- a(Y,X,_), (not(member(Y,Trail))).
  3766. legalnode(X,Trail,Y) :- a(X,Y,_), (not(member(Y,Trail))).
  3767.  
  3768.  
  3769. go(Start,Dest,Route) :-  go1([[Start]],Dest,R), rev(R, Route).
  3770. go1([First|Rest],Dest,First) :- First = [Dest|_].
  3771. go1([[Last|Trail]|Others],Dest,Route)  :-
  3772.       findall([Z,Last|Trail],legalnode(Last,Trail,Z),List),
  3773.       append(List,Others,NewRoutes),
  3774.       go1(NewRoutes,Dest,Route).
  3775. /*                    SIEVE OF ERASTHATONES
  3776.  
  3777.  
  3778. Ask ?-primes( 100, L ).   to get all the primes from 1 to 100
  3779. printed out as the list "L".  */
  3780.  
  3781. primes( Limit, Ps ) :- integers( 2, Limit, Is ), sift( Is, Ps ).
  3782.  
  3783. integers( Low, High, [Low|Rest] ) :-
  3784.    Low =< High, !, M is Low+1, integers(M, High, Rest ).
  3785. integers( _,_,[] ).
  3786.  
  3787. sift( [], [] ).
  3788. sift( [I|Is], [I|Ps]) :- remove(I,Is,New), sift( New, Ps ).
  3789.  
  3790. remove(P,[],[]).
  3791. remove(P,[I|Is],[I|Nis]) :-
  3792.    not( 0 is I mod P ),
  3793.    !,
  3794.    remove(P, Is, Nis).
  3795.  
  3796. remove(P,[I|Is],Nis) :- 0 is I mod P, remove(P, Is, Nis).
  3797.  
  3798.  
  3799. con_num_list( [H|T] ) :- rnum( H ), con_num_list( T ).
  3800. con_num_list( [] ).
  3801.  
  3802.  
  3803. /* To multiply a row by a column: */
  3804.  
  3805. mat_mult( [H1|T1], [H2|T2], res ) :-
  3806.     mat_mult1( [H1|T1], [H2|T2, 0, res ).
  3807.  
  3808. mat_mult1( [H1|T1], [H2|T2], sum, res ) :-
  3809.     sum is sum + H1 * H2,
  3810.     mat_mult1( T1, T2, sum, res ).
  3811.  
  3812. mat_mult1( [], [], sum, res ) :- res = sum.
  3813.  
  3814.  
  3815. /* To get the nth element of a list: 
  3816.    Let us assume a matrix in the form of a list of columns: 
  3817.  
  3818. [ L1, L2.....]
  3819.  
  3820. listel( N, [H|T], X ) :- 
  3821.     N > 0, 
  3822.     N is N - 1,
  3823.     listel( N, T, X ).
  3824.  
  3825. listel( N, [H|_], X ) :- X = H.
  3826.  
  3827. /* Below, X, Y are the "coordinates".
  3828.    L is the complex list representing the array.
  3829.    element is the returned value. */
  3830.  
  3831. matrix_el( X, Y, L, element ) :-
  3832.  
  3833. /* First get the row, represented as an element "rowel" in list L */
  3834.  
  3835.     listel( X, L, rowel ),
  3836.  
  3837. /* And now the value contained within the row. */
  3838.     listel( Y, rowel, element ).
  3839. get_answer( A ) :- 
  3840.     ratom( X ), name( X, String ),
  3841.         valid_resp( String, A ), !.
  3842.  
  3843. valid_resp( [H|T], A ) :- type_ans( H, A ).
  3844.  
  3845. type_ans( X, A ) :- ([X] = "y"; [X] = "Y"), A = yes.
  3846. type_ans( X, A ) :- ([X] = "n"; [X] = "N"), A = no.
  3847. type_ans( X, A ) :- ([X] = "w"; [X] = "W"), A = why.
  3848.  
  3849. ?-print('\nYou can start animal by typing "help.<CR>"\n' ).
  3850.  
  3851. valid_resp( [], A ) :-
  3852.     print('\nPlease try to give me a yes or no answer.'),
  3853.     get_answer( A ), !.
  3854.  
  3855. valid_resp( [H|T], A ) :- valid_resp( T, A ).
  3856.  
  3857. /*
  3858. Note: Carl is an A.D.A. PROLOG user who has contributed this program for
  3859. the enjoyment of others.
  3860.  
  3861.           Residential Air Conditioning Diagnosis System
  3862.                          by Carl Bredlau
  3863.                         909 Rahway Avenue
  3864.                    Westfield, New Jersey 07090
  3865. */
  3866.  
  3867.  
  3868. nothing(X)  :-
  3869.      print('Is there a/c (y/n)? '),
  3870.      ratom(n), !, X = running,
  3871.      asserta( (nothing(running) :- !) ).
  3872.  
  3873. nothing(X)   :-
  3874.       print('I can not diagnose this.  Will quit'), nl,
  3875.       asserta( (nothing(running) :- (!, fail)) ), fail.
  3876.  
  3877. check(thermostat_system_switch) :-
  3878.       nothing(running).
  3879.  
  3880.  
  3881. check(thermostat_fan_switch) :-
  3882.       check(thermostat_system_switch),
  3883.       switch(thermostat_system_switch,'in the cool position',yes).
  3884.       thermostat(calling).
  3885.  
  3886. check(for_air)   :-
  3887.      check(thermostat_fan_switch),
  3888.      switch(thermostat_fan_switch,on,yes),
  3889.      fan(furnace,on).
  3890.  
  3891. check(furnace_24_volts) :-
  3892.        check(thermostat_fan_switch),
  3893.        switch(thermostat_fan_switch,on,yes),
  3894.        fan(furnace,off).
  3895.  
  3896. check(furnace_110_volts) :-
  3897.     check(furnace_24_volts),
  3898.     voltage(furnace,24,no).
  3899.  
  3900. check(service_switch) :-
  3901.      check(furnace_110_volts),
  3902.      voltage(furnace,110,no).
  3903.  
  3904. check(circuit_breaker) :-
  3905.       check(service_switch),
  3906.       voltage('service switch',line,110,no).
  3907.  
  3908. check(coil_of_fan_relay) :-
  3909.       check(furnace_24_volts),
  3910.       voltage(furnace,24,yes).
  3911.  
  3912. check(fan,closed_coil_relays) :-
  3913.       check(coil_of_fan_relay),
  3914.       voltage( fan_relay_coil,24,yes).
  3915.  
  3916. check(Which,continuity_at_relay_coil) :-
  3917.       check(Which,closed_coil_relays),
  3918.       coil_contacts(Which,open).
  3919.  
  3920. check(Which,voltage_at_motor) :-
  3921.       check(Which,closed_coil_relays),
  3922.       coil_contacts(Which,closed).
  3923.  
  3924. check(Which,over_amp) :-
  3925.      check(Which,voltage_at_motor),
  3926.      motor_voltage(Which,line,yes),
  3927.      motor(Which,running,no),
  3928.      motor(Which,hot,yes),
  3929.      print( Which, 'motor has internal overload -- thermal relay kicked off'), nl,
  3930.      print( 'Cool and check for overamperage'), nl.
  3931.  
  3932. check(Which,motor_continuity) :-
  3933.      (check(Which,voltage_at_motor);
  3934.       (Which = compressor, check(continuity_at_compressor) )),
  3935.      motor_voltage(Which,line,yes),
  3936.      motor(Which,running,no),
  3937.      motor(Which,hot,no).
  3938.  
  3939. check(condensing_unit) :-
  3940.     check(for_air),
  3941.     air(yes),
  3942.     switch(system_cooling_switch,on,yes).
  3943.  
  3944. check(condenser_24_volts) :-
  3945.     check(condensing_unit),
  3946.     fan(condenser,off),
  3947.     voltage(condenser,220,yes).
  3948.  
  3949. check(condensing_unit_breaker) :-
  3950.      check(condensing_unit),
  3951.      fan(condenser,off),
  3952.      voltage(condenser,220,no),
  3953.      switch(disconnect,on,yes).
  3954.  
  3955. check(compressor_contactor_coil) :-
  3956.      check(condenser_24_volts),
  3957.      voltage(condenser,24,yes).
  3958.  
  3959. check(condenser,closed_coil_relays) :-
  3960.      check(compressor_contactor_coil),
  3961.      voltage(compressor_contactor_coil,24,yes),
  3962.      safeties(yes).
  3963.  
  3964. check(compressor_voltage) :-
  3965.      check(condensing_unit),
  3966.      fan(condenser,on),
  3967.      voltage(condenser,220,yes),
  3968.      voltage(condenser,24,yes),
  3969.      coil_contacts(compressor,closed),
  3970.      motor(compressor,running,no).
  3971.  
  3972. check(compressor_internals_overload) :-
  3973.      check(compressor_voltage),
  3974.      motor_voltage(compressor,line,yes).
  3975.  
  3976.  
  3977. check(continuity_at_compressor) :-
  3978.      check(compressor_internals_overload),
  3979.      coil_contacts(compressor_internals,closed).
  3980.  
  3981. check(gauges) :-
  3982.      check(condensing_unit),
  3983.      fan(condenser,on),
  3984.      voltage(condenser,220,yes),
  3985.      voltage(condenser,24,yes),
  3986.      coil_contacts(compressor,closed),
  3987.      motor(compressor,running,yes),
  3988.      gauge(X).
  3989.  
  3990.  
  3991.  
  3992.  
  3993. diagnosed(burned_out_transformer) :-
  3994.         check(furnace_110_volts),
  3995.         voltage(furnace,110,yes),
  3996.         print('Transformer burned out. Replace transformer'),nl.
  3997.  
  3998.  
  3999. diagnosed(repair_line_voltage_wire) :-
  4000.        check(service_switch),
  4001.        voltage('service switch',lOad,110,yes),
  4002.        print( 'Repair line voltage wire'), nl.
  4003.  
  4004.  
  4005. diagnosed(replace_service_switch) :-
  4006.         check(service_switch),
  4007.         voltage('service switch',lOad,110,no),
  4008.         voltage('service switch',line,110,yes),
  4009.         print( 'Replace service switch if switch is on'), nl,
  4010.         print( 'Otherwise, turn on switch'), nl.
  4011.  
  4012. diagnosed(replace_circuit_breaker) :-
  4013.         check(circuit_breaker),
  4014.         voltage('circuit breaker',lOad,110,no),
  4015.         voltage('circuit breaker',line,110,yes),
  4016.         breaker(circuit,on),
  4017.         print( 'Replace circuit breaker' ), nl.
  4018.  
  4019. diagnosed(repair_circuit_wire) :-
  4020.         check(circuit_breaker),
  4021.         voltage( 'service switch',line,110,no),
  4022.         voltage( 'circuit breaker',lOad,110,yes),
  4023.         print( 'Repair wire between circuit breaker and switch'),
  4024.         nl.
  4025.  
  4026. diagnosed(breaker_switch) :-
  4027.          check(circuit_breaker),
  4028.          breaker(circuit,off),
  4029.          print( 'Please turn on the breaker'), nl.
  4030.  
  4031. diagnosed(check_for_grounds) :-
  4032.         check(circuit_breaker),
  4033.         breaker(circuit,tripped),
  4034.         print('Check for grounded load'), nl,
  4035.         print('i.e., motor, relay, transformer, burnt ground wire'),nl.
  4036.  
  4037. diagnosed(replace_subbase) :-
  4038.        check(coil_of_fan_relay),
  4039.        voltage( fan_relay_coil,24,no),
  4040.        print('Jump R to G at thermostat'),nl,
  4041.        fan_turns(furnace,yes),
  4042.        print('Replace subbase of thermostat'), nl.
  4043.  
  4044.  
  4045. diagnosed(replace_therm_wire) :-
  4046.        check(coil_of_fan_relay),
  4047.        voltage( fan_relay_coil,24,no),
  4048.        fan_turns(furnace,no),
  4049.        print('Look for open wire between thermostat and subbase'), nl.
  4050.  
  4051. diagnosed([Which,replace_relay]) :-
  4052.       check(Which,continuity_at_relay_coil),
  4053.       continuity(Which,no),
  4054.       print( 'Replace ',Which, ' relay'), nl.
  4055.  
  4056. diagnosed([Which,replace_relay]) :-
  4057.       check(Which,continuity_at_relay_coil),
  4058.       continuity(Which,yes),
  4059.       print( 'Check for mechanical failure and replace ',Which, ' relay'), nl.
  4060.  
  4061. diagnosed([Which,repair_wire]) :-
  4062.      check(Which,voltage_at_motor),
  4063.      motor_voltage(Which,line,no),
  4064.      print( 'Repair wire between relay and ',Which,'?'), nl.
  4065.  
  4066. diagnosed([Which,replace_motor_or_adjust_pulley]) :-
  4067.      check(Which,over_amp),
  4068.      motor(Which,over_amperage,yes),
  4069.      print( 'With direct drive replace motor.'),nl,
  4070.      print( 'With pulley drive adjust pulley to proper amperage'),nl.
  4071.  
  4072. diagnosed([Which,replace_motor]) :-
  4073.      check(Which,motor_continuity),
  4074.      continuity(Which,no),
  4075.      print('Replace ',Which,' motor'),nl.
  4076.  
  4077. diagnosed([Which,look_at_capacitor]) :-
  4078.      check(Which,motor_continuity),
  4079.      continuity(Which,yes),
  4080.      motor(Which,hum,yes),
  4081.      print('Look at the ',Which,' motor capacitor'),nl.
  4082.  
  4083. diagnosed(dirty_stuff) :-
  4084.      check(for_air),
  4085.      air(no),
  4086.      maintenance(List),
  4087.      perform_maintenance(List).
  4088.  
  4089. diagnosed(replace_thermostat) :-
  4090.     check(condenser_24_volts),
  4091.     voltage(condenser,24,no),
  4092.     print('Jump out R to Y at thermostat'),
  4093.     fan_turns(condenser,yes),
  4094.     print('Replace thermostat'), nl.
  4095.  
  4096. diagnosed(repair_condenser_wire) :-
  4097.     check(condenser_24_volts),
  4098.     voltage(condenser,24,no),
  4099.     fan_turns(condenser,no),
  4100.     print('Repair wire between thermostat and condenser'),nl,
  4101.     print('Check 24 volt source'),nl.
  4102.  
  4103.  
  4104.  
  4105. diagnosed(replace_condensing_unit_breaker) :-
  4106.         check(condensing_unit_breaker),
  4107.         voltage('condensing unit breaker',lOad,110,no),
  4108.         voltage('condensing unit breaker',line,110,yes),
  4109.         breaker(condensing_unit,on),
  4110.         print( 'Replace condensing unit breaker' ), nl.
  4111.  
  4112. diagnosed(repair_condensing_unit_wire) :-
  4113.         check(condensing_unit_breaker),
  4114.         voltage( 'condensing unit breaker',lOad,110,yes),
  4115.         print( 'Repair wire between condensing unit breaker and switch'),
  4116.         nl.
  4117.  
  4118. diagnosed(condensing_unit_switch) :-
  4119.          check(condensing_unit_breaker),
  4120.          breaker(condensing_unit,off),
  4121.          print( 'Please turn on the condensing unit breaker'), nl.
  4122.  
  4123.  
  4124. diagnosed(check_for_condensing_unit_grounds) :-
  4125.         check(condensing_unit_breaker),
  4126.         breaker(condensing_unit,tripped),
  4127.         print('Check for grounded load'), nl,
  4128.         print('i.e., motor, relay, transformer, burnt ground wire'),nl.
  4129.  
  4130.  
  4131. diagnosed(safeties_open) :-
  4132.      check(compressor_contactor_coil),
  4133.      voltage(compressor_contactor_coil,24,no),
  4134.      safeties(no),
  4135.      print('Check safeties'),nl.
  4136.  
  4137. diagnosed(compressor_contactor) :-
  4138.      check(compressor_voltage),
  4139.      motor_voltage(compressor,line,no),
  4140.      print('Check for contactor problem or wire open'), nl.
  4141.  
  4142. diagnosed(refrigerant_charge) :-
  4143.      check(compressor_internals_overload),
  4144.      coil_contacts(compressor_internals,open),
  4145.      motor(compressor,hot,yes),
  4146.      print( 'Cool and check refrigerant charge'), nl.
  4147.  
  4148. diagnosed(replace_compressor) :-
  4149.      check(compressor_internals_overload),
  4150.      coil_contacts(compressor_internals,open),
  4151.      motor(compressor,hot,no),
  4152.      print( 'Replace compressor'), nl.
  4153.  
  4154. diagnosed(defective_condenser_fan) :-
  4155.      check(condensing_unit),
  4156.      fan(condenser,off),
  4157.      voltage(condenser,220,yes),
  4158.      voltage(condenser,24,yes),
  4159.      coil_contacts(compressor,closed),
  4160.      motor(compressor,running,yes),
  4161.      print('Defective condenser fan'),nl.
  4162.  
  4163. diagnosed(leak) :-
  4164.     check(gauges),
  4165.     pressure(high,High),
  4166.     pressure(low,Low),
  4167.     Low = 0, High = 0,
  4168.     print('Find leak, repair, and recharge'),nl.
  4169.  
  4170. diagnosed(replace_compressor) :-
  4171.     check(gauges),
  4172.     pressure(high,High),
  4173.     pressure(low,Low),
  4174.     60 =< Low, Low =< 90,
  4175.     60 =< High, High =< 90,
  4176.     Diff is High - Low,
  4177.     Diff =< 10,
  4178.     motor(compressor,drawing_amperage,yes),
  4179.     print('Pressures are almost equal.  Replace compressed'),nl.
  4180.  
  4181. diagnosed(low_pressure) :-
  4182.     check(gauges),
  4183.     pressure(low,Low),
  4184.     Low =< 40,
  4185.     print('(1) Check for lack of maintenance on inside'), nl,
  4186.     maintenance(X),
  4187.     print_maintenance_start(X),
  4188.     print('(2) short of coolant'), nl,
  4189.     print('(3) restriction in refrigerant line'), nl,
  4190.     print('(4) expansion valve problem'), nl.
  4191.  
  4192. diagnosed(high_pressure) :-
  4193.     check(gauges),
  4194.     pressure(high,High),
  4195.     High >= 300,
  4196.     print('(1) Lack of maintenance on outside : compressor plugged'),nl,
  4197.     print('(2) Overcharged unit'),nl.
  4198.  
  4199.  
  4200. diagnosed(compressor_valve) :-
  4201.     check(gauges),
  4202.     pressure(low,Low),
  4203.     pressure(high,High),
  4204.     65 =< Low, Low =< 70,
  4205.     220 =< High, High =< 280,
  4206.     maintenance(X),
  4207.     print_maintenance_start(X),
  4208.     motor(compressor,low_amperage,yes),
  4209.     print('Possible broken compressor valve. Replace'),nl.
  4210.  
  4211.  
  4212. diagnosed(replace_compressor) :-
  4213.     check(gauges),
  4214.     pressure(low,Low),
  4215.     pressure(high,High),
  4216.     65 =< Low, Low =< 70,
  4217.     220 =< High, High =< 280,
  4218.     maintenance(X),
  4219.     print_maintenance_start(X),
  4220.     motor(compressor,high_amperage,yes),
  4221.     print('Compressor worn out. Replace'), nl.
  4222.  
  4223.  
  4224.  
  4225.  
  4226. possibilities( [
  4227.      burned_out_transformer,
  4228.      replace_service_switch,
  4229.      repair_line_voltage_wire,
  4230.      replace_circuit_breaker,
  4231.      repair_circuit_wire,
  4232.      breaker_switch,
  4233.      check_for_grounds,
  4234.      replace_subbase,
  4235.      repair_therm_wire,
  4236.      [fan,replace_relay],
  4237.      [fan,repair_wire],
  4238.      [fan,replace_motor_or_adjust_pulley],
  4239.      [fan,replace_motor],
  4240.      [fan,look_at_capacitor],
  4241.      dirty_stuff,
  4242.      replace_thermostat,
  4243.      repair_condenser_wire,
  4244.      replace_condensing_unit_breaker,
  4245.      repair_condensing_unit_wire,
  4246.      condensing_unit_switch,
  4247.      check_for_condensing_unit_grounds,
  4248.      safeties_open,
  4249.      [condenser,replace_relay],
  4250.      [condenser,repair_wire],
  4251.      [condenser,replace_motor_or_adjust_pulley],
  4252.      [condenser,replace_motor],
  4253.      [condenser,look_at_capacitor],
  4254.      compressor_contactor,
  4255.      refrigerant_charge,
  4256.      replace_compressor,
  4257.      [compressor,replace_motor],
  4258.      [compressor,look_at_capacitor],
  4259.      defective_condenser_fan,
  4260.      leak,
  4261.      low_pressure,
  4262.      high_pressure,
  4263.      compressor_valve
  4264.   ]).
  4265.  
  4266. begin :-
  4267.     print('Expert a/c system'), nl,
  4268.     possibilities(X),
  4269.     begin1(X).
  4270.  
  4271. begin1([]) :-
  4272.     print('Seems I could not find the problem'), nl,
  4273.     print('Better luck next time'), !.
  4274.  
  4275. begin1([H|T]) :-
  4276.        diagnosed(H), ! ,
  4277.        print('We diagnosed ',H), nl,
  4278.        print('I hope that this is it'), nl.
  4279.  
  4280. begin1([H|T]) :-
  4281.        !, begin1(T).
  4282.  
  4283.  
  4284.  
  4285.  
  4286.  
  4287. switch(Switch,How,YesNo) :-
  4288.         print('Is the ',Switch,' ',How,' (y/n)? '),
  4289.         ratom(y),
  4290.         !,
  4291.         asserta( (switch(Switch,How,Y) :- (!, Y = yes)) ),
  4292.         YesNo = yes.
  4293.  
  4294. switch(Switch,How,YesNo) :-
  4295.         print('Please turn on the ',Switch, ' now'), nl,
  4296.         asserta( (switch(Switch,How,Y) :- (!, Y = yes)) ),
  4297.         YesNo = yes.
  4298.  
  4299. thermostat(X) :-
  4300.         print('Is the thermostat calling (y/n)? '),
  4301.         ratom(y),
  4302.         !,
  4303.         asserta( (thermostat(Y) :- (!, Y = calling)) ),
  4304.         X = calling.
  4305.  
  4306. thermostat(X) :-
  4307.         print('Please make call for cooling'),
  4308.         asserta( (thermostat(Y) :- (!, Y = calling)) ),
  4309.         X = calling.
  4310.  
  4311. fan(Which,OnOff) :-
  4312.         print('Do you hear the ',Which,' fan (y/n)? '),
  4313.         ratom(y),
  4314.         !,
  4315.         asserta( (fan(Which,Y) :- (!, Y = on)) ),
  4316.         OnOff = on.
  4317.  
  4318. fan(Which,OnOff) :-
  4319.         asserta( (fan(Which,Y) :- (!, Y = off)) ),
  4320.         OnOff= off.
  4321.  
  4322. voltage(X,Y,Volts,Z)   :-
  4323.        print( 'Is there ', Volts, ' volts on the ',
  4324.        Y, ' side of the ',
  4325.        X, ' (y/n)?  '),
  4326.        ratom(y),
  4327.        !,
  4328.        asserta( (voltage(X,Y,Volts,W) :- (!, W = yes)) ),
  4329.        Z = yes.
  4330.  
  4331. voltage(X,Y,Volts,Z)   :-
  4332.        asserta( (voltage(X,Y,Volts,W) :- (!, W = no)) ),
  4333.        Z = no.
  4334.  
  4335. voltage(X,Volts,Z)   :-
  4336.        print( 'Is there ', Volts, ' volts at the ',
  4337.        X, '? (y/n) '),
  4338.        ratom(y),
  4339.        !,
  4340.        asserta( (voltage(X,Volts,W) :- (!, W = yes)) ),
  4341.        Z = yes.
  4342.  
  4343. voltage(X,Volts,Z)   :-
  4344.        asserta( (voltage(X,Volts,W) :- (!, W = no)) ),
  4345.        Z = no.
  4346.  
  4347. motor_voltage(X,Volts,Z)   :-
  4348.        print( 'Is there ', Volts, ' volts at the ',
  4349.        X, ' motor? (y/n) '),
  4350.        ratom(y),
  4351.        !,
  4352.        asserta( (motor_voltage(X,Volts,W) :- (!, W = yes)) ),
  4353.        Z = yes.
  4354.  
  4355. motor_voltage(X,Volts,Z)   :-
  4356.        asserta( (motor_voltage(X,Volts,W) :- (!, W = no)) ),
  4357.        Z = no.
  4358.  
  4359.  
  4360.  
  4361.  
  4362. breaker(Which,X)  :-
  4363.         print('Is the ',X,' breaker tripped (y/n)? '),
  4364.         ratom(y),
  4365.         !,
  4366.         asserta( (breaker(Which,Y) :- (!, Y = tripped)) ),
  4367.         X = tripped.
  4368.  
  4369. breaker(Which,X)  :-
  4370.      print('Is the breaker on (y/n)'),
  4371.      ratom(y),
  4372.      !,
  4373.      asserta( (breaker(Which,Y) :- (!, Y = on)) ),
  4374.      X = on.
  4375.  
  4376.  
  4377. breaker(Which,X)  :-
  4378.      asserta( (breaker(Which,Y) :- (!, Y = off)) ),
  4379.      X = off.
  4380.  
  4381. fan_turns(Which,X) :-
  4382.         print('Does the ',Which, ' fan turn now (y/n)? '),
  4383.         ratom(y),
  4384.         !,
  4385.         asserta( (fan_turns(Which,Y) :- (!, Y = yes)) ),
  4386.         X = yes.
  4387.  
  4388. fan_turns(Which,X) :-
  4389.         asserta( (fan_turns(Which,Y) :- (!, Y = no)) ),
  4390.         X = no.
  4391.  
  4392.  
  4393. coil_contacts(Which,X)  :-
  4394.      print('Are the ',Which, ' contacts open (y/n)? '),
  4395.      ratom(y),
  4396.      !,
  4397.      asserta( (coil_contacts(Which,Y) :- (!, Y = open)) ),
  4398.      X = open.
  4399.  
  4400. coil_contacts(Which,X)  :-
  4401.      asserta( (coil_contacts(Which,Y) :- (!, Y = closed)) ),
  4402.      X = closed.
  4403.  
  4404.  
  4405. continuity(Device,YesNo) :-
  4406.         print('Is there continuity at the ',Device,' (y/n)? '),
  4407.         ratom(y),
  4408.         !,
  4409.         asserta( (continuity(Device,Y) :- (!, Y = yes)) ),
  4410.         YesNo = yes.
  4411.  
  4412. continuity(Device,YesNo) :-
  4413.         asserta( (continuity(Device,Y) :- (!, Y = no)) ),
  4414.         YesNo = no.
  4415.  
  4416. motor(Which,How,YesNo) :-
  4417.         print('Is the ',Which, ' motor ',How,' (y/n)? '),
  4418.         ratom(y),
  4419.         !,
  4420.         asserta( (motor(Which,How,Y) :- (!, Y = yes)) ),
  4421.         YesNo = yes.
  4422.  
  4423. motor(Which,How,YesNo) :-
  4424.         asserta( (motor(Which,How,Y) :- (!, Y = no)) ),
  4425.         YesNo = no.
  4426.  
  4427. air(X)  :-
  4428.         print('Do you hear any air (y/n)? '),
  4429.         ratom(y),
  4430.         !,
  4431.         asserta( (air(Y) :- (!, Y = yes)) ),
  4432.         X = yes.
  4433.  
  4434. air(X) :-
  4435.         asserta( (air(Y) :- (!, Y = no)) ),
  4436.         X = no.
  4437.  
  4438. perform_maintenance([]) :- !.
  4439. perform_maintenance([[Symptom,Action] | Tail]) :-
  4440.      do_maintenance(Symptom,Action),
  4441.      perform_maintenance(Tail).
  4442.  
  4443. do_maintenance(Symptom,Action) :-
  4444.      print('Is there a ',Symptom,'? (y/n)'),
  4445.      ratom(y), !,
  4446.      print(Action),nl.
  4447. do_maintenance(Symptom,Action).
  4448.  
  4449. maintenance( [
  4450.      [plugged_filter, replace],
  4451.      [broken_or_loose_belt, replace],
  4452.      [plugged_coil, clean],
  4453.      [dirty_blower_wheel, clean],
  4454.      [plugged_return_grill, clean],
  4455.      [closed_grill,open],
  4456.      [plugged_grill,clean] ]).
  4457.  
  4458.  
  4459.  
  4460. check_safeties([],Num) :- !, Num is 0.
  4461. check_safeties([[Safety,Action] | Tail], Num) :-
  4462.      ! ,
  4463.      check_safety(Safety,Action,Num1),
  4464.      check_safeties(Tail, Num2),
  4465.      Num is Num1 + Num2.
  4466.  
  4467. check_safety(Safety,Action, Num) :-
  4468.      print('Is the ',Safety,' safety open (y/n)?'),
  4469.      ratom(y), !,
  4470.      print(Action) ,nl,
  4471.      Num is 1.
  4472.  
  4473. check_safety(Symptom,Action,Num ) :- Num is 0.
  4474.  
  4475. safety_list( [
  4476.      [high_pressure,
  4477.        'Check for clogged condenser coils, condenser fan motor problem, overcharged unit'],
  4478.      [low_pressure,'Check for loss of refrigerant'],
  4479.      [compressor_internal,
  4480.        'Low refrigerant charge, expansion valve problems, filters, belts coils, compressor amperage']
  4481.            ]).
  4482.  
  4483. safeties(X)  :-
  4484.         safety_list(List),
  4485.         check_safeties(List,Num),
  4486.         Num = 0,
  4487.         !,
  4488.         asserta( (safeties(Y) :- (!, Y = yes)) ),
  4489.         X = yes.
  4490.  
  4491. safeties(X) :-
  4492.         asserta( (safeties(Y) :- (!, Y = no)) ),
  4493.         X = no.
  4494.  
  4495.  
  4496. gauge(X) :-
  4497.         print('Install gauges'), nl,
  4498.         asserta(( gauge(X) :- !)).
  4499.  
  4500.  
  4501. pressure(HighLowSide,Pounds) :-
  4502.         print('What is the pressure on the ',HighLowSide,' (end # with .)? '),
  4503.         read(Pounds),
  4504.         asserta((pressure(HighLowSide,Pounds) :- !)).
  4505.  
  4506.  
  4507.  
  4508. print_maintenance_start(X) :-
  4509.     print('Be sure that the maintenance has been done first'), nl,
  4510.     print_maintenance(X),
  4511.     asserta( (print_maintenance_start(Y) :- !) ).
  4512.  
  4513.  
  4514. print_maintenance([]) :- !.
  4515. print_maintenance([Head | Tail] ) :-
  4516.     print('     '),
  4517.     Head = [Symptom, Stuff], print(Symptom),nl,
  4518.     print_maintenance(Tail).
  4519.  
  4520.  
  4521.  
  4522.  
  4523.  
  4524.  
  4525.  
  4526. /*
  4527. Note: Carl is an A.D.A. PROLOG user who has contributed this program for
  4528. the enjoyment of others. 
  4529.  
  4530.                  Towers of Hanoi by Carl Bredlau
  4531.                         909 Rahway Avenue
  4532.                    Westfield, New Jersey 07090
  4533. */
  4534.  
  4535.  
  4536. %   stuff for prolog 86
  4537. %   print is changed to prin
  4538.  
  4539. put(X) :- ascii(C,X), putc(C).
  4540.  
  4541. makelist(1,[1]).
  4542. makelist(N, [N|Y]) :- N1 is N - 1, makelist(N1,Y).
  4543.  
  4544. biggie(1,X,[X]).
  4545. biggie(N,X,[X|Z]) :- N1 is N - 1, X1 is X + 1, biggie(N1,X1,Z).
  4546.  
  4547. alist(N,Y) :- biggie(N,1,Y).
  4548.  
  4549. %/* get the size of a list  */
  4550.  
  4551. size([],0) :-  !.
  4552. size([_|X],Num) :- size(X,N1), Num is N1 + 1.
  4553.  
  4554. %/* Might as well keep track of the disks on the poles.  This is
  4555. %   not really necessary; all we need to know is how many
  4556. %   disks are on a pole  */
  4557.  
  4558.  
  4559.  
  4560. readtop(N,Y)  :- retract(pole(N,[Y|X])), asserta(pole(N,X)).
  4561.  
  4562. writetop(N,Y) :- retract(pole(N,X)),  asserta(pole(N,[Y|X])).
  4563.  
  4564. makepoles(N) :- alist(N,Y), asserta( pole(1,Y)),
  4565.                 asserta(pole(2,[])), asserta(pole(3,[])).
  4566.  
  4567. %/* stuff for pretty printing   */
  4568. %/* Note: the CONFIG.SYS file must contain the line ANSI.SYS.  Also,
  4569. %   the ANSI.SYS file must be on the disk when the system is booted     */
  4570.  
  4571. out(X) :- put(27), prin(X).
  4572. clear  :- out('[2J'). % /* clear screen */
  4573.  
  4574. goto(X,Y)  :- put(27),prin('[',X),put(59),prin(Y,'H').  % /* 59 is ; */
  4575.  
  4576.  
  4577. stuff(1,X) :- prin(X), !.
  4578. stuff(N,X) :- prin(X), N1 is N - 1, stuff(N1,X).
  4579.  
  4580.  
  4581.  
  4582. newhanoi(1,A,B,C) :- move(1,A,B).
  4583. newhanoi(N,A,B,C) :- !,  N1 is N - 1,
  4584.                        newhanoi(N1,A,C,B),
  4585.                        move(N,A,B),
  4586.                        newhanoi(N1,C,B,A).
  4587.  
  4588.  
  4589.  
  4590. %/*  As mentioned earlier size and readtop are not really needed,
  4591. %    but I threw them in so that you can see what's there.             */
  4592.  
  4593. move(N,A,B)  :- !,   pole(A,Adisk), size(Adisk,ANum),readtop(A,N),
  4594.                    X1 is 20 - ANum, Y1 is 5 + (A - 1)* 15,
  4595.                    goto(X1,Y1), stuff(N,' '),
  4596.                    writetop(B,N), pole(B,Bdisk), size(Bdisk,BNum),
  4597.                    X2 is 20 - BNum, Y2 is 5 + (B - 1)* 15,
  4598.                    goto(X2,Y2), stuff(N,'*'),
  4599.                    goto(24,1),
  4600.                    prin('Move disk ',N,' from ',A,' to ',B,'         ').
  4601.  
  4602.  
  4603. firstpole(N,1)  :- X1 is 20 - N, goto(X1,5),
  4604.                    stuff(1,'*'), !.
  4605.  
  4606. firstpole(N,L) :-  X1 is (20 - N) + (L - 1), goto(X1,5),
  4607.                    stuff(L,'*'),
  4608.                    L1 is L - 1, firstpole(N,L1).
  4609.  
  4610.  
  4611.  
  4612. start :- prin('How many disks? '), read(N), clear, firstpole(N,N),
  4613.             makepoles(N), newhanoi(N,1,2,3), !.
  4614.  
  4615.  
  4616.  
  4617.  
  4618. factor(0,Y) :- Y is 1, !.
  4619. factor(X,Y) :- Z is X - 1, factor(Z,W), Y is X*W.
  4620.  
  4621. %/* recursive version a n! and towers of hanoi  */
  4622. hanoi(1,A,B,C) :- prin('Move disk ',1,' from ',A,' to ',B),nl, !.
  4623. hanoi(N,A,B,C) :-   N1 is N - 1,
  4624.                     hanoi(N1,A,C,B), !,
  4625.                     prin('Move disk ',N,' from ',A,' to ',B), nl,
  4626.                     hanoi(N1,C,B,A), !.
  4627.  
  4628.  
  4629.  
  4630.  
  4631.  
  4632.  
  4633.  
  4634.  
  4635.  
  4636.  
  4637.  
  4638.  
  4639.  
  4640.  
  4641. s(X,Y) :- X1 is X + 1, (Y is X1 ; s(X1,Y) ).
  4642.  
  4643. s1(X,Y) :- X1 is X + 1, Y is X + 1.
  4644. s1(X,Y) :- X1 is X + 1, s1(X1, Y).
  4645. list1(X) :- clause(X,Y),output_clause(X,Y),
  4646.         write( '.' ), nl, fail.
  4647.  
  4648. list1(X).
  4649.  
  4650. output_clause(X,true) :- !, write(X).
  4651. output_clause(X,Y)    :- write( (X :- Y) ).
  4652.  
  4653. a( b ).
  4654. a( c ).
  4655. outputclause(X,true) :- !, write(X).
  4656. outputclause(X,Y)    :- write( (X :- Y) ).go  :- 
  4657.     repeat,
  4658.     print( 'Enter a number: ' ),
  4659.     get0( Num ), nl,
  4660.         (
  4661.           ( (Num > 58), print( 'You did not enter a number'),
  4662.                 nl, fail)
  4663.           ;
  4664.           (print( 'Do it over, please. ' ), nl, fail )
  4665.         ).
  4666.                        /* Sorting Lists */
  4667.  
  4668. /*
  4669. The order predicate determines how you would like the list to be ordered:
  4670. */
  4671.  
  4672. order( A, B ) :- A > B.
  4673.  
  4674. /* The bubble sort. Invoke as ?-busort( [1,2,3], Sortlist ).
  4675. The answer is instantiated as the sorted list. */
  4676.  
  4677. busort( L, S ) :-
  4678.     append( X, [A, B|Y], L ),
  4679.     order( A, B ),
  4680.     append( X, [B,A|Y], M ),
  4681.     busort( M, S ).
  4682. busort( L, L ).
  4683.  
  4684. /* Used by most sorting algorithms. */
  4685. append( [], L, L ).
  4686. append( [H|T], L, [H|V] ) :- append( T, L, V ).
  4687.  
  4688.  
  4689. /* The quick sort. */
  4690.  
  4691.  
  4692. quisort( [H|T], S ) :-
  4693.     split( H, T, A, B ),
  4694.     quisort( A, A1 ),
  4695.     quisort( B, B1 ),
  4696.     append( A1, [H|B1], S ).
  4697.  
  4698. /* This important clause was left out by Clocksin and Mellish: */
  4699. quisort( [], [] ).
  4700.  
  4701. /* List splitting predicates used by both quick sort algorithms: */
  4702.  
  4703. split( H, [A|X], [A|Y], Z ) :- order( A, H ), split( H, X, Y, Z ).
  4704. split( H, [A|X], Y, [A|Z] ) :- order( H, A ), split( H, X, Y, Z ).
  4705. split( _, [], [], [] ).
  4706.  
  4707.  
  4708. /* 
  4709. A compact form of the quick sort. 
  4710. Invoke as: ?-quisort( List, Sortlist, [] ).
  4711. */
  4712.  
  4713. quisortx( [H|T], S, X ) :-
  4714.     split( H, T, A, B ), 
  4715.     quisortx( A, S, [H|Y] ),
  4716.     quisortx( B, Y, X ).
  4717. quisortx( [], X, X ).
  4718.  
  4719.  
  4720. /*
  4721. The insertion sort:
  4722. Invoke as ?-insort( List, Sortlist ).
  4723. */
  4724. insort( [], [] ).
  4725. insort( [X|L], M ) :- insort( L, N ), insortx( X, N, M ).
  4726.  
  4727. insortx( X, [A|L], [A|M] ) :- order( A, X ), !, insortx( X, L, M ).
  4728. insortx( X, L, [X|L] ).
  4729.  
  4730.  
  4731. insort( [], [], _ ).
  4732. insort( [X|L], M, O ) :- insort( L, N, O ), insortx( X, N, M, O ).
  4733.  
  4734.  
  4735. /* 
  4736. This form of the insertion sort needs no sort parameter.
  4737. O is instantiated to a predicate or operator which orders the elements.
  4738. Invoke as: insort( List, Sortlist, <order> ).
  4739. For instance, ?-insort( List, Sortlist, < ).
  4740. */
  4741.  
  4742. insortb( [], [], _).
  4743. insortb( [X|L], M, O ) :- insortb( L, N, O ), insortxb( X, N, M, O ).
  4744.  
  4745.  
  4746. insortxb( X, [A|L], [A|M], O ) :-
  4747.     P =.. [ O, A, X ],
  4748.     P,
  4749.     !,
  4750.     insortxb( X, L, M, O ).
  4751. insortxb( X, L, [X|L], O ).
  4752. /* 
  4753. This program performs symbolic differentiation. 
  4754.  
  4755. Sample forms to differentiate:
  4756.  
  4757.   ?-d(x+1,x,X).
  4758.   ?-d(x*x-2,x,X).
  4759.  
  4760. See C & M for more on this. 
  4761. */
  4762.  
  4763.  
  4764. ?-op( 9, fx, '%' ).
  4765.  
  4766. d(X,X,1) :- !.
  4767. d(C,X,0) :- atomic(C).
  4768. d(%U, X, %A) :- d( U, X, A ).
  4769. d( U+V, X, A+B) :- d(U,X,A), d(V,X,B).
  4770. d( U-V, X, A-B ) :- d(U,X,A), d(V,X,B).
  4771. d(C*U,X,C*A) :- atomic(C), C \= X, d(U,X,A), !.
  4772. d(U*V,X,B*U+A*V) :- d(U,X,A), d(V,X,B).
  4773. d(U/V,X,A) :- d(U*V**(%1),X,A).
  4774. d(U**V,X,V*W*U**(V-1)) :- atomic(V), c \= X, d(U,X,W).
  4775. d(log(U),X,A*U**(%1)) :- d(U,X,A).
  4776.  
  4777. /*   This is a sample network path finding algorithm.  To make use of this 
  4778. see CM (second edition) pages 168-169.  You can make use of "look" */
  4779.  
  4780.   append( [], L, L ).
  4781.   append( [Z|L1], L2, [Z|L3] ) :- append( L1, L2, L3 ).
  4782.   printstring( [] ).
  4783.   printstring( [H|T] ) :- put( H ), printstring( T ).
  4784.  
  4785.  
  4786.   rev( [], [] ).
  4787.   rev( [H1|TT], L1 ) :- rev( TT,ZZ), append( ZZ, [H1], L1 ).
  4788.  
  4789. /* Recursive member of list definition. 
  4790.  
  4791. Ask: Member( X, [a,b,c,d,e,f] ) to get successively all
  4792. the members of the given list. */
  4793.  
  4794.  
  4795. mem( YY, [YY|_] ).
  4796. mem( B, [_|C] ) :-  mem(B, C ).
  4797.  
  4798. pp([H|T],I)  :-  !, J is I+3, pp(H,J), ppx(T,J), nl.
  4799. pp(X,I)  :- tab(I), print(X), nl.
  4800. ppx([],_).
  4801. ppx([H|T],I)  :-  pp(H,I),ppx(T,I).
  4802.  
  4803.  
  4804. a(newcastle,carlisle,58).
  4805. a(carlisle,penrith,23).
  4806. a(townB,townA,15).
  4807. a(penrith,darlington,52).
  4808. a(townB,townC,10).
  4809. a(workington, carlisle,33).
  4810. a(workington,townC,5).
  4811. a(workington, penrith,39).
  4812. a(darlington,townA,25).
  4813.  
  4814. legalnode(X,Trail,Y,Dist,NewDist) :- 
  4815.     (a(X,Y,Z1) ; a(Y,X,Z1)),
  4816.     not(mem(Y,Trail)),
  4817.     NewDist is Dist + Z1.
  4818.  
  4819. go(Start,End,Travel) :-
  4820.       go3([r(0,[Start])],End,R55),
  4821.       rev(R55,Travel).
  4822.  
  4823. findall(X5,G,_) :-
  4824.       asserta(found(mark)),
  4825.       G,
  4826.       asserta(found(X5)),
  4827.       fail.
  4828. findall(_,_,L5)  :-  collect_found([],M5),!, L5  =  M5.
  4829.  
  4830. collect_found(S,L5)  :-  getnext(X5), !, collect_found([X5|S],L5).
  4831. collect_found(L5,L5).
  4832.  
  4833. getnext(X5)  :-  retract(found(X5)), !, X5 \== mark.
  4834.  
  4835. go3(Rts,Dest,Route) :- 
  4836. shortest(Rts,Shortest,RestRts),
  4837. proceed(Shortest,Dest,RestRts,Route).
  4838.  
  4839.  
  4840. proceed(r(Dist,Route),Dest,_ ,Route) :- Route = [Dest|_].
  4841.  
  4842.  
  4843. proceed(r(Dist,[Last|Trail]),Dest,Rts,Route)  :- 
  4844. findall(r(D1,[Z1,Last|Trail]),
  4845. legalnode(Last,Trail,Z1,Dist,D1),List),
  4846. append(List,Rts,NewRts),go3(NewRts,Dest,Route).
  4847.  
  4848.  
  4849. shortest([Route|Rts],Shortest,[Route|Rest])  :- 
  4850. shortest(Rts,Shortest,Rest),shorter(Shortest,Route),!.
  4851. shortest([Route|Rest],Route,Rest).
  4852. shorter(r(M1,_),r(M2,_)) :- M1 < M2.
  4853.  
  4854. look :- print('enter the starting location: '),nl,
  4855.          ratom(Beg),nl,
  4856.     print('enter the destination: '),
  4857.     nl,ratom(Dest),
  4858.     go(Beg,Dest,RRT),
  4859.     pp( RRT, 1 ).
  4860.  
  4861. 11-22-85
  4862.  
  4863.  
  4864. Dear Bob,
  4865.  
  4866.      I received your letter and the disk with EDPROLOG today and
  4867. must admit I was very gratified to find you were interested in my
  4868. Tree puzzle program - and thank you for the new ProLog!
  4869.  
  4870.      I have been happily busy with logic problems and syllogisms since
  4871. doing the Tree puzzle and I include two other of my efforts on this disk.
  4872. All three programs work just fine on my AT&T 6300 and all three have
  4873. simple start-up commands (like "go." or "hello.").  My machine does have
  4874. quite a lot of RAM (640k) but, as I understand things, this should not
  4875. pose problems for users with smaller systems since the language only
  4876. makes use of about 256k.  I hope this is the case.
  4877.  
  4878.      You will be interested to know that I have started planning a 
  4879. "free university" style course for next semester at the community college
  4880. where I teach.  The course, Introduction to ProLog, will be using your
  4881. public domain version of the language and Clocksin & Mellish's book.
  4882. This first offering will probably be limited to other staff members,
  4883. but if all goes well I'll offer it next year to faculty & students.  My
  4884. real motive is to enable my own learning as much as to teach the language.
  4885.  
  4886.      Hope you enjoy the programs.  They really were a lot of fun to write.
  4887.  
  4888.  
  4889.  
  4890.  
  4891. Sincerely,
  4892.  
  4893.  
  4894.  
  4895.  
  4896. Tom Sullivan
  4897. 5415 Grand Ave.
  4898. Western Springs, IL 60558
  4899. /* A Prolog solution to Family Trees by Virginia McCarthy.
  4900.             Tom Sullivan
  4901.             5415 Grand Ave.
  4902.             Western Springs, IL 60558  -  October 30,1985.
  4903. */
  4904.  
  4905. northside (larch).
  4906. northside (catalpa)    :- northside ('Grandes').
  4907. northside ('Grandes')  :- have ('Grandes', larch).
  4908. have ('Grandes',larch) :- not_own ('Crewes',larch),
  4909.                           not_own ('Dews',larch),
  4910.                           not_own ('Lands',larch).
  4911.  
  4912. southside ('Crewes').
  4913. southside ('Dews').
  4914. southside (dogwood) :- northside (larch), northside (catalpa).
  4915. southside (ginko)   :- northside (larch), northside (catalpa).
  4916.  
  4917. here ('Grandes').
  4918. there (catalpa).
  4919.  
  4920. /* belongs_to provides a recursive definition of membership in a list
  4921.    see Clocksin & Mellish p. 53.  */
  4922.  
  4923. belongs_to (X,[X|_]).
  4924. belongs_to (X,[Y|Z]) :- belongs_to (X,Z).
  4925.  
  4926. same_first_letter (['Dews', dogwood]).
  4927. same_first_letter (['Grandes',ginko]).
  4928. same_first_letter (['Lands',larch]).
  4929. same_first_letter (['Crewes',catalpa]).
  4930.  
  4931. human (['Grandes','Crewes','Dews','Lands']).
  4932. plant ([catalpa,ginko,dogwood,larch]).
  4933.  
  4934. person (X) :- human (Y), belongs_to (X,Y).
  4935. tree (X)   :- plant (Y), belongs_to (X,Y).
  4936.  
  4937. not_own (X,Y) :- same_first_letter (Z),
  4938.                  belongs_to (X,Z), belongs_to (Y,Z).
  4939. not_own (X,Y) :- here (X), there (Y).
  4940. not_own (X,Y) :- (person (X), person (Y));
  4941.                  (tree (X), tree (Y)).
  4942. not_own (X,Y) :- (northside (X),southside (Y));
  4943.                  (southside (X),northside (Y)).
  4944. not_own ('Crewes', X) :- owns ('Dews', X).
  4945. not_own ('Lands', X)  :- owns ('Crewes', X).
  4946. not_own ('Lands', X)  :- owns ('Dews',X).
  4947.  
  4948.  
  4949. owns (X,Y) :- person (X), tree (Y), not (not_own (X,Y)).
  4950. hello :- owns(Person,Tree),print (Person,' owns the ',Tree).
  4951.  
  4952. /* query with "owns (Person,Tree), write (Person,Tree)."
  4953.    or just say "hello."  */
  4954.  
  4955.  
  4956. /* The puzzle - "Family Trees" by Virginia McCarthy as found in
  4957.    Dell Champion Variety Puzzles, November, 1985
  4958.  
  4959.    The Crewes, Dews, Grandes, and Lands of Bower Street each have 
  4960.    a front-yard tree -- a catalpa, dogwood, gingko, and larch.  The
  4961.    Grandes' tree and the catalpa are on the same side of the street.
  4962.    The Crewes live across the street from the larch, which is across
  4963.    the street from the Dews' house.  If no tree starts with the same 
  4964.    letter as its owner's name, who owns which tree?     
  4965. */
  4966. /* A deduction problem from Dell Official Puzzles December, 1985.
  4967.    ProLog program by Tom Sullivan  -  November, 1985
  4968.  
  4969.    Abby, Barb, & Cora have clothes in  blue, green, purple, red & yellow.
  4970.    None wears yellow with red.  Each has a two-piece outfit in two colors.
  4971.    Abby is wearing blue.  Barb is wearing yellow but not green.  Cora wears
  4972.    green but not blue or purple.  One has on red.  One color is worn by both
  4973.    Barb and Cora, while Abby & Barb, between them, have on four different
  4974.    colors.  Name the colors each woman is wearing.
  4975. */
  4976.  
  4977. human ([abby,barb,cora]).
  4978. hue ([blue,green,purple,red,yellow]).
  4979.  
  4980. member (X,[X|_]).
  4981. member (X,[_|Y]) :- member (X,Y).
  4982.  
  4983. person (X) :- human (Y), member (X,Y).
  4984. color (X) :- hue (Y), member (X,Y).
  4985.  
  4986. hason (abby,blue).
  4987. hason (barb,yellow).
  4988. hason (cora,green).
  4989.  
  4990. notwear (barb,green).
  4991. notwear (cora,blue).
  4992. notwear (cora,purple).
  4993. notwear (X,Y) :- not (X = cora),hason (Z,Y).
  4994. notwear (abby,X) :- wears (barb,Z),member (X,Z).
  4995. notwear (cora,X) :- wears (abby,Z),member (X,Z).
  4996.  
  4997. wears (X,([Color1,Color2])) :- person (X), color (Color1), color (Color2),
  4998.                                hason (X,Color1),
  4999.                                not (notwear (X,Color2)),
  5000.                                not (Color1 = Color2),
  5001.                                not ((Color1 = red),(Color2 =yellow);
  5002.                                 (Color1 = yellow), (Color2 = red)).
  5003.  
  5004. go :- wears (X,Y),print (X,' wears ',Y).
  5005.  
  5006. /* to query type >> go.<CR>  */
  5007.  
  5008.  
  5009.  
  5010.  
  5011. /* A ProLog program to handle syllogisms of the type
  5012.                        All X are Y
  5013.                        Z is X
  5014.           Therefore -  Z is Y
  5015.  
  5016.    Tom Sullivan - 5415 Grand Ave. Western Springs, IL 60558
  5017.                      November, 1985
  5018.    Type go.<CR> to run the program.  Enjoy.
  5019. */
  5020.  
  5021. go :- nl,nl,nl,nl,nl,nl,nl,nl,nl,
  5022.       nl,nl,nl,nl,nl,nl,nl,nl,nl,
  5023.       print ('                     <<<<<   Theorem Prover   >>>>>'),
  5024.       nl,nl,
  5025.       print ('         Enter the name of a Greek,animal,plant,fish,insect,or bird.'),
  5026.       go1.
  5027.  
  5028. go1 :- nl,nl,nl,nl,nl,print ('Name  >> '),
  5029.        read (N),
  5030.        onlist (N),!.  /* see Clocksin & Mellish pp. 88-90  for use of cut */
  5031.  
  5032. listof (men,    ([socrates,plato,aristotle,homer])).
  5033. listof (animals,([dog,cat,horse,cow,pig,bear,lion])).
  5034. listof (plants, ([rose,petunia,daisy,oak,elm,corn])).
  5035. listof (fish,   ([waleye,pike,muski,bass,trout])).
  5036. listof (insects,([ant,beetle,spider,fly,mantis])).
  5037. listof (birds,  ([wren,robin,heron,eagle,hawk,crow])).
  5038.  
  5039. mortals ([men,animals,plants,fish,insects,birds]).
  5040.  
  5041. member (X,[X|_]).
  5042. member (X,[_|Y]) :- member (X,Y).      /* see C&M p.53  */
  5043.  
  5044. onlist (N) :- listof (Y,(Z)), member (N,Z),
  5045.               nl,nl,print ('Searching ... ',Y),
  5046.               nl,nl,print ('        1. ',N,' is in ',Y,'  ... and '),
  5047.               mortal (Y,N);stop.
  5048.  
  5049. mortal (Y,N) :- mortals (Z), member (Y,Z),
  5050.                 nl,nl,print ('Searching .... mortals'),
  5051.                 nl,nl,print ('        2. ',Y,' are mortal.'),
  5052.                 nl,nl,print ('        Therefore ',N,' is mortal.'), go1.
  5053.  
  5054. stop :- nl,print ('   That\'s not in the data!'),
  5055.         nl,nl,print ('                           Type <go.> for more.').
  5056. /* 
  5057. Except for the PD version, A.D.A. supports the grammar rule syntax. 
  5058. If the syntax is supported, one could ask the question:
  5059. sentence( X, [every, man, loves, a, woman], [] ).
  5060. and see the result translated into a formula of the predicate calculus.
  5061. If you want to compile this under the PD version, add the declaration:
  5062.   op( 150, xfy, '-->' ). However, the program won't run under type PD
  5063. unless you write a grammar rule expander (definitely feasible). 
  5064. */
  5065.  
  5066.  
  5067. ?-op( 100, xfx, '$' ).
  5068. ?-op( 150, xfy, '->' ).
  5069.  
  5070. sentence( P ) --> noun_phrase(X,P1,P), verb_phrase(X,P1).
  5071.  
  5072. noun_phrase(X, P1, P ) -->
  5073.    determiner(X,P2,P1,P), noun( X, P3 ),
  5074.    rel_clause( X, P3, P2 ).
  5075. noun_phrase( X, P, P ) --> proper_noun( X ).
  5076.  
  5077. verb_phrase( X, P ) --> trans_verb(X,Y, P1), noun_phrase(Y, P1, P ).
  5078. verb_phrase( X, P ) --> intrans_verb(X, P ).
  5079.  
  5080. rel_clause(X,P1,(P1$P2)) --> [that], verb_phrase(X, P2).
  5081. rel_clause(_, P, P) --> [].
  5082.  
  5083. determiner(X, P1, P2, all(X, (P1->P2))) --> [every].
  5084. determiner(X, P1, P2, exists(X,(P1$P2))) --> [a].
  5085.  
  5086. noun(X, man(X) ) --> [man].
  5087. noun(X, woman(X)) --> [woman].
  5088.  
  5089. proper_noun(john) --> [john].
  5090.  
  5091. trans_verb(X, Y, loves(X,Y)) --> [loves].
  5092.  
  5093. intrans_verb(X, lives(X) ) --> [lives].
  5094. /*   PIE.TM : A PROLOG INFERENCE ENGINE AND TRUTH MAINTENANCE      */
  5095. /*            SYSTEM                                               */
  5096.  
  5097.  
  5098.  
  5099. /* This file contains most of the fundamental predicates necessary */
  5100. /* for doing truth maintenance. PIE uses the prolog interpreter as */
  5101. /* an input parser by declaring most of the PIE syntax as goals.   */
  5102. /* Prior to execution the operators must be declared.This is       */
  5103. /* simplified by using the redirect feature of ADA Prolog with the */
  5104. /* command line:       'prolog kops'                               */
  5105. /* The system is not yet complete and several extentions are       */
  5106. /* planned, many of which have already been implemented but        */
  5107. /* remain to be integrated with this particular piece of code.     */
  5108. /* Examples of planned extentions follow: one-directional rules,   */
  5109. /* a non-rule based inference based on mathematical set covering,  */
  5110. /* confidence factors, and more refined techniques for displaying  */
  5111. /* and editing a knowledge base. At the moment it is useful to know*/
  5112. /* or have a copy of the underlying representation. There is not   */
  5113. /* a lot of code here and it has not been thoroughly tested, but it*/
  5114. /* is quite powerful and flexible.                                 */
  5115.  
  5116.  
  5117. /* Sets 'X implies Y' up as a goal. NOTE: In order for the input to*/
  5118. /* be parsed properly antecedents and consequents must be given as */
  5119. /* lists, e.g. '[X is a male,X is a human] implies [X is a man]'.  */
  5120. /* Consequents may themselves be rule declarations. The rules      */
  5121. /* are bi-directional and may contain Prolog goals as elements     */
  5122. /* of the antecedent or consequent lists. To force forward         */
  5123. /* chaining 'fc' may be made a member of the antecedent or         */
  5124. /* consequent lists.                                               */
  5125.  
  5126. X implies Y :-
  5127.         assert_r(X implies Y).
  5128.  
  5129.  
  5130.  
  5131. /* Cycles through all the forward chaining rules to find out if  */
  5132. /* the most recent assertion will cause any to fire. The         */
  5133. /* efficiency of this function can be increased dramatically by  */
  5134. /* copying the original rule to a 'non-conflict' stack and       */
  5135. /* effacing those conditions that have already been met. This    */
  5136. /* will result in ever shorter antecednt lists for the rules.    */
  5137.  
  5138.  
  5139. fc:-
  5140.         clause(rule(N,D,Y implies Z,C),true),
  5141.         given_mem(Y),
  5142.         check_mult_con(N,Z),
  5143.         fail.
  5144. fc.
  5145.  
  5146. /* Checks to see if an antecedent that is part of a list exists  */
  5147. /* as a given in the kb.                                         */
  5148.  
  5149. given_mem([]).
  5150. given_mem([Y|Z]):-
  5151.         (Y;fact(N,D,Y,C)),
  5152.         given_mem(Z),!.
  5153.  
  5154. /* Reads through a list of consequents and passes them on to     */
  5155. /* the infer function only if they do not already exist in the   */
  5156. /* kb. This should be enhanced so that confidence factors can    */
  5157. /* be incremented.                                               */
  5158.  
  5159. check_mult_con(N,[]).
  5160. check_mult_con(N,[X|Y]):-
  5161.         infer(N,X),
  5162.         check_mult_con(N,Y),!.
  5163.  
  5164.  
  5165.  
  5166. /*The PIE assert adds facts to the knowledge base. While doing  */
  5167. /*so it checks to make sure that no conflicting facts exist. If */
  5168. /*conflicting facts do exist their identity is displayed.       */
  5169. /*Planned extentions include backward truth maintenance, wherein */
  5170. /*the inferences that led to both of the conflicting facts will */
  5171. /*be evaluated for confidence and 'distance' from input.        */
  5172. /* A typical assertion made by the user might look like:        */
  5173. /*                assert([bill is a man]).                      */
  5174. /* If the assert(X) is followed by an 'fc', forward chaining    */
  5175. /* will occur for the entire system.                            */
  5176.  
  5177. /* This is a special instance of the PIE assert. It allows new  */
  5178. /* relations to be declared in the form of operators. Asserting */
  5179. /* 'loves is a relation' will allow subsequent use of 'loves' as*/
  5180. /* an infix operator in antecedents or consequents of rules,    */
  5181. /* e.g. [X loves Y] implies [Y loves X].                        */
  5182.  
  5183. assert([]).
  5184. assert([X is a Rel|Y]) :-
  5185.         nonvar(R),
  5186.         R=relation,
  5187.         gensym(rel,N),
  5188.         asserta(relation(N,_)),
  5189.         op(10,xfx,X),
  5190.         assert(Y).
  5191. assert([X|Y]):-
  5192.         fact(Number,Dependence,X,Confidence),
  5193.         assert(Y).
  5194. assert([X|Y]):-
  5195.         fact(Number,Dependence,not(X),Confidence),!,
  5196.         print('Sorry, in conflict with existing information.'),nl,
  5197.         print('Dependency for ',Number),nl,
  5198.         prt_dependency(Number),
  5199.         assert(Y).
  5200. assert([not(X)|Y]):-
  5201.         fact(Number,Dependence,X,Confidence),!,
  5202.         print('Sorry, in conflict with existing information.'),nl,
  5203.         print('Dependency for ',Number),nl,
  5204.         prt_dependency(Number),
  5205.         assert(Y).
  5206.  
  5207. assert([not(X)|Y]):-
  5208.         check_word(X,_),
  5209.         functor(X,F,N),
  5210.         (atom(X);N>0),
  5211.         gensym(f,Number),
  5212.         assertz(fact(Number,input,not(X),Conf)),
  5213.         print('Inserted: ',Number,' not',X),nl,!,
  5214.         assert(Y).
  5215. assert([X|Y]):-
  5216.         check_word(X,_),
  5217.         functor(X,F,N),!,
  5218.         N>0,
  5219.         gensym(f,Number),
  5220.         assertz(fact(Number,input,X,C)),
  5221.         print('Inserted: ',Number,' ',X),nl,!,
  5222.         assert(Y).
  5223.  
  5224.  
  5225. /* Specifically designed for adding rules to the knowledge base */
  5226.  
  5227. assert_r(not(X)):-
  5228.         check_word(X,Y),
  5229.         functor(X,F,N),
  5230.         F=implies,
  5231.         gensym(r,Number),
  5232.         assertz(rule(Number,input,not(X),Conf)),!,
  5233.         print('Inserted: ',Number,' not',X),nl.
  5234. assert_r(X):-
  5235.         check_word(X,Y),
  5236.         functor(X,implies,N),
  5237.         gensym(r,Number),
  5238.         assertz(rule(Number,input,X,Conf)),!,
  5239.         print('Inserted: ',Number,' ',X),nl.
  5240.  
  5241. /* The 'infer' clause allows assertions to be made as a result of */
  5242. /* inference. It is similar to 'assert', but allows the passing   */
  5243. /* of a dependency bound to 'N'.                                  */
  5244.  
  5245. infer(N,not(X)):-
  5246.         fact(Num,Dependence,X,Confidence),!,
  5247.         print('Sorry, in conflict with existing information.'),nl,
  5248.         print('Dependency of existing info ',Num,' ',X),nl,
  5249.         prt_dependency(Num),
  5250.         print('Dependence of new conflicting info not',X),nl,
  5251.         prt_dependency(N).
  5252. infer(N,X):-
  5253.         fact(Num,Dependence,not(X),Confidence),!,
  5254.         print('Sorry, in conflict with existing information.'),nl,
  5255.         print('Dependency of existing info ',Num,'not',X),nl,
  5256.         prt_dependency(Num),
  5257.         print('Dependence of new conflicting info ',X),nl,
  5258.         prt_dependency(N).
  5259. infer(N,X):-
  5260.         (X;fact(_,_,X,_);rule(_,_,X,_)).
  5261. infer(N,X):-
  5262.         X='implies'(_,_),
  5263.         gensym(r,Number),
  5264.         assertz(rule(Number,N,X,Conf)),
  5265.         print('Inserted: ',Number,' ',X),nl,!.
  5266. infer(N,X):-
  5267.         (atom(X);true),
  5268.         gensym(f,Number),
  5269.         assertz(fact(Number,N,X,Conf)),
  5270.         print('Inserted: ',Number,' ',X),nl,!.
  5271.  
  5272. /* Builds a vocabulary for the system and ensures that typographical errors */
  5273. /* are not introduced. A typographical error might result in what would     */
  5274. /* to be two different values for an attribute or two different attributes  */
  5275. /* for an object.                                                           */
  5276.  
  5277. check_word(X,_):-
  5278.         var(X).
  5279. check_word(X,_):-
  5280.         word(X).
  5281. check_word(X,Y):-
  5282.         X= '`s'(A,B),
  5283.         check_word(A,A1),
  5284.         check_word(B,B1).
  5285. check_word(X,Y):-
  5286.         X= 'is a'(A,B),
  5287.         check_word(A,A1),
  5288.         check_word(B,B1),
  5289.         setval(B1,A1).
  5290. check_word(X,Y):-
  5291.         X=F(A,B),
  5292.         check_word(A,A1),
  5293.         check_word(B,B1),
  5294.         setval(A1,B1).
  5295. check_word([X|Tail],_):-    /* Allows the use of ';'and lists within a list */
  5296.         check_word(X,_),
  5297.         (Tail =[];check_word(Tail,_)).
  5298. check_word(X,Y):-
  5299.         print('Is ',X,' a correct value? y/n: '),
  5300.         ((ratom(y),X=Y);(replace_value(Y))).
  5301. replace_value(Y):-
  5302.         print('Please, type in correct value: '),
  5303.         ratom(Y).
  5304. setval(A,B):-
  5305.         nonvar(A),
  5306.         nonvar(B),
  5307.         asserta(legval(A,B)).
  5308. setval(A,B):-
  5309.         nonvar(A),
  5310.         asserta(word(A)),
  5311.         fail.
  5312. setval(A,B):-
  5313.         nonvar(B),
  5314.         asserta(word(B)),
  5315.         fail.
  5316. setval(_,_).
  5317.  
  5318. /* A simple recursive function that will print out the rule    */
  5319. /* numbers on which a fact or rule depends. Extensions to this */
  5320. /* will allow for viewing in various modes and editing.        */
  5321.  
  5322. prt_dependency(input).
  5323. prt_dependency(N):-
  5324.         (fact(N,input,_,_);rule(N,input,_,_)),
  5325.         print('input').
  5326. prt_dependency(N):-
  5327.         (fact(N,D,_,_);rule(N,D,_,_)),
  5328.         (fact(D,D1,X,Conf);rule(D,D1,X,Conf)),
  5329.         write(D),tab(2),write(X),tab(2),write(Conf),nl,
  5330.         prt_dependency(D1).
  5331.  
  5332. rule(X):-
  5333.     rule(X,Dep,Body,Conf),
  5334.     print(X,'  ',Dep,'  ',Body,'  ',Conf),nl.
  5335. rules:-
  5336.     clause(rule(A,B,C,D),true),
  5337.     print(A,' ',B,' ',C,' ',D),nl,
  5338.     fail.
  5339. rules.
  5340.  
  5341. fact(X):-
  5342.     fact(X,Dep,Body,Conf),
  5343.     print(X,'  ',Dep,'  ',Body,'  ',Conf),nl.
  5344. facts:-
  5345.     clause(fact(Num,Dep,Body,Conf),true),
  5346.     print(Num,'  ',Dep,'  ',Body,'  ',Conf),nl,
  5347.     fail.
  5348. facts.
  5349.  
  5350.  
  5351.  
  5352. /* Allows removal of rules or facts by reference to their gensym  */
  5353. /* index. This could easily be enhanced by allowing instantiation */
  5354. /* through explicitly typing out the item to be removed.          */
  5355. /* Automatically removes assertions that depend on the retracted  */
  5356. /* item.                                                          */
  5357.  
  5358. remove(N):-
  5359.         retract(rule(N,D,X implies Y,C)),
  5360.         print('Removed: ',N,' ',X,'implies',Y),nl,
  5361.         remove_con(N,Y).
  5362. remove(N):-
  5363.         retract(fact(N,D,X,C)),
  5364.         clause(rule(N1,_,Y implies Z,_),true),
  5365.         print('Removed: ',N,' ',X),nl,
  5366.         mem(X,Y),
  5367.         remove_con(N1,Z),
  5368.         fail.
  5369.  
  5370. /* 'Remove' will automatically forward chain in order re-infer  */
  5371. /* things that may be obtained through a different route than   */
  5372. /* that affected by the retraction process. This is necessary   */
  5373. /* because not all facts are taken advantage of in inferencing. */
  5374. /* That is to say, if a fact already exists 'infer' and 'assert'*/
  5375. /* will not add them redundantly to the kb. This will change    */
  5376. /* with the addition of confidence factors.                     */
  5377.  
  5378. remove(N):-
  5379.         fc.
  5380.  
  5381.  
  5382. /* Exhaustively checks facts in the kb and removes them if they */
  5383. /* depend on another item removed. NOTE: 'N=D' is part of a     */
  5384. /* disjunction, if it fails the fact will be reinserted in the  */
  5385. /* kb. At the moment this does not take advantage of the ADA    */
  5386. /* Prolog indexing capability, but it should in a dedicated     */
  5387. /* ADA application.                                             */
  5388.  
  5389. remove_con(N,[]).
  5390. remove_con(N,[X|Y]):-
  5391.         retract(fact(N1,N,X,C)),
  5392.         print('Removed: ',N1,' ',X),nl,
  5393.         remove_con(Y).
  5394. remove_con([X|Y]):-
  5395.         remove_con(Y).
  5396.  
  5397.  
  5398.  
  5399.  
  5400. /* Activates backward chaining. A complex function, the first */
  5401. /* two clauses REQUIRE a list to function properly, but valid- */
  5402. /* ation is not done. This is required by the inference       */
  5403. /* mechanism. Its effect is to ensure that inheritance is not */
  5404. /* carried over to uninstantiated objects.                    */
  5405.  
  5406. obtain([]).
  5407. obtain(X):-
  5408.         X =[Y|Z],!,
  5409.         obtain_1(Y),
  5410.         obtain(Z).
  5411. obtain_1(X):-
  5412.         X.
  5413. obtain_1(X):-
  5414.         clause(fact(N,D,X,C),true).
  5415.  
  5416. obtain_1(X):-
  5417.         clause(rule(N,D,Y implies Z,C),true),
  5418.         nl,
  5419.         not(chk(N)),     /* Prevents double pattern match. */
  5420.         mem(X,Z),
  5421.         asserta(chk(N)),
  5422.         obtain(Y).      /* Recursive check for ant as a con.*/
  5423. obtain_1(F(A,B)):-
  5424.         X=F(A,F1(C,D)),
  5425.         nonvar(F1),!,
  5426.         print(A,' ',F,' ',C,' ',F1,' ',D),nl,
  5427.         obtain_1a(F(A,F1(C,D))),
  5428.         assert([F(A,F1(C,D))]),
  5429.         refresh.          /* Removes 'chk' tag. */
  5430. obtain_1(F(A,B)):-
  5431.         print(A,' ',F,' ',B),nl,
  5432.         obtain_1b(F(A,B)),
  5433.         assert([F(A,B)]),
  5434.         refresh.
  5435. obtain_1a(F(A,F1(B,C))):-
  5436.         print('Please,fill in the blanks:'),nl,
  5437.         get_val(A,_),
  5438.         print(A,' ',F,' '),
  5439.         get_val(B,A),
  5440.         print(B,' ',F1,' '),
  5441.         get_val(C,B).
  5442. obtain_1b(F(A,B)):-
  5443.         print('Please,fill in the blanks:'),nl,
  5444.         get_val(A,_),
  5445.         print(A,' ',F,' '),
  5446.         get_val(B,A).
  5447. get_val(X,_):-
  5448.         nonvar(X).
  5449. get_val(X,Y):-
  5450.         listvals(Y),
  5451.         r_val(X,Y).
  5452. r_val(X,Y):-
  5453.         ratom(Z),
  5454.       /*  legval(Y,Z), */
  5455.         Z=X.
  5456.  
  5457.  
  5458. /* Refreshes rules */
  5459. refresh:-
  5460.     retract(chk(_)),
  5461.     fail.
  5462. refresh.
  5463.  
  5464.  
  5465. listvals(_).  /* Temporarily axiomatic */
  5466. listvals(X):-
  5467.         clause(legval(X,Y),true),
  5468.         print(Y),nl,
  5469.         fail.
  5470. listvals(_).
  5471.  
  5472.  
  5473. /* Standard Prolog append.                                      */
  5474.  
  5475. append([],X,X).
  5476. append([A|B],C,[A|D]):-
  5477.         append(B,C,D).
  5478.  
  5479.  
  5480. /* Standard Prolog member.                                      */
  5481.  
  5482. mem(X,[X|_]).
  5483. mem(X,[Y|Z]):-
  5484.         mem(X,Z).
  5485.  
  5486. /* Standard Prolog gensym.                                       */
  5487.  
  5488. gensym( Root, Atom ) :-
  5489.         get_num( Root, Num ),
  5490.         name( Root, Name1 ),
  5491.         integer_name( Num, Name2 ),
  5492.         append( Name1, Name2, Name ),
  5493.         name( Atom, Name ).
  5494.  
  5495. get_num( Root, Num ) :-
  5496.         retract( current_num( Root, Num1 )), !,
  5497.         Num is Num1 + 1,
  5498.         asserta( current_num( Root, Num)).
  5499.  
  5500. get_num( Root, 1 ) :- asserta( current_num( Root, 1 )).
  5501.  
  5502. integer_name( Int, List ) :- integer_name( Int, [], List ).
  5503. integer_name( I, Sofar, [C|Sofar] ) :-
  5504.         I < 10, !, C is I + 48.
  5505. integer_name( I, Sofar, List ) :-
  5506.         Tophalf is I/10,
  5507.         Bothalf is I mod 10,
  5508.         C is Bothalf + 48,
  5509.         integer_name( Tophalf, [C|Sofar], List ).
  5510.  
  5511.  
  5512. append( [], L, L ).
  5513. append( [Z|L1], L2, [Z|L3] ) :- append( L1, L2, L3 ).
  5514.  
  5515.  
  5516. prolog kops
  5517.  
  5518. batch.
  5519. op(240,xfx,'implies').
  5520. op(240,xfx,'only if').
  5521. op(220,xfx,'is a').
  5522. op(220,yfy,'`s').
  5523. op(219,xfx,'eq').
  5524. nl,
  5525. print('\n                                   PIE.TM\n',
  5526. '           A Forward and Backward Chaining Prolog Inference Engine\n',
  5527. '                          With Truth Maintenance\n',
  5528. '                Public Domain Version 1.1   19 November 1985\n',
  5529. '                            By Simon Blackwell\n',
  5530. '          Dept. of Philosophy, Bowling Green State University, Ohio').
  5531. consult('know.pro').
  5532. see(user).
  5533. ..head01rCS680
  5534. ..foot59ci
  5535.  
  5536.                   A Prolog Augmented Transition Network Parser
  5537.  
  5538.                                Table of Contents
  5539.  
  5540.  
  5541. 1.  General                                                         1
  5542.  
  5543. 2.  ATN Form                                                        1
  5544.  
  5545.         Figure 1.  ATN Network                                      1
  5546.         Figure 2.  Phrase Network                                   2
  5547.  
  5548. 3.  Approach                                                        2
  5549.                                                                        
  5550.         Phase I                                                     2
  5551.         Phase II                                                    2
  5552.         Phase III                                                   2
  5553.  
  5554. 4.  Phase I                                                         2
  5555.  
  5556. 5.  Phase II                                                        3
  5557.  
  5558. 6.  Phase III                                                       5
  5559.                                                                     
  5560.         ATNBLD                                                      6
  5561.         ATNNEW                                                      8
  5562.  
  5563. 7.  Conclusions                                                     9
  5564.  
  5565. Attachements:                                                       
  5566.  
  5567. 1.  ATN.PRO                                                         
  5568.  
  5569.         Program Listing                                             1-1
  5570.         Phase I - Example I                                         1-5
  5571.         Phase I - Example II                                        1-6
  5572.  
  5573. 2.  ATNREV.PRO                                                      
  5574.  
  5575.         Program Listing                                             2-1
  5576.         Phase II - Example I                                        2-4
  5577.         Phase II - Example II                                       2-4
  5578.  
  5579. 3.  ATNBLD.PRO                                                      
  5580.  
  5581.         Program Listing                                             3-1
  5582.         Example ATN Definition                                      3-3
  5583.  
  5584. 4.  ATNNEW1.PRO                                                     
  5585.  
  5586.         Program Listing                                             4-1
  5587.         Phase III - Example I                                       4-4
  5588.         Phase III - Example II (Subject Predicate Agreement)        4-5
  5589. ..head01rCS680
  5590. ..foot59c##
  5591.                   A PROLOG AUGMENTED TRANSITION NETWORK PARSER
  5592. 
  5593.                                   submitted by
  5594.  
  5595.                               Ludwig J. Schumacher
  5596.  
  5597.                                November 26, 1985
  5598.  
  5599.  
  5600. 1.  General.  This report documents the development of an Augmented Transition
  5601. Network (ATN) sentence parser in the PROLOG language and is submitted in partial
  5602. fulfillment of the requirements for CS 680, Natural Language Processing, George
  5603. Mason University, Fall 1985.  It is assumed that the reader is familiar with
  5604. natural language processing and PROLOG so associated terms will be used without
  5605. explanation.  The author had no prior experience with logic programming and does
  5606. not presume to evaluate the PROLOG language, only that small subset of commands
  5607. he was able to master in attempting to apply the language to this specific
  5608. project.  The examples contained herein are executed under A.D.A. PROLOG,
  5609. ver 1.6c, (c) Automata Design Associates 1985.
  5610.  
  5611.  
  5612. 2.  ATN Form.  The form of the ATN which is used in this paper is shown in
  5613. Figure 1.  This form has been chosen not as comprehensive coverage of English
  5614. sentence structure but to provide a simple form which does incorporate the major
  5615. features of ATNs.  Figure 2 is the noun and prepositional phrase network.
  5616. 
  5617.               -  Transition from node to node based upon specified conditions.
  5618.  
  5619.               -  Optional conditions, such as the determiner and adjective in
  5620.                  the noun phrase.
  5621.                  
  5622.               -  Augmentation with sub-networks such as the noun and
  5623.                  prepositional phrases.
  5624.  
  5625.               -  Recursiveness such as the adjective in the noun phrase and the
  5626.                  noun phrase at node 2.
  5627.  
  5628.  
  5629.  
  5630.  
  5631.                                         np              pp
  5632.                                       *   *           *   *
  5633.                             verb     \/  *      pp   \/ * 
  5634.       np  **** >  q1 ************** > q2/ ********** > q3/
  5635.       *              *                /\
  5636. q0 *                    *    aux      * verb
  5637.       *                     *         *
  5638.       aux **** >  q4 ************* >  q5
  5639.                           np
  5640. 
  5641.  
  5642.  
  5643.  
  5644.                              Figure 1.  ATN Network
  5645.                    adj
  5646.        det      *   *
  5647.       ******    \/  *     noun                         prep
  5648. qnp *       * >  qnp1 ********** > qnp2/        qpp ****** > qnp
  5649.       ******
  5650.        jump
  5651.  
  5652.                            Figure 2.  Phrase Network
  5653.  
  5654.  
  5655. 3.  Approach.  The project was conducted in three phases.
  5656.  
  5657.     a.  Phase I.  Phase I was experimentation with PROLOG to develop some
  5658. working application. An ATN parser was programmed but only by forcing PROLOG
  5659. into a procedural mold.  This phase culminated in a briefing presented in class
  5660. on 24 October.
  5661.  
  5662.     b.  Phase II.  Phase II was the translation of the procedures developed in
  5663. Phase I into facts and rules appropriate for a logic program, which would more
  5664. fully exploit the capabilities of PROLOG.
  5665.  
  5666.     c.  Phase III.  Phase III was additional experimentation to make the programs
  5667. more dynamic and to exploit the power of an ATN to pass differing information
  5668. depending upon the particular transition.  These experiments included procedures
  5669. to automatically update the vocabulary, allow for user defined networks,
  5670. refinement of the database search procedures, and incorporation of subject and
  5671. predicate agreement verification.
  5672.  
  5673.  
  5674. 4.  Phase I
  5675.  
  5676.     a.  The program developed during Phase I (Attachment 1) is not a logic
  5677. program, but a set of procedures executed with the PROLOG language.  Procedures
  5678. are defined as a set of 'q' rules which are passed the current node number and
  5679. the unparsed and parsed parts of the sentence.  Each set of procedures defined
  5680. the actions to be taken at that node. For example, q(0,_) are the procedures for
  5681. node 0, q(1,_,_) are the procedures for node 1, etc.
  5682.  
  5683.     b.  There are a number of limitations to this approach.
  5684.  
  5685.               -  The addition of a node requires a new set of procedures and
  5686. modification of the code for each node from which a transition can be made.
  5687.  
  5688.               -  It would be difficult to modify the code dynamically, since
  5689. procedures must be executed in sequence, and changes would have to be inserted at
  5690. specific points.
  5691. 
  5692.               -  Elimination of a node requires not only the elimination of the
  5693. procedure for that node, but the removal of all calls to that node from other
  5694. procedures.
  5695.  
  5696. ..page
  5697. 5.  Phase II.  Phase II was the development of a logic program to accomplish the
  5698. same functions as that developed during Phase I.  The approach was to translate
  5699. the statement, "There is a transition from node X to node Y on condition Z", to
  5700. the PROLOG equivalent "arc(X,Y,Z)".  The complete program is at Attachment 2 and
  5701. appropriate sections are reproduced below.
  5702. 
  5703.     a.  The first step was to redefine the facts.  The transitions are in the
  5704. form of arc(from node,to node,condition).
  5705.  
  5706.             arc(q0,q1,np).
  5707.             arc(q1,q2,verb).
  5708.             arc(q2,q2,np).
  5709.             arc(q2,q3,pp).
  5710.             arc(q0,q4,aux).
  5711.             arc(q4,q5,np).
  5712.             arc(q1,q5,aux).
  5713.             arc(q5,q2,verb).
  5714.  
  5715.  
  5716.     b. The terminal nodes are identified by term(at node,empty list), where the
  5717. remainder of the sentence is the second variable.
  5718.  
  5719.             term(q2,[]).
  5720.             term(q3,[]).
  5721.  
  5722.     c.  Since phrase transitions are based upon a series of words rather than a
  5723. single condition, they are treated as separate networks.  The empty list as the
  5724. transition condition is used to effect a jump.
  5725.  
  5726.             arc(qnp,qnp1,det).
  5727.             arc(qnp,qnp1,[]).
  5728.             arc(qnp1,qnp1,adj).
  5729.             arc(qnp1,qnp2,noun).
  5730.             arc(qpp,qnp,prep).
  5731.  
  5732.  
  5733.     d.  With these 'facts' established, one can now use the recursive and
  5734. backtracking nature of PROLOG to find a path from the initial point to a
  5735. terminal node.
  5736.  
  5737.               1)  A sentence is input as a PROLOG list enclosed in brackets and
  5738. with each word separated by a comma.  There is no punctuation at the end of the
  5739. sentence.  All words must be in lower case.
  5740.  
  5741.               2)  Once the sentence, S, has been input, control is passed to the
  5742. rule trans (transition).  The variables are: current node, next node, parsed
  5743. sentence, sentence remaining to be parsed, and sentence remaining to be parsed
  5744. after transition.
  5745.  
  5746.          trans(q0,Nq,Parse,S,S1)
  5747. 
  5748. ..page
  5749.               3)  If the current node (Lq) is a terminal node and the remainder
  5750. of the sentence (S1) is null, then the sentence has been parsed.
  5751.  
  5752. trans(Lq,_,Parse,S1,_) :- term(Lq,S1),nl,
  5753.                           print('Completed ',Lq),
  5754.                           nl,print(Parse).
  5755.  
  5756.  
  5757.               4)  If the next word (S0) is the type (Type) to effect a
  5758. transition, then trans is called recursively.  (Note: Nbr is a variable designed
  5759. to provide information on the singularity or plurality of the word.  It is not
  5760. used in this example.)
  5761.  
  5762. trans(Lq,Nq,Parse,[S0|S1],S1) :- word(S0,Type,Nbr),
  5763.                                  arc(Lq,Nq,Type),
  5764.                                  nl,
  5765.                             print('Transition ',Lq,' ',Nq,' ',S0, ' ',Type),
  5766.                                  append(Parse,[[Type],S0],P1),
  5767.                                  !,
  5768.                                  trans(Nq,Z,P1,S1,S2).
  5769.  
  5770.  
  5771.               5)  If the next word in the sentence does not establish the
  5772. criteria for a transition, check to determine if a phrase does. If so, the rest
  5773. of the sentence is checked for the proper phrase, either np or pp. This requires
  5774. the separate network check, ptrans, which allows parsing as the network is
  5775. transitioned, but will return unchanged if it fails.
  5776. 
  5777.  
  5778.  
  5779. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,np),
  5780.                              ptrans(qnp,Nq,Lq,S0,[np],Parse).
  5781.  
  5782.  
  5783. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,pp),
  5784.                              ptrans(qpp,Nq,Lq,S0,[pp],Parse).
  5785.  
  5786.  
  5787.               6)  If no word or phrase has been found to effect a
  5788. transition, the sentence will not parse.
  5789.  
  5790.  
  5791. trans(Lq,Nq,Parse,S0,S1) :- !,nl,
  5792.                             print('The sentence failed at ',Lq),
  5793.                             nl,print('Parsed ',Parse),
  5794.                             nl,print('Left ',S0).
  5795.  
  5796.  
  5797.               7)  The phrase transition network code is almost identical to the
  5798. primary code, except that it continues to call itself until such time as it
  5799. reaches qnp2, which is the terminal node, or fails at a node other than qnp2.
  5800. In the first case it will effect a transition to the next node (Nq) and call
  5801. trans with the new data.  In the second case, ptrans will fail and conditions
  5802. remain unchanged.
  5803. ptrans(Bq,Nq,Lq,[S0|S1],Pr,Parse) :- word(S0,Type,Nbr),
  5804.                                      arc(Bq,Zq,Type),
  5805.                                      append(Pr,[[Type],S0],P1),
  5806.                                      !,
  5807.                                      ptrans(Zq,Nq,Lq,S1,P1,Parse).
  5808.  
  5809. ptrans(qnp2,Nq,Lq,S0,Pr,Parse) :- nl,
  5810.                                   print('Transition ',Lq,' ',Nq),
  5811.                                   nl,
  5812.                                   print(Pr),
  5813.                                   append(Parse,Pr,P1),
  5814.                                   !,
  5815.                                   trans(Nq,Rq,P1,S0,S1).
  5816.  
  5817.  
  5818. 6.  Phase III
  5819. 
  5820.     a.  These programs demonstrate that PROLOG can be used to develop an ATN
  5821. parser but still do not exploit the power of PROLOG to operate in a dynamic
  5822. environment.  Specific capabilities which should be included are:
  5823.  
  5824.               1)  The ATN should be able to provide additional information
  5825. beyond whether or not the sentence parsed.
  5826.  
  5827.               2)  The program should assist the user in construction of the ATN
  5828. definition.
  5829.  
  5830.               3)  The program should verify the words are in the vocabulary set
  5831. before attempting to parse, and if not, allow them to be added.
  5832.  
  5833.               4)  The database search should be efficient. Especially in the
  5834. case of a large vocabulary set, the initial form of word(Name,[type]) is
  5835. unacceptable in that PROLOG must conduct a linear search of the entire set of
  5836. words to identify success or failure.
  5837.  
  5838.                         (a)  Dr. Berghel, University of Nebraska, suggested in
  5839. his presentation at George Mason that the vocabulary be stored as individual
  5840. letters and the search space would be reduced to words of a particular length.
  5841. For example, the word 'the' would be in the database as "word(t,h,e)".  In order
  5842. to identify if "the" is in the vocabulary set, it is partitioned into the
  5843. individual letters and only those words with arity 3 would need to be searched.
  5844.  
  5845.                         (b)  An alternative to the use of arity would be to
  5846. construct each word as a separate fact.  Thus "the" would be represented as
  5847. "the(word)".  It is assumed that PROLOG is more efficient searching a database
  5848. of unique facts rather than one of fewer facts differentiated by arity.  There
  5849. may, however, be some impact on memory requirements.  It must also be noted that
  5850. this would not serve the spelling correction procedure outlined by Dr. Berghel.
  5851.  
  5852.                         (c)  A concept which could be integrated with either of
  5853. the two approaches outlined above would be to allow the facts which are used
  5854. more often to migrate to the front of the list. Specifically, exploit PROLOG's
  5855. capability to alter the program by deleting facts when used and adding them to
  5856. the front of the list.
  5857.     b.  The two programs at Attachments 3 and 4 incorporate some of these
  5858. concepts.  Attachment 3 is a listing and example use of the program 'ATNBLD'
  5859. which can be used to build the primary transition network.  Attachment 4,
  5860. 'ATNNEW', uses the network developed by ATNBLD, stores each word as a separate,
  5861. unique fact, and identifies sentences in which the subject and predicate are not
  5862. in number agreement.
  5863.  
  5864.               1) ATNBLD  ATNBLD requests the names of the nodes and the
  5865. conditions for transition and establishes a separate database for the network
  5866. defined by the user.  The initial node must be entered and all terminal nodes
  5867. identified.  Then the user defines paths to each terminal node.
  5868.  
  5869.                         (a)  BUILD.  Build is the entry point into the program.
  5870. It requests the start node (Q0), the terminal nodes, termnode, then transfers to
  5871. flow.  The predicate 'ret', defined in a standard routine, is used to retract
  5872. predicates.  It is used here to retract any predicates which would interfere
  5873. with the construction of the network.  The predicates generated within the
  5874. program are:
  5875.  
  5876.                         - term: defines that a node is a
  5877.                                 terminal node
  5878.  
  5879.                         - qend: identifies nodes that have a
  5880.                                 path to a terminal node
  5881.  
  5882.                         - arc:  identifies the transitions from
  5883.                                 node to node
  5884.  
  5885.  
  5886. build :- batch,ret(qend),nl,ret(arc),nl,ret(term),asserta(term([],[])),
  5887.          consult(node),nl,print('Enter the start node: '),read(Q0),
  5888.          asserta(qend(Q0)),termnode,flow(Q0).
  5889.  
  5890.  
  5891. termnode :- print('Enter the next terminal node or the word done: '),
  5892.             read(QT),
  5893.             not(QT=done),
  5894.             termck(QT),
  5895.             assertfa(node,term(QT,[])),
  5896.             asserta(qend(QT)),
  5897.             termnode.
  5898.  
  5899. termnode  :-  !,true.
  5900.  
  5901.  
  5902. termck(Qt)  :-  not(term(Qt,[]));
  5903.                 nl,print('Terminal node ',Qt,' already entered'),nl.
  5904.  
  5905. ..page
  5906.                         (b)  FLOW.  Flow is the primary control structure.  It
  5907. requests the next node and the condition for transition.  It verifies that the
  5908. condition is valid, that the arc has not been previously defined, and adds it to
  5909. the database. The predicate 'qendck' verifies a path has been completed.
  5910.  
  5911. flow(Q0)  :- nl,print('Transition from ',Q0,' to ? '),read(Qnext),
  5912.              print(' on condition ? '),read(Con),
  5913.              con(Q0,Con),arcck(Q0,Qnext,Con),
  5914.              assertfz(node,arc(Q0,Qnext,Con)),
  5915.              qendck(Q0,Qnext).
  5916.  
  5917. con(Q0,Con) :- condition(Con).
  5918.  
  5919. con(Q0,Con) :- nl,print(Con,' is an invalid condition. '),
  5920.                flow(Q0).
  5921.  
  5922. condition(verb).
  5923. condition(noun).
  5924. condition(aux).
  5925. condition(prep).
  5926. condition(aux).
  5927. condition(pp).
  5928. condition(np).
  5929.  
  5930.  
  5931. arcck(Q0,Qn,Z)  :-  not(arc(Q0,Qn,Z));
  5932.                     nl,print('Arc from ',Q0,' to ',Qn,' on ',Z,'
  5933.                               exits.').
  5934.  
  5935.  
  5936.                         (c) The predicate 'qendck' verifies that there is a path
  5937. from the end node of the arc just entered to a terminal node.  If not, control
  5938. is passed to 'flow', otherwise 'nextnode' allows a new path to be initiated or
  5939. the program terminated. Pthck is used to verify that there is a path to each of
  5940. the terminal nodes before the program is terminated. Checkstart prevents
  5941. isolated nodes from being inserted into the network.
  5942.  
  5943.  
  5944. qendck(Q0,Qnext)  :- qend(Qnext),(qend(Q0);asserta(qend(Q0))),nextnode.
  5945.  
  5946. qendck(Q0,Qnext)  :-  (qend(Q0);asserta(qend(Q0))),flow(Qnext).
  5947.  
  5948.  
  5949. nextnode :- nl,print('Enter next start node or the word done ? '),
  5950.             read(Ns),
  5951.             not(Ns=done),
  5952.             ((checkstart(Ns),
  5953.             flow(Ns));nextnode).
  5954.  
  5955. ..page
  5956. nextnode :-  pthck,
  5957.              !,retract(term([],[])),
  5958.              nl,print('Network completed'),
  5959.              listing(arc),listing(term),
  5960.              nl,print('Enter name of new ATN file '),read(S),
  5961.              update(node,S),forget(node).
  5962.  
  5963.  
  5964. nextnode :-  nextnode.
  5965.  
  5966.  
  5967. pthck   :-    term(Q,[]),not(Q=[]),not(arc(_,Q,_)),
  5968.               nl,print('No path to terminal node ',Q),
  5969.               !,fail.
  5970.  
  5971. pthck    :-  term([],[]).
  5972.  
  5973.  
  5974. checkstart(Ns) :-  qend(Ns);
  5975.                    nl,print(Ns,' is an invalid node '),fail.
  5976.  
  5977.  
  5978.               2) ATNNEW  One of the features of an ATN vis-a-vis other parsers
  5979. is that the path of transversal can be used to provide information. ATNNEW is an
  5980. example which demonstrates that this can be accomplished in PROLOG. This
  5981. program, which is limited to the ATN in Figure 1, identifies the subject of the
  5982. sentence as the noun (or nouns) in the noun phrase used to transition between
  5983. nodes q0 and q1, or q4 and q5.  It also uses the 'p' or 's' associated with each
  5984. noun or verb and checks the subject and predicate for agreement in number.  The
  5985. code for the predicate ptrans, below, is annotated along the right-hand column
  5986. with numbers to correspond to the notes below.
  5987.  
  5988.  ptrans(Bq,Nq,Lq,[S0|S1],Pr,Parse) :-  S0(Type,Nbr),                       -1
  5989.                                        arc(Bq,Zq,Type),
  5990.         ( ( not(Type=noun); subj(_) ); asserta(subj(Nbr)) ),               -2
  5991.                                        append(Pr,[[Type],S0],P1),
  5992.                                        ptrans(Zq,Nq,Lq,S1,P1,Parse).
  5993.  
  5994. ptrans(Bq,Nq,Lq,S,Pr,Parse)   :-  arc(Bq,Zq,[]),
  5995.                                   ptrans(Zq,Nq,Lq,S,Pr,Parse).
  5996.  
  5997.  
  5998. ptrans(qnp2,Nq,Lq,[S0|S1],Pr,Parse) :-  S0=and,(Lq=q4;Lq=q0),              -3
  5999.                        ( ( subj(_),retract(subj(_)) ); not(subj(_)) ),     -4
  6000.                                         asserta(subj(p)),                  -5
  6001.                                         append(Pr,[and],P1),
  6002.                                         ptrans(qnp,Nq,Lq,S1,P1,Parse).
  6003.  
  6004.  
  6005. ptrans(qnp2,Nq,Lq,[S0|S1],Pr,Parse) :-  S0=and,                            -6
  6006.                                         append(Pr,[and],P1),
  6007.                                         ptrans(qnp,Nq,Lq,S1,P1,Parse).
  6008.  
  6009.  
  6010.  
  6011.  
  6012. ptrans(qnp2,Nq,Lq,S0,Pr,Parse) :-  nl,
  6013.                                    print('Transition ',Lq,' ',Nq),
  6014.                                    nl,
  6015.                                    print(Pr),
  6016.                                    append(Parse,Pr,P1),
  6017.                                    trans(Nq,Rq,P1,S0,S1).
  6018.  
  6019.  
  6020.  
  6021.               -1.  S0 is the next word in the sentence.  Each word is defined as
  6022. a unique fact with a type and number (p or s or x).
  6023. 
  6024.               -2.  This line establishes the number of the subject as that of
  6025. the noun unless one has already been established.  The subject for all sentences
  6026. covered by the ATN in Figure 1 will be located in noun phrases parsed before
  6027. reaching node q2, hence nouns in noun phrases at node q2 or q3 will be ignored.
  6028.  
  6029.               -3.  This predicate is a special provision for the use of 'and' if
  6030. the next word is 'and', and we are attempting to transition from node q4 or q0.
  6031.  
  6032.               -4.  Retract the predicate subj which contains the number for the
  6033. subject.  The not(subj(_)) is actually not required, since the subj has had to
  6034. been asserted if the program gets to this point but is included for balance.
  6035.  
  6036.               -5.  This part of the clause establishes the number associated with
  6037. the subject as plural based on the use of and.
  6038.  
  6039.               -6.  This clause accounts for the case of an and in a noun phrase
  6040. not at node q0 or q4.
  6041.  
  6042. 6.  Conclusions.  The programs developed for this project demonstrate that
  6043. PROLOG is a powerful language that offers unique capabilities and interesting
  6044. potential but little user interface. It is surprising that the power of the
  6045. language has not been exploited to enhance the utility.
  6046.  
  6047.     a.  Any useful application will require some form of procedure.
  6048. Construction of these procedures, such as in the Phase III example, is awkward
  6049. in the current language.
  6050.  
  6051.     b.  Although all variables are local to a predicate, the dynamic nature of
  6052. PROLOG enables the programmer to establish global variables through program
  6053. modification.  It is this feature which appears to offer great potential.
  6054.  
  6055.     c.  There are some alternative search techniques, beyond the scope of this
  6056. paper, which should be evaluated.
  6057.  
  6058.     d.  Given that these examples only employ the most rudimentary PROLOG
  6059. commands, the language appears to offer a rich environment, limited primarily by
  6060. the lack of user interface.
  6061.  
  6062.  
  6063.  
  6064. ..pgno1
  6065. ..foot59c1-##
  6066. /*                 Augmented Transition Network Program
  6067.  
  6068.                                   ATN.PRO
  6069.  
  6070.                                  10/22/85
  6071. */
  6072.  
  6073.  
  6074. /*  Standard routines for append & membership checking       */
  6075.  
  6076.  
  6077. append([],L,L).
  6078. append([Z|L1],L2,[Z|L3]) :- append(L1,L2,L3).
  6079.  
  6080. printstring([]).
  6081. printstring([H|T]) :- put(H), printstring(T).
  6082.  
  6083. member(X,[X|_]).
  6084. member(X,[_|Y]) :- member(X,Y).
  6085.  
  6086.  
  6087. /*  The start module accepts a set of words, enclosed in brackets and
  6088. separated by commas.  It calls wordck to verify that each of the words is
  6089. in the vocabulary set.   */
  6090.  
  6091.  
  6092.  
  6093. start :- batch,nl,print('INPUT'),nl,print('-----'),nl,
  6094.          nl,print('Input sentence: '),read(S),nl,
  6095.          print('The working set is ',S),wordck(S),!,
  6096.          nl,nl,print('TRANSFERS'),nl,nl,print('---------'),nl,nl,q(0,S).
  6097.  
  6098.  
  6099.  
  6100. /*  Wordck checks for the end of the set, [], then if the word is in the
  6101. vocabulary.  If not, it asks for the category, and adds it to the file
  6102. WORD.TEM which is joined with the program after it has run.*/
  6103.  
  6104.  
  6105.  
  6106. wordck([])   :-   !,true.
  6107.  
  6108. wordck([H|T]) :- word(H,Y),wordck(T).
  6109.  
  6110.  
  6111. wordck([H|T]) :-  nl,print(H,' is not a recognized word '),
  6112.                   nl,print(' enter verb,aux, .. '),read(Z),
  6113.                   wordnew(H,Z),wordck(T).
  6114.  
  6115. wordnew(W,Z) :- assertz(word(W,Z)),open('word.tem',ar),
  6116.                 nlf('word.tem'),
  6117.                 printf('word.tem', 'word(', W, ',', Z, ').'),
  6118.                 close('word.tem').
  6119.  
  6120.  
  6121. /*  Trans checks the category of the current word (H) versus the category
  6122. required to make a transition (Z).  */
  6123.  
  6124.  
  6125. trans(H,Z)   :-  word(H,X), member(X,[Z]).
  6126.  
  6127.  
  6128. qfail(Nq,S,E)  :-  !, nl,nl,print('The sentence failed at ',Nq),nl,
  6129.                  print('The sentence form to this node is ',E),nl,
  6130.                  print('The rest of the sentence is ',S),qend1.
  6131.  
  6132.  
  6133. qend(Z,E)  :-  nl,nl,print('OUTPUT'),nl,print('------'),nl,nl,
  6134.                print('The sentence is:'),nl,nl,print(E),nl,nl,
  6135.                print('The sentence is completed at node ',Z),qend1.
  6136.  
  6137.  
  6138. qend1 :- open('word.tem',ar),nlf('word.tem'),
  6139.          close('word.tem'),exec('ren atn.pro atn.sav'),
  6140.          exec('copy atn.sav+word.tem atn.pro'),
  6141.          exec('erase atn.sav'),exec('erase word.tem').
  6142.  
  6143.  
  6144. /*    Print transfer from node to node */
  6145.  
  6146.  
  6147. qout(A,B,C,D,E,F) :- append(E,[C,'(',A,')'],F),
  6148.                      nl, print('Transfer from node ',B,' to node ',D,
  6149.                      ' by word ',A,' evaluated as a ',C).
  6150.  
  6151.  
  6152. /*   Main program to check the conditions for transfer from node to node.
  6153.      The first number is the number of the node, i.e. q(0.. is node 0.
  6154.      The module either checks for a word type and transfers control
  6155.      directly, or passes to np / pp the next node.                    */
  6156.  
  6157.  
  6158. /*  Node 0 - aux to 4 / np to 1 / or fail          */
  6159.  
  6160.  
  6161. q(0,[H|T])  :-  trans(H,[aux]),!,qout(H,0,[aux],4,E,F), q(4,T,F).
  6162.  
  6163. q(0,[H|T])  :-  np(H,T,1,[],0,[np]).
  6164.  
  6165. q(0,S)      :-  qfail(0,S,[]).
  6166.  
  6167.  
  6168. /*  Node 1 - verb to 2 / aux to 5 / or fail    */
  6169.  
  6170.  
  6171. q(1,[H|T],E)  :-  trans(H,[verb]),!,qout(H,1,[verb],2,E,F), q(2,T,F).
  6172.  
  6173. q(1,[H|T],E)  :-  trans(H,[aux]),!, qout(H,1,[aux],5,E,F), q(5,T,F).
  6174.  
  6175. q(1,S,E)      :-  qfail(1,S,E).
  6176.  
  6177. /*  Node 2 -  null to end / np to 2 / pp to 3 / or fail     */
  6178.  
  6179.  
  6180. q(2,H,E)      :- member(H,[[]]), !,
  6181.                    qend(2,E).
  6182.  
  6183. q(2,[H|T],E)  :-  np(H,T,2,E,2,[np]).
  6184.  
  6185.  
  6186. q(2,[H|T],E)  :-  pp(H,T,3,E,2,[pp]).
  6187.  
  6188. q(2,S,E)      :-  qfail(2,S,E).
  6189.  
  6190.  
  6191. /*  Node 3 - null to end / or fail         */
  6192.  
  6193.  
  6194. q(3,H,E)  :- trans(H,[]), !,
  6195.              qend(3,E).
  6196.  
  6197. q(3,S,E)      :-  qfail(3,S,E).
  6198.  
  6199.  
  6200.  
  6201. /*  Node 4 - np to 5 / or fail           */
  6202.  
  6203.  
  6204. q(4,[H|T],E)  :-  np(H,T,5,E,4,[np]).
  6205.  
  6206. q(4,S,E)      :-  qfail(4,S,E).
  6207.  
  6208.  
  6209.  
  6210. /*  Node 5 - verb to 2 / or fail         */
  6211.  
  6212.  
  6213. q(5,[H|T],E)  :-  trans(H,[verb]),!, qout(H,5,[verb],2,E,F), q(2,T,F).
  6214.  
  6215. q(5,S,E)      :-  qfail(5,S,E).
  6216.  
  6217.  
  6218.  
  6219. /*  Noun phrase -  (det) (adj) (adj) .. noun        */
  6220.  
  6221. /*  The np1 clause is required to allow recursive calls for adj   */
  6222.  
  6223.  
  6224. np(H,[S|T],Nq,E,Lq,G)  :-  trans(H,[det]), !,
  6225.                            append(G,['det(',H,')'],G1),
  6226.                            np1([S|T],Nq,E,Lq,G1).
  6227.  
  6228.  
  6229. np(H,Z,Nq,E,Lq,G)      :-  np1([H|Z],Nq,E,Lq,G).
  6230.  
  6231.  
  6232.  
  6233. np1([H|T],Nq,E,Lq,G)  :-  trans(H,[adj]),
  6234.                           append(G,['adj(',H,')'],G1),
  6235.                           np1(T,Nq,E,Lq,G1).
  6236.  
  6237.  
  6238. np1([H|T],Nq,E,Lq,G)  :- trans(H,[noun]),!,nl,
  6239.                          append(G,['noun(',H,')'],G1),
  6240.                          append(E,G1,F),
  6241.                          print('Transfer from node ',Lq,' to ',Nq),
  6242.                          print(' by ',G1),q(Nq,T,F).
  6243.  
  6244.  
  6245. /*  Prep phrase requires a prep followed by a np   */
  6246.  
  6247.  
  6248. pp(H,[S|T],Nq,E,Lq,G)  :-  trans(H,[prep]),
  6249.                            append(['prep(',H,')'],G,G1),
  6250.                            np(S,T,Nq,E,Lq,G1).
  6251.  
  6252.  
  6253. /*   Word defines the vocabulary set                  */
  6254.  
  6255.  
  6256. word(the,[det]).
  6257. word(boy,[noun]).
  6258. word(runs,[verb]).
  6259. word(happy,[adj]).
  6260. word(john,[noun]).
  6261. word(can,[aux]).
  6262. word(run,[verb]).
  6263. word(a,[det]).
  6264. word(big,[adj]).
  6265. word(small,[adj]).
  6266. word(girl,[noun]).
  6267. word(dog,[noun]).
  6268. word(on,[prep]).
  6269. word(pretty,[adj]).
  6270. word(fast,[adj]).
  6271. word(barks,[verb]).
  6272. word(to,[prep]).
  6273. word([],[]).
  6274. word(giant, [noun]).
  6275. word(is, [verb]).
  6276. word(giant, [noun]).
  6277. word(is, [verb]).
  6278. word(sleeps, [verb]).
  6279. word(mary, [noun]).
  6280. word(likes, [verb]).
  6281.  
  6282. ..pgno1
  6283. ..foot59c2-##
  6284. /*                 Augmented Transition Network Program
  6285.  
  6286.                                 ATNREV.PRO
  6287.  
  6288.                                  11/24/85
  6289. */
  6290.  
  6291.  
  6292. /*  Standard routines for append & membership checking       */
  6293.  
  6294.  
  6295. append([],L,L).
  6296. append([Z|L1],L2,[Z|L3]) :- append(L1,L2,L3).
  6297.  
  6298. printstring([]).
  6299. printstring([H|T]) :- put(H), printstring(T).
  6300.  
  6301. member(X,[X|_]).
  6302. member(X,[_|Y]) :- member(X,Y).
  6303.  
  6304.  
  6305. /*  The start module accepts a set of words, enclosed in brackets and
  6306. separated by commas.  It calls wordck to verify that each of the words is
  6307. in the vocabulary set.   */
  6308.  
  6309.  
  6310. start :- batch,nl,print('INPUT'),nl,print('-----'),nl,
  6311.          nl,print('Input sentence: '),read(S),nl,
  6312.          print('The working set is ',S),wordck(S),!,
  6313.          nl,nl,print('TRANSFERS'),nl,nl,print('---------'),nl,nl,
  6314.          Parse=[],
  6315.          trans(q0,Nq,Parse,S,S1).
  6316.  
  6317.  
  6318. /*  Wordck checks for the end of the set, [], then if the word is in the
  6319. vocabulary.  If not, it asks for the category, and adds it to the file
  6320. WORD.TEM which is joined with the program after it has run.*/
  6321.  
  6322.  
  6323. wordck([])   :-   !,true.
  6324.  
  6325. wordck([H|T]) :- word(H,_,_),wordck(T).
  6326.  
  6327.  
  6328. wordck([H|T]) :-  nl,print(H,' is not a recognized word '),
  6329.                   nl,print(' enter verb,aux, .. '),read(Z),
  6330.                   wordnew(H,Z),wordck(T).
  6331.  
  6332. wordnew(W,Z) :- assertz(word(W,Z,s)),open('word.tem',ar),
  6333.                 nlf('word.tem'),
  6334.                 printf('word.tem', 'word(', W, ',', Z, ').'),
  6335.  
  6336.  
  6337.  
  6338.  
  6339. /*  The arcs are defined in terms of from node, to node, condition.
  6340. Terminal nodes are identified with the empty list.  Words are defined by
  6341. type word name, type, and a character to be used in later examples with the
  6342. number (plural or singular).  */
  6343.  
  6344.  
  6345. arc(q0,q1,np).
  6346. arc(q1,q2,verb).
  6347. arc(q2,q2,np).
  6348. arc(q2,q3,pp).
  6349. arc(q0,q4,aux).
  6350. arc(q4,q5,np).
  6351. arc(q1,q5,aux).
  6352. arc(q5,q2,verb).
  6353.  
  6354. term(q2,[]).
  6355. term(q3,[]).
  6356.  
  6357.  
  6358. word(boy,noun,s).
  6359. word(boys,noun,pl).
  6360. word(run,verb,pl).
  6361. word(runs,verb,s).
  6362. word(the,det,s).
  6363.  
  6364. arc(qnp,qnp1,det).
  6365. arc(qnp,qnp1,_).
  6366. arc(qnp1,qnp1,adj).
  6367. arc(qnp1,qnp2,noun).
  6368. arc(qpp,qnp,prep).
  6369.  
  6370.  
  6371. /*  Trans recursively checks the conditions for transition from the last
  6372. node (Lq) to the next node (Nq).  Phrases are specifically treated as pp or
  6373. np in order to allow the type of phrase to be identified in the parsed
  6374. sentence.  */
  6375.  
  6376.  
  6377. trans(Lq,_,Parse,S1,_) :- term(Lq,S1),nl,
  6378.                           print('Completed ',Lq),
  6379.                           nl,print(Parse).
  6380.  
  6381.  
  6382. trans(Lq,Nq,Parse,[S0|S1],S1) :-  word(S0,Type,Nbr),
  6383.                                   arc(Lq,Nq,Type),
  6384.                                   nl,
  6385.                                   print('Transition ',Lq,' ',Nq,' ',S0,
  6386.                                             ' ',Type),
  6387.                                   append(Parse,[[Type],S0],P1),
  6388.                                   !,
  6389.                                   trans(Nq,Z,P1,S1,S2).
  6390.  
  6391.  
  6392. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,np),
  6393.                              ptrans(qnp,Nq,Lq,S0,[np],Parse).
  6394.  
  6395.  
  6396. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,pp),
  6397.                              ptrans(qpp,Nq,Lq,S0,[pp],Parse).
  6398.  
  6399.  
  6400.  
  6401. trans(Lq,Nq,Parse,S0,S1) :- !,nl,
  6402.                             print('The sentence failed at ',Lq),
  6403.                             nl,print('Parsed ',Parse),
  6404.                             nl,print('Left ',S0).
  6405.  
  6406. /*  Ptrans checks the transition of the phrase network.  The first clause
  6407. calls itself recursively until node qnp2 has been reached, which concludes
  6408. the transition.  Success results in trans being called  with the new node.
  6409. Failure returns the trans with conditions unchanged.  */
  6410.  
  6411.  
  6412. ptrans(Bq,Nq,Lq,[S0|S1],Pr,Parse) :-  word(S0,Type,Nbr),
  6413.                                 arc(Bq,Zq,Type),
  6414.                                 append(Pr,[[Type],S0],P1),
  6415.                                 !,
  6416.                                 ptrans(Zq,Nq,Lq,S1,P1,Parse).
  6417.  
  6418. ptrans(qnp2,Nq,Lq,S0,Pr,Parse) :-  nl,
  6419.                                    print('Transition ',Lq,' ',Nq),
  6420.                                    nl,
  6421.                                    print(Pr),
  6422.                                    append(Parse,Pr,P1),
  6423.                                    !,
  6424.                                    trans(Nq,Rq,P1,S0,S1).
  6425.  
  6426. ..page
  6427. ..pgno1
  6428. ..foot59c3-##
  6429. /*        PROGRAM TO BUILD PRIMARY AUGMENTED TRANSITION NETWORK
  6430.  
  6431.                                 ATNBLD.PRO
  6432.  
  6433.                                  11/24/85
  6434.  
  6435. */
  6436.  
  6437.  
  6438. /*  Build is the entry point into the program.  It requires that the
  6439. program with standard routines and the program node, which is empty, have
  6440. already been consulted.  */
  6441.  
  6442. /*  The program requests the start and terminal nodes, the paths and
  6443. transition conditions, then establishes a node program with a name
  6444. specified by the user.  */
  6445.  
  6446. /*  Ret removes any data from memory which might interfere with network
  6447. construction.  Term([],[]) is required to prevent failure when checkint
  6448. terminal conditions.  Qend identifies all nodes for which there is a path
  6449. to a terminal node.  The start node is identified initially since the
  6450. program will require this path be completed before any other can be
  6451. constructed.  Termnode accepts the terminal nodes.  Flow accepts the
  6452. transition arcs and conditions.  */
  6453.  
  6454.  
  6455. build :- batch,ret(qend),nl,ret(arc),nl,ret(term),asserta(term([],[])),
  6456.          nl,print('Enter the start node: '),read(Q0),
  6457.          asserta(qend(Q0)),termnode,flow(Q0).
  6458.  
  6459.  
  6460. termnode :-  print('Enter the next terminal node or the word done: '),
  6461.              read(QT),
  6462.              not(QT=done),
  6463.              termck(QT),
  6464.              assertfa(node,term(QT,[])),
  6465.              asserta(qend(QT)),
  6466.              termnode.
  6467.  
  6468. termnode  :-  !,true.
  6469.  
  6470.  
  6471. /*  Flow requests transitions from node to node and adds each arc and new
  6472. node to the database.  Qendck will continue to call flow until such time as
  6473. a terminal node has been reached then allow a new path to be initiated.  */
  6474.  
  6475.  
  6476.  
  6477. flow(Q0)  :- nl,print('Transition from ',Q0,' to ? '),read(Qnext),
  6478.              print(' on condition ? '),read(Con),
  6479.              con(Q0,Con),arcck(Q0,Qnext,Con),
  6480.              assertfz(node,arc(Q0,Qnext,Con)),
  6481.              qendck(Q0,Qnext).
  6482.  
  6483. con(Q0,Con) :- condition(Con).
  6484.  
  6485. con(Q0,Con) :- nl,print(Con,' is an invalid condition. '),
  6486.                flow(Q0).
  6487.  
  6488. termck(Qt)  :-  not(term(Qt,[]));
  6489.                 nl,print('Terminal node ',Qt,' already entered'),nl.
  6490.  
  6491. arcck(Q0,Qn,Z)  :-  not(arc(Q0,Qn,Z));
  6492.                     nl,print('Arc from ',Q0,' to ',Qn,' on ',Z,' exits.').
  6493.  
  6494. qendck(Q0,Qnext)  :-  qend(Qnext),(qend(Q0);asserta(qend(Q0))),nextnode.
  6495.  
  6496. qendck(Q0,Qnext)  :-  (qend(Q0);asserta(qend(Q0))),flow(Qnext).
  6497.  
  6498.  
  6499. /*  Nextnode allows a new path to be initiated or the program to be
  6500. terminated.  Before termination it calls pthck to insure there is a path to
  6501. each terminal node.  Checkstart prevents an isolated node from being
  6502. entered.  */
  6503.  
  6504. nextnode :-  nl,print('Enter next start node or the word done ? '),
  6505.              read(Ns),
  6506.              not(Ns=done),
  6507.              ((checkstart(Ns),
  6508.              flow(Ns));nextnode).
  6509.  
  6510. nextnode :-  pthck,
  6511.              !,retract(term([],[])),
  6512.              nl,print('Network completed'),
  6513.              listing(arc),listing(term),
  6514.              nl,print('Enter name of new ATN file '),read(S),
  6515.              update(node,S).
  6516.  
  6517. nextnode :-  nextnode.
  6518.  
  6519. pthck   :-    term(Q,[]),not(Q=[]),not(arc(_,Q,_)),
  6520.               nl,print('No path to terminal node ',Q),
  6521.               !,fail.
  6522.  
  6523. pthck    :-  term([],[]).
  6524.  
  6525. checkstart(Ns) :-  qend(Ns);
  6526.                    nl,print(Ns,' is an invalid node '),fail.
  6527.  
  6528. /*  Condition lists the acceptable conditions for a transition.   */
  6529.  
  6530. condition(verb).
  6531. condition(noun).
  6532. condition(aux).
  6533. condition(prep).
  6534. condition(aux).
  6535. condition(pp).
  6536. condition(np).
  6537. ..pgno1
  6538. ..foot59c4-##
  6539. /*              FINAL AUGMENTED TRANSITION NETWORK PROGRAM
  6540.  
  6541.                                 ATNNEW1.PRO
  6542.  
  6543.                                  11/24/85
  6544. */
  6545.  
  6546.  
  6547. /*  Start is the entry into the program.  It requires that a set of
  6548. standard routines has already been consulted (append in particular).  It
  6549. allows the user to specify the network program, which can be build using
  6550. ATNBLD.  Words is a file with the vocabulary set.  The sentences is a list
  6551. of words separated by commas and enclosed in brackets.  Wordck verifies
  6552. that the words are in the vocabulary set, and if not requests required
  6553. data. Parse is the sentence as it is parsed.  Trans controls the flow from
  6554. node to node.  */
  6555.  
  6556. start :- nl,print('ATN network file? '),read(Fn),
  6557.          consult(Fn),nl,
  6558.          asserta(file(Fn)),
  6559.          consult(words),nl,
  6560.          batch,nl,print('INPUT'),nl,print('-----'),nl,
  6561.          nl,print('Input sentence: '),read(S),nl,
  6562.          print('The working set is ',S),wordck(S),
  6563.          nl,nl,print('TRANSFERS'),nl,nl,print('---------'),nl,nl,
  6564.          Parse=[],
  6565.          trans(q0,Nq,Parse,S,S1).
  6566.  
  6567.  
  6568. wordck([])   :-   true.
  6569.  
  6570. wordck([H|T]) :- H(_,_),wordck(T).
  6571.  
  6572. wordck([H|T]) :-  nl,print(H,' is not a recognized word '),
  6573.                   nl,print(' enter verb,aux, .. '),read(Z),
  6574.                   nl,print(' enter p or s or x  '),read(Z1),
  6575.                   wordnew(H,Z,Z1),wordck(T).
  6576.  
  6577. wordnew(W,Z,Z1) :- assertfz(words,W(Z,Z1)).
  6578.  
  6579.  
  6580. /*  Since the phrase transition network includes more specific procedures
  6581. than the primary network, it is included in this program rather than in the
  6582. network file consulted by start.  It could be more dynamic, but that was
  6583. considered beyond the scope of this project.  */
  6584.  
  6585.  
  6586. arc(qnp,qnp1,det).
  6587. arc(qnp,qnp1,[]).
  6588. arc(qnp1,qnp1,adj).
  6589. arc(qnp1,qnp2,noun).
  6590. arc(qpp,qnp,prep).
  6591.  
  6592.  
  6593. /*  Trans controls the flow along the network.  If a terminal node has been
  6594. reached and the entire sentence has been parsed, the agreement in number
  6595. (plural or singular) between the subject and predicate is checked.  If they
  6596. do not agree, this fact is displayed.  Update words creates a file
  6597. WORDS.$$$ which contains the new vocabulary.
  6598.  
  6599. If a conditions for termination has not been met, trans checks for a
  6600. transition word or a transition phrase.  If none of these conditions are
  6601. met, the sentence will not parse.
  6602.  
  6603. When a verb is encountered the number (singular or plural) is 'filed'.
  6604. This procedure is unique for a specific network in which only one verb can
  6605. be encountered.  */
  6606.  
  6607. trans(Lq,_,Parse,S1,_) :- term(Lq,S1),nl,
  6608.                           print('Completed ',Lq),
  6609.                           nl,print(Parse),
  6610.                        (  ( subj(Nbr),pred(Nbr) );
  6611.                       (nl,print('The subject and predicate do not agree.')
  6612.                           ) ),
  6613.                           update(words),
  6614.                           exec('erase words.pro'),
  6615.                           exec('ren words.$$$ words.pro'),
  6616.                           forget(words),
  6617.                           file(Fn),
  6618.                           forget(Fn),
  6619.                           endclr.
  6620.  
  6621. endclr :-  (not(file(_));ret(file)),(not(subj(_));ret(subj)),
  6622.            (not(pred(_));ret(pred)).
  6623.  
  6624.  
  6625. trans(Lq,Nq,Parse,[S0|S1],S1) :-  S0(Type,Nbr),
  6626.                                   arc(Lq,Nq,Type),
  6627.                                  ((Type=verb,asserta(pred(Nbr)));
  6628.                                      not(type=verb)),
  6629.                                   nl,
  6630.                                   print('Transition ',Lq,' ',Nq,' ',S0,
  6631.                                             ' ',Type),
  6632.                                   append(Parse,[[Type],S0],P1),
  6633.                                   trans(Nq,Z,P1,S1,S2).
  6634.  
  6635.  
  6636. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,np),
  6637.                              ptrans(qnp,Nq,Lq,S0,[' '+np],Parse).
  6638.  
  6639. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,pp),
  6640.                              ptrans(qpp,Nq,Lq,S0,[' '+pp],Parse).
  6641.  
  6642. trans(Lq,Nq,Parse,S0,S1) :- nl,
  6643.                             print('The sentence failed at ',Lq),
  6644.                             nl,print('Parsed ',Parse),
  6645.                             nl,print('Left ',S0),
  6646.                             endclr.
  6647. /*  Ptrans checks the transition of the phrase network.  It calls itself
  6648. recursively until node qnp2 is reached.  Provisions are included to
  6649. establish the number (plural or singular) of the subject, which is designed
  6650. for a specific network in which the noun phrase in which the subject is
  6651. located will be encountered before any other noun phrase.
  6652.  
  6653. The upon reaching qnp2 a check is made for the word 'and'.  If encountered,
  6654. the number of the subject is changed to plural and a check for another noun
  6655. phrase is initiated.
  6656.  
  6657. The spacing of the parenthesis is to facilitate reading of the code.  */
  6658.  
  6659.  
  6660. ptrans(Bq,Nq,Lq,[S0|S1],Pr,Parse) :-  S0(Type,Nbr),
  6661.                                 arc(Bq,Zq,Type),
  6662.                           (
  6663.                              (
  6664.                                  not(Type=noun);
  6665.  
  6666.                                  subj(_)
  6667.                                           );
  6668.                                  asserta(subj(Nbr))
  6669.                                                   ),
  6670.                                 append(Pr,[[Type],S0],P1),
  6671.                                 ptrans(Zq,Nq,Lq,S1,P1,Parse).
  6672.  
  6673. ptrans(Bq,Nq,Lq,S,Pr,Parse)   :-  arc(Bq,Zq,[]),
  6674.                                   ptrans(Zq,Nq,Lq,S,Pr,Parse).
  6675.  
  6676.  
  6677. ptrans(qnp2,Nq,Lq,[S0|S1],Pr,Parse) :-  S0=and,(Lq=q4;Lq=q0),
  6678.                                        ( ( subj(_),retract(subj(_)) );
  6679.                                          not(subj(_)) ),
  6680.                                          asserta(subj(p)),
  6681.                                           append(Pr,[and],P1),
  6682.                                           ptrans(qnp,Nq,Lq,S1,P1,Parse).
  6683.  
  6684.  
  6685. ptrans(qnp2,Nq,Lq,[S0|S1],Pr,Parse) :-  S0=and,
  6686.                                           append(Pr,[and],P1),
  6687.                                           ptrans(qnp,Nq,Lq,S1,P1,Parse).
  6688.  
  6689.  
  6690.  
  6691.  
  6692. ptrans(qnp2,Nq,Lq,S0,Pr,Parse) :-  nl,
  6693.                                    print('Transition ',Lq,' ',Nq),
  6694.                                    nl,
  6695.                                    print(Pr),
  6696.                                    append(Parse,Pr,P1),
  6697.                                    trans(Nq,Rq,P1,S0,S1).
  6698. append([],L,L).
  6699. append([Z|L1],L2,[Z|L3]) :- append(L1,L2,L3).
  6700.  
  6701. printstring([]).
  6702. printstring([H|T]) :- put(H), printstring(T).
  6703.  
  6704. ret(X) :- X(_),retract(X(_)),ret(X).
  6705. ret(X) :- X(_,_),retract(X(_,_)),ret(X).
  6706. ret(X) :- X(_,_,_),retract(X(_,_,_)),ret(X).
  6707. ret(X) :- nl,print(X,' has been retracted').
  6708. /*         final atn program    */
  6709. /*                empty node program          */
  6710. /*        PROGRAM TO BUILD PRIMARY AUGMENTED TRANSISTION NETWORK
  6711.  
  6712.                                 ATNBLD.PRO
  6713.  
  6714.                                  11/24/85
  6715.  
  6716. */
  6717.  
  6718.  
  6719. /*  Build is the entry point into the program.  It requires that the
  6720. program with standard routines and the program node, which is empty, have
  6721. already been consulted.  */
  6722.  
  6723. /*  The program requests the start and terminal nodes, the paths and
  6724. transistion conditions, then establishes a node program with a name
  6725. specified by the user.  */
  6726.  
  6727. /*  Ret removes any data from memory which might iterfere with network
  6728. construction.  Term([],[]) is required to prevent failure when checkint
  6729. terminal conditions.  Qend identifies all nodes for which there is a path
  6730. to a terminal node.  The start node is identified initially since the
  6731. program will require this path be completed before any other can be
  6732. constructed.  Termnode accepts the terminal nodes.  Flow accepts the
  6733. transition arcs and conditions.  */
  6734.  
  6735.  
  6736. build :- batch,ret(qend),nl,ret(arc),nl,ret(term),asserta(term([],[])),
  6737.          nl,print('Enter the start node: '),read(Q0),
  6738.          asserta(qend(Q0)),termnode,flow(Q0).
  6739.  
  6740.  
  6741. termnode :-  print('Enter the next terminal node or the word done: '),
  6742.              read(QT),
  6743.              not(QT=done),
  6744.              termck(QT),
  6745.              assertfa(node,term(QT,[])),
  6746.              asserta(qend(QT)),
  6747.              termnode.
  6748.  
  6749. termnode  :-  !,true.
  6750.  
  6751.  
  6752. /*  Flow requests transistions from node to node and adds each arc and new
  6753. node to the database.  Qendck will continue to call flow until such time as
  6754. a terminal node has been reached then allow a new path to be initiated.  */
  6755.  
  6756.  
  6757.  
  6758. flow(Q0)  :- nl,print('Transisition from ',Q0,' to ? '),read(Qnext),
  6759.              print(' on condition ? '),read(Con),
  6760.              con(Q0,Con),arcck(Q0,Qnext,Con),
  6761.              assertfz(node,arc(Q0,Qnext,Con)),
  6762.              qendck(Q0,Qnext).
  6763.  
  6764. con(Q0,Con) :- condition(Con).
  6765.  
  6766. con(Q0,Con) :- nl,print(Con,' is an invalid condition. '),
  6767.                flow(Q0).
  6768.  
  6769. termck(Qt)  :-  not(term(Qt,[]));
  6770.                 nl,print('Terminal node ',Qt,' already entered'),nl.
  6771.  
  6772. arcck(Q0,Qn,Z)  :-  not(arc(Q0,Qn,Z));
  6773.                     nl,print('Arc from ',Q0,' to ',Qn,' on ',Z,' exits.').
  6774.  
  6775. qendck(Q0,Qnext)  :-  qend(Qnext),(qend(Q0);asserta(qend(Q0))),nextnode.
  6776.  
  6777. qendck(Q0,Qnext)  :-  (qend(Q0);asserta(qend(Q0))),flow(Qnext).
  6778.  
  6779.  
  6780. /*  Nextnode allows a new path to be initiated or the program to be
  6781. terminated.  Before termination it calls pthck to insure there is a path to
  6782. each terminal node.  Checkstart prevents an isolated node from being
  6783. entered.  */
  6784.  
  6785.  
  6786.  
  6787. nextnode :-  nl,print('Enter next start node or the word done ? '),
  6788.              read(Ns),
  6789.              not(Ns=done),
  6790.              ((checkstart(Ns),
  6791.              flow(Ns));nextnode).
  6792.  
  6793.  
  6794. nextnode :-  pthck,
  6795.              !,retract(term([],[])),
  6796.              nl,print('Network completed'),
  6797.              listing(arc),listing(term),
  6798.              nl,print('Enter name of new ATN file '),read(S),
  6799.              update(node,S).
  6800.  
  6801.  
  6802. nextnode :-  nextnode.
  6803.  
  6804.  
  6805. pthck   :-    term(Q,[]),not(Q=[]),not(arc(_,Q,_)),
  6806.               nl,print('No path to terminal node ',Q),
  6807.               !,fail.
  6808.  
  6809. pthck    :-  term([],[]).
  6810.  
  6811.  
  6812. checkstart(Ns) :-  qend(Ns);
  6813.                    nl,print(Ns,' is an invalid node '),fail.
  6814.  
  6815.  
  6816. /*  Condition lists the acceptable conditions for a transistion.   */
  6817.  
  6818.  
  6819. condition(verb).
  6820. condition(noun).
  6821. condition(aux).
  6822. condition(prep).
  6823. condition(aux).
  6824. condition(pp).
  6825. condition(np).
  6826.  
  6827. plane(noun,s).
  6828.  
  6829.  
  6830. green(adj,x).
  6831.  
  6832.  
  6833. gocart(noun,s).
  6834.  
  6835.  
  6836. black(adj,x).
  6837.  
  6838.  
  6839. bikes(noun,p).
  6840.  
  6841.  
  6842. farm(noun,s).
  6843.  
  6844.  
  6845. girls(noun,p).
  6846.  
  6847.  
  6848. car(noun,s).
  6849.  
  6850.  
  6851. rides(verb,s).
  6852.  
  6853.  
  6854. and(conj,x).
  6855.  
  6856.  
  6857. girl(noun,s).
  6858.  
  6859.  
  6860. ran(verb,p).
  6861.  
  6862.  
  6863. big(adj,x).
  6864.  
  6865.  
  6866. bike(noun,s).
  6867.  
  6868.  
  6869. ride(verb,p).
  6870.  
  6871.  
  6872. store(noun,s).
  6873.  
  6874.  
  6875. to(prep,x).
  6876.  
  6877.  
  6878. home(noun,s).
  6879.  
  6880. /*           words file          */
  6881. /*          11/19/85           */
  6882.  
  6883.  
  6884. boy(noun,s).
  6885. boys(noun,p).
  6886. run(verb,p).
  6887. runs(verb,s).
  6888. can(aux,x).
  6889. the(det,s).
  6890. /*                 Augmented Transition Network Program
  6891.  
  6892.                                 ATNREV.PRO
  6893.  
  6894.                                  11/24/85
  6895. */
  6896.  
  6897.  
  6898. /*  Standard routines for append & membership checking       */
  6899.  
  6900.  
  6901. append([],L,L).
  6902. append([Z|L1],L2,[Z|L3]) :- append(L1,L2,L3).
  6903.  
  6904. printstring([]).
  6905. printstring([H|T]) :- put(H), printstring(T).
  6906.  
  6907. member(X,[X|_]).
  6908. member(X,[_|Y]) :- member(X,Y).
  6909.  
  6910.  
  6911. /*  The start module accepts a set of words, enclosed in brackets and
  6912. separated by commas.  It calls wordck to verify that each of the words is
  6913. in the vocuabulary set.   */
  6914.  
  6915.  
  6916.  
  6917. start :- batch,nl,print('INPUT'),nl,print('-----'),nl,
  6918.          nl,print('Input sentence: '),read(S),nl,
  6919.          print('The working set is ',S),wordck(S),!,
  6920.          nl,nl,print('TRANSFERS'),nl,nl,print('---------'),nl,nl,
  6921.          Parse=[],
  6922.          trans(q0,Nq,Parse,S,S1).
  6923.  
  6924.  
  6925. /*  Wordck checks for the end of the set, [], then if the word is in the
  6926. vocabulary.  If not, it asks for the catagory, and adds it to the file
  6927. WORD.TEM which is joined with the program after it has run.*/
  6928.  
  6929.  
  6930.  
  6931.  
  6932.  
  6933. wordck([])   :-   !,true.
  6934.  
  6935. wordck([H|T]) :- word(H,_,_),wordck(T).
  6936.  
  6937.  
  6938. wordck([H|T]) :-  nl,print(H,' is not a recognized word '),
  6939.                   nl,print(' enter verb,aux, .. '),read(Z),
  6940.                   wordnew(H,Z),wordck(T).
  6941.  
  6942. wordnew(W,Z) :- assertz(word(W,Z,s)),open('word.tem',ar),
  6943.                 nlf('word.tem'),
  6944.                 printf('word.tem', 'word(', W, ',', Z, ').'),
  6945.                 close('word.tem').
  6946.  
  6947.  
  6948.  
  6949.  
  6950. /*  The arcs are defined in terms of from node, to node, condition.
  6951. Terminal nodes are identified with the empty list.  Words are defined by
  6952. type word name, type, and a character to be used in later examples with the
  6953. number (plural or singular).  */
  6954.  
  6955.  
  6956. arc(q0,q1,np).
  6957. arc(q1,q2,verb).
  6958. arc(q2,q2,np).
  6959. arc(q2,q3,pp).
  6960. arc(q0,q4,aux).
  6961. arc(q4,q5,np).
  6962. arc(q1,q5,aux).
  6963. arc(q5,q2,verb).
  6964.  
  6965. term(q2,[]).
  6966. term(q3,[]).
  6967.  
  6968.  
  6969. word(boy,noun,s).
  6970. word(boys,noun,pl).
  6971. word(run,verb,pl).
  6972. word(runs,verb,s).
  6973. word(the,det,s).
  6974.  
  6975. arc(qnp,qnp1,det).
  6976. arc(qnp,qnp1,_).
  6977. arc(qnp1,qnp1,adj).
  6978. arc(qnp1,qnp2,noun).
  6979. arc(qpp,qnp,prep).
  6980.  
  6981.  
  6982. /*  Trans recursively checks the conditions for transistion from the last
  6983. node (Lq) to the next node (Nq).  Phrases are specifically treated as pp or
  6984. np in order to allow the type of phrase to be identified in the parsed
  6985. sentence.  */
  6986.  
  6987.  
  6988.  
  6989.  
  6990. trans(Lq,_,Parse,S1,_) :- term(Lq,S1),nl,
  6991.                           print('Completed ',Lq),
  6992.                           nl,print(Parse).
  6993.  
  6994.  
  6995. trans(Lq,Nq,Parse,[S0|S1],S1) :-  word(S0,Type,Nbr),
  6996.                                   arc(Lq,Nq,Type),
  6997.                                   nl,
  6998.                                   print('Transition ',Lq,' ',Nq,' ',S0,
  6999.                                             ' ',Type),
  7000.                                   append(Parse,[[Type],S0],P1),
  7001.                                   !,
  7002.                                   trans(Nq,Z,P1,S1,S2).
  7003.  
  7004.  
  7005. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,np),
  7006.                              ptrans(qnp,Nq,Lq,S0,[np],Parse).
  7007.  
  7008.  
  7009. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,pp),
  7010.                              ptrans(qpp,Nq,Lq,S0,[pp],Parse).
  7011.  
  7012.  
  7013.  
  7014. trans(Lq,Nq,Parse,S0,S1) :- !,nl,
  7015.                             print('The sentence failed at ',Lq),
  7016.                             nl,print('Parsed ',Parse),
  7017.                             nl,print('Left ',S0).
  7018.  
  7019. /*  Ptrans checks the transistion of the phrase network.  The first clause
  7020. calls itself recursively until node qnp2 has been reached, which concludes
  7021. the transistion.  Success results in trans being called  with the new node.
  7022. Failure returns the trans with conditions unchanged.  */
  7023.  
  7024.  
  7025.  
  7026. ptrans(Bq,Nq,Lq,[S0|S1],Pr,Parse) :-  word(S0,Type,Nbr),
  7027.                                 arc(Bq,Zq,Type),
  7028.                                 append(Pr,[[Type],S0],P1),
  7029.                                 !,
  7030.                                 ptrans(Zq,Nq,Lq,S1,P1,Parse).
  7031.  
  7032. ptrans(qnp2,Nq,Lq,S0,Pr,Parse) :-  nl,
  7033.                                    print('Transisiton ',Lq,' ',Nq),
  7034.                                    nl,
  7035.                                    print(Pr),
  7036.                                    append(Parse,Pr,P1),
  7037.                                    !,
  7038.                                    trans(Nq,Rq,P1,S0,S1).
  7039. /*              FINAL AUGMENTED TRANSISTION NETWORK PROGRAM
  7040.  
  7041.                                 ATNNEW1.PRO
  7042.  
  7043.                                  11/24/85
  7044. */
  7045.  
  7046.  
  7047. /*  Start is the entry into the program.  It requires that a set of
  7048. standard routines has already been consulted (append in particular).  It
  7049. allows the user to specify the network program, which can be build using
  7050. ATNBLD.  Words is a file with the vocabulary set.  The sentences is a list
  7051. of words separated by commas and enclosed in brackets.  Wordck verifies
  7052. that the words are in the vocabulary set, and if not requests required
  7053. data. Parse is the sentence as it is parsed.  Trans controls the flow from
  7054. node to node.  */
  7055.  
  7056.  
  7057. start :- nl,print('ATN network file? '),read(Fn),
  7058.          consult(Fn),nl,
  7059.          asserta(file(Fn)),
  7060.          consult(words),nl,
  7061.          batch,nl,print('INPUT'),nl,print('-----'),nl,
  7062.          nl,print('Input sentence: '),read(S),nl,
  7063.          print('The working set is ',S),wordck(S),
  7064.          nl,nl,print('TRANSFERS'),nl,nl,print('---------'),nl,nl,
  7065.          Parse=[],
  7066.          trans(q0,Nq,Parse,S,S1).
  7067.  
  7068.  
  7069.  
  7070. wordck([])   :-   true.
  7071.  
  7072. wordck([H|T]) :- H(_,_),wordck(T).
  7073.  
  7074.  
  7075. wordck([H|T]) :-  nl,print(H,' is not a recognized word '),
  7076.                   nl,print(' enter verb,aux, .. '),read(Z),
  7077.                   nl,print(' enter p or s or x  '),read(Z1),
  7078.                   wordnew(H,Z,Z1),wordck(T).
  7079.  
  7080. wordnew(W,Z,Z1) :- assertfz(words,W(Z,Z1)).
  7081.  
  7082.  
  7083. /*  Since the phrase transition network includes more specific procedures
  7084. than the primary network, it is included in this program rather than in the
  7085. network file consulted by start.  It could be more dynamic, but that was
  7086. considered beyond the scope of this project.  */
  7087.  
  7088.  
  7089. arc(qnp,qnp1,det).
  7090. arc(qnp,qnp1,[]).
  7091. arc(qnp1,qnp1,adj).
  7092. arc(qnp1,qnp2,noun).
  7093. arc(qpp,qnp,prep).
  7094.  
  7095.  
  7096. /*  Trans controls the flow along the network.  If a terminal node has been
  7097. reached and the entire sentence has been parsed, the agreement in number
  7098. (plural or singular) between the subject and predicate is checked.  If they
  7099. do not agree, this fact is displayed.  Update words creates a file
  7100. WORDS.$$$ which contains the new vocabulary.
  7101.  
  7102. If a conditions for termination has not been met, trans checks for a
  7103. transition word or a transistion phrase.  If none of these conditions are
  7104. met, the sentence will not parse.
  7105.  
  7106. When a verb is encountered the number (singular or plural) is 'filed'.
  7107. This procedure is unique for a specific network in which only one verb can
  7108. be encountered.  */
  7109.  
  7110.  
  7111. trans(Lq,_,Parse,S1,_) :- term(Lq,S1),nl,
  7112.                           print('Completed ',Lq),
  7113.                           nl,print(Parse),
  7114.                        (  ( subj(Nbr),pred(Nbr) );
  7115.                       (nl,print('The subject and predicate do not agree.')
  7116.                           ) ),
  7117.                           update(words),
  7118.                           exec('erase words.pro'),
  7119.                           exec('ren words.$$$ words.pro'),
  7120.                           forget(words),
  7121.                           file(Fn),
  7122.                           forget(Fn),
  7123.                           endclr.
  7124.  
  7125.  
  7126. endclr :-  (not(file(_));ret(file)),(not(subj(_));ret(subj)),
  7127.            (not(pred(_));ret(pred)).
  7128.  
  7129.  
  7130. trans(Lq,Nq,Parse,[S0|S1],S1) :-  S0(Type,Nbr),
  7131.                                   arc(Lq,Nq,Type),
  7132.                                  ((Type=verb,asserta(pred(Nbr)));
  7133.                                      not(type=verb)),
  7134.                                   nl,
  7135.                                   print('Transition ',Lq,' ',Nq,' ',S0,
  7136.                                             ' ',Type),
  7137.                                   append(Parse,[[Type],S0],P1),
  7138.                                   trans(Nq,Z,P1,S1,S2).
  7139.  
  7140.  
  7141. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,np),
  7142.                              ptrans(qnp,Nq,Lq,S0,[' '+np],Parse).
  7143.  
  7144.  
  7145. trans(Lq,Nq,Parse,S0,S1) :-  arc(Lq,Nq,pp),
  7146.                              ptrans(qpp,Nq,Lq,S0,[' '+pp],Parse).
  7147.  
  7148.  
  7149.  
  7150. trans(Lq,Nq,Parse,S0,S1) :- nl,
  7151.                             print('The sentence failed at ',Lq),
  7152.                             nl,print('Parsed ',Parse),
  7153.                             nl,print('Left ',S0),
  7154.                             endclr.
  7155.  
  7156.  
  7157.  
  7158. /*  Ptrans checks the transition of the phrase network.  It calls itself
  7159. recursively until node qnp2 is reached.  Provisions are included to
  7160. establish the number (plural or singular) of the subject, which is designed
  7161. for a specific network in which the noun phrase in which the subject is
  7162. located will be encountered before any other noun phrase.
  7163.  
  7164. The upon reaching qnp2 a check is made for the word 'and'.  If encountered,
  7165. the number of the subject is changed to plural and a check for another noun
  7166. phrase is initiated.
  7167.  
  7168. The spacing of the parathesis is to faciltiate reading of the code.  */
  7169.  
  7170.  
  7171. ptrans(Bq,Nq,Lq,[S0|S1],Pr,Parse) :-  S0(Type,Nbr),
  7172.                                 arc(Bq,Zq,Type),
  7173.                           (
  7174.                              (
  7175.                                  not(Type=noun);
  7176.  
  7177.                                  subj(_)
  7178.                                           );
  7179.                                  asserta(subj(Nbr))
  7180.                                                   ),
  7181.                                 append(Pr,[[Type],S0],P1),
  7182.                                 ptrans(Zq,Nq,Lq,S1,P1,Parse).
  7183.  
  7184. ptrans(Bq,Nq,Lq,S,Pr,Parse)   :-  arc(Bq,Zq,[]),
  7185.                                   ptrans(Zq,Nq,Lq,S,Pr,Parse).
  7186.  
  7187.  
  7188. ptrans(qnp2,Nq,Lq,[S0|S1],Pr,Parse) :-  S0=and,(Lq=q4;Lq=q0),
  7189.                                        ( ( subj(_),retract(subj(_)) );
  7190.                                          not(subj(_)) ),
  7191.                                          asserta(subj(p)),
  7192.                                           append(Pr,[and],P1),
  7193.                                           ptrans(qnp,Nq,Lq,S1,P1,Parse).
  7194.  
  7195.  
  7196. ptrans(qnp2,Nq,Lq,[S0|S1],Pr,Parse) :-  S0=and,
  7197.                                           append(Pr,[and],P1),
  7198.                                           ptrans(qnp,Nq,Lq,S1,P1,Parse).
  7199.  
  7200.  
  7201.  
  7202.  
  7203. ptrans(qnp2,Nq,Lq,S0,Pr,Parse) :-  nl,
  7204.                                    print('Transisiton ',Lq,' ',Nq),
  7205.                                    nl,
  7206.                                    print(Pr),
  7207.                                    append(Parse,Pr,P1),
  7208.                                    trans(Nq,Rq,P1,S0,S1).
  7209. /*                 Augmented Transition Network Program
  7210.  
  7211.                                   ATN.PRO
  7212.  
  7213.                                  10/22/85
  7214. */
  7215.  
  7216.  
  7217. /*  Standard routines for append & membership checking       */
  7218.  
  7219.  
  7220. append([],L,L).
  7221. append([Z|L1],L2,[Z|L3]) :- append(L1,L2,L3).
  7222.  
  7223. printstring([]).
  7224. printstring([H|T]) :- put(H), printstring(T).
  7225.  
  7226. member(X,[X|_]).
  7227. member(X,[_|Y]) :- member(X,Y).
  7228.  
  7229.  
  7230. /*  The start module accepts a set of words, enclosed in brackets and
  7231. separated by commas.  It calls wordck to verify that each of the words is
  7232. in the vocuabulary set.   */
  7233.  
  7234.  
  7235.  
  7236. start :- batch,nl,print('INPUT'),nl,print('-----'),nl,
  7237.          nl,print('Input sentence: '),read(S),nl,
  7238.          print('The working set is ',S),wordck(S),!,
  7239.          nl,nl,print('TRANSFERS'),nl,nl,print('---------'),nl,nl,q(0,S).
  7240.  
  7241.  
  7242.  
  7243. /*  Wordck checks for the end of the set, [], then if the word is in the
  7244. vocabulary.  If not, it asks for the catagory, and adds it to the file
  7245. WORD.TEM which is joined with the program after it has run.*/
  7246.  
  7247.  
  7248.  
  7249.  
  7250.  
  7251. wordck([])   :-   !,true.
  7252.  
  7253. wordck([H|T]) :- word(H,Y),wordck(T).
  7254.  
  7255.  
  7256. wordck([H|T]) :-  nl,print(H,' is not a recognized word '),
  7257.                   nl,print(' enter verb,aux, .. '),read(Z),
  7258.                   wordnew(H,Z),wordck(T).
  7259.  
  7260. wordnew(W,Z) :- assertz(word(W,Z)),open('word.tem',ar),
  7261.                 nlf('word.tem'),
  7262.                 printf('word.tem', 'word(', W, ',', Z, ').'),
  7263.                 close('word.tem').
  7264.  
  7265.  
  7266.  
  7267.  
  7268. /*  Trans checks the catagory of the current word (H) versus the catagory
  7269. required to make a transistion (Z).  */
  7270.  
  7271.  
  7272.  
  7273.  
  7274. trans(H,Z)   :-  word(H,X), member(X,[Z]).
  7275.  
  7276.  
  7277. qfail(Nq,S,E)  :-  !, nl,nl,print('The sentence failed at ',Nq),nl,
  7278.                  print('The sentence form to this node is ',E),nl,
  7279.                  print('The rest of the sentence is ',S),qend1.
  7280.  
  7281.  
  7282. qend(Z,E)  :-  nl,nl,print('OUTPUT'),nl,print('------'),nl,nl,
  7283.                print('The sentence is:'),nl,nl,print(E),nl,nl,
  7284.                print('The sentence is completed at node ',Z),qend1.
  7285.  
  7286.  
  7287. qend1 :- open('word.tem',ar),nlf('word.tem'),
  7288.          close('word.tem'),exec('ren atn.pro atn.sav'),
  7289.          exec('copy atn.sav+word.tem atn.pro'),
  7290.          exec('erase atn.sav'),exec('erase word.tem').
  7291.  
  7292.  
  7293. /*    Print transfer from node to node */
  7294.  
  7295.  
  7296.  
  7297. qout(A,B,C,D,E,F) :- append(E,[C,'(',A,')'],F),
  7298.                      nl, print('Transfer from node ',B,' to node ',D,
  7299.                      ' by word ',A,' evaluated as a ',C).
  7300.  
  7301.  
  7302.  
  7303.  
  7304. /*   Main program to check the conditions for transfer from node to node.
  7305.      The first number is the number of the node, i.e. q(0.. is node 0.
  7306.      The module either checks for a word type and transfers control
  7307.      directly, or passes to np / pp the next node.                    */
  7308.  
  7309.  
  7310. /*  Node 0 - aux to 4 / np to 1 / or fail          */
  7311.  
  7312.  
  7313. q(0,[H|T])  :-  trans(H,[aux]),!,qout(H,0,[aux],4,E,F), q(4,T,F).
  7314.  
  7315. q(0,[H|T])  :-  np(H,T,1,[],0,[np]).
  7316.  
  7317. q(0,S)      :-  qfail(0,S,[]).
  7318.  
  7319.  
  7320.  
  7321.  
  7322. /*  Node 1 - verb to 2 / aux to 5 / or fail    */
  7323.  
  7324.  
  7325.  
  7326. q(1,[H|T],E)  :-  trans(H,[verb]),!,qout(H,1,[verb],2,E,F), q(2,T,F).
  7327.  
  7328. q(1,[H|T],E)  :-  trans(H,[aux]),!, qout(H,1,[aux],5,E,F), q(5,T,F).
  7329.  
  7330. q(1,S,E)      :-  qfail(1,S,E).
  7331.  
  7332.  
  7333.  
  7334.  
  7335. /*  Node 2 -  null to end / np to 2 / pp to 3 / or fail     */
  7336.  
  7337.  
  7338.  
  7339. q(2,H,E)      :- member(H,[[]]), !,
  7340.                    qend(2,E).
  7341.  
  7342. q(2,[H|T],E)  :-  np(H,T,2,E,2,[np]).
  7343.  
  7344.  
  7345. q(2,[H|T],E)  :-  pp(H,T,3,E,2,[pp]).
  7346.  
  7347. q(2,S,E)      :-  qfail(2,S,E).
  7348.  
  7349.  
  7350.  
  7351.  
  7352. /*  Node 3 - null to end / or fail         */
  7353.  
  7354.  
  7355. q(3,H,E)  :-
  7356.                trans(H,[]), !,
  7357.                qend(3,E).
  7358.  
  7359. q(3,S,E)      :-  qfail(3,S,E).
  7360.  
  7361.  
  7362.  
  7363.  
  7364.  
  7365. /*  Node 4 - np to 5 / or fail           */
  7366.  
  7367.  
  7368.  
  7369.  
  7370. q(4,[H|T],E)  :-  np(H,T,5,E,4,[np]).
  7371.  
  7372. q(4,S,E)      :-  qfail(4,S,E).
  7373.  
  7374.  
  7375.  
  7376. /*  Node 5 - verb to 2 / or fail         */
  7377.  
  7378.  
  7379. q(5,[H|T],E)  :-  trans(H,[verb]),!, qout(H,5,[verb],2,E,F), q(2,T,F).
  7380.  
  7381. q(5,S,E)      :-  qfail(5,S,E).
  7382.  
  7383.  
  7384.  
  7385.  
  7386. /*  Noun phrase -  (det) (adj) (adj) .. noun        */
  7387.  
  7388. /*  The np1 clause is required to allow recursive calls for adj   */
  7389.  
  7390.  
  7391.  
  7392. np(H,[S|T],Nq,E,Lq,G)  :-  trans(H,[det]), !,
  7393.                            append(G,['det(',H,')'],G1),
  7394.                            np1([S|T],Nq,E,Lq,G1).
  7395.  
  7396.  
  7397. np(H,Z,Nq,E,Lq,G)      :-  np1([H|Z],Nq,E,Lq,G).
  7398.  
  7399.  
  7400.  
  7401. np1([H|T],Nq,E,Lq,G)  :-  trans(H,[adj]),
  7402.                           append(G,['adj(',H,')'],G1),
  7403.                           np1(T,Nq,E,Lq,G1).
  7404.  
  7405.  
  7406. np1([H|T],Nq,E,Lq,G)  :-
  7407.                           trans(H,[noun]),!,nl,
  7408.                           append(G,['noun(',H,')'],G1),
  7409.                           append(E,G1,F),
  7410.                           print('Transfer from node ',Lq,' to ',Nq),
  7411.                           print(' by ',G1),q(Nq,T,F).
  7412.  
  7413.  
  7414.  
  7415.  
  7416. /*  Prep phrase requires a prep followed by a np   */
  7417.  
  7418.  
  7419.  
  7420.  
  7421. pp(H,[S|T],Nq,E,Lq,G)  :-  trans(H,[prep]),
  7422.                            append(['prep(',H,')'],G,G1),
  7423.                            np(S,T,Nq,E,Lq,G1).
  7424.  
  7425.  
  7426.  
  7427.  
  7428.  
  7429. /*   Word defines the vocabulary set                  */
  7430.  
  7431.  
  7432. word(the,[det]).
  7433. word(boy,[noun]).
  7434. word(runs,[verb]).
  7435. word(happy,[adj]).
  7436. word(john,[noun]).
  7437. word(can,[aux]).
  7438. word(run,[verb]).
  7439. word(a,[det]).
  7440. word(big,[adj]).
  7441. word(small,[adj]).
  7442. word(girl,[noun]).
  7443. word(dog,[noun]).
  7444. word(on,[prep]).
  7445. word(pretty,[adj]).
  7446. word(fast,[adj]).
  7447. word(barks,[verb]).
  7448. word(to,[prep]).
  7449. word([],[]).
  7450.  
  7451. word(giant, [noun]).
  7452. word(is, [verb]).
  7453.  
  7454. word(giant, [noun]).
  7455. word(is, [verb]).
  7456. word(sleeps, [verb]).
  7457.  
  7458. word(mary, [noun]).
  7459. word(likes, [verb]).
  7460.  
  7461.  
  7462.  
  7463.  
  7464. word(fly, [verb]).
  7465.  
  7466.  
  7467. word(rides, [verb]).
  7468. word(large, [adj]).
  7469. word(bike, [noun]).
  7470. word(store, [noun]).
  7471.  
  7472.  
  7473.  
  7474. word(gull, [noun]).
  7475. word(green, [adj]).
  7476.  
  7477. word(plane, [noun]).
  7478. word(silver, [adj]).