User-Defined Functions

Flora allows you to define functions. This capability provides a nice syntax that allows more concise expressions, often omitting some variables.

As an example, suppose we want to define several operations on quantities (see Creating Structured Data). We have the addq predicate as defined earlier, and also analogously subq, divq and multq.

Now suppose we have to do some complex math on quantities where we combine several of these operations. For example, let’s calculate “((3m + 10cm) * (1.4m)) / 2ft” (omitting namespace prefixes for brevity):

addq(?q1, quantity(3,meter), quantity(10,centimeter)),
multq(?q2, ?q1, quantity(1.4,meter)).
divq(?q3, ?q2, quantity(2,foot)).

We have to use three expressions, and two intermediate variables ?q1 and ?q2. This is reminiscent of RPN [1] .

Instead, we can define functions:

\udf addqf(?q1,?q2) := ?q3 \if addq(?q3,?q2,?q1).
\udf multqf(?q1,?q2) := ?q3 \if multq(?q3,?q2,?q1).
\udf divqf(?q1,?q2) := ?q3 \if divq(?q3,?q2,?q1).

and so on. This allows us to write the expression above as:

?q = divqf(
       multqf(
         addqf(
           quantity(3,meter),
           quantity(10,centimeter)),
         quantity(1.4,meter)),
       quantity(2,foot)).

(indentation used is to clearly show the structure of the term). Internally, Flora will convert this to something like our original non-functional version above.

[1]http://en.wikipedia.org/wiki/Reverse_Polish_notation