0

I cannot figure out to call something from an expression generated with TemplateHaskell. Let's say I want to use ":" operator to cons list. I saw syntax '(:) but it is not working for me. Also I tried to lookupNameValue and wrap mkName ":" into (AppE (VarE (mkName ":")), but none of above is working...

{-# LANGAUGE TemplateHaskell #-}
{-# LANGAUGE TemplateHaskellQuotes #-}
{-# LANGUAGE QuasiQuotes #-}
import Language.Haskell.TH
import Language.Haskell.TH.Syntax as S
import Language.Haskell.TH.Quote

useColon :: Q [Dec]
useColon = do
  let fName = mkName "f"
  pure [ FunD fName 
         [Clause []
          (NormalB (InfixE (Just (LitE (IntegerL 1)))
                   '(:)
                    (Just (ListE [])))) []]
  ]

• Syntax error on '(:)
  Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
• In the Template Haskell quotation '(:)    | 36 |                                '(:)    |                                ^^^^
Daniil Iaitskov
  • 5,525
  • 8
  • 39
  • 49

1 Answers1

0

I made a type in TH extension and I had to use ConE instead of VarE

useColon :: Q [Dec]
useColon = do
  let fName = mkName "f"
  pure [ FunD fName [Clause []
                     (NormalB (InfixE (Just (LitE (IntegerL 1)))
                               (ConE '(:))
                               (Just (ListE [])))) []]]

Daniil Iaitskov
  • 5,525
  • 8
  • 39
  • 49