Parametric Polymorphism in Haskell

Parametric Polymorphism in Haskell

Parametric Polymorphism in Haskell Ben Deane 30th October 2014 Ben Deane Parametric Polymorphism in Haskell 30th October 2014 1 / 15 Disclaimer I suspect there are several people in the audience who know more about this than I do! This is what I think I know. (Broadly on the topic of Parametric Polymorphism.) Ben Deane Parametric Polymorphism in Haskell 30th October 2014 2 / 15 Universal Existential? Compile-time Runtime (also) C++ templates, Classical Java generics ("normal" OO) Two kinds of polymorphism Parametric Ad-hoc Type variables Type classes (a, b, etc) (Eq, Num, etc) Ben Deane Parametric Polymorphism in Haskell 30th October 2014 3 / 15 Compile-time Runtime (also) C++ templates, Classical Java generics ("normal" OO) Two kinds of polymorphism Parametric Ad-hoc Type variables Type classes (a, b, etc) (Eq, Num, etc) Universal Existential? Ben Deane Parametric Polymorphism in Haskell 30th October 2014 3 / 15 C++ templates, Classical Java generics ("normal" OO) Two kinds of polymorphism Parametric Ad-hoc Type variables Type classes (a, b, etc) (Eq, Num, etc) Universal Existential? Compile-time Runtime (also) Ben Deane Parametric Polymorphism in Haskell 30th October 2014 3 / 15 Two kinds of polymorphism Parametric Ad-hoc Type variables Type classes (a, b, etc) (Eq, Num, etc) Universal Existential? Compile-time Runtime (also) C++ templates, Classical Java generics ("normal" OO) Ben Deane Parametric Polymorphism in Haskell 30th October 2014 3 / 15 Polymorphic Datatypes For example. 1 data Maybea= Nothing| Justa 2 3 data Lista= Nil| Consa(List a) 4 5 data Eitherab= Lefta| Rightb Ben Deane Parametric Polymorphism in Haskell 30th October 2014 4 / 15 Polymorphic Functions For example. 1 reverse:: [a] -> [a] 2 3 fst:: (a,b) ->a 4 5 id::a ->a Ben Deane Parametric Polymorphism in Haskell 30th October 2014 5 / 15 Universally quantified Work over all types Assume nothing behaviour-wise Parametricity Intuitively, all instances act the same way Theorems for free eg. reverse . map f , map f . reverse Ben Deane Parametric Polymorphism in Haskell 30th October 2014 6 / 15 Totality Partial functions 1 head:: [a] ->a 2 3 tail:: [a] -> [a] What happens when a is []? These are not total functions. They are undefined for some inputs. Ben Deane Parametric Polymorphism in Haskell 30th October 2014 7 / 15 Type Inference and Unification Unification: the process of solving a system of equations in type variables. and Inference: Why you thought you meant Int -> Int but the compiler knows you really meant Num a => a -> a. Ben Deane Parametric Polymorphism in Haskell 30th October 2014 8 / 15 Almost nothing! We know it has to be id. What can we say about Int -> Int? Even though we know more about Int than about a! An Odd Thought What can we say about a -> a? Ben Deane Parametric Polymorphism in Haskell 30th October 2014 9 / 15 Almost nothing! What can we say about Int -> Int? Even though we know more about Int than about a! An Odd Thought What can we say about a -> a? We know it has to be id. Ben Deane Parametric Polymorphism in Haskell 30th October 2014 9 / 15 Almost nothing! Even though we know more about Int than about a! An Odd Thought What can we say about a -> a? We know it has to be id. What can we say about Int -> Int? Ben Deane Parametric Polymorphism in Haskell 30th October 2014 9 / 15 Even though we know more about Int than about a! An Odd Thought What can we say about a -> a? We know it has to be id. What can we say about Int -> Int? Almost nothing! Ben Deane Parametric Polymorphism in Haskell 30th October 2014 9 / 15 An Odd Thought What can we say about a -> a? We know it has to be id. What can we say about Int -> Int? Almost nothing! Even though we know more about Int than about a! Ben Deane Parametric Polymorphism in Haskell 30th October 2014 9 / 15 Types as Sets The set of representable values How many values inhabit the type Characterised by cardinality of the set For example 0 ? (in some languages, void) 1 a -> a 2 Bool 264 Int Ben Deane Parametric Polymorphism in Haskell 30th October 2014 10 / 15 Sum Types and Product Types Sum types represent alternation For example a + b , Either a b 1 + a , Maybe a Ben Deane Parametric Polymorphism in Haskell 30th October 2014 11 / 15 Sum Types and Product Types Product types represent composition For example a * b , (a,b) a * 2 , (a,Bool) Ben Deane Parametric Polymorphism in Haskell 30th October 2014 12 / 15 What about function types? They equate to a power function ba , a -> b (For concrete types, not type variables) Ben Deane Parametric Polymorphism in Haskell 30th October 2014 13 / 15 From CIS194 2014 Week 4, Exercise 5 1 -- How many distinct functions inhabit this type? 2 ex5:: Bool -> Bool 3 -- Answer: 4 4 ex5= const True 5 ex5_2= const False 6 ex5_3= id 7 ex5_4= complement 8 -- Using type algebra: 9 -- Bool -> Bool 10 -- => 2 -> 2 11 -- => 2^2 = 4 Ben Deane Parametric Polymorphism in Haskell 30th October 2014 14 / 15 Further Reading/Watching Theorems for Free - Philip Wadler The Algebra of Algebraic Data Types - Chris Taylor London HUG video Adventures with Types in Haskell - Simon Peyton-Jones Ben Deane Parametric Polymorphism in Haskell 30th October 2014 15 / 15.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    22 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us