SlideShare a Scribd company logo
1 of 78
Download to read offline
Simon Peyton Jones (Microsoft Research)

         Texas A&M, March 2011
Practitioners



                1,000,000



                  10,000


                     100
Geeks




                                  The quick death
                       1


                            1yr    5yr     10yr     15yr
Practitioners



                1,000,000



                  10,000


                     100
                                        The slow death
Geeks




                       1


                            1yr   5yr      10yr          15yr
Threshold of immortality
Practitioners



                1,000,000



                  10,000
                                            The complete
                     100                   absence of death
Geeks




                       1


                            1yr      5yr       10yr           15yr
Practitioners



                1,000,000



                  10,000


                     100
                                        The committee
Geeks




                                          language
                       1


                            1yr   5yr   10yr       15yr
“Learning Haskell is a great way of
                                                                     training yourself to think functionally
                                  “I'm already looking at           so you are ready to take full advantage
                                                                          of C# 3.0 when it comes out”
Practitioners


                                 coding problems and my
                                 mental perspective is now                       (blog Apr 2007)
                1,000,000         shifting back and forth
                               between purely OO and more
                                   FP styled solutions”
                                      (blog Mar 2007)
                  10,000


                     100
                                                         The second life?
Geeks




                       1


                            1990          1995               2000             2005                2010
langpop.com Oct 2008
        langpop.com
langpop.com Oct 2008
Ideas
•   Purely functional (immutable values)
•   Controlling effects (monads)
•   Laziness
•   Concurrency and parallelism
•   Domain specific embedded languages
•   Sexy types in general
•   Type classes in particular
Let‟s write
   code
Higher order    Polymorphism
Type signature
                                (works for any
                                    type a)

 filter :: (a->Bool) -> [a] -> [a]
 filter p [] = []
 filter p (x:xs)
       | p x       = x : filter p xs
       | otherwise = filter p xs
Higher order       Polymorphism
Type signature
                                      (works for any
                                          type a)

  filter :: (a->Bool) -> [a] -> [a]
  filter p [] = []
  filter p (x:xs)
        | p x       = x : filter p xs
        | otherwise = filter p xs


Functions defined         Guards          fxy
   by pattern           distinguish    rather than
    matching            sub-cases         f(x,y)
Higher order    Polymorphism
Type signature
                                (works for any
                                    type a)

 filter :: (a->Bool) -> [a] -> [a]
 filter p [] = []
 filter p (x:xs)
       | p x       = x : filter p xs
       | otherwise = filter p xs

 data Bool = False | True
 data [a] = []     | a:[a]
                                Declare new data
                                     types
member :: a -> [a] -> Bool
  member x []                 = False
  member x (y:ys) | x==y      = True
                  | otherwise = member x ys

Test for equality

 Can this really work FOR ANY type a?
 E.g. what about functions?
 member negate [increment, ¥x.0-x, negate]
 Similar problems
     sort :: [a] -> [a]
     (+) :: a -> a -> a
     show :: a -> String
     serialise :: a -> BitString
     hash :: a -> Int
 Local choice
   Write (a + b) to mean (a `plusFloat` b) or
    (a `plusInt` b) depending on type of a,b
   Loss of abstraction; eg member is monomorphic

 Provide equality, serialisation for everything,
  with runtime error for (say) functions
   Not extensible: just a baked-in solution for
    certain baked-in functions
   Run-time errors
Works for any type „a‟,
         provided ‘a’ is an
      instance of class Num


    square :: Num a => a -> a
    square x = x*x


Similarly:
       sort      :: Ord a => [a] -> [a]
       serialise :: Show a => a -> String
       member    :: Eq a   => a -> [a] -> Bool
Works for any type „n‟                              FORGET all
 that supports the                                   you know
  Num operations                                     about OO
                                                      classes!
       square :: Num n       => n -> n
       square x = x*x
                                               The class
                                            declaration says
       class Num a       where               what the Num
         (+)    ::       a -> a -> a         operations are
         (*)    ::       a -> a -> a
         negate ::       a -> a
         ...etc..                              An instance
                                            declaration for a
                                           type T says how the
       instance Num      Int where         Num operations are
         a + b    =      plusInt a b       implemented on T‟s
         a * b    =      mulInt a b
         negate a =      negInt a
         ...etc..                        plusInt :: Int -> Int -> Int
                                         mulInt :: Int -> Int -> Int
                                         etc, defined as primitives
When you write this...              ...the compiler generates this
square :: Num n => n -> n           square :: Num n -> n -> n
square x = x*x                      square d x = (*) d x x




                          The “Num n =>” turns into an
                           extra value argument to the
                                      function.
                         It is a value of data type Num n



                                        A value of type (Num T) is a
                                        vector (vtable) of the Num
                                           operations for type T
When you write this...          ...the compiler generates this
square :: Num n => n -> n       square :: Num n -> n -> n
square x = x*x                  square d x = (*) d x x

class Num a      where          data Num a
  (+)    ::      a -> a -> a      = MkNum (a->a->a)
  (*)    ::      a -> a -> a               (a->a->a)
  negate ::      a -> a                    (a->a)
  ...etc..                                 ...etc...

                                (*) :: Num a -> a -> a -> a
                                (*) (MkNum _ m _ ...) = m
The class decl translates to:
• A data type decl for Num
• A selector function for           A value of type (Num T) is a
  each class operation           vector of the Num operations for
                                               type T
When you write this...          ...the compiler generates this
square :: Num n => n -> n       square :: Num n -> n -> n
square x = x*x                  square d x = (*) d x x


instance Num      Int where     dNumInt :: Num Int
  a + b    =      plusInt a b   dNumInt = MkNum plusInt
  a * b    =      mulInt a b                    mulInt
  negate a =      negInt a                      negInt
  ...etc..                                      ...



  An instance decl for type T
     translates to a value          A value of type (Num T) is a
   declaration for the Num       vector of the Num operations for
        dictionary for T                       type T
When you write this...          ...the compiler generates this
f :: Int -> Int                 f :: Int -> Int
f x = negate (square x)         f x = negate dNumInt
                                       (square dNumInt x)

instance Num      Int where     dNumInt :: Num Int
  a + b    =      plusInt a b   dNumInt = MkNum plusInt
  a * b    =      mulInt a b                    mulInt
  negate a =      negInt a                      negInt
  ...etc..                                      ...



  An instance decl for type T
     translates to a value          A value of type (Num T) is a
   declaration for the Num       vector of the Num operations for
        dictionary for T                       type T
 You can build big overloaded functions by
      calling smaller overloaded functions

sumSq :: Num n => n -> n -> n
sumSq x y = square x + square y



                        sumSq :: Num n -> n -> n -> n
                        sumSq d x y = (+) d (square d x)
                                            (square d y)




             Extract addition
                                   Pass on d to square
             operation from d
 You can build big instances by building on
      smaller instances
class Eq a where
  (==) :: a -> a -> Bool

instance Eq a   => Eq [a] where
  (==) []       []     = True
  (==) (x:xs)   (y:ys) = x==y && xs == ys
  (==) _        _      = False

                data Eq = MkEq (a->a->Bool)
                (==) (MkEq eq) = eq

                dEqList   :: Eq a -> Eq [a]
                dEqList   d = MkEq eql
                  where
                    eql   []     []     = True
                    eql   (x:xs) (y:ys) = (==) d x y && eql xs ys
                    eql   _      _      = False
class Num a where                    Even literals are
  (+) :: a -> a -> a                   overloaded
  (-) :: a -> a -> a
  fromInteger :: Integer -> a
  ....
                                       “1” means
inc :: Num a => a -> a              “fromInteger 1”
inc x = x + 1



            inc :: Num a -> a -> a
            inc d x = (+) d x (fromInteger d 1)
propRev :: [Int] -> Bool
   propRev xs = reverse (reverse xs) == xs

   propRevApp :: [Int] -> [Int] -> Bool
   propRevApp xs ys = reverse (xs++ys) ==
                      reverse ys ++ reverse xs

Quickcheck (which is just a Haskell 98 library)
 Works out how many arguments
 Generates suitable
  test data                ghci> quickCheck propRev
 Runs tests               OK: passed 100 tests

                          ghci> quickCheck propRevApp
                          OK: passed 100 tests
quickCheck :: Testable a => a -> IO ()

    class Testable a where
      test :: a -> RandSupply -> Bool

    class Arbitrary a where
      arby :: RandSupply -> a

    instance Testable Bool where
      test b r = b

    instance (Arbitrary a, Testable b)
          => Testable (a->b) where
      test f r = test (f (arby r1)) r2
               where (r1,r2) = split r

split :: RandSupply -> (RandSupply, RandSupply)
propRev :: [Int] -> Bool


                                Using instance for (->)
test propRev r
= test (propRev (arby r1)) r2
where (r1,r2) = split r         Using instance for Bool
= propRev (arby r1)
 Equality, ordering, serialisation
 Numerical operations. Even numeric constants
  are overloaded
 Monadic operations
       class Monad m where
         return :: a -> m a
         (>>=) :: m a -> (a -> m b) -> m b

 And on and on....time-varying
                                                Note the
  values, pretty-printing, collections,       higher-kinded
  reflection, generic programming,           type variable, m
  marshalling, monad transformers....
 Type classes are the most unusual feature of
  Haskell‟s type system

                                                          Hey, what’s
Wild enthusiasm                                           the big deal?




                         Despair                  Hack,
Incomprehension                                   hack,
                                                  hack



     1987         1989             1993    1997
                          Implementation begins
Higher kinded
                                    Implicit
          type variables
                               parameters (2000)
             (1995)

Wadler/                          Extensible
             Multi-            records (1996)      Computation
 Blott
           parameter                               at the type
 type
          type classes                                 level
classes
(1989)       (1991)         Functional
                           dependencies
                              (2000)                 Generic
           Overlapping                             programming
            instances

            “newtype
            deriving”                                Testing
                              Associated
                             types (2005)
            Derivable
           type classes                             Applications

          Variations
Type classes
               and
   object-oriented programming
1. Type-based dispatch, not value-
   based dispatch
 A bit like OOP, except that method suite
  (vtable) is passed separately?

        class Show where
          show :: a -> String

        f :: Show a => a ->
          ...


 No!! Type classes implement type-based
  dispatch, not value-based dispatch
class Read a where
                      read :: String -> a

                    class Num a where
                      (+) :: a -> a -> a
                      fromInteger :: Integer -> a

read2 :: (Read a, Num a) => String -> a
read2 s = read s + 2



   read2 dr dn s = (+) dn (read dr s)
                          (fromInteger dn 2)

 The overloaded value is returned by read2,
  not passed to it.
 It is the dictionaries (and type) that are
  passed as argument to read2
So the links to intensional polymorphism are
closer than the links to OOP.
The dictionary is like a proxy for the
(interesting aspects of) the type argument of a
polymorphic function.
                                     Intensional
                                    polymorphism
    f :: forall a. a -> Int
    f t (x::t) = ...typecase t...
                                       Haskell
    f :: forall a. C a => a -> Int
    f x = ...(call method of C)...
Type classes
               and
   object-oriented programming
1. Type-based dispatch, not value-
   based dispatch
2. Haskell “class” ~ OO “interface”
A Haskell class is more like a Java interface
  than a Java class: it says what operations
  the type must support.

                            interface Showable {
class Show a where            String show();
  show :: a -> String       }

                            class Blah {
f :: Show a => a -> ...
                              f( Showable x ) {
                                  ...x.show()...
                            } }
 No problem with multiple constraints:
f :: (Num a, Show a)          class Blah {
  => a -> ...                   f( ??? x ) {
                                    ...x.show()...
                              } }

  Existing types can retroactively be made instances
   of new type classes (e.g. introduce new Wibble
   class, make existing types an instance of it)

class Wibble a where           interface Wibble {
  wib :: a -> Bool               bool wib()
                               }
instance Wibble Int where
                               ...does Int support
  wib n = n+1
                                 Wibble?....
Type classes
               and
   object-oriented programming
1. Type-based dispatch, not value-
   based dispatch
2. Haskell “class” ~ OO “interface”
3. Generics (i.e. parametric
   polymorphism) , not subtyping
 Polymorphism: same code works on a variety
     of different argument types




 OO culture                                        ML culture

cost :: Car -> Int                 rev :: [a] -> [a]
cost works on Fords, Renaults...   rev works on [Int], [Char],...
 Each approach has been elaborated considerably over
     the last decade



Add
interfaces,                               Add type classes ,
generics,                                 type families,
constrained                               existentials
generics




    What differences remain?
    Can one develop a unified story?
 Haskell has no sub-typing
 data Tree = Leaf | Branch Tree Tree

 f :: Tree -> Int                  f‟s argument must
 f t = ...                        be (exactly) a Tree


 Ability to act on argument of various types
  achieved via type classes:       Works for any
 square :: (Num a) => a -> a           type supporting
 square x = x*x                            the Num
                                          interface
 Means that in Haskell you must anticipate
  the need to act on arguments of various
  types
                 f :: Tree -> Int
                         vs
           f’ :: Treelike a => a -> Int




(in OO you can retroactively sub-class Tree)
 Type annotations:
   Implicit = the type of a fresh binder is inferred
                         f x = ...
   Explicit = each binder is given a type at its binding
    site                 void f( int x ) { ... }
 Cultural heritage:
   Haskell: everything implicit
             type annotations occasionally needed
   Java:    everything explicit;
             type inference occasionally possible
 Type annotations:
   Implicit = the type of a fresh binder is inferred
                         f x = ...
   Explicit = each binder is given a type at its binding
    site                 void f( int x ) { ... }
 Reason:
   Generics alone => type engine generates equality
    constraints, which it can solve
   Subtyping => type engine generates subtyping
    constraints, which it cannot solve (uniquely)
eqList   :: [Int] -> [Int] -> Bool
    eqList   []     []     = True
    eqList   (x:xs) (y:ys) = x==y && eqList xs ys
    eqList   _       _     = False

Here we know that the two arguments have
exactly the same type
     class Eq a where
       (==) :: a -> a -> Bool

     instance Eq a   => Eq [a] where
       (==) []       []     = True
       (==) (x:xs)   (y:ys) = x==y && xs == ys
       (==) _        _      = False
Result: any super-   Argument: any sub-
                     type of INum         type of INum
 In Java (ish):
       INum inc( INum x )
                                              Result has
 In Haskell:                               precisely same
                                           type as argument
       inc :: Num a => a -> a

 Compare...
 x::Float                       x::Float

 ...(x.inc)...                  ...(inc x)...

       INum                             Float
 In practice, because many operations work by
  side effect, result contra-variance doesn‟t
  matter too much
                                      None of this
        x.setColour(Blue);
                                    changes x‟s type
        x.setPosition(3,4);

 In a purely-functional world, where setColour,
  setPosition return a new x, result contra-
  variance might be much more important
 F#‟s immutable libraries don‟t use subclassing
  (binary methods big issue here too; eg set union)
 Java and C# both (now) support constrained
  generics
        A inc<A>( A x)
          where A:INum {
           ...blah...
        }

 Very like
        inc :: Num a => a -> a

 (but little used in practice, I believe)
Legal iff T
                                                      is only
                                                     returned
                                                        by
interface IEnumerator<out T> {                       methods,
                                                      but not
  T Current;
                                                    passed to a
  bool MoveNext();                                   method,
}                                                    nor side-
                                                     effected
 Why? So that this works
     m( IEnumerator<Control> )
     IEnumerator<Button> b
     ...m(b)...
   Button is a subtype of Control, so
   IEnumerator<Button> is a subtype of IEnumerator<Control>
 OOP: must embrace variance
   Side effects => invariance
   Generics: type parameters are co/contra/invariant
    (Java wildcards, C#4.0 variance annotations)
   Interaction with higher kinds?
      class Monad m where
        return :: a -> m a
        (>>=) :: m a -> (a -> m b) -> m b

          (Only Scala can do this, and it‟s very tricky!)
 Variance simply does not arise in Haskell.
 And we need constraint polymorphism anyway!
In a language with
    • Generics
    • Constrained polymorphism
do you (really) need subtyping too?
What I envy
about OOP
 The power of the dot
    IDE magic
    overload short function
     names

 That is:    Use the type of the first (self) argument to
              (a) “x.”: display candidate functions
              (b)“x.reset”: fix which “reset” you mean

 (Yes there is more: use argument syntax to further
  narrow which function you mean.)
 Curiously, this is not specific to OOP, or
  to sub-typing, or to dynamic dispatch
 Obvious thought: steal this idea and add this
  to Haskell

module M where
  import Button -- reset :: Button -> IO ()
  import Canvas -- reset :: Canvas -> IO ()

  fluggle :: Button -> ...
  fluggle b = ...(b.reset)...
 OOP lets you have a collection of
  heterogeneous objects
           void f( Shape[] x );

           a::Circle
           b::Rectangle

           ....f (new Shape[] {a, b})...
void f( Shape[] x );

                              a::Circle
                              b::Rectangle

                              ....f (new Shape[] {a, b})...


 You can encode this in Haskell, although it is
  slightly clumsy

data Shape where
   MkShape :: Shapely a => a -> Shape

a :: Circle
b :: Rectangle

....(f [MkShape a, MkShape b])...
 The ability to make run-time type tests is
  hugely important in OOP.
 We have (eventually) figured out to do this
  in Haskell:
cast :: (Typeable a, Typeable b) => a -> Maybe b


  class Typeable a where
    typeOf :: a -> TypeRep

  instance Typeable Bool where
    typeOf _ = MkTypeRep “Bool” []

  instance Typeable a => Typeable [a] where
    typeOf xs = MkTypeRep “List” [typeOf (head xs) ]
New
developments in
  type classes
plusInt    :: Int -> Int -> Int
                    plusFloat :: Float -> Float -> Float
                    intToFloat :: Int -> Float




class GNum a b where
  (+) :: a -> b -> ???

instance GNum Int Int where
  (+) x y = plusInt x y

instance GNum Int Float where
  (+) x y = plusFloat (intToFloat x) y

test1 = (4::Int) + (5::Int)
test2 = (4::Int) + (5::Float)
class GNum a b where
     (+) :: a -> b -> ???

 Result type of (+) is a function of the
  argument types                      SumTy is an
                                     associated type of
   class GNum a b where                 class GNum
     type SumTy a b :: *
     (+) :: a -> b -> SumTy a b

 Each method gets a type signature
 Each associated type gets a kind signature
class GNum a b where
    type SumTy a b :: *
    (+) :: a -> b -> SumTy a b

 Each instance declaration gives a “witness”
  for SumTy, matching the kind signature
  instance GNum Int Int where
    type SumTy Int Int = Int
    (+) x y = plusInt x y

  instance GNum Int Float where
    type SumTy Int Float = Float
    (+) x y = plusFloat (intToFloat x) y
class GNum a b where
    type SumTy a b :: *
  instance GNum Int Int where
    type SumTy Int Int = Int
  instance GNum Int Float where
    type SumTy Int Float = Float

 SumTy is a type-level function
 The type checker simply rewrites
   SumTy Int Int --> Int
   SumTy Int Float --> Float
  whenever it can

 But (SumTy t1 t2) is still a perfectly good type,
  even if it can‟t be rewritten. For example:
   data T a b = MkT a b (SumTy a b)
 Inspired by associated types from OOP
 Fit beautifully with type classes
 Push the type system a little closer to
  dependent types, but not too close!
 Generalise “functional dependencies”
 ...still developing...
 It‟s a complicated world.
 Rejoice in diversity. Learn from the
  competition.
 What can Haskell learn from OOP?
   The power of the dot (IDE, name space control)

 What can OOP learn from Haskell?
   The big question for me is: once we have
    wholeheartedly adopted generics, do we still
    really need subtyping?
Georg Weissenbacher [georg.weissenbacher@magd.ox.ac.uk] emailed this info:

I mentioned today that James Gosling once said in an interview that he'd remove inheritance from
Java if he could. Unfortunately, I can't find the original interview anymore, but there's a plethora of
references to his statement on the web. For instance:
 http://www.artima.com/intv/gosling34.html
 http://www.javaworld.com/javaworld/jw-06-2001/j1-01-gosling.html?page=3
Venners: When asked what you might do differently if you could recreate Java, you've said you've
wondered what it would be like to have a language that just does delegation.
Gosling: Yes.
[...]
Venners: But by delegation, you do mean this object delegating to that object without it being a
subclass?
Gosling: Yes -- without an inheritance hierarchy. Rather than subclassing, just use pure interfaces.
It's not so much that class inheritance is particularly bad. It just has problems.
 or:
 http://www.newt.com/wohler/articles/james-gosling-ramblings-1.html
 There were two main questions: What would you take out? What would you put in? To the first, James
evoked laughter with the single word: Classes. He would like to replace classes with delegation since
doing delegation right would make inheritance go away. But it's like Whack-A-Mole (more laughter)
since when you hit one mole, er, problem, another pops up. What they already had to take out was pre-
and post-assertions.
 See paper “Fun with type functions” [2009]
  on Simon PJ‟s home page
 Consider a finite map, mapping keys to values
 Goal: the data representation of the map
  depends on the type of the key
   Boolean key: store two values (for F,T resp)
   Int key: use a balanced tree
   Pair key (x,y): map x to a finite map from y to
    value; ie use a trie!

 Cannot do this in Haskell...a good program
  that the type checker rejects
data Maybe a = Nothing | Just a

                             Map is indexed by k,
class Key k where            but parametric in its
  data Map k :: * -> *         second argument
  empty :: Map k v
  lookup :: k -> Map k v -> Maybe v
  ...insert, union, etc....
data Maybe a = Nothing | Just a


                             Optional value
class Key k where
                               for False
  data Map k :: * -> *
  empty :: Map k v
  lookup :: k -> Map k v -> Maybe v
  ...insert, union, etc....            Optional value
                                            for True
instance Key Bool where
  data Map Bool v = MB (Maybe v) (Maybe v)
  empty = MB Nothing Nothing
  lookup True (MB _ mt) = mt
  lookup False (MB mf _) = mf
data Maybe a = Nothing | Just a

class Key k where
                                      Two-level
  data Map k :: * -> *
                                        map
  empty :: Map k v
  lookup :: k -> Map k v -> Maybe v               Two-level
  ...insert, union, etc....                        lookup

instance (Key a, Key b) => Key (a,b) where
  data Map (a,b) v = MP (Map a (Map b v))
  empty = MP empty
  lookup (ka,kb) (MP m) = case lookup ka m of
                            Nothing -> Nothing
                            Just m2 -> lookup kb m2

 See paper for lists as keys: arbitrary depth tries
 Goal: the data representation of the map
  depends on the type of the key
   Boolean key: SUM
   Pair key (x,y): PRODUCT
   List key [x]: SUM of PRODUCT + RECURSION

 Easy to extend to other types at will
Client               Server

 addServer :: In Int (In Int (Out Int End))
  addClient :: Out Int (Out Int (In Int End))
 Type of the process expresses its protocol
 Client and server should have dual protocols:
     run addServer addClient          -- OK!
     run addServer addServer          -- BAD!
Client              Server

 addServer :: In Int (In Int (Out Int End))
  addClient :: Out Int (Out Int (In Int End))

   data In v p = In (v -> p)
   data Out v p = Out v p
   data End     = End


          NB punning
data In v p = In (v -> p)
 data Out v p = Out v p
 data End     = End



addServer :: In Int (In Int (Out Int End))
addServer = In (¥x -> In (¥y ->
            Out (x + y) End))


 Nothing fancy here
 addClient is similar
run :: ??? -> ??? -> End

       A process      A co-process


class Process p where
  type Co p
  run :: p -> Co p -> End

 Same deal as before: Co is a type-level
  function that transforms a process type into
  its dual
class Process p where       data In v p = In (v -> p)
  type Co p                 data Out v p = Out v p
  run :: p -> Co p -> End   data End     = End


instance Process p => Process (In v p) where
  type Co (In v p) = Out v (Co p)
  run (In vp) (Out v p) = run (vp v) p

instance Process p => Process (Out v p) where
  type Co (Out v p) = In v (Co p)
  run (Out v p) (In vp) = run p (vp v)


             Just the obvious thing really
HASKELL                            C#
   A statically typed,           A statically typed
   purely functional             object oriented
    language...                    language...
   designed 20 years ago...      designed 10 years ago
   by a distributed              by a tight-knit group
    committee...                  based in industry
   of pointy-headed              for real applications
    academics...
   as a research language.

More Related Content

What's hot

Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Real World Haskell: Lecture 4
Real World Haskell: Lecture 4
Bryan O'Sullivan
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
Bryan O'Sullivan
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
Bryan O'Sullivan
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
Bryan O'Sullivan
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
stasimus
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
Bryan O'Sullivan
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
stasimus
 
C# Language Overview Part I
C# Language Overview Part IC# Language Overview Part I
C# Language Overview Part I
Doncho Minkov
 

What's hot (19)

Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Real World Haskell: Lecture 4
Real World Haskell: Lecture 4
 
3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does not
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Lezione03
Lezione03Lezione03
Lezione03
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does not
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
C# Language Overview Part I
C# Language Overview Part IC# Language Overview Part I
C# Language Overview Part I
 

Viewers also liked

オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
Preferred Networks
 
tensor-decomposition
tensor-decompositiontensor-decomposition
tensor-decomposition
Kenta Oono
 
Jubatusにおける大規模分散オンライン機械学習
Jubatusにおける大規模分散オンライン機械学習Jubatusにおける大規模分散オンライン機械学習
Jubatusにおける大規模分散オンライン機械学習
Preferred Networks
 

Viewers also liked (20)

Peyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futurePeyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_future
 
MongoDB as Search Engine Repository @ MongoTokyo2011
MongoDB as Search Engine Repository @ MongoTokyo2011MongoDB as Search Engine Repository @ MongoTokyo2011
MongoDB as Search Engine Repository @ MongoTokyo2011
 
jubatus pressrelease
jubatus pressreleasejubatus pressrelease
jubatus pressrelease
 
Succinct Data Structure for Analyzing Document Collection
Succinct Data Structure for Analyzing Document CollectionSuccinct Data Structure for Analyzing Document Collection
Succinct Data Structure for Analyzing Document Collection
 
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
オンライン凸最適化と線形識別モデル学習の最前線_IBIS2011
 
rcast_20140411
rcast_20140411rcast_20140411
rcast_20140411
 
Herd
HerdHerd
Herd
 
LCCC2010:Learning on Cores, Clusters and Cloudsの解説
LCCC2010:Learning on Cores,  Clusters and Cloudsの解説LCCC2010:Learning on Cores,  Clusters and Cloudsの解説
LCCC2010:Learning on Cores, Clusters and Cloudsの解説
 
ウェーブレット木の世界
ウェーブレット木の世界ウェーブレット木の世界
ウェーブレット木の世界
 
tensor-decomposition
tensor-decompositiontensor-decomposition
tensor-decomposition
 
Jubatusにおける大規模分散オンライン機械学習
Jubatusにおける大規模分散オンライン機械学習Jubatusにおける大規模分散オンライン機械学習
Jubatusにおける大規模分散オンライン機械学習
 
bigdata2012ml okanohara
bigdata2012ml okanoharabigdata2012ml okanohara
bigdata2012ml okanohara
 
MapReduceによる大規模データを利用した機械学習
MapReduceによる大規模データを利用した機械学習MapReduceによる大規模データを利用した機械学習
MapReduceによる大規模データを利用した機械学習
 
大規模データ時代に求められる自然言語処理
大規模データ時代に求められる自然言語処理大規模データ時代に求められる自然言語処理
大規模データ時代に求められる自然言語処理
 
文法圧縮入門:超高速テキスト処理のためのデータ圧縮(NLP2014チュートリアル)
文法圧縮入門:超高速テキスト処理のためのデータ圧縮(NLP2014チュートリアル)文法圧縮入門:超高速テキスト処理のためのデータ圧縮(NLP2014チュートリアル)
文法圧縮入門:超高速テキスト処理のためのデータ圧縮(NLP2014チュートリアル)
 
bigdata2012nlp okanohara
bigdata2012nlp okanoharabigdata2012nlp okanohara
bigdata2012nlp okanohara
 
Development and Experiment of Deep Learning with Caffe and maf
Development and Experiment of Deep Learning with Caffe and mafDevelopment and Experiment of Deep Learning with Caffe and maf
Development and Experiment of Deep Learning with Caffe and maf
 
Chainer meetup20151014
Chainer meetup20151014Chainer meetup20151014
Chainer meetup20151014
 
Chainer Meetup LT (Alpaca)
Chainer Meetup LT (Alpaca)Chainer Meetup LT (Alpaca)
Chainer Meetup LT (Alpaca)
 
A Chainer MeetUp Talk
A Chainer MeetUp TalkA Chainer MeetUp Talk
A Chainer MeetUp Talk
 

Similar to Peyton jones-2011-type classes

High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak
 

Similar to Peyton jones-2011-type classes (20)

Tip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python Typing
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programming
 
Python-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxPython-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptx
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Python basics
Python basicsPython basics
Python basics
 
Scala: A brief tutorial
Scala: A brief tutorialScala: A brief tutorial
Scala: A brief tutorial
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Testing for share
Testing for share Testing for share
Testing for share
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
Cats in Scala
Cats in ScalaCats in Scala
Cats in Scala
 
Python Data Types,numbers.pptx
Python Data Types,numbers.pptxPython Data Types,numbers.pptx
Python Data Types,numbers.pptx
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
Python introduction
Python introductionPython introduction
Python introduction
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 

Recently uploaded

FULL NIGHT — 9999894380 Call Girls In Delhi Cantt | Delhi
FULL NIGHT — 9999894380 Call Girls In Delhi Cantt | DelhiFULL NIGHT — 9999894380 Call Girls In Delhi Cantt | Delhi
FULL NIGHT — 9999894380 Call Girls In Delhi Cantt | Delhi
SaketCallGirlsCallUs
 
Call Girls in Sakinaka 9892124323, Vashi CAll Girls Call girls Services, Che...
Call Girls in Sakinaka  9892124323, Vashi CAll Girls Call girls Services, Che...Call Girls in Sakinaka  9892124323, Vashi CAll Girls Call girls Services, Che...
Call Girls in Sakinaka 9892124323, Vashi CAll Girls Call girls Services, Che...
Pooja Nehwal
 
DELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| Delhi
DELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| DelhiDELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| Delhi
DELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| Delhi
delhimunirka444
 
UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)
UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)
UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)
Business Bay Call Girls || 0529877582 || Call Girls Service in Business Bay Dubai
 
FULL NIGHT — 9999894380 Call Girls In Mahipalpur | Delhi
FULL NIGHT — 9999894380 Call Girls In Mahipalpur | DelhiFULL NIGHT — 9999894380 Call Girls In Mahipalpur | Delhi
FULL NIGHT — 9999894380 Call Girls In Mahipalpur | Delhi
SaketCallGirlsCallUs
 
Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)
Business Bay Call Girls || 0529877582 || Call Girls Service in Business Bay Dubai
 
(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶
(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶
(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶
delhimunirka444
 
❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...
❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...
❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
Bobbie goods coloring book 81 pag_240127_163802.pdf
Bobbie goods coloring book 81 pag_240127_163802.pdfBobbie goods coloring book 81 pag_240127_163802.pdf
Bobbie goods coloring book 81 pag_240127_163802.pdf
MARIBEL442158
 
Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..
mvxpw22gfc
 
Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)
Business Bay Call Girls || 0529877582 || Call Girls Service in Business Bay Dubai
 
FULL NIGHT — 9999894380 Call Girls In Saket | Delhi
FULL NIGHT — 9999894380 Call Girls In Saket | DelhiFULL NIGHT — 9999894380 Call Girls In Saket | Delhi
FULL NIGHT — 9999894380 Call Girls In Saket | Delhi
SaketCallGirlsCallUs
 
FULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | Delhi
FULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | DelhiFULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | Delhi
FULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | Delhi
SaketCallGirlsCallUs
 
Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...
Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...
Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...
home
 

Recently uploaded (20)

FULL NIGHT — 9999894380 Call Girls In Delhi Cantt | Delhi
FULL NIGHT — 9999894380 Call Girls In Delhi Cantt | DelhiFULL NIGHT — 9999894380 Call Girls In Delhi Cantt | Delhi
FULL NIGHT — 9999894380 Call Girls In Delhi Cantt | Delhi
 
Call Girls in Sakinaka 9892124323, Vashi CAll Girls Call girls Services, Che...
Call Girls in Sakinaka  9892124323, Vashi CAll Girls Call girls Services, Che...Call Girls in Sakinaka  9892124323, Vashi CAll Girls Call girls Services, Che...
Call Girls in Sakinaka 9892124323, Vashi CAll Girls Call girls Services, Che...
 
GENUINE EscoRtS,Call Girls IN South Delhi Locanto TM''| +91-8377087607
GENUINE EscoRtS,Call Girls IN South Delhi Locanto TM''| +91-8377087607GENUINE EscoRtS,Call Girls IN South Delhi Locanto TM''| +91-8377087607
GENUINE EscoRtS,Call Girls IN South Delhi Locanto TM''| +91-8377087607
 
Barasat call girls 📞 8617697112 At Low Cost Cash Payment Booking
Barasat call girls 📞 8617697112 At Low Cost Cash Payment BookingBarasat call girls 📞 8617697112 At Low Cost Cash Payment Booking
Barasat call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
DELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| Delhi
DELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| DelhiDELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| Delhi
DELHI NCR —@9711106444 Call Girls In Majnu Ka Tilla (MT)| Delhi
 
UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)
UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)
UAE Call Girls # 971526940039 # Independent Call Girls In Dubai # (UAE)
 
FULL NIGHT — 9999894380 Call Girls In Mahipalpur | Delhi
FULL NIGHT — 9999894380 Call Girls In Mahipalpur | DelhiFULL NIGHT — 9999894380 Call Girls In Mahipalpur | Delhi
FULL NIGHT — 9999894380 Call Girls In Mahipalpur | Delhi
 
Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 0522916705 # Call Girl Number In Dubai # (UAE)
 
(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶
(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶
(9711106444 )🫦#Sexy Desi Call Girls Noida Sector 4 Escorts Service Delhi 🫶
 
Mayiladuthurai Call Girls 8617697112 Short 3000 Night 8000 Best call girls Se...
Mayiladuthurai Call Girls 8617697112 Short 3000 Night 8000 Best call girls Se...Mayiladuthurai Call Girls 8617697112 Short 3000 Night 8000 Best call girls Se...
Mayiladuthurai Call Girls 8617697112 Short 3000 Night 8000 Best call girls Se...
 
❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...
❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...
❤ Sexy Call Girls in Chandigarh 👀📞 90,539,00,678📞 Chandigarh Call Girls Servi...
 
Bobbie goods coloring book 81 pag_240127_163802.pdf
Bobbie goods coloring book 81 pag_240127_163802.pdfBobbie goods coloring book 81 pag_240127_163802.pdf
Bobbie goods coloring book 81 pag_240127_163802.pdf
 
Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..
 
Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)
Dubai Call Girl Number # 00971588312479 # Call Girl Number In Dubai # (UAE)
 
(INDIRA) Call Girl Dehradun Call Now 8617697112 Dehradun Escorts 24x7
(INDIRA) Call Girl Dehradun Call Now 8617697112 Dehradun Escorts 24x7(INDIRA) Call Girl Dehradun Call Now 8617697112 Dehradun Escorts 24x7
(INDIRA) Call Girl Dehradun Call Now 8617697112 Dehradun Escorts 24x7
 
FULL NIGHT — 9999894380 Call Girls In Saket | Delhi
FULL NIGHT — 9999894380 Call Girls In Saket | DelhiFULL NIGHT — 9999894380 Call Girls In Saket | Delhi
FULL NIGHT — 9999894380 Call Girls In Saket | Delhi
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
 
FULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | Delhi
FULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | DelhiFULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | Delhi
FULL NIGHT — 9999894380 Call Girls In Shivaji Enclave | Delhi
 
Sirmaur Call Girls Book Now 8617697112 Top Class Pondicherry Escort Service A...
Sirmaur Call Girls Book Now 8617697112 Top Class Pondicherry Escort Service A...Sirmaur Call Girls Book Now 8617697112 Top Class Pondicherry Escort Service A...
Sirmaur Call Girls Book Now 8617697112 Top Class Pondicherry Escort Service A...
 
Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...
Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...
Authentic # 00971556872006 # Hot Call Girls Service in Dubai By International...
 

Peyton jones-2011-type classes

  • 1. Simon Peyton Jones (Microsoft Research) Texas A&M, March 2011
  • 2. Practitioners 1,000,000 10,000 100 Geeks The quick death 1 1yr 5yr 10yr 15yr
  • 3. Practitioners 1,000,000 10,000 100 The slow death Geeks 1 1yr 5yr 10yr 15yr
  • 4. Threshold of immortality Practitioners 1,000,000 10,000 The complete 100 absence of death Geeks 1 1yr 5yr 10yr 15yr
  • 5. Practitioners 1,000,000 10,000 100 The committee Geeks language 1 1yr 5yr 10yr 15yr
  • 6. “Learning Haskell is a great way of training yourself to think functionally “I'm already looking at so you are ready to take full advantage of C# 3.0 when it comes out” Practitioners coding problems and my mental perspective is now (blog Apr 2007) 1,000,000 shifting back and forth between purely OO and more FP styled solutions” (blog Mar 2007) 10,000 100 The second life? Geeks 1 1990 1995 2000 2005 2010
  • 7. langpop.com Oct 2008 langpop.com
  • 9. Ideas • Purely functional (immutable values) • Controlling effects (monads) • Laziness • Concurrency and parallelism • Domain specific embedded languages • Sexy types in general • Type classes in particular
  • 11. Higher order Polymorphism Type signature (works for any type a) filter :: (a->Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs
  • 12. Higher order Polymorphism Type signature (works for any type a) filter :: (a->Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs Functions defined Guards fxy by pattern distinguish rather than matching sub-cases f(x,y)
  • 13. Higher order Polymorphism Type signature (works for any type a) filter :: (a->Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs data Bool = False | True data [a] = [] | a:[a] Declare new data types
  • 14. member :: a -> [a] -> Bool member x [] = False member x (y:ys) | x==y = True | otherwise = member x ys Test for equality  Can this really work FOR ANY type a?  E.g. what about functions? member negate [increment, ¥x.0-x, negate]
  • 15.  Similar problems  sort :: [a] -> [a]  (+) :: a -> a -> a  show :: a -> String  serialise :: a -> BitString  hash :: a -> Int
  • 16.  Local choice  Write (a + b) to mean (a `plusFloat` b) or (a `plusInt` b) depending on type of a,b  Loss of abstraction; eg member is monomorphic  Provide equality, serialisation for everything, with runtime error for (say) functions  Not extensible: just a baked-in solution for certain baked-in functions  Run-time errors
  • 17. Works for any type „a‟, provided ‘a’ is an instance of class Num square :: Num a => a -> a square x = x*x Similarly: sort :: Ord a => [a] -> [a] serialise :: Show a => a -> String member :: Eq a => a -> [a] -> Bool
  • 18. Works for any type „n‟ FORGET all that supports the you know Num operations about OO classes! square :: Num n => n -> n square x = x*x The class declaration says class Num a where what the Num (+) :: a -> a -> a operations are (*) :: a -> a -> a negate :: a -> a ...etc.. An instance declaration for a type T says how the instance Num Int where Num operations are a + b = plusInt a b implemented on T‟s a * b = mulInt a b negate a = negInt a ...etc.. plusInt :: Int -> Int -> Int mulInt :: Int -> Int -> Int etc, defined as primitives
  • 19. When you write this... ...the compiler generates this square :: Num n => n -> n square :: Num n -> n -> n square x = x*x square d x = (*) d x x The “Num n =>” turns into an extra value argument to the function. It is a value of data type Num n A value of type (Num T) is a vector (vtable) of the Num operations for type T
  • 20. When you write this... ...the compiler generates this square :: Num n => n -> n square :: Num n -> n -> n square x = x*x square d x = (*) d x x class Num a where data Num a (+) :: a -> a -> a = MkNum (a->a->a) (*) :: a -> a -> a (a->a->a) negate :: a -> a (a->a) ...etc.. ...etc... (*) :: Num a -> a -> a -> a (*) (MkNum _ m _ ...) = m The class decl translates to: • A data type decl for Num • A selector function for A value of type (Num T) is a each class operation vector of the Num operations for type T
  • 21. When you write this... ...the compiler generates this square :: Num n => n -> n square :: Num n -> n -> n square x = x*x square d x = (*) d x x instance Num Int where dNumInt :: Num Int a + b = plusInt a b dNumInt = MkNum plusInt a * b = mulInt a b mulInt negate a = negInt a negInt ...etc.. ... An instance decl for type T translates to a value A value of type (Num T) is a declaration for the Num vector of the Num operations for dictionary for T type T
  • 22. When you write this... ...the compiler generates this f :: Int -> Int f :: Int -> Int f x = negate (square x) f x = negate dNumInt (square dNumInt x) instance Num Int where dNumInt :: Num Int a + b = plusInt a b dNumInt = MkNum plusInt a * b = mulInt a b mulInt negate a = negInt a negInt ...etc.. ... An instance decl for type T translates to a value A value of type (Num T) is a declaration for the Num vector of the Num operations for dictionary for T type T
  • 23.  You can build big overloaded functions by calling smaller overloaded functions sumSq :: Num n => n -> n -> n sumSq x y = square x + square y sumSq :: Num n -> n -> n -> n sumSq d x y = (+) d (square d x) (square d y) Extract addition Pass on d to square operation from d
  • 24.  You can build big instances by building on smaller instances class Eq a where (==) :: a -> a -> Bool instance Eq a => Eq [a] where (==) [] [] = True (==) (x:xs) (y:ys) = x==y && xs == ys (==) _ _ = False data Eq = MkEq (a->a->Bool) (==) (MkEq eq) = eq dEqList :: Eq a -> Eq [a] dEqList d = MkEq eql where eql [] [] = True eql (x:xs) (y:ys) = (==) d x y && eql xs ys eql _ _ = False
  • 25. class Num a where Even literals are (+) :: a -> a -> a overloaded (-) :: a -> a -> a fromInteger :: Integer -> a .... “1” means inc :: Num a => a -> a “fromInteger 1” inc x = x + 1 inc :: Num a -> a -> a inc d x = (+) d x (fromInteger d 1)
  • 26. propRev :: [Int] -> Bool propRev xs = reverse (reverse xs) == xs propRevApp :: [Int] -> [Int] -> Bool propRevApp xs ys = reverse (xs++ys) == reverse ys ++ reverse xs Quickcheck (which is just a Haskell 98 library)  Works out how many arguments  Generates suitable test data ghci> quickCheck propRev  Runs tests OK: passed 100 tests ghci> quickCheck propRevApp OK: passed 100 tests
  • 27. quickCheck :: Testable a => a -> IO () class Testable a where test :: a -> RandSupply -> Bool class Arbitrary a where arby :: RandSupply -> a instance Testable Bool where test b r = b instance (Arbitrary a, Testable b) => Testable (a->b) where test f r = test (f (arby r1)) r2 where (r1,r2) = split r split :: RandSupply -> (RandSupply, RandSupply)
  • 28. propRev :: [Int] -> Bool Using instance for (->) test propRev r = test (propRev (arby r1)) r2 where (r1,r2) = split r Using instance for Bool = propRev (arby r1)
  • 29.  Equality, ordering, serialisation  Numerical operations. Even numeric constants are overloaded  Monadic operations class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b  And on and on....time-varying Note the values, pretty-printing, collections, higher-kinded reflection, generic programming, type variable, m marshalling, monad transformers....
  • 30.  Type classes are the most unusual feature of Haskell‟s type system Hey, what’s Wild enthusiasm the big deal? Despair Hack, Incomprehension hack, hack 1987 1989 1993 1997 Implementation begins
  • 31. Higher kinded Implicit type variables parameters (2000) (1995) Wadler/ Extensible Multi- records (1996) Computation Blott parameter at the type type type classes level classes (1989) (1991) Functional dependencies (2000) Generic Overlapping programming instances “newtype deriving” Testing Associated types (2005) Derivable type classes Applications Variations
  • 32. Type classes and object-oriented programming 1. Type-based dispatch, not value- based dispatch
  • 33.  A bit like OOP, except that method suite (vtable) is passed separately? class Show where show :: a -> String f :: Show a => a -> ...  No!! Type classes implement type-based dispatch, not value-based dispatch
  • 34. class Read a where read :: String -> a class Num a where (+) :: a -> a -> a fromInteger :: Integer -> a read2 :: (Read a, Num a) => String -> a read2 s = read s + 2 read2 dr dn s = (+) dn (read dr s) (fromInteger dn 2)  The overloaded value is returned by read2, not passed to it.  It is the dictionaries (and type) that are passed as argument to read2
  • 35. So the links to intensional polymorphism are closer than the links to OOP. The dictionary is like a proxy for the (interesting aspects of) the type argument of a polymorphic function. Intensional polymorphism f :: forall a. a -> Int f t (x::t) = ...typecase t... Haskell f :: forall a. C a => a -> Int f x = ...(call method of C)...
  • 36. Type classes and object-oriented programming 1. Type-based dispatch, not value- based dispatch 2. Haskell “class” ~ OO “interface”
  • 37. A Haskell class is more like a Java interface than a Java class: it says what operations the type must support. interface Showable { class Show a where String show(); show :: a -> String } class Blah { f :: Show a => a -> ... f( Showable x ) { ...x.show()... } }
  • 38.  No problem with multiple constraints: f :: (Num a, Show a) class Blah { => a -> ... f( ??? x ) { ...x.show()... } }  Existing types can retroactively be made instances of new type classes (e.g. introduce new Wibble class, make existing types an instance of it) class Wibble a where interface Wibble { wib :: a -> Bool bool wib() } instance Wibble Int where ...does Int support wib n = n+1 Wibble?....
  • 39. Type classes and object-oriented programming 1. Type-based dispatch, not value- based dispatch 2. Haskell “class” ~ OO “interface” 3. Generics (i.e. parametric polymorphism) , not subtyping
  • 40.  Polymorphism: same code works on a variety of different argument types OO culture ML culture cost :: Car -> Int rev :: [a] -> [a] cost works on Fords, Renaults... rev works on [Int], [Char],...
  • 41.  Each approach has been elaborated considerably over the last decade Add interfaces, Add type classes , generics, type families, constrained existentials generics  What differences remain?  Can one develop a unified story?
  • 42.  Haskell has no sub-typing data Tree = Leaf | Branch Tree Tree f :: Tree -> Int f‟s argument must f t = ... be (exactly) a Tree  Ability to act on argument of various types achieved via type classes: Works for any square :: (Num a) => a -> a type supporting square x = x*x the Num interface
  • 43.  Means that in Haskell you must anticipate the need to act on arguments of various types f :: Tree -> Int vs f’ :: Treelike a => a -> Int (in OO you can retroactively sub-class Tree)
  • 44.  Type annotations:  Implicit = the type of a fresh binder is inferred f x = ...  Explicit = each binder is given a type at its binding site void f( int x ) { ... }  Cultural heritage:  Haskell: everything implicit type annotations occasionally needed  Java: everything explicit; type inference occasionally possible
  • 45.  Type annotations:  Implicit = the type of a fresh binder is inferred f x = ...  Explicit = each binder is given a type at its binding site void f( int x ) { ... }  Reason:  Generics alone => type engine generates equality constraints, which it can solve  Subtyping => type engine generates subtyping constraints, which it cannot solve (uniquely)
  • 46. eqList :: [Int] -> [Int] -> Bool eqList [] [] = True eqList (x:xs) (y:ys) = x==y && eqList xs ys eqList _ _ = False Here we know that the two arguments have exactly the same type class Eq a where (==) :: a -> a -> Bool instance Eq a => Eq [a] where (==) [] [] = True (==) (x:xs) (y:ys) = x==y && xs == ys (==) _ _ = False
  • 47. Result: any super- Argument: any sub- type of INum type of INum  In Java (ish): INum inc( INum x ) Result has  In Haskell: precisely same type as argument inc :: Num a => a -> a  Compare... x::Float x::Float ...(x.inc)... ...(inc x)... INum Float
  • 48.  In practice, because many operations work by side effect, result contra-variance doesn‟t matter too much None of this x.setColour(Blue); changes x‟s type x.setPosition(3,4);  In a purely-functional world, where setColour, setPosition return a new x, result contra- variance might be much more important  F#‟s immutable libraries don‟t use subclassing (binary methods big issue here too; eg set union)
  • 49.  Java and C# both (now) support constrained generics A inc<A>( A x) where A:INum { ...blah... }  Very like inc :: Num a => a -> a  (but little used in practice, I believe)
  • 50. Legal iff T is only returned by interface IEnumerator<out T> { methods, but not T Current; passed to a bool MoveNext(); method, } nor side- effected  Why? So that this works m( IEnumerator<Control> ) IEnumerator<Button> b ...m(b)...  Button is a subtype of Control, so  IEnumerator<Button> is a subtype of IEnumerator<Control>
  • 51.  OOP: must embrace variance  Side effects => invariance  Generics: type parameters are co/contra/invariant (Java wildcards, C#4.0 variance annotations)  Interaction with higher kinds? class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b (Only Scala can do this, and it‟s very tricky!)  Variance simply does not arise in Haskell.  And we need constraint polymorphism anyway!
  • 52. In a language with • Generics • Constrained polymorphism do you (really) need subtyping too?
  • 54.  The power of the dot  IDE magic  overload short function names  That is: Use the type of the first (self) argument to (a) “x.”: display candidate functions (b)“x.reset”: fix which “reset” you mean  (Yes there is more: use argument syntax to further narrow which function you mean.)
  • 55.  Curiously, this is not specific to OOP, or to sub-typing, or to dynamic dispatch  Obvious thought: steal this idea and add this to Haskell module M where import Button -- reset :: Button -> IO () import Canvas -- reset :: Canvas -> IO () fluggle :: Button -> ... fluggle b = ...(b.reset)...
  • 56.  OOP lets you have a collection of heterogeneous objects void f( Shape[] x ); a::Circle b::Rectangle ....f (new Shape[] {a, b})...
  • 57. void f( Shape[] x ); a::Circle b::Rectangle ....f (new Shape[] {a, b})...  You can encode this in Haskell, although it is slightly clumsy data Shape where MkShape :: Shapely a => a -> Shape a :: Circle b :: Rectangle ....(f [MkShape a, MkShape b])...
  • 58.  The ability to make run-time type tests is hugely important in OOP.  We have (eventually) figured out to do this in Haskell: cast :: (Typeable a, Typeable b) => a -> Maybe b class Typeable a where typeOf :: a -> TypeRep instance Typeable Bool where typeOf _ = MkTypeRep “Bool” [] instance Typeable a => Typeable [a] where typeOf xs = MkTypeRep “List” [typeOf (head xs) ]
  • 59. New developments in type classes
  • 60. plusInt :: Int -> Int -> Int plusFloat :: Float -> Float -> Float intToFloat :: Int -> Float class GNum a b where (+) :: a -> b -> ??? instance GNum Int Int where (+) x y = plusInt x y instance GNum Int Float where (+) x y = plusFloat (intToFloat x) y test1 = (4::Int) + (5::Int) test2 = (4::Int) + (5::Float)
  • 61. class GNum a b where (+) :: a -> b -> ???  Result type of (+) is a function of the argument types SumTy is an associated type of class GNum a b where class GNum type SumTy a b :: * (+) :: a -> b -> SumTy a b  Each method gets a type signature  Each associated type gets a kind signature
  • 62. class GNum a b where type SumTy a b :: * (+) :: a -> b -> SumTy a b  Each instance declaration gives a “witness” for SumTy, matching the kind signature instance GNum Int Int where type SumTy Int Int = Int (+) x y = plusInt x y instance GNum Int Float where type SumTy Int Float = Float (+) x y = plusFloat (intToFloat x) y
  • 63. class GNum a b where type SumTy a b :: * instance GNum Int Int where type SumTy Int Int = Int instance GNum Int Float where type SumTy Int Float = Float  SumTy is a type-level function  The type checker simply rewrites  SumTy Int Int --> Int  SumTy Int Float --> Float whenever it can  But (SumTy t1 t2) is still a perfectly good type, even if it can‟t be rewritten. For example: data T a b = MkT a b (SumTy a b)
  • 64.  Inspired by associated types from OOP  Fit beautifully with type classes  Push the type system a little closer to dependent types, but not too close!  Generalise “functional dependencies”  ...still developing...
  • 65.  It‟s a complicated world.  Rejoice in diversity. Learn from the competition.  What can Haskell learn from OOP?  The power of the dot (IDE, name space control)  What can OOP learn from Haskell?  The big question for me is: once we have wholeheartedly adopted generics, do we still really need subtyping?
  • 66. Georg Weissenbacher [georg.weissenbacher@magd.ox.ac.uk] emailed this info: I mentioned today that James Gosling once said in an interview that he'd remove inheritance from Java if he could. Unfortunately, I can't find the original interview anymore, but there's a plethora of references to his statement on the web. For instance:  http://www.artima.com/intv/gosling34.html  http://www.javaworld.com/javaworld/jw-06-2001/j1-01-gosling.html?page=3 Venners: When asked what you might do differently if you could recreate Java, you've said you've wondered what it would be like to have a language that just does delegation. Gosling: Yes. [...] Venners: But by delegation, you do mean this object delegating to that object without it being a subclass? Gosling: Yes -- without an inheritance hierarchy. Rather than subclassing, just use pure interfaces. It's not so much that class inheritance is particularly bad. It just has problems. or:  http://www.newt.com/wohler/articles/james-gosling-ramblings-1.html There were two main questions: What would you take out? What would you put in? To the first, James evoked laughter with the single word: Classes. He would like to replace classes with delegation since doing delegation right would make inheritance go away. But it's like Whack-A-Mole (more laughter) since when you hit one mole, er, problem, another pops up. What they already had to take out was pre- and post-assertions.
  • 67.  See paper “Fun with type functions” [2009] on Simon PJ‟s home page
  • 68.  Consider a finite map, mapping keys to values  Goal: the data representation of the map depends on the type of the key  Boolean key: store two values (for F,T resp)  Int key: use a balanced tree  Pair key (x,y): map x to a finite map from y to value; ie use a trie!  Cannot do this in Haskell...a good program that the type checker rejects
  • 69. data Maybe a = Nothing | Just a Map is indexed by k, class Key k where but parametric in its data Map k :: * -> * second argument empty :: Map k v lookup :: k -> Map k v -> Maybe v ...insert, union, etc....
  • 70. data Maybe a = Nothing | Just a Optional value class Key k where for False data Map k :: * -> * empty :: Map k v lookup :: k -> Map k v -> Maybe v ...insert, union, etc.... Optional value for True instance Key Bool where data Map Bool v = MB (Maybe v) (Maybe v) empty = MB Nothing Nothing lookup True (MB _ mt) = mt lookup False (MB mf _) = mf
  • 71. data Maybe a = Nothing | Just a class Key k where Two-level data Map k :: * -> * map empty :: Map k v lookup :: k -> Map k v -> Maybe v Two-level ...insert, union, etc.... lookup instance (Key a, Key b) => Key (a,b) where data Map (a,b) v = MP (Map a (Map b v)) empty = MP empty lookup (ka,kb) (MP m) = case lookup ka m of Nothing -> Nothing Just m2 -> lookup kb m2 See paper for lists as keys: arbitrary depth tries
  • 72.  Goal: the data representation of the map depends on the type of the key  Boolean key: SUM  Pair key (x,y): PRODUCT  List key [x]: SUM of PRODUCT + RECURSION  Easy to extend to other types at will
  • 73. Client Server  addServer :: In Int (In Int (Out Int End)) addClient :: Out Int (Out Int (In Int End))  Type of the process expresses its protocol  Client and server should have dual protocols: run addServer addClient -- OK! run addServer addServer -- BAD!
  • 74. Client Server  addServer :: In Int (In Int (Out Int End)) addClient :: Out Int (Out Int (In Int End)) data In v p = In (v -> p) data Out v p = Out v p data End = End NB punning
  • 75. data In v p = In (v -> p) data Out v p = Out v p data End = End addServer :: In Int (In Int (Out Int End)) addServer = In (¥x -> In (¥y -> Out (x + y) End))  Nothing fancy here  addClient is similar
  • 76. run :: ??? -> ??? -> End A process A co-process class Process p where type Co p run :: p -> Co p -> End  Same deal as before: Co is a type-level function that transforms a process type into its dual
  • 77. class Process p where data In v p = In (v -> p) type Co p data Out v p = Out v p run :: p -> Co p -> End data End = End instance Process p => Process (In v p) where type Co (In v p) = Out v (Co p) run (In vp) (Out v p) = run (vp v) p instance Process p => Process (Out v p) where type Co (Out v p) = In v (Co p) run (Out v p) (In vp) = run p (vp v) Just the obvious thing really
  • 78. HASKELL C#  A statically typed,  A statically typed  purely functional  object oriented language... language...  designed 20 years ago...  designed 10 years ago  by a distributed  by a tight-knit group committee...  based in industry  of pointy-headed  for real applications academics...  as a research language.