Skip to content

Instantly share code, notes, and snippets.

@NorfairKing
Last active November 17, 2015 16:30
Show Gist options
  • Save NorfairKing/1a9c0661ea1b87972d09 to your computer and use it in GitHub Desktop.
Save NorfairKing/1a9c0661ea1b87972d09 to your computer and use it in GitHub Desktop.
Template Haskell Errors
[1 of 2] Compiling TH ( TH.hs, interpreted )
TH.hs:11:52:
Couldn't match type ‘Exp’ with ‘Body’
Expected type: [Body]
Actual type: [Exp]
In the third argument of ‘zipWith’, namely ‘fnBodies’
In the expression:
zipWith (\ p b -> Clause [p] b []) patterns fnBodies
TH.hs:13:29:
Couldn't match expected type ‘Pat’ with actual type ‘Name’
In the expression: x'
In the first argument of ‘Clause’, namely ‘[x']’
TH.hs:13:33:
Couldn't match expected type ‘Body’ with actual type ‘Exp -> Exp’
Probable cause: ‘appBody’ is applied to too few arguments
In the second argument of ‘Clause’, namely ‘appBody’
In the expression: Clause [x'] appBody []
TH.hs:14:23:
Couldn't match expected type ‘Exp -> Exp’ with actual type ‘Exp’
The function ‘VarE’ is applied to two arguments,
but its type ‘Name -> Exp’ has only one
In the first argument of ‘AppE’, namely
‘(VarE (mkName "bigBadMathProblem") (VarE x'))’
In the expression:
AppE (VarE (mkName "bigBadMathProblem") (VarE x'))
Failed, modules loaded: none.
{-# LANGUAGE TemplateHaskell #-}
module Test where
import TH
$(precompute [1..1000])
module TH where
import Language.Haskell.TH
precompute :: [Int] -> DecsQ
precompute xs = do
let name = mkName "lookupTable"
patterns = map intToPat xs
fnBodies = map precomputeInteger xs
precomputedClauses =
zipWith (\p b -> Clause [p] b []) patterns fnBodies
x' = mkName "x"
lastClause = [Clause [x'] appBody []]
appBody = AppE (VarE (mkName "bigBadMathProblem") (VarE x'))
clauses = precomputedClauses ++ lastClause
return [FunD name clauses]
intToPat :: Int -> Pat
intToPat = LitP . IntegerL . toInteger
precomputeInteger :: Int -> Exp
precomputeInteger = LitE . DoublePrimL . toRational . bigBadMathProblem
bigBadMathProblem :: Int -> Int
bigBadMathProblem = (*2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment