Ubiquitous Types: Introduction to Algebraic Data Types
Tags: typescript, javascript, ddd, algebraic
Published: 2021-12-22 18:23:13

> Ubiquitous: present, appearing, or found everywhere.

most of the time we as humans are able to recognize and label things in the same way “most probably”. the small example about our ability to do that recently is the most annoying fraud checking system “reCAPTCHA” yeah, the one that’s always asking you to select and label some kind of type in every action we usually do these days on the internet to validate you’re a human.

the model of reCAPTCHA is so easy it just showing to you a type and asks you to select this type from the recommended images.

So labeling things is something we naturally do, we are used to labeling things around us in a way that makes them distinguishable.

 

the same process happens with programming as well. everytime you think and write a program you almost try to find the similarities between things then you have to label, composing those similarities.

in the Functional programming universe, everything is around `functions`. the function is simple it's just a small kind of box that accepts input and produces output. input -> output.


![Diagram represent the function process. in the Diagram there are 3 boxes in the same line sequence. the 1st one shows the function input then the 2nd one shows the function the the 3rd one point of to the function output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ls8eyi9zpc9js3h46b51.png)

the (input -> output) description is called type signature. the type signature is just the definition of the function input and output.

```jsx
// Signature -> multiplay2 :: Number -> Number
// Which Means that the multiplay2 function takes and 
// expect to pass parameter(input) from type Number and
// produce(output a value from type Number.
const multiplay2 = x => x * 2;

// Signature -> sum:: (Number, Number) -> Number
// Which Means that the sum function takes and expect to
// pass 2 parameter(input) from type Number
// and produce(output) a value from type Number.
const sum = (x, y) => x + y;
```

So we could define type as:

> it’s just a given label or name for some values that could be used as input or output for function.


the insight and understanding of the function from just reading a type signature are powerful. being able to express, recognize the function itself without going into the function details that’s one of the powerful things that type signature provides.

so the type is just a name for any kind of thing that could be used as input or output for functions.

## Algebraic Data Types

Wikipedia's definition: 

> an **algebraic data type** is a kind of composite type,  i.e., a type formed by combining other types.


So basically the algebraic data type is just a composition of types. composition means putting things together so you can combine a couple of things to make a bigger thing.

the common types in algebraic data types are “**Product type**” which basically represented by “**AND**ing” things together and “**Sum type**”  which basically represented by “**OR**ing” things together.

### Product type:

Product type is a compound for other types. and shortly speaking Product type is just like the “AND” operator. you need every type to make a new one.

```tsx
type FruitSalad = {
   apple: AppleKinds,
   banana: BananaKinds,
   orange: OrangeKinds
}
```


![Diagram for product type shows the function sequence started from the function input to the function and on the end the function output. the function input is represented by showing 3 boxes, 1st box has 2 apple, 2nd one has 2 banana, 3rd one has 2 orange. then the function output is 8 fruit salad. diagram shows the type signature "type FruitSalad = Apple AND Banana AND Orange". the equation for this is "2 apple * 2 banana * 2 Orange = 8 fruit salad"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfseowvijouvs263si2d.png)

The naming Product and equation comes from mathematics, type theory, category theory,  [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product)

### Sum type:

Sum type is a type where your value must be one of the choices types. and shortly speaking Sum type is just like the “OR” operator. you need either this or that type, not both. 

```tsx
type FruitSnack = Apple | Banana | Orange
```


![Diagram for sum type shows the function sequence started from the function input to the function and on the end the function output. the function input is represented by showing 3 boxes, 1st box has 2 apple, 2nd one has 2 banana, 3rd one has 2 orange. then the function output is 6 fruit snack. diagram shows the type signature "type FruitSalad = Apple OR Banana OR Orange". the equation for this is "2 apple + 2 banana + 2 Orange = 6 fruit snack"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8nkdh7tgk6uz3oba8l0.png)


The naming Sum and equation comes from mathematics, type theory, category theory, [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union)

if you’re working with a strongly typed language you will find yourself dealing with composing types and algebraic data types as well.


 below is a naive example of composed types:

```tsx
type Amount = number | string;
type Currency = 'USD' | 'EUR';
type CardType = 'Visa' | 'MasterCard';
type CardNumber = number;

type CreditCardInfo = {
  CardType: CardType,
  CardNumber: CardNumber
}

type Payment = {
  Amount: Amount,
  Currency: Currency,
  CreditCardInfo: CreditCardInfo
}
```

the main points from this article are to just simplify the concept of Product, Sum types. not meant to implement functions and models around both types might in next articles to write more about how to use both to build types model that works with domains driven.

note: usually the elimination of choices in Sum type needs to implement a matching pattern to eliminate the choices.



### Recap:

- type signature is just the definition of the function input and output.
- algebraic data type is just a composition of types.
- the types that are built using AND are called Product types.
- the types that are built using Or are called Sum types.