ArticleslgStudy

science

Tagged union

Tagged union is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Tagged union rather than just read about it. In short: In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type, or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which type is in use.

Tagged union — main illustration
Tagged union — illustration

Key takeaways

  • Tagged union belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Tagged union to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tagged union from memory before moving on to harder problems.

Reference excerpt

In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type, or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which type is in use. It can be thought of as a type that has several "cases", each of which should be handled correctly when that type is manipulated. This is critical in defining recursive datatypes, in which some component of a value may have the same type as that value, for example in defining a type for representing trees, where it is necessary to distinguish multi-node subtrees and leaves. Like ordinary unions, tagged unions can save storage by overlapping storage areas for each type, since only one is in use at a time.

Description Tagged unions are most important in functional programming languages such as ML and Haskell, where they are called datatypes (see algebraic data type) and the compiler can verify that all cases of a tagged union are always handled, avoiding many types of errors. Compile-time checked sum types are also extensively used in Rust, where they are called enum. They can, however, be constructed in nearly any programming language, and are much safer than untagged unions, often simply called unions, which are similar but do not explicitly track which member of a 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. A constructor is a function or an expression that produces a value of the tagged union type, given a tag 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 {\displaystyle A+B} , it is possible to determine whether it came from A {\displaystyle A} or B {\displaystyle B} . If an element lies in both, there will be two effectively distinct copies of the value in A + B {\displaystyle A+B} , one from A {\displaystyle A} and one from B {\displaystyle B} . In type theory, a tagged union is called a sum type. Sum types are the dual of product types. Notations vary, but usually the sum type A + B {\displaystyle A+B} comes with two introduction forms (injections) inj 1 : A ↦ A + B {\displaystyle {\texttt {inj}}_{1}:A\mapsto A+B} and inj 2 : B ↦ A + B {\displaystyle {\texttt {inj}}_{2}:B\mapsto A+B} . The elimination form is case analysis, known as pattern matching in ML-style languages: if e {\displaystyle e} has type A + B {\displaystyle A+B} and e 1 {\displaystyle e_{1}} and e 2 {\displaystyle e_{2}} have type τ {\displaystyle \tau } under the assumptions x : A {\displaystyle x:A} and y : B {\displaystyle y:B} respectively, then the term c a s e e o f x ⇒ e 1 ∣ y ⇒ e 2 {\displaystyle {\mathsf {case}}\ e\ {\mathsf {of}}\ x\Rightarrow e_{1}\mid y\Rightarrow e_{2}} has type τ {\displaystyle \tau } . The sum type corresponds to intuitionistic logical disjunction under the Curry–Howard correspondence. An enumerated type can be seen as a degenerate case: a tagged union of unit types. It corresponds to a set of nullary constructors and may be implemented as a simple tag variable, since it holds no additional data besides the value of the tag. Many programming techniques and data structures, including rope, lazy evaluation, class hierarchy (see below), arbitrary-precision arithmetic, CDR coding, the indirection bit, and other kinds of tagged pointers, are usually implemented using some sort of tagged union. A tagged union can be seen as the simplest kind of self-describing data format. The tag of the tagged union can be seen as the simplest kind of metadata. In languages with flow-sensitive typing, tagged unions can be implemented by a combination of union types and record types.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Tagged union

Start with the simplest possible case. Write down what Tagged union claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Tagged union before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Tagged union ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Tagged union

In research
Tagged union appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Tagged union in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Tagged union is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Tagged union outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Tagged union” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Tagged union in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tagged union means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Tagged union out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tagged union in simple terms?

In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type, or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time…

Why does Tagged union matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Tagged union?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Tagged union.

Tags

  • Data types
  • Type theory

Keep exploring