Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 336
gatoatigrado Avatar asked May 25 '11 09:05

gatoatigrado


1 Answers

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.

like image 91
sclv Avatar answered Oct 23 '22 12:10

sclv