Keith Pinson
I am a software developer who is getting into speaking.
A construct for enforcing the logic of a system at compile-time
Something you should care about
This is my sister.
She is creative and intelligent.
She hates math.
(...and she has a boyfriend already. Sorry.)
...then this talk is for you
Translate this into a data type:
"An entree has a sauce"
public class Entree
{
public string Sauce { get; set; }
}
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; }
}
Not Boolean
Propositions are logical
Created a type to express a proposition
Saying "and" for types is indispensable
What about "or"? Would that be useful?
Make "no value" explicit instead of...
null
throw
bool TryGetValue(A, out B)
Clearly represent known finite possibilities
Multiply | 4 | 5 | 6 |
---|---|---|---|
2 | 8 | 10 | 12 |
3 | 12 | 15 | 18 |
Add | 4 | 5 | 6 |
---|---|---|---|
2 | 6 | 7 | 8 |
3 | 7 | 8 | 9 |
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 |
Operation | x | y | z |
---|---|---|---|
a | a op x | a op y | a op z |
b | b op x | b op y | b op z |
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?
Pasta = Spaghetti or Tortellini or Gnocchi
Sauce = Marinara or Alfredo
Entree = Pasta and Sauce
Spaghetti | Tortellini | Gnocchi | |
---|---|---|---|
Marinara | Spaghetti with Marinara | Tortellini with Marinara | Gnocchi with Marinara |
Alfredo | Spaghetti with Alfredo | Tortellini with Alfredo | Gnocchi with Alfredo |
type Pasta = Spaghetti | Tortellini | Gnocchi
type Sauce = Marinara | Alfredo
type Entree = (Pasta, Sauce)
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
type Entree = (Pasta, Sauce)
Entree has 2 x 3 = 6 possible values
"And" types behave like multiplication!
type ParseResult =
| Success of Token * string
| Failure of Error
type Maybe<'a> = Nothing | Just of 'a
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
Compiler can check for logic errors
Auto-gen constructors, conversions, equality...
Algebraic types are computable types
Tell the computer and the programmer the same story
Make illegal states unrepresentable
Logic is inherent in the domains we model
This logic is algebraic
ADTs naturally fit the shape of the domain
By Keith Pinson
A hand-waving introduction to how data types can be mathematical. This is the version I presented as an AfterDark at CodeMash 2016.
I am a software developer who is getting into speaking.