0

I am beginning to learn Template haskell, so I want create a function that receive a String param that define the name to the function to generate

build_p5 :: String -> [Dec]
build_p5 name =
[ FunD p1 
         [ Clause [TupP [VarP a,VarP b]] (NormalB (AppE (VarE c) (VarE a))) [FunD c [Clause [VarP a] (NormalB (VarE a)) []]]
         ]
] where
   p1 = mkName name
   a = mkName "a"
   b = mkName "b"
   c = mkName "ident"

to run the definition :

$(build_p5 "hola")

but when I run the program get the following error

Couldn't match type ‘[Dec]’ with ‘Q [Dec]’
Expected type: DecsQ
  Actual type: [Dec]
In the expression: build_p5 "hola"

correct code

build_p2 :: Monad m => String -> m [Dec]
build_p2 name =
return [ FunD p1 
     [ Clause [TupP [VarP a,VarP b]] (NormalB (AppE (VarE c) (VarE a))) [FunD c [Clause [VarP a] (NormalB (VarE a)) []]]
     ]
  ] where
  p1 = mkName name
  a = mkName "a"
  b = mkName "b"
  c = mkName "ident"
oriaj
  • 768
  • 1
  • 16
  • 33
  • 2
    You have to have your template haskell function return a `Q [Dec]`, and since `Q` is a `Monad` you should be able to just `return [FunD p1 ...]` in your function definition. Does that work for what you want to do? – bheklilr Jun 04 '15 at 14:19
  • thank you it work add the code corrected in the answers, i create this method only to exercise, the idea is that you can define the name to the function as a parameter – oriaj Jun 04 '15 at 17:15
  • add the code corrected – oriaj Jun 10 '15 at 21:17

0 Answers0