SlideShare a Scribd company logo
Parametricity
Shyamendra Solanki
Key Idea: Generalize Abstractions
 Generalize an abstraction because removing non-essential information
clarifies its independent meaning.
 Reuse is Accidental.
Parametricity: Definition
 parametricity is an abstract uniformity property enjoyed by parametrically
polymorphic functions, which captures the intuition that all instances of a
polymorphic function act the same way.
Types are documentation
 Parametricity is an efficient and reliable tool to assist code-readability to
assist non-trivial software in a team environment.
 A function is specified by its parametric type and its tests.
 Parametric Type: proof-positive
 Test: proof-negative
Observe the types
 def add10(n: Int): Int
What does the function do?
 def add10(n: Int): Int
What does the function do?
 def add10(n: Int): Int
 Theorem: it returns one of the 2^32 values of int.
 Otherwise, it would not have compiled.
What does the function do?
 def function(elements: List[Int]): List[Int]
What does the function do?
 def function(elements: List[Int]): List[Int]
 The number of potential implementations is HUGE.
 -> It can return list of prime numbers.
 -> It can add 17 to every 11th element.
 -> It can return empty list.
 -> It can concatenate list with itself.
What does the function do?
 def function(elements: List[Int]): List[Int]
 What if we make it polymorphic?
What does the function do?
 def function(elements: List[Int]): List[Int]
 i.e. Abstract over type of list.
What does the function do?
 def function[A](elements: List[A]): List[A]
 Theorem:
 Every element in output list comes from input list.
 Otherwise, it would not have compiled.
What does the function do?
 def function[A](elements: List[A]): List[A]
 Theorem:
 Every element in output list comes from input list.
 Otherwise, it would not have compiled.
 By adding freedom in types, we have constrained the implementations.
Parametricity works on language subset
 These things break parametricity:
 Null
 Exception
 isInstanceOf
 asInstanceOf
 Side effects
 toString, hashCode, equals
 Notify/wait
 classOf
 recursion
Observe the types
 def irrelevant[A, B](in: List[A]): List[B]
Observe the types
 def irrelevant[A, B](in: A): B
Observe the types
 def irrelevant[A](a: A): A
Observe the types
 def irrelevant[A](a: A): (A, A, A, A)
Observe the types
 def irrelevant[A](a, A: b: A): A
Observe the types
 def irrelevant[A](abcd:(A, A, A, A)): A
Observe the types
 def irrelevant[A](a: String, b: A): A
Observe the types
 def irrelevant[A, B](la: List[A], lb: List[B]): Option[A]
Observe the types
 def irrelevant[A, B](f: A => B, i: Int): A => (B, B)
Observe the types
 def irrelevant[A, B, C](f: A => B, g: B => C, a: A): C
Observe the types
 def irrelevant[A](maybe: Option[A]): List[A]
Observe the types
 def irrelevant[A](elements: List[A]): Int
Observe the types
 def irrelevant[A, B: Monoid](f: A => B, elements: List[A]): B
Observe the types
 def irrelevant[A, B, F: Monad](fa: F[A], fb: F[B]): F[B]
Observe the types
 def irrelevant[A, B, F: Functor](fab: F[A => B], a: A): F[B]
Types are documentation
 Use them to your advantage.

More Related Content

What's hot

Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
Raajendra M
 
Nullable type in C#
Nullable type in C#Nullable type in C#
Nullable type in C#
ZohaibAnwerChaudhry
 
Python basic operators
Python   basic operatorsPython   basic operators
Python basic operators
Learnbay Datascience
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
Operators in java
Operators in javaOperators in java
Operators in java
Muthukumaran Subramanian
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
ArraLafuente
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
Emmanuel Alimpolos
 
Ch02
Ch02Ch02
Functions in c
Functions in cFunctions in c
Functions in c
kalavathisugan
 
Typeclass
TypeclassTypeclass
Comparable vs comparator
Comparable vs comparatorComparable vs comparator
Comparable vs comparator
DigvijayKale9
 
Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)
Syed Arslan Rizvi
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressions
maznabili
 
Operators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETOperators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NET
Shyam Sir
 
8 if
8 if8 if
8 if
fyjordan9
 
C# operators
C# operatorsC# operators
Ch06
Ch06Ch06
C# chap 4
C# chap 4C# chap 4
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
Rakesh Roshan
 
5.functions
5.functions5.functions
5.functions
Hardik gupta
 

What's hot (20)

Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
Nullable type in C#
Nullable type in C#Nullable type in C#
Nullable type in C#
 
Python basic operators
Python   basic operatorsPython   basic operators
Python basic operators
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Ch02
Ch02Ch02
Ch02
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Typeclass
TypeclassTypeclass
Typeclass
 
Comparable vs comparator
Comparable vs comparatorComparable vs comparator
Comparable vs comparator
 
Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressions
 
Operators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETOperators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NET
 
8 if
8 if8 if
8 if
 
C# operators
C# operatorsC# operators
C# operators
 
Ch06
Ch06Ch06
Ch06
 
C# chap 4
C# chap 4C# chap 4
C# chap 4
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
5.functions
5.functions5.functions
5.functions
 

Similar to Parametricity

Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
Alexander Podkhalyuzin
 
Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)
Alexander Podkhalyuzin
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
Michal Bigos
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
amiable_indian
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
Bryan O'Sullivan
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
Rahul Budholiya
 
What Referential Transparency can do for you
What Referential Transparency can do for youWhat Referential Transparency can do for you
What Referential Transparency can do for you
Luka Jacobowitz
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
Taisuke Oe
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
IMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxIMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptx
lemonchoos
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
Andrew Raj
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
Ankita Shirke
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
Akshaya Arunan
 
Write a complete C programme to implement the following sorting algor.docx
 Write a complete C programme to implement the following sorting algor.docx Write a complete C programme to implement the following sorting algor.docx
Write a complete C programme to implement the following sorting algor.docx
ajoy21
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
www.ixxo.io
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
Daniel Sebban
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Python’s filter() function An Introduction to Iterable Filtering
Python’s filter() function An Introduction to Iterable FilteringPython’s filter() function An Introduction to Iterable Filtering
Python’s filter() function An Introduction to Iterable Filtering
Inexture Solutions
 
Typeclasses
TypeclassesTypeclasses
Typeclasses
ekalyoncu
 

Similar to Parametricity (20)

Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
 
Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)Under the hood of scala implicits (Scala eXchange 2014)
Under the hood of scala implicits (Scala eXchange 2014)
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
 
What Referential Transparency can do for you
What Referential Transparency can do for youWhat Referential Transparency can do for you
What Referential Transparency can do for you
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
IMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxIMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptx
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Write a complete C programme to implement the following sorting algor.docx
 Write a complete C programme to implement the following sorting algor.docx Write a complete C programme to implement the following sorting algor.docx
Write a complete C programme to implement the following sorting algor.docx
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Python’s filter() function An Introduction to Iterable Filtering
Python’s filter() function An Introduction to Iterable FilteringPython’s filter() function An Introduction to Iterable Filtering
Python’s filter() function An Introduction to Iterable Filtering
 
Typeclasses
TypeclassesTypeclasses
Typeclasses
 

Recently uploaded

原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 

Recently uploaded (20)

原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 

Parametricity

  • 2. Key Idea: Generalize Abstractions  Generalize an abstraction because removing non-essential information clarifies its independent meaning.  Reuse is Accidental.
  • 3. Parametricity: Definition  parametricity is an abstract uniformity property enjoyed by parametrically polymorphic functions, which captures the intuition that all instances of a polymorphic function act the same way.
  • 4. Types are documentation  Parametricity is an efficient and reliable tool to assist code-readability to assist non-trivial software in a team environment.  A function is specified by its parametric type and its tests.  Parametric Type: proof-positive  Test: proof-negative
  • 5. Observe the types  def add10(n: Int): Int
  • 6. What does the function do?  def add10(n: Int): Int
  • 7. What does the function do?  def add10(n: Int): Int  Theorem: it returns one of the 2^32 values of int.  Otherwise, it would not have compiled.
  • 8. What does the function do?  def function(elements: List[Int]): List[Int]
  • 9. What does the function do?  def function(elements: List[Int]): List[Int]  The number of potential implementations is HUGE.  -> It can return list of prime numbers.  -> It can add 17 to every 11th element.  -> It can return empty list.  -> It can concatenate list with itself.
  • 10. What does the function do?  def function(elements: List[Int]): List[Int]  What if we make it polymorphic?
  • 11. What does the function do?  def function(elements: List[Int]): List[Int]  i.e. Abstract over type of list.
  • 12. What does the function do?  def function[A](elements: List[A]): List[A]  Theorem:  Every element in output list comes from input list.  Otherwise, it would not have compiled.
  • 13. What does the function do?  def function[A](elements: List[A]): List[A]  Theorem:  Every element in output list comes from input list.  Otherwise, it would not have compiled.  By adding freedom in types, we have constrained the implementations.
  • 14. Parametricity works on language subset  These things break parametricity:  Null  Exception  isInstanceOf  asInstanceOf  Side effects  toString, hashCode, equals  Notify/wait  classOf  recursion
  • 15. Observe the types  def irrelevant[A, B](in: List[A]): List[B]
  • 16. Observe the types  def irrelevant[A, B](in: A): B
  • 17. Observe the types  def irrelevant[A](a: A): A
  • 18. Observe the types  def irrelevant[A](a: A): (A, A, A, A)
  • 19. Observe the types  def irrelevant[A](a, A: b: A): A
  • 20. Observe the types  def irrelevant[A](abcd:(A, A, A, A)): A
  • 21. Observe the types  def irrelevant[A](a: String, b: A): A
  • 22. Observe the types  def irrelevant[A, B](la: List[A], lb: List[B]): Option[A]
  • 23. Observe the types  def irrelevant[A, B](f: A => B, i: Int): A => (B, B)
  • 24. Observe the types  def irrelevant[A, B, C](f: A => B, g: B => C, a: A): C
  • 25. Observe the types  def irrelevant[A](maybe: Option[A]): List[A]
  • 26. Observe the types  def irrelevant[A](elements: List[A]): Int
  • 27. Observe the types  def irrelevant[A, B: Monoid](f: A => B, elements: List[A]): B
  • 28. Observe the types  def irrelevant[A, B, F: Monad](fa: F[A], fb: F[B]): F[B]
  • 29. Observe the types  def irrelevant[A, B, F: Functor](fab: F[A => B], a: A): F[B]
  • 30. Types are documentation  Use them to your advantage.