Skip to content

Higher order functions

Kyle Knoepfel edited this page May 10, 2023 · 8 revisions

The concept of higher-order functions are fundamental to Meld's design. The Wikipedia definition of a higher-order function is:

A function that does at least one of the following:

  • takes one or more functions as arguments, which is a parameter of a procedure that is itself a procedure,
  • returns a function as its result.

The first item above applies for framework contexts. In particular, the framework user supplies a function $f$ that serves as an operation to a framework-supported higher-order function of the form:

    $\left\{(\boldsymbol{a})_n \xrightarrow{f} (\boldsymbol{b})_m\right\} \in \mathcal{D}$

where $\mathcal{D}$ is the domain of the higher-order function. Below, we explain the term domain and list the higher-order functions supported by Meld.

Function domains

Consider the graph below (previously described here):

There are three explicit domains in the above figure:

  • $\mathcal{R}$, which corresponds to a run,
  • $\mathcal{S_\alpha}$, which corresponds to a collection of subruns, and
  • $\mathcal{E_n}$, which corresponds to a collection of events.

The mappings depicted in the graph above are representable mathematically as:

  • $\left\{(a)_8 \xrightarrow{f} (b)_8\right\} \in \mathcal{E_n}$ (transform),
  • $\left\{(c)_8 \xrightarrow{g_0} (K)_4\right\} \in \mathcal{S_\alpha}$ (reduction),
  • $\left\{(J,K)_4 \xrightarrow{h_0} (W)_1\right\} \in \mathcal{R}$ (reduction), where the sequences $(J)_4$ and $(K)_4$ are zipped together and presented as a pair to the function $h_0$.

Observe that for reductions, the arrow connecting two sequences crosses one or more boundaries from a more nested domain to a higher one. For splitters, the arrow is reversed and it connects two sequences by crossing one or more boundaries from a higher domain to a more nested one.

Note: The domain of each input argument to the user function $f$ need not be part of the same domain as the elements of the product-sequence mapping. An example is the $g_0$ reduction referenced above, where the products labeled $c$ are elements of the $\mathcal{E_n}$ domain, but the actual reduction takes place in the $\mathcal{S_\alpha}$ domain.

Supported higher-order functions

Meld's supported higher-order functions are shown in the table below.

Higher-order function Product sequence mapping User operation
Transform (map) $(\boldsymbol{a})_n \xrightarrow{f} (\boldsymbol{b})_n$ $f(\boldsymbol{a}) \rightarrow \boldsymbol{b}$
Filter $(\boldsymbol{a})_n \xrightarrow{f} (\boldsymbol{a})_m$ $f(\boldsymbol{a}) \rightarrow \text{Bool}$
Monitor $(\boldsymbol{a})_n \xrightarrow{f} (\ \ )_0$ $f(\boldsymbol{a}) \rightarrow \text{Void}$
Reduction (fold) $(\boldsymbol{a})_n \xrightarrow{f_b} (\boldsymbol{b})_1$ $f_\boldsymbol{b}(\boldsymbol{a}) \rightarrow \boldsymbol{b}$
Splitter (unfold) $(\boldsymbol{a})_1 \xrightarrow{f_p} (\boldsymbol{b})_n$ $f_p(\boldsymbol{a}) \rightarrow (\boldsymbol{b})_p$