SlideShare a Scribd company logo
1 of 58
Download to read offline
Of Harmony and Stinginess
Applicatives, Monads, and incremental library design
Joseph Tel Abrahamson
@sdbo / tel / jspha
July 2015
A story
First Iteration
$ cd company-app
$ ./app
*** Exception: CLIENT_ID: getEnv: does not exist (no
environment variable)
$ export CLIENT_ID=b114b7e51f0e6810efeacf80132670
$ ./app
*** Exception: CLIENT_SECRET: getEnv: does not exist
(no environment variable)
$ export CLIENT_SECRET=0400eb02db68230048010d39f63fef08
$ ./app
[log] Starting server
.
.
.
*** Exception: NETWORK_ID: getEnv: does not exist (no
environment variable)
$ ahoiyinyasetinoasyet
bash: ahoiyinyasetinoasyet: command not found
$ rm -rf /
We have got to be able to do better, right?
Transparent abstraction, to draw apart and disentangle
data Config =
Config
{ clientId :: String
, clientSecret :: String
, networkId :: String
} deriving ( Eq, Show )
getConfig :: IO Config
getConfig = do
id <- getEnv “CLIENT_ID”
secret <- getEnv “CLIENT_SECRET”
netid <- getEnv “NETWORK_ID”
return (Config id secret netid)
main :: IO ()
main = do
config <- getConfig
server <- Server.start config
{- ... -}
Server.bind config
A library
First Iteration
A type E a
A “computation” which reads from the
environment to produce a value of type a.
An API get :: String -> E String
Get a single key from the environment.
run :: E a -> IO a
Run the E a “computation” in IO to receive the
promised a value.
Instances instance Functor E
With this we can interpret Strings from the
environment as more sophisticated types
instance Monad E
With this we can compose calls to get together to
build larger types like Config
Applicative Monad<=
Instances instance Functor E
With this we can interpret Strings from the
environment as more sophisticated types
instance Monad E
With this we can compose calls to get together to
build larger types like Config
instance Applicative E
… well, Monad implies this so we might as well
throw it in there, too
data Config =
Config
{ clientId :: String
, clientSecret :: String
, networkId :: String
} deriving ( Eq, Show )
getConfig :: E Config
getConfig = do
id <- get “CLIENT_ID”
secret <- get “CLIENT_SECRET”
netid <- get “NETWORK_ID”
return (Config id secret netid)
main :: IO ()
main = do
config <- run getConfig
server <- Server.start config
{- ... -}
Server.bind config
Is this the right design to offer?
Let’s talk about types
zero :: Nat
succ :: Nat -> Nat
A type
An operation
unwind :: Nat -> (r -> r) -> (r -> r)
:: (r -> r) -> r -> (Nat -> r)
:: (r -> r, r) -> (Nat -> r)
unwind :: (r -> r, r) -> (Nat -> r)
unwind (succ, zero) n = {- ... -}
data Nat = Zero | Wind Nat
-- the data type
data Nat = Zero | Wind Nat
-- the introductory side of the API
zero :: Nat
wind :: Nat -> Nat
-- the elimination side of the API
unwind :: (r -> r, r) -> (Nat -> r)
unwind (wind, zero) n = {- ... -}
Things fit together
—- if we let (r ——> Nat)
unwind (wind, zero) :: Nat -> Nat
unwind (wind, zero) == id
Logical harmony
Gentzen Wittgenstein Dummett
Intros Elims
{Intros, elims, data decls} fit together
Harmony
{Intros, elims, data decls} of known data types fit together
Let’s talk about unknown types
Lets talk about libraries
Second Iteration
Library design is often API-first
concrete = we know everything
abstract = some parts are unknown
A type E a
A “computation” which reads from the
environment to produce a value of type a.
An API get :: String -> E String
Get a single key from the environment.
run :: E a -> IO a
Run the E a “computation” in IO to receive the
promised a value.
get run
E a
Is this the right design to offer?
My theory
As we discover the “right” API for a library we move
from offering unknown types to concrete ones and must
gradually move to respect logical harmony
A corollary
If we offer too much, too quickly on the side of elims or
intros then this lack of balance will eventually be felt
Another story
Second Iteration
$ cd company-app
$ ./app
*** Exception: Environment requires following vars:
CLIENT_ID, CLIENT_SECRET, NETWORK_ID, TIMEOUT.
$ ./app --help
Some Company, Inc THE APP
Usage: app
Environment:
CLIENT_ID: id for authentication handshake
CLIENT_SECRET: secret for authentication handshake
NETWORK_ID: name for server to listen on
TIMEOUT: milliseconds to wait
$ :)
bash: syntax error near unexpected token `)'
A type E a
A “computation” which reads from the
environment to produce a value of type a.
An API get :: String -> E String
Get a single key from the environment.
run :: E a -> IO a
Run the E a “computation” in IO to receive the
promised a value.
examine :: E a -> [String]
Extract from the computation all variables that
will be accessed when run.
Instances instance Functor E
With this we can interpret Strings from the
environment as more sophisticated types
instance Monad E
With this we can compose calls to get together to
build larger types like Config
instance Applicative E
… well, Monad implies this so we might as well
throw it in there, too
data E a =
E
{ examine :: [String]
, run :: IO a
}
data E a =
E
{ examine :: [String]
, run :: IO a
}
deriving instance Functor E
get :: String -> E String
get name = E { examine = [name], run = getEnv name }
data E a =
E
{ examine :: [String]
, run :: IO a
}
instance Applicative E where
pure a = E { examine = [], run = return a }
E ns1 io1 <*> ns2 io2 =
E (ns1 ++ ns2) (io1 <*> io2)
-- No monad.
E a ~ (Const [String] a, IO a)
(>>=) :: Const e a -> (a -> Const e b) -> Const e b
Const e >>= _ = Const e
Applicative Monad<=
Applicative T Monad T<=
Given a type T
Applicative Monad<
Monad run
examine
A library
Third Iteration
data E a =
E
{ examine :: [String]
, run :: IO a
} derving Functor
instance Applicative (E a) where
pure a = E [] (pure a)
E ns1 io1 <*> E ns2 io2 =
E (ns1 <> ns2) (io1 <*> io2)
get name = E [name] (getEnv name)
data Config =
Config
{ clientId :: String
, clientSecret :: String
, networkId :: String
, timeout :: Int
} deriving ( Eq, Show )
getConfig :: E Config
getConfig =
Config
<$> get “CLIENT_ID”
<*> get “CLIENT_SECRET”
<*> get “NETWORK_ID”
<*> fmap read (get “TIMEOUT”)
Harmony
Thank you
“Of Harmony and Stinginess”
Joseph Tel Abrahamson
@sdbo / tel / jspha.com
Questions?

More Related Content

What's hot

Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分bob_is_strange
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is hereSebastiano Armeli
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsMohammad Shaker
 
Python Yield
Python YieldPython Yield
Python Yieldyangjuven
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsBrian Moschel
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)David de Boer
 
An Intro To ES6
An Intro To ES6An Intro To ES6
An Intro To ES6FITC
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsP3 InfoTech Solutions Pvt. Ltd.
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016Manoj Kumar
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overviewhesher
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and InferenceRichard Fox
 

What's hot (20)

C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分
 
C++ L07-Struct
C++ L07-StructC++ L07-Struct
C++ L07-Struct
 
C++ L06-Pointers
C++ L06-PointersC++ L06-Pointers
C++ L06-Pointers
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+Operators
 
Python Yield
Python YieldPython Yield
Python Yield
 
Socket.io (part 1)
Socket.io (part 1)Socket.io (part 1)
Socket.io (part 1)
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Swift 2
Swift 2Swift 2
Swift 2
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
Don't do this
Don't do thisDon't do this
Don't do this
 
An Intro To ES6
An Intro To ES6An Intro To ES6
An Intro To ES6
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overview
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and Inference
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
 

Viewers also liked

Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...
Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...
Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...Steven Perrault
 
Eklavya - The voice coming from Margin
Eklavya - The voice coming from MarginEklavya - The voice coming from Margin
Eklavya - The voice coming from MarginBhumi Dangi
 
Resume Christian Cisneros Sanchez
Resume Christian Cisneros SanchezResume Christian Cisneros Sanchez
Resume Christian Cisneros SanchezChristian Cisneros
 
Paper 6 Victorian Literature.
Paper 6 Victorian Literature. Paper 6 Victorian Literature.
Paper 6 Victorian Literature. Bhumi Dangi
 
Cavitazione Medica Milano
Cavitazione Medica Milano
Cavitazione Medica Milano
Cavitazione Medica Milano verdantbaby4626
 
Final_Fiona Day - CV for -First Port
Final_Fiona Day - CV for -First PortFinal_Fiona Day - CV for -First Port
Final_Fiona Day - CV for -First PortFiona Day
 
Victimisation of Rosencrantz and Guildenstern
Victimisation of Rosencrantz and GuildensternVictimisation of Rosencrantz and Guildenstern
Victimisation of Rosencrantz and GuildensternBhumi Dangi
 
Paper.8 cultural studies
Paper.8 cultural studiesPaper.8 cultural studies
Paper.8 cultural studiesBhumi Dangi
 
hh2014_presentation2noBackup
hh2014_presentation2noBackuphh2014_presentation2noBackup
hh2014_presentation2noBackupJere Harrison
 
Character of Friday in 'Robinson Crusoe
Character of Friday in 'Robinson CrusoeCharacter of Friday in 'Robinson Crusoe
Character of Friday in 'Robinson CrusoeBhumi Dangi
 
Concept of Poetry- Wordsworth and Coleridge
Concept of Poetry- Wordsworth and ColeridgeConcept of Poetry- Wordsworth and Coleridge
Concept of Poetry- Wordsworth and ColeridgeBhumi Dangi
 
PAPER-5 ROMANTIC LITERATURE
PAPER-5 ROMANTIC LITERATUREPAPER-5 ROMANTIC LITERATURE
PAPER-5 ROMANTIC LITERATUREBhumi Dangi
 
Paper.7 criticism
Paper.7 criticismPaper.7 criticism
Paper.7 criticismBhumi Dangi
 

Viewers also liked (16)

Sena
SenaSena
Sena
 
Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...
Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...
Perrault & Hahn 2014 Addressing the Instability of DNA Nanostrctures in Tissu...
 
Eklavya - The voice coming from Margin
Eklavya - The voice coming from MarginEklavya - The voice coming from Margin
Eklavya - The voice coming from Margin
 
Resume Christian Cisneros Sanchez
Resume Christian Cisneros SanchezResume Christian Cisneros Sanchez
Resume Christian Cisneros Sanchez
 
Paper 6 Victorian Literature.
Paper 6 Victorian Literature. Paper 6 Victorian Literature.
Paper 6 Victorian Literature.
 
Cavitazione Medica Milano
Cavitazione Medica Milano
Cavitazione Medica Milano
Cavitazione Medica Milano
 
Final_Fiona Day - CV for -First Port
Final_Fiona Day - CV for -First PortFinal_Fiona Day - CV for -First Port
Final_Fiona Day - CV for -First Port
 
Victimisation of Rosencrantz and Guildenstern
Victimisation of Rosencrantz and GuildensternVictimisation of Rosencrantz and Guildenstern
Victimisation of Rosencrantz and Guildenstern
 
Paper.8 cultural studies
Paper.8 cultural studiesPaper.8 cultural studies
Paper.8 cultural studies
 
hh2014_presentation2noBackup
hh2014_presentation2noBackuphh2014_presentation2noBackup
hh2014_presentation2noBackup
 
ARR_Presentation
ARR_PresentationARR_Presentation
ARR_Presentation
 
Character of Friday in 'Robinson Crusoe
Character of Friday in 'Robinson CrusoeCharacter of Friday in 'Robinson Crusoe
Character of Friday in 'Robinson Crusoe
 
Sena
SenaSena
Sena
 
Concept of Poetry- Wordsworth and Coleridge
Concept of Poetry- Wordsworth and ColeridgeConcept of Poetry- Wordsworth and Coleridge
Concept of Poetry- Wordsworth and Coleridge
 
PAPER-5 ROMANTIC LITERATURE
PAPER-5 ROMANTIC LITERATUREPAPER-5 ROMANTIC LITERATURE
PAPER-5 ROMANTIC LITERATURE
 
Paper.7 criticism
Paper.7 criticismPaper.7 criticism
Paper.7 criticism
 

Similar to Of Harmony and Stinginess: Applicative, Monad, and iterative library design

From Ruby to Node.js
From Ruby to Node.jsFrom Ruby to Node.js
From Ruby to Node.jsjubilem
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechtureAnatoly Bubenkov
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Datagreenwop
 
fog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the CloudWesley Beary
 
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)Wesley Beary
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015Fernando Daciuk
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonManageIQ
 
Tasks: you gotta know how to run them
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run themFilipe Ximenes
 
Who pulls the strings?
Who pulls the strings?Who pulls the strings?
Who pulls the strings?Ronny
 
Ato2019 weave-services-istio
Ato2019 weave-services-istioAto2019 weave-services-istio
Ato2019 weave-services-istioLin Sun
 
Weave Your Microservices with Istio
Weave Your Microservices with IstioWeave Your Microservices with Istio
Weave Your Microservices with IstioAll Things Open
 
All Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istioAll Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istioLin Sun
 
Writing Redis in Python with asyncio
Writing Redis in Python with asyncioWriting Redis in Python with asyncio
Writing Redis in Python with asyncioJames Saryerwinnie
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpmsom_nangia
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpmwilburlo
 

Similar to Of Harmony and Stinginess: Applicative, Monad, and iterative library design (20)

Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
From Ruby to Node.js
From Ruby to Node.jsFrom Ruby to Node.js
From Ruby to Node.js
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
 
fog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloud
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++
 
Book
BookBook
Book
 
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
Plack - LPW 2009
Plack - LPW 2009Plack - LPW 2009
Plack - LPW 2009
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
 
Tasks: you gotta know how to run them
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run them
 
Who pulls the strings?
Who pulls the strings?Who pulls the strings?
Who pulls the strings?
 
Ato2019 weave-services-istio
Ato2019 weave-services-istioAto2019 weave-services-istio
Ato2019 weave-services-istio
 
Weave Your Microservices with Istio
Weave Your Microservices with IstioWeave Your Microservices with Istio
Weave Your Microservices with Istio
 
All Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istioAll Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istio
 
Writing Redis in Python with asyncio
Writing Redis in Python with asyncioWriting Redis in Python with asyncio
Writing Redis in Python with asyncio
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
 
Psgi Plack Sfpm
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
 

Recently uploaded

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
(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
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
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
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 

Recently uploaded (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
(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...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
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...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 

Of Harmony and Stinginess: Applicative, Monad, and iterative library design

  • 1. Of Harmony and Stinginess Applicatives, Monads, and incremental library design Joseph Tel Abrahamson @sdbo / tel / jspha July 2015
  • 3. $ cd company-app $ ./app *** Exception: CLIENT_ID: getEnv: does not exist (no environment variable) $ export CLIENT_ID=b114b7e51f0e6810efeacf80132670 $ ./app *** Exception: CLIENT_SECRET: getEnv: does not exist (no environment variable) $ export CLIENT_SECRET=0400eb02db68230048010d39f63fef08 $ ./app [log] Starting server . . . *** Exception: NETWORK_ID: getEnv: does not exist (no environment variable) $ ahoiyinyasetinoasyet bash: ahoiyinyasetinoasyet: command not found $ rm -rf /
  • 4. We have got to be able to do better, right?
  • 5. Transparent abstraction, to draw apart and disentangle
  • 6. data Config = Config { clientId :: String , clientSecret :: String , networkId :: String } deriving ( Eq, Show ) getConfig :: IO Config getConfig = do id <- getEnv “CLIENT_ID” secret <- getEnv “CLIENT_SECRET” netid <- getEnv “NETWORK_ID” return (Config id secret netid)
  • 7. main :: IO () main = do config <- getConfig server <- Server.start config {- ... -} Server.bind config
  • 9. A type E a A “computation” which reads from the environment to produce a value of type a. An API get :: String -> E String Get a single key from the environment. run :: E a -> IO a Run the E a “computation” in IO to receive the promised a value.
  • 10. Instances instance Functor E With this we can interpret Strings from the environment as more sophisticated types instance Monad E With this we can compose calls to get together to build larger types like Config
  • 12. Instances instance Functor E With this we can interpret Strings from the environment as more sophisticated types instance Monad E With this we can compose calls to get together to build larger types like Config instance Applicative E … well, Monad implies this so we might as well throw it in there, too
  • 13. data Config = Config { clientId :: String , clientSecret :: String , networkId :: String } deriving ( Eq, Show ) getConfig :: E Config getConfig = do id <- get “CLIENT_ID” secret <- get “CLIENT_SECRET” netid <- get “NETWORK_ID” return (Config id secret netid)
  • 14. main :: IO () main = do config <- run getConfig server <- Server.start config {- ... -} Server.bind config
  • 15. Is this the right design to offer?
  • 17. zero :: Nat succ :: Nat -> Nat A type An operation
  • 18. unwind :: Nat -> (r -> r) -> (r -> r) :: (r -> r) -> r -> (Nat -> r) :: (r -> r, r) -> (Nat -> r) unwind :: (r -> r, r) -> (Nat -> r) unwind (succ, zero) n = {- ... -}
  • 19. data Nat = Zero | Wind Nat
  • 20. -- the data type data Nat = Zero | Wind Nat -- the introductory side of the API zero :: Nat wind :: Nat -> Nat -- the elimination side of the API unwind :: (r -> r, r) -> (Nat -> r) unwind (wind, zero) n = {- ... -}
  • 21. Things fit together —- if we let (r ——> Nat) unwind (wind, zero) :: Nat -> Nat unwind (wind, zero) == id
  • 23. Intros Elims {Intros, elims, data decls} fit together
  • 25. {Intros, elims, data decls} of known data types fit together
  • 26. Let’s talk about unknown types
  • 27. Lets talk about libraries Second Iteration
  • 28. Library design is often API-first
  • 29. concrete = we know everything
  • 30. abstract = some parts are unknown
  • 31. A type E a A “computation” which reads from the environment to produce a value of type a. An API get :: String -> E String Get a single key from the environment. run :: E a -> IO a Run the E a “computation” in IO to receive the promised a value.
  • 33. Is this the right design to offer?
  • 34. My theory As we discover the “right” API for a library we move from offering unknown types to concrete ones and must gradually move to respect logical harmony
  • 35. A corollary If we offer too much, too quickly on the side of elims or intros then this lack of balance will eventually be felt
  • 37. $ cd company-app $ ./app *** Exception: Environment requires following vars: CLIENT_ID, CLIENT_SECRET, NETWORK_ID, TIMEOUT. $ ./app --help Some Company, Inc THE APP Usage: app Environment: CLIENT_ID: id for authentication handshake CLIENT_SECRET: secret for authentication handshake NETWORK_ID: name for server to listen on TIMEOUT: milliseconds to wait $ :) bash: syntax error near unexpected token `)'
  • 38. A type E a A “computation” which reads from the environment to produce a value of type a. An API get :: String -> E String Get a single key from the environment. run :: E a -> IO a Run the E a “computation” in IO to receive the promised a value. examine :: E a -> [String] Extract from the computation all variables that will be accessed when run.
  • 39. Instances instance Functor E With this we can interpret Strings from the environment as more sophisticated types instance Monad E With this we can compose calls to get together to build larger types like Config instance Applicative E … well, Monad implies this so we might as well throw it in there, too
  • 40.
  • 41. data E a = E { examine :: [String] , run :: IO a }
  • 42. data E a = E { examine :: [String] , run :: IO a } deriving instance Functor E get :: String -> E String get name = E { examine = [name], run = getEnv name }
  • 43. data E a = E { examine :: [String] , run :: IO a } instance Applicative E where pure a = E { examine = [], run = return a } E ns1 io1 <*> ns2 io2 = E (ns1 ++ ns2) (io1 <*> io2)
  • 45. E a ~ (Const [String] a, IO a)
  • 46.
  • 47. (>>=) :: Const e a -> (a -> Const e b) -> Const e b
  • 48. Const e >>= _ = Const e
  • 50. Applicative T Monad T<= Given a type T
  • 54. data E a = E { examine :: [String] , run :: IO a } derving Functor instance Applicative (E a) where pure a = E [] (pure a) E ns1 io1 <*> E ns2 io2 = E (ns1 <> ns2) (io1 <*> io2) get name = E [name] (getEnv name)
  • 55. data Config = Config { clientId :: String , clientSecret :: String , networkId :: String , timeout :: Int } deriving ( Eq, Show ) getConfig :: E Config getConfig = Config <$> get “CLIENT_ID” <*> get “CLIENT_SECRET” <*> get “NETWORK_ID” <*> fmap read (get “TIMEOUT”)
  • 58. “Of Harmony and Stinginess” Joseph Tel Abrahamson @sdbo / tel / jspha.com Questions?