comonads applicative monads codemania

Upload: msausu

Post on 09-Oct-2015

73 views

Category:

Documents


3 download

DESCRIPTION

Comonads Talk

TRANSCRIPT

  • 5/19/2018 Comonads Applicative Monads Codemania

    1/69

    Comonads, Applicative Functors, Monads

    and other principled things

    Tony Morris

    April 29, 2014

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    2/69

    If youve ever googled any of these. . .

    You probably got an answer as sensible as this

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    3/69

    Goals

    Emphasis on the practical motivationsfor the specific

    structures.This is not about the details of concepts like monads.

    This is about the process of reasoning that leads to theirdiscovery.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    4/69

    Goals

    Emphasis on the practical motivationsfor the specific

    structures.This is not about the details of concepts like monads.

    This is about the process of reasoning that leads to theirdiscovery.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    5/69

    Goals

    Emphasis on the practical motivationsfor the specific

    structures.This is not about the details of concepts like monads.

    This is about the process of reasoning that leads to theirdiscovery.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    6/69

    Goals

    Nothing I tell you pertains to any specific programming language.

    Java

    Python

    JavaScript

    doesnt matter, it still applies

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    7/69

    Goals

    There is no emphasis on a specific type of programming.

    Functional

    DysfunctionalObject-disoriented

    Dynamically-typed

    Hacking it out like a drunk dog muffin

    its all the same

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    8/69

    Principled Things

    What do we mean by a principled thing?

    Principled reasoning gives rise to useful inferences.p

    p q

    q

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    9/69

    Principled Things

    What do we mean by a principled thing?

    Principled reasoning gives rise to useful inferences.p

    p q

    q

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    10/69

    Principled reasoning is already familiarusing Java/C# syntax

    enum Order { LT , EQ , GT }

    interface C o mp a re < A > {

    O rd er c om pa re ( A a1 , A a2 ) ;

    }

    We define this interface because

    We can produce data structures to satisfy the interface.

    We can define operations that function on all instances of theinterface.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    11/69

    Principled Reasoning

    Data structures such as

    integersstrings

    list of elements where the elements can be compared

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    12/69

    Principled Reasoning

    Operations such as

    List#sortTree#insert

    List#maximum

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    13/69

    Principled ThingsLaws

    We might also define constraints required of instances.

    For example

    ifcompare(x, y) == LTthen compare(y, x) == GT

    ifcompare(x, y) == EQthen compare(y, x) == EQ

    ifcompare(x, y) == GTthen compare(y, x) == LT

    We will call these laws. Laws enable reasoning on abstract code.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    14/69

    Summary

    a principled interface

    law-abiding instances

    derived operations

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    15/69

    Principled Reasoning for Practical Application

    We try to maximise instances and derived operations,however, these two objectives often trade against each other.

    For example, all things that can compare can also be testedfor equality, but not always the other way around1.

    Obtaining the best practical outcome requires carefulapplication of principled reasoning.

    1such as complex numbers

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    16/69

    Some boring syntax issues

    Java

    enum Order { LT , EQ , GT }

    interface C o mp a re < A > {

    O rd er c om pa re ( A a1 , A a2 ) ;

    }

    Haskell

    data O rd er = LT | EQ | GT

    class C o mp ar e a where

    compare :: a -> a -> Order

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    17/69

    MappableThe interface

    Java 8/C# with the addition of higher-kinded polymorphism

    interface M a pp a bl e < T > {

    T map ( F un cti on < A , B > f , T < A> a );

    }

    Haskell

    class M a pp ab le t wheremap :: ( a -> b ) -> t a -> t b

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    18/69

    MappableThe laws

    Identity

    x . map (z -> z ) == x

    map (\ z -> z ) x == x

    Composition

    x . ma p ( z - > f ( g (z ) )) == x . ma p ( g ). m ap ( f )

    map ( \ z - > f ( g z ) ) x = = map f (map g x )

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    19/69

    Mappable

    Instances of things that map2

    List []

    map :: ( a -> b ) -> [ a ] -> [ b ]

    Reader (e ->)

    map :: ( a -> b ) -> ( e -> a ) -> ( e -> b )

    There are an enormousnumber of instances.

    2map is called Select in C#/LINQ.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    20/69

    MappableThe derived operations

    Map a constant value

    mapC onsta nt :: Mappable t = > a -> t b -> t a

    mapC onsta nt a b = fmap (\ _ -> a ) b

    Map function application

    mapApply :: Mappable t = > t ( a -> b ) -> a -> t b

    mapApply f a = fmap (\ g -> g a ) f

    The set of derived operations is relatively small.

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    21/69

    MappableSummary

    The more common name for Mappable is a functor.

    We have seen:The interface for a functorThe laws that the functor instances must satisfyThe instances of the functor interfaceThe operations derived from functor

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    22/69

    ?

    Make sure we understand Mappable!

    M d

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    23/69

    MonadThe interface

    Java 8/C# with the addition of higher-kinded polymorphism

    interface M on ad < T > {

    T jo in ( T a );

    T unit ( X x );

    }

    Haskell

    c la s s M on a d t wherejoin :: t ( t a ) -> t a

    unit :: x -> t x

    M d

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    24/69

    Monad

    The monad interface has laws too.

    The monad interface has strictly stronger requirements thanfunctor.

    In other words, all structures that are monads, are alsofunctors.However, not all structures that are functors, are also monads.

    Therefore, there are fewer monad instances than functor

    instances.

    M d

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    25/69

    MonadThe instances

    But still a very largeamount

    List

    Reader ((->) e)

    State s

    Continuation r

    Maybe/Nullable

    Exception

    Writer w

    Free f

    M d

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    26/69

    MonadThe operations

    and lots of operations too

    sequence :: [t a] -> t [a]

    filterM :: (a -> t Bool) -> [a] -> t [a]

    findM :: (a -> t Bool) -> [a] -> Maybe [a]

    M d

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    27/69

    MonadSome mythbusting

    This is what monad is for.

    A lawful interface.

    Satisfied by lots of instances.

    Gives rise to lots of useful operations.

    Monad

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    28/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Monad

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    29/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Monad

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    30/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Monad

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    31/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Monad

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    32/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Monad

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    33/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Monad

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    34/69

    MonadSome mythbusting

    Monad

    for controlling side-effects.

    make my program impure.

    something blah somethingIO.blah blah in $SPECIFIC_PROGRAMMING_LANGUAGE.

    blah blah relating to $SPECIFIC_MONAD_INSTANCE.

    Monads Might Not Matter, so use Actors insteada

    Too much bullshizzles to continue enumerating.ayes, seriously, this is a thing.

    Comonad

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    35/69

    ComonadThe interface

    Java 8 with the addition of higher-kinded polymorphism

    interface C o mo n ad < T > {

    T < T > d u pl ic at e ( T a );

    X e xt ra ct ( T x );

    }

    Haskell

    class C o mo na d t whereduplicate :: t a -> t ( t a )

    extract :: t x -> x

    Comonad

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    36/69

    Comonad

    Like monad, comonad is

    Another interface, with laws, instances and operations.

    The coprefix denotes categorical dual.

    Like monad, is strictly stronger than functor.

    All comonads are functors.

    Applicative Functor

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    37/69

    Applicative FunctorThe interface

    Java 8/C# with the addition of higher-kinded polymorphism

    interface A p p li c a ti v e < T > {

    T a pp ly ( T < Fu nct io n < A , B > > f , T a );

    T unit ( X x );

    }

    Haskell

    class A p p li c a ti v e t whereapply :: t ( a -> b ) -> t a -> t b

    unit :: x -> t x

    Applicative Functor

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    38/69

    pp

    Well blimey mate. Guess what?

    Its just another interface, with laws, instances and operations.

    An applicative functor is

    strictly stronger than functor. All applicatives are functors.strictly weaker than monad. All monads are applicative.

    Lets take a step back

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    39/69

    p

    Summary

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    40/69

    y

    Monads, Comonads, Applicative Functors . . .

    All just the names of common interfaces.

    with many distinct and disparate instances.

    with many derived operations.

    Each making different trade-offs for differences in utility.

    Utility

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    41/69

    y

    When might I use any of these interfaces?

    The same reason we already use interfaces.

    Begin with a simple principle and exploit its diversity to abstractaway code repetition.

    Ubiquity

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    42/69

    If these interfaces are so useful, why arent they used everywhere?

    familiarityexpressibility

    Familiarity

    http://goforward/http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    43/69

    Identifying the pattern

    Turning a list of potentially null into a potentially null list

    a r g s ( l i s t )

    r es ul t = new L i s t ;

    f or ea ch el in li st

    if ( el == null )

    r e tu r n n ul l ;

    else

    r e s u l t . a d d ( e l ) ;

    return r e s u l t ;

    Familiarityf

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    44/69

    Identifying the pattern

    Applying a list of functions to a single value

    a rg s ( lis t , t )r es ul t = new L i s t ;

    f or ea ch el in li st

    r e s u l t . a d d ( e l ( t ) ) ;

    return r e s u l t ;

    FamiliarityId if i h

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    45/69

    Identifying the pattern

    These expressions share structure

    L is t ( M ay be Nu ll a ) - > M ay be Nu ll ( L is t a )List (( t - >) a ) -> ( t - >) ( List a )

    List ( m a ) -> m ( List a )

    Commonly called sequence.

    FamiliarityId tif i th tt i

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    46/69

    Identifying the pattern again

    Keep elements of a list matching a predicate with potential null

    a rg s ( p re d , l is t )

    r es ul t = new L i s t ;

    f or ea ch el in li sta ns = p re d ( el ) ;

    if ( an s == null )

    r e tu r n n ul l ;

    e ls e if ( a n s )

    r e s u l t . a d d ( e l ) ;

    return r e s u l t ;

    FamiliarityId tif i th tt i

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    47/69

    Identifying the pattern again

    Keep elements of a list matching a predicate with argument passing

    a rg s ( pre d , li st , t )

    r es ul t = new L i s t ;f or ea ch el in li st

    if ( p re d ( el , t ) )

    r e s u l t . a d d ( e l ) ;

    return r e s u l t ;

    FamiliarityIdentifying the pattern again

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    48/69

    Identifying the pattern again

    These expressions share structure

    ( a -> M ay be Nu ll Bool ) -> List a -> M ay be Nu ll ( List a )( a -> ( t ->) Bool ) -> List a -> ( t ->) ( List a )

    ( a -> m Bool ) -> List a -> m ( List a )

    Commonly called filter.

    FamiliarityIdentifying the pattern once again

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    49/69

    Identifying the pattern, once again

    Find the first element matching a predicate with potential null

    a rg s ( p re d , l is t )

    r es ul t = new L i s t ;

    f or ea ch el in li sta ns = p re d ( el ) ;

    if ( an s == null )

    r e tu r n n ul l ;

    e ls e if ( a n s )

    return a ;

    r e tu r n n ul l ;

    FamiliarityIdentifying the pattern once again

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    50/69

    Identifying the pattern, once again

    Find the first element matching a predicate with argument passing

    a rg s ( pre d , li st , t )

    f or ea ch el in li stans = pred ( el , t );

    if ( a n s )

    r e tu r n t ru e ;

    r e tu r n f al s e;

    FamiliarityIdentifying the pattern once again

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    51/69

    Identifying the pattern, once again

    These expressions share structure

    ( a -> M ay be Nu ll Bool ) -> List a -> M ay be Nu ll Bool( a -> ( t - >) Bool ) -> List a -> ( t - >) Bool

    ( a -> m Bool ) -> List a -> m Bool

    Commonly called find.

    FamiliarityIdentifying the pattern last time

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    52/69

    Identifying the pattern, last time

    Turn a list of lists into a list

    a r g s ( l i s t )

    r es ul t = new L i s t ;

    f or ea ch el in li st

    r e s u l t . a p p e n d ( e l ) ;

    return r e s u l t ;

    FamiliarityIdentifying the pattern, last time

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    53/69

    de t y g t e patte , ast t e

    Turn a potential null of potential null into a potential null

    a r g s ( v a l u e )

    if ( v a lu e == null )

    r e tu r n n ul l ;

    else

    return v a l u e . g e t ;

    FamiliarityIdentifying the pattern, last time

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    54/69

    y g p ,

    Apply to the argument, then apply to the argument

    a rg s (f , t )

    return f (t , t );

    FamiliarityIdentifying the pattern

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    55/69

    y g p

    These expressions share structure

    List ( List a ) -> List a

    M ay be Nu ll ( M ay be Nu ll a ) - > M ay be Nu ll a

    ( t - >) (( t - >) a ) -> ( t - >) a

    m ( m a ) -> m a

    Commonly called join.

    Type systems and Expressibility Limits

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    56/69

    Some type systems limit expression of abstraction.

    Java

    C#

    F#

    Type systems and Expressibility Limits

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    57/69

    These type systems are limited in the kinds of interfaces that theycan describe.

    The missing type system feature is called higher-kindedpolymorphism.

    Type systems and Expressibility Limits

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    58/69

    Some type systems render abstraction humanly intractablea

    JavaScript

    Ruby

    Python

    athough some brave souls have tried

    Type systems and Expressibility Limits

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    59/69

    The likelihood of correctly utilising abstraction at the level of theseinterfaces approaches zero very quickly.

    Type systems and Expressibility Limits

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    60/69

    So we enter this feedback loop

    The programmer is limited by tools, and then the tools limit thecreative potential of the programmer.

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    61/69

    Imagine, for a minute, a programming language that did not allowthe programmer to generalise on list element types . . .

    The Parable of the listreverse project

    http://find/http://goback/
  • 5/19/2018 Comonads Applicative Monads Codemania

    62/69

    . . . and if you wanted to reverse a list of bananas, you would solvethat problem specific to bananas.

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    63/69

    But what if we then had to also reverse a list of oranges?

    Well, we would copy and paste the previous code :)

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    64/69

    But what if we then had to also reverse a list of oranges?

    Well, we would copy and paste the previous code :)

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    65/69

    Soon enough, there would be a listreverse project and contributors,with all the different list reversals.

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    66/69

    So, you asked. . .

    Why dont we use a programming environment that supportsreversal on anyelement type?

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    67/69

    and you were told. . .

    The listreverse project is doing just fine and is used in

    many enterprise projects and has many contributors

    successfully incorporating it into their solutions.

    The Parable of the listreverse project

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    68/69

    The reason

    These interfaces are not exploited is due to unfamiliarityand toolsupport that discourages exploitation providing the perception ofprogress.

    Mission

    http://find/
  • 5/19/2018 Comonads Applicative Monads Codemania

    69/69

    It is my mission is to change this and to help others exploit useful

    programming concepts, so please ask me more about it!

    http://find/