Top Up Prev Next Bottom Contents Index Search

6.1 Introduction

A function is higher-order if it takes a function as an argument and/or returns a function. A classic example is mapcar in Lisp, which takes two arguments, a function and a list. Its behavior is to apply the function to each element of the list and to return a list of the results. The HOF domain implements a similar function, in the form of a star called Map, that can apply any other star (or galaxy) to the sequence(s) at its inputs. Many other useful higher-order functions are also provided by this domain.

The HOF domain provides a collection of stars designed to be usable in all other Ptolemy domains. To preserve this generality, not all interesting higher-order functions can be implemented in this domain. As a consequence, some individual domains may also define higher-order functions. In fact, any higher-order function with domain-specific behavior must be implemented in its respective domain. The HOF domain is included as a subdomain by all other domains. In Ptolemy 0.7 and later, HOF can be used in both graphical and non-graphical Ptolemy Tcl interpreters.

A common feature shared by all the stars in this domain is that they perform all of their
operations in the preinitialize method. Moreover, their basic operation is always
to disconnect themselves from the graph in which they appear and then to self-destruct. Since
the preinitialization method of the stars in a universe is invoked before the preinitialization
method of the scheduler, the scheduler never sees the HOF stars. They will have self-
destructed by the time the scheduler is invoked. This is why these stars will work in any
domain. In code generation domains, an important feature of the HOF stars is that they add no
run-time overhead at all, since they self-destruct before code generation begins, and therefore
do not appear in any form in the generated code.

Many of the HOF stars will replace themselves with one or more instances of another star or galaxy, called the replacement block. Replacement blocks generally go into the graph in the same position originally occupied by the HOF star, but different HOF stars will connect these replacement blocks in different ways.

Some HOF stars have no replacement block. Before they self destruct, they will typically only alter the connections in the graph without adding any new blocks. An example is the BusMerge block, which merges two busses into one wider bus. These stars are called bus manipulation stars.

The experienced reader may have some difficulty connecting the concept of higher-order functions, as implemented in this domain, to that used in functional programming. This issue is covered in some depth in [Lee95], but we can nonetheless give a brief motivation here. In functional languages, there is no syntactic difference between a function argument that is a data value, one that is a stream (an infinite sequence of data values), and one that is a function. In visual programming, however, functions typically have two very different syntaxes for their arguments. Ptolemy is no exception. Stars and galaxies in Ptolemy are functions with two kinds of arguments: input streams and parameters. The HOF domain only contains stars where a parameter may be function. It does not contain any stars that will accept functions at their input portholes as part of an input stream, or produce functions at their output portholes. Although in principle such higher-order functions can be designed in Ptolemy, their behavior would not be independent of their domain, so the HOF domain would be the wrong place for them.



Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.