47
Categories for the Working Hacker Philip Wadler University of Edinburgh & IOHK Lambda Days 22 February 2018

Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Categories for theWorking Hacker

Philip WadlerUniversity of Edinburgh & IOHK

Lambda Days22 February 2018

Page 2: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Smart ContractsSimplicity

Michelson

Plutus

Page 3: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Categories

Page 4: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 5: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 6: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 7: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Products

Page 8: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 9: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 10: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 11: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 12: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 13: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 14: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 15: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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 16: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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 17: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Products in Haskell

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

Page 18: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Products in Haskell

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

one :: Intone = fst pair

two :: Stringtwo = snd pair

Page 19: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Sums

Page 20: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 21: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 22: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 23: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 24: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 25: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 26: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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 27: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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 28: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Sums in Haskell

data Sum a b = Left a | Right b

Page 29: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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 30: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Exponentials

Page 31: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 32: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 33: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 34: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 35: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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 36: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Exponentials in Haskell

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

incr :: Int -> Intincr = add 1

three :: Intthree = incr 2

Page 37: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Exponentials in Haskell

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

incr :: Int -> Intincr = add 1

three :: Intthree = incr 2

Page 38: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Simplicity & Michelson

Page 39: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Simplicity - Types

Page 40: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Simplicity - Semantics

Page 41: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Michelson - Sums

Page 42: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Plutus

IOHK is hiring 6programming language

engineers

https://iohk.io/careers/#op-235152-functional-compiler-engineer-

Page 43: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

Conclusions

Page 44: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 45: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 46: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University
Page 47: Categories for the Working Hacker - University of Edinburghhomepages.inf.ed.ac.uk/wadler/papers/categories/krakow.pdf · Categories for the Working Hacker Philip Wadler University

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