Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:59:15+00:00 2026-06-10T04:59:15+00:00

Quasiquotation as described in haskellwiki is shown mostly as useful tool for embedding other

  • 0

Quasiquotation as described in haskellwiki is shown mostly as useful tool for embedding other languages inside Haskell without messing around with string quotation.

Question is: For Haskell itself, how easy it would be to put existing Haskell code through a quasiquoter for the purpose of just replacing tokens and passing the result over to ghc? Perhaps Template Haskell is key here?

I have looked for code examples and didn’t find any. Some EDSLs can benefit from this ability by reducing the size of their combinating operators (e.g. turn ‘a .|. b .>>. c’ to ‘[myedsl|a | b >> c]’).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:59:16+00:00Added an answer on June 10, 2026 at 4:59 am

    You can build quasi-quoters that manipulate Haskell code by, for example, using the haskell-src-meta package. It parses valid Haskell code into an AST, which you can then modify.

    In this case, the easiest way to modify the AST is by using Data.Generics to apply a generic transformation to the whole AST that replaces operators with other operators.

    We’ll begin by building the transformation function for generic Haskell expressions. The data type that represents an expression is Exp in the template-haskell package.

    For example, to convert the operator >> to .>>. we’d use a function like

    import Language.Haskell.TH (Exp(..), mkName)
    
    replaceOp :: Exp -> Exp
    replaceOp (VarE n) | n == mkName ">>" = VarE (mkName ".>>.")
    replaceOp e = e
    

    This changes a variable expression (VarE), but cannot do anything to any other kind of expressions.

    Now, to walk the whole AST and to replace all occurrences of >> we’ll use the functions everywhere and mkT from Data.Generic.

    import Data.Generics (everywhere, mkT)
    
    replaceEveryOp :: Exp -> Exp
    replaceEveryOp = everywhere (mkT replaceOp) 
    

    In order to make several replacements, we can alter the function so that it takes an association list of any operator to replace.

    type Replacements = [(String, String)]
    
    replaceOps :: Replacements -> Exp -> Exp
    replaceOps reps = everywhere (mkT f) where
        f e@(VarE n) = case rep of
            Just n' -> VarE (mkName n')
            _ -> e
            where rep = lookup (show n) reps
        f e = e
    

    And by the way, this is a good example of a function that is much nicer to write by using the view patterns language extension.

    {-# LANGUAGE ViewPatterns #-}
    
    replaceOps :: Replacements -> Exp -> Exp
    replaceOps reps = everywhere (mkT f) where
        f (VarE (replace -> Just n')) = VarE (mkName n')
        f e = e
    
        replace n = lookup (show n) reps
    

    Now all that’s left for us to do is to build the “myedsl” quasi-quoter.

    {-# LANGUAGE ViewPatterns #-}
    
    import Data.Generics (everywhere, mkT)
    import Language.Haskell.Meta.Parse (parseExp)
    import Language.Haskell.TH (Exp(..), mkName, ExpQ)
    import Language.Haskell.TH.Quote (QuasiQuoter(..))
    
    type Replacements = [(String, String)]
    
    replacements :: Replacements
    replacements =
        [ ("||", ".|.")
        , (">>", ".>>.")
        ]
    
    myedls = QuasiQuoter
        { quoteExp  = replaceOpsQ
        , quotePat  = undefined
        , quoteType = undefined
        , quoteDec  = undefined
        }
    
    replaceOpsQ :: String -> ExpQ
    replaceOpsQ s = case parseExp s of
        Right e -> return $ replaceOps replacements e
        Left err -> fail err
    
    replaceOps :: Replacements -> Exp -> Exp
    replaceOps reps = everywhere (mkT f) where
        f (VarE (replace -> Just n')) = VarE (mkName n')
        f e = e
    
        replace n = lookup (show n) reps
    

    If you save the above to its own module (e.g. MyEDSL.hs), then you can import it and use the quasi-quoter.

    {-# LANGUAGE TemplateHaskell #-}
    {-# LANGUAGE QuasiQuotes #-}
    
    import MyEDSL
    
    foo = [myedsl| a || b >> c |]
    

    Note that I’ve used || instead of | because the latter is not a valid operator in Haskell (since it’s the syntactic element used for pattern guards).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some languages like Haskell (or Nemerle) have quasiquotations . I wonder what the quasi

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.