lookup-tables- Statically generate lookup tables using Template Haskell.

Safe HaskellNone




generateLUT :: (Bounded a, Enum a, Storable b) => (a -> b) -> Q (TExp (a -> b)) Source

generateLUT f generates an expression representing a memoized version of f. The lookup table is generated at compile time and stored directly in the final executable. The generated code is unsafe if the Bounded and Enum instances are not law-abiding or if the Storable instance is crazy.

Due to the constraints of Template Haskell, the function to memoize must be defined in a different module.

Example usage:

module Foo where

import Data.Word

fImpl :: Word8 -> Double
fImpl w8 = fromIntegral w / 255
module Bar where

import Foo

f :: Word8 -> Double
f = $$(generateLUT fImpl)