The POP-11 database package has many other features, but even with the four procedures that we have reviewed here, we can build a knowledge base for our Tourist Guide. For a fuller account of the POP-11 database see chapter 9 of Barrett, Ramsay, and Sloman (1985). In this section, we shall only employ the present procedure to answer tourists' questions, but the Guide could be further extended by using add and remove to set up the database with an appropriate store of knowledge and then to modify it from time to time as new knowledge is required (for example, if a new theatre show opens).
We will suppose that an appropriate database has been set up, as follows:
[ [[marble arch] near [hyde park]] [[marble arch] at [the west end of oxford street]] [[marble arch] isa [triumphal arch]] [[the science museum] near [the natural history museum]] [[the science museum] underground [south kensington]] [[the natural history museum] isa [museum]] ] -> database;
We can now define a version of the answer procedure which consults the database for information to construct a response to a query. (If you have difficulty in following the use of the pattern matching expressions used here, then go back to the last chapter, where their use is explained, or forward to the next section of this chapter, where there is a longer explanation.)
define answer(query) -> response; vars x y; if query matches [where is ??x] and present([^x at ?y]) then [^^x is to be found at ^^y] -> response; elseif query matches [== description of ??x] and present([^x isa ?y]) then [^^x could be described as a ^^y] -> response; elseif query matches [what == near ??x] and present([?y near ^x]) then [^^y is close to ^^x] -> response; elseif query matches [what == underground station to ??x] and present([^x underground ?y]) then [the nearest underground station to ^^x is ^^y] -> response; else [i cannot answer that] -> response; endif; enddefine;
Here is an example of how this procedure might be called, given the database as set up on page [*]:
answer([what is near the natural history museum])=> ** [the science museum is close to the natural history museum]
In the version of answer defined in chapter 2, the if part of each if ... then clause contained a pattern to be matched against the query, and the then part contained a list or pattern to be given as a response. In this new version each if ...then clause has three patterns, one to match the query, one to search the database, and one which is returned as a response -- for example,
Query pattern: [what == near ??x]
Database search pattern: [?y near ^x]
Response pattern: [^^y is close to ^^x]
The query pattern is matched against the query. If the match is successful, for instance, if the query were
[what is near the natural history museum]
then the variable x inside the pattern has its value set to the appropriate part of the query, in this case
[the natural history museum]
The database search pattern is now filled out with the value assigned to x, producing
[?y near [the natural history museum]]
This partially filled pattern is given as the argument to present which searches the database to find a match. If the search is successful (as it is in this case), the value of y is set from the database. In this example y will now stand for
[the science museum]
The response pattern is now fully specified, as both x and y now have values, and its value
[the science museum is close to the naural history museum]
is returned as the result of calling answer.