lambdas and cartesian closed categories

Upload: israel-perez

Post on 07-Aug-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    1/55

    Lambdas And CartesianClosed Categories

    A Tribute To Joachim Lambek5 December 1922 – 23 June 2014

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    2/55

    I’m Erik and I amaddicted to readinglanguage specifications.

    I tried them

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    3/55

    Hack

    C#

    Dart

    VisuBasi

    Swi

    ECMA

    Script

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    4/55

    But there is one that iseven too strong for me.

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    5/55

    Java 8 Lambdas/Method References A method reference expression is compatible in an assignment context, invocation context, orcasting context with a target type T if T is a functional interface type (§9.8) and the expressionis congruent with the function type of the ground target type derived from T.

    The ground target type is derived from T as follows:

    If T is a wildcard-parameterized functional interface type, then the ground target type is thenon-wildcard parameterization (§9.9) of T.

    Otherwise, the ground target type is T.

    A method reference expression is congruent with a function type if both of the following are

    true:The function type identifies a single compile-time declaration corresponding to the reference.

    One of the following is true:

    The result of the function type is void.

    The result of the function type is R, and the result of applying capture conversion (§5.1.10) tothe return type of the invocation type (§15.12.2.6) of the chosen compile-time declaration is R'(where R is the target type that may be used to infer R'), and neither R nor R' is void, and R' is

    compatible with R in an assignment context.hci

    You have not read thlanguage specificatio

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    6/55

    Saint Leslie LamportTuring Award Winner 2014

    http://www.mariowiki.com/File:Crystalcoconut.gif

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    7/55

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    8/55

    “category theoryis the study ofthe general formof mathematicaltheories, withoutregard to theircontent”

    SolikPa

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    9/55

    Category = ProgrammingLanguage

    Object = Type

    Morphism = Staticmethod f(a:A): B orproperty f: B on A

    This is theproblem we aregoing to fix in thistalk.

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    10/55

    Category Theory== Interface-based Modelling

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    11/55

    Let’s Decode That Greek

    Let C be a category with some objectsX1 and X2.

    Let C be a programming language withsome types A and B.

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    12/55

    And this noise

    An object X is a product of X1 and X2, denoted X1×satisfies this universal property: there exist morphisms

    X⇒ X1, π2 : X⇒ X2 such that for every object Y and

    morphisms f 1 : Y⇒ X1, f 2 : Y⇒ X2 there exists a uniqumorphism f 1△ f 2 : Y⇒ X such that the following diagramcommutes:

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    13/55

    Commuting Diagram

    X1 × X2 X2X1

    Y

    π2 π1

    f 1 f 2f 1△ f 2

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    14/55

    Translate to Equations

    h = f1△ f 2 ⇔

    π1◦ h = f 1 && π2◦h = f 2

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    15/55

    Is simply this specification

    A type ( A,B) is a product of A and B, iff it satisfies thuniversal property: there exist properties _1 : A, _2(A,B)such that for every pair of methods f(c: C): A

    B there exists a factory method f△ g(c: C): (A,B) suthe following diagram commutes:

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    16/55

    Commuting Diagram

    (A,B) BA

    C

    _2 _1

    f g

    f△ g

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    17/55

    Translate to Equations

    h = f△ g⇔

    h(c)._1 = c.fh(c)._2 = c.g

    Category Theory is JustInterface-Based DesignWith a Little Bit of

    Additional Rigor

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    18/55

    Scala Products

    trait Product2[+T1, +T2] extends Product { abstract def _1: T1 abstract def _2: T2}

    object Product2 { abstract def (f: C ⇒ A△ g: C⇒ B)(c: C): Pro}

    Wishfulthinking :->

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    19/55

    “There exists” is all we have

    Given any two methodsf(c: C): A and g(c:C):B, we c

    define a new method f △ g(c(A,B) = (f(c), g(c)).

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    20/55

    Derived Functions, same deal

    Given any two methodsf(a: A):C and g(c:B):D, we c

    define a new method f×g(ab(A,B)): (C,D) = (f(ab._1), g _2)) .

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    21/55

    http://en.wikipedia.org/wiki/John_McCarthy_ (computer_scientist)#mediaviewer/File:

    John_McCarthy_Stanford.jpg

    I could define △ and × genericallyin 1960 ...

    Ha, ha, ha, I didit already in1928.

    http://www.learn-minfo/history/photos

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    22/55

    https://netbeans.org/images_www/articles/73/javaee/ecommerce/intro/duke.png

    We don’tneed nostinkin’delegates,we alreadyhave virtualmethods

    Don’t reallyunderstandany of thistheory shit,

    but we havedelegates,which aremuch better,of course.

    http://upload.wikimedia.org/wikipedia/commons/9/99/Elvis_Presley_proe_Rock.jpg

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    23/55

    Objects Represent RealWorld Objects

    Categories RepresentMathematical Objects

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    24/55

    Define not just interface,but also algebraicproperties required ofimplementation

    And don’t buaround withgrandiloquen

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    25/55

    http://www.computer.org/portal/image/image_gallery?uuid=b15467b5-5dfe-44d3-8663-

    Reality: A Cousintwice Removed

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    26/55

    I thought this talk wasabout Cartesian ClosedCategories. Cut the

    crap please!

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    27/55

    Young man, before youdive into the deep end,let me note that inSmalltalk we hadblocks since 1970.

    http://en.wikipedia.org/wiki/Facepalm

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    28/55

    Exponentials

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    29/55

    Lets decode the Greek

    Let C be a category with binary products and leY and Z be objects of C. The exponential obZY can be defined as a universal morphismfrom the functor –×Y to Z. (The functor –C to C maps objects X to X×Y and morphisto φ×id).

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    30/55

    Lets decode the Greek

    Let L be a language that supports tuples and letA and B be types of L. The function typecan be defined as a factory method from thefunctor –×A to B. (The functor –×A in L mtypes C to C×A and methods m to m×id).

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    31/55

    What Is A Functor?

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    32/55

    Lets decode the Greek

    Let C be a category. A functor F is a mappingassociates to each object X an object F(X),associates to each morphism f:X⇒ Y a morpF(f):F(X)⇒ F(Y) such that the following twoconditions hold: F(id) = id and F(g◦f) = F(gfor all morphisms f:X⇒ Y, and g:Y⇒ Z. That functors must preserve identity morphisms andcomposition of morphisms.

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    33/55

    Lets decode the Greek

    Let L be a language. A functor C[_] is a genetype that associates to each type A an instantype C[A], and has a method map(f: A⇒ B)such that for all cs: C[A] the following two

    conditions hold: cs.map(id) = cs and cs.map(a)) = cs.map(g).map(f) for all functions f:Band g:A⇒ B. That is, functors must preserveidentity and composition of functions.

    You have messeNo worries I am a dirty

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    34/55

    http://andrej.com/mathematicians/large/Lambek_Joachim.jpg

    You have messebit by silently infunctions alreadis the type we wtrying to define

    No worries, I am a dirtyhacker. Purity is sooooooverrated. A function is a typethat reifies methods.

    That’s whatexponentialsare for.

    Doh!

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    35/55

    Continue to Decode the Greek

    Explicitly, the definition is as follows. Anobject ZY, together with a morphism(ZY×Y)⇒ Z is an exponential object if fo

    any object X and morphism g: (X×Ythere is a unique morphism λg: X⇒such that the following diagram

    commutes:

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    36/55

    Commuting Diagram

    ZY×Y ZZY

    X×Y

    eval

    gλg×id

    X

    λg

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    37/55

    Translate to Equations

    g(a,b)=

    (eval◦λg×id)(a,b)=eval (λg(a), b)

    To be precise Erik,λg is the unique morphism thatmakes thisequation hold

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    38/55

    http://www.haskell.

    org/wikiupload/8/86/HaskellBCurry.

    You may alsorecognize λg as“currying” in Haskell, i.e.

    curry g a b = g(a,b)uncurry f(a,b) = f a b

    Which I invented in1958.

    Prod cts nd Objects

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    39/55

    Products and Objects

    http://www.cse.unt.edu/site/node/517

    As I explained in 1instance method is static method that t

    the this pointer as aadditional argumen

    Hence an instance metho

    (b:B): C on type A is smorphism from (A×B

    And did I alreadymention that C++11has lambdas?

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    40/55

    This is argument 0 to a method call

    The Java Virtual Machine uses local variables to passparameters on method invocation. On class methodinvocation, any parameters are passed in consecutive localvariables starting from local variable 0 . On instance minvocation, local variable 0 is always used to pass a

    reference to the object on which the instance method isbeing invoked (this in the Java programming language).

    Any parameters are subsequently passed in consecutivelocal variables starting from local variable 1.

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    41/55

    Continue to Decode the Greek

    Explicitly, the definition is as follows. Atype B⇒ C, together with a method a(b: B): Cis a function type if for any tA and method m(b: B):C on A there ifactory method (a: A)::m :B⇒ C such (C)a::m.apply(b)=(C)a.m(b).

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    42/55

    Translate to Equations

    a::m.apply(b)=a.m(b)

    Dude, that

    is what Isaid all thetime!

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    43/55

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    44/55

    Hom-set?

    Hom(A,B) ={ m| m ∈ C

    &⇒

    }

    Just a fancy way tosay “all methods m(a: A): B”

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    45/55

    The Magic Of Method References

    Hom(A×B,C)≅ Hom(A,B⇒Instancemethods m(b:B):C on aninstance a: A

    Are isomorphic tolambda expressionsb->a::m(b)of type Function

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    46/55

    Unfinished Functor Business

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    47/55

    In our case

    Hom(A ×B,C) ≅ Hom(A ,B⇒ C)

    Hom(F[B,A ],C) ≅ Hom(A ,G[B,

    h d

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    48/55

    Show Me Some Code

    object F { def apply[A ,B](a: A , b: B): F[B,A ] = new F(a,b)}

    class F[B,A ](_1 : A , _2 : B) extends Pair[A, B](_1, _2) def map[C](f: A =>C): F[B,C] = F(f(_1),_2)}

    Sh M S C d

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    49/55

    object G { def apply[B,C](f: B=>C): G[B,C] = new G[B,C] { override def apply(b: B): C = f(b) }}

    trait G[ B,C] extends ( B=>C) { def map[C](f: C=>A ): G[B,A ] = G(b => f(apply(b)))}

    Show Me Some Code

    H k

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    50/55

    Homework

    Show that F[B,_] and G[B,_] are indeedfunctors .

    Show that F[B,_] and G[B,_] are adjointfunctors by defining implicit conversions eachdirection.

    M d A d Adj i

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    51/55

    Monads And Adjunctions

    Th d

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    52/55

    The monadthat arises fromF[B,_] and G[B,_] is theState Monad

    But that is atopic foranother talk

    http://www.cs.kent.ac.uk/people/staff/re

    Your Next Tattoo

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    53/55

    Your Next Tattoo

    eval(λm(a), b)

    a::m.apply(b)http://en wikipedia org/wik

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    54/55

    Method References

    Are Exponentials

    What keeps puzzling me

  • 8/20/2019 Lambdas And Cartesian Closed Categories

    55/55

    What keeps puzzling meis why did it take sodamn long before OOprogrammers made

    methods into first classobjects.

    http://blogs.vma/6a00d8341c530970b-pi

    Because I distracted

    them with Spring,and got stinking richdoing it ;-)