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

ajb at spamcop.net ajb at spamcop.net
Mon Aug 18 22:54:50 EDT 2008


G'day all.

Quoting Bjorn Buckwalter <bjorn.buckwalter at gmail.com>:

> I'd store the constants in a data structure along the lines of:
>
>> data AstroData a = AstroData
>>   { mu_Earth    :: GravitationalParameter a
>>   , leapSeconds :: LeapSecondTable
>>   }
>
> 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.

In this situation, there isn't necessarily any shame in using a
top-level unsafePerformIO as long as it's well-hidden:

     module AstroData (AstroData(..), globalAstroData) where

     data AstroData = AstroData Int

         -- You really don't want this function inlined.  You also
         -- really don't want this function to be polymorphic.
     {-# NOINLINE globalAstroData #-}
     globalAstroData :: AstroData
     globalAstroData = unsafePerformIO $ do
         d <- return 42    -- Or whatever
         return (AstroData d)

Cheers,
Andrew Bromage


More information about the Haskell-Cafe mailing list