[Haskell-cafe] some questions about Template Haskell

adam vogt vogt.adam at gmail.com
Tue Jul 2 02:40:16 CEST 2013


On Mon, Jul 1, 2013 at 5:42 PM, TP <paratribulations at free.fr> wrote:
> So what is the difference between lift and [||]?
> Although I feel stupid, I cannot lie and claim I have understood.

Hi TP,

Sometimes [| |] does need to call lift. If for some reason the
original lift wasn't exported, you could define:

myLift x = [| x |]

But if you have enough $( ) splices to balance out the [| |], there is
no lift involved:

myId1 x = $( [| x |] )
myId2 x = [| $(x) |]


I think your first example is supposed to do:

*Pr> let x = 5 in $(pr 'x)
x = 5

That's possible if you had defined pr as:

pr n = [| putStrLn $ $(lift (nameBase n)) ++ " = " ++ show $(varE n) |]

If there were no [| |] quotes, but still the ' syntax for getting a
Name, it would still be possible to define pr:

pr2 n
  = varE 'putStrLn `appE`
      (infixE (Just (lift (nameBase n))) (varE '(++))
         (Just
            (infixE (Just (lift " = ")) (varE '(++))
               (Just (appE (varE 'show) (varE n))))))

--
Adam



More information about the Haskell-Cafe mailing list