Why is a Monad Like a Writing Desk?

@carinmeier Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Monad

Monday, March 12, 12

Monad

Monad Monday, March 12, 12

Monad

Monad Monday, March 12, 12

Monad

Monad

Monad Monday, March 12, 12

Monad

Monad

Monad

Monad Monday, March 12, 12

?

Monad

Monad

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

“Shall I be pure or impure?” - Wadler

Monday, March 12, 12

Pure Lazy Evaluation Equational Reasoning Shall I be pure or impure?

Impure State Error Handling Input/ Output

Monday, March 12, 12

Pure Haskell

Impure Clojure Scheme Monday, March 12, 12

“Is there a way to combine the indulgences of impurity and the blessings of purity?” - Wadler

Monday, March 12, 12

Monday, March 12, 12

Engenio Moggi

Monday, March 12, 12

Engenio Moggi Proposed way of structuring semantics to describe state, exceptions, etc in programming

Monday, March 12, 12

Engenio Moggi Proposed way of structuring semantics to describe state, exceptions, etc in programming Category Theory: Monads

Monday, March 12, 12

Monad (Category Theory) Also known as a Kleisi triple or triple Is an endofunctor, together with two natural transformations Used in theory of pairs of adjoint functions

Monday, March 12, 12

Monday, March 12, 12

You Are Late

Monday, March 12, 12

Monad Symposium in 15 min

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

TextA monad is just a

monoid in the category of endofunctors, What's the problem?

Monday, March 12, 12

A monad is just a Purity has its regrets. monoid in the category of endofunctors, what's the problem?

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Is it true that this could be a monadic return operation? Is

(defn return [v] (fn [] v))

Monday, March 12, 12

Is it true that this could be a monadic return operation? Is

(defn return [v] (fn [] v)) (return “jelly”)Text ;=> #

Monday, March 12, 12

Is it true that this could be a monadic return operation? Is

(defn return [v] (fn [] v)) (return “jelly”)Text ;=> # ((return “jelly”)) ;=> “jelly”

Monday, March 12, 12

Yes, because the return object is a function Is (container/ monadic value) that when executed is “jelly” Text (defn return [v] (fn [] v)) ((return “jelly”)) ;=> “jelly”

Monday, March 12, 12

Is it true that this could be a monadic return operation? Is

(defn return [v] v)

Monday, March 12, 12

Is it true that this could be a monadic return operation? Is

(defn return [v] v) (return “jelly”) ;=> jelly

Monday, March 12, 12

No, because the return object “jelly” is not a function Is (container/ monadic value) (defn return [v] v) (return “jelly”) ;=> jelly

Monday, March 12, 12

Is it true that this could be a monadic bind operation?

Is (defn bind [mv f] (f (mv)))

Monday, March 12, 12

Is it true that this could be a monadic bind operation?

Is (defn bind [mv f] (f (mv)))

(defn with-toast [s] (return (str "toast & " s)))

Monday, March 12, 12

Is it true that this could be a monadic bind operation?

Is (defn bind [mv f] (f (mv)))

(defn with-toast [s] (return (str "toast & " s))) (bind (return "jelly") with-toast) ;=> container/ monadic value

Monday, March 12, 12

Is it true that this could be a monadic bind operation?

Is (defn bind [mv f] (f (mv)))

(defn with-toast [s] (return (str "toast & " s))) (bind (return "jelly") with-toast) ;=> container/ monadic value ((bind (return "jelly") with-toast)) ;=> “toast & jelly”

Monday, March 12, 12

Yes, because it takes a monadic value, and applies a function and returns a monadic value

Is (defn bind [mv f] (f (mv)))

(defn with-toast [s] (return (str "toast & " s))) (bind (return "jelly") with-toast) ;=> container/ monadic value ((bind (return "jelly") with-toast)) ;=> “toast & jelly”

Monday, March 12, 12

Monday, March 12, 12

Can you write a monadic return function for yourself? Is

Monday, March 12, 12

Can you write a monadic return function for yourself? Is (return "me") ;=> monadic value ((return "me")) ;=> “me”

Monday, March 12, 12

(defn return [v] (fn [] v))

Can you write a function that makes you grow and returns Is a monadic value?

Monday, March 12, 12

Can you write a function that makes you grow and returns Is a monadic value? (defn grow [s] (return (str s (last s)))) (grow "me") ((grow "me"))

Monday, March 12, 12

;=> monadic value ;=> “mee"

Now, can you use a monadic bind operation to grow? Is

Monday, March 12, 12

Now, can you use a monadic bind operation to grow? Is (defn bind [mv f] (f (mv))) (defn m-grow [mv] (bind mv grow)) (m-grow (return "me")) ;=> m. val ((m-grow (return "me"))) ;=> “mee”

Monday, March 12, 12

Grow Bigger! Is

((m-grow (m-grow (m-grow (return "me"))))) ;=> “meeee”

Monday, March 12, 12

Grow Bigger! Is

((m-grow (m-grow (m-grow (return "me"))))) ;=> “meeee” ((-> (return "me") m-grow m-grow m-grow)) ;=> “meeee”

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Where you are Destination Directions

Monday, March 12, 12

(defn directions [start] (return (.concat start (if (> 0.5 (rand 1)) ": right" ": left"))))

Monday, March 12, 12

(defn directions [start] (return (.concat start (if (> 0.5 (rand 1)) ": right" ": left")))) (defn m-directions [mv] (bind mv directions))

Monday, March 12, 12

(defn directions [start] (return (.concat start (if (> 0.5 (rand 1)) ": right" ": left")))) (defn m-directions [mv] (bind mv directions)) ((-> (return “here”) m-directions m-directions)) ;=> "here: right: left"

Monday, March 12, 12

(defn directions [start] (return (.concat start (if (> 0.5 (rand 1)) ": right" ": left")))) (defn m-directions [mv] (bind mv directions)) ((-> (return “here”) m-directions m-directions)) ;=> "here: right: left" ((-> (return nil) m-directions m-directions)) ;=> NullPointerException!

Monday, March 12, 12

(defn bind [mv f] (f (mv)) (defn bind [mv f] (let [v (mv)] (if (nil? v) (return nil) (f v))))

Monday, March 12, 12

(defn bind [mv f] (f (mv)) (defn bind [mv f] (let [v (mv)] (if (nil? v) (return nil) (f v)))) ((-> (return nil) m-directions m-directions)) ;=> nil

Monday, March 12, 12

That way ->

Monday, March 12, 12

(

That way ->

Monday, March 12, 12

)

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

(defn return [v] (fn [] v)) (defn bind [mv f] (f (mv))) (defn m-tea [mv name] (bind mv (fn [v] (return (str v " and " name)))))

Monday, March 12, 12

(defn return [v] (fn [] v)) (defn bind [mv f] (f (mv))) (defn m-tea [mv name] (bind mv (fn [v] (return (str v " and " name))))) ((-> (return "me") (m-tea "you"))) ;=> "me and you"

Monday, March 12, 12

(defn return [v] (fn [s] [v s]))

Monday, March 12, 12

(defn return [v] (fn [s] [v s])) (defn bind [mv f] (fn [s] (let [[v sn] (mv s)] ((f v) sn))))

Monday, March 12, 12

(defn return [v] (fn [s] [v s])) (defn bind [mv f] (fn [s] (let [[v sn] (mv s)] ((f v) sn)))) (defn m-tea [mv name] (bind mv (fn [v] (return (str v " and " name))))) ((-> (return "me") (m-tea "you")) 10) ;=> ["me and you" 10]

Monday, March 12, 12

(defn take-sugar [mv] (bind mv (fn [v] (fn [s] [v (dec s)]))))

Monday, March 12, 12

(defn take-sugar [mv] (bind mv (fn [v] (fn [s] [v (dec s)])))) ((-> (return "me") (take-sugar) (m-tea "you")) 10) ;=> ["me and you" 9] ((-> (return "me") (take-sugar) (take-sugar) (m-tea "you")) 10) ;=> ["me and you" 8]

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Identity Monad Is

(defn return [v] (fn[] v)) (defn bind [mv f] (f (mv)))

(defn m-grow [w] (bind w grow)) ((-> (return "me") m-grow m-grow m-grow)) ;=> “meeee”

Monday, March 12, 12

Maybe Monad (defn return [v](fn[] v))

Is (defn bind [mv f] (let [v (mv)] (if (nil? v) (return nil) (f v)))) ((-> (return nil) m-directions m-directions)) ;=> nil

Monday, March 12, 12

State Monad (defn return [v] (fn [s] [v s]))

Is

(defn bind [mv f] (fn [s] (let [[v sn] (mv s)] ((f v) sn)))) (defn take-sugar [mv] (bind mv (fn [v] (fn [s] [v (dec s)])))) ((-> (return "me")(take-sugar) (m-tea "you")) 10) ;=> ["me and you" 9]

Monday, March 12, 12

Three Monad Laws 1) Left Unit - "return" acts as a neutral element of bind (bind (return v) f) ≡ (f v)

Monday, March 12, 12

Three Monad Laws 1) Left Unit - "return" acts as a neutral element of bind (bind (return v) f) ≡ (f v) (defn return [v] (fn [] v)) (defn bind [mv f] (f (mv))) (defn grow [s](return (str s (str (last s))))) ((bind (return "me") grow))

;=> “mee”

((grow "me"))

;=> “mee”

Monday, March 12, 12

Three Monad Laws 2) Right Unit - "return" acts as a neutral element of bind (bind mv return) ≡ mv

((bind (return "me") return)) ;=> "me" ((return "me"))

Monday, March 12, 12

;=> "me"

Three Monad Laws 2) Right Unit - "return" acts as a neutral element of bind (bind mv return) ≡ mv (defn return [v] (fn [] v)) (defn bind [mv f] (f (mv)) (defn grow [s](return (str s (str (last s))))) ((bind (return "me") return)) ;=> "me" ((return "me"))

Monday, March 12, 12

;=> "me"

Three Monad Laws 3) Associative - Binding two functions in succession is the same as binding one function that can be determined from them (bind (bind mv f) g) ≡ (bind mv (fn [x] (bind (f x) g))

Monday, March 12, 12

Three Monad Laws 3) Associative - Binding two functions in succession is the same as binding one function that can be determined from them (bind (bind mv f) g) ≡ (bind mv (fn [x] (bind (f x) g)) (defn return [v] (fn [] v)) (defn bind [mv f] (f (mv)) (defn grow [s](return (str s (str (last s))))) ((bind (bind (return "me") grow) grow)) ;=> "meee" ((bind (return "me") (fn [v] (bind (grow v) grow))))

Monday, March 12, 12

;=> "meee"

Monday, March 12, 12

Monday, March 12, 12

Monday, March 12, 12

Summary Many More Types of Monads

• IO • Collections • Continuation • Writer Etc ...

Monday, March 12, 12

Summary Have your cake and eat it too!

Inside functions pure and messy impure stuff on the outside

Monday, March 12, 12

Summary Practical Uses

• Sequential execution( parsing, fuzzing, web request processing)

• Reusable modular side effect processing (error handling/ logging)

• Keep your business logic clean in pure functions

Monday, March 12, 12

Summary Use Caution When Mixing Monads and Cheese

Monday, March 12, 12

Resources Philip Wadler: Monads for functional Programming Comprehending Monads Adam Smyczek: Introduction to Monads (Video) http://www.youtube.com/watch?v=ObR3qi4Guys Jim Duey: Monads in Clojure http://www.clojure.net/2012/02/02/Monads-in-Clojure/ Clojure Lib: clojure.algo.monads Konrad Hinsen Monday, March 12, 12

Photos http://www.flickr.com/photos/jepoirrier/1387560191/sizes/z/in/photostream/ - blocks http://www.flickr.com/photos/alspic/409365553/sizes/m/in/photostream/ - cake http://www.flickr.com/photos/moyogo/4884992/sizes/z/in/photostream/ - stairs http://www.flickr.com/photos/aeireono/369981290/ - forest path http://www.flickr.com/photos/gillpoole/238722066/ - tree branch http://www.flickr.com/photos/tofflerann/5936636472/sizes/m/in/photostream/ - high tea http://www.flickr.com/photos/gsfc/4399423264/ - astronaught http://www.flickr.com/photos/via/2115916059/ - sugar cubes

Monday, March 12, 12

carinmeier - GitHub

http://www.flickr.com/photos/jepoirrier/1387560191/sizes/z/in/photostream/ - blocks http://www.flickr.com/photos/alspic/409365553/sizes/m/in/photostream/ - cake.

4MB Sizes 4 Downloads 38 Views

Recommend Documents

GitHub
domain = meq.domain(10,20,0,10); cells = meq.cells(domain,num_freq=200, num_time=100); ...... This is now contaminator-free. – Observe the ghosts. Optional ...

GitHub
data can only be “corrected” for a single point on the sky. ... sufficient to predict it at the phase center (shifting ... errors (well this is actually good news, isn't it?)

Torsten - GitHub
Metrum Research Group has developed a prototype Pharmacokinetic/Pharmacodynamic (PKPD) model library for use in Stan 2.12. ... Torsten uses a development version of Stan, that follows the 2.12 release, in order to implement the matrix exponential fun

Untitled - GitHub
The next section reviews some approaches adopted for this problem, in astronomy and in computer vision gener- ... cussed below), we would question the sensitivity of a. Delaunay triangulation alone for capturing the .... computation to be improved fr

ECf000172411 - GitHub
Robert. Spec Sr Trading Supt. ENA West Power Fundamental Analysis. Timothy A Heizenrader. 1400 Smith St, Houston, Tx. Yes. Yes. Arnold. John. VP Trading.

Untitled - GitHub
Iwip a man in the middle implementation. TOR. Andrea Marcelli prof. Fulvio Risso. 1859. Page 3. from packets. PEX. CethernetDipo topo data. Private. Execution. Environment to the awareness of a connection. FROG develpment. Cethernet DipD tcpD data. P

BOOM - GitHub
Dec 4, 2016 - 3.2.3 Managing the Global History Register . ..... Put another way, instructions don't need to spend N cycles moving their way through the fetch ...

Supervisor - GitHub
When given an integer, the supervisor terminates the child process using. Process.exit(child, :shutdown) and waits for an exist signal within the time.

robtarr - GitHub
http://globalmoxie.com/blog/making-of-people-mobile.shtml. Saturday, October ... http://24ways.org/2011/conditional-loading-for-responsive-designs. Saturday ...

MY9221 - GitHub
The MY9221, 12-channels (R/G/B x 4) c o n s t a n t current APDM (Adaptive Pulse Density. Modulation) LED driver, operates over a 3V ~ 5.5V input voltage ...

fpYlll - GitHub
Jul 6, 2017 - fpylll is a Python (2 and 3) library for performing lattice reduction on ... expressiveness and ease-of-use beat raw performance.1. 1Okay, to ... py.test for testing Python. .... GSO complete API for plain Gram-Schmidt objects, all.

article - GitHub
2 Universidad Nacional de Tres de Febrero, Caseros, Argentina. ..... www-nlpir.nist.gov/projects/duc/guidelines/2002.html. 6. .... http://singhal.info/ieee2001.pdf.

PyBioMed - GitHub
calculate ten types of molecular descriptors to represent small molecules, including constitutional descriptors ... charge descriptors, molecular properties, kappa shape indices, MOE-type descriptors, and molecular ... The molecular weight (MW) is th

MOC3063 - GitHub
IF lies between max IFT (15mA for MOC3061M, 10mA for MOC3062M ..... Dual Cool™ ... Fairchild's Anti-Counterfeiting Policy is also stated on ourexternal website, ... Datasheet contains the design specifications for product development.

MLX90615 - GitHub
Nov 8, 2013 - of 0.02°C or via a 10-bit PWM (Pulse Width Modulated) signal from the device. ...... The chip supports a 2 wires serial protocol, build with pins SDA and SCL. ...... measure the temperature profile of the top of the can and keep the pe

Covarep - GitHub
Apr 23, 2014 - Gilles Degottex1, John Kane2, Thomas Drugman3, Tuomo Raitio4, Stefan .... Compile the Covarep.pdf document if Covarep.tex changed.

SeparableFilter11 - GitHub
1. SeparableFilter11. AMD Developer Relations. Overview ... Load the center sample(s) int2 i2KernelCenter ... Macro defines what happens at the kernel center.

Programming - GitHub
Jan 16, 2018 - The second you can only catch by thorough testing (see the HW). 5. Don't use magic numbers. 6. Use meaningful names. Don't do this: data("ChickWeight") out = lm(weight~Time+Chick+Diet, data=ChickWeight). 7. Comment things that aren't c

SoCsploitation - GitHub
Page 2 ... ( everything – {laptops, servers, etc.} ) • Cheap and low power! WTF is a SoC ... %20Advice_for_Shellcode_on_Embedded_Syst ems.pdf. Tell me more! ... didn't destroy one to have pretty pictures… Teridian ..... [email protected].

Datasheet - GitHub
Dec 18, 2014 - Compliant with Android K and L ..... 9.49 SENSORHUB10_REG (37h) . .... DocID026899 Rev 7. 10. Embedded functions register mapping .

Action - GitHub
Task Scheduling for Mobile Robots Using Interval Algebra. Mudrová and Hawes. .... W1. W2. W3. 0.9 action goto W2 from W1. 0.1. Why use an MDP? cost = 54 ...