Opentopia Directory Encyclopedia Tools

Function composition (computer science)

Encyclopedia : F : FU : FUN : Function composition (computer science)


In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of the composed function is passed to the composing one via a parameter. Because of this similarity, the syntax in program code tends to closely follow that in mathematics. As a subroutine, the feature appears in most programming languages.

For example, suppose we have two arithmetic functions [f] and [g], as in [z=f(x)] and [y=g(x)]. One way of composing these two functions would be to first compute [y], and then to compute [z] from [y], as in [y=g(x)] followed by [z=f(y)]. In a programming context, the only obvious difference from mathematics involves notation. Here is the same example but implemented in the C programming language:

float foo (float x)
One could get the same result with the one line composition in mathematics [f(g(x))], by the corresponding one line function in C:

float foo (float x) 
Despite differences in length, these two implementations compute the same result (providing their data types are the same). The first implementation, the one having more surface area, is also known as a "single-assignment" form of function composition. This form is useful in the areas of parallel programming and embedding logic onto field programmable gate array devices (see Hammes, et. al). The example above illustrates a type of functional composition known as "sequential composition" (Abadi and Lamport pg 96), since the result of the second function depends on the result of the first. Another type of functional composition known as "parallel composition" (Pierce and Turner pg 2) (Abadi and Lamport pg 96), enables a developer to compose two or more functions so that each runs in parallel on its own separate computer.

The second implementation requires only one line of code and is colloquially referred to as a "highly composed" form. Readability and hence maintainability is one advantage of highly composed forms, since they require fewer lines of code, minimizing a program's "surface area" (Cox pp 15-17). DeMarco empirically verifies an inverse relationship between surface area and maintainability (DeMarco pp 133-135). On the other hand, it may be possible to overuse highly composed forms. A nesting of, say, seven (see Miller) or more functions may have the opposite effect, making the code less maintainable.

A related issue is the dilemma of whether to compose (put together) or "factor" (break apart) functions for maintainability and code reuse. A similar dilemma faces [WikipediansWikipedians] as to whether to merge together or to break apart certain articles.

In a fuctional programming language, such as Haskell, function composition can be expressed in a naturally. The example give above becomes:

f . g
using the composition operator (.) :: (b -> c) -> (a -> b) -> a -> c, which can be read as f after g or g composed with f.

== Research Survey == Notions of composition, including the principle of compositionality and composability, are so ubiquitous that numerous strands of research have separately evolved. The following paragraph is a sampling of the kind of research in which the notion of composition is central.

Steele directly applied function composition to the assemblage of building blocks known as 'monads' in the Haskell programming language. Bertrand Meyer addressed the software reuse problem in terms of composability. Martín Abadi and Leslie Lamport formally defined a proof rule for functional composition that assures a program's safety and liveness. Marcus Kracht identified a strengthened form of compositionality by placing it into a semiotic system and applying it to the problem of structural ambiguity frequently encountered in computational linguistics. Timothy van Gelder and Robert Port examined the role of compositionality in analog aspects of natural language processing. According to a review by Jeremy Gibbons, formal treatment of composition underlies validation of component assembly in visual programming languages like IBM's Visual Age for the Java programming language.

== References ==

== See also ==

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: