Search code examples
performancehaskelllazy-evaluationcompile-timetemplate-haskell

Force pre-computation of a constant


I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like,

myList = [(a, b), (c, d)]
...
map (f . fst) myList

take time in the fst call when I profile it (it does have 168M calls). The binary representation of myList is quite small, and could be, for example, copied into global memory [if this were a C program]. I'm compiling with -O3 -optc-O3 of course.

Thanks very much!

Generating Lift instances for a custom type

Any expression given to the lift call in sclv's answer must be an instance of Lift. There's a library named th-lift which will generate Lift instances for custom data types. See that package's documentation.


Solution

  • Generating a compile-time constant using Template Haskell:

    {-# LANGUAGE TemplateHaskell #-}
    import Language.Haskell.TH.Syntax(Lift(..))
    
    test = $(lift $ (map (*20) [0..100] :: [Int]))
    

    lift takes a Haskell value and lifts it into a TH Exp. The $() runs the enclosed quote, and splices the generated exp into the code at compile time.