All Topics  
Purely functional

 
Purely Functional

   Email Print
   Bookmark   Link






 

Purely functional



 
 
Purely functional is a term in computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
 used to describe algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
s, data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
s or programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s that exclude destructive modifications (updates). According to this restriction, variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
s are used in a mathematical sense, with identifiers referring to immutable, persistent values.

Benefits and applications
The persistence property of purely functional data structures can be advantageous in the development of many applications which deal with multiple versions of an object.

For example, consider a comprehensive web-based thesaurus service that uses a large red-black tree
Red-black tree

A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays....
 to store its list of synonym relationships, and that allows each user to add their own custom words to their personal thesaurus.






Discussion
Ask a question about 'Purely functional'
Start a new discussion about 'Purely functional'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Purely functional is a term in computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
 used to describe algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
s, data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
s or programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s that exclude destructive modifications (updates). According to this restriction, variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
s are used in a mathematical sense, with identifiers referring to immutable, persistent values.

Benefits and applications


The persistence property of purely functional data structures can be advantageous in the development of many applications which deal with multiple versions of an object.

For example, consider a comprehensive web-based thesaurus service that uses a large red-black tree
Red-black tree

A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays....
 to store its list of synonym relationships, and that allows each user to add their own custom words to their personal thesaurus. One way to do this is to make a copy of the tree for each user, and then add their custom words to it; however, this is wasteful duplication. Moreover, it would cause a significant processing delay to make the complete copy of the tree.

A better approach is to store the words in an immutable (and therefore purely functional) red-black tree. Then, one can simply take the original version and produce a new tree based on it for each set of custom words. Because these new trees share large amounts of structure with the main tree, the space overhead for each additional user is at most , where is the number of custom nodes. With a mutable red-black tree, this approach would not work, since changes to the main tree would affect all users.

Besides their efficiency benefits, the inherent referential transparency of functional data structures tends to make purely functional computation more amenable to analysis and optimization, both formal and informal.

Examples of purely functional data structures


Linked lists


This example is taken from Okasaki. See the bibliography.

Singly linked list
Linked list

In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of node s, each containing arbitrary data Field s and one or two reference s pointing to the next and/or previous nodes....
s are a bread and butter data structure in functional languages. In ML
ML programming language

ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM....
-derived languages and Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, they are purely functional because once a node in the list has been allocated, it cannot be modified, only copied or destroyed.

Consider the two lists: xs = [0, 1, 2] ys = [3, 4, 5]

These would be represented in memory by:

where a circle indicates a node in the list (the arrow out showing the second element of the node which is a pointer to another node).

Now concatenating the two lists: zs = xs ++ ys results in the following memory structure:

Notice that the nodes in list xs have been copied, but the nodes in ys are shared. As a result, the original lists (xs and ys) persist and have not been modified.

The reason for the copy is that the last node in xs (the node containing the original value 2) cannot be modified to point to the start of ys, because that would change the value of xs.

Trees


This example is taken from Okasaki. See the bibliography.

Consider a binary tree
Binary tree

In computer science, a binary tree is a Tree in which each node has at most two child node. Typically the child nodes are called left and right....
 used for fast searching, where every node has the recursive
Recursion

Recursion, in mathematics and computer science, is a method of defining Function in which the function being defined is applied within its own definition....
invariant
Invariant (computer science)

In Computer science, a predicate that, if true, will remain true throughout a specific sequence of Operation , is called invariant to that sequence....
that subnodes on the left are less than the node, and subnodes on the right are greater than the node.

For instance, the set of data xs = [a, b, c, d, f, g, h] might be represented by the following binary search tree:

A function which inserts data into the binary tree and maintains the invariant is:

fun insert (x, E) = T (E, x, E) | insert (x, s as T (a, y, b)) = if x < y then T (insert (x, a), y, b) else if x > y then T (a, y, insert (x, b)) else s

After executing ys = insert ("e", xs) we end up with the following:

Notice two points: Firstly the original tree (xs) persists. Secondly many common nodes are shared between the old tree and the new tree. Such persistence and sharing is difficult to manage without some form of garbage collection
Garbage collection (computer science)

In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage , or memory used by Object that will never be accessed or mutated again by the Application software....
 (GC) to automatically free up nodes which have no live references, and this is why GC is a feature commonly found in functional programming languages
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
.

Reference cycles

Since every value in a purely functional computation is built up out of existing values, it would seem that it is impossible to create a cycle of references, resulting in a reference graph (a graph which has edges from objects to the objects they reference) that is a directed acyclic graph
Directed acyclic graph

In computer science and mathematics, a directed acyclic graph, also called a DAG, is a with no ; that is, for any Vertex v, there is no nonempty directed path that starts and ends on v....
. However, in most functional languages, functions can be defined recursively
Recursion

Recursion, in mathematics and computer science, is a method of defining Function in which the function being defined is applied within its own definition....
; this capability allows recursive structures using functional suspensions. In lazy
Lazy evaluation

In computer programming, lazy evaluation is the technique of delaying a computation until such time as the result of the computation is known to be needed....
 languages, such as Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, all data structures are represented as implicitly suspended thunk
Thunk

The word thunk has at least three related meanings in computer science. A "thunk" may be:* a piece of code to perform a delayed computation * a feature of some virtual function table implementations ...
s; in these languages any data structure can be recursive. Some other languages, such as Objective Caml
Objective Caml

Objective Caml, or OCaml is the main implementation of the Caml programming language, created by Xavier Leroy, J?r?me Vouillon, Damien Doligez, Didier R?my and others in 1996....
, allow the explicit definition of recursive values.

See also

  • Pure function
    Pure function

    In computer programming, a function may be described as pure if both these statements about the function hold:# The function always evaluates the same result value given the same argument value....
  • Persistent data structure
    Persistent data structure

    In computing, a persistent data structure is a data structure which always preserves the previous version of itself when it is modified; such data structures are effectively immutable, as their operations do not update the structure in-place, but instead always yield a new updated structure....
  • VList
    Vlist

    Vlist is a village and municipality in the western Netherlands, in the province of South Holland. The municipality covers an area of 56.52 km? and had a population of 9,803 in 2004....
  • Identity (object-oriented programming)
    Identity (object-oriented programming)

    An identity in object-oriented programming, object-oriented design and object-oriented analysis describes the property of object s that distinguishes them from other objects....


External links

  • thesis by Chris Okasaki (PDF format)
  • by James R. Driscoll, Neil Sarnak, Daniel D. Sleator, Robert E. Tarjan (PDF)
  • by James R. Driscoll, Daniel D. Sleator, Robert E. Tarjan (PDF)
  • from MIT open course


Bibliography


Purely functional data structures by Chris Okasaki, Cambridge University Press
Cambridge University Press

Cambridge University Press is a printer and publisher granted a Royal Letters Patent by Henry VIII of England in 1534. It is the world's oldest continually operating book publisher....
, 1998, ISBN 0-521-66350-4.