How can Haskell quasiquotation be used for replacing tokens on the Haskell level?

前端 未结 1 1285
轮回少年
轮回少年 2021-02-05 12:47

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

相关标签:
1条回答
  • 2021-02-05 12:50

    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 讨论(0)
提交回复
热议问题