Mass spectrometry: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Taram
Undid revision 592973260 by Taram (talk) repair error from edit conflict
Updating the realtime link as author has changed url
 
Line 1: Line 1:
In [[computer programming]], particularly [[functional programming]] and [[type theory]], an '''algebraic data type''' is a kind of [[composite type]], i.e. a type formed by combining other types.
Two common classes of algebraic type are [[product type]]s, i.e. [[tuple]]s and [[record (computer science)|records]], and [[sum type]]s, also called [[tagged union]]s or ''variant types''.<ref>[http://caml.inria.fr/pub/docs/manual-ocaml/manual003.html#toc7 Records and variants] - OCaml manual section 1.4</ref>


The [[value (computer science)|values]] of a product type typically contain several values, called ''fields''.  All values of that type have the same combination of field types.  The set of all possible values of a product type is the [[Cartesian product|set-theoretical product]] of the sets of all possible values of its field types.


The values of a sum type are typically grouped into several classes, called ''variants''.  A value of a variant type is usually created with a quasi-functional entity called a ''constructor''.  Each variant has its own constructor, which takes a specified number of arguments with specified types.
Adrianne is what you has the capability to call me but I just don't like when the public use my full [http://Www.Wired.com/search?query=address address]. What I love doing are to play croquet  now I have your time to take on new things. The job I've been occupying for many years is an rule clerk. My husband and I decided upon to reside in Guam but I will attain to move in per year or two. You can sometimes find my website here: http://prometeu.net<br><br>Here is my blog; clash of clans hack android - [http://prometeu.net prometeu.net],
The set of all possible values of a sum type is the set-theoretical sum, i.e. the [[disjoint union]], of the sets of all possible values of its variants. [[enumerated type]]s are a special case of sum types in which the constructors take no arguments.
 
Values of algebraic types are analyzed with [[pattern matching]], which identifies a value by its constructor or field names and extracts the data it contains.
 
Algebraic data types were introduced in [[Hope (programming language)|Hope]],  a small functional programming language developed in the 1970s at Edinburgh University.<ref>{{cite conference
|quote=Presentations included Rod Burstall, Dave MacQueen, and Don Sannella on Hope, the language that introduced algebraic data types
|title=A history of Haskell: being lazy with class
|url=http://dl.acm.org/citation.cfm?id=1238856
|author=Paul Hudak, John Hughes, Simon Peyton Jones, Philip Wadler
|booktitle=Proceedings of the third ACM SIGPLAN conference on History of programming languages}}</ref>
 
==Examples==
One of the most common examples of  an algebraic data type is the singly linked list. A list type is a sum type with two variants, <code>Nil</code> for an empty list and <code>Cons ''x'' ''xs''</code> for the combination of a new element ''x'' with a list ''xs'' to create a new list:
<source lang="haskell">
data List a = Nil | Cons a (List a)
</source>
<code>Cons</code> is an abbreviation of ''cons''truct. Many languages have special syntax for lists. For example, Haskell and ML use <code>[]</code> for <code>Nil</code>, <code>:</code> or <code>:</code><code>:</code> for <code>Cons</code>, and square brackets for entire lists. So <code>Cons 1 (Cons 2 (Cons 3 Nil))</code> would normally be written as <code>1:2:3:[]</code> or <code>[1,2,3]</code> in Haskell, or as <code>1::2::3::[]</code> or <code>[1;2;3]</code> in ML.
 
For another example, in [[Haskell (programming language)|Haskell]] we can define a new algebraic data type, <code>Tree</code>:
<source lang="haskell">
data Tree = Empty
          | Leaf Int
          | Node Tree Tree
</source>
 
Here, <code>Empty</code> represents an empty tree, <code>Leaf</code> contains a piece of data, and <code>Node</code> organizes the data into branches.
 
In most languages that support algebraic data types, it is possible to define [[parametric polymorphism|parametric]] types. Examples are given later in this article.
 
Somewhat similar to a function, a data constructor is applied to arguments of an appropriate type, yielding an instance of the data type to which the type constructor belongs. For instance, the data constructor <code>Leaf</code> is logically a function <code>Int -> Tree</code>, meaning that giving an integer as an argument to <code>Leaf</code> produces a value of the type <code>Tree</code>. As <code>Node</code> takes two arguments of the type <code>Tree</code> itself, the datatype is [[recursive type|recursive]].
 
Operations on algebraic data types can be defined by using [[pattern matching]] to retrieve the arguments. For example, consider a function to find the depth of a <code>Tree</code>, given here in Haskell:
 
<source lang="haskell">
depth :: Tree -> Int
depth Empty = 0
depth (Leaf n) = 1
depth (Node l r) = 1 + max (depth l) (depth r)
</source>
 
Thus, a <code>Tree</code> given to <code>depth</code> can be constructed using any of <code>Empty</code>, <code>Leaf</code> or <code>Node</code> and we must match for any of them respectively to deal with all cases. In case of <code>Node</code>, the pattern extracts the subtrees <code>l</code> and <code>r</code> for further processing.
 
Algebraic data types are particularly well-suited to the implementation of [[abstract syntax]].  For instance, the following algebraic data type describes a simple language representing numerical expressions:
 
<source lang="haskell">
data Expression = Number Int
                | Add Expression Expression
                | Minus Expression
                | Mult Expression Expression
                | Divide Expression Expression
</source>
 
An element of such a data type would have a form such as <code>Mult (Add (Number 4) (Minus (Number 1))) (Number 2)</code>.
 
Writing an evaluation function for this language is a simple exercise; however, more complex transformations also become feasible.  For instance, an optimization pass in a compiler might be written as a function taking an abstract expression as input and returning an optimized form.
 
==Explanation==
What is happening is that we have a datatype, which can be “one of several types of things.” Each “type of thing” is associated with an identifier called a ''constructor'', which can be thought of as a kind of tag for that kind of data. Each constructor can carry with it a different type of data. A constructor could carry no data at all (e.g. "Empty" in the example above), carry one piece of data (e.g. “Leaf” has one Int value), or multiple pieces of data (e.g. “Node” has two Tree values).
 
When we want to do something with a value of this Tree algebraic data type, we ''deconstruct'' it using a process known as ''pattern matching.'' It involves ''matching'' the data with a series of ''patterns''. The example function "depth" above pattern-matches its argument with three patterns. When the function is called, it finds the first pattern that matches its argument, performs any variable bindings that are found in the pattern, and evaluates the expression corresponding to the pattern.
 
Each pattern has a form that resembles the structure of some possible value of this datatype. The first pattern above simply matches values of the constructor Empty. The second pattern above matches values of the constructor Leaf. Patterns are recursive, so then the data that is associated with that constructor is matched with the pattern "n". In this case, a lowercase identifier represents a pattern that matches any value, which then is bound to a variable of that name — in this case, a variable “<code>n</code>” is bound to the integer value stored in the data type — to be used in the expression to be evaluated.
 
The recursion in patterns in this example are trivial, but a possible more complex recursive pattern would be something like <tt>Node (Node (Leaf 4) x) (Node y (Node Empty z))</tt>. Recursive patterns several layers deep are used for example in balancing [[red-black tree]]s, which involve cases that require looking at colors several layers deep.
 
The example above is operationally equivalent to the following pseudocode:
 
switch on (data.constructor)
  case Empty:
    return 0
  case Leaf:
    let n = data.field1
    return 1
  case Node:
    let l = data.field1
    let r = data.field2
    return 1 + max (depth l) (depth r)
 
The comparison of this with pattern matching will point out some of the advantages of algebraic data types and pattern matching. First is [[type safety]]. The pseudocode above relies on the diligence of the programmer to not access <tt>field2</tt> when the constructor is a Leaf, for example. Also, the type of <tt>field1</tt> is different for Leaf and Node (for Leaf it is <tt>Int</tt>; for Node it is <tt>Tree</tt>), so the type system would have difficulties assigning a static type to it in a safe way in a traditional [[record (computer science)|record]] data structure. However, in pattern matching, the type of each extracted value is checked based on the types declared by the relevant constructor, and how many values you can extract is known based on the constructor, so it does not face these problems.
 
Second, in pattern matching, the compiler statically checks that all cases are handled. If one of the cases of the “depth” function above were missing, the compiler would issue a warning, indicating that a case is not handled. This task may seem easy for the simple patterns above, but with many complicated recursive patterns, the task becomes difficult for the average human (or compiler, if it has to check arbitrary nested if-else constructs) to handle. Similarly, there may be patterns which never match (i.e. it is already covered by previous patterns), and the compiler can also check and issue warnings for these, as they may indicate an error in reasoning.
 
Do not confuse these patterns with [[regular expression]] patterns used in string pattern matching. The purpose is similar — to check whether a piece of data matches certain constraints, and if so, extract relevant parts of it for processing — but the mechanism is very different. This kind of pattern matching on algebraic data types matches on the structural properties of an object rather than on the character sequence of strings.
 
==Theory==
{{main|Recursive data type}}
 
A general algebraic data type is a possibly recursive [[sum type]] of [[product type]]s. Each constructor tags a product type to separate it from others, or if there is only one constructor, the data type is a product type. Further, the parameter types of a constructor are the factors of the product type. A parameterless constructor corresponds to the [[empty product]]. If a datatype is recursive, the entire sum of products is wrapped in a [[recursive type]], and each constructor also rolls the datatype into the recursive type.
 
For example, the Haskell datatype:
 
<source lang="haskell">
data List a = Nil | Cons a (List a)
</source>
 
is represented in [[type theory]] as
<math>\lambda \alpha. \mu \beta. 1 + \alpha \times \beta</math>
with constructors <math>\mathrm{nil}_\alpha = \mathrm{roll}\ (\mathrm{inl}\ \langle\rangle)</math> and <math>\mathrm{cons}_\alpha\ x\ l = \mathrm{roll}\ (\mathrm{inr}\ \langle x, l\rangle)</math>.
 
The Haskell List datatype can also be represented in type theory in a slightly different form, as follows:
<math>\mu \phi. \lambda \alpha. 1 + \alpha \times \phi\ \alpha</math>.
(Note how the <math>\mu</math> and <math>\lambda</math> constructs are reversed relative to the original.) The original formation specified a type function whose body was a recursive type; the revised version specifies a recursive function on types. (We use the type variable <math>\phi</math> to suggest a function rather than a "base type" like <math>\beta</math>, since <math>\phi</math> is like a Greek "f".) Note that we must also now apply the function <math>\phi</math> to its argument type <math>\alpha</math> in the body of the type.
 
For the purposes of the List example, these two formulations are not significantly different; but the second form allows one to express so-called [[nested data type]]s, i.e., those where the recursive type differs parametrically from the original. (For more information on nested data types, see the works of [[Richard Bird (computer scientist)|Richard Bird]], [[Lambert Meertens]] and Ross Paterson.)
 
In [[set theory]] the equivalent of a sum type is a [[disjoint union]] – a set whose elements are pairs consisting of a tag (equivalent to a constructor) and an object of a type corresponding to the tag (equivalent to the constructor arguments).
 
==Programming languages with algebraic data types==
The following programming languages have algebraic data types as a first class notion:
* [[Clean (programming language)|Clean]]
* [[F Sharp (programming language)|F#]]
* [[Haskell (programming language)|Haskell]]
* [[haXe]]
* [[Hope (programming language)|Hope]]
* [[Language Of Temporal Ordering Specification|LOTOS]]
* [[Mercury (programming language)|Mercury]]
* [[Miranda (programming language)|Miranda]]
* [[Nemerle]]
* [[OCaml]]
* [[Opa (programming language)|Opa]]
* [[Racket (programming language)|Racket]]
* [[Rust (programming language)|Rust]]
* [[Scala (programming language)|Scala]]
* [[Standard ML]]
* [[Tom (pattern matching language)|Tom]]
* [[Visual Prolog]]
 
==See also==
* [[Tagged union]]
* [[Disjoint union]]
* [[Type theory]]
* [[Generalized algebraic data type]]
* [[Initial algebra]]
 
==References==
<references />
 
{{FOLDOC|algebraic+data+type|algebraic data type}}
 
{{data types}}
 
[[Category:Functional programming]]
[[Category:Type theory]]
[[Category:Data types]]
[[Category:Articles with example Haskell code]]
[[Category:Composite data types]]

Latest revision as of 13:25, 12 January 2015


Adrianne is what you has the capability to call me but I just don't like when the public use my full address. What I love doing are to play croquet now I have your time to take on new things. The job I've been occupying for many years is an rule clerk. My husband and I decided upon to reside in Guam but I will attain to move in per year or two. You can sometimes find my website here: http://prometeu.net

Here is my blog; clash of clans hack android - prometeu.net,