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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With