categories for the working hacker - university of edinburgh · • saunders maclane, categories for...
TRANSCRIPT
![Page 1: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/2.jpg)
![Page 3: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/3.jpg)
![Page 4: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/4.jpg)
![Page 5: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/5.jpg)
![Page 6: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/6.jpg)
![Page 7: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/7.jpg)
![Page 8: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/8.jpg)
![Page 9: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/9.jpg)
![Page 10: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/10.jpg)
![Page 11: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/11.jpg)
![Page 12: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/16.jpg)
![Page 17: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/17.jpg)
![Page 18: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/18.jpg)
![Page 19: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/19.jpg)
![Page 20: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/20.jpg)
![Page 21: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/21.jpg)
![Page 22: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/26.jpg)
![Page 27: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/27.jpg)
![Page 28: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/28.jpg)
![Page 29: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/29.jpg)
![Page 30: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/33.jpg)
![Page 34: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/34.jpg)
![Page 35: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/35.jpg)
![Page 36: Categories for the Working Hacker - University of Edinburgh · • Saunders MacLane, Categories for the Working Mathematician • Benjamin Pierce, Basic Category Theory for Computer](https://reader034.vdocuments.site/reader034/viewer/2022042405/5f1d3f195d580f0e85688292/html5/thumbnails/36.jpg)
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