QuasiQuotes with OverloadedLabels

Viewed 80

I'm trying to use OverloadedLabels with QuasiQuotes from here package. Using plain lenses works but #foo fails with parsing error during compilation. So does field @"foo". Is there a deeper reason this will not work or could it be be a bug in here's interpolated parser?

{-# language DataKinds #-}
{-# language DeriveGeneric #-}
{-# language DerivingStrategies #-}
{-# language OverloadedLabels #-}
{-# language OverloadedStrings #-}
{-# language TypeApplications #-}
{-# language QuasiQuotes #-}

import Control.Lens
import Data.Text (Text)
import qualified Data.Text.IO as T
import Data.Generics.Product
import Data.Generics.Labels
import Data.String.Here
import GHC.Generics (Generic)

data Test = Test
  { name :: Text
  } deriving stock (Eq, Show, Generic)

_name :: Lens' Test Text
_name f (Test a) = fmap (\a' -> Test a') (f a)

t :: Test
t = Test "test"

test :: IO ()
test = do
  -- ok
  T.putStrLn $ t ^. field @"name"
  T.putStrLn $ t ^. #name
  putStrLn [i|${t ^. _name}|]

  -- parse error
  putStrLn [i|The name is ${t ^. field @"name"}|]
  putStrLn [i|The name is ${t ^. #name}|]

Error for #name:

test.hs:36:12: error:
    • Exception when trying to run compile-time code:
        Failed to parse interpolated expression in string: The name is ${t ^. #name}
(line 1, column 25):
0
SrcLoc "" 1 6
Parse error in expression: t ^.

CallStack (from HasCallStack):
  error, called at src/Data/String/Here/Interpolated.hs:64:33 in here-1.2.13-HU0AD0x0dD36rY9YuL1gwE:Data.String.Here.Interpolated
      Code: Language.Haskell.TH.Quote.quoteExp
              i "The name is ${t ^. #name}"
    • In the quasi-quotation: [i|The name is ${t ^. #name}|]
   |
36 |   putStrLn [i|The name is ${t ^. #name}|]
   |
2 Answers

It looks like haskell-src-meta doesn’t support OverloadedLabels yet. The haskell-src-exts parser has an OverloadedLabels case, but haskell-src-meta doesn’t have a case for it in the ToExp instance for Exp. I guess the “unsupported” error message from haskell-src-meta is getting swallowed by the error handling in here.

Related