home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / readme < prev    next >
Encoding:
Text File  |  2000-09-21  |  9.7 KB  |  273 lines

  1. ______________________________________________________________________________
  2. Mini Prolog Version 1.5g             A simple Prolog interpreter, for Hugs 1.3
  3.  
  4.      Mark P. Jones, 23rd July 1991, updated for Hugs 1.3, June 1996.
  5. ______________________________________________________________________________
  6.  
  7. This document gives a brief introduction to Mini Prolog Version 1.5g, a simple
  8. Prolog interpreter that can be used with Hugs 1.3.  The original version of
  9. this program was written nearly two years ago as an Orwell program.  It has
  10. been through many minor changes since then, modified to run under Haskell B,
  11. then Gofer, and now Hugs.
  12.  
  13. This document isn't going to explain a  lot  about  how  Prolog  programs  are
  14. written and work.  But there are plenty of other references for that.   Please
  15. feel free to contact me with any questions or suggestions.  I'd very much like
  16. to receive any comments.
  17.  
  18. mpj@cs.nott.ac.uk
  19. ______________________________________________________________________________
  20.  
  21.                            GETTING STARTED
  22.  
  23. The  Mini Prolog interpreter  takes the form of a small  collection  of Hugs
  24. scripts.  The most important part of  any  implementation  of  Prolog  is  the
  25. inference engine  which  controls  the  search  for  goals  to  user  supplied
  26. queries.  Mini Prolog comes with a choice of two different inference  engines,
  27. the `pure' engine uses lazy evaluation to construct and  traverse  potentially
  28. infinite proof trees.  The `stack' engine uses an explicit stack  (implemented
  29. using a list) to provide a more concrete  description  of  backtracking.   The
  30. stack engine also implements  the  Prolog  cut  `!'  predicate,  used  in  the
  31. examples below.  Assuming that you've got everything set up  properly  to  use
  32. the Hugs interpreter, and that all of the Mini Prolog script files are in the
  33. current working directory, you should start Hugs with the command `hugs':
  34.  
  35.   C:\HUGS\DEMOS\PROLOG>hugs
  36.         ___    ___   ___    ___   __________   __________
  37.        /  /   /  /  /  /   /  /  /  _______/  /  _______/   The Haskell User's
  38.       /  /___/  /  /  /   /  /  /  / _____   /  /______        Gofer System
  39.      /  ____   /  /  /   /  /  /  / /_   /  /______   /
  40.     /  /   /  /  /  /___/  /  /  /___/  /  _______/  /          Version 1.3
  41.    /__/   /__/  /_________/  /_________/  /_________/          Release alpha
  42.  
  43.        Copyright (c) Mark P Jones, The University of Nottingham, 1994-1996.
  44.  
  45.   Reading script file "\Hugs\Lib\hugs.prelude":
  46.  
  47.   Hugs session for:
  48.   \Hugs\Lib\hugs.prelude
  49.   Type :? for help
  50.   ?
  51.  
  52. and then load the files for the Mini Prolog system:
  53.  
  54.   ? :l Main
  55.  
  56. Once the script files have been loaded,  start the Mini prolog interpreter by
  57. typing the expression `main' and pressing return.
  58.  
  59.   ? main
  60.   Mini Prolog Version 1.5g (stack based)
  61.  
  62.   Reading stdlib........done
  63.   > 
  64.  
  65. The `>' prompt indicates that the interpreter is running and waiting for  user
  66. input.
  67.  
  68.                         STANDARD PREDICATES
  69.  
  70. Before the `>' prompt appears, Mini Prolog reads a set of  standard  predicate
  71. definitions from the file `stdlib' in the current directory.  You are free  to
  72. modify this file to suit your own needs.  The only predicate that is built  in
  73. to Mini Prolog is the cut, written `!' whose use is demonstrated below.  There
  74. are no other  extralogical  predicates,  no  input/output  predicates  and  no
  75. arithmetic as found in full implementations of Prolog.  Some of these features
  76. could be added to the interpreter without too much  difficulty,  others  would
  77. require rather more work.
  78.  
  79. At any time, you can ask the interpreter to display the list of rules that are
  80. being held in the database by typing "??" and pressing the  return  key.   Try
  81. this after you've started the  interpreter  and  you'll  get  a  list  of  the
  82. predicates defined in the file `stdlib'.  For example:
  83.  
  84.   > ??
  85.   append(nil,X,X).
  86.   append(cons(X,Y),Z,cons(X,W)):-append(Y,Z,W).
  87.  
  88.   equals(X,X).
  89.  
  90.   not(X):-X,!,false.
  91.   not(X).
  92.  
  93.   or(X,Y):-X.
  94.   or(X,Y):-Y.
  95.  
  96.   true.
  97.   >
  98.  
  99.                           THE APPEND PREDICATE
  100.  
  101. The Mini Prolog interpreter does not support the standard  Prolog  syntax  for
  102. lists.    Instead,   you   have    to    write    the    list    [1,2,3]    as
  103. "cons(1,cons(2,cons(3,nil)))".  One of the first things I tried was  appending
  104. two simple lists:
  105.  
  106.   > ?- append(cons(1,nil),cons(2,nil),X)
  107.   X = cons(1,cons(2,nil)) ;
  108.   no.
  109.   >
  110.  
  111. Given a query, Mini Prolog attempts to find values for each of  the  variables
  112. (beginning with a capital letter) in the query.  Here Mini  Prolog  has  found
  113. that X = cons(1,cons(2,nil)) is a solution to the query.   When  I  press  the
  114. semicolon key, ";", it tries to find another solution, but fails and  displays
  115. the message "no.".
  116.  
  117. What amazed me when I first started experimenting with Prolog was that I could
  118. actually ask Mini Prolog to work through the problem in reverse, asking  which
  119. lists could be appended to get the list cons(1,cons(2,nil)):
  120.  
  121.   > ?- append(X,Y,cons(1,cons(2,nil)))
  122.   X = nil
  123.   Y = cons(1,cons(2,nil)) ;
  124.   X = cons(1,nil)
  125.   Y = cons(2,nil) ;
  126.   X = cons(1,cons(2,nil))
  127.   Y = nil ;
  128.   no.
  129.   >
  130.  
  131. Note that the interpreter pauses after displaying each solution and waits  for
  132. a key to be  pressed.  Pressing `;' tells Mini Prolog  to continue looking for
  133. another solution, displaying `no' if no more solutions can be found.  Pressing
  134. any other key stops the execution of the query.  If there are no variables  in
  135. the original query, then the interpreter simply outputs `yes' if the query can
  136. be proved and otherwise prints `no':
  137.  
  138.   > ?- append(cons(1,nil),cons(2,nil),cons(1,cons(2,nil)))
  139.   yes.
  140.   > ?- append(cons(1,nil),cons(2,nil),cons(1,cons(3,nil)))
  141.   no.
  142.   >
  143.  
  144. Unfortunately, typing a control C to interrupt a query with an  infinite  loop
  145. will exit the Prolog interpreter completely -- sorry, but I don't know  a  way
  146. around this at the moment.
  147.  
  148.  
  149.                        RUNNING IN THE FAMILY
  150.  
  151. You don't have to stick with the standard predicates that are already included
  152. in Mini Prolog.  Additional rules can be typed in at the ">" prompt.  Here are
  153. a couple of examples based around the idea of family trees:
  154.  
  155.   > parent(Child,Parent):-father(Child,Parent).
  156.   > parent(Child,Parent):-mother(Child,Parent).
  157.   > grandparent(GChild,Gparent):-parent(GChild,Parent),parent(Parent,Gparent).
  158.   >
  159.  
  160. Note that  Mini Prolog  expects  a maximum of one rule per line,  and will not
  161. allow predicate definitions to be spread out over a number of lines.
  162.  
  163. All you have to do now is enter some details about your family  and  then  you
  164. can ask who your grandparents are ... let's take a typical family:
  165.  
  166.   > father(charles,princePhilip).
  167.   > mother(charles,theQueen).
  168.   > father(anne,princePhilip).
  169.   > mother(anne,theQueen).
  170.   > father(andrew,princePhilip).
  171.   > mother(andrew,theQueen).
  172.   > father(edward,princePhilip).
  173.   > mother(edward,theQueen).
  174.   > mother(theQueen,theQueenMother).
  175.   > father(william,charles).
  176.   > mother(william,diana).
  177.   > father(harry,charles).
  178.   > mother(harry,diana).
  179.   >
  180.  
  181. And  now  we  can  ask  some  questions;  like  who  are  the  Queen  mother's
  182. grandchildren ?
  183.  
  184.   > ?- grandparent(X,theQueenMother)
  185.   X = charles ;
  186.   X = anne ;
  187.   X = andrew ;
  188.   X = edward ;
  189.   no.
  190.   >
  191.  
  192. or, who are Harry's grandparents ?
  193.  
  194.   > ?- grandparent(harry,Who)
  195.   Who = princePhilip ;
  196.   Who = theQueen ;
  197.   no.
  198.   >
  199.  
  200. Note that Mini Prolog can only use the facts it has been  given.   Tell  it  a
  201. little more about Diana's parents and you'll find it knows more about  Harry's
  202. grandparents.
  203.  
  204. Now suppose we define a sibling relation:
  205.  
  206.   > sibling(One,Tother) :- parent(One,X),parent(Tother,X).
  207.   >
  208.  
  209. Fine.  It all looks quite correct.  But when you try to find Harry's siblings,
  210. you get:
  211.  
  212.   > ?- sibling(harry,Who)
  213.   Who = william ;
  214.   Who = harry ;
  215.   Who = william ;
  216.   Who = harry ;
  217.   no.
  218.   >
  219.  
  220. Each of William and Harry  appears  twice  in  the  above.   Once  by  putting
  221. X=charles and once using X=diana in the definition of sibling above.   We  can
  222. use the cut predicate to make sure that we look for at most one parent:
  223.  
  224.   > newsib(One,Tother) :- parent(One,X),!,parent(Tother,X).
  225.   >
  226.   > ?- newsib(harry,Who)
  227.   Who = william ;
  228.   Who = harry ;
  229.   no.
  230.   >
  231.  
  232. Thats better, but we don't really want to list Harry as his  own  sibling,  so
  233. we'll add a further restriction:
  234.  
  235.   > newsib1(O,T):-parent(O,X),!,parent(T,X),not(equals(O,T)).
  236.   >
  237.   > ?- newsib1(harry,Who)
  238.   Who = william ;
  239.   no.
  240.   >
  241.  
  242. Thats just about perfect.  You might like to play with  some  other  examples,
  243. enlarge the family tree, work out suitable predicates for other relations (who
  244. are Harry's aunts ?) etc.  Initially, the answers that Mini Prolog gives  will
  245. all be pretty obvious to you.  Try getting involved in a  larger  family  tree
  246. and more complicated relations and you'll find it's not so easy.
  247.  
  248.                                   GOODBYES
  249.  
  250. I could go on with more examples, but I guess you've got the  picture  by  now
  251. ... at least I hope so !  I suppose I should just tell you how to get  out  of
  252. Mini Prolog (ok. ^C works but its not exactly elegant).  Just type  "bye"  (or
  253. "quit") and you're out.  Be warned though: when you leave Mini Prolog, it will
  254. not retain any new rules that you've entered, so  you'll  have  to  find  some
  255. other way to save them (I usually type  "??"  to  list  the  rules  that  I've
  256. entered and use the mouse to paste them into an editor in another window,  but
  257. that obviously requires you to be using a workstation at the time).
  258.  
  259.   > bye
  260.   Thank you and goodbye
  261.  
  262.   (12749 reductions, 1256 cells)
  263.   ?
  264.  
  265. The `?' prompt  tells you that you are now back in Hugs,  and you can restart
  266. Mini Prolog as before,  carry on with some other work in Hugs,  or use the :q
  267. command to exit Hugs and return to the operating system.
  268.  
  269. I hope you have fun with Mini Prolog;  please tell me if you have any comments
  270. you'd like to make.
  271.  
  272. ______________________________________________________________________________
  273.