与SYB一起参观GHC AST

Visiting GHC AST with SYB

我编写了一个使用 Haskell-src-exts 访问 AST 的程序。我正在尝试将其转换为使用 GHC API。前者使用 Uniplate,而对于后者,不幸的是我似乎被迫使用 SYB(文档非常稀缺)。

原代码如下:

module Argon.Visitor (funcsCC)
    where

import Data.Data (Data)
import Data.Generics.Uniplate.Data (childrenBi, universeBi)
import Language.Haskell.Exts.Syntax
import Argon.Types (ComplexityBlock(..))


-- | Compute cyclomatic complexity of every function binding in the given AST.
funcsCC :: Data from => from -> [ComplexityBlock]
funcsCC ast = map funCC [matches | FunBind matches <- universeBi ast]

funCC :: [Match] -> ComplexityBlock
funCC [] = CC (0, 0, "<unknown>", 0)
funCC ms@(Match (SrcLoc _ l c) n _ _ _ _:_) = CC (l, c, name n, complexity ms)
    where name (Ident s)   = s
          name (Symbol s) = s

sumWith :: (a -> Int) -> [a] -> Int
sumWith f = sum . map f

complexity :: Data from => from -> Int
complexity node = 1 + visitMatches node + visitExps node

visitMatches :: Data from => from -> Int
visitMatches = sumWith descend . childrenBi
    where descend :: [Match] -> Int
          descend x = length x - 1 + sumWith visitMatches x

visitExps :: Data from => from -> Int
visitExps = sumWith inspect . universeBi
    where inspect e = visitExp e + visitOp e

visitExp :: Exp -> Int
visitExp (If {})        = 1
visitExp (MultiIf alts) = length alts - 1
visitExp (Case _ alts)  = length alts - 1
visitExp (LCase alts)   = length alts - 1
visitExp _ = 0

visitOp :: Exp -> Int
visitOp (InfixApp _ (QVarOp (UnQual (Symbol op))) _) =
  case op of
    "||" -> 1
    "&&" -> 1
    _    -> 0
visitOp _ = 0

我需要访问函数绑定、匹配项和表达式。这是我设法写的(不工作):

import Data.Generics
import qualified GHC
import Outputable  -- from the GHC package

funcs :: (Data id, Typeable id, Outputable id, Data from, Typeable from) => from -> [GHC.HsBindLR id id]
funcs ast = everything (++) (mkQ [] (\fun@(GHC.FunBind {}) -> [fun])) ast

它抱怨 id 的实例太多,但我不知道它到底是什么。相关的 GHC 模块是: http://haddock.stackage.org/lts-3.10/ghc-7.10.2/HsBinds.html

我快疯了。目标是计算复杂性(如您在原始代码中所见)。我想切换到 GHC API 因为它使用与编译器相同的解析器,所以它可以解析每个模块而不用担心扩展。

编辑:这就是当前代码不起作用的原因:

λ> :m +Language.Haskell.GHC.ExactPrint.Parsers GHC Data.Generics Outputable
λ> r <- Language.Haskell.GHC.ExactPrint.parseModule src/Argon/Visitor.hs
λ> let ast = snd $ (\(Right t) -> t) r
.> 
λ> :t ast
ast :: Located (HsModule RdrName)
λ> let funcs = everything (++) (mkQ [] (un@(FunBind _ _ _ _ _ _) -> [fun])) ast :: (Data id, Typeable id, Outputable id) => [HsBindLR id id]
.> 
λ> length funcs

<interactive>:12:8:
    No instance for (Data id0) arising from a use of ‘funcs’
    The type variable ‘id0’ is ambiguous
    Note: there are several potential instances:
      instance Data aeson-0.8.0.2:Data.Aeson.Types.Internal.Value
        -- Defined in ‘aeson-0.8.0.2:Data.Aeson.Types.Internal’
      instance Data attoparsec-0.12.1.6:Data.Attoparsec.Number.Number
        -- Defined in ‘attoparsec-0.12.1.6:Data.Attoparsec.Number’
      instance Data a => Data (Data.Complex.Complex a)
        -- Defined in ‘Data.Complex’
      ../..plus 367 others
    In the first argument of ‘length’, namely ‘funcs’
    In the expression: length funcs
    In an equation for ‘it’: it = length funcs

GHC AST 是根据树中使用的名称类型进行参数化的:解析器输出带有 RdrName names which it seems you're working with. The GHC Commentary and the Haddocks 的 AST 有更多信息。

如果您告诉编译器您正在使用 HsBindLR RdrName RdrName,您可能会更幸运。

像这样:

import Data.Generics
import GHC
import Outputable  -- from the GHC package

funcs :: (Data from, Typeable from) => from -> [GHC.HsBindLR RdrName RdrName]
funcs ast = everything (++) (mkQ [] (\fun@(GHC.FunBind {}) -> [fun])) ast