You must use Template Haskell . With TH, you can generate code programmatically at compile time. In this case, your mymodulus is actually a "template".
For example, we can rewrite your program as follows to compute your function statically. firstly, the main code, as usual, but instead of calling the module function, it calls a function whose body is a splice that will be generated at compile time:
{-
And the code to create the table is static:
{-# LANGUAGE TemplateHaskell #-} module Table where import Language.Haskell.TH import Language.Haskell.TH.Syntax genmodulus :: Int -> Q Exp genmodulus n = return $ CaseE (VarE (mkName "n")) [ Match (LitP (IntegerL i)) (NormalB (LitE (IntegerL (i `mod` base)))) [] | i <- [0..fromIntegral n] ] where base = 10
This describes the abstract syntax of the case expression that will be generated at compile time. We just generate a big switch:
genmodulus 64 ======> case n of { 0 -> 0 1 -> 1 2 -> 2 3 -> 3 4 -> 4 ... 64 -> 4 }
You can see which code is generated using -ddump-splice. I wrote the template code in a straightforward style. Someone more familiar with TH should be able to make the template code simpler.
Another option is to create a value table offline and simply import this data structure.
You can also say why you want to do this. I assume you have a very complex table driven function?
Don stewart
source share