[To reply replace "Aaron.Sloman.XX" with "A.Sloman"]
Oliver J Glass <ug60ojg@cs.bham.ac.uk> writes:
> Date: Thu, 09 Mar 2000 11:35:22 +0000
> Also, some POP11 features like using the ! when pattern matching just
> don't seem to work in the Windows version.
This pattern prefix is an extension to Pop-11 in the Birmingham
local library. It can be fetched from the FreePoplog directory
http://www.cs.bham.ac.uk/research/poplog/freepoplog.html
and is included in the bhamteach.tar.gz file which you may already
have in the Birmingham linux CD. For more information on fetching
and installing the files, see below.
For people outside Birmingham, here's an explanation and a proposal:
The prefix works as follows. If the symbol "!" precedes a list
expression containing pattern variables, like this:
![appointment ?person [date ??date] ??info]
then every pattern variable, i.e. every word following "?" or "??"
is replaced by the corresponding identifier during compilation of
that list expression. This means that if the word has been declared
as lexically scoped using "lvars" (e.g. lvars person, date, info;)
then the pattern will refer to that lexically scoped variable.
So you can write things like
define show_diary();
lvars person, date, info;
foreach ![appointment ?person [date ??date] ??info] do
[You are seeing ^person on ^^date about ^^info] =>
endforeach;
enddefine;
Without the "!" that will not work because pattern variables are
treated as global variables and you would have to declare them
globally and use "dlocal" (or "vars") in the procedure to localise
them.
Even that will not *fully* localise them because dynamic local
variables can interact badly with global variables used in
procedures invoked within the dynamic context. This can lead to all
sorts of obscure bugs when using the pattern matcher.
For more on this see TEACH VARS_AND_LVARS, available online at
Birmingham and at
http://www.cs.bham.ac.uk/research/poplog/teach/vars_and_lvars
Some programmers refuse to use the pattern matcher because of this
problem, but I find it is far too useful, especially in teaching,
but also in many real programs.
For that reason I introduced "!" as an autoloadable pattern prefix
available to all our users here in Birmingham.
It overcomes the above problem, as it allows lvars variables to be
pattern variables. (It also allows sectionised global vars variables to
be used in patterns: see HELP SECTIONS)
This was not possible until Poplog version 15, when "valof" (which is
used by the matcher) was generalised to work with identifiers. (To learn
all the gory details regarding the differences between words and
identifiers, see REF WORDS, REF IDENT)
I believe this removes the major objection to the pattern matcher as
a serious programming tool. Hence my proposal:
PROPOSAL: "!" as pattern prefix turning words into identifiers
should become a standard part of the Pop-11 language.
Possible objection: some people may already be using "!"
for other purposes.
Reply:
(a) I have not yet met anyone who does.
(b) Such people will simply not be able to use "!" as a
pattern prefix, but they can define something else to perform
that role by slightly modifying my readpattern.p file
(see below).
NOTE: The Poprulebase production system language uses the same
technique to ensure that all pattern variables are lexically scoped.
Earlier versions (e.g. LIB NEWPSYS) were very hard to use in complex
programs because all pattern variables were essentially global.
Backtracking matcher:
There is another, less important (in my view), objection to the
pop-11 pattern matcher: namely that there are contexts where segment
pattern elements, i.e. using "??" or "==", require a *backtracking*
pattern matcher in order to find matches where the same segment
variable is repeated in different sublists, or where it is necessary
to try different ways of partitioning sublists to find a match.
Without that the matcher fails to find the match in this sort of
case:
vars common;
[[ 1 2 a 3] [ 11 a 12]] matches [[== ?common ==] [== ?common ==]] =>
** <false>
(common should have been bound to "a")
Compare this match, which succeeds:
[1 2 a 3 11 a 12] matches [== ?common == ?common ==] =>
** <true>
This example from Jeff Dalton in Edinburgh also causes the matcher
to fail:
vars x,y; [[1 2 3 4 5] 1 2] matches [[??x ??y] ??x] =>
** <false>
(x should have been bound to [1 2]).
and here too:
[[1 2 3 4][3 4 1 2]] matches [[??x ??y][??y ??x]] =>
** <false>
Finding those matches requires backtracking across different ways of
partitioning sub-lists.
The standard Pop-11 matcher does not do backtracking because of the
cost of saving all the choice points (continuations), though there is a
library program (fmatches) using techniques devised by Jonathan
Cunningham, which is distributed with Poplog. It does backtrack but is
too restricted in other ways. E.g. you can't use it as a prefix
to a pattern which is passed to another procedure as a parameter,
e.g. a procedure like present, or remove.
There is another Birmingham library (LIB doesmatch) which does the
backtracking and uses a clever technique devised by John Gibson
for saving continuations on the Pop-11 user stack. This means that
although it has an extra cost in simple cases, in some uses of
patterns with "??" or "==" it can be far less expensive in garbage
collection overhead than the standard pattern matcher!
These extensions are all available at from the Free Poplog directory.
They are in the auto/ lib/ help/ and teach/ files there, but also
packaged into this tar file:
http://www.cs.bham.ac.uk/research/poplog/pattern.tar.gz
or
ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/pattern.tar.gz
This contains the following files:
-rw-r--r-- 1807/55 12846 Jan 18 18:36 1997 lib/readpattern.p
This should go into $poplocal/local/lib
or if you don't want to setup a separate local directory, into
$usepop/pop/lib/lib/
it defines the prefix "!" for using pattern variables as
lvars.
-rw-r--r-- 1807/55 285 Sep 23 23:01 1996 auto/!.p
This autoloadable file simply lcauses LIB readpattern to be
compiled when you first make use of "!" as a pattern prefix.
This file should go into $poplocal/local/auto
where it will be autoloadable, or if you don't want to set
up a separate local directory, put it in
$usepop/pop/lib/auto
I don't know if windows allows file names containing "!".
If not simply put into your vedinit.p file
uses readpattern;
-rw-r--r-- 1807/55 24508 Oct 14 08:47 1996 help/readpattern
This help file explains readpattern in detail with a lot of
examples. It can be put in
$poplocal/local/help or $usepop/pop/help
-rw-r--r-- 1807/55 22009 Oct 14 08:38 1996 teach/matches
This is a revised, improved, version of the standard tutorial
file TEACH MATCHES, including the use of "!" as pattern prefix.
These optional extras are also included in the tar file, to provide
the backtracking matcher:
rw-r--r-- 1807/55 17953 Dec 29 17:51 1995 auto/doesmatch.p
Defines an operator doesmatch, which can be used with "!", and
provides the backtracking capability missing from matches, and
also supports "where" conditions to filter the matches proposed.
-rw-r--r-- 1807/55 1604 Dec 27 23:57 1995 lib/newmatches.p
This redefines matches, sysmatch and --> to use the "doesmatch"
backtracking capability so that all matches are found.
-rw-r--r-- 1807/55 2631 Dec 5 17:25 1995 auto/while_matching.p
Defines a new looping construct based on doesmatch, which can be
used in two forms
while_matching <list> with !<pattern> do <action>
endwhile_matching
while_matching <list> with !<pattern> when <condition> do <action>
endwhile_matching
-rw-r--r-- 1807/90 26597 Jan 10 13:11 1998 help/doesmatch
This gives documentation on doesmatch, newmatches, and
while_matching, with examples showing how the ordinary
matcher fails and the new one succeeds. Includes discussion
of efficiency issues.
NOTE: if you have Poplog version 15.5 or later, there is a powerful new
matcher described in HELP EQUAL (and in HELP NEWS: See the Jan 12 entry
by John Gibson). Eventually I expect to switch to using it, though
rewriting all the libraries and documentation will be a huge amount
of work as it uses a different syntax.
> I've been able to work around
> all the missing features I've found, but this is making my code long
> winded and inelegant. Are there any patches that will bring winpop11 up
> to the unix version's standard?
You can install the files referred to above to make the matcher work
as you are used to.
Aaron
==
--
Aaron Sloman, ( http://www.cs.bham.ac.uk/~axs/ )
School of Computer Science, The University of Birmingham, B15 2TT, UK
EMAIL A.Sloman AT cs.bham.ac.uk (NB: Anti Spam address)
TOOLS: http://www.cs.bham.ac.uk/research/poplog/freepoplog.html
|