Tagged Union Article Index for
Tagged
Website Links For
Union
 

Information About

Tagged Union




Tagged unions are most important in functional languages such as ML and Haskell , where they are called datatypes (see Algebraic Data Type ) and the compiler is able to verify that all cases of a tagged union are always handled, avoiding many types of errors. They can, however, be constructed in nearly any language, and are much safer than untagged unions, often simply called unions, which are similar but do not explicitly keep track of which member of the union is currently in use.

Tagged unions are often accompanied by the concept of a constructor, which is similar but not the same as a Constructor for a class. Constructors produce a tagged union value, given the initial tag value and a value of the corresponding type.

Mathematically, tagged unions correspond to '' Disjoint '' or ''discriminated unions'', usually written using +. Given an element of a disjoint union ''A'' + ''B'', it is possible to determine whether it came from ''A'' or ''B''. If an element lies in both, there will be two effectively distinct copies of the value in ''A'' + ''B'', one from ''A'' and one from ''B''.

In Type Theory , a tagged union is called a ''sum type''. Notations vary, but usually the sum type A+B comes with two introduction forms \mathrm{inj}_1 : A o A+B and \mathrm{inj}_2 : B o A+B. The elimination form is case analysis: if e has type A+B and e_1 and e_2 have type au under the assumptions x:A and y:B respectively, then the term