Created
November 17, 2015 17:05
-
-
Save NorfairKing/338152aac91c490476d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Problem where | |
bigBadMathProblem :: Integer -> Integer | |
bigBadMathProblem = (* 5) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module Test where | |
import Problem | |
import TH | |
$(precompute [1..1000]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TH where | |
import Language.Haskell.TH | |
import Problem | |
precompute :: [Integer] -> DecsQ | |
precompute xs = do | |
let name = mkName "lookupTable" | |
patterns :: [Pat] | |
patterns = map intToPat xs | |
fnBodies :: [Body] | |
fnBodies = map precomputeInteger xs | |
precomputedClauses :: [Clause] | |
precomputedClauses = | |
zipWith (\pattern body -> Clause [pattern] body []) patterns fnBodies | |
x' :: Name | |
x' = mkName "x" | |
bigBadMathProblem' :: Name | |
bigBadMathProblem' = mkName "bigBadMathProblem" | |
appBody :: Body | |
appBody = NormalB $ AppE (VarE bigBadMathProblem') (VarE x') | |
lastClause :: Clause | |
lastClause = Clause [VarP x'] appBody [] | |
clauses :: [Clause] | |
clauses = precomputedClauses ++ [lastClause] | |
return [FunD name clauses] | |
intToPat :: Integer -> Pat | |
intToPat = LitP . IntegerL . toInteger | |
precomputeInteger :: Integer -> Body | |
precomputeInteger = NormalB . LitE . IntegerL . bigBadMathProblem | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment