SlideShare a Scribd company logo
1 of 30
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 (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 FSharpDaniele Pozzobon
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... FunctionsMichal Bigos
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Twoamiable_indian
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Bryan O'Sullivan
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in cRahul 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 youLuka 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): Day1Taisuke 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.0Sheik Uduman Ali
 
IMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxIMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxlemonchoos
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III TermAndrew Raj
 
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.docxajoy21
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golangwww.ixxo.io
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaDaniel Sebban
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant 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 FilteringInexture Solutions
 

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

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Recently uploaded (20)

Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

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.