4.9 Procedures

A functional procedure is a procedure which computes a function. Given a value of the functions domain, it computes the result of applying the function to this value. If this computation terminates then this output value is returned as output.

As an example, we consider a the {\rm square} function of type

{\rm square}: {\rm number} \rightarrow {\rm number}

which maps a number to its square. The corresponding functional procedure can be applied to an integer input and the returns the square of the input as its output. It can be defined as follows:

declare Square = fun{$ N} N*end 

There exists a nicer syntax where dollar symbols are non needed. With this syntax, the above definition can be rewritten equivalently to:

declare fun{Square N} N*end 

We can now built expressions that describe the value to which the function maps another value. For instance, the value of the following application is 16.

{Inspect {Square 4}}

So far, we have seen only functions with a single input and a single output argument. It is also easily possible to define functional procedures for functions with several inputs. As an example, we consider the ternary Boolean valued function which tests whether the sum of the squares of the first two arguments is greater than the square of the third argument.

%% an arithmetic test  
declare Square = fun{$ X} X*end 
declare fun{Test X Y Z}
           {Square X} + {Square Y} == {Square Z}
        end 
{Inspect {Test 3 4 5}}

This function can be given two different types since the comparision function in Oz can either compare floats or integers.

\begin{array}{l}
{\rm maximum}: {\rm int} \times {\rm int} \times {\rm int} \rightarrow {\rm int} \\ 
{\rm maximum}: {\rm float} \times {\rm float}\times {\rm float} \rightarrow {\rm float} 
\end{array}

Note that floats cannot directely be compared with numbers and vice versa. Usually, one calls functions polymorphic if they can be applied to different types.

Functions without output argument are simply relations and the corresponding procedures relational procedures. Oz provides extra syntax to write relational procedures. For instance, we can define a our own output procedures (which output by side effect instead of returning values):

declare proc{MyInspect X}
           {Show X}
           {Inspect ['MyInspect: ' X]}
        end 
{MyInspect hiho}

The application of a relational procedure is a statement which does not describe a value in contrast to an expression. This explains also why {Browse X} does not have a value.


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)