.he 'RECURSION1''Page %'
.fo 'Steven Hardy'- % -'December 1077'
.ce 3
Recursive Definitions
=====================
(Illustrated with POP11 examples)
.sp
This handout discusses 'recursive' definitions, that is defining
some concept in terms of itself (or some group of concepts in terms of each other).
.sp
For example, does society condition the way people think or is it the way people
think that shapes society?
I think that both are true,
but many people feel intuitively suspicious of such circular arguments.
.sp
One way of defining things recursively is to treat them numerically, for example:
 	New_GNP = Old-GNP * Some-function(Old-population)
 	New_population = Old_population * Some_other_function(Old_GNP)
.br
.sp
However, the type of concept I am interested in modelling, cannot be reduced to numbers since I
am concerned to preserve their internal structure.
.sp
I can get some idea of how this is possible from modern programming languages.
These permit the programmer to define operations and predicates in terms of themselves.
Some people find this a little disturbing (or at least confusing)
and so it might be instructive to consider how a computer
can do this.
.sp
Consider the following definition of the 'the sum of a set of numbers':
.in+8
The sum of a set of numbers is zero if the set is empty and the first element of the
set plus the sum of the  rest otherwise.
.in-8
When translated into POP11 this becomes:
 	: FUNCTION SUM(SET);
 	:	IF SET = [] THEN
 	:		0
 	:	ELSE
 	:		HD(SET) + SUM(TL(SET))
 	:	CLOSE
 	: END;
.br
If you are completely unfamiliar with POP11 you will find it helpful to know that
.in+8
.ti -4
(a) [] is a representation of the empty set,
.ti -4
(b) HD is a built in operation to find the first element of a set,
.ti -4
(c) TL is a built in operation to find all but the first element of a set.
.in-8
.sp
A second example is the definition of the largest number in a set, thus:
.in+8
The largest number in a set with only one element is that number.
If there is more than one element then compare the first with the largest of the rest;
the greater of these two is the largest number in the whole set.
.in-8
.tp 10
One way of saying this in POP11 is:
 	: FUNCTION LARGEST(SET);
 	:	IF TL(SET) = [] THEN
 	:		HD(SET)
 	:	ELSEIF HD(SET) > LARGEST(TL(SET)) THEN
 	:		HD(SET)
 	:	ELSE
 	:		LARGEST(TL(SET))
 	:	CLOSE
 	: END;
.br
.sp
These two definitions can be typed into the POP11 system
and it will then be ready to answer questions about LARGEST elements and the like, for example:
 	: VARS AGES;
.br
ie - I am going to be using the word AGES as a variable.
 	: [23 42 17 9 16 32 15 12 7] -> AGES;
.br
ie - initialize the set of ages to the numbers 23, 42, 17 etc.
 	: SUM(AGES) =>
 	** 173
.br
ie - what is the sum of the ages?
 	: LARGEST(AGES) =>
 	** 42
.br
ie - what is the largest age?
.sp
To explain how the computer is able to understand these recursive
definitions I will have to make use of a metaphor that may strike you
as childish - yet for all that it is helpful, so bear with me.
.sp
Imagine that the inside of the computer houses a library of function
definitions and other such things. Close by the library is a waiting
room full of men sitting around doing nothing.
When I type in a request to run a function, for example:
 	: SUM(AGES) =>
.br
one of the men in the waiting room is instructed by a monitoring process to work this
out for me.
He gets a photocopy of the instructions for SUMming sets
from the library and takes note of the value of the variable
AGES (which is also stored in the library).
This he knows by the name SET, for this was the name given to
the argument of SUM when it was defined.
As the man works through the definition of SUM he always gets a friend
(from the waiting room) to evaluate subsiduary function calls, so that
when evaluating the line
 	: HD(SET) + SUM(TL(SET))
.br
he asks for a HD to be done (passing over the value he associates with
SET as argument to the man performing the HD operation), he then asks
for a TL to be done and then passes the result of that 'call' of
TL over to a man doing a second SUM process. Finally our man adds together
the result of the HD and SUM operations.
.sp
Notice that our first man does not get involved with evaluating the
recursive call. A second man comes from the waiting room, with his own
photocopy of the SUM instructions and his own idea of the value associated
with the name SET.
This second man will in turn ask a third man to perform a SUM operation.
At one stage, there will be
ten men working on slightly different versions of a SUM process.
The tenth will think the SET is empty and be able to 'return' the
result zero without 'invoking' an eleventh SUM operation. Once this tenth man
has finished the ninth continues (by getting a friend to the actual
addition) and then returns his result to the eighth man and so on
until the whole process is complete.
.sp
The advantage of this approach to evaluating recursive definitions
is that each of the men involved has a straightforward task - merely to
remember what his 'local variables' are and to remember how far through
the function definition he is. 
He doesn't have to understand the process as a whole - just his part in it
(which is simple).
.sp
The man carrying out an operation can be asked to remember values
other than their 'arguments' (or 'inputs'). Consider the following:
 	: FUNCTION LARGEST(SET);
 	:	VARS TEMP;
 	:	IF TL(SET) = [] THEN
 	:		HD(SET)
 	:	ELSE
 	:		LARGEST(TL(SET)) -> TEMP;
 	:		IF HD(SET) > TEMP THEN HD(SET) ELSE TEMP CLOSE
 	:	CLOSE
 	: END;
.br
In this example, the man performing a LARGEST process has
been asked to remember an additional local variable' called
TEMP (that's what the VARS statement is for). Initially the man
doesn't know what value to associate with this name, but if he has
to obey the instructions on the sixth line of the definition
he encounters an 'assignment statement'
 	: LARGEST(TL(SET)) -> TEMP;
.br
which can be roughly taken to mean 'work out the
largest element of the rest of the set and remember it by the name TEMP'.
.sp
Can you say why this second definition of LARGEST is quicker than the first?
.sp
This handout has been hurriedly prepared and I haven't been able to show
an example of 'mutually recursive' definitions -
where two or more operations are defined in terms of each other -
the POP11 system can use these sort of definitions as easily as the
more straightforward 'simple recursion' shown above.
Moreover, the examples I have given have relied on your understanding
of numbers. POP11 has many structure building and structure editing facilities.
Recursive definitions are often the
.ul
only
way of manipulating the complex objects one can create using these
facilities.
.sp
Some things to do:
.in+8
.ti-4
(a) Read the section on recursion in the Edinburgh LOGO notes (in the
Cognitive Studies library).
.ti -4
(b) With a few friends try working out the SUM and LARGEST
of a few simple sets of numbers.
.ti -4
(c) Write a definition of your own to find the SMALLEST number in some set.
.ti -4
(d) What does the following function do:
 	: FUNCTION SILLYPRINT(SET);
 	:	IF SET = [] THEN
 	:		"FINISHED" =>
 	:	ELSE
 	:		HD(SET) =>
 	:		SILLYPRINT(TL(SET));
 	:		HD(SET) =>
 	:	CLOSE
 	: END;
.ti-4
(e) Read the RECURSION2 demo.
