AN EXPERIMENT IN
KNOWLEDGE-BASED PROGRAM CONSTRUCTION
A Thesis
Presented to
The Chinese University of Hong Kong
In Partial Fulfillment of the Requirements
For the Degree of Master of Philosophy
In Computer Science
by
Ma Wai Yan
November 1985
1
PREFACE
This thesis covers an experiment to design and implement an automatic programming system called PROPSS, which aims to help the users to implement Prolog programs. PROPSS, which
is implemented in Prolog, is a knowledge-based programming
supporting system. Through this experiment, a method to
implement Prolog programs and a merge of two approaches to automatic programming-- Very High Level Language and the
Knowledge-based approach-- are investigated. The thesis is presented in seven chapters.
Chapter-1 presents a general idea of automatic programming.
Several approaches to automatic programming is also dis- cussed. It provides background knowledge for the later dis- cussion of the implemented experimental system.
Chapter 2 explains the concept of logic programming and
introduces the very high level language, Prolog. The main objective of this chapter is to show the methodology and
pitfalls of Prolog programming.
Chapter 3 considers the design of PROPSS. The objective of
the system is elaborated first, then the specification method, the approach, and the knowledge base of the system are described. 2
Chapter 4 illustrates the mechanism of PROPSS by two exam- ples of operation. Through these examples, it is hoped that the reader can have a better understanding of the design and implementation of- the system.
Chapter 5 shows the general format and content of the rules in the knowledge base of the system. Not all the rules, but the important or special ones are discussed.
Chapter 6 elaborates on the other implementation details.
Some of the codings will also be used to clarify the expla- nation.
Chapter 7 summarizes and evaluates this experiment. By the evaluation, a proposal on the future enhancement of the sys-
tem is also given. 3
TABLE OF CONTENTS
PREFACE i TABLE OF CONTENTS iii ACKNOWLEDGEMENTS vi ABSTRACT vii
CHAPTER 1 A SURVEY OF AUTOMATIC PROGRAMMING
1.1 Introduction ...... 1
1.1.1 Definition of Automatic Programming .. 2 1.1.2 Impact of Automatic Programming ...... 3
1.2 Approaches to Automatic Programming ...... 5
1.2.1 Very High Level Language ...... 6 1.2.2 Inductive Method...... 9 1.2.2.1 From Examples of I/O Pairs ...... 10 1.2.2.2 From Traces ...... 11 1.2.3 Deductive Method ...... 12 1.2.4 Knowledge-based approach ...... 14 1.2.5 Transformational approach ...... 18
1.3 Conclusion ...... 22
CHAPTER 2 LOGIC PROGRAMMING AND PROLOG
2.1 Introduction to Logic Programming ...... 23
2.2 Programming in Logic-- Prolog ...... 26
2.3 Programming in Prolog ...... 28
2.4 Examples of Prolog Programming ...... 33
2.4.1 The Method a Logician would use ...... 33 2.4.2 The Method a Programmer would use .... 35
2.5 Pitfalls in Prolog Programming ...... 40
2.5.1 Which Argument should be Initialized . 40 2.5.2 When Dummy Argument should be Used ... 42 2.5.3 Where Predicate should be placed ..... 44 2.5.4 How Detailed an Idea should be ...... 46 2.5.5 Is it Declarative or Imperative ...... 48 4
2.6 Conclusion...... 51
CHAPTER 3 DESIGN CONSIDERATION OF PROPSS
3.1 Introduction...... 53
3.2 General Consideration-- Aim and Scope 54
3.3 Design Consideration of the Specification... 57
3.4 Design Consideration of the Approach...... 64
3.5 Design Consideration of the Knowledge Base.. 67
CHAPTER 4 EXAMPLES OF PROGRAM TRANSFORMATION IN PROPSS
4.1 Example One-- Ordered Insertion...... 71
4.2 Example Two-- Ordered List...... 81
CHAPTER 5 KNOWLEDGE BASE FOR ELEMENTARY PROLOG PROGRAMMING
5.1 Rules for Formation of Predicates...... 83
5.2 Rules for Refinement of Control Structure... 89
5.3 Rules for Refinement of Data Structure...... 93
5.4 Rules for Refinement of Functor...... 95
CHAPTER 6 IMPLEMENTATION OF PROPSS
6.1 The Interface Module...... 98
6.2 The Refinement Controller...... 102 5
CHAPTER 7 EXPERIMENTAL RESULTS AND EVALUATION
7.1 Sample Programs ...... 105
7.2 Evaluation of PROPSS...... 108
REFERENCES...... 113
APPENDIX I Rules for formation of predicates 117
APPENDIX II Rules for refinement of control structure 122
APPENDIX III Rules for refinement of data structure 125
APPENDIX IV Rules for refinement of functor 126
APPENDIX V Prolog coding for the system 129
APPENDIX VI Sample programs 137
PUBLICATION 172 6
ACKNOWLEDGEMENTS
I would like to express my gratitude to my supervisor, Dr.
K.W. Ng, for guiding me in the correct direction in the pro- ject and giving me advice in these two years. I must also thank him deeply for his patience and effort, which have been necessary for me in order to finish this thesis.
I am also greatly indebted to my friends in the Computing
Lab. and the Micro-Lab. of the Department of Computer Sci- ence, CUHK, for their assistance and encouragement. 7
ABSTRACT
By considering the pitfalls of Prolog programming, the need of a Prolog programming supporting system becomes obvious.
An experimental automatic programming system called PROPSS
is then designed and implemented to support Prolog program- ming.
The experiment is not only to create a knowledge-based pro- gram construction system, but also to test the advantages and disadvantages of the combination of the very high level
language and the knowledge-based approach to automatic pro- gramming.
The system is built over an Edinburgh Prolog System which is
running on a PDP-11/34. It accepts formatted English as the
specification language, then it invokes the rules in its knowledge base to refine the specification to a Prolog pro- gram. During the refinement process, the system interacts with the user to ask for permission of some assumptions and
occasionally to ask for help. 8
The implemented system is proved to be helpful in some applications. Moreover, the outcome of the experiment shows
that the merge of the two approaches to automatic program- ming is a definitely promising way to make computers more accessible to the nonprogrammers. Besides these results, a methodology of Prolog programming is also explored.
Nevertheless, further development effort is still required to improve the natural language understanding ability of the system. Presently, the limitation in this ability hinders the performance of the system. CHAPTER 1
A SURVEY OF AUTOMATIC PROGRAMMING
1.1. Introduction
Today, everyone within the computer community knows that the most pressing issue facing the computer field is the software problem. Software is seldom produced on schedule to meet its performance goal, and is unreliable and often adulterated with some useless parts which, though harmless to the goal, reduce the efficiency of the software.
This inability to conquer the software development process, together with the intrinsic characteristics of hardware and software, makes the cost of software continue to increase, while that of hardware continue to decrease
[ 1]. Within the cost of software, an extremely large por- tion is for software maintenance. This cost is expected to increase, partly due to the accumulative amount of software, and partly due to the erosion of the optimality of the software. Most of the work in software maintenance is adapting old software to the newly changed environment.
However, the lack of understanding of the structure and the
meaning of the original software often makes the programmer omit, but not delete, those parts which are out of his con- trol. He only modifies what he must try to understand, but skips around what seems to be irrelevant to his duty. As 2
the frequency of maintenance increases, this accumulation of complexity and confusion of the software will inevitably result in a terrible increment of the maintenance costs and the execution costs.
1.1.1. Definition of Automatic Programming
In recent years, more and more research projects tend to focus on the software problem. Some of the researchers probe into the field of Software Engineering[ 2, 3]. They check and analyze the program for errors, thus validating the program. Some study the related aspects of automatic documentation and the frameworks to manage the information of the programs. Some even try to support a management sys- tem for software development. On the other hand, some researchers hope to design new systems to release the burden of programming from the human beings. These new systems
carry out part of the programming activity currently per- formed by human programmers in constructing machine execut- able programs. Thus they began the exploration of the field
of automatic programming.
Automatic Programming (AP) is the automation of some parts of the programming process. In a general sense, the objective of the work in automatic programming is the development of an application methodology, instead of the development of specific applications. It is going to 3 develop the method of specifying programs that will automate
the programming process in a given area. The ultimate goal is to make the computer more accessible to the non- programmer[ 4, 5]. From this fundamental sense of automatic programming, we can say that the first step towards automatic programming was, in fact, the development of com- pilers, or even the assemblers. They are equivalent to the advanced automatic programming system in that they assist the process of specifying what is to be done by the computer by raising the level at which instructions are specified.
1.1.2. Impact of Automatic Programming
The development of automatic programming techniques will inevitably affect the computer community. Firstly, by using automatic programming systems, these software systems
will hopefully be reliable enough to meet their performance goals, and the overall cost of software will be decreased.
Not only the cost of software development, but the cost of
software maintenance will also be reduced, since the old programs have been produced in a systematic way according to
some standard rules which are known to the users. The better understanding of these programs will enhance the
slight modification and the debugging of them.
Secondly, large classes of new users will be attracted
to computer usage since the prerequisite training in com- 4
puter programming is greatly reduced. They are no longer required to instruct the computer how to do something, instead they are only required to specify precisely what they want to be done. As a result, the application of com- puters will be enlarged to and deepened in different fields.
Moreover, the problem of the shortage of programmers and the problem of the misunderstanding between the communication of
the programmers and the final users will be overcome too.
Finally, a large number of computer scientists and software specialists will change from the role of the development of application programs to that of specializa-
tion in constructing processors that can generate programs
[6]. This means that more research projects will shift to focus on the discovery or development of the programming mechanism or on the implementation of AP system for any
specific application area. 5
1.2. Approaches to Automatic Programming
According to the definition given above, automatic
programming comprises a large part of software science.
However, we would like to restrict the discussion to only
include systems that have the ability to reason about pro-
grams, and to produce executable programs. Actually, this narrower idea can be used as a restricted definition of
automatic programming. N. S. Prywes even uses the term
automatic program generation to distinguish it from the
general name automatic programming which has been used to
refer to many other program development aids.
Every AP system has some significant characteristics
such as the way of dealing with missing information, the
efficiency and the understanding of the programs, etc.
Among these factors, four of them are the major identifying
characteristics: the specification method, the target
language, the problem area and the approach or method of
operation [7].
The specification method is the means for constructing and conveying to a computer the description of a desired program by a user. The different specification methods can
be classified as formal or informal. Programs can be
described in natural language, or in a conversational way,
or in other specific ways. Usually, a particular means of
specification will have a particular method of operation. 6
The target language means the programming language in which the output program will be coded.
The method of operation is the means for constructing and conveying to a user the desired program by a computer.
The systems can further be classified according to the dif- ferent approaches that they use:
(a) Very high level language (VHLL)
(b) Inductive method [8]
(c) Deductive method [9]
(d) Knowledge-based approach [10]
(e) Transformational approach
1.2.1. Very High Level Language
A very natural approach to automating the software development process is to continue the historical evolution- ary trend in developing even more powerful and expressive programming language, this is the very high level language approach [11]. This approach makes use of a formal specification of the problem. By which the users can com- pletely and precisely indicate what the programs are to accomplish. Fewer machine-oriented information details which are inessential to their problems are required to sup- ply to the computers. Furthermore, the very high level language can offer a conceptual framework in which the users can work while building the models of the problems. On the 7
other hand, this guidance will certainly reduce the users' flexibility and choices. The formal specification methods of which the syntax and the semantics are precisely and unambiguously defined, restrict the users in constructing programs.
As an approach to automatic programming, very high level languages, not the conventional programming languages, are selected because of their implementations. A very high
level language system has to capture the meaning of the pro- grams and discern the source of complexity in order to
effect major optimizing transformations on the programs.
This requires a flow analysis of the programs, a considera- tion of the operations, a built-in knowledge of data struc- ture alternatives, and some heuristics, etc. Usually, exe- cution monitoring and interaction with the users are needed too.
Generally, the underlying concept of very high level languages is to discard certain basic assumptions about pro- gramming and programming language, which derive from a Von geumann view of computation and make programming more com- plex than it need be.
An example of the very high level language is that of the SSL. Along with the language compiler is an optimizer, which in addition to selecting concrete realizations for its abstract data set, also selects a processing order for the various computations that comprise a program [12]. This 8 order is described implicitly by the programmer in terms of their data dependencies. MODEL is another VHLL, which belongs to a special kind of VHLL called data flow language
[6,13,14]. Another kind of VHLL is the logic programming language, with PROLOG as an example.
However, the most serious problem with the very high level languages is their inefficiency. The harder to represent the different aspects of application programs, the more difficult it is for the system to select an efficient way of implementation. The less coherent the application area i s, the harder it is for the system to optimize the
programs, since it has to capture a larger range of the pos- sible meaning of those programs. Therefore, the application area of a very high level language must be sufficiently coherent to allow domain semantics to be effectively cap- tured in the language design. However, not all aspects of application programs easily yield to an elegant conceptuali- zation that can be naturally embodied in the different con- structs of the very high level languages.
Nevertheless, the progress from the conventional pro- gramming languages to the very high level languages can actually improve the ability of the users to produce correct
and reliable programs. 9
1.2.2. Inductive Method
Induction, or inductive inference, is the system's
educated guess at what the user wants based on specifica-
tions that only partially describe the behaviour of the pro-
gram. All inductive inference systems rely on a good axiomatization of operations. The primitive operations in
the target language must be known by the system. Also, the
basic constructs in the target language must have a harmoni-
ous relation with the constructs in the specification
language. Besides these restrictions, it is still possible
that the system gets the incorrect meaning which is not the
one that the programmers want. Since the specifications are
incomplete, that is, the specifications allow for partial or
fragmentary descriptions of the desired programs, the
specifications can only partially describe the behaviour of
the programs. The system must check for and resolve the
inconsistencies among the different parts of the same
specification, and try to fill in the missing information.
Unfortunately, it is still possible that even though the
system gets the correct meaning of the specifications, it
could still produce syntax-corrected programs which are not
required by the user.
The specification methods used in the inductive
approach are often by examples of input/output pairs and
program traces, in both regular and generic form. 10
1.2.2.1. From Examples of Input/Output Pairs
After the examples are read in, the system will first make use of the information presented in them to select a
schema. A schema is like a program skeleton and it defines
the general structure of a program. A set of schemas are
predefined and stored in the system to characterize an
entire class of programs for a specific problem domain. In
order to select a schema, the system keeps a set of heuris-
tics and characteristics for each schema. It will first
apply a set of difference measures to the inputs and outputs
of the example pairs. They will find the differences
between pairs and associate the structure of the pairs with
a schema. Then, it will use the set of heuristics for each
program schema to determine a fit measure of the example
pairs. The chosen schema will be the one with the highest
value for fit. If more than one schema are chosen, a deci-
sion rule is required to select one. The system will then
try to instantiate the unfilled slots of the schema. A set
of rules associated with the specific schema will then guide
the system to instantiate the unfilled slots of the schema.
Finally, the system will generate some new examples for the
program, evaluating the synthesized program on the example
set. Also the system can check the result with the user, or
let him verify the correctness of the program by presenting
it to him.
Although it is easy for the user to create anu speciry 11
the example, this method is suitable only for synthesizing programs in those cases where the task domain is repetitive enough that a small set of pairs is sufficient to specify
the program. However, it is almost never the case in prac-
tical programming environment.
1.2.2.2. From Traces
This method describes the desired programs by specify-
ing the trace of the operations to produce the results. The basic approach is to generate, in order of increasing com- plexity, the possible programs constructed from the opera-
tors of the programming language and tests their functional
compositions and then, after each new program is generated,
to validate the given traces against the program. If it
accounts for the traces, it is the required solution. The
system has to make use of heuristics to prune and guide the
search process among the possible solutions.
Though it is also an informal partial specification
method, it is less ambiguous than by using the input/output
example pairs. It is also closer to the form in which
humans understand and describe programs. Furthermore, it is
a better compromise between the works of the computer and
those of the users. Since it is always a very easy task for
the users to give a hint, or a first step, to the solution,
but it can save a lot of the effort required by the com-
puter. 12
Usually, the traces are mixed with other information such as the operators applied to the data objects, the state snapshots of data, etc. Different classes of traces arise
if restrictions are placed on the kind of information that may appear.
However, it is known that the programs for the partial recursive functions cannot be generated from samples of
input-output behaviour [8]. The only solution is to enumerate a subset of the partial recursive functions for
which the halting problem is solvable or to allow for a par-
tial solution to the halting problem by limiting the number of steps a program may complete before it halts. As a result, this approach is only suitable to the inference of small programs in very simple domains.
1.2.3. Deductive Method
The deductive method regards program synthesis as a
theorem-proving task. It takes the program verification
idea one step further. Instead of simply trying to prove
the theorem derived from the input and output predicates, it produces the desired program as a side product. The early work relied strongly on mechanical theorem proving tech- niques such as the resolution-based theorem proving. How- ever, the difficulty of representing the principle of mathematical induction in a resolution framework hampered 13
these systems in the formation of programs with iterative or recursive loops. Newer theorem-proving systems are able to perform proofs by mathematical induction, but are useless for program synthesis because they have sacrificed the abil-
ity to prove theorems involving existential quantifiers [9].
In conjunction with the theorem proving based approach to automatic programming, the formal methods of specifying programs are used. The input and output conditions are specified in the formalism of the predicate calculus. So one can specify a program as
(all) sl.
(p(sl) (there exists) s2, q(sl,s2)) which states that for all values of input variables to the program, sl, for which the predicate p(sl) is true, there are output variable, s2, such that q(sl,s2) is true. Or put it in another way, we can specify the program as
f(sl)== find s2
such that q(sl,s2) where p(sl) for example,
sqrt(n)== find z such that
integer(z) and z**2= n (z+l)**2
where integer(n) and 0= n.
To derive a program from such a specification, the system will try to prove a theorem of the form
for all sl
if p(sl) then for some s2, q(sl,s2)
this is equivalent to what was stated before. 14
There are several constraints inherent in this theorem proving approach. First, for more complicated programs, it is difficult to specify correctly the input and output predicates of the program. Second, the theorem prover must
have enough axioms about the input and output predicates.
Especially it needs the proper induction axioms for the recursive program. Thus the application domain must be
axiomatized completely, and the users must be able to pro- vide the required axioms. This is another difficulty for
the users. Finally, present theorem provers lack the power to produce proofs for the specification of very complicated programs.
Nevertheless, an automatic programming system can incorporate theorem proving either where it is convenient or where correctness is an important requirement.
1.2.4. Knowledge-based Approach
An automatic programming system is said to be knowledge based when it is built by identifying the knowledge of expertise that is appropriate for program syn- thesis and understanding, that is, the ability to manipulate and analyze programs and by encoding this knowledge expli- citly in some knowledge representation.
Usually, a knowledge-based automatic programming sys- tem has three different components. The first component is 15
the interface between the users and the system. It is responsible for the acquisition of a model from the users' description of the action of the desired programs. Various forms can exist. For instance, the users of the NLPQ
(Natural Language Programming for Queuing Simulations) sys- tem describe their problem environment by providing facts about the structure of the stationary system, the arrival rates and processing times of the mobile objects, and so on.
However, the users of the PSI system, which addresses to a
much broader problem area, cannot just provide a problem
description, but must give a functional description of what
the desired system is intended to do. Furthermore, the
users must provide for their programs the overall control
structures, which provide the context into which the created
program segments are attached.
The second component is the knowledge base, which pro-
vides the necessary information for the interface section to
communicate with the users and to understand their require-
ments, and also the sufficient information for the next sec-
tion to generate the required programs. This knowledge
includes the wide variety of concepts of the domain applica-
tion area. These concepts are used for filling in the miss-
ing information from the users or making explicit some
unstated assumptions [15]. The knowledge base also includes
the specific implementation techniques, the guidelines or
heuristics suggesting when a particular implementation tech-
nique is appropriate, and the programming language knowledge 16
that is independent of an particular language.
Generally, the knowledge encoded within the knowledge
base can be divided into two parts. The programming knowledge includes the syntax and the semantics of the tar-
get programming language, and the general programming knowledge about the general computational mechanisms, optim- ization techniques, and strategy and planning techniques.
For instance, some kind of parameterized templates for stan- dard algorithms can be used. In order to apply this knowledge in different situations, the systems also need some understanding of why and how the basic algorithm works.
The other part is the domain knowledge which makes the
systems understand the structure and dynamics of the problem area for which the program is to be written. The system requires this information to infer from the program descrip- tion of what needs to be done to solve the problem. Some-
times, alternatives will be required to define the given problem and to solve them. In this part, researchers are
sharply divided on issues ranging from selecting mechanisms for representing domain semantics, to resolving the tradeoff
between the breadth of a system (the range of applications
it supports) and its depth (the extent of the programming
process that it automates). The automatic programming sys-
tems we stated before are examples of these differences.
The NLPQ system addresses only a very specialized
problem area-- queuing problems. The target language is 17
GPSS (General Purpose Simulation System), which is already a high level programming language specially designed for implementing queue models. On the other hand, the PSI sys- tem aims to include the whole area of symbolic computation, such as list processing, search and sort, and data storage and retrieval etc. Thus PSI is more flexible and compli- cated.
Furthermore, the knowledge base should also contain debugging knowledge such as how to discover and remove the cause of any discrepancy between the program performance and specifications, and knowledge of the user such as what information to expect from him, what information to send him, and how to converse with him in his own language.
Finally, another basic concern is how to represent knowledge in the systems, such that the search for relevant facts will not cause a combinatorial explosion. Various appropriate representations include,
(a) PLANNER-like procedural experts
(b) Refinement rules
(c) Semantic nets
(d) Amorphous systems that try several ad hoc techniques.
The third component is the code generator. In this part, the acquired program model is reduced to efficient code expressed in a conventional programming language. The main issue of this part is the efficiency of the resulting programs, so a lot of optimization techniques is needed. 18
Besides the above constructs, the implicit construct or the control mechanism of the system is the methods of reasoning. In addition to the traditional formalisms of logic, the systems can also make use of other techniques such as inference, program simplification, illustration and simplification for the users, decision trees, problem solv- ing techniques, and refinement, etc.
The central issue pertaining to this approach is the knowledge. The most important question is how the knowledge about knowledge can be obtained [8]. The answer is to study examples. Thus in the program synthesis domain, one should very carefully study how one example program might be created. Then one should look sequentially at how several similar programs might be produced, and eventually how the synthesis capability might be extended to other classes of
programs.
Along the advocation of the application of artificial
intelligence, especially the application of expert systems, and the announcement of the Japanese fifth generation com- puter project [16), this approach becomes more popular and
more important.
1.2.5. Transformational Approach
From a very general sense, the program transforma-
tional approach refers to any mechanical aids for 19 transforming a source description of a program into an equivalent target description of the program. For example, it can be an automated conversion of easily written, easily understood programs into more efficient, but perhaps more convoluted programs [17]. It can also mean a methodology of program construction by successive applications of transfor- mation rules[ 18].
However, from a more specific sense, this approach can be considered as the activity of integration, separation, modification and explanation of existing programs [191. The fundamental use of a programming system is no longer in creating sequences of instructions for accomplishing a task
( or carrying out algorithms), but in expressing and mani- pulating descriptions of computational processes and the objects on which they are carried out. It should provide a uniform framework for attaching the information that now appears in the declarations, assertions, and documentation.
Furthermore, the system should provide a set of tools of
generating, manipulating, and integrating descriptions of both results and processes in different ways [201. The
activity of writing a program is only one part of the
overall activity that the system must support, and emphasis
should be given to understanding programs.
This approach is based on the philosophy that the
building blocks out of which systems are built are not at
the level of programming language constructs. They are 20
"subsystems or packages, each of which is an integrated collection of data structure, programs and protocols. This
concept is actually an extension of that of structured pro- gramming. Consequently, the central issues in this approach are the overall structure of the system, the relationship among the different programs within the system, the recur-
sive structures within the programs which facilitate the
transformation of the programs the description of the pro-
perties of the original programs and the storage and
retrieval of this information. The last three issues are
already studied under the field of data structure, the field
of program specification and that of the information system,
but the first two issues are still without significant pro-
gress. Programs cannot be related just according to the
similarity of their specifications. For example, the pro-
gram to produce invoices from orders is very simple. How-
ever, a small change within the specification, such as the
change from one invoice per order to one invoice per custo-
mer will make two programs completely different from the
others [5]. It seems unrealistic to further divide the pro-
grams into smaller common fragments( besides the input and
the output), since these programs are already very simple
from the human's conception. Nevertheless, everyone can see
the similarity of the application of these programs with
that of the other programs such as the on-line processing
and the sort-and-execute process respectively.
As a conclusion, the advantage of this approacn is its 21 similarity to the human way of learning and creating new objects. The most primitive mechanisms for the human being to solve a problem are by analogy and by divide and conquer.
However, this promising approach must also make use of the other techniques such as the knowledge base and the formal- ism of logic etc, in order to be feasible. 22
1.3. Conclusion
Although five approaches are stated above, there are
other approaches that make use of the general problem solver, the traditional problem solving technique, and/or other heuristics etc. None of the above approaches can be considered as the most promising direction for research.
Actually, all of the existing automatic programming systems must make use of more than one type of technique to solve the problems. For instance, the PSI system makes use of both the knowledge base, and the deductive method. Similar to specification methods, it is believed that different approaches are suitable for different applications and even different users.
Though it seems that a general AP system which can be of use to programmer is still far from the present, along the development of artificial intelligence, which can be considered as a search for appropriate methods of automatic programming, we can have more confidence to believe that we could overcome this software crisis in the near future. CHAPTER 2
LOGIC PROGRAMMING AND PROLOG
2.1. Introduction to logic programming
Logic is an important tool in the analysis and presen-
tation of arguments. It is not concerned with the truth,
but with the relationships between the arguments, between
the assumptions and the conclusions. Symbolic logic was
first designed as a formalization of natural language and human reasoning. Thus, it can be said that as a specifica-
tion language, symbolic logic is the language which is entirely user-oriented. Naturally, it has been adapted to
as a specification language for computer programs.
Along the development of the theorem prover in
automated deduction, schemes have been found for processing
logic by computer. If we can see our programs as our
hypotheses about the world, and our questions are like
theorems that we would like to have proved, we can use the
theorem prover to prove the theorem, that is, to solve our
question. Therefore, symbolic logic is no longer only a
good specification language, but also an entirely user-
oriented programming language [21,22]. It is along the
development of computational logic, such as the invention of
unification and the effort to overcome the combinatorial
explosion problem with the resolution algorithm, that the 24
concept of logic programming was born [23]. The notion that
computational processing is equivalent to controlled deduc-
tion was first proposed by Pay Hayes. He also suggested a
decomposition of the specification of an algorithm, which
was formulated by Kowalski as [24]
Algorithm= Logic+ Control
One of the most important characteristic of the logic
component is that it possesses no features which are mean-
ingful only in machine-level terms. The method to find or
to search for the solution is already largely determined by
the inference system, that is, the proof procedure. To
improve the method of solution searching, researchers can
try to associate the logic program with different proof pro-
cedures. Several researchers, such as Michie and Kowalski,
have realized a rich variety of proof procedures guided by
heuristic DrinciDles or by domain-specific knowledge [25].
For the control component, it can be restricted not to
affect the meaning of the algorithm as if it is determined
by the logic component. Different control components can be
used to solve the same problem, but with different effi-
ciency. Sometimes, seemingly different algorithms can be
obtained by applying different methods of control to the
same logic definition. In fact, they are equivalent since
they can produce the same result for the same problem. How-
ever, the problem that they are actually intended to solve
can be very different. An example can be the quicksort 25 algorithm and the permutation generator.
All the evidence suggests that logic programs can express the logic of problem solving methods. It is less imperative and more descriptive than the other conventional programming languages. Hence, it is more congenial to inex- perienced programmers. It is also easier to verify. How-
ever, to realize the feasibility of symbolic logic as a pro- gramming language, it must be certain that problems can be
expressed convincingly in symbolic logic, and that programs
can be executed efficiently. The expressive ability of sym-
bolic logic is proved to be satisfactory by Kowalski's work
[26]. Its efficiency is also assured by the first practical
interpreter for logic programs in Prolog, which was
developed by Colmerauer and others at Aix-Marsaille. 26
2.2. Programming in Logic -- Prolog
Prolog means'PROgramming in LOGic. A logic program written in Prolog is similar to a set of Horn clauses, which are a special form of first-order predicate logic, whose conjunction gives an unsatisfiable sentence in clausal form.
The sentence's unsatisfiability can be proved using an
inference system for first-order logic such as that provided by the resolution principle [27]. Because this sentence is unsatisfiable, by a theorem of Herbrand, there exists an unsatisfiable finite set of variable-free instances of clauses in this sentence. The instances of the variables in these clauses can then be viewed as the solution of the problem stated by the sentence.
A Prolog [28,29] program consists of a set of clauses, where each clause is either a fact about the given informa-
tion or a rule about how the solution may relate to or be
inferred from the given facts. The general form of a clause
is
PO :- P1,P2,...,Pn. which can be interpreted as PO is true if P1 and P2 and...
Pn are true. Or from the sense of procedural interpreta-
tion, to satisfy goal P0, satisfy goal P1 then P2 then...
Pn. In the special case that the clause represents a fact,
then it is a clause without. the body, but with the head
only, that is of the form
P. 27
Proof of a predicate, that is to process a computation, can be started by a prefix symbol'?-' and followed by a clause, as in the form
?- P1,P2,...,Pn.
It is a question to be solved and is interpreted as are P1 and P2 and... and Pn true? or as satisfy P1 and P2 and
... and Pn. Prolog will try to satisfy them by matching
all the valid procedure and by instantiating the variables
in them.
The symbols P,P1,P2,...,Pn in the above clauses
represent the uniform data structure provided by Prolog
called term. A term is either a constant, a variable, or
a structure. Constant is either an integer number or an
atom. Atom is either made up of letters and digits or made
up from special symbols. Variable is a special atom with
the first character as a capital letter. Structure is a
single term which is a collection of other terms. It is
written in Prolog by specifying its functor and components.
A functor can only be an atom, and a component is just
another term. The components are enclosed in round brackets
and separated by commas, and the functor is written just
before the open bracket.
Prolog is a VHLL whose basic mechanism mainly deals
with the relationship between defined objects, so it does
not only blur the difference between specification and pro-
gram, but also the difference between database and program. 28
2.3. Programming in Prolog
To write a Prolog program, one can follow the steps of a logician to consider a practical problem as a logic state- ment, and derive a set of first order predicate clauses from
it. However, it is very probable that the Prolog inter- preter will reject them since the syntax is incorrect. To make them executable, one will find that it is not always
possible to just correct the syntax without rewriting the
clauses. It should be emphasized that programming in Prolog
is not equivalent to programming in logic. For simpler
applications, especially for problems pertaining to deduc-
tive information retrieval and intelligent database process-
ing, etc., the programmer. can immediately type in the
correct predicate clauses, because most of the simpler prob-
lems of these types are similar to the basic design philoso-
phy and the process mechanism of Prolog. However, for the
more complicated problems, it will be impossible.
Following the method of a programmer, one should first
consider the procedural intepretation of the predicate
clauses. A Prolog clause can be constructed as a combina-
tion of three parts, the procedure heading, that is the head
of the clause, the condition check followed by a cut, and
the action which can be either immediate data manipulation
or procedure call. Actually, the terms that can be used in
the second part is the same as those in the third part. It
is a special property of Prolog that one can use a predicate 29
to instantiate an uninstantiated variable, and use the same predicate to prove the value of that instantiated variable.
Procedure call is one of the control structures in
Prolog. The break from the procedure, that is the cut('!') is another one. The head of a clause in Prolog can be con- sidered as the procedure statement in conventional program- ming language like Pascal. However, when the head of the clause is not a simple atom, the invocation of a procedure is not only determined by the name, but also by the struc- ture and/or content of the passed parameters. In case the predicate name and the arguments match with those of the caller, the condition will be checked. The action will be performed only after all of these have passed. If any of
the conditions fails, the Prolog interpreter will automati- cally backtrack to try to satisfy the condition by other means. If all the conditions fail, the interpreter will
then just try to match with another procedure, that is to backtrack to try to satisfy the caller. After all the con- ditions and the term 'cut' are passed, the actions will be executed. Same as the conditons, if any of the actions fails, the interpreter will also try to satisfy the action
by other means. However, if all the actions fail, the
interpreter cannot get through the cut again, it can only
fail the call action which invokes this procedure. Another
two control structures in Prolog are 'repeat' and 'fail'.
'Repeat' is a predicate that will always be satisfied, and
'fail' is the one that can never be satisfied. 'Fail' is 30 always used after the 'cut' to work as a trap case which
will fail the call action when some invalid conditions are met.
The action can be a call of another procedure. A pro-
cedure call will be introduced in the action whenever there
is a conditional branch situation, which is similar to the
If-then-else structure, or the conditional looping in the
structural algorithmic programming language. Therefore,
every predicate clause in a program will have either a
sequential processing structure or a recursive structure.
The action can also be a data manipulation statement. In
Prolog, there are actually four types of data, the constant,
the list, the structure and the file. Each type of data has
specific operations pertaining to it.
The constant can be a number or an atom. Prolog pro-
vides a set of common arithmetic operators to manipulate the
numeric data. They include the operator is which means
the expression after the operator is evaluated and assigned
to the variable before the operator. The function of this
operator is a deviation from the general concept of Prolog,
since it is actually an assignment, but not a proof of some-
thing nor a statement of something. You cannot use this
operator to instantiate, or to prove, the value of a data to
the right of the operator is. In fact, the advantage of
Prolog is not in the manipulation of numeric data. This set
of nnpratnrq is only a necessary accessory for Prolog to 31 become a practical programming language. Atom is usually a component of the other data types, such as an element of a list, or a part of a structure. Although Prolog has only a limited set of operations which are specially used for checking the condition of atoms, reading and printing an
atom, etc, the atom is a very common data type to be pro-
cessed by the users.
The list is the most common data type to be used in
Prolog programming. It can be represented by enclosing a
sequence of atoms by the square brackets and separating the
atoms by commas. This sequence of atoms can also be fol-
lowed by a vertical bar, and then a list which is the tail
of the current list. Besides the declarators stated above,
the 'name' operation which converts data between atom and
list, and the 'length' operator to return the number of ele-
ments in a list, Prolog has no other operator specially
designed for list processing. List processing is usually
implemented by using recursive execution to separate the
elements of a list, and by manipulating the individual atom
one by one each time.
The third data type is the structure, which has
already been described in section 2.2. Prolog has operators
to retrieve and construct the different parts of a struc-
ture, which are the 'functor' and the 'arg It has also
the operator which converts the list on the right side
to the structure on the left side. A special type of struc- 32
ture is the clause, which is actually a structure with the
functor ':-'. It is the program sentence that can be exe-
cuted by the Prolog interpreter. The clauses that can be
interpreted are all stored in the database of the system.
Therefore, Prolog provides operators to 'assert' clauses
into the database and to 'retract' clauses from it.
Prolog also has the file data type, which is similar
to that of the other programming language. 'See' is the
operation which opens a file for reading, and 'tell' is the
operation to open a file for writing. 'Seen' and 'told'
close a file open in the above situation respectively. How- ever, it does not have an operator to position the window of
the file. Similiar to the structure, a special type of f ilE
is the file which stores clauses. One can 'consult' thiE
type of file to interpret the clauses in it. A clause will normally be asserted in the database, or it will be executed
if it is preceded by the symbol One can also 'recon-
sult' a file. The clauses in the database with the same
predicate as the head of the clause in the file will then be
deleted.
Besides the above possible actions, Prolog also has
other operators which are specially for controlling or
checking the system status. However, they are less fre-
quently used, especially for the user who needs a supporting
system to help him to construct a Prolog program. 33
2.4. Examples of Prolog Programming
The methods stated above are only a foundation for the programmer to percieve how to perform programming in Prolog.
To be familiar with Prolog programming, one can only study
through examples. In the following paragraphs, the applica-
tion of the programming methods of Prolog will be illus-
trated by examples.
2.4.1. Example 1-- the method a logician would use
For an application such as deductive database process-
ing, the operation needed is implemented as the basic
mechanism of Prolog execution. The programming process will
be very straightforward. For instance, the user can specify
the fact of a family tree by predicate clauses as,
father(john,johnson).
father(john,mary).
mother(ellis,john).
and ask questions about the relationship between different
people, such as using
?- father(X, johnson) ,mother(Y,X).
to find out who are the grandmother of Johnson. However,
for other applications, they require the user to think and
construct a more complicated program. 34
This first example demonstrates the programming of applications which have clear logical expression. An
instance of such kind of application is the evaluation of a
formula in which the relationship between variables is
clearly defined, such as the evaluation of the Fibonacci
number, and the factorial of a number, etc. Another
instance is the application of the set manipulation.
In case the user wants to find out if a set X is a
subset of another set Y. By definition, an empty set is a
subset of any set, and a set is a subset of another set if
all the elements of the first set are also the elements of
the second set. According to this understanding, the uses
can derive the following logical statement,
X is empty X is subset of Y if tor all e E X, e E Y
This definition has two conditions, therefore we should have
two Prolog clauses. The first condition of this statement
is straightforward. The second condition involves the mani-
pulation of the elements in a list, which indicates the
usage of a recursive program structure. So this statement
can be transformed to the following Prolog clauses,
subset([],-).
subset([EIX],Y):- member(E,Y), subset(X,Y).
where the '[]' means an empty list and the underscore
means a data that matches anything. 35
The second application is to prove if an object is a list. Similarly, the user can define what a list is. By definition,
X is an empty list, X is a list if
X has a head and a tail.
This definition has also two cases, both of them are
straightforward. The content of the head and the tail in
the latter case are even not significant. Therefore, the
Prolog clauses that constitute the required program are,
islist
islistl
From these examples, the beauty of the perfect compa-
tibility between the problem definition and Prolog's sen-
tence is shown. In some applications, the problems seem to
be more complicated. Usually, they can be reduced to a set
of subproblems that can be easily satisfied individually by
defining the subproblem precisely. It seems that program-
ming in Prolog is relatively easy if one is familiar with
the basic idea, such as pattern matching, automatic back-
tracking, etc. In fact, there are some pitfalls that we
must be careful.
2.4.2. Example 2-- the method a programmer would use
Tsually, a programmer has been trained for programming 36 in other programming languages before he tries to write a program in Prolog. He has been used to think about the con- trol flow and data manipulation. This example is going to show that one can also construct a good Prolog program from this thinking process.
The task is to write a program which performs the quicksort. The programmer can first construct the sorting program in an algorithmic pseudo-language as,
Procedure quicksort(x,sortx: list)
Var xt,upper,lower,sortu,sortl: list
ele: atom
Begin
If x nil
then begin
ele:= head of (x)
xt:= tail of (x)
par tition(xt,upper,lower,ele)
quicksort(upper,sortu)
quicksort(lower,sortl)
concatenate(sortu,ele.sortl,sortx)
end
If x= nil then sortx:= nil
end
It is assumed that this pseudo-language has the advanced
feature of list processing. The data types 'atom' and
'list' are provided. The operation 'head-of', 'tail-of' and
are also available in the language. The operator is 37 equivalent to the 'conc' in Lisp, and the constant 'nil'
means an empty list in this language.
As stated before, the procedure call is one of the
control structure in Prolog. The invocation of a procedure is not only determined by the name of the procedure, but also by the successful pattern matching of the arguments.
The procedure shown above actually has two alternatives for processing according to the content of the input arguments.
Therefore, the above procedure can be divided into two pro- cedures as,
Procedure quicksort(ele.xt,sortx: list)
Var upper,lower,sortu,sortl: list
Begin
partition(xt,upper,lower,ele)
quicksort(upper,sortu)
quicksort(lower,sortl)
concatenate( sortu,ele. sortl, sortx)
End
Procedure quicksort(nil,sortx: list)
Begin
sortx:= nil
End
Furthermore, besides the 'is' operator which expli-
citly assigns the value of a numeric expression to an atom,
the assignment in Prolog can be performed by the instantia-
tion of the arguments. The Prolog interpreter has the pro- 38 perty that it will try its best to satisfy the matching requirement by instantiating specific value into the vari- ables that do not have any values, but not just to test the equivalence. In this way, the low level, machine oriented
operation like assignment is avoided. So the second pro- cedure shown above can be further simplified as,
Procedure quicksort(nil,nil: list)
Begin End
Finally, after the modification of the syntax of the above procedures, the required Prolog program will be,
quicksort([],[]).
quicksort([ElelXt],Sortx):-
partition(Xt,Upper,Lower,Ele),
quicksort*(Upper,Sortu),
quicksort(Lower,Sortl),
append( Sortu,[EleISortl],Sortx).
The programming method shown above first implements
the control flow by using the head of clauses and then uses
the Prolog operators to perform the required manipulation.
In some cases, the control cannot be fully determined by the
head, so the condition part is included. It is shown in the
following example,
union([],X,X).
union([ X I R] Y, Z):-
member(X,Y),!,
union(R,Y,Z). union([XIR],Y,[XIZ]):- 39union(R,Y,Z).
The calling which can be satisfied by the head of the third clause can also be satisfied by that of the second clause.
However, the difference is that, in order to execute the action of the second clause, the interpreter must also try
to satisfy the condition 'member(X,Y)' stated in the second
clause.
As a conclusion, it seems programming in Prolog is
relatively easy if one is familiar with the basic idea, such
as pattern matching, automatic backtracking, etc. In fact,
there are some pitfalls that we must. be careful. 40
2.5. Pitfalls in Prolog Drogramminu
A programming language is an intermediate medium between the human user and the machine. Ideally it should reduce the gap between human reasoning and machine process- ing mechanism to its minimum. Accordingly, none of the con- temporary programming languages can be said to be ideal. It is no exception for Prolog. However, it is usually stressed
that programming in Prolog is very easy, even a naive pro- grammer can handle it within a few hours of learning. The rationale behind this conception is that Prolog clauses are actually some logical statements, and logic is the basis of every rational subject. The problem arises in that Prolog programming is not equivalent to logic programming. Prolog still shares the influence of the fundamental von Neuman's idea for conventional programming languages. If a user wants to write a Prolog program just according to his logi- cal reasoning, he will probably fall into an unexpected trap of the language.
2.5.1. Which Argument should be Initialized
One of the advantages of Prolog is that the arguments
(the components) of the procedure (the structure) are not
fixed for input or output[ 30], as can be shown by the con-
catenate predicate. 41 conc([],L2,L2).
conc([XI L1] ,L2, [XI L3]):- conc(L1,L2,L3),
If the user states
?- conc([a,b],[c,d],L).
the system will return successfully with the variable L instantiated to a list of a,b,c and d. On the other hand, if the user states
?- conc(L,[c,d],[a,b,c,d]), then the system will also return successfully with the vari- able L instantiated to a list of a and b. The operation can be done in two opposite ways.
However, it is not always the case. In some situa- tion, the function of Prolog's arguments cannot be arbi- trarily changed. For example, one wants to calculate the factorial of a number. Similar to the above example, start- ing with the definition, the Prolog program can be,
factorial(0,1).
factorial(M,N):-
M1 is M- 1,
factorial(M1,N1),
N is M* Ni.
Using this program, one can only initialize the value of M and find the value of N, which is the value of factorial M.
One cannot state the value of N to find the value of M, that is, one cannot reverse the operation as shown above. This pitfall is induced by the assignment operator 'is', which restricts the variables of the expression at the right hand 42
side to be initialized before the assignment is executed.
However, it is not all the necessary consideration associ- ated with the arguments.
2.5.2. When the Dummy Argument should be Used
There are other situations that the user should be cautious about with the arguments. Consider the following two Prolog programs,
collect_list_1([XIL]):-
retract (nextnode(X)),
collect list 1(L).
collect-list-1([1).
collect list 2(L,Ln):-
retract(nextnode(X)),
collect_list_2([XIL],Ln).
collect list 2(L,L).
The functions of these two programs are roughly the same
except the sequence of the elements in the list returned is
reversed. Their functions are to find a list which stores
all the nextnode X. The first program is simple. For every
nextnode X retracted from the database, it will be instan-
tiated as the head of the list with the tail still being
uninstantiated. When all the nextnodes have been retracted,
the tail will be set as an empty list. However, the next
program is more peculiar. It makes use of two arguments,
an its first argument must be an empty list when it is 43
called. At each recursive step, the interpreter will add a nextnode X to.this list. When all the nextnodes have been
retracted, the process meets the boundary case, so the next
clause will be executed. The next argument will then be
instantiated as a list of all the nextnode.
Though the first program seems to be more natural, in
some cases, it is necessary to construct a program in the
second way. One example is to write a Prolog program to
implement the breadth first search. The clauses of the Pro-
log program are shown as,
search(X,Y):- breadthtirst(L XJ, Y).
breadthfirst(L,D):- member(D,L).
breadthfirst(L,D):-
nextlevel(L,[],Ln),
breadthf irs t(Ln, D).
nextlevel([],L,L).
nextlevel([ N (Ns], Sf, L):-
findall(D,nextnode(D,N),Ds),
union(Ds,Sf,Sf1),
nextlevel (Ns, Sf 1, L).
This program is going to prove if node Y can be accessed
from node X. The strategy is to find if the destination is
one of the nodes of the current level of the tree or graph.
If it is not, then find whether it belongs to the next
level. It is assumed that the database has already had the
assertions of nextnode. A 'nextnode(D,N)' assertion means D
is the son node of N. The function of 'f indall' is to 44 create the list of Ds whose elements are D's, the son nodes of node N.
For the predicate 'nextlevel', a third argument is needed besides the initial list and the final list. This third list must be initialized as an empty list before exe- cution because the notation '[HIT] restricts the variable
H to be considered as a unique element of the list, while
the programmer wants to use the defined function 'f indall'.
Though the latter makes the program simpler, yet it returns a list. The problem is that Prolog does not allow one to
instantiate a group of front elements of a list at one time.
So the programmer must use the function 'union' to create a new list, which is passed to the next recursive step by the
first argument. With the boundary case where all the nodes
of the current level are processed, all the nodes at the
next level will then be passed back to the caller of
'nextlevel' by the second argument.
It can be shown that programming in Prolog is not as
simple as a direct transformation of the human-oriented rea-
soning. The programmer must consider the problem induced by
the variables. Nevertheless, this is not all the difficulty
that will be met in programming in Prolog.
2.5.1 Where the Predicate should be Placed
Suppose that it is wanted to implement the insertion 45
sort. The program will check and insert each item of a list into the appropriate position of a new list.
insert sort([],[]).
insert_sort([XI L] M):-
sortx(X,N,M),
insert sort(L,N).
sortx(X, [AIL], [AIM]):- X A. sortx(X,L,M).
sortx(X,L,[XIL]).
The meaning of the program is:
If the list is empty, then it is sorted.
Otherwise, pick an element X from the list,
and insert it in a new list in the appropriate order,
and repeat to sort the remaining element of the list.
/* To insert the element X into the new list:*/
compare X with the first element of the new list,
if X is larger, compare with the next element,
otherwise, just insert in the head of the new list.
The logical reasoning of the process is alright, how-
ever, the program execution is not. The problem is with the
instantiation of the variable N in the predicate 'sortx'.
As it has no value, the two 'sortx' clauses cannot function
properly. To correct it, the second clause of 'insert-sort'
should be modified to,
insert sort([XI L] ,M):-
insert sort(L,N),
sortx(X,N,M).
The predicate 'insert sort' will recursively call 1 cseli 46 until all the elements of L are picked out. Then by the first clause of 'insert sort', variable N will be instan-
tiated as an empty list. Afterward, the execution flow will return to insert the elements in order one by one. However, it is somewhat different from what the human reasoning is.
To make use of the computer, one must consider the
restriction imposed by the communication medium-- the pro- gramming language. Programming in Prolog is still not a
direct mapping of logic manipulation. Prolog is not a per- fect mean for programming in logic. The programmer should beware of the restriction imposed by the mechanism of the machine execution.
2.5.4. How Detailed an Idea should be ExDressei
In some cases, the problem does not only come from the
syntax of the language, but from the semantics of the
language. In this example, we try to search a graph by
finding if a node is a successor of another node. At first,
the fact of the graph are stated as,
adjacent(g,d).
adjacent(e,d).
They state that node g and node h are adjacent, node g and
node d are adjacent too, and so on. By definition, a node 47
is a successor of another node if it is adjacent to that node, or it is a successor of a node which is adjacent to
that node. So the Prolog program can be,
successor(A,B):- adjacent(A,B).
successor(A,B):-
adjacent(A,X),
successor(X.B).
However, it has two errors. Firstly, it is a non-
directed graph. Therefore, the adjacency relation should be
reflexive, however, the program does not reflect it. To
correct it, we replace the predicate adjacent(X,Y) in the
above two clauses by the new pattern
(adjacent(X,Y)adjacent(Y,X)). The semicolon means the
or' relationship. Secondly, even after the above correc-
tion, the program will go into an infinite loop. The situa-
tion is that when node A is a successor of node C which is
adjacent to node B, then node B is also adjacent to node C,
and A is a successor of node B too. So the process will
infinitely shift between these two nodes. To correct it, we
must mark down the node that we have already visited.
successor(A,B,T):- (adjacent(A,B)adjacent(B,A)).
successor(A,B,T):-
(adjacent(A,X)adjacent(X,A)),
not(member(X,T),
successor(X,B,[XI-T]).
Again, we notice the incongruity between the human rea 48 soning and the programming. The programmer must take care of the assumption in the definition which is constructed
implicitly in his conception, but is not stated explicitly
to the machine.
Along the increment of the complexity of the problem, more problem reductions are needed to divide the problem
into several smaller subproblems, and more imperative pro-
gramming techniques are needed to comply with the situation
that the problem cannot be easily declared. In the last
example, a condition will be met where the problem has to be
stated imperatively.
2.5.5. Is it Declarative or Imperative
A trait of imperative programming in Prolog is the
explicit database manipulation operation such as the
asserta' and the 'retract'. In this application, we want
to implement the 'findall' operator, whose function has
already been stated in section 2.5.2. The Prolog clauses
for 'findall' are as follows,
findall(X,G,):-
ass e r to (found (mark)),
call(G),
asserta(found(X)),
fail.
findall(, ,L):- collect- found([] ,L),! 49
The first clause will assert the predicate
found(mark), which serves as the bottom marker of a stack.
The interpreter will then try to satisfy the predicate
child(X,john) by calling G, which has been instantiated to
that predicate. For each X found, it will be asserted in
the database. This process will be forced to repeat by the
predicate fail, which makes the first clause of predicate
findall fail forever. The special point is that the
clause asserted will not be retracted even the process is
backtracked through the predicate asserta. When the
predicate child(X,john) can no longer be satisfied, the
first clause of predicate findall will fail. Then the
second clause will call the procedure collect found to
retract all the X found before, and collect their values in
L. This process will continue until the predicate
found(mark) is met.
In this program, the first asserta predicate cannot
be executed or placed after the next asserta. This is
clearly the result of the application of imperative program-
ming technique. Since it is impossible for a user to con-
struct a declarative explanation of this problem by which
the interpreter can understand the problem, the user must
show the operation steps to the machine and let them direct
its execution. It is believed that imperative programming
is much more difficult than declarative programming for the
common users. In this application, instead of exploiting
the logic of the problem, most of the user's effort is spent 50
in dealing with the programming issue, and how to remember the value of X and to retrieve this value. This is actually
not the main issue.of the problem, but the issue of program- ming. Thus it requires the non-programmer to have also the programming knowledge, but not only the logical reasoning power. 51
2.6. Conclusion
Certainly, Prolog has already been a significant evo- lution from the conventional programming languages. It works as an automatic programming system to support the com- mon users. However, as shown in the above examples, Prolog
is still not suitable for a non-programmer user to perform programming. The first problem is that programming in Pro- log is actually different from the ideal programming para- digm, the logic programming. Besides the mechanism of the logical assertion and deduction, the language is mixed with
other features such as the assignment and the execution order among clauses with heads of the same predicate, etc.
These features makes programming in Prolog more difficult.
Secondly, even in logic programming, there is difficulty in
constructing acceptable formal specification from concept
for the common users.
To overcome the first problem, different researches
try to implement the logic programming in a way different
from the Prolog programming. Some researches are going to
implement other logic programming languages [31]. Others
attempt to modify Prolog, such as the development of con-
current Prolog, in which the tree development will be done
in a parallel way and transparent to the users. Thus the
serial feature cut which seriously contempts Prolog as an
ideal logic programming language will be on its way out.
There are also other researches such as the development of 52 metalanguage which controls the execution of the object logic programs. This approach tries to separate the logic component of an algorithm from the control component more explicitly. Thus the implementation of the object logic programs can be easier [32].
For the second problem, computer scientists are still looking for forms of expression that are natural in the human's thought processes, so the user can represent his
action as nearly isomorphically to his thought as possible.
Some researchers try to merge logic programming with func- tional programming to find a more attractive form of pro- gramming which some call it as assertional programming
[331, which is believed to be a more suitable form to
achieve the declarative type of programming.
Presently, none of the researches can claim to be the most promising approach, and the difference between dif- ferent researches is still very large. Nevertheless, we believe that this variety of direction is certainly a guarantee of the future of programming for nonprogrammers. CHAPTER 3
DESIGN CONSIDERAION OF PROPSS
3.1. Introduction
In the last section, the difficulty of programming in
Prolog was discussed. So in order to make Prolog more
accessible to the common users, an experiment is performed
to design and implement a system to facilitate the program- ming. However, the approach of the undergoing project is not to continue the improvement of the language, but to
create an automatic programming support system, which is
based on a Prolog interpreter. It is called PROPSS for PRO-
log Programming Support System. It is implemented in the
Edinburgh Prolog System Version NU7 running under Unix in
the Department of Computer Science of the Chinese University
of Hong Kong.
In this experiment, not only a Prolog programming sup-
port system is constructed, but also a methodology of Prolog
programming is tested. Its general design consideration
will be discussed in this chapter. 54
3.2. General Consideration-- aim and scope
The aim of PROPSS is to help the user to perform pro- gramming. It is not going to improve any algorithm, or to optimize the execution time of any algorithm, as what is being done-in other researches [9,34]. It is an outer layer covering the Prolog interpreter, transforming what is stated by the user to a Prolog program. Therefore, it covers the programming part of the software development process only.
It does not help the user in constructing the specification, nor does it help the user in managing the files or data.
Prolog is a language which has functions similar to a deductive database system. As a database storing data, it is not rare for a user to have files storing assertion in a
Prolog system. Similar to the database query, the user will also want to create a Prolog program to deduce some results from the asserted facts. It is helpful to the user for a supporting system to have knowledge of the existing files,
such as when a file should be consulted, and what the prac- tical meaning an assertion has. The user can just type in what he wants, and the system can then automatically correct his program with the stated assertions. However, after con- sidering the difficulty of constructing such a system and the ease with which the user can give hints about the
defined data to the system, it is decided not to provide such kind of service. 55
Certainly, the programs generated by PROPSS will be written in Prolog, but the problem area of the system is not fixed in advance. Nevertheless, the problems such as rela-
tional database operation, logical inference and general
list processing can best be solved by the system. It is believed that the range of application field is mainly
determined by the expressive ability of the specification
method and that of Prolog. Though the expressive ability of
Prolog is fixed, the application field can still be enlarged
by improving the expressive power of the specification
method. So the system should be structured into several
modules, in which the interface module can be updated to
accept different types of specification, but still to pro-
duce the same type of output to the next module.
Finally, it must be stressed that the proposed system
will only relieve the user of the burden of programming, but
not the work of thinking. It is a tool for program
transformation, but not a general system for programming.
It does not provide the facility to check if the algorithm
described in the specification by the user is correct or
not. If the user types in an algorithm which is incorrect,
it is possible for PROPSS to generate a syntactically
correct Prolog program for the user, but surely it is not
what the user wants. It is the duty of the user to make
sure of the correctness of the reasoning. The user should
also make use of the facility of the operating system to 56 protect the specifications and created programs, since the system will not provide any protection to them. 57
3.3. Design Consideration of the Specification
The first task of the system is to accept the specifi- cation of the problem from the user. The specification language has to be natural and easy to use, and at the same time, better to be computer-intelligible. The function of
the specification language is not only a way for the user to express his idea. By the imposed restriction of specifica-
tion, the specification method should be a model of thinking which can guide the user to solve problems in a way that can be programmed in Prolog. Following the way (model) of
thinking, the user can type in the solving method of a prob-
lem in the format of the specification, the supporting sys-
tem will then transform it to Prolog clauses.
Imprecise natural language is the one most natural and
easy to use. Several researches have inclined to use
representation schemes for natural language based upon such
structures as semantic network, and this schemes can use-
fully be reformulated in symbolic logic [35]. There are
also some automatic programming systems which accept specif-
ication through natural language dialogue [36], one of them
is NPGS. Due to the rather narrow problem area of NPGS, it
can have a set of decoding rules to specify how strings of
text can be converted into defined records, which are infor-
mation about words and concepts relevant to simple queuing
problems. These rules are basically phrase structure gram-
mer rules, augmented with arbitrary conditions and structure 58 building actions.
On the other hand, precise formal language is easier to process directly by the system. C.J. Hogger has
developed a method of goal-oriented derivation of logic pro- grams [37], in which the specification is a set of sentences constructed by the logical symbols, such as
sublist(x,y) <->
(dk) (dui) (item(u, i+k,y) <- item(u,i,x))
However, the formal languages are difficult to learn. They are by themselves not very accessible to the common users.
How can they be expected to make Prolog programming more accessible to the users?
Within the past decade, several specification
languages, like Gypsy, have been developed. These languages
can provide a means for merging formal and informal descrip-
tions of a system. However, the resource limitation con-
strains the usage or the creation of a language of such
kind. To compromise between these extremes, the specifica-
tion language in PROPSS is prefered to be a quasi-natural
language. Its format is regular so that it can be precise
and unambiguous, but it allows the user flexibility in
explaining his idea.
In English, we have five basic sentence types.
Enriching these basic structures by the connective words,
the modifiers and the relative pronouns, etc., we can create
English sentences in a more prosperous way. Furthermore, 59
the expression of the same idea can be varied by different vocabularies. Due to the limitation of time, we have only tried the simplest English sentence structure, that is,
Subject- Verb to be- Complement
Similarly, we also accept the following sentence type,
Subject- Verb in passive voice- Object
Only the most primitive form is allowed. Therefore, the user cannot use these structures to form compound sentences
or complex sentences. Also the user should be prevented from using modifiers to modify the sentences. One of the exceptions is to use prepositional phrases, especially using the preposition of, to modify the nouns. However, it should be noticed that the complement should usually not be
substituted by a prepositional phrase.
An acceptable specification is then a paragraph ter-
minated by a fullstop. The sentences within the paragraph are limited to the English sentence types stated above. The
sentences are separated from each other by the semicolon.
The comma is retained for the user to improve the readabil-
ity of the specification.
The key-phrase for the user is simple is beautiful
It is believed that these sentence types are suffi-
cient for the users to express their ideas. The reason is
that Prolog is basically a-language for expressing relation-
ship between different objects. The sentence types and the
formation rules that are allowed in the system are actually 60
the most primitive way in English to express relationship.
The other more complicated English sentences can of course express more complicated idea, however, they can be broken down to several simpler sentences that can be expressed in the forms allowed in the system. Furthermore, it is believed that the set of specification formats can be enlarged if more and more reasoning types are modelled, that is, the system has more programming knowledge.
The system will also provide a certain set of vocabu- lary that the users are prefered to make use of. In case
the system meets a word that it does not understand, it will ask the user. The user can type in instructions to direct
the system to continue the derivation of the programs, or
the user can even type in a Prolog clause to help the system
in case the programming is easy for the human, but difficult
for the machine. If the unknown is an action for which the
system does not know how to perform, the user can just iden-
tify it as an operation and later define it to the system.
In the above paragraphs, only the restriction of the
syntax of the language is specified. (Of course, this res-
triction does imply restriction in the reasoning method.)
The semantics has also a little restriction in that when the
user wants to construct a program which specifies a method
of deduction, he should always be thinking that he is giving
a definition of the deduction to the system. The following
is an example of specification which states the deduction of 61
the subset relationship,
x is a subset of y provided
x is empty
else if every element of x is also
a member of y. where the words 'provided' and 'else if' are keywords for the determination of the meaning of the specification. The users are restricted to use them in their statements. For
the completness of the meaning of the specification, several forms are provided for these keywords. The phrase is defined as' or the word 'if' can be used instead of the word
'provided'. The words 'otherwise', in case', or 'whereas'
can also be used to replace the phrase 'else if'. The user can feel free to use these words or phrases whenever he
thinks they can represent his idea. The basic idea for this
design is that all these words or phrases are actually sig- nals of another conditional case for the problem. It means
that the system should have another Prolog clause for the
description that follows. Furthermore, when the specifica-
tion involves the concept that all the occurrences of a cer-
tain data object will be processed by a certain step, it is
restricted not to mix with the processing which is specific
to a single object. The user can overcome this problem by
splitting it into two specifications, where the first
specification deals with the single object, and has a call
to the procedure defined by the second specification. The
next procedure will then process all the objects by that 62
step.
On the other hand, when the user just wants to perform a query, he should not type in a question, but a description
of the relation that he wants to check. For example, the
specification can be,
X is a list of all the successors of Y.
instead of
Which are the successors of Y?
In the specification, the user can explain the relations
among objects by more than one sentence.
During the construction of the specification, the user
should adhere to a certain style of speech. For example, if
he has stated the relation
x is the f of y.
He should always adhere to this expression whenever he wants
to talk with this relation. If at a second time, he makes
the statment as
the f of y is x.
He will probably cause an error in the final Prolog clauses.
The users are not restricted to use any form of expression.
However, they are restricted to adhere to certain standard.
Nevertheless, the users are still encouraged to choose one
of the styles stated above as his usual way of expression.
Although the specification is acquired, the system
cannot use it directly as the representation model for the
derivation of the final program. If the specification 63 language is a formal one like that of Hogger's stated above,
the specification itself can logically be this model, like
the logical equations in the example. (of course, the prac-
tical representation of the model can be different. For
example, the model can be represented as a frame or a
script, provided they are supported by the based system.)
In PROPSS, the representation model is designed as a
simplified and transformed version of the original specifi-
cation. The input specification will first be simplified by
removing the articles, the prepositions and the relative
pronouns, etc. The system will then translate some words in
the specification to predefined keywords. Finally, the
specificaion will be rephrased to the representation model
by reversing the position of the subject and the verb-to-be,
that is similar to the form of a question. All the words
which modify the nouns are kept in their original order.
The only reason for this modification is to make the follow-
ing transformation process easier.
Finally, the model derived will be passed to the next
model of PROPSS for transformation. 64
3.4. Design Consideration of the Approach
One of the approaches to generate Prolog programs is the deductive method. A deductive program synthesis method is developed by Clark Tarnlund [38]. The basis of the method is a calculus where the data structures and the com- putable relations on them are axiomatized, and also the specifications of the executable objects are defined in first order predicate logic. Horn clause logic programs can
then be derived in natural deduction. It is equivalent to
the proof of theorems from the specification. However, due
to reasons similar to the choice of specification language, and the reason that there has already been an implemented
semi-automatic deductive computer system based on this method [39], this approach is abandoned.
The approach used in PROPSS is the knowledge-based
approach. This decision is based on three reasons. The
first one is related to advantage of knowledge-based
approach, that the rules in a knowledge-based system can be
implemented individually and independently. Since the
resources and time for the project are limited, it is hoped
that a prototype system should be implemented first, and
then more features can be added to it later when the
resources allow.
The second point is related to the aim of the experi-
ment. Besides the creation of a system, it is also hoped 65
that a methodology of Prolog programming can be derived. So the rule-form refinement is prefered since it is easier to review the refinement process by studying the rules involved.
Finally, the last point is basically an implementation consideration. After the preliminary feasibility study of
the system, it is decided to use Prolog as the implementa-
tion language. Since PROPSS is designed as a layer covering
the Prolog interpreter, the implementation of PROPSS will
then be restricted by the programming facility provided by
the underlying Prolog system. Besides Prolog, which is cer-
tainly one of the programming languages supported, the Unix
Prolog System has the facility for the user to call other
shell procedures and utilities. However, the linkage
between the utility or the product of the utility and the
Prolog system will become rather complicated. Furthermore,
it is believed that Prolog, or a derivative from Prolog,
will play an important role in the future intelligent
knowledge-based computer systems. So it could be a signifi-
cant attempt to implement the system. in Prolog. By this
decision, the knowledge-based approach to automatic program-
ming is chosen, since it is well known that Prolog is highly
suitable for the creation of rule based deductive expert
system.
As a conclusion, the transformation will be a
knowledge-based refinement which can be viewed as a tree 66 search. The root of the derivation tree will be a step to accept an explanation of some unknown term (the specifica- tion of a problem). The sons of the root will be the sim- plification and transformation steps. Afterward, the tree will spread to different branches with each new node as the result of applying a particular rule. Some leafs of the tree that are proved to be successful will be the Prolog clauses of the created program. The refinement process will finally be terminated by printing out the clauses. 67
3.5. vesign Consideration of the Knowledge Base
The most important component of the system PROPSS is the knowledge base which stores the rules that guide the refinement of the specification. In contrast to the knowledge base of PECOS, (an automatic programming system developed for Lisp programming,) in which most of the rules may be classified as general in the sense of being relevant to a variety of different programming languages,
all the rules in PROPSS are specialized for Prolog program- ming. The most important rule is the refinement rule which repeatedly applies different rules to continue the deriva-
tion until the result is tested to be a valid Prolog pro-
gram. In order to make the derivation proceed smoothly, the
system must logically have knowledge (rules) about the fol-
lowing items:
(1) Syntax of the language
(2) Pattern of program text
(3) Data abstraction
(4) Possible missed assumption.
Logically these four groups of knowledge are rather
independent. The knowledge of the first category is the one
that cannot be missed in any AP system. This knowledge of
the syntax of the language suggests the final statements
that can be written in a Prolog program and also serves as a
termination case for a refinement path. Usually, this type
of rules can be simpler than the others in that it works as
a mapping of a certain intermediate refined object to a 68 defined Prolog predicate.
The second set of knowledge constructs the framework of a program. According to the main control structure used in the algorithm, different patterns of program texts will be used. For instance, the 'condition' pattern, recognised as the existence of the key phrase is defined as, deter- mines if the program should be of the form either P. or
PO:- P1,P2,..,Pn., whereas the keyword 'otherwise' and others stated above determines if the program should have another clause. There are also other patterns such as
'all-element' pattern and 'all' pattern, etc.
However, a pattern does not mean a program. Starting with a main framework, the system must repeatedly refine the specification until it is a Prolog program. One of the necessary refinement is the implementation of data struc- tures. In Prolog, the only possible data structures are atom, list and structure. So the situation will not be too complicated. A single data will be represented by an atom.
A homogenous aggregated data will be represented by a list and a heterogenous one will be represented by a structure.
Ideally, the system should be able to determine the data
type of the objects specified by the user in the specifica-
tion. For example, the system should know that a set is a homogenous aggregated data structure, so it can be
represented by a list. However, it is impossible for the
system to have such a large knowledge base to remember the 69
data type of all the possible data objects. The method used in PROPSS is not to determine the type by considering the name of an object, but by the operation applied to it. If there is any request to get the 'head' or an 'element' of a data object, this object can certainly be represented by a list. In case that the user specify a certain 'field' or
'attribute' of an object, then most probably the object is a structure. Otherwise, as there is no special data manipu- lating function applied to the data object, it is assumed
that the object is an atom.
Another suitable knowledge for refinement is those
'possible assumptions'. For instance, in section 2.5.3, the
instruction only specifies that an element should be picked
from the list, but does not state which element. So the
system has to assume the equivalence between an element
and the first element. However, for these specific
equivalent relations, the system must ask for the user's
permission to make the assumption. If it is not permitted,
the system cannot substitute the old term with the new one
to continue the refinement. In the extreme case, this
equivalance assumption can be analogous to a realization of
the transformational methods studied in other researches
[40,41].
Another situation to apply this type of knowledge is
the case shown in the section How detailed an idea should 70
be expressed. The system has to supply the specification with assumptions that are not explicitly stated. However,
it is very difficult for the system to recognise when to
apply a specific assumption. Even the system recognise that
a specific assumption should be applied at a certain case,
it is very difficult for it to know at which point in the
refinement path the assumption should be inserted. One of
the possible solutions is to allow the user to control the
refinement process. As in the example, the system initially
derives the first set of Horn clauses (the incorrect ones).
By executing the program, the user finds out that it is
incorrect. However, he does not know what is the error,
therefore he can invoke the AP system to find another imple-
mentation again.. At this time, when the system suggest the
same result as before, the user can reject it as an unac-
ceptable one. The system will then try to refine the
specification again (search another branch of derivation).
Additional assumptions can then be added to the derived pro-
gram at different points of the refinement path. This pro-
cess is non-deterministic in that the system only tries
other implementation alternatives without knowing which is
the actual answer that the user needs. Certainly, this type
of refinements requires the user's permission too. On the
other hand, if the user knows the reason of the error, he
should start another transformation process in PROPSS and
use another specification which can yield the correct imple-
mentation. CHAPTER 4
EXAMPLES OF PROGRAM TRANSFORMATION IN PROPSS
In the last chapter, the concept of the system has been described. Before the discussion of the implementation
of the system, which is the content of the next two chapters, two examples of program transformation in PROPSS will be displayed in this chapter to illustrate the opera-
tion mechanism within PROPSS.
4.1. Example One-- Ordered Insertion
In case the user wants to implement an insertion sort
program, he is required to define a Prolog program to insert
a selected element orderly in an ordered list (i.e the
predicate 'sortx' stated in section 2.6.3.). So the user
types in the word 'transform' to PROPSS, and after he
answers the name of the problem, he is requested to input
the description of the problem. In his former description
of insertion sort, he has made the statement that m is an
ordered list of x and n, where m and n represent the
ordered list after and before the element x is inserted ord-
erly. The description is shown in listing one. It will
then be simplified by the system as shown in listing two.
(There are two execution steps, the first one condenses the
specification and the next modifies the keywords.) This sim-
plified version will further be transformed to the 72
Listing One
m is an ordered list of x and n provided
if x is greater than the first element of n then the first element of n will be the first element of m and the remain of m will be the ordered list of x and the remain of n
otherwise x will be the first element of m and n is the tail of m.
Listing Two
m be ordered list x n Drovided
if x be more head n head n be head m tail m be ordered list x tail n
if x be head m n be tail m.
Listing Three
proviaea be m oraerea list x n
be x more head n be head n head m be tail m oredered list x tail n
be if x head m be n tail m
representation model as shown in listing tnree.
There is a special point in the last transformation
which has not been discussed before. It is the position of
the word 'provided' and the addition of the first semi-colon
to the specification. As-it has been discussed before that
the system should identify whether the requirement of the
user is to create a Prolog program for deduction or just a 73
query of something. This is determined by the existence of
the keyword 'provided Therefore, the system has to first
identify its existence before continuing to identify the
structure of the sentences. Every sentence should finally
be converted to one or more than one predicate in the pro-
gram clause. As the head of the program clause is also a
predicate, its derivation is the same as the other. So the
first semi-colon is added to identify the head predicate.
The system will then start the refinement of the model
from the end of the specification to the beginning of the
specification. Actually, during the derivation of the
representation, the most recently derived sentence are
reversed and inserted to the head of the representation
list. As stated before, there is a list of the derived
terms. During the refinement, new created predicates will
be stored in it, and the objects waiting for refinement will
also be stored there temporarily. So, in order to simplify
the explanation, the refinement will be shown as the changes
in this list. The followings are the steps that change this
list.
Step 0:--> [sy_end].
Before the beginning of the refinement, the system
symbol 'sy_end' is inserted first. It is used for denoting
the information related to.the current sentence but not the
last processed sentence. 74
Step 1: --> [ m,sy_end]
The first word that the system encounters is 'm'.
Since it is an oject that the system does not recognize, it will ask the user if it is a data object in the problem. It is a list to be processed in the problem, so the user presses the carriage return to approve of it. The system
will then assert it as an object in the database, and push the word to the list.
Step 2:--> [tail (m), sy end].
PROPSS recognizes that 'tail' is a system defined operator. It will then gather the data objects (parameter) remaining in the list and form a predicate with the defined
operator as the functor and the objects as the arguments.
Step 3: --> [n, tail(m)].
Step 4: --> [sy_end,tail(n,m)].
The encounter of the word 'be' signifies the end of a
sentence. So the free parameters in the list should also be
arguments of the most recent predicate. This predicate
corresponds to the sentence n is the tail of m. The sys-
tem symbol 'sy_end' should also be repositioned to the head
of the list to denote the end of the last sentence.
Step 5:--> [m,sy_end,tail(n,m)].
Step 6:--> [head(m),sy_end,tail(n,m)].
Step 7:--> [x,head(m),sy_end,tail(n,m)] 75
Step 8:-- [x,head(m),ser(if) ,sy end.... I.
The word 'if' represents the beginning of another clause, so it should be placed at the very beginning of the sentence. However, for the ease of processing, it is tem- porarily stored at the position just before' sy end' and quoted as to be reversed (by syr) to the front position when the process of the current sentence is finished. This repo- sition operation is done by the rule dealing with the word
be. It is shown in the next step.
Step 9:-- [sy end,if,head(x,m),tail(n,m)].
Step 10:-- [n,sy end,if,...].
Step 11:-- [tail(n),sy_end,if,...].
Step 12:-- [x,tail(n),sy_end,if,...].
Step 13:-- [ordered_list(x,syvl),
syr(tail(syvl,n)),sy_end,...].
Similar to the recognition of the tail, the system
knows that ordered list is a functor, since it is stated as
the name of the problem. So the arguments are picked up
after the current functor. However, another predicate is
met before the end o f the sentence( t h a t i s before
'sy end'). The system will then generate a system variable
'syv#' where# means a random number, and insert it in the
proper positions of the predicates. It should be noticed
that the predicate which has. already been stored in the list
will also be quoted as to be reversed later. The reason is
that it is a modifier of an argument of the current 76 predicate, so it should be operated first to generate an instantiated argument for the current predicate.
Step 14:-- [m,ordered list(x,syvl),
syr(tail(syvl,n)),sy end,...].
Step 15:-- [tail(syv2,m),syr(ordered list(syv2,x,syvl)),
syr(tail(syvl,n)),sy end,...].
The construction of predicate tail is similar to
that of 'ordered list'. The exception is the placement of
the system generated variable. Actually, in PROPSS, the
data words, such as 'tail','head' and 'structure', etc.,
have a fixed format of expression internally. This format
is fixed as object A is the tail or head of object B, but
not as the tail or head of object B is object A. However,
the user does not have this restriction in his specifica-
tion, so the system will check for it and insert argument in
the correct position as shown above.
Step 16:-- [sy_end,tail(syvl,n),ordered_list(syvZ,x,syvl),
tail(syv2,m),...].
The verb-to-be works as it is stated before.
Step 17:-- [m,sy_end,...].
Step 18:-- [head(m),sy_end,...].
Step 19:-- [n,head(m),sy_end,...].
Step 20:-- [head(syv3,n),syr(head(syv3,m),sy_end,...].
Step 21:-- [sy_end,head(syv3,m),head(syv3,n),...].
Step 22:-- [n,sy_end,... I. 77
Step 23:-- [head(n),sy end,...].
Step 24:-- [more(syv4),syr(head(syv4,n)),sy end,...].
Step 25:-- [x,more(syv4),syr(head(syv4,n)),sy end,...].
Step 26:-- [sy_end,head(syv4,n),more(x,syv4),...].
Step 27:-- [n,sy end,head(syv4,n),more(x,syv4),... J.
Step 28:-- [x,n,sy end,...].
Step 29:-- [ordered list(x,n),sy end,... J.
Step 30:-- [m,ordered list(x,n),sy end,... J.
Step 31:-- [sy end,ordered list(m,x,n),...
Step 32:
-- [ordered list(m,x,n),:-,
head(syv4,n),more(x,syv4),
head(syv3,m),head(syv3,n),
tail(syvl,n),ordered_list(syv2,x,syvl),
tail(syv2,m),'.',
ordered list(m,x,n),:-,
head(x,m),
tail(n,m),'.'].
The last step is performed by the word' provided'. it
will insert the symbol':-' after the first predicate and
insert the fullstop, the head of clause and that symbol to
the beginning of another clause which is denoted by the key-
word' if'.
These predicates will then be classified as control
structure group, data structure group and program group.
Since there is no special control predicate in the list, the 78
control structure group will be empty. After filtering the data modification predicates, the program group will be,
[ordered_list(m,x,n),:-,
more(x,syv4),ordered_list(syv2,x,syvl),'.',
ordered_list(m,x,n),:-,'.'].
As stated before, the two clauses are actually processed
individually. For the first clause, the related data predi-
cates are
[head(syv4,n),head(syv3,m),head(syv3,n),
tail(syvl,n),tail(syv2,m)].
After passing the control structure refinement, (in
this example, there is not any change), PROPSS will refine
the clause according to the data predicates sequentially.
Step 33:-- [ordered-list(m,x,[syv4lnt]),:-,more(x,syv4),
ordered list(syv2,x,syvl),...].
As it is stated by -head(syv4,n) that syv4 is the
first element of 'n', so 'n' will be replaced by '[syv4lnt]'
where 'nt' represents the tail of n. For the processing of
data structure predicate, a special point is that the same
data modification expression will be stated more than once
in the specification. In the example, the user repeats to
state the first element of 'n'. The system will generate
different data predicates for them, however, they actually
mean the same element. So the data predicates will try to
check and delete any other data predicates which are
equivalent to them. As in the current step, the predicate 79
'head(syv4,n)' will cause the elimination of the predicate
'head(syv3,n)'. However, the variable stated in the other predicates, the instance is 'syv3', will be refered to by the other terms in the clause. So a new data modification predicate is appended to the list to set these two variables equivalent. In this example, it is 'setto(syv3,syv4)' which means 'syv3' is set to 'syv4'.
Step 34:-- [ordered_list([syv3lmt],x,[syv4lnt]),:-,
more(x,syv4),ordered list(syv2,x,syvl),..].
Step 35:-- [ordered_list([syv4lmt],x,[syv4lnt]),:-,
more(x,syv4),ordered list(syv2,x,syvl),..].
In the last step, the predicate 'setto(syv3, syv4)' ic.
processed.
Step 36:-- [ordered list([ syv4lmt] ,x, [syv4) syvl]),:-,
more(x,syv4),ordered list(syv2,x,syvl),..],
Step 37:-- [ordered list([ syv4l syv2] ,x,[ syv4) syvl]),:-,
more(x,syv4),ordered list(syv2,x,syvl),..],
In the last two steps, the predicates tail(syvl,n)' and 'tail(syv2,m)' are processed. The 'nt' and 'mt' will be replaced by 'syvl' and 'syv2' respectively.
After the refinement of the data structure, the system will proceed to refine the functor of the clause.
Step 38:-- [ordered-list([syv4lsyv2J,x,[syv4lsyvlj),:,
x syv4, ordered_list(syv2,x,syvl),...]. 80
The functor refinement process will scan through all
the predicates in the program clause, and modify the predi- cates that it recognizes. Here, it modifies the predicate
'more' to the form suitable for Prolog.
Similarly, the next clause will be derived as,
[... ,ordered([xln],x,n),:-,'.'].
where the symbol':-' will be dropped out by the model which prints out the clauses. The final Prolog program can then be
ordered_list([syv4lsyv2],x,[syv4lsyvl]):-
x syv4,
ordered 1ist(syv2,x,syv1).
ordered_list([.xln],x,n). 81
4.2. Example Two-- Ordered list
A user wants to check if the element of a list is ordered. Listing four is the description he gives, and listing five will be the representation model of the specif- ication.
Listing Four
A list is ordered if all the elements of the list are less than the next element in the list.
Listing Five
provided be list ordered be all ele list less next ele list
The key phrases all the element of and the next element will be recognised by the system and modified to the single keywords. Though the data object is called
'list', however it does not affect the operation of the sys- tem, since the type of an object is determined by the action not the name. For simplicity, the earlier refinement steps will be skipped. It is assumed that the model has already been refined to the state shown below,
[ordered(list),:-,
next ele(syvl,list),less(syv2,syvl),
all ele(list),head(syv2,list),'.']
After the classification, the control structure grout contains the predicate 'all_ele(list)', the data structurE 82
group has the predicates 101next_ele(syv1,list)' and
'head(syv2,list)', and the rest belongs to the program group. The steps of the following refinement are shown below.
Step 1:-- [ordered(list),:-,
less(syv2,syvl),ordered(list t),'.'].
The key phrase all element will actually produce two predicates, the 'all ele(list)' for control and the
'head(syv2,list)' for data modification. The predicate
'all ele' will append the modified head to the list as shown above, where 'list t' means the tail of the list. It will also create another clause, ordered([]) in this example, which will be placed in front of the current clause.
Step 2:-- [ordered([syv2llist_t]),:-,
less(syv2,syv1),ordered(list t),'.'].
Step 3:-- [ordered([syv2,syvlllist_t]),:-,
less(syv2,syvl),ordered([syvlllist_t]),'.'].
In step 2, the clause is modified according to the
'head' predicate, and in step 3, it is the 'next_ele' predi-
cate. Finally, after the refinement of the functor, the
Prolog clauses derived will be
ordered([]).
ordered([syv2,syvl l list_t]):-
syv2 syvl,
ordered([syvillist_t]). CHAPTER 5
KNOWLEDGE BASE FOR ELEMENTARY PROLOG PROGRAMMTNa
As it was stated before, there are logically four types of knowledge or rules in the knowledge base. Practi- cally, they are dispersed in the different phases of refine- ment. For the ease to understand the operation of the sys- tem, the sequence of the rules to be discussed will follow the sequence of its appearance in the different phases of refinement. In this chapter, the rules will only be presented in English. The actual Prolog coding of them will be shown in appendices I to IV.
5.1. Rules for Formation of Predicates
The most important rules in the first phase are the rules to deal with the verb-to-be, the word 'provided' and the operation stated in the specification. For the simpli-
city of discussion in the following paragraph, the phrase current word will be used to identify the current word in the specification which is under consideration by the current step of refinement. The word 'list' will mean the list of predicates which is constructed by the refinement.
Also, the action inserts something to the list will mean the construction of a new list with something as the head and the original list as the tail. It is also assumed that the current word and the elements of the list which are 84
unaer consideration will be dropped after refinement, except when there is any explanation.
Rule BE.1.1
If the current word is 'be' and the head of the list is 'sy pro', then moves the symbol 'sy end' to the head of the list.
This is for the case that a Prolog clause is inserted by the
user to the list of predicate. The system will only ignore
all the objects of the current sentence which are waiting
for being processed.
Rule BE.1.2
If the current word is 'be', the first element of the list is any data object X, the second element is a number N, and the third element is 'sy end', then inserts setto(X,Y)' to the list and moves 'sy end to the head of .the list.
Since the assignment of a number can be implemented in Pro-
log by using the pattern matching facility, that is using
the argument to return the value, it is arranged here for
the coming data modification phase.
Rule BE.1.3
If the current word is 'be', the first element of the list is any data object X and the second element is an expression E, then inserts 'X is Y' to the list and move 'sy end' to the head of the list.
Rule BE.2.1
Otherwise, if the current word is be ana the neau Ul the list is a data object, then keeps the current word and stores the data object in another temporary list. 85
Rule BE.2.2
Otherwise, if the current word is 'be' and the head of the list is a predicate, then makes the objects in the temporary list-as the additional arguments of this predicate, and repositions any predicate which is to be reversed until the symbol 'sy end' is met. The symbol 'sy end' will then be moved to the head of the list.
The predicate which is needed to be reversed will be marked
by the predicate syr' as shown in the example in section
4.1.
Rule FUNCTOR.1.1
If the current word is an operation and the head of the list is a parameter of the problem, then keeps the current word and stores the parameter in another tem-
porary list.
The current word is an operation if it is either a system
defined operation. or a user defined operation. The latter
includes the current program which is defined by the user.
A parameter of the problem represents either a number or an
object of the problem.
Rule FUNCTOR.1.2
If the current word is an operation and the neaa or the list is the system symbol 'syend, then keeps the sym-
bol 'sy end' and inserts to the list the predicate whose functor is the current word and whose arguments
are the elements of the temporary list.
Rule FUNCTOR.1.3
If the current word is an operation and the head of the list is an operation too, then generates a system vari-
able keeps the latter predicate but inserts the system variable to it as its first argument and inserts to the list a predicate whose functor is the current word
and whose arguments include the elements of the tem-
nnr_arv list and the system variable. 86
In the rule FUNCTOR.1.3, the system variable will be the last argument of the inserted predicate. Actually, there is another set of rules for functor which will place the system variable as the first argument of the predicate. These latter set of rules are used for the data word such as
'head' and 'tail', etc. An example to use the second set of rule FUNCTOR is the Rule CLAUSE.
If the current word is a clause, then keeps the current word and calls the rules FUNCTOR.2.
Rule PROVIDED
If the current word is 'provided', then inserts the symbol':-' as the third argument of the list drops the head of the list which should be 'sy end' and modifies the list by changing every occurrence of the word 'if' to a fullstop, a predicate which is the head of the current list of predicates and a symbol':-'.
This rule belongs to the category of the pattern of program.
The previous two sets of rules are very difficult to clas-
sify as members of a particular category. If it is neces-
sary to classify them, they can only be viewed as the rules
to mechanically construct the clauses, so they are the rules
for the syntax of the program. In the following, examples
of the rules in the category of the data structure and the
assumption will be shown.
Rule ELEMENT.1
If the current word is 'element', then assumes its meaning as the first element, and asks permission if
it is permitted, calls rules HEAD. 87
Rule ELEMENT.2
If the current word is 'element', then assumes its meaning as any element, and asks permission if it is permitted, calls rules MEMBER.
Rule HEAD.1
If the current word is 'head' and the first element of the list is an operation, then keeps the current word, and calls rules FUNCTOR.1.
Rule HEAD.2
If the current word is 'head' and the first element of the list is a parameter, then keeps the current word, and calls rules FUNCTOR.2.
Another example which shows the assumption of processing is
the rule 'all-element
Rule ALL ELEMENT
If the current word is 'all element', then assumes the element will be processed in sequential order and asks permission, if it is permitted, calls rules HEAD.
Besides the above rules, there are three other rules which
need to be explained in this phase of refinement.
Rule IGNORE.2
If the current word is the word that can be ignored when it is followed by an operation and the head of the list is an operation, then keeps the head of the list
only.
One of the words of this kind is the word 'result This
rule is used for the case as something is the result of
removing..., in which the word 'result' should not have
any effect on the final program. There is also another
Ignore Rule which will ignore all the words that stated to 88
be ignored by the user.
Rule REPLACED
If the current word is 'replaced', then keeps the current word, inserts the predicate syr(sy_rep) to the list, and calls the rules FUNCTOR.1.
The word 'replaced' has a special meaning in PROPSS, which denotes an assignment or a replacement of some original lists or structures. (The word 'is' can be used for the assignment of number.) This invokes a program structure similar to that of the example 'collect-list-2' shown in section 2.5.2. Since this replacement should only affect
the predicates after this word, so the symbol 'sy_rep' is
placed here to denote its original position. (The predicate with the functor 'replaced' will be filtered as a control
word later.)
Rule FUNCTOR-MODIFIER
If the current word is a tunctor moalrier, Lnen mar6
it to be reversed, and moves it to the position just after the first predicate in the list.
The functor modifiers include the words all ana nOL,
etc. They will only affect the first predicate after them,
so their only argument is that predicate but not the other
parameters that can appear after them. Therefore, the rule
will skip all the elements of the list until the first
predicate. 89
5.2. Rules for Refinement of Control Structure
The system will apply the predicates in the control structure group one by one to the list of predicates which are constructed by the rules in the previous phase of refinement. For the clarity of explanation, the pattern for which the rule is applied will be displayed after each rule is discussed below. There are totally seven control rules defined in the system.
Control-Rule ALL-ELEMENT
If the control word is 'all-element'.., and 'X' is the argument of it, then appends a new predicate to the list. The new predicate is constructed by substituting the 'X' by 'X t' for all its occurrence in the head of the list. Also, creates a new clause which has only the head of the current clause but substituted the 'X'
by '[]' for all its occurrence in the head.
Pattern Example:
Ordered([]).
Ordered([X,YIL t]):- X Y, Ordered([YIL t]).
('nn trnl -Rtt1 a AT.
If the control word is 'all', then appends the head of the list to the end of the list. Also, creates a new
clause which is only the head of the list.
Pattern Example:
all retract(X):- retract(X),all_retract(X)
all retract(X). Control-Rule LIST_OF_ALL_ELEMENT
If the control word is sy lae' and its arguments are 'X', 'Y' and 'Ele', then appends the head of the list to the end of the list, and substutites the 'X' and 'Y' in the head of the list by Also, creates a new clause which is only the head of the list but substituted the X and 'Y' by'[], and also creates another new clause whose head is the head of the list but substi¬
tuted the 'Y' by'[ E1 e| Y]', and its only predicate in the body is the head of the current list.
Pattern Example:
Large r__l i s t([],[], X).
Larger___lis t( [Ele|Llist],[ Ele| List] ,X):-
Ele X,
Large r_list(Llist,List,X).
Large r__list(Llist,[Ele|List],X):-
Large r__l ist(Llist,List,X).
Control-Rule CLAUSE.1
If the control word is 'clause', with the arguments and the operation in the program is to read; then deletes all the predicates in the control structure group whose functor is 'clause', with the arguments 'Z' and 'Y', where 'Z' represents any argument; and appends the
predicate 'setto(Z,X)' to the list. Also, create a new clause which is going to open the file Y for input.
Pattern Example:
Consult(File):-
seeing(Input) ,see(File),
Consult1(File),
seen, see(Input).
Consul11(File):-... There is another control rule for clause which is simi¬
lar to this one, but deals with the case for output. So the operations in the additional clause will be to 'tell' a file instead of to 'see' a file.
Control-Rule REPLACE.2
If the control word is 'replaced' with the arguments 'X', which is not a system variable, and 'Y', then sub¬ stitutes all the occurrence of 'X' after the symbol 'sy_rep' in the list by 'Y'; and modifies all the occurrence of the predicates, which have the same func¬ tor as the head in the list, by adding a new argument.
Also, creates a new clause and modifies any existing additional clause of the current clause.
Pattern Example:
Collect_2(L) Collect_a([],L).
Collec t__a( L, Ln)...., Col lec t_a( LI, Ln)
Collec t__a( L, L).
In the above example, the second clause is the current clause which has originally only a single argument in the predicate 'Collect a'. It has been modified to two argu¬ ments. However, in order to be compatible with other clauses defined by the user which will call these clauses,
the first clause is created. Also, for the third clause, which is an additional clause created by another control
word of the current clause, it will be modified too.
Although seven rules are used in this phase, only the
above six rules are relevant to be considered as the rules
for the general pattern of program. The other rule of replaced, which is used for the retraction and assertion of predicates in the database, should only be considered as a functor modification word. However it is considered in
this phase, because the word replaced' will be classified
as a control word no matter what the meaning this word has.
This can only be checked when its rule is invoked. _5._3. Rules for RefInement of Data Structure
After the generation of the pattern, the system will
modify the data structure. These rules are mainly for the
manipulation of the list. The other data structures like
file and structure are usually manipulated explicitly by the
predicates in the clauses, it is sufficient to handle them
by the refinement of the functors of those predicates.
Data-Rule SETTO
If the data word is 'setto' with the arguments 'X and 'Y', then substitutes all the occurrence of 'X' (in the list) by' Y.
Data-Rule EQUAL
If the data word is 'equal' with the arguments 'X' and Y, then substitutes all the occurrence of Y by 'X'.
The above two data-rules deal with the content of an atom,
in the following, the rules deal with the structure of a
list.
Data-Rule EMPTY
If the data word is empty' with the argument X, then substitutes all the occurrence of X by[]'.
Data-Rule HEAD
If the data word is 'head' with the argument 'X and 'Y' then deletes all the predicates in the data struc¬
ture group whose functor is head, with the arguments Z and 'Y', where Z' represents any argument; appends
the predicate 'setto(Z,X)' to the list; and substitutes all the occurrence of Y by [X|Y t]. Data-Rule TAIL
If the data word is tail, then changes its functor to tail_l and appends it to the end of the data struc¬ ture group.
The similar rule is for the data word 'next_ele'. Whenever these data words appear, it is relevant to assume that the user must also state about the head of the list in the
specification. However, the order of appearance of these words and the word head will be very complicated, so in order to simplify the process, their executions are post¬ poned to the end of this refinement phase.
Data-Rule TAIL_1
If the data word is tail_l' with the arguments 'X' and , then substitutes all the occurrence of Y__t by
Data-Rule NEXT_ELE_1
If the data word is 'next_ele_r with the arguments X' and 'Y', then substitutes all the occurrence of 'Y_t'
by '[X| Y_tK. _3 ._4• Ru 1 e for Ref inement of Functor
Finally, the rules in the last phase of refinement
will be discussed. These rules are simpler. Usually, they
only corrects the functor or the argument of the predicate
in the list. The following is an example of them,
Functor-Rule WRITE.1
If the current predicate in the list has the functor 'written' and two arguments, then the current predicate in the list should have the functor 'write' and with
only the first argument of the original predicate.
Besides these simpler functor-rules, there are several
rules which are so special that explanation is needed.
Functor-Rule NOT
If the current predicate in the list is 'not', and the
predicates in the list after it have already been refined, then the current predicate in the list should have the functor 'not', and with the next predicate in the list as its only argument. The next predicate should then be dropped.
The rule must first call the refinement rule to continue the
refinement of the other predicates until the end of the
list. With the next predicate in the list refined, the rule
will take it as the argument of the predicate not to form
the negation. Functor-Rule CONCATENATE
If the functor of the current predicate in the list is concatenate', then modifies the arguments so that each
of them represents a list, and forms a list of 'append' predicates with them as the arguments to replace the current predicate.
This rule is for the concatenation of a group of lists. The first list is constructed by assuming each argument besides the first argument as a list. If this assumption is not permitted, the argument will then be assumed as an indivi¬ dual element and stored in a temporary list. Otherwise, this argument will be appended to the temporary list. The list of 'append' predicates will then append these arguments to some temporary variables and finally to the first argu¬ ment of the original predicate.
Functor-Rule STRING CONC
If the functor of the current predicate is
'string cone', then forms a list of 'name' predicates which transforms the individual argument to a list of characters, and then forms a list of 'append' predi¬ cates to group together these lists, the final list will then be transformed back to a string by a 'name'
predicate. All the predicates constructed so far will be used for replacement of the original predicate.
This rule will make use of a function defined in the previ¬ ous 'concatenate' rule. It is going to concatenate a group of strings. During the formation of the list of 'name' predicates, the system will ask the user if the argument represents a number or not. In the former case, the system will use 'namelnt' predicate instead of the name predi- ca te. 97
Actually, there are still a lot of rules used in these phase, however, a full explanation of them will affect the clarity of this article. So they are just skipped here. CHAPTER 6
IMPLEMENTATION OF PROPSS
Besides the construction of the rules, the other main issues in the implementation of the system is the construc- tion of the interface and the central controller which applies the rules to refine the specification. In this chapter, these issues will be explained, and part of the actual coding will also be elaborated. Other Prolog coding of the system will be shown in appendix V.
6.1. The Interface Module
As stated in the previous chapters, the system will
first accept a specification, and along the derivation, it will frequently ask for suggestion and permission from the
user. Therefore, the system has a special interface module
to accept the specification, and another interface module to
interact with the user.
The interface module will use a colon as a prompt.
The user can call shell procedures through this interface by
typing the single quoted shell commands followed by a
fullstop. He can also break to the Unix Prolog System by
typing 'prolog' or exit to it by typing 'exit' (not quoted,
but also terminated by fullstop.) If the user wants to con-
struct a program, he can type in 'transform'. The system will respond by asking for the name of the problem and the description of the problem. The specification acquired will then be transformed to the representation model which will be passed to the next module for refinement. The coding of this interface module is shown in listing six.
Listing Six
run(propss) see(t ty), tell(tty), repeat, interface. interface
write(':'), read(Command), invoke(Command)3 !, fail.
invoke(transform)
interact('name of the problem:', Titles), asserttl(Titles), log(start),log(Titles), interact('description:', Despt),log(Despt), condense(Despt,Cdespt),log(Cdespt), modify(Cdespt,Fdespt),log(Fdespt), derive(Fdespt,Derform),log(Derform),!,
As shown in the listing, the clauses 'run(propss)' and
'interface' will repeat to read and process commands. The command processed will not be retried because of the 'cut operation before 'fail'. The clause 'interact' will print the first argument and read a passage which is terminated by a fullstop. The articles, the prepositions, the pronouns, the auxiliary verb and several words like than and fol lowing' will then be removed from the specification by the clause 'condense'. After that, the system will use the clause modify to change some keywords in the condensed
specification. An example of this modification is the
change of the phrase next element to the word next ele.
Another example is the change of the word are to the word
Finally, the clause derive will transform the
specification to the representation model whose format has
been discussed before.
The representation model is implemented by a list.
Originally it was designed to be implemented as a set of
goals asserted as structures in the database of the Prolog
system. The final decision is based on the idea that PROPSS
should make use of the backtracking facility of Prolog, and
let the user occasionally control the direction of program
transformation. As a result, the system cannot assert any¬
thing in the database, since it is not automatically
retracted when backtracking is being taken.
Moreover, for the user and the constructor of the sys¬
tem to check if the system performs correctly. The action
of the system will be recorded in a log file. It is done by
the log clauses shown in listing six. After the user has
keyed in the possible names of the problem, the log writer
will construct a log file with the first name provided by
the user and concatenate it with .log. Then the name and
the description of the problem, the simplified version or
specification, the representation model and the results of
the successful applications of rules will all be recorded in i t
During the refinement of the representation model, the
user will notice some sentences being output on the termi¬ nal. Usually, they are just information to the user. In
case the sentence is ended by a question mark, it is a
request for ensuring certain assumption or derivation. The
user's response can be one of the four characters 't', 'a',
'r', and 'n', or just a carriage return. A carriage return
means an approvement of the information. The character 't'
means to trace the next execution steps, and the character
'r' means to run and returns it to notrace. The character
'a' is used for aborting the execution. For all the above
three inputs, the system will continue to prompt the ques¬
tion mark asking whether the assumption is permitted. In
case if the user rejects the assumption, he can key in the
character 'n'. The system will ignore all the other charac¬
ters.
Finally, after the derivation of the program, the last
interface module will be called by the refinement controller
to print the clauses in a pretty format. It is the clause
'print clause' shown in the next section. The Refinement Controller
A small but very important module is the refinement module. Its coding is shown in listing seven
Listing Seven
invoke(transform)
refine(Derform,[sy_end],Clause). p rin t c lause(Clause), log(Clause), log(finish),
re tract_user_info.
refine([] ,Predicates, Clause)
f ur ther_re fine(Predicates,Clause). refine([Word|Spec] ,Predicates,Clause):-
rule(Word,Predicates,Refined_P), log(Re fined_P), refine(Spec,Ref ined__P, Clause).
f urther__ref ine(Predicates, Clause):- locate.' predicates, Cur__P, 0 ther_P), classify( Cur__P, Da ta,Control,Program), control_refine( Control program, C__refined, [], Add__Clause), data_refine(Data,C_refined,D_refined), f unc tor__r e f ine( D_ref ined, Re fined), furtherref ine( Other_P, 0 ther__Ref ined), appendcl(Add Clause,Refined,0ther_Refined,Clause),
It is called by the interface module. The refinement module will invoke the different types of rules in the knowledge base in different order to refine the model. The
first set of rules to use is for the formation of the struc¬
ture of the terms of the Prolog clauses. Rules are called
according to the words in the representation model. The
refinement module will first scan through the model, to pick
up each operation word and associate it with the correspond¬
ing arguments, to form a predicate. For the special words 103 that it can recognize, such as the words 'all' and 'not',
etc., the system will associate the corresponding predicate with them. This is done by the loop of 'refine' and the application of 'rule
After all the words in the specification have been refined to a list of predicates, the system will, by the
clause 'locate', separate this newly created list of predi- cates into several parts according to the position of the fullstop. Each part represents an individual Prolog clause.
For each clause, the refinement module will 'classify' the predicates, which actually determines the structure pattern of the program or which determine the data structure of the objects, from the ordinary predicates. After this classifi- cation, the 'control refine' clause will call the corresponding control rules to set up the general pattern of
the program text. It will then be followed by the applica-
tions of the data rules which are called by the
'data refine' clause. Finally, the refined clauses will
pass through the 'functor_refine' step, by which all the
created predicates will be checked and corrected to the form
which is suitable for the Prolog Interpreter. Along the
refinement by the control rules, new clauses can probably be
produced. I t will then be either inserted before the
current clause, or appended after it by the clause
'appendcl'0
After all the clauses are retinea, tney Will uc displayed to the user on the terminal, and also stored in the log file, but they will not be asserted directly by the system into the database.
Before the finish of the transformation process, the system will clear up all the user's information. This information is stored as assertions in the database, and can be retrieved by retracting the assertions or just trying to prove them. However, they are different from the 'property' rules or the 'query' rules in PECOS. They are only informa¬
tion about the user's specification, such as the words in
the specification that should be ignored, the name of the current problem, the names of the parameters of the problem,
the name of the log file and the names of the temporary variables and functions. Whereas the 'property' in PECOS is actually the information of the refinement nodes. In
PROPSS, it is restricted not to generate too much informa¬
tion of the refinement itself. If it is needed, the infor¬ mation will usually be stored in the list of predicates that
is under the transformation, or it will be stored in another
temporary list. It will not be stored as assertion in the
database, because of the same reason as that in the imple¬
mentation of the representation model.
After that, the system will restart to accept command
from the user. CHAPTER 7
EXPERIMENTAL RESULTS AND EVALUATION
In order to test the capability of the implemented
system, sample specifications are feed into the system.
Although the programs constructed can successfully implement
the -required applications, the shortcommings of the system can also be percieved. In this final chapter of the thesis, several sample tests will be presented and the remedy of the shortcommings will also be discussed. Full listings of the
terminal sessions and the log files of these sample tests will be shown in appendix VI.
7.1. Sample Programs
The first two samples tested are the calculation of
factorial number and the proof of a successor relationship
in a graph. Both problems are simple because their defini-
tions are clear. They are only used for demonstrating the
feasibility of the methodology used in the experiment.
The second set of samples tested includes a proof of
subset relationship, the predicate 'collect-list-l' shown in
section 2.5.2, and the program which tests if the elements
in a list are in order as shown in section 4.2. Since all
of the problems involve the manipulation of all the elements
in a list, the system is required to refine their control 106 structures and data structures. Therefore, the problems are more complicated.
The third sample is the problem of Hanoi Tower. Since
it is a very practical application that involves a descrip-
tion of some physical objects, the specification will not be
so clear as those in the above tests. The system will find
it difficult to fully understand the specification. How-
ever, it is a very easy task for the user to eliminate those
useless information in the specification. Therefore, in
this sample, the system always asks for help from the user.
There have been instances that the user tells the system to
ignore some information or even writes the needed predicate
for it. The difficulty of this problem does not come from
the internal relationship among the objects of the problem,
but from the expression of the problem. In this problem, it
actually involves the implementations of two predicates.
The fourth set of samples solves the sorting problem.
It includes an insertion sort and a quicksort. Compared
with the above problems, both of them are more complicated
in that both of their definitions require predicates which
must be defined later. They are used for testing the data
refinement rules and the control refinement rules.
The fifth sample implements the predicate 'gensym-
defined in [291. It .is used for testing the replacement
concept and the functor refinement of the system. 107
the sixLn sample becomes more difficult, it tries to implement the predicate 'collect-list-2' stated in section
2.5.2. It is used for testing the replacement concept too.
The last set of sample is not the most difficult prob- lems. They are just used for checking the correctness of structure manipulation and file manipulation. It also involves the testing of the 'all' control structure.
Besides the samples stated above, the system is also tested by other sample problems, but they will be skipped in this thesis. 108
7.2. Evaluation of PROPSS
Before the physical implementation of PROPSS, it was first necessary to have a methodology to construct a Prolog program mechanically. In fact, at least a quarter of the time spent on this project was used for this purpose. The method deduced includes the concept of the specification language which is stated in section 3.3, and also the con- cept of the control structure such as the concept of replacement and the 'all' relation. This method is very satisfactory in that the author not only uses the method to construct programs for new applications, but also gets a new
idea of Prolog programming through the implementation of application programs. Based on this methodology, the system
implemented is proved to be satisfactory as shown in the
above applications.
Moreover, through the implementation of PROPSS, the
advantage of combining the VHLL and knowledge-based approach
to automatic programming becomes apparent. The high level
conceptualization of a VHLL reduces most of the difficulty
that would be met during the implementation of an even
higher level automatic programming system. The most signi-
ficant effect in PROPSS is the simplification of the natural
language interface module. The mechanism behind Prolog lets
the module to abstract only the relationships among dif-
ferent objects and operations in the problem, but not all
the semantics related to them. On the other hand, the knowledge-based system permits109the VHLL a more flexible working capabilty and outlook to the user, and as powerful
as an upgraded language of the VHLL. Besides, the most
important advantage is that through the study of the pro-
gramming rules of the VHLL, the researchers can have a
deeper understanding of that language. This better under-
standing will in return induce further improvement of the
language.
However, there are also some drawbacks in PROMS. The
first point is that the system does not have a complete
understanding of the program. Actually, at the very begin-
ning, the system is conceived as a process to check a
variety of refinement parameters such as the number of argu-
ments and the validity of the arguments of a predicate.
Through this refinement parameters, the system can be con-
sidered to have primary understanding of the program. How-
ever, it is very difficult to deal with the large variation
among different problems, so the mechanical method is used
presently.
Secondly, the most serious drawback is the limitation
of the expressive power of the specification language. In
some applications, such as those involving the file manipu-
lation, the problem area is very narrow that a user can only
either read a file or write a file. (The operation to the
data object after it is read from or before it is written to
a file is not file manipulation.) So the system can have a 110 formatted way for the user to express his idea without any
inconvenience. (Actually that format is the most natural way to express the idea.) However for the applications that
involve structure manipulation, the problem comes as there are too many reasonable and natural ways for the user to express his idea. The most natural way to express the con- struction of structure is to use a complex sentence that comprises more than one relationships. However, the specif-
ication language does not allow complex sentence. It is a kind of limitation introduced by the system but not an
inherent characteristic from the problem.
The limitation of the expressive power of the present
specification language also comes from the ambiguity of the natural language. The following is an example,
the ordered list of the first element of x and y.
The confusion in the above sentence comes from the imprecise
statment of the scope of the modifier 'first element of'.
It can mean only the first element of x, but it can also
mean the first element of x and the first element of y. So
the user needs to state it as
the ordered list of y and the first element of x.
Finally, a small shortcomming in PROPSS is that the
rules in it are too specialized for Prolog programming. It
is inherited from the 'very-high-level' characteristic of
Prolog, which makes it very difficult to translate a Prolog 111 program to a program in another language. So it is the same as translating the Prolog programming consideration, that is, the rules.
Nevertheless the proposed system is only the starting point of the experiment to support the non-programmer to perform programming. Although it has still a lot of res- triction from the view of the users, it can be improved through three directions of effort.
The first direction is to increase the expressive power of the specification language by enlarging the set of specification format. In fact it is equivalent to finding other ways to construct a Prolog program. It is similar to
the work to find other models of logic programming. Any important result in this research would surely make a signi- ficant impact on the computer community.
The second way is to increase the rules in the knowledge base. It is believed that with the increment of a
small number of rules, a large group of applications can be
implemented. As it is stated that a structure of a class of
algorithms can be exposed by synthesising each algorithm in
the class from a common high level specification [40].
Therefore it is the most feasible and effective method to
improve the system.
Finally, it is possible to allow the system to improve
itself. Actually, the programming process can be perceived 112 as a process of problem solving. Memorization is one of the problem solving techniques. It is possible to implement a learning module, which functions to remember the input
specification and the result program in the system. The
most important and difficult task of this module will be to
match the user requirement with a solved specification.
However, it will be surely the most difficult direction for
development. REFERENCES
[1] Boehm B.W.
Software and its impact: a quantitative assessment Datamation, May 1973, pp 48-59.
[21 Boehm B.W. Sof tware Engineering IEEE Trans. on Computers, vol C-25 no 12, Dec. 1976, pp 1226-41.
[3] Lerner E.J. Programming for nonprogrammers IEEE Spectrum, August 1982, pp 34-38.
[4] Kim K.H. A look at Japan's development of software engineering technology Computer, 16(5), May, 1983.
[5] Goldberg P.C.
Automatic programming Programming methodology Lecture notes of computer science, vol 23, 1975, pp 347-355.
[6] Prywes N.S.
Automatic generation of computer programs Advances in computers, vol 16 M. rubinoff and M. Yovits (Eds.), 1977.
[7] Barr A. Feigenbaum E. The handbook of artificial intelligence, vol
pp 297-379.
[8] Biermann-A. Approaches to automatic programming Advances in computers, vol 15 M. rubinoff and M. Yovits (Eds.), 1976,
[91 Manna Z. Waldinger R. A deductive approach to program synthesis ACM Trans. on programming languages and systems, 2(1)
1980, pp 120-121.
[10] Barstow D.R. Knowledge-based program construction Amsterdam: Elsevier 1979. 114
[11] Hammer M.& Ruth G.
Automating the software development process Research direction in software technology Wegner (Ed.), pp 767-790.
[ 12] Ruth G.
Automatic design of data processing systems Proc. 3rd ACM symposium on principles of programming lang. Jan. 1976.
[ 13] Lerner E.J.
Automating programming IEEE Spectrum, August 1982, pp 28-33.
[14] Kosinski P.R.
“A data flow language for 0/ S programming” SIGPLAN Notices, vol 8 no 9, Sept. 1973, pp 89-93.
[15] Barstow D.R.
“An experiment in knowledge-based automatic programming” Artificial Intelligence, 12(2), 1979, pp 73-119.
[16] Feigenbaum E. “The f if th generation.”
[17] Burstall R.M.& Darlington J.
“A transformation system for developing recursive program” Journal ACM, 24(4).
[18] Partsch H. Steinbruggen R. ”Program transformation systems,“ ACM Comp. Surveys, 15(3),
Sept. 1983, pp199-236.
[19] Winograd T. “Beyond programming languages” Comm. ACM, 22(7), 1979, pp 391-400
[20] Lerner E. (Ed.) Function-level programming IEEE Spectrum, August 1982, pp 28-33,
[211 Kowalski R.A. Logic as a computer language Logic programming, Clark and Tarnlund (Eds.) Academic Press, pp 3-16.
Sept. 1983, pp199-236.
[22] Kowalski R.A. Predicate logic as programming language
Proc. IFIP-74 Congress. North Holland. pp569-574 [23] Robinson J.A.
Logic programming- past, present and future
New generation computing, 1(2), 1983, pp 107-124.
[24] Kowalski R.A.
Algorithm= Logic+ Control CACM, 22(7), July 1979, pp 424-436.
[25] van Emden M.H.
Programming in resolution logic Machine Intelligence, 8, pp 266-299.
[26] Kowalski R.A.
Logic for problem solving Artificial Intelligence series, North Holland.
[27] Robinson J.A.
A machine-oriented logic based on the resolution principle Journal ACM, 12, pp 23-41.
[28] Sammut R.A. Sammut C.A.
Prolog: A tutorial introduction The Australian Computer Journal, 15(2), May 1983.
[29] Clocksin W.F. Mellish C.S. Programming in Prolog Springer-Verlag, 1981.
[30] Warren, et al. Prolog: the lang. its implementation compared with Lisp Proc. Symp. on AI and Programming Language, Special issue, SIGPLAN Notices, vol 12 no 8, (SIGART Newsletter no. 64), pp 109-115.
[31] Hansson A., Haridi S. Tarnlund S-A. Concurrent logic programming Logic programming, Clark and Tarnlund (Eds.) Academic Press.
[32] Gallaire H. Lasserre C. Metalevel control for logic programs Logic programming, Clark and Tarnlund (Eds.) Academic Pres s.
[33] Robinson J.A. Sibert E.E. LOGLISP: motivation, design implementation
Logic programming, Clark and Tarnlund (Eds.) Academic Press.
[34] Hansson A. Tarnlund S-A. Program transformation by data structure mapping Logic programming, Clark and Tarnlund (Eds.) Academic Press, pp 117-122. 116
[35] Deliyanni A. Kowalski R.A. Logic and Semantic Networks Comm. ACM 22(3), pp 184-192.
[36] Heidorn G.E.
Automatic programming through natural language dialogue: a survey
IBM J. Res. Develop., 20(4), pp 302-313.
[37] Hogger C.J.
Goal-oriented derivation of logic programs Mathematical foundations of computer science Proc. 7th symposium, 1978, pp 267-276.
[38] Clark K.L. Tarnlund S.
A first order theory of data and programs Proc IFIP-77, Toronto, 1977.
[39] Eriksson A. Johansson A.
Computer-based synthesis of logic programs Proc. Int. Symp. on Programming, 5th Colloquium, Turin, April 1982, Lecture notes in Computer Science 137 Springer-Verlag, DD 105-115.
[40] Clark K.L.' Darlington J. Algorithm classification through synthesis' The computer journal 23(1), pp 61-65.
[41] Hogger C.J.
Derivation of logic programs Journal ACM 28(2), April 1981, pp 372-392. APPENDIX I RULES FOR FORMATION OF PREDICATES
A l' LI 1 0 V:- T' 0 r'(• 0 i' :i. |i 0
')• 1.11.-:.') 1.1 .1 f! -r
r LI J. 0'• b 0!' I.' P V O 1 O 3! P j p I 3 y.. 0 n rj I p J) ru J. 0: X!' I'. P TO 1 0 3! I::' 3 y r p |-D I G3! P j} ru.1.0( be y I' m PI) t( b0....oaae.... 1( P p PI) be... caa0...2 (X? I i p)' i.j 0....:: ';J I. K I.. A y T y S 0 i| Ll I{ J y... 0'I'V'i! i I•''-• O• -...i 0' I( A•' p f! i J )Y| i'.'.'i 0'( i)•« W I' I.-1;:.1 A.' P W!' X t 0' j,•'!•' W I' :i. t (0')!' I i J. f u i! c t a r( Z a 0tt a? 2) y 3 r 3 1 y 2? X) y a r 3 2 y Z p Y) b 0... e a a 0... 1 (I.'! X y Y y a y.... 0 r, d!!...:! y i: a y.... 0 n d y Z! I... 3) t••• 0 b -...i 0 0 t,( X)? 0 x p r 0 a s :i o n( Y•' y 1 p wr:i.t-e( Y) ?wr:i.te( is an e a p r 0 n-aa :i. o n to'} pwaite(X)? n 1» r Uf I«... t U I k._!'•::?::: A .1. iv.S i I P .v.. P: I' iv.S i.'' .i.. P A.' P 13• y ,f„|. b 0.._ c 3 a 0 2 (i. X! 1... J p I... 1 p i... 2)•••• ( nu in bo r( X) p ob Jo civ( X))?! p b 0.... c e; a 0.... 2 (I.?! X! I...!! 1 p L2)'• b 0 c a a 0.... 2( C X! I... 3 y L1 p i. a y.... 0 n d! I... 2.'!!) t•••• f I..I n C t O T( X p l:: y N) p a r 3 a( N? X? L. ..I p A y% 3 I.) p a p p 0 n d( L. 1 p A r 31 p T) p!? w r i 10( F) p w r i 10(' i -a:}. 11..1 ii c:-1 :i. o n w :S. t h :j r 3 u m 0 ft -t) y w r :i. 10( 7)? i-i I. p Y==, i: F! T 3? r 0 v f( C Y :i p!... p L 2) 3 r 3 a( 0?.... y I...? I...« a r 3 a( 0?....? I. p M'••••! y f a :i. 1
a rss (N? T p L? L1.) t- a r 3 C M y T p X) p N1 i N-•:!• y a rss• N:l, p Tp tX! LZI d...!), r 0 v T( Y p L a a.... 0 n d! I...! p I... 1)••• p p 0 ii d{- v p I... p I....!.• r 0 v f( Y y t a y.... r 0 v( X)! L 3? I... 1.) J- r 0 v f( s: X! Y 3 9 L p L :i.)• r 0 v f( Y? i::x! L::I pL:I.;•••• 3 P P 0 n d( Y P. X 3? Y) v r 0 v f( Y a p I... y I... 1)• 0 a p r 0 a a i o ii(( X+ Y)) t•- 0 :a p r 0 a a :i. o n X) p 0 p i• 0 v. u 11••••• e v.- P r e s s ;j o n C( X- Y)$-• 0 a p r 0 a a :i. o n( X) p 0 a p r 0 a a i o r i( Y) 0 a p r 0; a i o n(( X Y))?- 0 x f r 0 a a :i. o n( X) p 0 a p r 0 a a i o r.( Y), 0 a p r 0 a a i o n( X Y)) t- 0 a r r 0 a :i. o i' i( X) p 0 .a p r 0 a a :i. o r i Y)- 0 p p 0•::: ;j n||( X)• OD%J0C t A) P' lUlllljO 1') 1• Y T' Ll!! C t O P P L! J. 0 Y
P LI .1. 0( X I•' yP .1.) t—
( sys ...op() y t i. t J, 0( X) y li a 0 r.. op C X))?! y w p :i. 10(' p 0 c 0 a la i z 0 id o p 1') y w p i t 0( X)? n'! y f li it... c a a 0... 1. C X y l::•• f :i v p1.;, f u n....:• a a 0... 1. FA,met II' X! P I» p 1 v P2) t- ( IIuIIIb0 P( X) y ubJect( X))! y f li i t.... e :i Li e... :l.( F li n t P? I X! P :l 3? P 2) f '..i n.... e a -a 0.... .1.( F u n c t? r a y.... 0 n d! P .I y A r a a y I X y a 'a... 0 r. .-j I P; I) i P 0 V( A P a Li y I-' i::'i P a Li) y X•• I.. I•..( I T C t' I'1! H 1 a Li ,.l• f Li i t_.. c a a 0.... 1 (X F-1..1 ft c t y L Y 1' I' ..I y A p a a v!.. X y l? 0.... r 0 vy{ y.) JI-'. j.) f t' U li Pi t(!) P( Y 1 y Y F y N) v! y P 0 V( A r a S y R A p a Li) y a 0 n:: m( a 'a v y S) y a P a a( N y Y T y I. .I y I...) y Y= I.'.' Y F y S! L 3 y a P P 0 I T d( R A P a Li y I.'.' S :i y F;.: A 8) y X= CXFurict: PASII
f LI n... c 3 s e... 2 (F u r. c t y CX 1 P1 p 1, p 2) -t ii u in b e r( X y o b J e c t( X)):! y f li r.... c a a e... 2 F u n c 1yP y L X !P 1 H yP 2), f u r'i... c a l e.... 2( F u ii e t? I a y.... e it d I P y A r a 'a yI X y Li 'a.... 0 n d I3)$- r e v( A r a s? R A r a a) y X=• f F li n a t!R A p a a 3 f' u it.... c a s 0.... 2( X F li ii o t y L. Y 1! PII•• A r a a!.. X? a a... p 0 v( Y)! J)— f' li ii c t o P( Y T? Y F v N) y! y p 0 v( A r a a y R A r a a) y a 0 n Li y it 1( s y v y S) y a T3 Li' fx Y 1 y 1. .1!• L y- i: y f y s 11... :i X x. a I Xl:: li net y S I PA rata J Y' p r o v :i. d e d' r u1 e Y
r i.j.!. 0( p r o v :i. d 0 d 9 II's y.... 0 n d v X! P ;;i r x...-; p -j.... p r 0 v i d 0 d.... 03 0.0 C X? I::' y P;[), p r o v :i. d 0 d c 3 s 0 x y 1;; ;;i, 1;; ;;i)+
Provide d.... c 0 s e X v r 1 f I P IS-'! P1;- Prc,,, vv r.., Y„,,,. P r o v :i. d 0 d_.. 0 3 s e( X y IIY! PI! y II Y I p| r-rov-dr,-j,
Y•- ;i. f- rule Y
r i.j 10 (:i. f y F- y p :l.):-
w r i t0(• r 0 c o ri i 0:0 w o r d :i. f') y n]. y locate( S•::!.... 0 n d y P y P2:• P 3•' y
append( P2 y r sy-fpv {:if) sc»;vd|J PFU«F:i.)
' 3 I. J. 0.!. 0 IY« 0 fit-' i'i.iJ 0 Y,'
r 1..110( 3]. 10? P y IIX v 3 y.... r 0 v (3 110 (I...) I p :i I!)$ 1
W r .i. t 0 C 3 3 3 I ..I ITl 0 0 J. 0 IVl 0 !'lt P)• o C 0 5 0 Q t. !l S 0 0 U 0 I! t j. 3 J. O T d 0 T'? u s 0 r._ r o y F '.J .1. 0( hi 0 3 d y I' I.. X I P 1 .J) y 3 T 3(2? X y I...•
Y' r 0 p 1 a c e d rs ij 1 e Y
r u 1 e( r e p 1. a c e d y P? IIX? s y._ r 0 v( 3 y.... r 0 p)! P11])-
j w r i 10(' r 0 c o .3 n i z 0 w o r d r 0 p 1 3 c 0 d n 1 fun-case-1( replaced? P y IIII? IIX! PI II)•
rule for 'all' 'not%
r i.j 10( Y? P y P1) t- •3 p 0 c :i. a .1_ w o r d'• V) v w r i I.- 0(' r 0 c o 3 ii ;i. z 0 w o r d.) w r i. 1,0( Y j? i 1 I. y 3 p 0 c i. 31._ c•:: 0( Y:• P y P1) 3 p 0 c :i. a 1 c :i s 0' Y Z X! P1 I X• 1•... r 01Y! P!) 1 f i.j ii c t o r( X? F?_' ( 3 y so p( F)? t .i. 110 (I) v '.J 3 0 ro p i:r))« 3 P 0 c i 3.1 c 3 3 0( Y IIX I PII•• IIX I P1II) t ( nuiTibe r( X)? object( X)) special-ca30(Y yPyP1)+
% 1 i 3 t„ o fa 11 rule
r u 1 e( s y_. 13 y IIT! P H y!.. s y!. a( X y (1) 11-' ..1) t— f LI ri c t o r( T y F? N) r S e ri s y ir»( 3 y v y X) y ;j r 3 s( N y T y[ II9 L)? 1;5~ t A n I? X! I I v
Y' r% 0 s i.j 11r u 10 Y
r«.j 10( r 0 3 u 1 t y IIX I PII y IIX I PII:~ f 1..1 it c t o r( X 'r 7..)» (•:::MMn M p( J»»' r)'• t- I I 1 0( F) U 0 T%... l)( F)• 'K 3'!• 3 I..I i l l M!'•( I r t| j 0 r u I e( X, P v[; x: p :i)••••
( riumbe r( X) 5 object( X)) y! y
w r i t e(' r e c a 3 n :i. z e'} y w r i t e( X) n 1
'clause' rule r u J. e (c1 a i..i s e y P y P:!.);-
w r :i. t e(' r e c o 3 n i. z e w o r 6 c 1 a u a e'') r,]... f u n.... a s e.... 2£ c J. a ,t s e- P i;; ;;i» p ;j.
X' e' .i i-• s t !1 ,i c t u r r= i1 e:! T'U 1 e s ou i t r u y I-'• P j. i••••
w r 1 t e(•' r e c o 3 nz e p h r a s e s a m e a•!;. r u c b u••• e') a 1 f u n.... c a s e.... 2( e o u :i. s b r i j p r;I- p j),.
$' e .i. e fi! e n t' r u .1. e Y r u 1 e( e .1 e iti e ri t y P y P1) t—
write1-.' e 1 e iii e ri b rneans the head of the list T') y use r... re v! r u 1 e( h e 3 d P y P1). r ,..l J. 0( 0 J. 0 lYi 0 li 'b y P!• I' 1) t•
write1-. 0.1. eiYien b rneans a irieiviber of the list 2') y user.... rey! v r u 1 e (m e in b e r• P y P1) rule(heady CXIPU?P1) O b J 0 C t( X) y f ij ri_ c a s e„2( h e a d v b X! P1 y II' I? P1} ru1e(head 7 CX IP3? P1) t- ( s y s.„ o p (X)? b :S. 11 e( X) 5 u a e r.... o p (X)) y f ur.._cas0._l (head y CX! PI? C .1 v PI.), rule (t a i 1 y C X: P1? P1.)- O b x j 0(- b( X) y
f% u ri_ c a s e.... 2 t a :i. J. y I- X! I'..! y I.. J y b 1« rule (tail y CX! P3= I' 1:- ( s y s_ o p( X) y t i 11 e( X) y u s e r._ o p( X)) y r u n_ c a s e.... 1( b a :i. .1 y C X! I::' 3 y C :i y i:: 1), t structure rule! r i j 1 e( X y C Y y s y e n d! PII y C Z.» s_ e n d I P 3)+~ s t v »..i c t u r e... w o r d( X) y ri u in b e r( Y)? o b J e c b( Y))»! y f u ri c t o r (Z.»X? 1)? 3 rS( 1 y Z y Y)•
r u 1 e( X y C! PII y C Z y a v-j.... e n d! 1' 1 ..I)'• s b r ij c b u r e_ w o r d( X y (i i u iti b e r( Y) y (i) b J e c b'• v y I y 1 o c 3 b e( s a.... e n d y P v P 2 y P1. y a p p e n d (P 2 y C YII y A s v ••v}• y i a... r M:::: a. L A i 11':••
r u 1 e( a b b c- i b u t e t IIX y s 'a.... e ri •«I P 3• C 111.( X) s a.... e n d! I• I!)- r u 1 e( a i b r i b u t e? IIX.» A y s 3.... e n d I PII y II a r a( X:• ii) y s a.... e n d! PII}• r U I 0( X? I' I')$•••• ;j. |-|y-( V) a r I..I I 0( X!• p!• p 1)$
write( X) v wr its(' :i s an object of the problem'• 'K i..i s 0 p.... p•:: v
s s s 0 r t a( o b... i 0 c t( X)) v r u 10( X y p v P1.) r li 10 (X v i::' yp:!.) i••••
write', x) y wntfv?(' i s a rlinci, i.on?)• Li::: 0 p.... p 0 v
3:: 0 P't 3 K• .1 0 P.... 0 p( X}) y p li 10 X l::' v p;|.)+ Pule(Xypjrp)
w r:i. 1,0 f'. ji ist :i. 3 no re') y w p ;i. 10( X)... 1,1 j•!••( 0) LI S 0 I.... T' Q?
3SS0 r'!.••: V i. l io r0( X .-1 5•• PLI1 0( X 9 P y I..' P PO]. 03 y Y| I1 j.']):••••
W P 110(' C O LI :i d Q LI u P i. 10 t h 0 P P O J. O 3 C I 3 LI S 0 f Q p ;j.!;.. 3 y) v u s 0 r.... p o.! W P i 10' C LI S 0 P .I•') y li 1 y P 0 3 d Y) y
w p :i. t0(' li s 0 r c a n a li.'!. t0 d') y d 0.1 0 !i d (I1 y P .1.) PIJ10( X 9... y...} X
w p i t0(' S 0 p p y y I d 0 ii a t k n o w i-g h a t :i. s') y w p i t0( X) y n?
w r i 10(' B 3 c!••:. t r 3 c k t0 t p y a a a i. n') n1 y ! 9 f 3 i I d 010n d (C! s y.... 0 n d! PII ?P), d 0101-, d (c x: p ::i y P1) t d 010 p. d P v P1
.j...... 'j ?v-«...•:: r i! r» LI S 0 P.... P 0-- a... P P O C( 3 I..I t- 0) 9 ll i. u s 0 r.... r (s• ...I«....•••jY.4•!• n( V„ (X•= 10 9 ( X•••••:!. .1.6 y ?A 0 10(....) 9 i r a c 0 y w r i. 10(!)? u 0 r... p c)? VA... (?7 V..1 WdN-'••'• p I 0 r t p :i. mc 0 r e 9 p 0 t p a c t 11 (t :i. 110 X)) y 10( f :i. r i :i. s bi) y c a 11( p u ri( p r 0 p s s)))? ( X= 114 y a 010(....) v ri 01 p a c: 0 9 w p :i. 10(!) 9 li a 0 r.... r o.? ( X= 110 y 3 010 y! f a :i. 1) y ( a+()() y uj r :i. 10( y?') y'- i» 0 p••••} 5•=• APPENDIX II RULES FOR REFINEMENT OF CONTROL STRUCTURE
$ c o ii t r o.!. r 01 .1 n 0.
C O li t r O I.... T' 0 T' i. Ii 0( i. J y I' y P y F' t I' t)• cor. t r0 'I r0f i. ne (a 1 1.0(!. :i.••• t)! I.. I II!-!! TII 9 I11. y Pt- P t:!.) J•• writer '—.1.0as0 ch0ck it' the bounders c3s 0 is correct, 1 5': y n!.. a d d c P( H.... t y I... i S t y I... i s 11) y s 1..1 b s t :i. t1..110 (I... :i. s 1t• I... :i. s t 9 H 9 H1) y subst :i. 1u10• I.. 1 F. i. st y 1! y H.)• append( CM J Til? IH:!. .1 y P2) 1 0£( P 2) y c 0 n t r 01 r 0 f i n 0 (I... y P 2? P1 9 II s a.... i t M 2 I P t .J v P t1)
c o n t r 01_ r 0 f i ii 0( II a 11! i... 3? C H! 7 II y P1? F' t?!-' t1.) t- w r i 10(•'— p 10 a s 0 c h 0 c k i f t h 0 b 0 u n d a r a c a s 0 :i. s c 0 r r 0 c t f!') 9 n I 3 p p© n d LI H ITU? IIH39 P 2) v 1 o si( P 2) y 1 c 0 n t r o I- r 0 f :i. n 0 (I... 9 P 2 y F' 19II s a.... t y H I P t II9 P11)•
control-re f i ri e (II s a.... 1 a 0 Y y X» T 0 in p)! I... II91!.' I-I! T I! y P1 y P t y Pt1) I- ' w r i t e('- p 10 a s 0 c h© c k :i. f t h 0 b 0 u n d a r a e a s 0 i s e o r r 0 c t!!') y n 1 » substituteCTempIXHyXyHyHI)y subst i tut©( CTemp I YII y Y y H1 y H2) y substitute (II 3 y Y y H v M3) y subst :i. tut0 C 3 y X y M3 y H4) y append( CH2! TII y HH3 y P2) y 1 q £s( P 2) y CO fit r 0 I ret i 110 1 I... y!-'2 y I' I. y I sa... n y H 4 y s a...•' l-l.!. y•- v I' I F- y' F.!.. c o ii t r o !i i- 0 f 11!e•. I. c 1 au 5 e' X Y)! L II• II Fl! T1 •1; P i v P-l,?:- 1 .L(.) L :5 (0 p j P Q I} rj»»] y•.».««! n 1 li c t. n r H!•' I• N) •::? T ':r't( N V I!». .J!':•!-••; '• 3 fj S If!( I- J.1 Ml)'
' i c h a I. a e p r e d N F? F C H 1 T1» V. N H i N T I!)» C h 3 !i 13 0 P r 0 rj( N! p i,!' I,5 I..I|| ;j. t( X Z I.«. I!::• 0' V•'!... I....!) i~ (i:: n H: N T n) c o h t ro .1.... r 0!' :i. ii0 (I... .1. y L r!! r! 1!•.•!••' 1 y F Sy...h y H y•—? £00 i ns( :i. HP) y Se0. Y)? NH y S0 0l i 00 Q'• .11i;-}!!•' 'b 2 ..i:!' t .1.) v
c o n t r 0 1.... r 0 f i n 0 (1 c 1 a u s 0( X? Y) 11...I!? I H! T II y i;:' 1 t v I-t1.I- 1 o c 3t e p r 0 d( w r :i. bt0 n y T... y....)! y f IJ|| C t O f( H y F' y N) y
oPSiS( N y H y L II y AS) y i 3 0 n 5 a m( F? N F) y c h 3 r. 4 0 p r 0 d( N F y F y II H! T II y II N H! N TII) y c h 3 n 3 0 p r 0 d( N Fr? F v P t y P12) y U li i f y( X y Z y C.s 1 U S 0( Z y Y) y!... y I... 1)• lo(CNH!NT3)y c 0 n t r 01_ r 0 f i n 0 (L1? F N!••!! NTH y P1. y C s y.... h y H y t- y 10 X 1 i n i3( o «..i t p)? 10 X ICY) y N l-i y b o]. d? b 0 X X C o• .1 b p)! P12II? P b 1)
i X o c 3 b 0 p r 0 d (y F H y... y....) t™! y F 3 i I• X 0 c 310 p r 0 d( X F Y 1 T1 y F II y T):- f .1 n c 10 r(' Y y X y...) 10 c 310 P r e d C X! Y! TII. IIY! T X.] i 2) i- X o c a b 0 p r 0 d X.• T T1» f 2)-
e h 3 ri i3 0 p 0 d(... y.... y F II y F II)• c h 3 r i a 0 p r 0 d( N F y F? IIX! P J y F N X I NI' .1) ._ ,1, f I tj r i u' i.. u 1 k .a u r•• a T'- s s( N X v II'! y N A 0)? NX= CNF!NAs3f ch3risep red C NF F y F' y NP)»
c Xi 3 ri 13 0 p r 0 d C N F y F? F X! PII r F X! N PII) t•••• c? h a n ?3 0 p r 0 d C N F? r v P y N F'). c o n t r o 1_ r e f :i. n e( C R! LII F'? P l p t, P t :l.) I- f1J n e t o r (R y r e p I a c e d y N 3 P 3{ 1 y R? l-i) y
11 a lit e( A y i: :i. iy:! 21 :l 18.... II) y ! y
1 o c -Hi t e (y.... r a p F' y P 2 II T! P 3 ;;i)... 3 r 3 s( N y K II II y IIA! R A s II y 1' li ii c I'.- o r( 1... I v jvi)... M:!. :i. s M- :l. y a r a s( M :l. y j, 1;: ;;i.. t a s) v i; ! i B P P 0! 1Q( I A 3 y K A S y{- A 3) v Z~•• IIF! F A 3 I!'• f i .1 n t o r( Y? a s a 0 r t a y 1.)...... • 4 v•. c:- I'.v.S .1. I V ,l N1 .i s N 1 y QUPI ist('..'? N1 y DL) y 1 3 P P 0 li d( I A S y D I... y 'J I...) y V=• CFIUL1, f t.j ii c t o r( X? r 01 r a c i y 1) y
3 r 3( 1 y X y v1) y 3 P P 0 r. d( P 2 y IIX y Y I P 3 II y P 4 y 1 03( P4)
c o n t r o 1.... r 0 f i n 0 (l._ y P 4? F' 1? P t y F:'t .1.)• d U P I i S t( X y :L y C X U d u p 1 i 0; t( X y N y IIX! I... II) t- N1. :i. s N~ 1? d u p I :i. t (X y N:!.? I:..),
control_ref ine (II rep 1 seed (X y Y)! L.11? LIH ITU? P1 y Ft y P11) t- p not (name (X y C115 y 121 y 118 !.._U)) r! y ' locate sb... rep y T y T1 y 12) y subl(YyXyT2yT3)y append( f 1.? 1 3 y I 4) y
0 d d C P( M.... li y X? X li y f»..! n C10 r( 1-1 y F: y N) y 3 e n s 0 iti( F» N F) y a q d s•: r 3 (F- y X n y II ll I I 4 ..I? 2) y c h a ii 3 e p r e d( N Fr? F y F:' 2 y l:: 3 y a d d a a r 3( F y X y F' b F' 12) y c h a ri 3 e p i e d( N F y F y P 12 y F' b 3): subs t i t LI 10( IIII y X y ll y ll 1)? a d d s a r 3( F? X y IIH1II v IIH 2II) y chaii3epr0d( NF ?F? IllH12IJ• IIH-.5II) y 1 o 3( P 3) y c o ri t r o 1_. r 0 f :i. n e( L y F 3? F' .1. y!.. s:.... i'i y F! y—y H 3 11' 13 ..1? F-' t 1)
3 d d s a r 3(-?._ y C H y !I H) a d d s a r 3( F? X n? L X I P..! y L Y!!' 1 ..I)»• f u ii c b o r( X y F y N) y!? a r 3 s( N y X y C .11? A S) y a p p e ii d( A s y IIX n J? A X) y Y IIF 1 AXII y a d d s a r 3 (F y X n y F't F 1)• addsar3 F? Xn I.. X! F J »I.. X! I .1..!) -vaiusa rs F y X11 y f y F 1
C o r-11 r o 1 l e t i n 0 (IIX I LII? P P1? P t? P t :i t- w r i 10(' u r i r 0 o 3 n :i. z e•::: o n t r o J. w o r ».i')? w r :i b 0«: x s l; I. c a n t r% o 1._ r e f J. ii 0 (L- y P v P1v P b y P b 1) APPENDIX III RULES FOR REFINEMENT OF DATA STRUCTURE
....|,... I •' -Y- I u i oats.. r(•:•»r :i, hp
..1 L.... p(•'; I' j| i iy.«( t! v;•' r''•.
data.... ret .j. ne (|head C X y Y.- 7); I., i.. p,, p -j)•....
d a t a.... r e 1 i ii e( i.. e e i .1 a!.•• X Y)- h eq( v,, y j j„ j„... u a1'•:?.... r e t i ii e(! e ir p t a( X)! I... .1 i::'? I::' 1):•••• S LI IJ .1.' I. .J A!' v I' 2) V!» I o a( P 2) y d a -I;, a.. r e f :i. n e (I.. y t::' 2» P :l.).,
•..I l--0.... 0!! !i 0'• i..'('. 3 I.! V X y Y! i J y(•' y j-') i; c:-}••• R7! I»..«•. I., K.•|. j j i. y'{ VJ j«.»[|' y G L•... I G T f»(•• j.., lj?!' y)..
Gri t-.... re t :i. l !G K I.. L- 3 .i.!... 1' V 5! mmj n r 1'!;!•!••• 3 d do p( 19 5 1.16 ::i Y Y t)» Li u I.'• Y».« y 'L «»I' m{••' y) u' v J. G'• I-1 .V.;l)? Ci :ii: I..- »0T I TlG'••? I.- V!' I.) 3uuCP 1 L-J. y!.). y ML•• ii a if! e( L. y L J.) y append (LI f CI? Ni...I)» ri a iti e( N!... y N!.„ .1'-
d a t a„ r e P :i. ri e (II' e ::y u a 1( X? Y)! I... I y!-•?;|.) t- a Ll b .1( X y Y y F-' y I' 2)! y 1 u ?-s( I' 2)? d a 13.... r e t :j. ii e( L y L' 2 y F-' 1)
d a t a.... r e P :i. n e (I s e 11 o( X y Y)! L I v P? F:' 1) X S U b .L( Y y X y I' y I' 2) y! y lo(P2)y d a t a.... r e f i ri e (L y P 2? F:' 1)
d a t a._ r e f i ri e( L !i e a d( X y Y! I... y!' y F; I) t- LI li 'i. P '.Y( X? Z y h e a d( Z? Y) y I...••• I... I) y a i.'i d C(.... t V y Y t' S LI b J.'• I.. X! Y F- .1 y Y y I' y!' 2'•• loa(P2)y d a t a... r e t i n e(!... 1. P 2 1)..
• S I![ IS i..l n 11 a y.... yy F. J y L..!« u n i. P w( X y Z y Fr y I F! I. 1 y I.. I i- u n i P y( X?.... y l:r y L y I... 2) a p p e n d (I... 2 y C e 11 a( Z y X).! L .1) u ri i f y( X y Z y F y C T I L. 1 y C T! L1 j) i™ U l l i P y( X y Z y Fr y I... y L 1)-
d a t a.... r e P i n e( Z r. e t.... e 1 e X y Y••! I... 1 y P y P1.):- a P P 0 li d (I... y I.. ii 0 t.... 0.!. 0.... .1.( X y f) ..I• L I) y d a t a.... r% e P :i. ii e (I... 1 yy P1) d a t... r 0 P i. n 0 (I ri 0 2 t.... 0 i. e.... t y Y 5! I.. ..I:• F-' y F-' I!-••• addcp( H.... t y Yv Yt) y SubKEX! Yfl y YtyP yR2) y 10 3( P 2) y d a t a... r 0 f :i. i» 0 (I- y P 2 y P t)-
d a t a.... t- 0 P :i. n 0 C X! L1 y P r P1)- d t a.... r e fri a C!...• P P1.;. APPENDIX IV RULES FOR REFINEMENT OF FUNCTOR
$ T '..i ii c b o r r 0 f ;L ii 0!•• f unctor..„r0f :i.ne( II I! y CI), t u n c t(.) r.... r 0 f :i. n 0( II s 0.... 0 n d !PII yP 1)$- 1? f u n c t o r-.... v e f :i. n0 (P? p:!,), f 1..1 n c t 0 r.... r 0 f :i. n 0 I s a.... I a( L X G) I P '.'I,• C f :i. n d 011( X y G d...} j p t::!)••• 1 v f 1..1 n t a r.... r 0 f :i. n 0 (P 9X)•. f u n c 10 r.... r 0 f :i. n 0 (IIt r u 0 C X) I p I! L 0 aX X( X) I P1.!) i- f uncto r( X 9 v) m 1 funeto r.... r-e f :i. no( !'«p :i), f u n e 10 r._ r 0f ;i. n 0 I. w r :i. tt0 n c X) i L I y II w v- :i. b0( X) JI X II)- !? f 1 j ri e 10 v.... r 0 f :i. n 0(!....«• L1. f 1 j i! c 10 r.... r 0 f :i. n 0 (I! w iv i 110 n( X? 'T')!{... I»I »,.i v i. 10( X)! 1. f; t•••• ! 9 t u n c 10»... 'f 0 f :i. ii 0( L. y I... .1.) ruric10 r.... r0 r :i. 110( L r-0ad X 9 1')! L .1: I. read( X)! I... 1..) i•••• !? t u ii i0 t o r... r 0 T' :i. ii 0'•!._•!... 1) f ».jr i c 10 r.... r0 r i 110(!.. a a a :i. an( X? Y) 11 1? L mS3:i. s r i! i... 1 ,.l)•••• n a i n 0( X X .1.) 9 ri a iyi 0 Y Y 'J.) 9 append(X17C32 ?105,1159321Y129 A1 9
n a iii 0 A s :i. d 1 19 AI 9 f «..i n c 10 0... r 0 f :i. ii 0(!... y I... 1.)•
f i.j ii(.? 10 r.... r 0 f :i. i 10 (I a 1 u s( X? Y v Z)! I... II Z !I! a p 1 u•::! I...:!. I!) t- a r i t h.... f 0 r m (II4 3II X Y v Z I a p 1 u s)? ! f f u 1 1 c t o v.... r 0 f i n 0( L 91... 1)
f u r'i c 10 r.... r 0 f :i. n 0( L m :i. n u s( X 9 Y- Z) 11... II? II1in :i. n u 11... 1II) t a r i. t Ii....!% 0 r in C[. 4 3 ..19 X 9 Y 9 Z 9.1. s in :i. ii 1..11:) 9 !? f ij 1 1 c 10 1-.... r 0 f :i. n 0 (I... 91... 1)+
f'uncto v.... ret i n0!.. t- i 1110a X 9 Y 9 X.)! 1 19 L .1. a t i iti0•••• 11,. 1..! I•• a r :i. t hi.... f 0 r m (4 21! X 9 Y? Z? I a t :i. i n 0 a) ! 91 u ii 1? 10 r.... v 0 f ;i. ii 0 (I... I... :1.•
f u ri c 10 i'.... Y 0 f i 1 10( II d :i. v :i. :i. 0 n X v Y Z 1 1... II9III s d :i. v! I... 11!? i•••• a r i t h 1V 0 r iti(!.! 4 71!; X Y Z 9 J d :i v) 9 !? f Li ii c 10 r.... r 0 f i ii 0 I... 9!... 1)•
f uncto r.... ref :i. 110 (I. mod X• Y• Z) 11 1? L J. a mod 11... J...!.' a r :i. t h.... f 0 r m( iyi o d 9 X 9 Y Z? I m 0 d) 9 ! y f u ii c t o 1r.... r 01 :i. r'i 0 (I... L :1.)
a r i. t Ii.... r 0 r m( 0 p 9 X y Y y Z 91- 0 r m)-• ri a iti 0( Z 9 Z1) y n a in 0( Y y YI)? n a iyi 0( X y XX) y aPP0md( Op y II32! ZX II TompX) y a p p 01« d( Y X 91:3 2! T 0 m p X II y f 0 m p) y append( X X? C32 y X 05? 1 X 5 y 32! Temp II? Fo rm 1) y r'i a iyi 0( F 0 r m y F 0 r m 1) f u n c t o r_ r e f i n e C1 e s s (X Y)! PII» C Z! P11%-
(«' 0 I n P 3 V 0... T O r ITl([. O 0 ,.l y X y Y y) y f unc10 r....r0f i. ri0( P y p J.)• 1 f u n c t or.... r 0 f i n 0 CC it. o r 0( X y Y)! p :i II Z J p ;s. IS'• c 0 rfi p 0 r 0.... f 0 r iti( II 6 2 IS? X Y y Z)» f' .1 n t. 0 r... r 0 f :i. n 0 P yP 1),, f '..i n c t0 r.... r 0 f ;i. i t 0( H 0 e li 31( X Y) ;F»! Z! p;!. I1} 1 O IT. P S ri?.... T o TUT!..•:: 1 ..I X Y y Z.) f li n c 10 r.... r 0 f :i. n 0 P l;:' 1).. I li!. c b o 1.... L 3'!' i 1' 10(!. X 0 X y Y}! I'.! I p I!•'
C O ill P 0 T 0.... T Q i• I'll!.. y '.I J y X y Y y Z y ''!' LI I T C t O!•.... i' 0 T' i. IT 0(!' y I' 1) a 1 f LI I T c t o r.... r 0 f :j. r, 0( I 10 C X, Y! P I,[' Z 1 P1.1!• r i,.1111 3 T 0 1(!) T ITT I., i'4 I. y £0 .1 y•• v! y L•' y T L! I T (!)'t(!)!•••• f!!. fi 0 V I' y I' 1. 1 C(!) ITi P 3 r 0.... T(!))• I'm'! N .1. y X y Y y Z 1 I
j r 13 ITT 0( X y XI) y r 10 iti 0( Y y Y1) y append N1 y Z 32! Y1.1? T1)? append (X1? C32! T1II y Z1) I'T a in 0( Z y ZI),
f u i t c t o t-._ r 0 f :L i t 0 (n o t! P? !I Y! P11!) t- f li ri c t o r._. r 0 f i ri 0( P? IIX! P1 U) y f li ii e t o r( Y y r i 0 t? 1) y srx(1» Y »X)a
f unctor_ref ine( Ceouist ru( Y y X)! PU? CF1 y F2 I PI II J- aensym(sxv?T1)y X e n 0 y m( s y v? T 2) y f u n c t o r (F1? f 1.1 n c 10 r- y 3) y 0 T X v 1? I 1? X)• 3 r X( 2? F1. y T1.) y . -v r j r 3 T X k -.:'j y I 1 v I:) y f 1.1 I'T c 10 r (F 2 y f m r 1 c h 0 r y 3}? a T% X( 1. 7 F 2 y Y) y 3 r X( 2 7 Fr 2 T1) r 3 r 3( 3? l:r 2 y T 2) y f u n c t o r.... r e f 1 n 0( P y P1)+
f u r'i c b r.... r 0 f i i t 0( Z 3111( A 9 N y X S|! I.J y L a r X( N y X v A)!!••' 1 J) l f u i t 01 o r._ r e f i n e( P P1?• f u ri c t o r._re fv i i t e (CXI P1 y IIZ! P :L II)$- f u ri c 10)•( X y 0 I'T t :i. b .i y N) y N:L is N-:L y a r X s( N1? X 7II1 y A s) f a r X( N y X y A) y I v ''T 1..1 r'i c b o r (.i. 7-?•:.) y a r X (1 Z 7 A) y arX(2 y Z y As)y •P1 1 i -i r-!. n p rp t' i ii p(!•' v|-' 1.)«•
f u n c t o r..re f i ri©( C X! P1 C X! P1.1{- f u n c t o r.... r e f i n m i P? P1 f u n c IQ r.... I- 0 f :i. n 0( C X! L'!!•«!. 3) t-- I Li fi C I.V (j P j. X•' C (1 i'i0 v jv) m ...... I I I I.| A••• c: 1 :-j.' IN y A L J? 1-) 1|-| y
f o!• IT!.... I ;i. 3!;,... I( A 3 j.. ;i. t, j• j...
t I IT!... a P P e11 i.'j... J.( A j.!... 1.3 t]. I S A p:••• e:,•;;! I) T u ii o t o r.... r e f :i. r i 0 (I... y I... j.) y
a p p 0 n d( A p p 0 ri d 1 I... 1. y I... 3), f or in ...append.. 1( T e iv» p y I X 1?... II append[';;! y(... 7 p? hi;- 1;; I O T ITi....3PP0!ii.'.'i... i. i 0I'm» I.. A v Y,.| y Append'. 3P }'i 1! C)'?! j.;ii;} i Ae r!d j'' t O T' I'll... 3 P P 0li G i,(: A? I., a! I... j. 3 f I.!' A P P 0 !i '.J.! I A P P 0 li d J.• Si 01 i 3b I'll( S b V v 1 0 IT'i P) v
T D T' ITi 3 P P 0 li d.... .1.( I 0 IT'i P y I... i S t .1. y L 3 P P 0 ii d( X v F 0 In P? I A j 1 A p'• 0 I T d 'I. J v F A P P 0 i 'i d j'. form™ .1 i s t.„ 1 (L II? IIII ?II 1. f o r m.1 i 51... I([]? I E11 y E E1)£- r 0 v( E1 y E EI), f o«•• in™ .1. i s t... .1.( f x 11... ::i f y: l j. s 11 ::i r e e 1) i- p o s( 21 w r i t0( X) y w r :i. t0 C' :i. 3 0 .3 r o u p o f d 010? y) y u s 0 r... r o. y r 0 V( E 1 y E F. 1) y 3 PP0 nd(EE1y X y Y) y f o nT 1 i. s t.... 1 (I... y L :i. s 11 y .1) f o r m...]. i s t.... 1 (FX! I... 1 y I... i s 11 y R E:.' I) t••• f o r in]. :i. 31... 1( L y!... :i. 51-1 v i X! E E1II. functor™ ref i ne (CXI F I! y P1):- f unctor(X y strconc?N)y a r s s( N y X y III v FA! As!) y f o r iti.... n a iti 0... 1( A 3 y N 3 m 01 y T 0 in p I) y .3 0 ri 3 b in( 3 b-! V y T 0 IT'I P y 3 3 3 0 'P 13( 3 b-.... P i O C( 1.!!.(.)• y f o r ITl.... 3 P P 0 I T d....].(' T 0 ITT P y T 0 !Ti P I• F I! v A P P 0 li d 1 y r 01 fv 3; C t( 3 b... P r O C( 31..I t O)) y
f% i.j |i C t O T™I' 01' i I! 0( I' v I' 2) y 3 p p 0 !i d( A p p 0 n d L I., i t 3 in 0 r. A y T 3 in p)! F' 2 ..I y f p t y 3 p p e i t d( N a in 01? T p ?1) f 0 r in.... n a m0.... I (IIII•• IIII y IIII) v f o r in™ name™ 1( FA I Asl y llnamei nt( A 1 0 m p)! N a m 0.1 J y L] emp l i em p.!. .J) l-- p 0 3( 21 y 1 y w r i 10( A) y w r :i. 10( y 13 a n 1.1 in b 0 t-? y y 1 j 3 0 r™ r q y si e ri 3 b m( s b v T e m p) y f o r m„ ri a m 0.... 1( A 3 y N a m 0.1.») 0 m p I) f o r iti._. ri a iti 0„ 1 (I.. A! A 3 ..1 y F n a itt 0( A y F 0 in p)! N 3 in 0.1.1 y 1.1 0 iti p! 1 0 in p .1. .J; si 0 i t 3 b m( s b v y T e in p) y f o r rri™ n a itt e™.1( A s y N a in 01 y 1 0 m p .1.)» APPENDIX V PROLOG CODING OF THE SYSTEM
Partial list of removed word
K f c::« t• a n d e r i a e!
c a n d 0 r i s 0( I. A! BII• 0) t- w 3 a t e.. uj c i r d( A) y! y;; 0 n d 0 ri 0 B 0;, e 0 n a 011 a e( I.. i-i! B.J• I., h! G.J)$ condi;.' nap( P C).. c o n d 0 n a 0 (IIIIII' I!),
w a a t0.... w o r d X) t- 3 r b :i.e J. 0( X). W 3 S b0.... W 0 Vs d( X)— p V 0 p o a ;i j. 0 ii( X) W 0 0b 0.... W 0 TP d X P l'(!) I( O i..!||( X j
w 3 3 b0.... w 0 r d( X) t- 3 u x.... v 0 r b( X)• w 0 0 t0.... w o!'• d( X)••••• c o n n c t :i. o n X). w a s 10.... w 0 r d( X)•••• 0 t h 0p 0 X a r t :i. c: 10 (a) a r t i c]. 0( 0: ii) a r t i e I0( b h 0) p r 0 p 0 0 i b :i. 0 n( w :i. t h) p r 0 p 0 a :L b :i. 0 n (:i. n)• p r 0 p 0 Mi i t i 0 n( 0 n) v p r 0 p 0 s :i. t :i. 0 n( a t)• p r 0 p 0 s i t :i. 0 n (b 0) p r 0 p 0 5 i b :i. 0 ri( f r 0 iri)• prep00 i t i0n( 10)• p r 0 p 0 0 :i. t .i. 0 ii( 0 f) p f- 0 p 0 a :i. t :i. 0 n( w :i. t h :i. n)• p r 0 p 0 a i t i 0 n( 0 u b) p r 0 p 0 a :i. t :L 0 n( :i. n b0) p r 0 p 0 a :i. t :i. o n( f a r)- p r 0! i 0 u n( i t) p r 0 ii 0 u r'i (:i. t ;.;j)- p r 0 n 0 u ii (t n 0 m) v p r0ngUn( 01! :i. el t) P r 10 ll 01.1 fl( t I I•:: t)- p r 0 ri 01..1 ii( w (10 a 0)• p r 0 li 0 u li (11• 10 0)+ p r 01 101..1 ii (0 n 0 a) a «.j x.._ v 0 r b( a I) i:; .11) a u x.„ v 0 r b( 0 :i. 11) a 1 j x.... v 0 r b( 0 a n)« a u x- v 0 r b( a h 0 u 1 d)« c 0 n u n c t :i. 01 1: ti d)• 01 h 0 r a( a a) 0 t li r a( I 0.1. 1.0 w i•• 011'10'!•: ('..1 ii t i I 0 t l l 0 l' k t I I i:;!•) 01 h 0 r s( b ii 0 r i) t For modifa
c h a n .a 0 i.j 0 r d(!.. a a its 0 v 0 tp1.1 ct1..1 r 0! LII y II 0 ij j. a t ru! I... I!), c h a n d 0 w 0 r d (I 1..1 n :i. n a t a n t i a 10 d? v a r :i. a b1 0! I... 1 y II v a r! I... I), c h a n d 0 w 0 r d( II p r :i. n 10 d p r 0 f :i.? f o r m J!...;;| v H d :i a p I. .a 0! I...::!), c ii a n d0 w o i- d( II i'- 0 0 d y 0 v 0 r w r :i. t0! L. II y I. r 0 c 0 n a i..t'! t! I... I!) c h a n d 0 w 0 r d( II r 0 c 0 n a u 110 d! LII9 II r 0 c 0 n s u It11... II), ehai!d0w0 rd (II c0nsu 1t0d! LII y II c0n a-u 1.1! I... II) c h a n a 0 w 0 r d (II m 0 d 1.110! I... II y II m o d 11... II), chansSewo rd (III endth y 1 :i. at! I. II y II 1 endth! I.. I!, chandewerd(!.. J. i. at y character name 11... II»'In a tin:-' I LI!).
chandewo rd (III x a t 9 cha racte r!|. v n na 11.0! LII•'• chans0wo rd (I.I I endth 9 a t r i. nd! I... II? II a t r 1 en! I„ I)„ c Li a ii d 0 w 0 r d (I., w r :i. tt0 n 9 n 0 t y 1 i n 0 11...] f n 1.1 p ,j. h 0II r 111„ I!). chan.deword (IIprinted J LIU 9 IIprint I I...II) c h a n d 0 w 0 r d (II p r :i. ri 10 d 9 n 0 a 19 'I. :i. n 0! I... II9II w r :i. t 0 .'I r 1! I... I!:, c n a n den 0 p d( L J. x a t? a .1. .1? 0.1.0 m 0 ii t -a- 11 I! y II a a....!, 3 0!!!' ch an d0 word (II a 11.9 element a! L II v Hal :i 0! LI!, c h a ri dew o r d I. ,1. 1 at y a.I I.! I I y[. s y.... I. a' I I) c |-i a n d 0 w 0 r d (II p r 0 v :i. d 0 d 9 :i. P I I... II 9II e r 0 v i d 0 d! LII)«. c h a n d 0 w 0 r d (II d 0 f i n 0 d! I... II? II p r 0 v :i. d 0 d! I... II)•• c h a n d 0 w 0 r d (II01 h 0 r w :i. a 0! LII y II :i. f! I... II). c h a ri d 0 w 0 r d II w ia 0 r 0;:! I... II y II ;i. f! I... .11) c h a ri d 0 w 0 r d (01 s 0 y :i.! I... II y II :i. f 11... II). c h a n d 0 w 0 r d II p r 0 c- 0 0 d i ri d 9 0]. 0 in 0 n t! LII y II ii 0 a d 11... II) c ii a n d 0 w 0 r d (I!! t x r s 19 010 iti 0 n t! I... II9II h 0 d! I... II)• c h a r i d 0 w 0 r d (II a n o 1110 v y 0 J. 0 m 0 n t! LII y II n 0 a t.... 01 e! I... M)• c i t a ri d 0 w 0 r d( Z ii 0 a t y 010 m 0 ri 111... II y II n 0 a t.... 010! I... II) c t'i a n d 0 w 0 r d II p r 0 d u o t! I... y II t :i. m 0 a! I... II c L i a n d 0 w 0 r d (II a 1..1 iti! LII9 C p 1 u a! I... H) c h a ri d ewo r d (II d i f f 0 r 0 rj c 0 I LII? II iti :i. n '..i a! LII) c h a ii d 0 w 0 r d (II d :i. v i a :i. 0 n! LII v II d :i. v i d 0 d! LII)• c ti a ri d 0 w 0 r d( H f 3 :i. 10 d 11... II? II f a :i. 1 11... II)• c li a ri d 0 w 0 r d (II c? a 110 d I i... II9 H t r u 0! LII) chandeword( H re t racted! I I? L v-01X'3c t i I...1• a h a ii d 0 0 r d (II a m;•; 110 r I LII9II1 0 a i.:: I LII) c h a ii d 0 w 0 r d (II d r 0 a 10 r! LII y II m o r 0! I I) c ii a i i d 0 w 0 r d (II d r 0 a 10 r y 0 r 0 e 1..1 .1.! 1 19 L d 0 11 1)• ch a nde wo r d (II mo re? o r y aame! I I y!.. do! L .1• c Li 3 ri d 0 w o r d (II a in 0110 r y 0 r y 0 o' 3!• I I I v 11 o! I I• c h a n d 0 w 0 r d (II10 0 0 y o r 9 s 3 in 0! 1 19 L .1.0! I I)
c h a ii d 0 w o r d (II c 0 n c 9 s t r :i. 1 1 d! 1 19 L 0 t r c 0 n I I... I y• c h a ii d 0 w 0 r d (II p r 0 b 10 in 11 19!.. r 0 a 1..111! I I c ti a ri dewo r d (II10! L I! y II b 0 11 1)•
c ri a n d e w 0 r d (II0 r 0! LII? II b 0! I I• c h a ri d 0 w 0 r d (II c a n n 01J LII? H n 01! LII) v c t'i a ri d 0 w 0 r d( H a 1 1 y' 1 19!.. a J. 1! I I)
c h a ri d 0 w 0 r d (II in :i. n »..i 0 11 1 L m :i. n '..10! I I)• c h a r i d 0 w 0 r d (IIX! 1 191.. X 11 1) ri a iti 0( X a 9 X 0 'I.) y a P P 0 ll d( X J.!•': y X .1.) 9 w 1 V n 5?. rn 0 v a y a 1' c l i a n d 0 w 0 r d( X y X)
IT I d :i. P k.: (IIII 9 I -I m d :i. P 3 (I- 9 L X I L t..!) c Li n d 0 w 0 r d I- 91- X I- 0 ..I y in (Li d :i. f o( L 2 9 L. 1) % f n r rip v' i p!' i.'j 0 r- i V 0( rj J i 1') j
f :i. ii d w 0 r d( I- d v S i.; b J? U 0 r d y (J b• J c o n d ;j| q n( UJ 0 r ij
(( W o r d::::! 2 a p p 0 n d( F d v I j) j| y I:: d 1)? d 0 r i v 01 rj:!... i;; j,, n j•);; Word Ij ;;iappend GbJ y I 2: Ob-,.it)••
a P p 0 n d( 8 u b j r:'?': G b,1. j!:• F. j :l), derive! (F d 1 n C p r v .i. H 0 d J D f?, d 0 r I v 0 1 i...! y 8 y 8). d 0 1- :i. v 01 d v D f yD f j.):•••• ]. o c a't 0 C f?'? F- d y F d 1 y F d 2) y f j. li d W(.') I- d( I d 1 v 81..! b -J! I;J 0 r d• G b J v 0'!• b(!.] 0 V d)) 9 r 0 V( 8 U b J y S I..I b... J1) y a p p 0 n d( 8 u b J:! y I.. W 0 r d! 0'!..!? T) y r 0 v( G 0 J y 0 b ..j 1) y a P P 0 ri d( G b J:!. y T It f 2) y U0r :LV0l( Fd2 y Df 2 v Df 1),
J. O C a 10(._ v I.. ..I!. ..i!.. .J)• :i.0cat0()y i::xil::iy t::i?d, 10 c a 10( X y 1: Y! L 2 y C Y! I... 1 jl y I... 2)?- 10 c a 10( X y I... y L1 y L. 2). f i I! d w 0 r d (1:: :i y C :i y C ll y 1:::.] y....) f i r. d w 0 r d (i: X! 8II y .y X y 8 v G) i•••• c a 11( G)- f i ndwo rd I.. X' 8 ..I y!. X! 81..1 b-..i..! v Go vd (.)!....i y l;)$— t :i. rtdwo rd( 8: 8ub%j Go d y ObJ v iG} r e f i n 0 (I II m p »I-'- f) ! y
C1P y POS( 1? 1) y
w r I t 0( 11 i 0 p r o 10 3 e J. 3 i..i s 0 s 3 r 0 ci 0 t% :i. v 0 d 3 s y') y ll 1!' Il J.? f Li r n :i. s h (l;;' y P f) y P r i ft t 0 .1 S 1 j s 0( p f) furnish FII 9 FII) f u r n i s h (F:'? p f)~ locate('' y P y PI jrpt)
f._ c 1 a s s i f 3( P 1. y P d y I' e y P P) y c o n t r o 1._ y1, 0 f i n 0( P c y P p y i:;' 1 c y II J y 11) y data._ r 0 f i n 0 (P d y F:' 1 c y P1 d) y f u n c t r„. r 0 f :i. n 0( P1 d y P 1 F) y F u r n .i. s h (l:: t y P t f) y a p p 0 n d e 1 (P11? P J. f y P t f y P f), r 0 f i n 0 (I! X! L .1 y P y P1):- rule( X y F' y P2) y lo.d(P2) y refineLyP2VPI) 9 f „c 1 ass i f y (IIII y C II y II U r IIII) f _c 1 ass i f y (FX! YII y LI X! DII y C 9 P) t- f IJ r'l c t o r( X y F y N) y d a t a.... w o r d (Fr) y! y f-classify(YyDyCyP), •F- c 1 a s s i f y( C X! YII9 D y C X! C H y P:- f u fi c t o r( X y F' y N) y corrt rol-ward (Fr) y! v f «.c 1 assi f y( Y y 0 y C y P) f_ c 1 a s s i f y( C X! YII y D y C y IIX! P I!)- f„.. c 1 a s s i f y( Y y D y C y P), a P P 0 n d c 1( r II y P1 f y P t f y P f) i••• a P P 0 n d( P1 f y II''! P t F IJ y P f), append c .1( F s s.._ h I P11II y P1 f y P t f y P f)- cutcl(PltyPhyPltt)y append (Ph y F' y! P1 f II y T) y a p p 0 n d c 1( P :L 11 y T y P lv f y P F) sppendclCFs v. t! P11U y l:: 1 F y P t F y l;:' f)$- cutcl( P11 y Pt y F:' 111) y 3 p p e ri d (P t? F'' I F:'t FII y T) y a p p 0 n del( P111 y P1 f y T 9 P f)• c u t c 1( II U y F II y F II) v c u t c 1( F s w_ h! C1.] y L ..I y l» s s h I (.1.1. .1) e ij t c 1( F s y t! C1 H y F II y I., s s t! C J. J)• cutcl( FX! C J. II y FX! NC1 J y UP1)™ o'J Fe.1. (i J. y NL.!. y 0L.1.) p r i n t.... e 1a u s e(. I) t•••• n 1 p r i n t.... c 1 a i.j s e (I. X?'' i Z .'3) t••••
n I? w r :i. t e( X) y w r :i. I;.. e(«) y p r i ii t.... c 1 a i..i s 0( Z) p r i nt™C 1 ause( Z X? t••••' J Z.! i i' i 3.? w r :i. t e( X) y w r i t0(' v.') y p r :i. n t.... c I a !..i s 0( Z)• p r :i. i'i t....1 aua0 (i: X y t~? Y y','! Z !3)••••
n 1 y w r :i. t0( X)? w r i t0('') y y r :l. i0( i-) y w r :i. t0(') y w r :i. 10( Y) y w r :j. t0('.')? p t i 111._. c .L a i.j a 0( Z)«• p r i n t~. c 13 u s 0( Z X y J- 7 Y! Z :j) J- n 1? w r i t e( X) ywrite'') ?write 7 n 17 w r :i. t0('')? y r :i. t0 Y)? w r :i. t0' y' 7 p r :i. ri t..„ c I a u s 0.._ 1( Z) p r i n t.... c I a u s 0 1 (I X 7''! Z.'.I)- ii 17 w r :i. 10('') 7 w r :i. 10( X) 7 w r :i. t0(,) 7 print....c 1 a 1Js0 Z) p r i ri t.„ c 3. a u s 0.... 1 (I.'. X! Z I) 2-
n 1? w r :L 10(' ')?w r :i. t0( X) 7 w r :i. t0(' 7') 7 p r i n t_ c 13 u s 0... 1 (Z)
3. ad(start) t~ 1 o .3„. f i 10 F i 1 n a iti)? t011 :i. ri d( 01..1t p 1..1t)? 10 3.1( F i 3. n a m) 7 w r :i. 10(' K Y Y S 'I' A R1 Y Y Y) 7 n 3.- 1011( 0 u t p i..i t) v 3. o 3( f i r i i s h)•- v 01 r a::• t( 3. o.... f :i. .1.0( F :i. 3. ii•:: m)) 7 r 01 r a c t a 3.3.( i d i( o r 0( X)) 7 r 01 r cJ c t a 11( o b J 0 e t( X)) 7 r 0 t r a c t a 3.1( c u r 01 11.... ii u rn( X 7 Y)) 7 10 3.1 i n a (0 u t p u t) 7 t0.'L 1( Fi In am) 7 writ0('YYY END of LOG YYY')? told? 1013.( 0 u t p u t)
3. o d( n 1)- 3. o a.... f i 3.0( F :i. 3. n a m) 7 103.1 :i.nd (Output) 7
10 3.3. (F :i. 3. n -a m)? ri 3.7 1013. 0 u t p u t)»
3. O( M 0 3 S) 3. o :i... r :i. 3.0 F :i. 3. n in) 7 10 3.3. :i. r» d( 0 u t p u t) 7 t e 3.3.( F i 3. ri a m) 7 w r :i. 10 (ii 0 a d)? i 'i 3.? i« .1.7 10 3.3.((!) u t p 1..11). Partial list of system parameter c o i 'i d :i. t i a n( p r o v i. d 0 d)• c o n d i t i 0 n (:i. f)« v 0 r b (b 0) v % S..„ O P( 'Hi.... 0 IT O), Bi y 0.„. '..J P C .1. 0 Bi:)• Bi B Bi.... 0 P( fYl(!) r 0)• Hi 3 3.... O P( Bi 0}. IB Bi.„. 0 P( 0 Q U 0 1){. a y s.... o p'• i'fi 0 iti b 0 r)• Bi Bi.... O P 0' J. 0 If) 0||).. a 3 s_. o p hi 0 a d)« s y bi.... o p (ii e t.... e 10) IB 3 3.... O P( t3 1. X)- S 3 Bi.... 0 P( C.!. 3 LI S 0)• s 3 3.... 0 p (t :i. i n 0 a) sy a....op (in i.mis)• bi- y b.... 0 p( 1 u a) bi 3 a.... 0 p( d .i. v i. d 0 d)• b y a 0 p (in 0 d)• a 3 bi 0 p( i i t 10 Bi 0 1-). bi 3 a.... 0 p( a 10 itt)• a 3 bi.... 0 p -a t Dm:i. c) IB BI„.. 0 P( C 0|| IB Li 1 t) a 3 a.... 0 p( r 0 c 0 n bi li .1. t) bi y bi.... 0 p( t- 01 r a c t)• ib y bi.... 0 p 3 bi a 0 r 13) a 3 a„. 0 p( r 0 a d) a y a.... 0 p w :i. 110 i t)- a y a._ 0 p( f a i 1)• a y bi.... 0 p (.Bi 01) bi y a._ 0 p( a 010) a 3 a 0 p (p T- :i. n t) bi y a_.. 0 p( r 0 p t 0 a t)• a y a.„. 0 p (10 n Bi t h)• a 3 bi.... 0 p (a t r 10 i t) a y a._. 0 p( w r :i. 101 n) bi y bi._. 0 p( c 0 r'i 0) a y a0 p( a 1 r 0 0 ii 0) a 3 a.„. (j p (t r li 0 bi y a.... 0 p( bi 3.... 1 a 0)« a 3 a.... o p T 0 p 1 a e 0 d) a y a..„ 0 p( 0 m p 13)• a y a._. 0 p( 0 q u :i. a t r li)• d bi 13- w 0 r d( li i t :i. t y)» d a t a.„ w 0 T- d (h 0 a d)• d a 13- w 0 r d( 0 m p t y)• d 313w 0 T d 13 :L 1• d a 13.... w 0 d (0 0 li a 1) d a t a„.. w o r d( bi 0110)« d 313- W O i' d( i'T 0 t.... 010)'•
e 0 i t t r 0] w 0 r d (c 1 a li bi 0)• c(.') i t t r 01 w 0 r d( r 0 r- 1. a 0 0 d) c. c 0 r i t r 01~ w 0 r d( 3.1. .1.0) C 0 I T t r O 'I W 0 Tv d( Bi 11)» c 0 i t t r 01 w 0 r d( a 3_.. .1. a 0
a p 0 0 i Bi 1.... w 0 r d (3 J. 10)• a p 0 0 i 31.... w 0 r d (Bi 11)• bi p 0 c :i. Bi 1_.. w 0 'r d( i t 01) $. c h e c k :i. f t h e c h a r a 0 t e r s a r 0 n 11 ivi b 0 r ft
n u iti b 0 r( X)- a t o iti( X)• n a me( X v X.!.) Il U III I( X].)
ri 1 j in I( II) ii 1..1 lit J.(!.. X! 1 1)-• 1 Y A 7 tt Y•;' A.« I 11 r« 1! iv I(' I. I 1 K 1 R 0 H: d i n -l:; s 0 n t0 ri r- 0
f' 0: d I.. W i I'J•..!) P [m.) y j% P ij l..J P V Q' IJ•- U» IJ)•,»; 0 P {1.' !;j.«]']'|.»; A I
ft G i v 0 ii a w o r d a ii d t h 0 c h a r a c t0 r a f 10 r :i. t y r 0 a d :i. ri, t! 10 r 0 st o f s 0 n 10
restsent(„ y C y L' 1):- last.._ehar C) y a010(....) y!
r 0 a t s 0 ii t( W y C 9 r: W1! W s~.) t•••• r 0 a d w o r d C? W :l. y CI) y. r e•: t a 0 n t( UjI? C1? W a).
1 7ft R 0 a d x n a s 1 n S 10 w o r d y a :i. v 0 n a n :L r'i i t i a I c h -a r a e 10 r y and rememberins what character came after the word ft 7ft sinsle...char are c h a r a c 10 r s t h a t f o r m w o r d s o n t h 0 i r o w n ft
resdward( C y W y C1) J- s :i. ri a 10.._ c h a r( C) y! y n a m 0 (W I. C.!)? a 010 (C1), readword(C?W«C2) t-
- in_word(C?NewC)v !y aetO(Cl)y. restwo rd(CI? Csy C2)t
n a iti 0 (W y C N 0 w C! U s ..I) r P fi IaI fi V i't( v IaI m I a P 1. 0 (11)«? V P 1 i LJ fi 'f f i (11 i» Ixl I 'J) a
r 0 s t w o r d( C? C N 0 w C! C a I? C 2) t- i iiw o r d( C? N 0 w C) 9!? S 0 t() C 1) y restword(C1CsyC2), r-.w.i-, 1%-t( r.. r i„ r
1 T ri 0 s 0 c 1 1 a r a e 10 r s c a n a p p 0: w :i. t i'i i n a w o r d t The seco n d c I a u s 0 c o n v 0 r t s o hi a r a r- f. 0 r a 1 I, u 0 r c 3 3 0 t
in-word(CfC) C96y C123, in._.wo rd (C? L) t~ C64? C91r L is C+32. A B Z :-K, i ri__ w o r d( C y C) t~ C! 4 7 y 5 6 1 1 2 9 K.
in_word(95r95)- i n._ w o r d( 4 3 y 4 3) 1 t T i ri™wo rd( 45 y 45) 7' 3. n.„ w o r d( 4 2 y 4 2) 7 T T 7 J .J A••} A 7 '.i'
I ci f }i r i 4.'! I.
graphic operator for DEC's VT100 terminal
C J. ll 3 I'i'l 0( V i.. V.: v '.j{)!•' 7 4..!) ij j. v.. p j. 3 y'•
a s!• p o s$-• i- 0 p e a t. w r i t e(•' I i. n e n».1 m |j 0 i?), r e a cl( X; w r :i. te(' ca J. Limn n imbe r?')• r can( X)... pn-;( X- Y- ...... ••• y .J A .v A .V..• .4 A'.•.•. ... j''••••... j(. tt f ,m I. J ii.•A.. .'..I I 11»•. 1. J -.J.' 11 i;•«».,I. 1 I l ii
w 1 :i. t 0( p 0 a- :i. t :i. 0 n o u t- o t' 'a- c r e 0 r,') y 1;! 9f a :i. .1. 1 p o a( X y Y) t•••' ii a iti 0 :i. ii t X• .X 1.•• ii a m 0 i ri tY y Y' .1) y append Y'!.• l72j y Pi) v 3 P P 0 ll Cl'• X.I. I.X.'j J...! I') 1 ii a iti 0 (I' y I. 2'!•' y 1 I I' 2..!' y
I display(P) savepos t- nam0 (F:' y I!!27 y l552) y d :i. 1?,p 1 ay( P)+ res t p 0 s- n a m 0( P y C! 2 7 y 5 6II) v d :i. s p I a y P) 1 APPENDIX VI SAMPLE PROGRAMS
Sample one
p r o p s s
PROLOG Version NO7, :l.
loading propss.. propss1oaded « t T'- a !i S f O I !Yl
n a i n e o f t• i e p r o b I© cm t f a c t o r :i. a 1«
d© s c r :i. p t :i. o n x :i s a f a c to r :i. a 1 o f y p r- o v :i. d e d i f y :i. s 0? t h e n i:i :i. s:!. y
o t h e v w :i. s e y x :i.% t h© p r o d u c t o f y a n d t h© f a c t o r i a 1 o f m :i. ri u a y b y
recognise i y is an object of th© problem? recognize y recognize word minus recoSniz©wordfactoriaI r© c o S n i z© y recognize word times x is an object of the problem? recognize y, r© c o S n i z e w o r d i f
t i m e s i s a f u n c t i o ri w i t h a r S u m© n t s I z? y y s y v 2 .I r© c o 3 n i z© 1 r© c o d n i z© z z i s 1. r© c o d n i z e 0 r© c o s n i z© y y i s 0 r© c o d r i :l z© y recoiiniz© wo rd facto r :i. a 1 r© c o S r i i z© z f a c t o r :i. a I i s a f u n c t i o n w :i. t h a r S u in e n t s L' z y y! C2JC1rINThe Prolog cl a u s© s a r© d© r :i. v© d a s y
f actorial (1 y 0)• f a c t o r i a1( z y y)$- syvl is y 1y
f a c t o r i. a 1( s y v 2 y s S:' v 1) v x is y syv2 c a t f a c t o r :i. a 1 1 o«
START I!! f a c t o r :i. a 1 II
L••• 9 :i s» a y factor ia 1? of y y y p rovi ded y :i. f y y y is y 0 y? y then y x y i a y :L y? y otherw:i. se X y i 5 y Li 10 y p rOdllC L y (!)T y y y and y tii0 y T SC'to r i 3 J. y of y d i t'fp rorio-o•• nf u'•••!»'! i
!.. X y i 3 y I 3 c t(!) I- !i. a .1. y y y P Y'(!) V j. d 0 d y ;i. f V y y !i. S y() y y y I', j) 0|'i y X y ;i. 3 y 1 y y y Q11 l e T W!i. S 0 y X!•' !i. p r 0 d u c t y y y f a c t o r :i. a 3. y d :i. f f e r e n c e y -a y :L II f. 1 I!.. i L X 9 b 0 9 T 3 O L O V :i. 3 J. y :i 9 P r O V .'i. u 0 d y y D 0 9 0 9 9 y 1 i i 0 li y i y b 0 y 1 y y y i 1v y y b 0 y t, ,'i. I'fi 0 S y y y f a c t(!) r :i. a]. y m:i. r i u s y e; y 1. I
1 I.. 1 y'- y I'll :i. I i '..I S y T 3 C t(!) f ;i. 3 .1. y y y t !i. ITi 0 S y;•( y :i. T v b(!•! y 1 y X y t fi e I i y b 0 y 0 y '3 y b 0 y!.- y t'•) Ct(!) V' !i. 3 J. y I X y b 0 9 P r o V i d 0 d ::i r
C1 y s y e n d I
I y y 1 y s a... e n d 1
L m in us( a? 1.) y s a._ 0 n d 11
r. facto rial (syvl) y s a rev( m i nus( swvl y a y 1)) y s a_. 0 ri d 11
II a y factor i a 1( s a v 1)? a a.... rev( m i nus( syv 1 y a y 1)) y sy„end II I•---•
III i Hies( a y s a v 2) y s a... rev( facto r i a 1( sa v2 y syv 1)) y sw_ rev in i nus( syvl y y y 1))? a
Jlx y times( a y ssv2) y sy__rev( factorial( swv2 y syvl)) y sy.rev( minus( syvl y a y 1))
L' x y t i mes( y y sy v2) ysa.... r0v( f acto r i a 1( sy v2 y sv 1)) y sy.... r0v( mi nus( ssiv.1. y y y 1)) s.... r 0 v (:i. f) 9 s y.„ 0 ri d
[sy„erid y :i. f y rr :i. nus( sav 1 y y y 1) y rac Lo) :i. a 1( sy v2 y sy v 1) y t im0s( x 9 a? sy v2) II
[; i y; a 0 n d y i f y m :L n 1.1 s( s a v 1 y s' y'l)? f a c t o r i a I. s si v 2 y s y v 1) 91. :i. m 0 s( x 9 a y s a v 2) II
II x y 1 y s y.._ e n d y i f y m i n u( s a v 1 y y 91) y f a c t o r i a 1( s y v 2 y s y v 1) 91 ;i. m 0 s( x 9 a y s y v 2) II
II x y 1 y s y e n d y i f y m :L ri u s( s y v 1 y y y 1) y f a c t o t% :i a 1 (s a v 2 y s y v 1) y t .i. m 0 s(;•( y y y s y v 2; J
C s y-.. 0 ri d y s 0 L t o( x y 1) y i L y m i n u s( s a v 1 y y y 1) y f a c t o r .i a].( s y v 2 y s y v 1 y t i m 0 s( x y y 9
II0 y sy_.end y set to( x y 1) y i f 9 m i nus( sy v 1 y y y 1) 9 f actor i a 1( sy v2 y syv 1) y t :i. mes( x 9
C y y 0 y s y- e n d y s e 11 o x y 1) y i f 9 nri .i. n u s( s y v 1 y y y 1) y r a c t o r i a 1 s y v 2 9 s y v 1) y t i m 0 s
C s y 0 r i d y s e 11 o y y 0) y s 011 o x 9 1) 9 i f'% 9 m :i. n u s (S) a v 1 y a 91) y L a c t o r i a 1( s y v 2 y s y v 1) t i m 0 s( x y y y s y v 2) II
C y y s y_ 01| d y s 011 o( y y 0) 9 s 011(!) x y 1) y i f y m i n u s( s y v 1 9 a y 1) 9 f a c t o r i a 1( s y v 2 9 s a v h i m 0 si( x y y y s a v 2) J
I f a c t o r i.•) 1( y) y s a._. e n d y s 011 o( a y 0) y s 01 L o(;( 9.1.) y± f y iti :i. ri u a( s y v 1 y a y .1.) y f a c t o r i a 1( s y v 2 y s y v 1) 91 :i. in e a( x y a 9 s a v 2) ..I II x y f a e t o r i a 1 (y)• ay.... end y a e 11 o (y y 0) v s e 11 a (y 1)• i f v in :i. n '..i a (ay v 1 y y y 1) y I f a e t a v :i. a 1( a a v 2 y a y v 1) y t :i. rn e a( x y y y a y v 2) II
II 'a y.... e n d y f a e 1 o r :i. a 1( a y y) y a e t1 o y y 0) v s e tt o (x y 1) y i f y m :i. n u a (a y v 1 y y y 1y f a c t o r :i. a 1( a y v 2 y hi y v J.) y t :i. in e a (x y y y -a y v 2) II
C f a e t o r :i. a 1( a y y) y£•••• y a e 11 o (y? 0) y a e 1t o x y 1) y• y f a c t o r :i. I (x y y) y i•••• y m :i. n '..i a( a. y v 1 y y y 1) y f a c t o r :i. a 1.( a y v 2 y a y v 1) y t :i. rn e hi (x v y y a v 2) I!
II f a e t o r :i. a I( a y 0) y t II i i
'Cf3ctoria 1 (:l. »0), ;~:i ' I «» A 4m-Tr -X-
•I- v...... «...
R 3 lYl 0 O t t, hi(» o»}•, P, I
ci 0 s c i' :i. p I.. .i n n t
:»:• c:: S Li C C u T r. P r O V L i j 0 f j i T x i s !i e x t.... b 0 y? e I s e i f z j. a n 0 x t.... t o y? :i I 1Z! 'j. S Z' 'S I j C O Q Z•
.1 S: i I (..i i.j J 01. t(!) T I. ji 0|- Q |i j 0 j|'[ z 0 e o s !i :i. z 0 z
recognize word succ x is an object of the problem? r 0 c o£ n i z 0 x
s u c c i s a f u ri c t :i. o n w :i. t1 1 a r u m 0 n t s C x v z 1 a is an ooJ001 (!)T tn0 pzob.1.0in r e c o a n :i. z 0 a
n 0 z tt o i s a n o b J 0 c t o f t h 0 r o b .10 m? n next._.to is a function? r 0 c o a n i z 0 w o r d n 0 z t.... t o r 0 c o a ri i z 0 z r 0 c o a n ;i. z 0 w o r d :i. f n 0 z t„ t o i s a f u n c t i a n w i t hi a r a u in 0 n t s I z i r 0 c o a n :i. z 0 y r 0 c o a n i z 0 w o r d n 0 z t... t o r e:::• o a n i z 0 z n 0 z t_. t o i s a f u n o t i o n w i t h a r Li u m 0 n t a I z? y r 0 C O Li ii 1 Z 0 r0coJni z0wordsucc r 0 c o .a n :i. z 0 z s u c c i a a f! .1 ri c 11(!) 1 w ;i. t I) a r J u m 0 n t a!.. z 0: ..I [! 2 -.J il 1? J. H T1! 0 P r(!) I o a c 1 a u a 0 s a r 0 d 0 r !i. v 0 d a a
s u c c( z? a)- n 0)•( t.... t o( z a) s ij c c( z v a) t••• n 0 z t... t o( z m y) y s u c c( z y z) I c e t si 1..1 e e 1 o s
START :tt:={ II s u e c II
I x v i si? s 9 «i u e c» o f v y p p r o v :i. d 0 d? :i. f y x ?:i. s? n 0 x t.... t o y 9?• e ;i.$ oi f,, j $n e1 j 0,...,,-, t h 01!!• x y :i. y 3 y 5 1.1 c e o f y;'|
L x: i s? s '..1 e c 9 y y p r 0 v :i. d 0 d v :i. f y x p :i. s n 0 x t.... t o si 7 5? 01 s 0 p .1. f y z P ;j. 3 y n x t.... t o v y v? p t- h 0 !i 9 X y :i. S p S LI C 0- y Z II
II x be? succ y y y p rov :i. ded y x v be y next... to• y v» :l. f y z... be p next.... to y y y? y then v x y be» sue l
L Z y|SLICCf y X y then y be y y y next ...to y Z y :i. t' y be1 y y y next.... to v X y bO•'- SLICC y X y be y P rov x ded;
IIzI y 'sy-endll
II Si LI cc(z) y Si y 0 li d II
IIX y S i J C C( Z) y Si Si.._ 0 li d II : II xpsucc(z) y s y.... 0 n d U
II s y.... 0 n d y s u e e (x y z) II 1 -
IIy y sy-end y succ (x y z) H i 4.. II ri 0 x t.... t o( y) y s y„ e n d y s u e c x? z) U
II z y ri e x t.... t o( y) y si y.... e n d y si u::• c x y z) U
Liz y next....to( y) v siy....rev( i f) y sy....end v SiLicc( x y z) II
II s y_. e n d y :i. f y n e x t.... t o( z y s) v s u e( x• z II
H y y sy„. end y :i. f y next ....to( z v si) succ( x y z).! t: n e x t„ t o( y) y s y.... e ii d y i f y ii e x t... t o( z y y) y s u c: c( x v z) II
C x y n e x t.... t o( y) y s y.... e n d y i f y n e x tt o( z y y) y s u c c( x y z) II
II si y e ii d.» n e x tt o(' v y) y :i. f19 r i e x t... t o( z 9 y) y si u e c?( x v z) ..I
IIy y sy—end? next....to( x 9 y) y i T y next....to( z y y) y siucc( x y z ..I
Csucc( y) y sy-end y next....to( x y w) »:i. f y next-to z y y) y succ x y z) II
fix y succ( y) y sy-end y next....to C x y si) y :i. f y riext.... to( z y y) 7 succ C x v z) J
II ssi....end y succ (x 9 y) y next-to( x y si) y :i. f y next-to( z 7 y) y succ( x y z) J
Ccjijcc( x y y) y«-• 7 next-to( x y y y y succ( x y y) y J- 7 next....to' z y y) 7 slioo( x 9 z) I
% THIS PAGE IS INTENTIONALLY LEFT BLANK Samrle three
1 Vs r» P r% v. iv, ii 3 in e ot h e P i' o b I c iii t 3 if h s e t„ d 0:::• C P I p t i O Pi t z i s 3 s u b s e t o f y i f z :i. s e it, p t y 1 S 0 j. 1' 3 I 1 0 J. 0 I'll 0| I f;..». O f; 3 p 0 111 0 |f| iVi 0 I Q I-'.
y is a it o i j J c t oi the p r o l.v« J. 0 in i' r 0 c o si n i z 0 y
P 0 C O 31! 1 Z 0 W O P d I'll 0 FIT b 0 P z is an object of the problem? recognize z ass u m 0 010 iti 0 n t s p p o c 0 s s 0 d 1 ii s 0 o. u 0 ii t i a J. o r d 0 r 'T recoSnize word if h 0 a d i s a f u ri c t i o n w i t h a r 3 u m 0 ft t s I. s y v 1 ?z 2 r 0 c o 3 n 1 z 0 w o r d 0 in p t y r 0 c o 3 n :i. z 0 z 0mp ty is a f unc t ion wi thi a r Jum0nts Cz1 r 0 co Snize y recognize word subset recoSniz0 z subset is a f unc t i0n w:i. tIt arsuiti0nts Cz? y .J C2JC1rlHThe Pro 1 od clauses ar0 d0r i v0d as?
— please check if the boundary case is correct!! s u b s 01( C 1« y)-
s u b s 01( Z 3 9 y)• s u b s 0t( C s y w 1! z.... 12 y t- membe r'• sy v 1 y y.
subset( z.. t y•'•, , c a t s u b s 0 t +1 o d
START Rsubset II
I•|• - x?- o s 0 t? o f v v :i. f• x y :i. s? e in p t y y? y 0 J. s 0 9 :i. T y a 11.? 0 I 0 m e n t a y 0 f y a v a c- 0:• in© m h 0
C X?; i S? S LI b S 0 t? 'rJ y i T y X y :i. S V 0 III t V» 0 j. S 0 ;i. f'•],] 1. p] 0 j|[ 0 |,y X a!• 0 in 0 III b 0 T•' b!
L X y be v Subset 9 b y i T y X y be y 0IYiPtb y y y ,'i. T y a 1 I. v 0 J. 0 merit y a y be y in 0 III be'('• y b I!
I., y y iyi e in b e r» a y 010 in e n t y a I I. y :i. f y b 0 y 0 in p t y y x y b 0 y y y s 1..1 b s 01 x!' b 0 r q v :i. d 0 d 11 i I C y y s y.... 0 in d I « j 1: iti 0 iyi b 0 r( y) y s b.... 0 n d I
I. X y ITi 0 IYi b 0 r( b) y S b'.... 0 li d I t h e a d( sy v :i. y x) y s y.... r e v (in e iyi b 0 r( s y v .1. y y)) y s y.... 0 n d II
II h e 3 d( s y v 1 y x) y s y.... r 0 v (a II. 1.) y s b.... r 0 v C iyi 0 iyi b 0 y- C s y v 1 y y)) y s y„ 0 n d U
II h0 a d( s y V :L y x) y s b.._ r 0 v( a11) y y.... r 0 v (in 0 rn b 0 r( s y v 1 y y)) y s y.... y 0 v( i f) y s y._ 0 n d II
l C'.sy-end y i f y mem be r (sy v l»y) y a 11 y he3d (sy v 1 y x) I! i II0 Hi P t y y S y.... 0 H d y :i. f y IYi 0 IYi b 0 T'( S b V 1 y b) y a 11 y h 0 a d( S y V 1 y X) I!
II X y 0 IYi P t y y S y„ 0 ri d y .i. f y IYi 0 IYi b 0 r S y V 1 y y) y O 1 J. y Ii 0 a d S y v 1 y X) I!
C S y- 0li d y eIYiP t b( X) y :i. f y IYi0IYi b 0 V( S b V 1 y y) y 3 I .1. y h 0 d C S b V 1 y i) 1
H b y S b™0 r'l d y 0 IYi P t y() y i f y IYi 0 IYi b e Y( S 3 v 1 y y y I 1 y h 0 Q( iii y V 1 y; i) II
r s u b s e t( y)? s y.... e n d y e iyi p t ib( x) y :i. f y iyi e iyi b e r( s y v 1 y x) y a 11 y i i e d (y v 1 y x) II
C x r s u bset(y) y s y.... e n d y e iyi p t x- (i-i) y :i. f y iyi e in b e r( s y v 1 y b) y a 11? h© s d C w v 1 y i-i) II
tab-. 0 in d y s u b s e t( x y b) y 0 iyi p i y (i-i) y i f v iyi 0 in b 0 r (v 1 y y) y 3 J. 1 y h 0 a-i d( s b v 19 x) II
II subset( x y y) 9 t- y empty( x) y y subset( x y y)$ 9 me in be ys( sy v 1.? y) 9 a 11 y head( sbv 1•
Csubset (IIII y y) y -3
II s u b set (i-i y y) y? m 0 iyi b 0 r( s y v .1. y y) y s u b s 0 t k x v x) ..I
II s u b s e t (II s y v 1 I i-i.... t U y y) 9 t•- y m0 iyi b e r( s y v .1.9 3) 9 s u b s 01 (II s y v .1. I x.... t II y y) II
My I. n r» iv« r..•. -f% v. J--.'! v.•... 1 .1.... .1.•!
d e s c r :i. p t :i. o ii
1 :i. s a c 1 :i. s t :l. p r o v :i. d e d a .1.1 e J. e in e n t s o f I :i. s?• e t n o d e« getnode is an object of the problem? n getnode is a function? r e o g n :i. z e w o r d J e t n o d e I is an object of the ppobi.em Y r e c o g n i z e I ass u m e e 1 e in e n t s p r o c e s s e d i n s e® u e n t i a .1 o r d e r? head is a f urict ion w:i. th arguinents 11sav.1.• I i recognize word elistl r e c o g n i z e 1 c 1 i s t1 i s a f ij n c t :i. o n w i t h a r g u in e n t s C. 1 J r: 2 JI 1? 1H T h e P r o :i. o g c 1 a u s e s a r e d e r :i. v e d a s•
~ please check if the boundary case is correct!! c 3. i s 11( Z Ii) e 1 :i.st1.( [syvl! It.J) t getn.ode(syvl)v •|•• j y|| cat o 1 :is tl, 1 oa
?K START r c-1 -i q+.i i
C J.? :i. s y a y e I. :i. a t;|. p r a v i. d e d y a .'I I y e© m©n t a y o t 3. y i a? d e t n c d ©I! I ij J. f i 3 y(.. X .i. S L J.? P V X' I 0..f f: 1 J. y 0 .1. 0 III 0 P| t y J. V j. y s 0! i| Q Q 0 J, Ii
LI 1 y b 0 y C1 i S11 y P p O V :i. d 0 d y a I 0 y I b 0 y 0 t ii o d 0 .I
C £s©t n O d 0 y 1 y a .11 0 y b 0 y c 1 :i. S t 1 y 3. y b 0'? v P Vs o v :i. d 0 d!
C£ 01 I i ad 0 y a v..._ 0 n d II I
CI y 01n0d0 y Sy....0I'ldI
I II h 0 a d (a y v 1 y 1) y sy.... r 0 v (a 3.1 ©(l))y a a.... r e v(© t n o d e (a y v 3.)) y a y....© n d II
II a y.„ 0 ii d y d 0t ii o d© a y v .1.) y a 11 e (1) y h© a d( a y v 1 y 1) II
C c 1 :i. a 11 y a y._ e ii d y a e t ri o d©( a y v 1) y a 3.3. e( 3.) y h© a d( a y v 3. v 3.) II
C 1 y c 3. i a t1 y a y.... e n d y d e t ii o d e( a y v 1)? a 3.3. e 3.)• h© a d (a y v 1 v 3.) II
C a y... 0 i i d y c 1 i. a 11 (3.) y 3 e t n o d e (a y v 1) y a 3.3. e 3.) y h© a d C a y v 1. y!.) II
C c 1 i at 3. (3.) y J•••• y act node( ay v 1) y a 3.3. e (3) y head( ay v 1 v 3. II
C c lis t1 (3.) y I- y .d© t ci o d e C a y v 3.) y c 3. :i. ai 13. (3. t) .11
C e 3. i a t3. (I!! a y v 3.! .1.... t II) y t•••• y a e t n o 3©( a y v 3.) y e 3. :i. a t .1. (3. t) II
% Sample f i vp
a h r fi P n r iii a
ii a in e a f t h e p r o b X e n t o r d e r e d•
descriptian a
z i i s o r d e r e d :i. f a X X t h e e X e in e r i t s o f z :i. s s in a X X e r t h 3 n t h 0 n 0 y t 0 J. 0 n 0 r 1t i r 1 y.
x is an object of the problem rocosn ize z r 0 c o$ n :i. z 0 w o r d n 0 z t.... 0 X 0 recognize word loss r 0 e o g n i z 0 z assum0 010its0rits p roce ss0d i n s001..10pi t i aX o y• d0 r? h 0 a d i s a f u ri c t i o pi w :i. t h a r s u in 0 n t s I s y v 2? z! r0coari iz0word0rd0r0d r e c o a n :i. z e z ordered is a function with arguments Czl! Z2 J II1? 1HTh0 P ra log c 1 aus0s a r0 d0 r i v0d as s
— p10as0 check if the boundary case is correct!!
ordered(C II) orde red (Csyv2 syv1! z.„.1H)$- t : s y v 2 s y v 1? o r d 0 r 0 d (II s y v X! z.... t II) a A C: 3 t O r d 0 V 0 u• .1. o s
' START II o r d e r 0 d II
L. x v :i. a 9 o r d 0 r 0 d y :i. f 9 a J. J.? 0 .'1.0 m0 n t9 x 9 :i. a? I 0 s a? n 0 x I, y 010 m0 n t y x j 1
C x ds? o r d 0 r 0 d? :i. f y a 11? 010 in 0 n 19 x 9 :i. a y 10 s a v n 0 x 19 0 I 0 in 0 n 19 x J
II x 9 b 0 y o v d 0 r 0 d 9 .i. t 9 a1 10? x y b 0 y 10 a a y in 0 y, t 010 y y, II
II X 91| 0 X t.... 0 1 0 y 10 3 3 y X y 3 I 1 0 y b© t O r d 0 T 0 d y X y b 0 y P r Q V i d 0 d J
II X y S y.... 01't d II
II n© x t.... 010 x) y s 0 n d II
Z less( s«vl) y sy»r0v( next-ele( sy v 1 y x))? sy0ndII
Lx y 1 ©ss( sy v 1) y syr0v( next-e 10( sy v 1 y x)) y sy.„.0nd II
II h 0 a d( s y v 2 y x) y s y.... r% 0 v( a 110( x-)) y s y» r 0 v (J. 0 s( s y v 2 y s y v 1)) y s y.... r 0 v in 0;; t010( s y v 1 s y.... 0 in d I!
II ii -.i.... 0 n d y li 0 X't.... 0 :i. 0( S y V 1. y X) y I© S S S y V 2 y S y V 1) y 3 .1. 1 0( X) y ii 0 3 d( 3 S V 2 y; .5.]
II0 T'- d 0 r 0 d y y.... 0 n d y it 0 t,.... 010( s y v 1 y) y 10 s( a v 2 y a y v j.) 9 3110(..)? I i 0 a d( a y v 2? x) J
II x y o r d 0 r 0 d y a y.._ 0 pi d y ii 0 x t..„ 010( a v 1 y x) y 10 a a( a y v 2 y i-i y v 1) y a 1.1.0( x) y 110 a d( a y v 2 y x) II
II a y... 0 n d y o r d 0 r 0 d( x) y n 0 x t.„. 0 J. 0( a y v 1 y x) y 10 a a a v 2 y a y v 1) y a 1.1.0 (x) y h 0 a d( a y v 2 y x) II
Cord0red(x) y™ y n 0 x t.... 010( a y v 11 x) y 10 a a( a y v 2 y a a v .1.) y a 110( x) y h 0 a d( a y v 2 y;) II
LI o r d 0 r 0 d( x) y~ y 10 s a( a y v 2 r a y v 1) y o r d 0 r 0 d x.... t) II
Corder©d( Cayv2 I x-t3) y£™ y 1 ©aa( ayv2 y ayv 1) y ordered( x....t) II
% Sample six
b T'::• ll S 1 OT'' ft'l%• ll 3 ITl 0 O T t il 0 P'!• Q b .1 0 ill t il 3 ll O 1.... t Q W 0 V• d e s c r i p t :i. o n t f problem of hano.i tower with n discs is defined as n (ii s i... s are moved i rom the J e f t....o J. 0 bo the r ighf... pole W 1 t1 I t M 0 l'l 0 .1. P U!' b I i 0 C 0 I? t 0.... o .1. 0.%
cent re-pole is an object of the problem? r e b o£ n i z e c e n b r e.... p o 1 e 1 help is an object of the problem n h e 1 p i s a f u n c t i o n? r» i Just ignore help? right-pole is an object of the problem? recoSn i ze r i gh b....po 1 e left-pole is an object of the problem? recog n :i. z e 1 e f b.... p o 1 e moved is an object of the problem? n m o v e d i s t a f u n a t i o ri? recognizei word moved disc is an object of the problem? n disc is a function? n Just ignore disc? n is an object of the problem? r e c o g n i z e ri m o v e d i s a f u ri c-1 i o i! w :i. t h a r g u in c-.' r i t s I., n y .1. e f b.... o.!. e;•= r i g i; t.... p o J. e v c e n b r e....; v e c o g n i z e n r e c o g n :i. z e w o r d h a r? o :i b o w e r i'i a n o J b o w e r :i. s a f u n c t i o n w i b i) r g u in e n b is II' n J i: 2 J I 1? 1H T hi e P r o 1 o g c: 1 u is e s c? r e d e r- i v e d s
h a n o :i b o w e r (ii) t™ iti o v e d( n v 1 e f b.... p o 1 e v r i g ii t.... p o ,1. e v c e n t r e.... p o 1 e) c a t ii a ii o :i 1 o we p• I q
STARf%%% I h a n o :i.... t o w e r I
!• J. 0 111 v(.) I y l 131 i(.) i t u W(v.1 I' y Wj. I i i y 11 y d j. 3y j. a y d 0! j. i 'lGQ 8 S y! i d !i. 5 C y J. 3 y !Ti O V 0 d y f r a m y t h e v 10 f b.... p o I e y t 0 v t h e? r :i. d h t.... p o 1 e y w i t h t h 0 y h 01 p? 0 f» te? 0 0 n t p 0.... p 010 ;;j
L p r 0 b .1.0 in y 11 a n 0 :i.... 10 w 0 p y n y d ;j. a c y :i. a d 0 f i n 0 d y n? d :i. a y 1 a v m0 v 0 d y J. 0f t.... p 0I0 y r j. :~5 h t... p 0 J. 0 y ri 0 j. p c 0 j-i 11- 0.... p o;|. 0 ;;i
!-• r 0 s 1 j .1. t y h a ii 0 :i. i o w 0 'f y ii y d i s c y b 0 y p p 0 v :i. d 0 d y r i v d i s c v b 0 y iyi 0 v 0 d v !101 b.... fv 010? P i i~: h t.... P 0 .1. 0 y h 0 1 p y C 0 ii t 0.„. P O 1 0 TJ
L C.%0 ii t P 0.... P 0.1. 0 y h 0.!. P y P :i. I I b.... P O.1. 0 y I 0 r b.... P 0 .1. 0 y ITi 0 V 0 d y d i S C y li y b 0 y d i.•••:• C y Pi y i 1 a li 0 i„ b OW0 t r 0 s 1..111 y b 0 y p p 0 v :i. d 0 d I
II C. 0 r-| t P 0.... P 010y S 3.... 0 li d II
C c 0 ri b 0.... p 010 y a y0 n d 1
II r .i d h t... p 010 y c 0 n t 0.._ p 010 y s y.... 0 n d !!l
[! 10 f t.... p 010 y p i d bi t_ p 010 y e 0 r i t r 0 p o 10 y s y 0 ri d II
L no 0 v 0 d( .1.0 f b.... p 0 J. 0 y p i d h b p 010 y c 0 ii b p 0.„ p 010} y a y.... e n d II
II in o v 0 d (10 f tp 010 y r :i. d h b.... p o .10 y c 0 n b p- 0.... p o 10) y c: y_. 0 r i d II
II li y III 0 v 0 d' .1. 0 f t P O I 0 y p i d h t.... P 0 I 0 y C 0 li b P 0.... P 0 I 0 y S 3... 0 !i d II
II 3 3... 0 !i d y Ifi (J V 0 d(. |i? .1.0 b i,„.. P 0 J. 0 y r i d h b.... P o .1. 0 y C 0 !i b I- 0 P 0 J. 0) ..I
LI a y.... 0 n d y ir o v 0 d( n y 1 e f t.... p o .10 y r :i. d h b.... p 010 y c 0 n b c%0._ p 010) II
L. r i y a 3.... 0 n d y iyi 0 v 0 d( ri y 10 f t.... p 010 y r i d h b_ p 0.1.0 y c 0 r i b p 0.... p 010) J
LI h a ii 0 :i t a w 0 p (ii) y s 3 0 r» d y m«::) v 0 d( n y 10 f t_. p o 10 y r i d ri t... p a J. 0 y c 0 n b r 0.._ p 010) J
II h a ri 0 :i b 0 wer( ri) y 33 0 ii d y iyi 0 v g d (ii y 1.0 'f b.... p 0.1.0 y 'i% i d ii b p o 10? c 0 ri b p 0._ p 0 i ox..1
C ay 0iid y han0 i.» b0wer( n) y iti0 v0d (ri y .1.0i% bp0 J. 0 y r .i. dh bp0.1.0 y c0ri b p0.._p0 J. 0..!
i;; h a pi 0 :i 10 w 0 p (ii) y t- y rn 0 v 0 d n y 1 e f b.... p 010 y r i d i i b..„ p 0 j. 0? c 0 n 11 0p 010) II
u ' v Jain Die sevcn t V 3 I'i 3 f O P IVl ri a in e o f t in 0 p i- o b 1 e m• in o v e d, de3eriptiant n discs are moved from left to r:i.3ht with the help of tine centre p rovided i i m :i. s n•••• ;j.?
and iti discs are moved from the left to the centre
w :i. t ii t ii e h e 1 p- o f t h p y. ;j p;•••, t t, t h e n t h e u s e r :i. s :i. n f o r m e d t o m o v e t h e s s i d i s c t o r :i 3 I, l 5 t h e n m d i s a a r e m o v e d f r o iti t h e e e n f y p t t h e r :i 3 i 1t wi th thie he I p c« f tine lef t.;
!. e!' t i S 3 ll O b J 0 C t O f t i| 0 p r O b I. 0 I'M r e o 3 r, ;j. z 0]. 0 f t
help is an object of tine problem? n hi 01 p i s a f i..i n c t i.n? n !, .1-•« u• ,Ll i j 1 -.i 1 1 t-j 1 1
r i 3nt ,i.an ob-iei;-1 of tine proh i. em T'
r e c o 3 ii i z e r :i. 3 in t 1 ci'it T'0 is an o0 Jet of Jti 10 p t' (.ni.n J. 0m f
recosnize centre| recognize word moved
d i s c i s a n o b -.J e c t o f t in 0 p r o b 10 in? n disc is a function? n Just iSnore disc?
in is an object of the problem? r e c o 3 n i z e m
m o v e d i s a f u n c t :i. o n w i t in a r 3 u m e n t s t in v c 0 n t r 0 r :i. 3 h t? 10 f t .J recognize r:i.3ht last is an object of tine problem? n last is a function? n Just ignore last? n Could sou write the Prolo.3 clause for it? Cuser1 .i n f o r iti( 1 e f t? r i 3 in t) u s e r c o n s u .1. t e d r e c o 3 rn 1 z 0 r 13 in r' e c o 3 n i z 0 c 0 n t r- 0
t% e c o 3 n i z 0 10 f t r 0 c o 3 n i z 0 w! n d til o v 0 d r e c o 3 n :i. z 0 m
m o v 0 d .i. s a f u n c t :i. o n w i. t in a r 3 u in 0 n t t m? .10 f t c 0 n t r 0 13 i 11 ii— 1 i s an ob-J0c I.- of tin0 p t «jb i. em T recognize n~:i. r e c o 3 n i z 0 m n-1 is an expression to in recoSnize centre
reco3nizeri3iit r e c o 3 n :i. z e 1 e f t r e c o 31 1 i z e w o r d m o v e d ri is an object of the problem r e(. o 3 n J. z e n m a v e d i s 3 f u n c t i o n w :i. t ii a r 3 u m e 111 s[ n 10 f t y r :i. 3 in t e n t r e..! C 2 J L 1? 11-1T h e P o 1 o 3 c J. a u s e s a r 0 d 0 r :i. v 0 d 3 s v
moved(n rlef t? ri3htycentre:- in i s n -1 y iyi o v e d( m. 1 e f t y c e n t r e• r 13 in t y
i n f o r m (J. e f t y r i 3 in t) y iyi o v e d( iyi y c e n t Y'- e y r :i. 3 ii t y .1.0 f t) C 8t IYi O V 0 d 1 03
«K START !Xft I IT| o v 0 d I
L li y '.1 :i. 3 C 3 P 0 y ITi Ci V 0 d? T I- O ITi y J. 0? f t yt 0 y P j. 3 h t y Wi t it t it 0 y h G 1 P y O T y t i10 y C 0 It t P 0 y P r Q V :i. d 0 d y, 3 li u y ITi y d 1 S C y 3 r 0 y Hi O V 0 d y T P 0 In y t it 0 y J. 0 f I;, y I;, O y t it 0 y C 011 t P 0 y W:i. t h y t it 0 y it 0 1 P y 0 f y bit 0 y P ;i. 3 th0It y th0 y US0 r y 1 3 y i l it 0 'PIYiGPj y t0 y ITiO V© y t h0 y .1. 3St y d X SC y to y P i 3h°b y y y t h e n y III y d X 3 C; y 3 P0 y ITi 0 V 0 d y f P 0 ITi y t h 0 9 C 0 It t P 0 y t O y t h 0 y P :i. 3 h t y Wi tit y f it 0 y PI 0 1 P y O f v 1 01' t J h It y d i:: C y 3 P 0 y ITi 0 V 0 '.'J v J. 0T't y P X 3 it t y h 0 J. P y 'X' 0 It t 'p 0 y p P 0 V J. u 0 d y III X 8 y I 1 1 y y y ITi y d .i S C y 8 P 0 y ITi 0 V 0 d y 1 0 f t 9 C 0 I T t P©y h 0 1 P y P j. 3 h t y y y
th0ri y user? :i. s y :i.itf 0pm0d y iyi0v0 y X ast y d:i. sc? y pi 3ht y? y 1 t h 0 li y IYi y d i S C y 3 T%0 y ITi 0 v 0 d r C 0 Pi t P 0 y r i 3 it t y h 01P y 1 0 f t
C Pi y d 1 C y b 0 y IYi 0 V 0 d y 1 0 f t y V :i. 3 h t y ii 0 1 P y C©n t P- 0 y P P 0 V :i. d 0 cf y ITi y b 0 y I T~ 1 y y y ITi y d .i. 3 C y b 0 y IYi 0 V 0 d y 1 0 f t v C 0 I T t P 0 y h 01P y P X 3 PT t y y y bh0ri y userybey inf opiyied y move y 1 ast y dxsc y px3h b y y y t hi 0 ri y IYi y d i S C y b 0 y IYi 0 V 0 d v C 0 IT b P e y P X 3 h t y h 0 j. P y 1 0 T b 1 clef t yhelpy Ts .i 3 h t y C IT t V© y IYi 0 V 0 d y d iscy IYi y t it 0 IT y b 0 r i 3 h t y d x s c? 3.3 sb y rn o v 0 y x n f 0 r iyi d yusery t h 0 it y b 0 y pi3ht9 he1p ?centre 91efty movedy disc9m 9 be 9 n~l y iyi 9 be 9 centrey he1p9rx 3htylefty movedy disc r n y be y provxdedll
C .10 f 19 s y e i t d 3
C 10 f b y s 3 0 ri d 1
C r i 3 It t? 1 eft? s y0 it d H
C c e it trey r x 3 h t y 10 f t y s y.... 0 it d II
C iyi 0 v e d( c 0 it trey ri 3 h t y 1 e f t) 93 y.... 0 n d J
[moved (ceitt r0 y p:i.3ht y 1 ef t) y sy.._end.1
C m 9 moved(centre 9ri3ht y1eft)9sy„end1
C iTi 9 iyi 0 v 0 d (c 0 i t t p 0 y r x 3 h t y 10 f t) y s y.... 0 n d J
C s y 0 n d y rn 0 v 0 d( m 9 c 0 n t y% e y y%:i. 3 h t y 10 f t) ..I
C r i 3 h t y s y.„ e it d y iti 0 v 0 d( m y c 0 it t r 0 y p x 3 h t y 10 f t) ..I
C r i. 3ht y sy....eitd y iyi0v0d( iyi rcent ve 9 p x 3ht y .1.0f t) ..I
C r :i. 3 h t y s y... 0 n d y mo v 0 d( iyi y c 0 it t ts 0 y y%:i. 3 h t y 10 f t) J
C mO V 0( T%X 3 h t) 9 3 y... 0 It d y ITi 0 V 0 d( ITi 9 C 0 It b P 0 y p .i. 3 i 1 b y .1. 0 I I,) J
C p p 01 o 3 9 :i. It f 0 p iti (10 f t y r :i. 3 h t) v iyi 0 v 0 d( iti y (.7 0 n t t 0 y rs x 3 It t y 10 f t) II [.?-• y o 1 o3 v 1.111(.) riii( .1. et b v r :i. 53 hb)? ii'ioved (rn y e0n b re y r :i. 53 hb y 1 eIb) I!
C p r 0 .1 o 5.3 r i n I' o r m( .1. e f t y r :i. 53 h b) y in o v e d( my e e n b r e y p i 3 h t y 1 e I' t) II
II a y_ e n d :i. n f o r m( X e f t y 1- :i. 3 h b) y in o v e d (in y e e n t p e y p :i. 53 h b v 1 e f t) 2
L r :L 53 h b y a y.... e md y i n 1' r rn( I e f b y r :i. 53 i'i t) y in o v e d( my e n t r e y r i :3 h t y 1 ef t) ..I
I- p :i 3 h t y a y... e n d v i 1 1f o r rn( 1 e f b r :i. 3 h b) y in o v e d( my e e 1 1b p e y p i 53 i'i t y .1. e f t .1 1
L. c€? 1 '1b14 e y r :i. 3 h b y a y.... e n d y i n r(.') r rn( .1. e Tb y r- :i. 53 i'i b) y in 0 v 0 d (in y c 0 n b r 0 y r i 53 i 1b?X 0 f t)..!
L lei b y e 0 ii b r 0 y y1- ;i. 53 ii b• s'.•.... 0 n d y i ri t' o r in( 101by r i ?3 i'i b) y in o v e d( iyi y c 0 nb p 0 y r :i. 3 i'i b y i. 0 I' L..!
L In 0 V 0 d K .1. 0 I I, y C 01 1 b r 0 y Y :i. 3 i'i b) y S y.... 0 li d y ;i. Ii f Q 111( .1. 0T t y 1' i 53 i'i b) y 111(.') V 0 d( in y C 0 li t P 0 y P i :3 i'i b y J. t
L rn 0 v 0 d c I e i t y e e n t p 0 y p :i. 53 h t) y a y.... 01 1 d y i 1 1f 0 p in( J. 0 f i y p :i. 53 i'i I,) y in 0 v 0 d' in? 0 n t r 0 y r .i. 3 i'i L y J.:
C Hi y mOV 0 i.'.i( .1. 01 I', y(• 01 1 11 0 y 1 :i. .3 i 1 t) y 3•:'.... 0 li d y 11 1T(.') Y' IYi(.!. 01 t y r :i. 53 i'i t) y In OV 3- i.'.'i In y C: 0 r 1 11' 0 y P ,i. 5311't
C s y.... 0 r'i d y iyi 0 v 0 d (rn y 10 f t y c e in t r 0 y p i ?3 i'i t) y :i. i 'i f o p m(1 e f t y p :i. 3 i'i t y rn 0 v 0 d (rn y c 0 ri t r 0 y r :i. 3 i 11
C! ri™ 1 y sy.... 0 n d y m 0 v 0 d( iyi y J. 0 f t y c 01 11 p 0 y yn i 53 i'i t) y i 1 1(' a r in (10 f t v r i 53 i i t) y rn 0 v 0 d in y e- 01 11 0 y r
C rri y r'i -1 y s e r i d y iyi o v 0 d (in y 10 r t y c: 0 n t yn 0 y y%i .3 i 'i t) y :i. 1 1 f 0 y%iyi (10 f b? y-- :i. ?3 i'i b)? in o v 0 d (rn y c 0 ii b p 0 . i 'I
C s y.... 0 r i d y 3 s 53 i 31 1( iyi y n -1) y in 0 v 0 d my 10 f t y c: 0 n b p of? y r i :3 i 11 y :i. 1 1 r 0 p in(]. 0 f b y r :i. 3 i'i b) r in 0 v 0 d(:
(Ice r'i t r e y s -.t0 ri d y a s s i 5.31 1( m y r'i -1) y iyi o v 0 d (in y 10 f b y c 0 n b p 0 y p i 5.3 h b) i n f 0 r in( 10 f b y r i .3 i'i t) y iyi o v 0 d( iyi y e 0 ri b rv 0 y r i 3 h b y J. 01 b) II
II c0nt p0 y sy0nd y iiss i 31 1( m y n™ 1) y mov0d( m y J. 0f t y c0n b reyr i 3h b) .i ri fv 0 Y'- iyi (10 f b y p :i. 3 h b) y in 0 v 0 d (in y c 0 n b re? r :i. .3 i'i b y 10 f b) II
II p i 3 h b v c 0 ii b r 0 y s y0 n d y a s s :i. ?3 n (in y 1 1--1) y m o v 0 d (in y 1(•? f b y c 0 n b r 0 y r i 3 ii b) y i r i f 0 r m (10 f b y p :i. 3 i'i t)? in 0 v 0 d (in r c 0 n t r e y r i 3 h t y J. 0 f b) II
II !L 0 f b y y- :i. 3 h b y c e ii b r e y y.... e 1 1 d y a a s i .3 ri in y n -1) y m0 v 0 d (rn y 10 f b y c e n b p e r i 53 (1 b.) 5 i r'i f o y- rn( 1 e f t y r i 3 h b) y in o v 0 d( iyi y c: 01 111- 0 y r .i 3 h t y 10 f b) II
C IYi O V 0 d( 1 0 f b y I- i 3 i'i b y C 01 i b r 0' v a VJ.... 01 id y 3 S i 531 11 In y i i .1. i In u V e Q 5 Hi y e i!_? e I i b i e y P i iii i f I-. :i n f o r iyi (1 e r b y y%:i. 3 i'i b) y iyi o v e «':if, in y c e n b p e y r i 53 i'i b?.1. e I t) II
II rn o v 0 d (10 f t y r i 3 h b y c e n t r©) y a y.... 0 ri d y a a a :i. 311 (in y n-• 1) y in 0 v e d (in y 10 f t y c e n b p 0 y r :i. 3 i'i b inf 0prn (lef t y ri.3ht) y m0v0d( m v c01 11v0 y r i3i 11 r 10f t) II
C n y rn o v 0 d( 10 f b y p i 3 h t y c? 0 n t p 0) y a y 0 ii d y a a a i 3 ii (in y n -1 y in 0 v 0 d( iyi y 10 f b y c 01 1 b p 0 y p :i. 3 i 1 i 11 f D Y%IYi( J. 0 f tfri 3 h b) y moV 0 d (in y C 01 i 11- 0 y v i 3 h t y .L 0 f t) II
II a y-. 0 n d y iy. a v 0 d( p. 110 f b y y i 3 h t y0 n 1 y- e) y a a 13 n (in y r 1 -1) y rn o v 0 d (n« y 1 e f b y 0 e ii t r 0 y p :i. 3 h i n f o r rn (10 f t y p i i' t) y m o v 0 d in y e 0 n t p 0 y 1- i 3 i'i b y 10 f t) 1
I rn 0 v 0 r.1( n y I ebb? r 'i. 53 h b y 00 r i b y% 0) y$ y aai: i 3n (in y n 1} y moved(in y .1. e I b y l ei 1 b i 0 y y .i. .a i 1 b) y i r i f 0 y-iyi( 1 e f b y r i 3 i'i b) v rn 0 v 0 d C in y a 0 n b v 0 y r 13 I'i b y 1 e l b) J
% A Sample eight
t r a n s f o in«
n a m e o f t h e p r a b I e m t :i. n a o r t© d 1«
d e s c T' i p t :i. o ri£
y .La aii .i.i isor Led J. oT z p y--ovided it z is empty r then y is 0 mp!.? else if z is the head of z? t hi e n y 1 j. s- t h© i n s o p t© d 1 o f t h e ta :i. 1 o f z?
a n d y i s t h e r© s 1..11t o f o r d e y-© d.... i ns e 1-t z :i. r 1t o y 1
' yl ,is an object of the problem? recognize y:!. z .i. s a n o b J e t o f t h e p r o b 1 e in? r© c o d n i z e z
o rdered1 i.nseY't is an ob ject of the p yob.1.em V n o r d e r e d.... i n s e r t i s a f u n::• t :i. o n? r e t o 3 n i z e w o r d o r d e r e d .i. n s e r t y is an object of the problem? recognize y 0 r d e r e d.„ .i ri s e y t :i. s a f u n c t :i. o n w :i. t h a r S u m e n t s LI y, z v y 1 z is an object of the problem? recognize z
recognize word insortedl » recognize yl 1 n s o r t e d 1 :i. s a f u n c t :i. o ri w i t h a r u m e n t s II y 1? s y v 1II recognize z recognize z recognize word if tead :i. s a f unct i on with a rSuinen ts Hz v z II
recoSni ze wo rd emp ty recoSn:i.ze y empty is a function with arguments IIyU recoJn i ze wo y -d emp ty r e c o .d n i z e z empty is a function with arguments IIz I recoSnize z reca3ri izewordinsarted1 reco .S n i z e y i ri s o r t e d 1 :i. s a f u n c t i o n w :i. t h a r S u in e n t s II y v z II C2JClflHThe P r o 1 o £1 c 1 a u s© s a r e d e r i v e d a?
insortedl (II U? C! II) i n s o r t e d 1( y v II z! s y v 1II)$- i n s o r t e d 1(•: 1 v s y v 1) v o r d e r e d.... i i» s e r t (-.i v z? .i 1)« c a t j. n o r t0 d 1+]. o$
START [' :i. n s 0 v I', 0 d I!
I.. b? i S y 3 li y :i. !i S O Vt0 d 3. y Q f y X y p r 0 V i d 0 d :i. f y X y :i. S y 0 til p i :i y?« t h 0 n y y ;i. S y 0 li'i P t b y y y 0 .1. S 0 y 1 'f y Z y i Hi- y t h 0 y h 0 a d y(!) t y X y y y then y b 1. y :i. S y the y i liSO P ted I. y of y t ii0 y ts i .1. y of' y X y y y a r'i d•' b y :i s• t h 0 y r 0 s i.j J. t y 0 r y 0 r d 0 r 0 d.... i t 1 e r--1 y z y :i. n 10 y .1.!!! j L' b y .1 S y j. n S 0 r 10 d 1 y X y P r 0 V :i d 0 d y i f y Z y i S y 0 rn P t 'b y y y then y y y j. 5 y eiriP'tb y y y e 1 S0 y j. f y Z y 1 S y head y X y y y t h 0 li y b 1 y :i. s y i n s 0 r 10 d I y t :i. 1 y Z y y y b y i s y r- 0 s u 11 y 0 r- d 0 r 0 d..„ i ii s 0 r t y z y i n 10 y y 1II
C b y be y i riso rted 1 y z y prov i ded y z y be y empty y y y then y b y be y emptyy 5 y :i. f y Z y be y head y Z y y y then y y 1 y be y i nso rted 1 y ta i 1 y zdy b y be y resu 11 y ordered™irisert y z y into y y 11!
Cblyintoy z y ordered™insert y result?by be y
z1 y t a i 1 y i. n s 0 r t e d 1 y b 1 y t h e ri y b e y Z yhesdyZyify b e y e iyi p t b y y y t h e ii y b 0? e m p t y z y b e y z y :i. ri 0 r 10 d 1 y b y b 0? p r 0 v :i. d e d J
II y .1. y s b.... 0 n d II
C bl y s be n d
[z y bl y Sb._end3
Cordered™insert(zybi)y sy™endH
[.'ordered™insert (zybl) y sb™erid3
IIb y 0 rdered™irise rt (zybl) y sb-endII
C sb„end y 0 rde red™ .irise rt( b y z y y .1.) U
C z y ss™erid y o rde red™ i nse rt (y y z y y 1) II
C t 3 .i. 1( z) y :i y™e n d y 0 r d e r e d™:i. ii s e r I( b y z y b 1) II
II i ri s 0 v t e d 1( s b v 1) y s b.._ r e v (t a i X( s b v 1 y)) y.... e n d y 0 r d 0 r 0 d.... i n s 0 r t (y z y y 1 II
C b 1 y i ii'ii0 r ted 1( sbv 1) y sb™ rev( t:j i I( sbv :l. y z)) y y....0nd y 0 vd0 r0d.... :i. ns0 rt C y y z y 1 1! I., S .1. y illS(.) r t 0 I.j .1.( Si :l V .1.) Si :i.... T'- 0 V( t 0 j. .1.( Si V 1 V X)) V Si.... 0 ll CI P O Z U 0 Z 0 U.... I ll Si 0 Z L( y y Z P :i .1.) ..I
II s y_ e n d y t a :i. 3.( s y v 1 ?x) y :i. n s 0 z b 0 d 3.( y :l. y a y v .1.) y o z d 0 r 0 d.... :i. n a 0 r t (y z y y J.) :i
L. z y a y_.. 0 ii d y b a :i. J. a y v 1 y z) y i n•: 0 r b 0 d 1( y 1. y a y v 1.) y 0 vv d 0 z 0 d.... i n a 0 z b( y y z y y :L) I!
L1 i 0 a d (z) y a y.... 0 ri d y b 0 :i. 3.( a y v :l. y z) y :i. n 0 r 10 d .'I. y 3. y a y v 1) y 0 z d 0 z 0 d.... i n a 0 z t( y z? y 1) II
L z y h 0 a d (z p a y.... 0 n d y b b :i.3. si y v:!. y z) y ;i. n a 0 z b 0 d 3.( y 1 v a -y v 1) y 0 z d 0 z 0 d.... i n a 0 z t( y 9 z v 'a3.) II
II Z y I I 0 0 d 'v Z) Si y.... Z 0 V ii. T)!•' Si VJ.... 0 l| d y b B ii. J.( SiV 1 y Z) ii. Il Si O Z t- 0 d J.( 1!' S V 1) 'J O Z d 0 Z 0 U.... iJ. f 'l Si 0 Z t
i; II a y._ 0 ii d v ii. f y h 0 a d( z y z) y b b ii. J. (v 1. y z) y .i. ii a 0 z b 0 d 3.( y :i. y a y v 3.) y o r d e r e d.... :i. n -a 0 z b (-.i v z y y 1)
I 0 iti p b y y y0 n d v i. r y hi 0 ri:; d( z y z) y b a ;i. I (v:!. y z) y :i. n a o z b 0 d 1( y 1 y•:: va v 1) y 0d 0 r 0 d.... :i. ii 0 z b (.-i y z y 'a :i.) .1
I L y y 0i'i»p b y y a y.„(:? itd y ii. I' y hi0 a d( z y z) y b 3 ;i. J.( a y v 1 y z) y :i. ii s 0 z 1,0 d .1. (;i. y: v !l.) y ' o d 0 r 0 d.... :i. ri a 0 r t C ?zyyl )3
[ y y 0 m p t y y a -.i 0 n d y .i f y bi 0 a d( z y z) 91 a i 1( a y v 1 y z)? :l ii a 0 z b 0 d 1( y 1? a y v 1) y 0 r d 0 z 0 d i ii 0 r t( y? z y y 1) 31
L3 si y.... 0 n d y 0 rn p b y( y) y ;i. f y h 0 3 d( z y z) y b a :L I( a 'a v 1 y z y :i. n a o r 10 d 1( y 1 y a y v 1 y orde Z0d.„. :i. ns© rt(y?z?yl)3 •
C 0 iyi p t y y si y0 n d y 0 m p b y( y) y .i f y bi 0 a d( z? x) y t a ii. X( a y v 1 y z) y i ii a 0 r 10 d I (yip a y v 1) v 0 v d 0 Y- 0 d.... ii. ii a 0 r t( y y z? y 1) Ii
C z r 0iyipty y sy„©nd p ©lYipty( y) p :i. f p head( z p z)? ta i 1( ay v 1 y z) p i nsor bed 1( y 1 y ay v 1) y o rd0 y-0dii.nser%t (y p z? y 1) II
C y._ 0 n d? 0 iyi p b y( z) y 0 iyi p t y( y)? :i. f p bi 0 3 d (z? z) y b a i .1.( a y v 1 y z)? :i. ii a 0 r 10 d 1( kj 1? s y v 1) y 0 rde r©d„ i na© rt(yz y y 1) II
LI z v a y.... 0 n d y 0 iti p b y( z)» 0 iyi p b y( y)? :i. f? bi 0 a d( z y z) y b a :i. 1( a y v 1? z p :L ti a o t- b 0 d 1( y 1? a y v 1) p 0 r d 0 z© d„ :l ii a 0 r t( y p z y y 1) II -+ II ii ii Si o z 10 d 3.( z) y a si 0 n d y 0 in p b s( z)? 0 iyi p t s'() y :i. h y hi 0 a d( z? z) 91 a 13.( si y v 3.? z) y 1 ii a o z b 0 d 1( y 1 p a y v 1) y o z d 0 z 0 d.._ :i. n a 0 r-1( y y z 1 y 1) U
II K-.i y n. ii a 0 yn b 0 d 3.( z) y si s.... 0 ri d y 0 iyi p b v.( z) p 0 rn p b y( y) y ii. f' y bi 0 a d z?;) y 'b a i .1.a y v 3. y i) p i ii a 0 r b 0 d 1 (si 3. p a y v 1) p o v d 0 z 0 d :i. ii si 0 t'- b( y? z y s J.) .3
II ay _..end v ii. nao rb©d 3.( y y x) p ©rriP'by( z) y 0rrip 1-.i( y)? i 1 y n©ad (.z y z) y ba i J.ay v 1? z) p i r i a o z 10 d 1( y 1 p a y v 1)? o t- d 0 y -0 d i ii a 0 r t( y p z p y 3.) .3
II i ii a (ii z b 0 d 3.( y y z) p—? 0 iyi p b y( x) p 0 in p b y( y) p p i ii si 0 y-- b 0 d J.( y y z)??~? bi 0 a d( z p.••;; y L- a 13. (s i n a o v 10 d 3.( y 3. rsyvl) v o r d 0 v 0 d_.. :i. n a 0 r b( y y z y y 3.) 1
C i r i a 0 r 10 d 3.( y p IIH) p t- II
CineortedKCHf C3 p :™II
C i ii si 0 v 10 d 3.( y y II z!.... t II) r$- p :i. ii o r 10 d 3. (-.i 3. v a y v 3.) 9 o r d 0 r 0 d.... :i. n a 0 z t( y 9 z y -.i 3.) II
C i n a o r 10 d 3.( y 9II z! a y vl II)m- p 1 n a o r b 0 d 3.( y 3. 9 s v 3.) p 01 d 0 z 0 d.... :i. n a 0 1 b( y y z p -a 3.) II
•V Sample nine
ll 3 ITl 0 OI th 0 P I'- o b J. 0 II I$ Q I..I ;i. c|.;: q) t j,.
d 0 s c r :i. p k :i. o it
y :i. s a G u i c k s 0 r t 3. o r x p j•• q y ;i. d 0 d if z is the head of x5
u J. is the larAerpar t of y, con 1 pa r0d wi th x? 11 is the smallerpart of x compared with z5 'land s 1..11 is the Quicksort! of uly s 11 i s t h 0 q u i c k s o r t1 o f 3 3. 5 then y is the conc o f s'..13. y z and s3 3.
s .L 1 i s a fi o b J 0 c t o f th0 p r o b 3.0 m T r e c o a Pi i z 0 s 13.
z is an object of the problem? r 0 c o a n i z e z sul is an object of the problem? 'r 0 c o .a it i z 0 s u 3. r 0 c o a n ;i. z e w o r d c o n c y is a pi object of the problem? r 0 c o a n :i. z 0 y
j c o n c i s a f u pi c t i o pi w 1 t h -a r i .1 m 0 pi k y 9 s u 3.• z -:.:j 3.3. .3 ill is a pi o b J 0 c t o f k h e p r o b 3. e m? r 0 c; o a n i z e 3.1 ! recognize word Quicksortl recognize si1 04 u i c k s o r 11 i s a f u n c t :i. o pi w i t i' i a r S u m e r i k s 1.3 s 13. y 3.3.31 ul is an object of the problem? r e c o k ri i z e u 3.
r e c o s' n :i. z e w o v d 04 ..i i c k s o r 13. r e c o n :i. z e s u 3. 04 u i c k s o r 13. :i. s a f u r'i c k i o n w i t h a r si u m e r'i t s 13 s u 1 y u 12 recognize z compared is an object of the problem? n 1 compared is a function? n Just ignore compared? x is an object of the problem? r e c o si n :i. z e x s iti a 11 e r pert i s a r'i o b J e c k o f t h e p r o b 3. e m n s m a 3.1 e r p a r k :i. s a f u it::• t :i. o n? t- e c o n i z e w o r d s m a 3.1 e r p cj r k r e c o s; n i z e 3.!!. s ni a 11 e r p r k :i. s a f u i t o.v t i o n w :i. k it a r J u iti e i t t s 13 3.3.? x? z 31 r e c o ri i z e z r e c o :i i t i z e x r e c o n .i z e w o r d 3. a r e r% p r k r e c: o a n i z e u 1 1 a r a e r p a r k :i. s; a f u n c 13. o n w :i. k h ii r sf u iti e n t s 1.3 u 3. y x? z 3! r o? c o a n i z o? x recoait:i.ze z head is a f unc kioit w:i. th araumeit ks 13z y x3! reco a ri i z e x r e c o a n i z e w o r d 04 u i iz k s o r k 1 r e (P o a n i z e w 04 u i c k s o r k 1 :i. s a f u n c k i o n w i k h a r- a u m e i t k s 13 y y x .3! L32JC1 y lHThe Proloa clauses are derived as
[; 21 y IHsul is a a roup of data? 1321 rl Hz is a a roup of data? n 13215 1 Ms 11 is a a roup of data?
04 u i c k s o v 11 (a v 13 z! x.... 131) t- 1 a r a e r p a r k (u 3. y 13 z! x..- k 31 y z) y s m a 11 e r p a t- k (3.1 y 13 z i x.... 13 y z) v 04 u :i. c k s o r k 3. (u 3 y u .3.) y n 11 'j ck• C' r'!( I I•• cat Quicksort Iod
START C o. u :i. c k. s o p t I
Iy v '•s? «J e u :i. c k s a r 11 y o f? z y p r o v i d 0 d i. f y z y :i. s y t h 0? h 0 a d? o 1' z? y u 1 y :i. s y tI I0 y 1 a p d 0 p p a pt y 0 f y z? c a in p a p 0 d y w :i. t h y z y y y 1 X y :i. S y t I I0 y S In a 1 .1 0 p P 8 r t y o f y Z y C C) IYi P 8 r G d y Wi t i i y Z y y y ar id y Si. 1.1. y :i. S y t h 0 y 0i.l :i. ckso P t I. y Of y u I y y y S 'I. 1 y :i. S y t h 0 y G u :i C k S O P t1 y O f y I X y y y t h 01' l y y :j. S y t h 0 y c o n C y O f y s u I y Z y a ii d y S 1 1 ::i
LI a y :i. s y« u X c k a a p t1 y z y p p o v :i. d e d y :i. f y z y :i. s y i i 0 a d y z y y y U .1. y i Z y .1. 3 T d 0 P P a 1 t y Z y COIII P a r 0 d y Z y y y .1. 1 y :i. S y IVi .1. .1. 0 P p a 1 t y Z y C O ITi }'-v S P 0 d y Z y y y S LI I y :L S y (S U i C k. S O P I y I..11 y y y S I X y :i S y Q u :i. e I'.', s o r 11 y I 1 y y y then y y y :i. S y cone y SU1 y Z y s 111
Cw y b0 y quiek.sop tl »zyppovidedyz?bey Xi0ad? x y y y u X y be y 1 a p.Sg ppa pt y z y coiripsped y z y? y X X y be y sina X X 0 ppspt y z y compa ped yzd? SUX y be y QUicksSOPt X y UX y y y s X X y be y OHJ i ckso pt X y X X y y y then y ay be y cone y suX y z y sX X 3
C S X X y Z y SU X y COT'iC y a y t h0li y be y X X y gu :i. ckso pt X y s X X y be y u X y «»..i i ek.so rt X y su X y be? z y co m p a r(•:? d y z y s in a X X e p p a p t y X X y be y z y c o iyi p a r% e d y z y X a p d e r p a r% t y u X y b e y Z y X 'i(•? a d y Z y b 0 y Z y Q U :i. C.%k. S O P t .X y y b 0 y p p o v :i. d e d!
C s X X y s a.... e n d..!
L Z y S J. .1. y S a... 01 i d .J
II's i.j X y z y s X X y s 'a.... 0 n d
Ceo n e( s pi X y z y s X X) y s a.... e n d 1
[yyeonc( su 1 y z y X X) y s-.i....end:i
C a y e o r i c( s u X y z y s X X) y; 'a.... 0 ii d II
C s 'a_. e n d y c o n c( a y s u X y z y; X X) II
C X X y s a.... 0nd y cone( a y su X z y s X X) II
C gu i ckso pt X( X X 9 s a.... 0 n d y cone( a y su X y z y s X X) II
C s X X y a u :i. e k. s o r t X( X X y s a.... 0 n d v c o n•:::( a y s u 1.? z v s X X) I!
C sa.„0nd y gu :i. ckso pt X( s I X? X X) v cone ('a t su X z 9 s I..!.) J
C u X y s a.... e n d? o. u :i. c I-:. s o r t X( s X X v I X):• 0 0 r- e« a• s 1,1.!. y z y s J. I.'. I
LI g u i c k. s o p t X( u X) y s a.... 0 n d y g '..i :i. c k, s o r t J. i-: J? I' o n r a u .1. z y s .1 J. 1 .1
CsuX y eui ckso pt I( uX) y sa... end v 0i..i:i.ck sop t.l s'.! v'':'•''':•' 1 Ls3_endyQuicksort1 (sul y u!) y Quicksort 1( s! I I 1) ycouc•; u. at:•• z v i: IS
L z 9 s 3.... e n d? q «.i :i. c k s o r t!( s u I y u I)„ q u :j. c k s o p t I( s 11 v J. J.) c a n o( 3• y I?:•.!'!•!
L z y s.... e ii d? a u :i. c k s o r t .1.( s i..iX u I)}.® u ;i. c k o r t! (s! 1. y i I.) y e o i 'i c (3? s 11! p. j.!),!
II x v z y s3....0nd ru :i. ckso rt I su .1 y I.I I}!•' qu i. ckso r t!( s 11? i X) y cock:;( 3 y su 1 v z y s1) II
I--s m 3'• 0 r'p r t x v z) v s y. r• i d v o. 11 i c k s o v t1 (s u Iu X) y q i..i i k s o p 11 s .1.1 v X1)? c o n c y y s u X y z y s 1X):!!
111? S ITi 8 X 1 e P P 3 P t( X y Z)? S y.... 0 f i d y Q U ;j.k• i•!-.! ij... i|!) y q»..! X O k 3«J P 1X( S j X y I].)... c a n c y y s u 1? z? s X].) I
L s y._. 0r id? s m a .1. .1. e p p a p t• X X y x y z y r i..i :i, e k sa r t X( s i1! yi'!• au i. c.• k s: r!, j. c]. X: X I.)••• c a n c (3 v s i..i X y z y s X X) I
II z y s y.... 0 n d y s iyi a X X e p p a r t( X 1. y x y z)? q u :i. c k s o p 11( s »..i I. y i..i X)? u :i. c!•. s: v-!,'!.(.].! f j. I)? C O i C A y S U 1 y Z y S X I) 1
r.z y sy...0!id y srcia.!. .1.0 rpa rt( X X x y z)- ri..i:i. cksor't X( su X '..i I.) y rc.i :i.cl'.so r t X( a i. 1 y 1 X y COfiC( y y V i .1. y Z y S .1. I.) ..I
I x y z y sy....end» sins X X e rps rt X X y x: y z) y qu :i. ckso rt].•: su X 7 u X) y qu :i. ckso rt X( s X.'!.? X X) y C O li C( y y S I.J I y Z y S X X) 1
C X a r£ 0 r p a r t( x y z) y s y.... 0 n d• s in 3 X X 0 r p a rv t( X I y x y z) y 0 u i c k s o r t j. (u I? u X) v a u i e k s o r 11.( s X X y 1 J.) y c o o c ('3 y s u X y z y i: X X) ::i
CuXyXarorpar t( x y z) y s -.i.... 0 u d y m a X X 0 r p a r t(]. X y x y z) y c-i u i c k s o r t X( s u I y '..1X) y« u :i. c k s o r t X (1 J. y X X) 9 c o ri c (y u I. y z 9 s 11 .'J
II sy „0nd y X rd0 rps r t( u X y x y z) y smX 10 r p:• r-1 (.X 1 y;? z) y auicksortX( suX y uX) 9 au:i.ck.stot.!.( sX X 9 X X) 9 conc:( y :uX 9 z y•;]. X) I!
II x 9 s s 0 n d y X a yv S 0 r p a r t( u X y x y z) y s iti a X X 0 r p a r k( X J. y? z) y qui ckso rt .1.( su X y u X) y quickso rt X( sX X y X X) y cone( 3 9 su X 9 z» sX X) II
H110 a d( x) y s 3„.. 0 n d y X a r S 0 r p -a r t( u !l. 9 x y z) y iii iyi a X X 0 r p r k(!!. X y.» z) y qu :i. cksort X C su X y uX) y qu:i ckso r t X( s 1. X y X 1.) y cone 3 y suX? z? sX X) J
L z y h e a d( x) y s 3.... 0 n d y X a r d 0 r p a y% k u X 9 z) y m X X 0 r p 3 r k( X X y x 9 z) y q u i c k s o r t X( s 1..1 X y u X) y R '..i :i. c k. s o r t I.( s .1 .1. y .1. X) y1 f« c: r( 3 ;h J. yy.! J.)..!
II S3._0lid y head( Z y X) y X 3 Pd0 PP3 P t C U X y X y Z.) y SlYia I. X 0 PPa P t C i.]. y ?X y Z p qu j. ckso r t X( su X y u X) y qu i ckso pt X S X X 9 X X) y cone 3 y SU X 9 z y s X X) ::i
LI Z y S 3™ end? h ead(z? x) y X a r si e r p a t( u X y x ?z? s in a X X© r p a r k( X 1 y x y z) v q i.j i c k s o p k X( s u X y u X) y Q '.J i c k s o Y'- k .1.( s X X y .1. X) y c o n c!-.•: y s u .L y z y s .1. J...!
II oui e k s o r k 1( x) y r 3 e n d y ki 0:? d C z y x) y X:? y% d 0 y% p a r t( u X 9 x ?z) y s in a .1.10 r p a r k( X .'J. v x 9 z) 9 q u i c k. s o r t X( s u X 9 u X) 9 q u :i. c k. s o r t X C s X X 9 X X) y c: o n c:( 3 9 s u X 9 z 9 s X I) 1
II3 y q u i c k. s o r t X( x) y s 3... 0 n d y h 0 a d z y x) y X 3 r d 0 yv p a r k u X 9 x ?z) y s in: X X 0 r p r t( X X 9 x y z y qu i ckso r t X( su X y u X) y qu :i. ckso r t X( s X X 9 X X 9 cone( 3 v su I? z y s X X II
II s 30 n d y q u i e k. s o r k X( 3 9 x) 9 h 0 a d( z y x) 9 X a r d 0 p p a r k( u X y x y z) y s in a I. J. 0 r! V a r t( X X? x v z) y w u i c k s o r t X( s 1..1 X y u X) y Q u i c k o y% t X( s X X y X X) 9 e o n c( 3 y s u X y z y X X) II
L! q u :i. c k s o r k X( 3 y x) y t- y h 0 a d z y x) y X a r d 0 r p a r t( u X y x y z) y s in a X X 0 1- p a r t (.1 X y•••:• z•'• q u i c k. s o r k 1( s u X y u X) 9 a u :i. c k s 0 r t X( s X X y X .X) y c a n c( 3 9 s u.'!. y z 9 s .1. .1. ..I
r a• 1 i. c k s o v 11 «rCz! x.... 13)rJ- 1 s r si e r p a r t (u 1 C z!:.... 11 z)? s m a 11 e r p a r t (I J. r z: K J oui ckso rt 1 su 1. u 1.) au ickso rt .1. (s 11• 11) cone (a, su 1 r c »s 11 3 Sample ten
t yv a i 'i s f o r m,
ii a ti! e o f t h 0 p r o b .1.0 m J 1 a r g 0 r- p 3 rt«
d e s e r i p t i o 11$
u.l. is I ne J. 0 r ?.%s 0 r p s:::: r t oi z o o m p a r 0 d 10 lib z i t 1..1.1. i a a .1. :i. s t a f 311 t h 0 0.1 0 m 0 n t s :i. n x ti r 0a 10 r t h 1 n z.
z is an ooJect ot the problem 1 recognize z
r 0 c o d n :i. z 0 w o r d m o r 0 X :i. 3 a li O b J 0 0 t 0f t i'i 0 P Z O b I. 0 ITi
r 0 e o ti n i z 0 x j( r 0 e o ti n :i. z 0 w o z d -a y... 1 a 0 I ,ul is 1 an obJ00 t of th0 prob3.0m T
i j r e c o ti h i z 0 u I s y1 a 0 i s a f u n c t i o ri w i t h a r ti u m 0 n t3 II u i• x y s y v 1II recognize z compared is an object of the problem? n compared is a function? n Just itinor0 compared? r0co3nize x r 0 c o ti n i z 0 w o r d 1 a r ti 0 r p a y- t recognize ul 1 a r ti 0 r p a y• t i s f u 1 1 c t i o n w i t h a y% ti u in 0 nt s L u .1. y x? z ..I
C 2 J i: 1 f 1H T110 P r o I o ti 0 3. a u s 0 s a r e d 0 r i v 0 d a s v
-•- please check if the boundary case is correct!!
1 a r 3 0 r p a r t( II11 y I!! II y z) 1 a r ti, 0 r p a r t (II s y v 1! u J. 11 y II s y v ;l.! x ..I y z) J- s y v 1 z? 3. a r ti 0 r p a r t( u 3. 9 x 9 z)• 3. a r ti 0 r p a r t( u 3.9II s y v 1! z II z) t-• 3. a r ti 0 r p a r t( u 3.? z? z)• e a t 1 a r a e r p a r t, I o a
KK START ttt r; I a r a 0!• p -a r t, 1
L u.'. v i a y t,h 0! .1. ra 0 r p 3 r t- y 0 f y x? c 0 in a p 0 d y w :i. h y z• :i. i' y «.,i],• :i. a? 3 y j. ;j. 3 I..? n f... 3j ]t 0,, 0.1.01'fi 0 ii i, a y i n y x y t, h a t y a r 0 a t.0 r y t h 3 n y z II
T U 1 y :i. 3 v 1 a P a 0 P p 3 P t y X y C O I'i'i P 3'!• 0 d y Z y i T y LI J. y ;i. a y 1 i 3 t y 3 J. J. y 0 J. 0 In 0 na- y X y a p 0 3 10 P y Z II
II LI 3. y b 0 y 3. 3 P 3 0 P P 3 PI y X y C O ITi P a P 0 d v Z y :i. f y LI 3. y b 0 y a 3.... I 3 0 y X y In Q P 0 y Z II
II Z y In O P 0 y X y 3 y.... 3. a 0 y U 3. y b 0 y Z y C (3 ITi P P 0 d y X y I 3 P 3 0 P P 3 P t y 1J 3. y b 0 y P P 0 V I d 0 u II
f. z y s y.... 0 n d II
H rri 0 r 0 (z) y a y.... 0 n d I!
• II X y ITi 0 P 0( Z) y S y.... 0 IT d II
H ay.... ,'L ao?( x y ay v 1) y ay__ ro?v (ma re ay v 1? z))? ay._end II
C 1..13. y a y.... 3.3 e( x y a y v 3.) y a 3r 0 v (in 0 r 0( a 3 v :l. y z)) y a y.... 0 it d !3
II a y.... 0? n d iri 0 p e( a y v 3.» z) y a y 1 a 0?( u 3. y x 9 a y v 1) !3
C z y a y.... 0 n d y m 0 r 0( a 3 v 3. ?z) y a y.... 3. a 0( u .1. y x y a y v 1) II
IIZ y 'a 3.... 0 IT d y ITi O P 0( S 3 V 3. y Z) y 3 '3.... 3. 3 0( LI 3. y X y ii!- 3 V 3.) II
C X y Z y S 3.... 0 I T d? ITi O P 0( 3 y V 3. y Z) y 3 3.... 3. 3 3( LI 3. y X y 3 3 V 3.) II
H 13 r£ e p p a r t( x y z) y a 3„ 0 it d y iti 0 r 0( a y v 3.? z) y a y.... 3. a 0( u 19 x 9 a y v 3.) II
C u 3. y 3. a pao? rp3 pt( x 9 z) y ay....0?itd y iti0 p0? (ay v 3.? z) y a3„.. .1. a0?( li .1. y x y ay v 3.) J
II a y__© n d y I a p a 0 p p 3 p t (u .1. y x y z) y m 0 p 0?( a 3 v 3. y z} y a 3.1. a 0?( li 3. y x y a y v 1) ..I
r 3.3 rd© P P a P't( LI 3. y X y Z) y~ 9 ITi O T%©( a 3 V 3. ?z) y i! I. 3©( LI 3. y iS y V 3.) ..I
II1 a r d 0? p p a r t( C y v 3.! li 113 y II a v 3. I x II y z) v t-• y in 0 r©( a y v 1 y z) 1 a r a 0 r p a r t( u .1. v x y z) J
% ' Sample eleven
t r a n s f o r m n a in e o f t h e p r o b 1 e it t a e r, a a in 1., d 0 s e r :j. p t i o n£
s y in i a t h e a 0 i' i s 3 m 1 0 f r o o t p r o v ;i. d 0 d :i. f t h 0 c 1 j r r 0 n t.... in u in o f r 0 01 i. a n u 1„? I 1li in 1. i a n li in f .1.?
II itv (..'..IT 1 0| 1 L.... I 1 LI Hi (j I 1 0 (j ;j, a V 0!'•'' .1. 3 1.; 01.1 bI'TLiml? t J' T 0 n 3 3 in :i. s a o 0 i t c 0 f t h e a t r i i t a r 0 01 a n d i t li in 1 ri li in 1 i. s 3 n 0 b•. j 0 c t 0 f' t h 0 p r 0!.10 m T T- 0 C 0 a I T :L Z 0 I T LI ITT 1
root :i.a an object of the problem? r e c o a n :i. z e r o o t r e c o a n :i. z 0 w 0 r d a t r c 0 n c a,am is an 0tivj0(: t of the problem'?
1 j 0 e 0 d n i z 0 i:: m a t r c 0 n c :i. a a f li i t c t :i. a n w :i. t i t a r d u itt 0 n t a L' a a m?r 0 0 t y i t li m l I'i r 0 c o it :i. z 0 n u nri 1
r 0 c 0 n :i. z 0 w 0 r d t% 0 p 1 a o 0 d r 0 c 0 n :i. z 0 r 0 01
cur rent....n li in :i. a an object of the problem? it current_num is a function? r 0 c 0 a 1 n i z 0 w 0 r d c u r r 0 n t.... n u itt c u r tv 0 n t™ i t li in :i. a a f u n c: t :i. 0 i t w :i. t i t ;;j r a u itt e n t a C! r 0 0 t? a a v 1 I numfl ia an object of the problem? recognize it li m -11 r e c 0 a it i z 0 it u m 1
numfl ia an expression to numl num is an object of the problem? r e c o a i t i z 0 n t..i in r 0 c o a i t .i. z 0 r 0 01 r 0 c 0 a i t :i. z 0 w 0 r d c u r r 0 n t.... i t u itt c u r T 0 n t... n u m :i. a a f u i t t i 0 n u» :i. t i t b r a u m 0 n t s r 0 01 y n u itt 2 r e c 0 a n i z 0 r 0 01 r0c0ani z0w0rda0naam1 r e c 0 a i t :i. z 0 a 3 itt a 0 ii a 3 m 1 :i. a f u n ic t i o it w i t h r a u in e i t t s a in y r ij 0 t [!2-..J C15 1HTi t0 I' r01 oa co 1 auaes a r0 d0 r :i. ved ::?a- y
t: 21 y 1H r (j 01 i a n u in b 0 r? n Z 21? 1H n u itt 1 i a 3 n u itt b 0 r?
a e n a 3 in 1( a 3 m y r 0 01) t- c «..i r r 0 n t... i t u m( r 0 01 v n u itt) 9 ri u in 1 i -a n li m+:L y r e t r a c t (c u r r 0 n t.... ii u itt( r 0 01 y....)) y a s s 0 r t a C c u r r 0 n't.... n u iri r o 01 y n u m 1)) 9 n a m 0( r 0 o t y s 3 v 2) y n a m 0 i n t( i t li iri 1 y s v 3) 9 a p p 0 n d( a v 2 y a vj v 3 y v 4) y i t a m 0 (s a m r 3 3 v 4)• c a t 3 0 ii s y in 1 X o 3
START nt , II s 0 n s y m13
r s y in y :i. s y t hi 0 y 3 0 n s y in 1 y 0 f v r a a t9 p r 0 v :i. d 0 d y t h 0 y c u r 1- 0 in t.... n u m9 0 f 9 r o 0 t9 i s? n u m9 9 9 , n 1..1 m 1 y i s y n u m+In? t h 0 y c u r r 0 n t.._ n u m v 0 f 9 r 0 0t v i s 9 r 0 p I a 0 0 d y b y y n u in:!. 9?? I t h 0 n y s y m 9 :i. s?s? c o n 0 y o T y t h 0? s t r :i. n 3 y r 0 0 t y 3 n d 9 n u in 1 II
L s y m 9 i s 9 3 0 n s y m 1. y r o 0t p r 0 v :i. d 0 d y c u r r 0 n t... n u in? r 0 019 i s 9 n u in 9 9 9 n u in 19 i s 9 n u in 4-19?? cu r rent...nuin y root 9 :i. s v pop J. accd 9 nuin 1 y? y thon 9 sym 9 :i. s 9 cone st v :i. n3 9 root 9 nuin 1 II
I., s y in yb0 9 3 011 s y rn 1 y r o 01 y p r 0 v :i. d 0 d y c u r r 0 n t.... it u in 9 r 0 0 t? b 0 y 1 1 u m9? y 1 1 u in 19 b 0 y ri u mf 1 y y y cu r r0nt.„num ?root? b0 y r0p ,'L ac0d y riuin 1 y y 91h0n y syin y b0 9 st rc0nc y root? riuin 1 J
C n u in 1 y 0 01 y s t r c 0 n c?sy in y t hi 0 n y b 0 y n u in lyre p 1 a e 0 d y r o ot?c u r r 0 n I.„ n u rn y b 0 y n u in f 1 y li U m1 y b 0 y li U ITi y P 0 01 y Ci U P P 0 li t.... Pi U ITi y b 0 y 0 01 y 3 0 Pi S y Hfi 1 y S 'J in y b 0 y P P O V i d 0 d 2
II Pi U ITi 1 9 S y_ 0 Pi d :.l
I ra01 y inuml y sy„0PId2
t S 1C O Pi C P O O t y Pi ij ml) y s y._ 0 Pi d .I
C S y ITi y S t P C 0 Pi C( P 0 01 y Pi u ml) 9 s y„ 0 Pi d 3
Csyin y stpconc( root y nurnl) y sy„eridll
C sy -.end y st pconc (sym? root y nuin .1.).']
II n U ITi 1 y 3 y.... 0 Pi d f S t P C 0 Pi C( S y ITi y P O O t y Pi U ITi 1) II
II r 0 p 1 a c 0 d( n u in 1) y s y..„ r ev(sy„.re p) ?sy„© n d y s 11-o i i c( s y m y r o o t y n u m 1) J
II p o o t y r© p 1 a c 0 d( n u m 1) ?s y.„. p 0 v( s y... p 0 p) y s y.... 0 n d y s t p c 0 n c?( s y m y r o o t y 1 1 u in 1) II
LI o u r r 0 pi t..„ n u m( ts 0 019 s y v 2) y y.... v 0 v( v 0 p 1 a c 0 d( s a v 2 y n u m 1)) 9 s a.... r 0 v( s_„. t- 0 p y s.... 0 n c sire 0 pi o( s y in y p oot? n u in 1)3
t sa0nd 9 sy.... rep?repl ac0d( sy v2 y nuiti 1)? cu r r0nt_num( r001 y sy v2) y strconc (sym y root y num.1.) II
II PiUITi f 1 y Sy _..0lid y Sy P0P y T%0P 1 3C0d( Syv2? nUITi 1) y C:U P P0PitPiU111 001 9 S b' V2)! s t r c o r i c( s y in y r o o t y n u m1) II
n U ITi 1 f Pi U ITi r 1 y S y.... 0 Pi d y S a.... V 0 P? P 0 P 1 3 C 0 d( S a V 2 y Pi U ITi 1) y C U T%P 0 I T t.... Pi U in( P 0 01 y S y V 2) y s t r c o n c( s a m y r o o t y n u iti 1) II
r; ci y.... 01, d y a s s :i. 3 n( i t u in 1 y n u in f 1) y s.._ r 0 p y p 0 p J. c 0 d( s v 2 y i t u m1) y c u r r 0 n t.... n u iti( r 0 0 t y S t r C O Pi C( S y ITi y r 0 01 y I T U ITi 1) 2 I n 1..1 iti y sy....eindvsss :i. dn( nlim 1 y n 1..1 m+ 1) y ay.... r0p y r0p 1 ac0d (ay v2 v n1..1 in 1) y c 1..1 r r 0 in t.... in 1..1 in C r 0 o t y s y v 2) y a 1 p c o n c( s y iti y r o o t y 1 1 '..1 rn 1) I
II v- o o t y in 1..1 rn y a y.... 0 n d y 3 3 a :i. d in (in i..i in 1 y in li rn+ :l. y a y.... r e p y r e p J. a e 0 d( a y v 2? n li rn 1) y cli r r 0 n t.... n u rn p 0 01 y a y v 2) y a t r- c 0 n e( a y rn y r 0 0 t y ri u rn 1) II
II e »..i r t- 0 n l,.... in 1..1 iii r 0 01 v n u in) y a y.... 0 ri d y a a a :i. d n( 1 n li rn 1 y in li in f 1) y s y.... r 0 p y r 0 p X 0 e 0 d t. a y v 2 y n li n c 1.j v r 0 in t.... n 1 j rn( r 0 0 t y s y v 2) y '0t r e a in c C s y in y r 0 01 y in 1..1 rn 1 I!
I., a y.... 0 in d v c li!'• r 0 n t.... ii 1..1 in( t%0 01- v r i u rri) y y• :i. d 1 1• r i 1..1 in 1 y 1 1 f..i in 1) y s.... v- 0 p y v 0 p J. c 0 d( s y v 2 y 11 r.i n c '..I r v- 0 r i t.... r i u in r 0 0 t y s y v 2) y y t r c 0 n c:( s y rn? r 0 0 t y n u rn 1) II i; I i n p 0 0 i y s„.. 0 ri d y c 1..1 p p 0 ri t.... in 1..1 m( r 0 01 y n u rn) y :i•;: :i. d 11( n u rn 1 y nin f 1) y y.... r 0 p y I'- 0 .1. 3 C 0 d( y V 2 y ll LI !Tl .1.)!• C LI P P 0 ll t.... Il LI I'll( P O 0 t V 3 3 V 2 y t P C 0 ll C!'( S ::i I'll? P 0 0 t! I Li ill .1. .J
[. d 0 R1::- III 1( V 0 0 t) V 'r.{... 0 I I GV '0•..! P P 0 I l 'I'.-.... I l«..! Ill( 'P 0 0 t y ll '..I I'll) V 3 S j. d I l( ll L.I I'll 1!-! I l LI I'll f 1)?:i y.... P 0 P y v 0 p X e 0 d( y y v.2 y ii u rn .1.) y c u r r 0 in t... 1 1 um( r(j 0 t y v 2 y t p c o inc 'I s y rn? ro 01 y inuin 1) II I
II s y in y d 0 in s y in X( p o o t) y y... e in d y c lj p p 0 in t.... in u rn( p o 0 t y ii li in) y 3 s s :i. d in mi_i mX y in u in +1) y•: p 0 r p e p 1.: c 0 d (y v 2 y 1 1 li m 1) y(.:• u p p 0 in t... ii lj in( p 0 01 y s v 2) y s t p c o i i c( s y rn y p q o t y n lj rn 1) J
II s y... 0 ii d y d 01 1 s y rn 1( s y in y p 0 0 t) y c u t- p 0 n t.... in li in( p o o t? in lj in y a s s 1 d in 1 1 lj in 1 y n li in f 1) y y y.... p 0: r e p J. a c 0 d( s y v 2 y in lj in X) y c li p t- 0 in t... in l.i in( p o o t y s y v 2) y s t r c o in c( s y my r 0 01 y in u rn 1) II • H d 0 n s y in 1( s y iri y p 0 01)? X~ y c u r r'- 0 in t.... n »..i in( r 0 o t y in lj rn)? a a s i d in( n lj in X y n u in -f 1)? s y„. p 0 p y pep X a c 0 d( s y v 2 y in u m 1) y c u p f% 0 in t.... in u in( p 0 01 y s y v 2 y s t p c o ri c.( s y in y t- 0 01 y in u in .1.) II
C d 0 in a y in X( a y in y r 0 o t)? i y c.s «..i p 0 in t in lj in( v 0 01? ri u m)? a•: a j. d in (in u in 1 y in u in f 1) y r e t p a 01( c u r r 0 n t.„ ri u in( r o 01 y...)) ?ass 0 t a( c lj v v 0 in bin lj m( y oot? in u m 1)) v st pconc( ayin y poot y numl) II Sample twelve
t r a n s f o r it» ii a m e 0 f t h e p r o b 10 in t c o 3.3. 0 c t.... 3. :i. 'a t d 0 s e r :i. p t :i. o n$ t h 0 o .1.3.0 0 t.... 3. ;i. s t :i. s 3. p r o v i. d 0 d f o r x i s a n a$ 0t n o d 0? 3. i s t h 0 ri r 0 p 3. a e 0 d b y 3.3. 5 x w :i. 3.1 b 0 t i 'i 0 h 0 a d 0 f 3. :l$ a n d 1 w i 3.3. b 0 t h 0 t a :i. 3. o f 3.:!. 11 i s a n 0 b J 0 t a f t h 0 p r 0 b 3.0 in? r 0 c 0 a n :i. z 0 3.1 3. is an object of the prob3.0in? r 0 e o a r 1 :i, z 0 3. t a i 3. :i. s a f i.j r i c t i o 1 1 w :i. t h a r si u in 0n t -a L J.? .1. .1...! r 0 c 0 a n i z 0 3. :L x is an object of th0 problem? r 0 c o a n i z 0 x i 'i e a d i s a f u n c t i 0 n w i t h a r a u rn 0 n t s 13 x 3.1 31 recoss n :i. z 0 1 :L r0c0aniz0 word r0P3.a(.70d recognize 3. replaced i s a f u n c t i o ri w i t11 a r a :..i m 0 n t s 13 3.? .3.1 2 r e c o a n i z e w 0 r d a 0t n o d 0 recoani z0 w0 rd a 1.3. recoSriize x a etriod0 is a function with arguments 13x2 recognize 3. r 0 c a a 1 1 i z 0 w o r d c 0 3.3.0 c t.... 1 i s t c o 13.0 c t... 3. :i. s t i s -a f un c1 i 0 n w i t h a r a -'..i rn 0 n t s 13131 13 2 J13 'J.? 3. H1 h 0 I' r o .1. o a (.7.3. a u s 0 s a r 0 d 0 r i. v 0 d a s
•--• p3.0ase chock if the boundary case is correct!! c o 3.3.0 c t.... 3. i s t (3.) i- c o 3.10 c t.... 3. i s 11 (31 9 3.)••• c 0 3.3.0 01... 3. i s t :l. (1 9 3 1 1)$•
a 01 ri 0 d 0( x) y c 0.3.10 c t.... 1 :i. t :l. (1.3 x! 3.31 v 3 n)• c o 13.0 c t.... 1 :i. s 11 (3.9 3.) e a 1 c a 11 e et I s t 1 a£
M START ttt C c o 11 e c t.... 1. i a t1!
I.t h e? c o lie c t.... 1 :i. a t? :i. a 1. y p r o v i d e d y 1 o r? x? :i. a? a n a y d o tn o d 0?? 1 y i a? then? rep .1. ac0d y ba.!. 1 y v xywi X1ybeyt heyheadvofy11yy 3 n d? 1 y w i 11 y b 0 v!, h e 9 t a :i. 1? a 19 1 1!
[. C O .1. .1. e C t.... 1 1 S t y .'i. 3 y J. y P V Q y i. 1 j 0 d y f O V y y i 3 y 3 li a y 3 0t ii u d 0 y y 1 y :i. a y t h0 n y r 0 p 1. a 0 0 d y 1 :l. y y x y be y head v .1.1 y y 1 •bert3.il »113
C co 110et... 1 i st y be y 1? p r o v i ded y fo r y x ?be? a 11? 3etnode y 1y bey then y rep1acedv11y y x y b e y h e a d y 11 y y lybey tally 113 1 C11y tai1y1y be y 11yheadyXybey I 11 y rep 1 aced y then y 1 y be y 3e tn o d ey a11y xyfor?bey 1 y e o 11 e c t.... 1 i a t y b e y provided!
C 11 y a a.:. e n d 3
t a j. 1 11.) y a a.... e n d 3
P]. y t a i 1 (11) y a a.... e ri d 3
[ a a„ e r i d y t a :i. 1 (1 y 11) 3
C 11 y a a.... e n d y t a :i. 1 (1 y 11) 3
h e a d (11) y s a.... e ri d y t a :i. 1 (1 y 11) 3
C x y h e a d (11) y a a.... e n d y t a :L 1 (1 y 11) 3
L a a.._ e n d y h e a d (x y 11)? t a :i. 1 (1 y 11) 3
C 11 y a a.... e n d y h e•: d (x y 11) y t a .i 1 (1 y 1.1.) 3
C replaced (11) y a a.... r e v( a a.... r e p) y s a.... e n d y h e a d C x y 11) y t a :i. 1 (1 y 11) 3
C r e p 1 a c e d (11) y s a.... r e v( a a.... r e p y a a.... e ii d v h e a a' (y 11) v t a ;j. I. (j.? 11) 3
i: i y r e p l a c e d (11) y a a.... r e v( a a.... r e p) y a a.„. e n d y h e a d (x y 1 1) v t .a :i. 1 (1 y 11) I
c: a a.... e ii d y s a_ r 0 p y r e p 1 a e e d (1 y 11) y h e :j d( x y 11) y t i 1( 1 y 11 .1 L'Set node y sy....end11 y sw....rep v replaced (1y11) y head( a y 1.1.)? Ka i I (1 y I! I 1
Cgetnode? a a.... r e v( a 11) y a a....end y a a.... r e p y replaced (.1. v 11) v heada: 11 v U j 1( I.•'! 1. LI
C y, y getnode y sy.... rev( a 11)? sy..end y sy.... rep y rep 1 aced(] y 1:!.) lie ad (a v 11) ta i 1 1'!.!
I..' a? getnode y sy.... rev( a .11} y a a.... e n d? 'a a.... rep y rep 1 aced (1 1.1) y head (a y 1 1) y ta ;i. 1( X»!. 1.) LI |
I a a.... e n d y a 11 y getnode( a) y a 'a... rep y rep J. aced (1 y 11) y lie ad (a y 11) y ta :i. 1 (.1 y 1) LI
L 1 y sy ....end y a .L J.? g01iiode( a) a ar ep y r ep J. aced( .1.? I.1) y he ad (a v.1.) y t a 1!( I L i?! I i C CO 11 e c t.„. 1 i at (1) y a a e n d y a 11 y getnode( a) y a v.... rep v rep 1 aced (1 r 1:!.) y head( a y]. 1.?? ta :i. 1( 1 y I
I. syirid y co 11 ect.... 1 :i. at (1) y a 11 y ge tnode C a) y sy... rep- rep 1 aced 1 y 11) y head a?] 1) y tai'!( 'i f
IL co 1 lldct.-. 1 i at (1)? t~ y a 11? ge tnode( a) y a a.... rep y rep 1 aced (1 y 11• lie ad (a? X1? ta :i. 1( X y !S. 1.) J • j I 1 C c a 11 e 11„ 1 :i. a t( I) y t- y g e t ri o d e( a)? a a.... r e p y c o1 .1 e c t.... 1 :i. a t (1) L'l
C c o 11 e e t„. 1 i a t1(1? 1n) y t- y a e t n o d e( a) y c o .1.1 e::• t.... 1 :i. a t1 (11 y n) LI
IL c o 11 e c t_ 1 j. a 11. (1 y 1._. ri) 9 t~ y g e t n o d e( a) y c a 11 e 01.... 1 :i. a t1( IL a! 11... 12 y 1 ri) LI r Ccollect_listlll._n) r I-? S3 e t n o d e:•:)? c o 11 e c t.... 1 :i. s 11( C: I 1 I 1 n) I
7.- x Sample thirteen
$ i r a i i a f o r iyi
ii a m e o f t h 0 p r o b 10 iyi t v a n i 'a h 0 d
d 0 a c r :i. p t :i. 0 n t
a r 0 e o r d :i. s v a n :i. a h 0 d p ra v :i. d 0 d :i. f a n y z :i. a t n 0 a a ivi 0 a t r u t u r 0 a a t to 0 1• 0 c a r d? t h 0 n z i a r 0 t r a c 10 d
r 0 c 0 a n i z 0 w 0 r d r 0t r a c t z ia an 0hJ0ct of the prob10m? r 0 c 0 3 n i z 0 z r 0t1 a 0 t :i. a a f i..i n c t :i. o n w :i. f h a r 3 u in 0 ri t a II z .1 viec0rd is an 0bJ0ot of the problem? r 0 c 0 3 n :i. z 0 ro c 0 r d r e e 0 3 n i z 0 p h r a a 0 a a in 0 a t r u et u r 0 r 0 c 0 3 n i z 0 z r 0 c 0 3 n i z 0 w 0 r d a 11
0« u i a t r u :i. a a f u n c t i on w i t h a r 3 u in en t a II z 9 r 0::• 0 r d II i 0co3 n i z 0 w o r d v a n .i. a h 0 d r0c03riiz0r0e0rd v a ri i a h 0 d i a a f u n 0t i 0 n w :i. i h a r 3 u in 0 n t a II r 0 0 0 r d II II2 JII1? 1M1 1 10 P r 010 3 01 a u a 0 a a r 0 d 0 r :i. v 0 d a s 9
p10 a a 0 ahaak if the boundary 0 a a 0 ia 00roof!!
vanished(rooord)$~ f u ri 0t0 r( r 0 c 0 r d 9 a y v 1 a y v 2)? f u n 0t0 r( z? s y v 1? a y v 2) 9 r 01 r a 01( z) 9 v a n i a h 0 d( r 0 0 0 r d) v a n i a h 0 d( r 0 o 0 r d)• o s t v a n :i. s h e d I
START ttt I. v a ii i s h 0 dII
L 3 y r0e0 rq» i. s y vsn ;i sn0d 9 p po1-1 i od ;i f' y any y x y :i. s y the? s3in0 y st .1 ;•u p0•« 3p.,• bhe 0o t h 01 1 y X y i S y 1' 0 t P 3 C t0 d .i 1
L f 0 CQP d y :L S y V 31I .'i.•• h 0 d y P P O V i. d 0 d y :i. T y 8 |i y y j. 3 y 3 3 in 0 v 3b P PiC t '.a Py P 0 C 0 p d y y y
1 th0 ii y X y :i. S y P 0t P 3 C t 0 d..!
II reco rd y b0 y V3H :i. shed y p rov :i. ded y 3.1.1? x v b0 0ou :i. s 1. ru y reco rd• then? x v bo v re b rap
I I C r01 ract y x y th0r« r b0? record? 00ui st ru y x y a 11? h0? van:i. shed y r0co pd1 y b0 y prav :i. d0d 1! ;
[.' p 01 ra c t y s y... 0 n d H
II X y P 0t r 3 C t y S y.... 0 li d 1 t Lx y retrset y sy....eridII
l II s y„ 0 n d y r e t r a e t( x) II I II r0co pd y sy.„©nd y re t rac b( x) II
II e a u i st ri..i( reco rd) y sy_..©nd? re t rac t( x) II
x y© a '..I :i. s t p i.j( r 0 c o p d) y s y.... e n d y r e b r c t( x) II
II x y© e u :i. s t r u( r e c o r d) y y... r g v( a 1 X)» s y... e n d r e t r a c t (x II
IIsy™©iid y a .1.1. y ©Gu :i. st pi..i( x y reco rd) v p© b 'pact,(. x) ..I
C van i shed y sy....end y a 11 v eeu :i. s b r «j( x? r-eeo r d) v r0 r;:'»'•:;' I
II record y van :i. shed y sy....end v a 1 X y ecu :i. st ru( x» reco rd 5? ret p ;x:: I•;) II
[ s y._ 0 n d y v a n i s h 0 d( r 0 c o r% d) y ci !l. J. y 0 g u :i. s t p u(;•( 9 r e c o p d) y p 0 b p :i c 1,(;.; II
II v a ii i s h 0 d( r 0 c o r d) y™ 9 a 1.1? e g li i b r% u( x 9 r e c o r d) y p e t r c: t C x) J
FI v a n i s h 0 d( re c: o r d) 9 t- 9 0 0 u :i. s t r u( x y 0 c: a r d Jure t :i c:? t,( x) y v ii n :i. s I 10 d( r 0 c o p d) ,1
%
% Sample fourteen
£ t r 3li S I O I'll« i i 3 in e o f t1 i e p i' c) b 10 ill S c o ii 0 u 1 t1+ d 0 S C T 1 P t 1 01'I£ consultf of z is defined as f o r -a n y y i s a c 1 a u s e o f a? y i s r e a d? a ri d a :L s p r o c e s s e d« reepSni zewordproce-a sed y i s a ii o b J 0 c t o T th e p r o lv• .1. e ni' f r e e o ii i z e y p r o c 0 s is 0 d :i. s a f u n c t :i. o n w i t h a r si u n e ii t I y j r 0 c o si n i z e w o r d r e ad recoSiiizey read is a function with arguments .y2 z is aii obJ0ct of bhe prob 1 em? recoSriiz© z r e c o S ri i z e w o r d c 1 a u s e r e c o S i i :i. z© y
r e c o si ii i i z e w o r d a1 1 c 1 a u s e i s a f u n c t :i. o ii w i t h a r S u in e n t s C y? z 1 recoSiiizez r© c o si ri i z e w o r d c o n s u 1 t f consul tf is a function with arguments lz! E 2 JII1? 1H T h e P r o 1 o si c 1 a u s e s a r 0 d 0 r i v 0 d a s?
please check if the boundary case is correct!!
C O ll s u 11 f (z t™ s 0 e i 1 1( i ri p) v s 0 0 (z)? c o 1 1 is u 11 f :L (z) 9 c; p pi |i«?
s 0 0( i ri p)• c o ri s 1 j 11 f 1 (z)$ r© -a d (a v p r o c 0 s•: e d( y) y c o ri s u 11 f 1( z) c o ll s u .11 f 1C z) t 0 z i t cat consu 1'tf lo3
START ttt II co n s u 11 f II
II c o ri s u 1 t f y a f y x v :i. s y d e f :i. n 0 d y a a? f o r y a n a a y :i. s y a y»::• I. a li s e o f x y? y a y i S? P 0 a d y y y li u y a y .i. S y p p O O 0 0 S 0 d .'.'I
C. COT1SI..1.1.!• I y X y :i. a y U0 I X Ii0d y f' G P y Sliy y y 'i, S y C .1. SLIS0 9 X y y:• a y!. S y rG?3d y y a y X y p pOC0SS0dI!
L consu I if y x y be y p rov :i. d0d y f o r y a 1I y a be e 1 ause y x y? y a be? read? 5 y a be v p rores sed I!
C p r o c 0 s s 0 d a y b 0 y r 0 a d y a y b 0 y x y c 1 a li s e y? 3 1 X y f q p y b e y;; y o n a u I i f y b e y p p o v :i. d e d I!
L p r o c e s s 0 d ys y.... 0 ri d .J i1
TI y y P)% o a 0 s s 0 d y s a. e n' i I
11 II s a.... 0 1 d y p r o' c 0 s s 0 d a) .II 1 i'1 1 1 t C readsy „.0rid y p roc0ss0d( a) II .
LI a y r 0 a d y s a.... 0 n d p p o 0 0 s s 0 d( a) II
II s a... 0 ri d y r 0 a d( a) y p p o c 0 s s 0 d (a) II
II x y s a„ 0 n d y p 0 a d( a)? p r o c 0 s s 0 d( a) I! 1
C c 1 a u s 0( x) y s a.... 0 ri d y p 0 a d( a) y p r o c 0 s a 0 d (a II 1i I II a r c .L a li s 0( x) y s a... 0 n d y p 0 a d( a) y p p o c 0 a s 0 d( a) 1 A t
C a y c 1 a li s 0( x)? s a... p 0 v( a 1 X) y s a.... 0n d y r 0 -a d( a) v p p o c 0 s s 0 d a) II
Li a y c 1 a li s 0( x) y s a_. r 0 v( a X X) y s a;.... 0 ri d r 0a d( a) y p r o c 0$ s e d( a) I!
II s a... 0 n d y a I X y c X ause( a y x) y r0ad( a)• p rocossed( a) II
[. x s a... 0iid y a X .1. v c .1. a 1.1 s 0( a• x) y r 0 3d( a) p r-er: 0s s 0 d'•).!
II c o ri s li X t f( x) y s a.... 0 ri d y a X 1 y c X a u s 0(•: y x) y r 0: d( a)? p r o 0 0 a•: e d().!
L s a_ 0 ri d y c o n s 1.111 f( x) y a J. J. y c X a u s 0( a y x) y r 0 a d( a) y p r o c 0 a s 0 d( a) ..I
C C O li SIJ 11 f( X) y all yc 1 a LI s 0( a y) y!• 0•: d'•) y P P o c.% 0 a: 0 d( a) .1
II corisI..111 f( x) y?- y P0Sd( a) y p roc0ss0d( a) y corisu X t fv() II
c; consu 11f 1 (x) y t- y r03d a) y p roc0s:0d( a) y con;u X tf 1 x) I! PUBLICATION
[1] Kam Wing Ng Wai Yan Ma
"Design Considerations for a Prolog Program Generator" Hong Kong Computer Journal, vol 1 no 5, May, 1985, pp 20-26.