[Haskell-cafe] Static computation/inlining

Lennart Augustsson lennart at augustsson.net
Sun Oct 10 22:05:16 EDT 2010


I would not worry about doing that at runtime.
The only reliable way to make sure it happens at compile time that I
can think of would be some Template Haskell.
(Or some really deep magic with dictionaries.)

  -- Lennart

On Mon, Oct 11, 2010 at 3:51 AM, Alexander Solla <ajs at 2piix.com> wrote:
> Hi everybody,
> I'm working on a module that encodes "static" facts about "the real world".
>  For now, I'm working on an ISO 3166 compliant list of countries, country
> names, and country codes.  I've run into a bit of an optimization issue.
> There is a static bijective correspondence between countries and their
> codes.  In order to keep one just one "large" data structure representation
> as Haskell code, I encoded this bijection using a list.  I'm looking to
> write queries against this list, but it is rather tedious.  I figured I
> could make some Data.Maps to handle it for me.
>
>
> -- Country and ISOCountryCodes derive (Data, Eq, Ord, Show, Typeable)
> countries_and_iso_country_codes :: [ (Country, ISOCountryCode) ]
> countries_and_iso_country_codes =
>
>         [ ( Afghanistan                           , ISOCountryCode  AF
> AFG    (isoNumericCode 004) )
>         , ( AlandIslands                    , ISOCountryCode  AX    ALA
>  (isoNumericCode 248) )
>         , ( Albania                            , ISOCountryCode  AL    ALB
>  (isoNumericCode 008) )
> ...
>         , ( Zimbabwe , ISOCountryCode ZW ZWE (isoNumericCode 716) ) ]
> map_country_to_country_code :: Map Country ISOCountryCode
> map_country_to_country_code = fromList countries_and_iso_country_codes
> map_country_code_to_country :: Map ISOCountryCode Country
> map_country_code_to_country = fromList . fmap (\(a,b) -> (b, a)) $
> countries_and_iso_country_codes
> Is there anyway to instruct GHC (and maybe other compilers) to compute these
> maps statically? Are GHC and the other compilers smart enough to do it
> automatically? Although the list isn't huge, I would still rather get rid of
> the O(2*n) operation of turning it into maps at run-time. (Especially since
> some later list encodings like these might be enormous) What should I be
> looking into?
> Thanks
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list