[Haskell-cafe] Reader monad, implicit parameters, or something else altogether?

Henning Thielemann lemming at henning-thielemann.de
Mon Aug 18 14:02:30 EDT 2008


On Mon, 18 Aug 2008, Bjorn Buckwalter wrote:

> On Mon, Aug 18, 2008 at 11:16 AM, Henning Thielemann
> <lemming at henning-thielemann.de> wrote:
>>
>> On Mon, 18 Aug 2008, Bjorn Buckwalter wrote:
>>
>>> I would like to know if there is any consensus on what is the best way
>>> to make such a data structure accessible in pure functions. Passing it
>>> explicitly would be a mess. It seems that two options are to use
>>> either a Reader monad or implicit parameters. Using a Reader monad is
>>> straight forward enough though it requires writing/converting code
>>> in/to monadic style and adds some clutter to the formulae. It seems
>>> implicit parameters could be cleaner but I've seen them referred to as
>>> everything from evil to "just what you need" and rendering the Reader
>>> monad obsolete...
>>
>> I expect that you will get the same range of opinions as you got from your
>> search. As far as I know implicit parameters break referential transparency.
>>  http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue2/FunWithLinearImplicitParameters
>>  So I prefer Reader monad. The burden of converting to monadic style pays
>> off when you need to use the same code with different values for the
>> "constants". (E.g. find out for which value of the Planck constant the
>> universe collapses and for which it oscillates etc. :-)
>
> Love the example but could you elaborate a little on how monadic style
> helps with this? (This is probably a matter of it not being obvious to
> me what approach you would take to solving the problem.)

Instead of
   muEarth :: GravitationalParameter a
   muEarth = ???

   escapeVelocity :: a
   escapeVelocity = ... muEarth ...

you would write

   data AstroData a = AstroData
     { muEarth     :: GravitationalParameter a
     , leapSeconds :: LeapSecondTable
     }

   escapeVelocity :: Reader (AstroData a) a
   escapeVelocity =
      do mu <- asks muEarth
         return (... mu ...)

Even better you would introduce a newtype for Reader (AstroData a). This 
way you can add any monadic functionality later (Writer et.al.).


More information about the Haskell-Cafe mailing list