1a2,3
>                                                         Revised Nov 2011
>                                              (With help from Will Price)
5,9c7,13
< This file introduces the use of the Pop-11 pattern matcher. In Sept 1996
< it was extended to include the use of the pattern prefix "!" which
< allows matcher variables to be lvars. Readers who are familiar with
< pattern matching will find a more complete and concise summary in
< HELP * MATCHES, but not including the use of "!" described below.
---
> This file introduces the use of the Pop-11 pattern matcher.
> Readers who are familiar with pattern matching will find a more complete
> and concise summary in HELP * MATCHES.
> 
> (Note for experts:
> In Sept 1996 this file was extended to include the use of the pattern
> prefix "!" which allows matcher variables to be lexically scoped.)
17a22,23
>  -- Using the matcher in conditions: matching expressions
>  -- A pattern includes one or more pattern elements
19c25,26
<  -- Patterns are lists used as templates for matching
---
>  -- A pattern is a list used as a template for matching
>  -- Pattern elements with a memory
22a30
>  -- Segment pattern elements
41,45c49,54
< mentioning all of its capabilities, since that would be too confusing.
< At the end of this file there are pointers to additional things to read,
< giving information about the more sophisticated facilities. If you look
< at TEACH RESPOND after this you will see how to use the MATCHER to write
< an Eliza-like program.
---
> mentioning all of its capabilities, since that would be too confusing at
> this stage. At the end of this file there are pointers to additional
> things to read, giving information about the more sophisticated
> facilities, and some uses of the matcher. If you look at TEACH * RESPOND
> after this you will see how to use the MATCHER to write a simple chatbot
> program.
48,53c57,144
< The pattern matcher, and other procedures defined in terms of it, can be
< used for checking whether some information is present in a list or list
< of lists, and for extracting components from memory stores. It can also
< be used for comparing a sentence typed in by the user with various
< templates and choosing an action depending on which template the input
< matches.
---
> Solving problems can then require programs to examine various lists to
> find information that is relevant to the problem and then make use of
> that information. Writing programs to dig into structures of varying
> complexity to find information can be tedious and error prone. Use of
> the matcher simplifies many commonly occurring cases. However it is not
> claimed that this is only way to store, find and reuse information in AI
> systems. For example use of neural nets is another. There is no single
> right way to do it (even brains seem to use varied ways of storing and
> using information).
> 
> The pattern matcher described below, and other procedures defined in
> terms of it, can be used for checking whether some information is
> present in a list or list of lists, and also for extracting components
> from memory stores. It can also be used for comparing a sentence typed
> in by the user with various templates and choosing an action depending
> on which template the input matches. This file introduces those
> mechanisms and some additional syntax for easily constructing new
> information structures on the basis of what was previously found.
> 
> -- Using the matcher in conditions: matching expressions --------------
> 
> Checking whether some required information is in a stored structure can
> take many forms, but the basic form we explain in this tutorial is use
> of an expression in which the operator "matches" is used to compare two
> items, a "datum" and a "pattern". This is a matching expression:
> 
>     <datum> matches <pattern>
> 
> A matching expression, when run by pop11, will produce a boolean (true
> or false) result, so that it can be used as a test in conditional
> expressions, such as
> 
>     if <datum> matches <pattern> then <perform some actions> endif
> 
> The use of patterns allows many complex tests to be collapsed into a
> simple form, producing shorter, clearer, programs, that are easier to
> write than more common alternatives, as will be demonstrated below.
> 
> -- A pattern includes one or more pattern elements -----------
> 
> The <datum> can be any list, including a list of lists.
> 
> The <pattern> can also be a list, but in addition to the sorts of items
> normally found in lists it can contain "pattern elements" of various
> sorts. These pattern elements control how the various items in the datum
> are grouped and compared with items in the <pattern>. In addition to
> pattern elements a list may include constant items, e.g. numbers, words,
> strings, vectors, procedures, and lists (among others).
> 
> The pattern expression [the = is ?x on == and ??y] contains four
> constants, the words "the", "is", "on", "and", and also four pattern
> elements "=", "==", "?x" and "??y", whose uses will be explained below.
> If there are no pattern elements in the pattern, then the comparison is
> the same as that done by the pop11 equality operator "=". I.e.
> 
>     [a b c] matches [a b c]
> 
> has the same effect as
> 
>     [a b c] = [a b c]
> 
> [Note: later you will meet a stronger equality operator "==" which
> requires strict identity rather than similarity of structure and
> contents. These two uses of "=" and "==" as infix operators and as
> pattern elements are quite distinct. The context should make clear which
> is being used.]
> 
> So a pattern containing only constants, without any pattern elements,
> can be thought of as a "limiting case" of a pattern.
> 
> When a matching expressing uses a pattern containing only constants,
> it produces a boolean (true or false) result. But when the pattern
> contains pattern elements there can also be important side effects. In
> particular, the matching process can discover and remember components of
> the datum, for use in subsequent actions.
> 
> This "data extraction" capability, combined with the economy of
> expression mentioned above, makes writing some of the kinds of programs
> required for intelligent systems much faster and easier than use of a
> conventional programming language. Examples will be given below, and in
> many other teach files. The Pop11 matcher is at the heart of the
> Poprulebase system and the SimAgent toolkit, which are powerful
> extensions to the core of pop11.
> 
> There is a more general and more powerful version of the matcher which
> was added much later by John Gibson. It is described in HELP EQUAL and
> may be used by advanced programmers. However, it would be a major job to
> rewrite all the libraries and documentation to use the new version.
56a148,149
> The <datum> in a matching expression is a list.
> 
58a152
> 
68a163,166
> This is a list containing a number, a word and a string
> 
>     [ 99 hundred 'a hundred and one']
> 
90c188,190
< -- Patterns are lists used as templates for matching ------------------
---
> -- A pattern is a list used as a template for matching --------------
> 
> In a matching expression
92,93c192,200
< A pattern is itself a list, which may contain 'pattern elements'. An
< example of a pattern is:
---
>     <datum> matches <pattern>
> 
> The <pattern>, like the <datum> is a list. But that list may contain not
> only Pop11 items such as words, numbers and lists, but also special
> items called 'pattern elements'. These are components of the pattern
> that are matched against individual items or groups of items in the
> datum.
> 
> An example of a pattern is:
100c207
<     [the oldest person in the romm is a very happy woman]
---
>     [the oldest person in the room is a very happy woman]
107c214
< The pattern element "==" will "match" any sequence of words in a list, a
---
> The pattern element "==" will "match" any sequence of items in a list, a
110c217
< subsequence of list elements, even an empty subsequence, so that these
---
> sub-sequence of list elements, even an empty sub-sequence, so that these
116c223,259
< The normal format for using the matcher is:
---
> -- Pattern elements with a memory -------------------------------------
> 
> The pattern element "==" does not remember what it matched. It is
> therefore a memory-less pattern element. However when we want a matching
> expression not only to produce a true or false result, but also to
> remember what was found, then we can replace the memory-less element
> with an element of the for ??<variable>, to remember an arbitrary
> sub-sequence of items in the datum, or ?<variable> to remember exactly
> one matching item in the datum. For example if we declare the variables
> role1, role2;
> 
>     vars role1, role2;
> 
> and then try these two matching expressions,
> 
>     [Fred Bloggs is a good teacher] matches [== is a ??role1] =>
> 
>     [Mary is a doctor] matches [== is a ?role2] =>
> 
> Then both should produce the result true. The pattern elements "==" will
> not remember what they were matched against. But the variables role1 and
> role2 will remember. The first will remember that it was matched against
> a list containing two words:
> 
>     role1 =>
>     ** [good teacher]
> 
> The second will remember that it was matched against a single item, a
> word (not a list containing that word):
> 
>     role2 =>
>     ** doctor
> 
> The differences between "??" and "?" as prefixes for pattern elements,
> and their uses, will become clearer with practice, provided below.
> 
> Summary so far: The normal format for using the matcher is:
121,122c264,265
< or FALSE, and may have additional useful side effects, as will be shown
< below.
---
> or FALSE, and if the pattern contains "??" or "?" pattern elements, the
> associated variables will remember what they were matched against.
149,150c292,293
< written on each side. The first is sometimes called a "datum", the
< second a "pattern".
---
> written on each side. The first is the "datum", the second the
> "pattern".
153c296
< on the right, by comparing the two element by element. If it does match
---
> on the right, by comparing the two element by element. If it does match,
166,175c309,319
< If the pattern element "==" is found on the right hand side, the matcher
< will try matching different numbers of items from the list on the left
< hand side with it, to see if it can find a way to make everything else
< match. There are other sorts of pattern elements, which are interpreted
< differently, described below.
< 
< This sort of ability can be used in programs like Eliza to sort
< sentences typed by the user into different categories, to be treated
< differently, as explained in TEACH RESPOND. For example you can write
< programs of the form:
---
> If the pattern element "==" is found in the pattern, the matcher will
> try matching different numbers of items from the datum list, to see if
> it can find a way to make everything else match.
> 
> Pattern elements with a memory, ie. pattern elements containing a
> variable name, are interpreted differently, as described below.
> 
> This sort of ability can be used in programs like Chatbots (e.g. Eliza)
> to sort sentences typed by the user into different categories, to be
> treated differently, as explained in TEACH RESPOND. For example you can
> write programs of the form:
216a361,362
> There are many ways of doing that. How many?
> 
218a365,366
> -- Segment pattern elements -------------------------------------------
> 
223c371,372
< Later we'll see how to use ?? with a name, to achieve that.
---
> Later we'll see how to use ?? with a name, as a segment pattern element
> with a memory, to store the item or items that were matched.
240,241c389,390
< list, like [the cat], is treated as ONE element of the list on the list,
< so it can match an occurrence of "=".
---
> list, like [the cat], is treated as ONE element of the list, so it can
> match an occurrence of "=".
247c396,397
< them in a subsequent command.
---
> them in a subsequent command. For that Pop11 allows use of pattern
> elements with a memory, represented by a Pop11 program variable.
279c429,432
< Rule1 (Pop-11):
---
> For example, this could be part of a rule-based program:
> 
>     vars input, output;
>     [Uncle fred is often very impatient] -> input;
287,288c440
< (Don't try typing that in yet. We have not defined INPUT so it will not
< work.)
---
>     output =>
290,293c442,466
< Here "??" is used to define a PATTERN ELEMENT used in matching and
< remembering what is matched, while "^^" is used as a LIST CONSTRUCTOR
< ELEMENT to produce a new list starting with the word "suppose", that is
< assigned to the variable "output".
---
> Mark all of that  and then run the marked range (Ctrl-d) to see what is
> printed out. Alter either the list assigned to input, or the pattern
> after "matches", then see what happens when you mark and re-compile the
> whole range.
> 
> [Note, Pop11 uses 'endif' as a closing bracket for 'if', 'endwhile' as a
> closing bracket for 'endwhile', 'endfor' as a closing bracket for 'for',
> and so on.]
> 
> Here "??" is used, in combination with a variable name, e.g. ??first or
> ??second (though spaces can be used between them) to define a PATTERN
> ELEMENT used in matching and remembering what is matched.
> 
> In contrast, after the "then" of the conditional instruction, "^^" is
> used, in combination with a variable name (as in "^^first"), as a LIST
> CONSTRUCTOR ELEMENT, or an "insertion element", to produce a new list
> starting with the word "suppose", that is assigned to the variable
> "output".
> 
>         [suppose ^^first were not ^^second] -> output;
> 
> [NB: Output here does not refer to the computer's output, such as a
> printer or screen. It is just programming variable, to which the list
> can be assigned. It could have been "x", or "y", or "supposition" for
> example.]
301a475,477
> Getting round that stupidity would require giving the program an
> understanding of English grammar, introduced in another teach file
> (TEACH GRAMMAR).
305,306c481,484
< Let's look more closely at the new pattern elements used in the first
< line
---
> -- More detailed explanation of segment pattern elements using ?? -----
> 
> Let's look more closely at the pattern elements used in the first
> line of the conditional:
312c490
< returns the result TRUE, i.e. the match succeeds, then the variables
---
> returns the result TRUE, i.e. if the match succeeds, then the variables
351a530,537
> So "??" in a pattern expression means, roughly, "set the value of...."
> and "^^" means "Use the value of. It can also be used in more complex
> ways to insert found or constructed components into a list.
> 
> Later you will learn also to use "?" to set the value but without
> constructing a new list, and "^" to insert the value, but without first
> putting it in a list.
> 
354a541,542
> -- More examples ------------------------------------------------------
> 
407c595
< Use the Load This Line command to find out the answers.
---
> Use the "Compile This Line" (ESC-d) command to find out the answers.
444a633,645
> In the next example the use of "=" before "and" and at the end of the
> list plays a crucial role:
> 
>     vars sublist;
> 
>     [he is a teacher and he is a father] matches [??sublist = and ??sublist =] =>
>     ** <true>
> 
>     sublist =>
>     ** [he is a]
> 
> 
> -- Using the same pattern variable more than once in a pattern --------
468,471c669,673
< So far we have prefixed variables with the double query symbol '??',
< which tells POP-11 to match the variable with any number of elements in
< the left hand list. We sometimes want to make sure we get only one item
< from the list on the left, and assign that item to a variable. Try:
---
> So far we have mostly prefixed variables with the double query symbol
> '??', which tells POP-11 to match the variable with any number of
> elements in the left hand list. We sometimes want to make sure we get
> only one item from the list on the left, and assign that item to a
> variable. Try:
653c855,866
< This is a modified version of the Poplog TEACH file.
---
> Advanced applications of the matcher are used in the Poprulebase library
> and in the SimAgent toolkit. Adventurous learners wishing to find out
> more about those two can do the following:
> 
> First make the libraries available
> 
>     uses newkit
> 
> then you can access these:
> 
>     TEACH * RULEBASE
>     HELP  * POPRULEBASE
655,656c868,869
< --- C.all/teach/matches ---------------------------------------------------
< --- Copyright University of Sussex 1987. All rights reserved. ----------
---
>     TEACH * SIM_AGENT
>     HELP  * SIM_AGENT (long)
658,659c871,873
< --- $poplocal/local/teach/matches
< --- Copyright University of Birmingham 1996. All rights reserved. ------
---
> --- $usepop/pop/teach/matches
> --- Copyright University of Sussex 2011. All rights reserved. ----------
> --- Copyright University of Birmingham 2011. All rights reserved. ------
