SlideShare a Scribd company logo
ABSTRACT TYPECLASSES
— HOW TO DESIGN A
FUTURE-PROOF TYPECLASS
@mod_poppo
2019-11-09
CHANGES TO TYPECLASSES
• Functor-Applicative-Monad
• Semigroup-Monoid
• MonadFail
MIGRATION IS PAINFUL
• Adding a superclass
• data Foo a = …

instance Monad Foo where …

-- No instance for Applicative Foo
• Removing a method
• instance Monad Foo where …

fail x = …

-- 'fail' is not a (visible) method of class
'Monad'
• Sometimes #ifdef s cannot be avoided 😵
ABSTRACT TYPECLASS
• A data type whose constructors are hidden is abstract
• data Foo = {- hidden -}
• A similar concept could be employed to type classes: A type class
whose methods are hidden
• class Foo a where {- hidden -}
AN EXAMPLE OF ABSTRACT TYPECLASS
• An example of abstract
typeclass is
GHC.TypeLits.KnownN
at
• Its method natSing is
hidden
• The user can use this
class via

natVal :: KnownNat
n => proxy n ->
Integer
THE MERIT OF BEING ABSTRACT
• The author of the class can freely change the definition of the
class
• e.g. KnownNat has changed its representation since GHC 8.2
(Integer → Natural)
• The user doesn't need to care if its representation changed, as
long as natVal doesn't change
MAKING ‘Monad’ CLASS ABSTRACT
• module MyMonad (MyMonad, (>>=), return, fail)
where
• class MyMonad m where

-- the methods are not exported!

bind :: m a -> (a -> m b) -> m b

return_ :: a -> m a

fail_ :: String -> m a
• (>>=) :: MyMonad m => m a -> (a -> m b) -> m b

(>>=) = bind

return :: MyMonad m => a -> m a

return = return_

fail :: MyMonad m => String -> m a

fail = fail_
THE PROBLEM WITH ABSTRACT TYPECLASSES
• newtype Foo a = …

instance MyMonad Foo where

{- … what can I write here? 🤔 -}
• Third-party cannot write instances of such classes…
• …without using
• default definitions
• GeneralizedNewtypeDeriving
• …and yes, DerivingVia 😎
USING DerivingVia TO WRITE INSTANCES
• module MyMonad (…, MyMonadV1(..), ImplV1(..)) where
• class MyMonad m where …
• class MyMonadV1 m where

-- public!

bindV1 :: m a -> (a -> m b) -> m b

returnV1 :: a -> m a

failV1 :: String -> m a

failV1 = error
• newtype ImplV1 m a = ImplV1 (m a)
• instance MyMonadV1 m => MyMonad (ImplV1 m) where

-- hypothetical code; needs InstanceSigs to work

bind = coerce (bindV1 @m)

return_ = coerce (returnV1 @m)

fail_ = coerce (failV1 @m)
USING DerivingVia TO WRITE INSTANCES
• Want to write an instance of MyMonad? Use DerivingVia!
• import MyMonad
• newtype Identity a = Identity a

deriving MyMonad

via ImplV1 Identity
• instance MyMonadV1 Identity where

bindV1 (Identity x) f = f x

returnV1 x = Identity x
CHANGING THE CLASS HIERARCHY
• Now suppose you want to refactor MyMonad class
• class Applicative m => MyMonad m where

bind :: m a -> (a -> m b) -> m b
• class MyMonad m => MyMonadFail m where

fail_ :: String -> m a
• Can we avoid breakage?
BREAKAGE CAN BE AVOIDED
• …if we change ImplV1's instances accordingly!
• MyMonadV1 is kept as is!
• instance MyMonadV1 m => Functor (ImplV1 m) where …

instance MyMonadV1 m => Applicative (ImplV1 m) where …

instance MyMonadV1 m => MyMonad (ImplV1 m) where …

instance MyMonadV1 m => MyMonadFail (ImplV1 m) where …
• -- a dirty hack is needed (unfortunately)

instance {-# OVERLAPPABLE #-} MyMonadV1 m => Functor m
where

fmap = liftM

instance {-# OVERLAPPABLE #-} MyMonadV1 m =>
Applicative m where

pure = returnV1; (<*>) = ap
WHEN TO USE THIS TECHNIQUE?
• When writing a new library
• When you are sure that you would want to change details in the future
• My library unboxing-vector uses a technique similar to this:
• class … => Unboxable a where

type Rep a

-- hidden methods:

unboxingFrom :: a -> Rep a

unboxingTo :: Rep a -> a
• Users can use a newtype wrapper to derive an instance of
Unboxable with Generic
I'LL WRITE MORE DETAILS
IN A BLOG (HOPEFULLY
SOON…)

More Related Content

Similar to Abstract typeclasses

Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
rivierarb
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Rosie Jane Enomar
 
البرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكالالبرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكال
Mahmoud Alfarra
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
Abner Chih Yi Huang
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
Kent Ohashi
 
Android course session 3 ( OOP ) part 1
Android course session 3 ( OOP ) part 1Android course session 3 ( OOP ) part 1
Android course session 3 ( OOP ) part 1
Keroles M.Yakoub
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
MLG College of Learning, Inc
 
04inherit
04inherit04inherit
04inherit
Waheed Warraich
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
SivaSankari36
 
하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4
Kwang Yul Seo
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
LalfakawmaKh
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
Ayush Gupta
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
Abed Bukhari
 
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013 The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
GR8Conf
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
Justus Eapen
 

Similar to Abstract typeclasses (19)

Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
البرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكالالبرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكال
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
Android course session 3 ( OOP ) part 1
Android course session 3 ( OOP ) part 1Android course session 3 ( OOP ) part 1
Android course session 3 ( OOP ) part 1
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
04inherit
04inherit04inherit
04inherit
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013 The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
 

Recently uploaded

在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
Top 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptxTop 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptx
devvsandy
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
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
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 
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
 
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
 
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
 

Recently uploaded (20)

在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
Top 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptxTop 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptx
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
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
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 
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
 
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
 
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
 

Abstract typeclasses

  • 1. ABSTRACT TYPECLASSES — HOW TO DESIGN A FUTURE-PROOF TYPECLASS @mod_poppo 2019-11-09
  • 2. CHANGES TO TYPECLASSES • Functor-Applicative-Monad • Semigroup-Monoid • MonadFail
  • 3. MIGRATION IS PAINFUL • Adding a superclass • data Foo a = …
 instance Monad Foo where …
 -- No instance for Applicative Foo • Removing a method • instance Monad Foo where …
 fail x = …
 -- 'fail' is not a (visible) method of class 'Monad' • Sometimes #ifdef s cannot be avoided 😵
  • 4. ABSTRACT TYPECLASS • A data type whose constructors are hidden is abstract • data Foo = {- hidden -} • A similar concept could be employed to type classes: A type class whose methods are hidden • class Foo a where {- hidden -}
  • 5. AN EXAMPLE OF ABSTRACT TYPECLASS • An example of abstract typeclass is GHC.TypeLits.KnownN at • Its method natSing is hidden • The user can use this class via
 natVal :: KnownNat n => proxy n -> Integer
  • 6. THE MERIT OF BEING ABSTRACT • The author of the class can freely change the definition of the class • e.g. KnownNat has changed its representation since GHC 8.2 (Integer → Natural) • The user doesn't need to care if its representation changed, as long as natVal doesn't change
  • 7. MAKING ‘Monad’ CLASS ABSTRACT • module MyMonad (MyMonad, (>>=), return, fail) where • class MyMonad m where
 -- the methods are not exported!
 bind :: m a -> (a -> m b) -> m b
 return_ :: a -> m a
 fail_ :: String -> m a • (>>=) :: MyMonad m => m a -> (a -> m b) -> m b
 (>>=) = bind
 return :: MyMonad m => a -> m a
 return = return_
 fail :: MyMonad m => String -> m a
 fail = fail_
  • 8. THE PROBLEM WITH ABSTRACT TYPECLASSES • newtype Foo a = …
 instance MyMonad Foo where
 {- … what can I write here? 🤔 -} • Third-party cannot write instances of such classes… • …without using • default definitions • GeneralizedNewtypeDeriving • …and yes, DerivingVia 😎
  • 9. USING DerivingVia TO WRITE INSTANCES • module MyMonad (…, MyMonadV1(..), ImplV1(..)) where • class MyMonad m where … • class MyMonadV1 m where
 -- public!
 bindV1 :: m a -> (a -> m b) -> m b
 returnV1 :: a -> m a
 failV1 :: String -> m a
 failV1 = error • newtype ImplV1 m a = ImplV1 (m a) • instance MyMonadV1 m => MyMonad (ImplV1 m) where
 -- hypothetical code; needs InstanceSigs to work
 bind = coerce (bindV1 @m)
 return_ = coerce (returnV1 @m)
 fail_ = coerce (failV1 @m)
  • 10. USING DerivingVia TO WRITE INSTANCES • Want to write an instance of MyMonad? Use DerivingVia! • import MyMonad • newtype Identity a = Identity a
 deriving MyMonad
 via ImplV1 Identity • instance MyMonadV1 Identity where
 bindV1 (Identity x) f = f x
 returnV1 x = Identity x
  • 11. CHANGING THE CLASS HIERARCHY • Now suppose you want to refactor MyMonad class • class Applicative m => MyMonad m where
 bind :: m a -> (a -> m b) -> m b • class MyMonad m => MyMonadFail m where
 fail_ :: String -> m a • Can we avoid breakage?
  • 12. BREAKAGE CAN BE AVOIDED • …if we change ImplV1's instances accordingly! • MyMonadV1 is kept as is! • instance MyMonadV1 m => Functor (ImplV1 m) where …
 instance MyMonadV1 m => Applicative (ImplV1 m) where …
 instance MyMonadV1 m => MyMonad (ImplV1 m) where …
 instance MyMonadV1 m => MyMonadFail (ImplV1 m) where … • -- a dirty hack is needed (unfortunately)
 instance {-# OVERLAPPABLE #-} MyMonadV1 m => Functor m where
 fmap = liftM
 instance {-# OVERLAPPABLE #-} MyMonadV1 m => Applicative m where
 pure = returnV1; (<*>) = ap
  • 13. WHEN TO USE THIS TECHNIQUE? • When writing a new library • When you are sure that you would want to change details in the future • My library unboxing-vector uses a technique similar to this: • class … => Unboxable a where
 type Rep a
 -- hidden methods:
 unboxingFrom :: a -> Rep a
 unboxingTo :: Rep a -> a • Users can use a newtype wrapper to derive an instance of Unboxable with Generic
  • 14. I'LL WRITE MORE DETAILS IN A BLOG (HOPEFULLY SOON…)