home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / pop / 58 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  6.5 KB

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!sdd.hp.com!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!math.fu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!uknet!news.cs.bham.ac.uk!axs
  2. From: axs@cs.bham.ac.uk (Aaron Sloman)
  3. Newsgroups: comp.lang.pop
  4. Subject: Re: A little Pop history
  5. Summary: illustrating pattern matching in Pop-11
  6. Message-ID: <BxsD3E.31C@cs.bham.ac.uk>
  7. Date: 16 Nov 92 01:50:01 GMT
  8. References: <Bxpqvt.Lq@deshaw.com> <1e3bt3INNjc9@agate.berkeley.edu> <BxrLG6.2v4@deshaw.com> <1e6euqINN104@agate.berkeley.edu>
  9. Sender: news@cs.bham.ac.uk
  10. Organization: School of Computer Science, University of Birmingham, UK
  11. Lines: 204
  12. Nntp-Posting-Host: emotsun
  13.  
  14. bh@anarres.CS.Berkeley.EDU (Brian Harvey) writes:
  15.  
  16. > Date: 15 Nov 92 21:20:26 GMT
  17. > Organization: University of California, Berkeley
  18. >
  19. > Of course, these examples are entirely equivalent to what could be done
  20. > in Lisp.
  21.  
  22. Agreed, or in Prolog
  23.  
  24. > I think you should post examples using some of the more unusual
  25. > POP features, such as pattern matching.
  26.  
  27. I am tempted. Hope it's not too long.
  28.  
  29. The Pop-11 pattern matchers (there are two now) are unlike
  30. prolog unification in that they go only one way: i.e. you can have
  31. variables only on the right: so you can't match two variables and
  32. have a later binding of one of them propagate to the other). However
  33. the Pop-11 matchers compensate for this (some would say more than
  34. compensate, others would say less than compensate) by allowing
  35. segment variables in the middle of a list, plus restriction
  36. procedures.
  37.  
  38. Here's an example of the use of forevery, which uses the
  39. (original) Pop-11 matcher;
  40.  
  41.     vars x, y, z;
  42.  
  43.     vars properties =
  44.         [[g is h][e is f] [d is e] [b is c] [a is b] [h is i]];
  45.  
  46.     forevery [[?x is ?y][?y is ?z]] in properties do
  47.         ;;; extend the list of properties
  48.         [[^x is ^z] ^^properties] -> properties;
  49.     endforevery;
  50.  
  51.     properties ==>
  52.     ** [[a is c]
  53.         [d is f]
  54.         [g is i]
  55.         [g is h]
  56.         [e is f]
  57.         [d is e]
  58.         [b is c]
  59.         [a is b]
  60.         [h is i]]
  61.  
  62.  
  63. Using the matcher you can define a very simple parser. In what
  64. follows ?<var> matches a single item in a list ??<var> matches
  65. an arbitrarily long segment of a list. If the <var> is followed
  66. by a colon, then the next item is taken to refer to a restriction
  67. predicate, as in ?word:noun. If a match is successful and the
  68. restriction predicate returns a non-boolean result, then the
  69. result is bound to the variable.
  70.  
  71. ;;; first define recognisers for lexical categories
  72. define recognise(word, category, list) -> result;
  73.     lvars word, category, list, result;
  74.     if member(word, list) then
  75.         [^category ^word] -> result;
  76.     else
  77.         false -> result
  78.     endif;
  79. enddefine;;
  80.  
  81. ;;; define particular recognisers by partially applying recognise
  82.  
  83. define noun =
  84.     recognise(%"noun", [cat dog mouse elephant man mat tree owl]%)
  85. enddefine;
  86.  
  87. define det =
  88.     ;;; recogniser for determiners
  89.     recognise(%"det", [each all every a the some]%)
  90. enddefine;
  91.  
  92. define adj =
  93.     recognise(%"adj", [happy hungry big rich yellow striped]%)
  94. enddefine;
  95.  
  96. define verb =
  97.     recognise(%"verb", [ate struck chased liked stroked tickled]%)
  98. enddefine;
  99.  
  100. ;;; a utility to check that all elements of a list satisfy a predicate
  101. define all_are(list, pred) -> result;
  102.     lvars list, pred, result = false, item;
  103.     for item in list do
  104.         returnunless(pred(item))
  105.     endfor;
  106.     true -> result;
  107. enddefine;
  108.  
  109. ;;; now recogniser/parsers for non-terminals
  110.  
  111. define np(list) -> result;
  112.     ;;; parse noun phrases
  113.     lvars list,result;
  114.  
  115.     vars word1,word2,word3,list1;   ;;; pattern variables
  116.  
  117.     if list matches [?word1:det ?word2:noun] then
  118.         [np ^word1 ^word2]
  119.     elseif list matches [?word1:det ?word2:adj ?word3:noun] then
  120.         [np ^word1 ^word2 ^word3]
  121.     elseif
  122.         list matches [?word1:det ??list1: ^(all_are(%adj%)) ?word2:noun]
  123.     then
  124.         [np ^word1 [adjs ^list1] ^word2]
  125.     else
  126.         false
  127.     endif -> result
  128. enddefine;
  129.  
  130. define vp(list) -> result;
  131.     ;;; parse verb phrases
  132.     lvars list, result;
  133.     vars word list2;
  134.     if list matches [?word:verb ??list2:np] then
  135.         [vp ^word ^list2]
  136.     else
  137.         false
  138.     endif -> result;
  139. enddefine;
  140.  
  141. define sentence(list) -> result;
  142.     lvars list,result;
  143.     vars list1, list2;
  144.     if list matches [??list1:np ??list2:vp] then
  145.         [sentence ^list1 ^list2]
  146.     else
  147.         false
  148.     endif -> result
  149. enddefine;
  150.  
  151. ;;; now for a bit of magic
  152.  
  153. np([the dog]) =>
  154. ** [np [det the] [noun dog]]
  155.  
  156. np([the big dog]) =>
  157. ** [np [det the] [adj big] [noun dog]]
  158.  
  159. np([each hungry yellow cat]) =>
  160. ** [np [det each] [adjs [hungry yellow]] [noun cat]]
  161.  
  162. vp([chased the cat]) =>
  163. ** [vp [verb chased] [np [det the] [noun cat]]]
  164.  
  165. sentence([the dog chased the rich yellow cat]) ==>
  166. ** [sentence [np [det the] [noun dog]]
  167.              [vp [verb chased]
  168.                  [np [det the] [adjs [rich yellow]] [noun cat]]]]
  169.  
  170. sentence([every rich happy elephant tickled some
  171.             big striped yellow mouse]) ==>
  172. ** [sentence [np [det every] [adjs [rich happy]] [noun elephant]]
  173.              [vp [verb tickled]
  174.                  [np [det some]
  175.                      [adjs [big striped yellow]]
  176.                      [noun mouse]]]]
  177.  
  178. ;;; or with a more pictorial display
  179. lib showtree
  180.  
  181. showtree(sentence([the big cat liked the striped mat]));
  182.  
  183.                 /--------\
  184.                 |sentence|
  185.                 \---+----/
  186.          /----------+----------\
  187.         /+-\                  /+-\
  188.         |np|                  |vp|
  189.         \+-/                  \+-/
  190.   /------+------\       /------+------\
  191. /-+-\  /-+-\  /-+--\  /-+--\         /+-\
  192. |det|  |adj|  |noun|  |verb|         |np|
  193. \-+-/  \-+-/  \-+--/  \-+--/         \+-/
  194.   |      |      |       |      /------+------\
  195.   |      |      |       |    /-+-\  /-+-\  /-+--\
  196.  the    big    cat    liked  |det|  |adj|  |noun|
  197.                              \-+-/  \-+-/  \-+--/
  198.                                |      |      |
  199.                                |      |      |
  200.                               the  striped  mat
  201.  
  202. Actually it looks nicer on a terminal with graphics, but that's
  203. not for a news posting!
  204.  
  205. Of course, there are better ways to write parsers, but this
  206. illustrates the matcher.
  207.  
  208. (The parsing example was based on one of Poplog Pop-11's online
  209. "teach" files, most of which contain examples that can be run at the
  210. terminal.)
  211.  
  212. Aaron
  213. -- 
  214. Aaron Sloman, School of Computer Science,
  215. The University of Birmingham, B15 2TT, England
  216. EMAIL   A.Sloman@cs.bham.ac.uk  OR A.Sloman@bham.ac.uk
  217. Phone: +44-(0)21-414-3711       Fax:   +44-(0)21-414-4281
  218.