1

I played around with symbolVal and template haskell as follows on ghci.

>>> :set -XTemplateHaskell -XQuasiQuotes -XDataKinds
>>> import Language.Haskell.TH
>>> import Data.Proxy
>>> import GHC.TypeLits
>>> $([| symbolVal (Proxy :: Proxy "foo") |]) -- Code (1)
"foo"

>>> runQ [| symbolVal (Proxy :: Proxy "foo") |]
AppE (VarE GHC.TypeLits.symbolVal) (SigE (ConE Data.Proxy.Proxy) (AppT (ConT Data.Proxy.Proxy) (LitT (StrTyLit "foo"))))

>>> $(appE (varE 'symbolVal) (sigE (conE ''Proxy) (appT (conT ''Proxy) (litT (strTyLit "foo")))))

<interactive>:9:3: error:
    • Type constructor ‘Proxy’ used where a value identifier was expected
    • In the first argument of ‘symbolVal’, namely
        ‘Proxy :: Proxy "foo"’
      In the expression: (symbolVal (Proxy :: Proxy "foo"))
      In an equation for ‘it’: it = (symbolVal (Proxy :: Proxy "foo"))

Why this error occurs though Code (1) works well? How we can use type level string in template haskell?

user3749167
  • 161
  • 6

1 Answers1

0

For completeness and future reference, I thought I would add the correct answer for reference.

λ> :set -XTemplateHaskell -XQuasiQuotes -XDataKinds
λ> import Language.Haskell.TH
λ> import Data.Proxy
λ> import GHC.TypeLits

λ> $([| symbolVal (Proxy :: Proxy "foo") |])
"foo"

λ> runQ [| symbolVal (Proxy :: 'Proxy "foo") |]
AppE (VarE GHC.TypeLits.symbolVal) (SigE (ConE Data.Proxy.Proxy) (AppT (PromotedT Data.Proxy.Proxy) (LitT (StrTyLit "foo"))))

λ> runQ [| symbolVal (Proxy :: Proxy "foo") |]
AppE (VarE GHC.TypeLits.symbolVal) (SigE (ConE Data.Proxy.Proxy) (AppT (ConT Data.Proxy.Proxy) (LitT (StrTyLit "foo"))))

λ> $(appE (varE 'symbolVal) (sigE (conE 'Proxy) (appT (conT ''Proxy) (litT (strTyLit "foo")))))
"foo"
MCH
  • 2,124
  • 1
  • 19
  • 34