Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,866
    • Issues 4,866
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 458
    • Merge requests 458
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #20862
Closed
Open
Created Dec 23, 2021 by Brandon Chinn@brandonchinn178

Provide basic QuasiQuoters in template-haskell?

Is there a reason why template-haskell doesn't provide QuasiQuoters corresponding to TH quotes [e| ... |], [d| ... |], etc.? A lot of packages provide QuasiQuoters that could be written as "[e| ... |] + extra stuff"; for example, a text interpolation library could be implemented as

-- the QuasiQuoter corresponding to [e| ... |]
eQQ :: QuasiQuoter

-- Interpolate: (let x = 1 in [text|x is: #{x}|]) == "x is: 1"
text :: QuasiQuoter
text =
  eQQ
    { quoteExp =
        appE [| concat |]
        . listE
        . map
          ( \case
            Left raw -> litE $ stringL raw
            Right interpolated -> quoteExp eQQ interpolated
          )
        . parse
    }

-- Parses "x is: #{x}" into [Left "x is: ", Right "x"]
parse :: String -> [Either String String]

Whereas currently, it'd have to pull in haskell-src-meta to parse the string.

As another example, we have a use case for easily writing lists of hlists:

rows =
  eQQ
    { quoteExp = listE . map (toHList . quoteExp eQQ) . lines
    }

-- generates [1 :& "hello" :& True :& Nil, 2 :& "world" :& False :& Nil]
[rows|
  [1, "hello", True]
  [2, "world", False]
|]

Today, the best way to do this without haskell-src-meta is to do

rows = listE . map toHList

$( rows
     [ [| [1, "hello", True] |]
     , [| [2, "world", False] |]
     ]
 )

I would imagine it's not straightforward, with the parsing of the TH quotes probably builtin to the compiler and not available at runtime, but if it's just a matter of breaking out the logic, I think it would be a big win on writing common QuasiQuoters

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking