SlideShare a Scribd company logo
MonadReader: Less is More
Sukant Hajra / @shajra / CognitiveScale
May 27, 2017
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 1 / 17
For Time, Not Covered
Monads
Monad Transformer Basics
Monad Transformer Library (MTL)
Optics (Lenses)
Classy Optics
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 2 / 17
Some Motivation
Figure 1: An Internet Joke by Matt Parsons
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 3 / 17
Exercise 1
Which one?
A
1parseConfig `bind` runApp
B
1parseConfig >>= runApp
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 4 / 17
Exercise 2
A
The Bobolinks emDash begun emDash newLine Then I said softly to myself
emDash newLine openDoubleQuotes That must have been the Sun
closeDoubleQuotes exclamationPoint
B
The Bobolinks – begun –
Then I said softly to myself –
“That must have been the Sun”!
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 5 / 17
Is the Naïve also Simple?
1makeUser :: Config -> IO ()
2makeUser = ...
4getInsight :: Config -> IO ()
5getInsight = ...
7runApp :: Config -> IO ()
8runApp = do
9user <- makeUser
10insight <- getInsight(user)
11showInsight(user, insight)
Complaint
passing around a global Config everywhere
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 6 / 17
Classy Optics
Does this help?
1makeUser
2:: HasDbConfig c
3=> c -> IO ()
5getInsight
6:: (HasDbConfig c, HasMetricsConfig c)
7=> c -> IO ()
Complaint
IO lock-in
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 7 / 17
Sometimes libraries force our hand
1makeUser
2:: (HasDbConfig c, MonadIO m)
3=> c -> m ()
5getInsight
6:: (HasDbConfig c, HasMetricsConfig c, MonadIO m)
7=> c -> m ()
Some Complaints
It’s getting kind of busy
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 8 / 17
Is MTL worth it?
1makeUser
2:: (HasDbConfig c, MonadReader c m, MonadIO m)
3=> m ()
5getInsight
6:: ( HasDbConfig c, HasMetricsConfig c
7, MonadReader c m, MonadIO m )
8=> m ()
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 9 / 17
Make the API reflect how you think!
Component-centric type classes
1class MonadIO m => Db m where
2getDbConn :: m DbConn
4class Monad m => Metrics m where
5sendMetric :: Component -> Event -> m ()
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 10 / 17
Say what you mean. . .
No more, no less
1makeUser :: Db m => m ()
3getInsight :: (Db m, Metrics m) => m ()
Observations/Comments
Parses better at a glance
Implementations decisions are hidden/deferred
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 11 / 17
We’re not quite done
We still need an implementation
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3newtype App a = App { unApp :: ReaderT Config IO a }
4deriving
5( Functor , Applicative , Monad
6, MonadIO, MonadReader Config )
8instance Db App where
9getDbConn = ask >>= liftIO . libMakeDbConn . getDbUrl
11instance Metrics App where
12sendMetric c s = ask >>= liftIO . libSendMetric c s
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 12 / 17
This is not new
Is there a name for this?
“MTL-style”
“finally tagless”
Good example code
Katip (a Haskell logging library)
github.com/Soostone/katip/blob/master/katip/examples/example.hs
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 13 / 17
Thanks
Remember
Reward human beings
Don’t punish not-machines
Some techniques
Maximize comprehension at a glance.
Keep high-level ideas visible.
Reduce contrast of high-level with low-level ideas.
Whitespace matters.
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 14 / 17
Quick mention
Figure 2: My Employer
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 15 / 17
Extra Material Not Presented
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 16 / 17
Teaser for a follow-up discussion
Problems we’ve had with MonadError
MonadError locks error type
Need to call runExceptT to handle exceptions
Inconvenient with a nested stack
Requires intermediate error types for matching
We’ve ended up with this kind of code
1runApp :: (Db m, Metrics m) => ExceptT SomeAPIFault m ()
Experimenting with a new approach
Explicitly Typed Exceptions for Haskell by Josè Iborra
Seems promising in theory and small examples
Want to use more at scale before recommending
Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 17 / 17

More Related Content

Recently uploaded

May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
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
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
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)

May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
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
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
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
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

MonadReader: Less is More

  • 1. MonadReader: Less is More Sukant Hajra / @shajra / CognitiveScale May 27, 2017 Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 1 / 17
  • 2. For Time, Not Covered Monads Monad Transformer Basics Monad Transformer Library (MTL) Optics (Lenses) Classy Optics Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 2 / 17
  • 3. Some Motivation Figure 1: An Internet Joke by Matt Parsons Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 3 / 17
  • 4. Exercise 1 Which one? A 1parseConfig `bind` runApp B 1parseConfig >>= runApp Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 4 / 17
  • 5. Exercise 2 A The Bobolinks emDash begun emDash newLine Then I said softly to myself emDash newLine openDoubleQuotes That must have been the Sun closeDoubleQuotes exclamationPoint B The Bobolinks – begun – Then I said softly to myself – “That must have been the Sun”! Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 5 / 17
  • 6. Is the Naïve also Simple? 1makeUser :: Config -> IO () 2makeUser = ... 4getInsight :: Config -> IO () 5getInsight = ... 7runApp :: Config -> IO () 8runApp = do 9user <- makeUser 10insight <- getInsight(user) 11showInsight(user, insight) Complaint passing around a global Config everywhere Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 6 / 17
  • 7. Classy Optics Does this help? 1makeUser 2:: HasDbConfig c 3=> c -> IO () 5getInsight 6:: (HasDbConfig c, HasMetricsConfig c) 7=> c -> IO () Complaint IO lock-in Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 7 / 17
  • 8. Sometimes libraries force our hand 1makeUser 2:: (HasDbConfig c, MonadIO m) 3=> c -> m () 5getInsight 6:: (HasDbConfig c, HasMetricsConfig c, MonadIO m) 7=> c -> m () Some Complaints It’s getting kind of busy Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 8 / 17
  • 9. Is MTL worth it? 1makeUser 2:: (HasDbConfig c, MonadReader c m, MonadIO m) 3=> m () 5getInsight 6:: ( HasDbConfig c, HasMetricsConfig c 7, MonadReader c m, MonadIO m ) 8=> m () Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 9 / 17
  • 10. Make the API reflect how you think! Component-centric type classes 1class MonadIO m => Db m where 2getDbConn :: m DbConn 4class Monad m => Metrics m where 5sendMetric :: Component -> Event -> m () Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 10 / 17
  • 11. Say what you mean. . . No more, no less 1makeUser :: Db m => m () 3getInsight :: (Db m, Metrics m) => m () Observations/Comments Parses better at a glance Implementations decisions are hidden/deferred Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 11 / 17
  • 12. We’re not quite done We still need an implementation 1{-# LANGUAGE GeneralizedNewtypeDeriving #-} 3newtype App a = App { unApp :: ReaderT Config IO a } 4deriving 5( Functor , Applicative , Monad 6, MonadIO, MonadReader Config ) 8instance Db App where 9getDbConn = ask >>= liftIO . libMakeDbConn . getDbUrl 11instance Metrics App where 12sendMetric c s = ask >>= liftIO . libSendMetric c s Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 12 / 17
  • 13. This is not new Is there a name for this? “MTL-style” “finally tagless” Good example code Katip (a Haskell logging library) github.com/Soostone/katip/blob/master/katip/examples/example.hs Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 13 / 17
  • 14. Thanks Remember Reward human beings Don’t punish not-machines Some techniques Maximize comprehension at a glance. Keep high-level ideas visible. Reduce contrast of high-level with low-level ideas. Whitespace matters. Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 14 / 17
  • 15. Quick mention Figure 2: My Employer Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 15 / 17
  • 16. Extra Material Not Presented Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 16 / 17
  • 17. Teaser for a follow-up discussion Problems we’ve had with MonadError MonadError locks error type Need to call runExceptT to handle exceptions Inconvenient with a nested stack Requires intermediate error types for matching We’ve ended up with this kind of code 1runApp :: (Db m, Metrics m) => ExceptT SomeAPIFault m () Experimenting with a new approach Explicitly Typed Exceptions for Haskell by Josè Iborra Seems promising in theory and small examples Want to use more at scale before recommending Sukant Hajra / @shajra / CognitiveScale MonadReader: Less is More May 27, 2017 17 / 17