Function Definitions

A Refal program consists of function definitions, each definition having either of the two forms:
    Fname \{ Snt1; Snt2; ... Sntn; };
    Fname  { Snt1; Snt2; ... Sntn; };

where Fname is the name of the function being defined, and Snt1, Snt2, ..., Sntn are sentences. (Being, at present, of no importance, the subtle difference between \{ and { will be explained later.)

Each sentence Sntj is of the form Pj Rj, with Pj being the input pattern of the sentence, and Rj the rest of the sentence. A function definition specifies the way in which the calls to the function are to be evaluated. Suppose a call
    <Fname Re>

to the function Fname is to be evaluated. Then the result expression Re is evaluated. If a ground expression Ge is returned, an attempt is made to match Ge against the input patterns P1, P2, ..., Pn, in order to find the first pattern Pj such that matching Ge against P succeeds. Let Env be the "first" variant of matching Ge against P. Then the rest Rj is evaluated in the environment Env. If a ground expression Ge' is returned, this expression is taken to be the result of evaluating the function call.

For the time being, for the sake of simplicity, each rest Rj will be assumed to be of the form
    = Rej        

where Rej is a result expression. A rest of the form = Re is a special case of right hand side. Evaluating a right hand side = Rej amounts to evaluating the result expression Rej. If the evaluation of Rej results in returning a ground expression Ge, then Ge is taken to be the result of the whole right hand side.

For example, let us consider a function SumSq computing the sum of the squares of two numbers. Here is the definition of this function written in traditional notation
    SumSq(X,Y) = X*X + Y*Y
which may be rewritten in Refal in the following way:
$func SumSq sX sY = sZ;

SumSq
 {
 sX sY = <Add <Mult sX sX> <Mult sY sY>>;
 };

It should be noted that the declaration of a function must precede the function's definition as well as the calls to the function, since the information provided by the declaration is necessary for compiling the function's definition as well as the calls to the function.

If the function declaration has the form
$func Fname Fin = Fout;
the compiler verifies that the input patterns P1, P2, ..., Pn are "instances" of the input format Fin, whereas all the rests R1, R2, ..., Rn are certain to return ground expressions satisfying the output format Fout.