Saturday, October 13, 2007

Monad.Reader

The Reader monad is a simple and easy-to-use structure from the Haskell library that solves a common problem.

Very often, it's the case that you have a lot of functions that all need to use the same values. For instance, you might be laying out some text on a page, and you need to keep the details of the page size around in all your layout algorithms. Typically, you handle this situation by adding a new parameter to all your functions which represents the state. So,


layoutPage :: Text -> Page


becomes


layoutPage :: PageLayoutParams -> Text -> Page


This works just fine, but it can get really ugly. Every single function gets a new parameter, which must be threaded through to every related function that it ever calls. This causes visual clutter, particularly for functions that don't actually use the parameter themselves but just pass it on.

A function written in the Reader monad is augmented with a hidden parameter. For the hypothetical example above, we would write:


import Control.Monad.Reader
type LayoutFunc = Reader PageLayoutParams

layoutPage :: Text -> LayoutFunc Page


Note that the Reader monad is partially applied here. The result, LayoutFunc, is a new monad that adds a parameter of type PageLayoutParams to any code that runs inside it.

So, now that we have this monad, how do we use it? The "ask" operation retrieves the parameter that's hidden in the monad:


ask :: Reader a a


or, specialized to LayoutFunc:


ask :: LayoutFunc PageLayoutParams


In order to remain close to the example in question, I will provide specialized type signatures for the rest of the library functions. You can find the generic type signatures in the Haskell library documentation for Control.Monad.Reader.

Suppose that layoutPage started out like this before it was moved into the monad:


layoutPage st txt =
let width = pageWidth st
...


In the Reader monad, this becomes:


layoutPage txt =
do st <- ask
let width = pageWidth st
...


As you can see, we've gone from one line of code to two. As this is clearly unacceptable for a clutter-reducing device, the Reader module provides a convenience function encapsulating the above pattern:


asks :: (PageLayoutParams -> a) -> PageLayoutFunc a


This allows us to simplify the monadic implementation to:


layoutPage txt =
do width <- asks pageWidth
...


It's quite common, when multiple functions share context information, to invoke a function in an altered context. For instance, we might have a layoutSubPage routine that changes the page width:


layoutSubPage st newWidth txt =
doLayout (st {pageWidth = newWidth} txt)


The Reader equivalent of this idiom is the function "local":


local :: (PageLayoutParams -> PageLayoutParams) -> LayoutFunc a -> LayoutFunc a


local executes an operation in a context modified by its first parameter. For instance,


layoutSubPage newWidth txt =
local (\st -> st { pageWidth = newWidth }) txt


Finally, to actually invoke a computation in the Reader monad, use runReader:


runReader :: LayoutFunc a -> PageLayoutParams -> a

main = showPage (runReader defaultPageParams someText)


For much more information on the Reader monad, see All About Monads, or the Haskell library documentation.

21 Comments:

At 6:11 PM, Blogger Alechandrina said...

You are the best...really... this was the best post I'd found, and I really really needed...thank u so much!

 
At 11:12 PM, Anonymous Anonymous said...

This is a great post; I'm definitely going to use the Reader monad in my code from now on, now that I understand it.

 
At 11:16 PM, Blogger Dave said...

Good tutorial. Thanks.

 
At 9:53 AM, Anonymous Anonymous said...

Good past, thanks!

 
At 4:40 PM, Anonymous Anonymous said...

I enjoy what you guys are up too. This kind of clever work and coverage!

Keep up the great works guys I've included you guys to my blogroll.

my webpage ... low fee cash advance
My website: very nice web site

 
At 6:56 PM, Anonymous Anonymous said...

Somebody necessarily help to make significantly articles I
might state. That is the first time I frequented your website page
and so far? I amazed with the research you
made to create this particular submit amazing.

Fantastic job!

My web-site - check our their site
Also see my site - natural breast enhancement pills that work

 
At 7:04 PM, Anonymous Anonymous said...

I think this is among the most significant information for me.
And i am glad reading your article. But want to remark on
few general things, The website style is perfect, the articles is really nice :
D. Good job, cheers

Stop by my blog :: check our their site

 
At 12:51 AM, Blogger Coach Factory said...

kate spade handbags, prada outlet, nike air max, louis vuitton, nike air max, louis vuitton outlet online, burberry outlet online, burberry outlet online, tiffany and co jewelry, michael kors outlet, gucci handbags, tory burch outlet, louis vuitton outlet, chanel handbags, jordan shoes, longchamp outlet online, nike shoes, louboutin shoes, red bottom shoes, coach outlet store online, kate spade outlet online, polo ralph lauren outlet, oakley vault, coach outlet, prada handbags, coach purses, louis vuitton handbags, longchamp handbags, christian louboutin outlet, oakley sunglasses, cheap oakley sunglasses, michael kors outlet online, michael kors outlet online, louis vuitton outlet, polo ralph lauren, christian louboutin shoes, michael kors outlet online, ray ban outlet, nike free, longchamp outlet, michael kors outlet store, michael kors outlet online, coach outlet, true religion, ray ban sunglasses

 
At 12:51 AM, Blogger Coach Factory said...

abercrombie and fitch, longchamp pas cher, north face, vans pas cher, nike free, nike air max, longchamp, tn pas cher, hollister, air max pas cher, michael kors canada, chaussure louboutin, oakley pas cher, sac vanessa bruno, ray ban uk, louis vuitton uk, hollister, new balance pas cher, lululemon, nike free pas cher, north face pas cher, scarpe hogan, nike trainers, air max, nike roshe, ralph lauren pas cher, barbour, nike huarache, burberry pas cher, guess pas cher, nike air max, true religion outlet, longchamp, lacoste pas cher, sac michael kors, ralph lauren, louis vuitton pas cher, michael kors uk, nike blazer pas cher, mulberry, nike air force, air jordan, hermes pas cher, louis vuitton, nike roshe run, timberland, sac louis vuitton, ray ban pas cher, converse pas cher, true religion outlet

 
At 12:52 AM, Blogger Coach Factory said...

herve leger, soccer jerseys, uggs outlet, jimmy choo shoes, lululemon outlet, nfl jerseys, canada goose pas cher, vans outlet, soccer shoes, roshe run, valentino shoes, moncler, rolex watches, chi flat iron, uggs on sale, canada goose, north face outlet, ghd, new balance outlet, north face jackets, mcm handbags, ugg soldes, moncler, canada goose uk, mont blanc pens, beats headphones, canada goose outlet, celine handbags, instyler ionic styler, ferragamo shoes, ugg boots, bottega veneta, ugg, moncler outlet, abercrombie and fitch, ugg outlet, wedding dresses, mac cosmetics, birkin bag, reebok outlet, canada goose outlet, insanity workout, moncler, giuseppe zanotti, asics shoes, hollister, canada goose outlet, babyliss pro, marc jacobs outlet, p90x workout

 
At 5:47 PM, Blogger Shijun Lin said...

shijun 6.4
air jordans
christian louboutin outlet
christian louboutin
pandora jewelry
kate spade diaper bag
jordan 4 retro
tory burch outlet
louis vuitton handbags
hollister clothing store
michael kors handbags
jordan 11s
mulberry uk
coach outlet online
oakley sunglasses
hollister kids
polo outlet
ralph lauren outlet
michael kors
jordan shoes
polo ralph lauren
prada bags
coach outlet store online
coach factory online
tory burch shoes
hollister outlet
adidas wings
louis vuitton outlet
louis vuitton handbags
coach factorty outlet
kate spade purses
michael kors uk
kate spade outlet
ray ban outlet
louis vuitton outlet
cheap lebron james shoes
christian louboutin sale
louis vuitton uk
mont blanc pen
michael kors outlet online
louis vuitton

 
At 11:20 PM, Blogger John said...

nike free runs
louis vuitton pas cher
nike roshe run women
air max 90
prada uk
christian louboutin
air max
snapbacks wholesale
adidas superstar
hollister co
cheap versace
nike air max 90
tods outlet
giuseppe zanotti sneakers
coach factory outlet
ugg sale
basketball shoes
timberland uk
jordan pas cher
canada goose outlet
coach outlet
ralph lauren polos shirts
abercrombie and fitch
retro jordans
louis vuitton borse
ghd
rolex replica watches
chaussure louboutin
adidas shoes
timberland boots
2015922yuanyuan

 
At 8:04 PM, Blogger 风骚达哥 said...

20151014 junda
Louis Vuitton Handbags Factory Store
Louis Vuitton Bags On Sale
Louis Vuitton Bags Outlet Store
cheap ugg boots
louis vuitton outlet
Michael Kors Outlet Real Handbags Online
ugg boots
Air Jordan 3 III Retro Free Shipping
louis vuitton outlet
celine
Christian Louis Vuitton Red Bottoms
coach factory outlet
ray ban sunglasses uk
Ugg Boots,Ugg Boots Outlet,Ugg Outlet,Cheap Uggs,Uggs On Sale,Ugg Boots Clearance,Uggs For Women
Coach Outlet Store Online Handbags Clearance
canada goose outlet
ugg boots outlet
Abercrombie And Fitch Kids Online
true religion outlet
cheap jordan shoes
New Louis Vuitton Handbags Outlet
cheap uggs
nfl jerseys
canada goose jackets
Abercrombie and Fitch Outlet Sale
louis vuitton outlet
canada goose outlet
uggs australia
michael kors outlet stores
Coach Factory Outlet Online Sale

 
At 12:21 AM, Blogger John said...

kobe bryant shoes
coach outlet online
kd shoes
concords 11
prada uk
oakley sunglasses
michael kors outlet
toms outlet
timberland boots
polo ralph lauren
tiffany and co
abercrombie fitch
michael kors outlet online
michael kors handbags
montblanc
michael kors outlet online
abercrombie
louis vuitton
uggs for men
uggs clearance
ugg boots
p90x workouts
ray ban sunglasses
louis vuitton
michael kors outlet
lebron james shoes
ray ban outlet
hollister clothing
coach factory outlet
cheap nfl jerseys
coach factory outlet
20151210yuanyuan

 
At 8:15 PM, Blogger oakleyses said...

uggs outlet, uggs on sale, ray ban sunglasses, ray ban sunglasses, louis vuitton, michael kors outlet online, oakley sunglasses wholesale, christian louboutin outlet, louis vuitton, uggs outlet, louis vuitton outlet, polo outlet, prada handbags, nike free, chanel handbags, longchamp outlet, michael kors outlet, replica watches, louis vuitton outlet, oakley sunglasses, michael kors outlet online, prada outlet, michael kors outlet online, longchamp outlet, burberry handbags, michael kors outlet, kate spade outlet, ray ban sunglasses, longchamp outlet, louis vuitton outlet, oakley sunglasses, nike air max, oakley sunglasses, replica watches, ugg boots, polo ralph lauren outlet online, ugg boots, gucci handbags, jordan shoes, cheap oakley sunglasses, michael kors outlet online, christian louboutin uk, burberry outlet, tory burch outlet, tiffany and co, christian louboutin shoes

 
At 8:17 PM, Blogger oakleyses said...

uggs outlet, uggs on sale, ray ban sunglasses, ray ban sunglasses, louis vuitton, michael kors outlet online, oakley sunglasses wholesale, christian louboutin outlet, louis vuitton, uggs outlet, louis vuitton outlet, polo outlet, prada handbags, nike free, chanel handbags, longchamp outlet, michael kors outlet, replica watches, louis vuitton outlet, oakley sunglasses, michael kors outlet online, prada outlet, michael kors outlet online, longchamp outlet, burberry handbags, michael kors outlet, kate spade outlet, ray ban sunglasses, longchamp outlet, louis vuitton outlet, oakley sunglasses, nike air max, oakley sunglasses, replica watches, ugg boots, polo ralph lauren outlet online, ugg boots, gucci handbags, jordan shoes, cheap oakley sunglasses, michael kors outlet online, christian louboutin uk, burberry outlet, tory burch outlet, tiffany and co, christian louboutin shoes

 
At 8:19 PM, Blogger oakleyses said...

beats by dre, nfl jerseys, nike trainers uk, mcm handbags, lululemon, reebok outlet, valentino shoes, wedding dresses, nike roshe run, celine handbags, ghd hair, ferragamo shoes, mont blanc pens, louboutin, giuseppe zanotti outlet, new balance shoes, timberland boots, oakley, soccer shoes, nike air max, iphone 5s cases, nike huaraches, iphone 6 plus cases, herve leger, iphone 6s plus cases, p90x workout, babyliss, vans outlet, north face outlet, iphone cases, jimmy choo outlet, iphone 6 cases, s6 case, abercrombie and fitch, bottega veneta, north face outlet, hermes belt, longchamp uk, ralph lauren, chi flat iron, insanity workout, iphone 6s cases, ipad cases, baseball bats, mac cosmetics, instyler, hollister, soccer jerseys, asics running shoes, hollister clothing

 
At 8:21 PM, Blogger oakleyses said...

doudoune moncler, pandora uk, moncler outlet, vans, converse outlet, montre pas cher, louis vuitton, moncler, moncler, canada goose, canada goose outlet, ugg uk, links of london, barbour uk, supra shoes, replica watches, lancel, nike air max, moncler, moncler, moncler outlet, coach outlet, wedding dresses, canada goose outlet, pandora jewelry, karen millen uk, ugg, marc jacobs, juicy couture outlet, converse, moncler uk, louis vuitton, ugg pas cher, swarovski, pandora jewelry, gucci, canada goose, canada goose uk, ugg,uggs,uggs canada, pandora charms, juicy couture outlet, louis vuitton, louis vuitton, ray ban, ugg,ugg australia,ugg italia, canada goose jackets, swarovski crystal, canada goose, hollister, thomas sabo, canada goose outlet, toms shoes

 
At 8:24 PM, Blogger oakleyses said...

michael kors, nike tn, ralph lauren uk, abercrombie and fitch uk, north face uk, ray ban pas cher, nike free uk, lululemon canada, michael kors, true religion jeans, coach outlet, coach outlet store online, hollister uk, sac longchamp pas cher, vans pas cher, nike blazer pas cher, louboutin pas cher, nike air max uk, michael kors pas cher, nike free run, nike air max uk, new balance, sac hermes, jordan pas cher, true religion outlet, replica handbags, nike roshe, longchamp pas cher, guess pas cher, true religion outlet, north face, polo ralph lauren, coach purses, hollister pas cher, oakley pas cher, timberland pas cher, air max, polo lacoste, nike air force, nike roshe run uk, burberry pas cher, converse pas cher, nike air max, sac vanessa bruno, mulberry uk, hogan outlet, michael kors outlet, true religion outlet, ray ban uk, kate spade

 
At 2:27 AM, Blogger dong dong23 said...

louis vuitton outlet
kate spade outlet
nike trainers
coach factory outlet
air jordan retro
christian louboutin shoes
true religion outlet
michael kors handbags
adidas shoes
michael kors outlet
jordan 3
hollister kids
marc jacobs
celine
ray ban outlet
celine
louis vuitton handbags
mont blanc
adidas uk
michael kors handbags
nike nfl jerseys
caoch outlet
michael kors outlet
louis vuitton bags
timberland outlet
kobe bryant shoes
tory burch flats
adidas outlet store
coach outlet
christian louboutin sale
adidas originals shoes
lebron james shoes
coach factory outlet
tory burch outlet
burberry outlet online
polo ralph lauren
christian louboutin outlet
fitflop sandals
oakley vault
cartier watches
20166.3wengdongdong

 
At 10:49 PM, Blogger raybanoutlet001 said...

adidas nmd
yeezy boost 350
michael kors outlet store
http://www.raybanglasses.in.net
christian louboutin outlet
kobe basketball shoes
tiffany jewellery
tiffany and co outlet
michael kors factory outlet
michael kors outlet online
true religion sale
christian louboutin shoes
fitflops clearance
adidas nmd
nike huaraches
huarache shoes
ralph lauren uk

 

Post a Comment

<< Home