开发者

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜