Slide 1

Slide 1 text

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

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 10

Slide 10 text

Slide 15

Slide 15 text

Slide 16

Slide 16 text

Slide 18

Slide 18 text

Slide 22

Slide 22 text

Slide 23

Slide 23 text

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 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)>

Slide 27

Slide 27 text

Slide 28

Slide 28 text

Slide 29

Slide 29 text

Slide 30

Slide 30 text

(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)

Slide 37

Slide 37 text

Thanks! Yasuhiro Inami @inamiy