Fwd: [Haskell-cafe] Reader monad, implicit parameters,
or something else altogether?
Henning Thielemann
schlepptop at henning-thielemann.de
Mon Aug 18 15:47:18 EDT 2008
Bjorn Buckwalter schrieb:
> On Mon, Aug 18, 2008 at 2:02 PM, Henning Thielemann
>> <lemming at henning-thielemann.de> wrote:
>>
>> 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.).
>
> Right, and I'd evaluate it using e.g.:
>
> runReader escapeVelocity myAstroData
>
> But with implicit params I suppose I'd define (untested) e.g.:
>
> escapeVelocity :: (?astro :: AstroData a) => a
> escapeVelocity = ... mu ... where mu = muEarth ?astro
>
> To evaluate this I'd use:
>
> let ?astro = myAstroData in escapeVelocity
>
> Which is comparable to the Reader version (with the
> advantage/disadvantage of the body of 'escapeVelocity' not being
> monadic).
In my opinion the implicit parameters don't make things simpler, only
less portable, that's why I prefer the Reader monad.
> In retrospect I think I misunderstood what you were saying in you
> first email. I thought you were arguing that the monadic style would
> have an advantage over implicit params in the Planck problem. But you
> probably only meant to reemphasize the advantage (of either solution)
> over hard-coding constants...
indeed
More information about the Haskell-Cafe
mailing list