What is an Algebraic Data Type?

A construct for enforcing the logic of a system at compile-time

Something you should care about

Sorry math nerds. Hand-waving to follow.

What good are mathy types?

Statement from Domain as a Type

Translate this into a data type:

"An entree has a sauce"

public class Entree
{
    public string Sauce { get; set; }
}

Statement with "and" as a type

Translate this into a data type:

"An entree consists of pasta and sauce"

public class Entree
{
    public string Pasta { get; set; }
    public string Sauce { get; set; }
}

We can put logic in a type

Not Boolean

Propositions are logical

Created a type to express a proposition

Type level AND...and OR?

Saying "and" for types is indispensable

What about "or"? Would that be useful?

OR could be useful to...

Make "no value" explicit instead of...

null

throw

bool TryGetValue(A, out B)

Clearly represent known finite possibilities

So logical types are useful. But are they algebraic?

We all agree this is math

2 \times 3 = 6
2×3=62 \times 3 = 6
2x = 6
2x=62x = 6
xy = z
xy=zxy = z

Remember multiplication tables?

Multiply 4 5 6
2 8 10 12
3 12 15 18

Or we could do addition

Add 4 5 6
2 6 7 8
3 7 8 9

Now moving into algebra land...

Add x y z
a a + x a + y a + z
b b + x b + y b + z
Multiply x y z
a a * x
a * y
a * z
b b * x
b * y
b * z

Abstract away the operation...

Operation x y z
a a op x a op y a op z
b b op x b op y b op z

Pop Quiz

An Italian restaurant offers an entree of pasta and sauce. Pasta options are spaghetti, tortellini and gnocchi. Sauce options are marinara and alfredo. What are the entree options?

 

This is a logic problem

Pasta = Spaghetti or Tortellini or Gnocchi

 

Sauce = Marinara or Alfredo

 

Entree = Pasta and Sauce

This is an algebra problem

Spaghetti Tortellini Gnocchi
Marinara Spaghetti with Marinara Tortellini with Marinara Gnocchi with Marinara
Alfredo Spaghetti with Alfredo Tortellini with Alfredo Gnocchi with Alfredo

This is a problem for ADTs

type Pasta = Spaghetti | Tortellini | Gnocchi

type Sauce = Marinara | Alfredo

type Entree = (Pasta, Sauce)

Sum Types

type Pasta = Spaghetti | Tortellini | Gnocchi

Sauce has 1 + 1 = 2 possible values

"Or" types behave like addition!

type Sauce = Marinara | Alfredo

Pasta has 1 + 1 + 1 = 3 possible values

Product Types

type Entree = (Pasta, Sauce)

Entree has 2 x 3 = 6 possible values

"And" types behave like multiplication!

Algebraic types compose!

type ParseResult =
    | Success of Token * string
    | Failure of Error

Consider: one-line ADT in F#...

type Maybe<'a> = Nothing | Just of 'a

Equivalent* Makeshift

ADT in C#

public abstract class Maybe<a>
{
    public static class Tags
    {
        public const int Nothing = 0;
        public const int Just = 1;
    }
    internal class _Nothing : Du.Maybe<a>
    {
    }
    public class Just : Du.Maybe<a>
    {
        internal readonly a item;
        public a Item
        {
            get { return this.item; }
        }
        internal Just(a item)
        {
            this.item = item;
        }
    }
    internal static readonly Du.Maybe<a> _unique_Nothing = new Du.Maybe<a>._Nothing();
    public int Tag
    {
        get { return (!(this is Du.Maybe<a>.Just)) ? 0 : 1; }
    }
    public bool IsJust
    {
        get { return this is Du.Maybe<a>.Just; }
    }
    public static Du.Maybe<a> Nothing
    {
        get { return Du.Maybe<a>._unique_Nothing; }
    }
    public bool IsNothing
    {
        get { return this is Du.Maybe<a>._Nothing; }
    }
    internal Maybe()
    {
    }
    public static Du.Maybe<a> NewJust(a item)
    {
        return new Du.Maybe<a>.Just(item);
    }
}

*Lacks equality methods

*Lacks string conversion methods

Algebra is inherent in the domains we model

Make friends with the compiler

Tell computers and humans the same story

Algebraic types are computable types

Who let algebra get funky with my data types?

By Keith Pinson

Who let algebra get funky with my data types?

A hand-waving introduction to how data types can be mathematical. This is the version I will present at LambdaConf 2016.

  • 1,085