36
Categories for the Working Hacker Philip Wadler University of Edinburgh QCon SF, 15 Nov 2017

Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Categories for theWorking Hacker

Philip WadlerUniversity of EdinburghQCon SF, 15 Nov 2017

Page 2: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 3: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 4: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 5: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 6: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 7: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 8: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 9: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 10: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 11: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 12: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Products in Javapublic class Product<A,B> { private A fst; private B snd; public Product(A fst, B snd) { this.fst = fst; this.snd = snd; } public A getFst() { return this.fst; } public B getSnd() { return this.snd; }}

Page 13: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Products in Java

public class Test { public Product<Integer,String> pair = new Product(1, “two”); public Integer one = pair.getFst(); public String two = pair.getSnd();}

Page 14: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Products in Haskell

data Product a b = Pair { fst :: a, snd :: b }

Page 15: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Products in Haskell

pair :: Product Int Stringpair = Pair 1 “two”

one :: Intone = fst pair

two :: Stringtwo = snd pair

Page 16: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 17: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 18: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 19: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 20: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 21: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 22: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Sums in Javapublic interface Sum<A,B> { public <C> C caseExpr(Function<A,C> f, Function<B,C> g);}public class Left<A,B> implements Sum<A,B> { private A x; public Left(A x) { this.x = x; } public <C> C caseExpr(Function<A,C> f, Function<B,C> g) { return f.apply(x); }}public class Right<A,B> implements Sum<A,B> { private B y; public Right(B y) { this.y = y; } public <C> C caseExpr(Function<A,C> f, Function<B,C> g) { return g.apply(y); }}

Page 23: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Sums in Java

public class ErrInt extends Sum<String,Integer> { public ErrInt err = new Left(“error”); public ErrInt one = new Right(1); public ErrInt add(ErrInt that) { return this.caseExpr( e -> new Left(e), m -> that.caseExpr( e -> new Left(e), n -> new Right(m+n) ) ); public ErrInt test = one.add(err);}

Page 24: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Sums in Haskell

data Sum a b = Left a | Right b

Page 25: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Sums in Haskell

type ErrInt = Sum String Int

err = Left “error”one = Right 1

add :: ErrInt -> ErrInt -> ErrIntadd (Left e) that = Left eadd this (Left e) = Left eadd (Right m) (Right n) = Right (m+n)

test = add one err

Page 26: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 27: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 28: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 29: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 30: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Exponentials in Java

public class Test { public Function<Integer,Integer> add (Integer n) { return x -> x + n; } public Function<Integer,Integer> incr = add(1); public Integer three = incr.apply(2);}

Page 31: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Exponentials in Haskell

add :: Int -> (Int -> Int)add n = \x -> n + x

incr :: Int -> Intincr = add 1

three :: Intthree = incr 2

Page 32: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Exponentials in Haskell

add :: Int -> Int -> Intadd n x = n + x

incr :: Int -> Intincr = add 1

three :: Intthree = incr 2

Page 33: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 34: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 35: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer
Page 36: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer

Further Reading

• Saunders MacLane, Categories for the Working Mathematician

• Benjamin Pierce, Basic Category Theory for Computer Scientists

• Bartosz Milewski, Programming Cafe (blog) Bartosz Milewski's Programming Cafe