Algebraic
Data Type
in Swift ɹɹɹɹɹɹɹɹɹ
2018/01/13 Tokyo iOS Meetup
Yasuhiro Inami / @inamiy
Slide 2
Slide 2 text
No content
Slide 3
Slide 3 text
Types in Swift
Bool, Int, Int(N), UInt(N), Float, Double, String,
Optional, Array, Set, Dictionary,
etc...
Value types can be expressed using struct and enum.
struct Pair { enum Either {
let a: A case a(A)
let b: B case b(B)
} }
Slide 4
Slide 4 text
Algebraic Data Type (ADT)
• Mathematics (algebra) just works in types
• Algebra = study of mathematical symbols and the rules
• e.g.
• Abstract Algebra = study of "algebraic structures"
• e.g. Magma, Semigroup, Monoid, Group, Ring, etc
• Data and rules inside the data set
• e.g. , ɹfor (Int, 0, )
Slide 5
Slide 5 text
Question 1 ✨"✨
struct Pair {
let a: A
let b: B
}
Are `Pair` and `Pair` the "same type"?
Slide 6
Slide 6 text
Pair == Pair ?
• No, they aren't the same ! (compiler distinguishes two)
• But internal data types are almost identical
• Only memory layout and function names (initializer,
getter) are different
• They are (categorically) isomorphic
• Pair Pair
• Mutual transformation exists
Slide 7
Slide 7 text
Isomorphic
Two objects cannot be
distinguished
↓
There exists an "arrow"
and also a unique
"inverse arrow"
( 1:1 correspondence)
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
/// From `Pair` to `Pair`
func swapAB(pair: Pair) -> Pair {
return Pair(a: pair.b, b: pair.a)
}
/// From `Pair` to `Pair` (inverse)
func swapBA(pair: Pair) -> Pair {
return Pair(a: pair.b, b: pair.a)
}
(Note: `swapAB` and `swapBA` are identical)
Slide 10
Slide 10 text
Question 2 ✨"✨
struct Pair {
let a: A
let b: B
}
struct A {}
Are these two "isomorphic"?
Slide 11
Slide 11 text
struct Pair {
let a: A
let b: B
}
struct A {}
! Making `Pair` from `A` seems impossible, so they
must be different...
! But wait! `Pair` can be isomorphic to `A`!
Question 3 ✨"✨
enum Either {
case a(A)
case b(B)
}
Are `Either` and `Either` "isomorphic"?
Slide 17
Slide 17 text
/// 1. From `Either` to `Either`
/// 2. From `Either` to `Either` (inverse)
func swap(either: Either) -> Either {
switch either {
case let .a(a): return .b(a)
case let .b(b): return .a(b)
}
}
! `Either` and `Either` are "isomorphic" "
Slide 18
Slide 18 text
Question 4 ✨"✨
enum Either {
case a(A)
case b(B)
}
struct A {}
Are these two "isomorphic"?
Slide 19
Slide 19 text
enum Either {
case a(A)
case b(B)
}
struct A {}
! Now, it's easy to make `Either` from `A`, but
opposite seems difficult...
! But wait! `Either` can be isomorphic to `A`!
Characteristics of structand enum
• struct ❤ Void
• enum ❤ Never
• " "What do we mean by ❤?"
• Let's think categorically algebraically
Slide 24
Slide 24 text
Think algebraically
• struct is a product type
• `struct Pair` 㱻
• `Void` 㱻
• enum is a sum type
• `enum Either` 㱻
• `Never` 㱻
Slide 25
Slide 25 text
• Pair A
•
• Either A
•
`Void` is `1` and `Never` is `0`
in type-system world, and
we can add (enum) and
multiply (struct) just like
elementary algebra ! " !
Slide 26
Slide 26 text
Think more algebraically
Elementary algebra:
• Associativity: ɹ( = )
• Distributivity:
In type system:
• ((A, B), C) (A, (B, C)) (A, B, C)
• (A, Either) Either<(A, B), (A, C)>
(continued1)
1 The Algebra of Algebraic Data Types, Part 2 - Chris Taylor
Slide 31
Slide 31 text
Function as Exponential Object
• `A -> B` can be expressed as
• (Never -> A) Voidɹ㱻ɹ
• (A -> Never) Neverɹ㱻ɹ ɹ( )
• (Void -> A) Aɹ㱻ɹ
• (A -> Void) Voidɹ㱻ɹ
Slide 32
Slide 32 text
Exponent Laws
•
• (C -> B -> A) ((B, C) -> A)ɹ/* currying ! */
•
• (Either -> A) (B -> A, C -> A)
•
• (C -> (A, B)) (C -> A, C -> B)
Slide 33
Slide 33 text
Algebraic Data Type
=
and for types!
Slide 34
Slide 34 text
Appendix 1: Initial Algebra
ADT as fixed point of endofunctor (initial algebra):
e.g.
Slide 35
Slide 35 text
Appendix 2:
Derivative of ADT
...2 trees below the hole, and a list of
"above node value (A)", "left or right
side of the node subtree (Bool = 2)",
"node subtree (Tree)".
Slide 36
Slide 36 text
References
• The Algebra of Algebraic Data Types, Part 1 - Chris Taylor
• The algebra (and calculus!) of algebraic data types
• Understanding F-Algebras | Bartosz Milewski's
Programming Cafe
• ݍษڧձ ୈ7ճ @ ϫʔΫεΞϓϦέʔγϣϯζ
• Steve Awodey, Category Theory (2010)