Skip to content

Instantly share code, notes, and snippets.

@NorfairKing
Created November 17, 2015 17:05
Show Gist options
  • Save NorfairKing/338152aac91c490476d0 to your computer and use it in GitHub Desktop.
Save NorfairKing/338152aac91c490476d0 to your computer and use it in GitHub Desktop.
module Problem where
bigBadMathProblem :: Integer -> Integer
bigBadMathProblem = (* 5)
{-# LANGUAGE TemplateHaskell #-}
module Test where
import Problem
import TH
$(precompute [1..1000])
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