Content deleted Content added
m Was this text meant to be in italics "that is, an update which"..? |
|||
Line 3:
{{refimprove|date=January 2017}}}}
In [[computer science]], a '''purely functional data structure''' is a [[data structure]] that can be implemented in a [[purely functional language]]. The main difference between an arbitrary data structure and a purely functional one is that the latter is (strongly) [[immutable object|immutable]]. This restriction ensures the data structure possesses the advantages of immutable objects: (full) [[persistent data structure|persistency]],<nowiki/> quick copy of objects, and [[thread safety]]. Efficient purely functional data structures may require the use of [[lazy evaluation]] and [[memoization]].{{not typo}}
==Definition==
[[Persistent data structure|Persistent data structures]] have the property of keeping previous versions of themselves unmodified. On the other hand, structures such as [[Array data structure|arrays]] admit '''destructive update'''
Formally a ''Purely functional data structure'' is a data structure which can be implemented in a [[purely functional language]], such as [[Haskell (programming language)|Haskell]]. In practice, it means that the data structures must be built using only persistent data structures such as tuples, [[sum type]]s, [[product type]]s, and basic types such as integers, characters, strings. Such a data structure is necessarily persistent. However, all persistent data structures are not purely functional
In the book ''Purely functional data structures'', Okasaki compare destructive updates to master chef's knives.{{r|Okasaki-book|p=2}}
==Ensuring that a data structure is purely functional==
A data structure is never inherently functional. For example, a stack can be implemented as a [[singly-linked list]]. This implementation is purely functional as long as the only operations on the stack return a new stack without altering the old stack. However, if the language is not purely functional, the run-time system may be unable to guarantee immutability. This is illustrated by Okasaki,{{r|Okasaki-book|pp=9-11}}
In order to ensure that a data structure is used in a purely functional way in an impure functional language, [[modular programming|modules]] or [[class (computer programming)|classes]] can be used to ensure manipulation via authorized functions only.{{cn|date=December 2018}}
Line 33:
Lazy evaluation is particularly interesting in a purely functional language{{r|Okasaki-book|p=31}} because the order of the evaluation never changes the result of a function. Therefore, lazy evaluation naturally becomes an important part of the construction of purely functional data structures. It allows computations to be done only when its result is actually required. Therefore, the code of a purely functional data structure can, without loss of efficiency, consider similarly data that will effectively be used and data that will be ignored. The only computation required is for the first kind of data that will actually be performed.{{cn|date=December 2018}}
One of the key tools in building efficient, purely functional data structures is memoization.{{r|Okasaki-book|p=31}}
===Amortized analysis and scheduling===
Some data structures, even those that are not purely functional such as [[dynamic array|dynamic arrays]], admit operation that are efficient most of the time (i.e. constant time for dynamic arrays), and rarely inefficient (i.e. linear time for dynamic arrays). ''[[Amortized analysis|Amortization]]'' can then be used to prove that the average running time of the operations are efficient.{{r|Okasaki-book|p=39}}
In general, having inefficient operations is not acceptable for persistent data structures, because this very operation can be called many times. It is not acceptable either for real-time or for imperative systems, where the user may require the time taken by the operation to be predictable. Furthermore, this unpredictability complicates the use of [[Parallel computing|parallelism]].{{r|Okasaki-book|p=83}}
In order to avoid those problems, some data structures allow for the inefficient operation to be postponed—this is called [[Scheduling (computing)|scheduling]].{{r|Okasaki-book|p=84}}
====Example: queue====
|