4

[...] 想要在遗留代码库中找到变量 foo 在 if 条件中使用的所有位置。

————为什么 Haskell 值得学习

我的代码是

import Language.C
import Data.Generics
import Control.Monad
import Text.Read
    
parseAndFindFoos :: FilePath -> IO (Either ParseError [Position])
parseAndFindFoos path = liftM (fmap findFooLocations) (parseCFilePre path)
findFooLocations input = fmap posOf (listify isIfOfInterest input)
isIfOfInterest (CIf cond _ _ _) = not (null (listify isFooIdent cond))
isFooIdent (Ident name) = (name == "foo")

如何为(Typeable Lexeme)添加实例声明?

4

1 回答 1

8
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
import Data.Typeable

deriving instance Typeable Lexeme

应该管用。

然而,有一个陷阱,这仅适用于要在库中定义此实例的情况。 Lexeme将成为 的实例Typeable,如果任何其他库包含类似的实例,则会发生冲突。不幸的是,您真正可以确保不添加的全局Typeable实例Lexeme是希望它base在某个时候被添加,或者使用newtype包装器并手动 wrap 和 unwrap Lexeme

newtype LexemeWrapper = WrapLexeme { unwrapLexeme :: Lexeme } deriving Typeable
于 2013-04-14T02:36:32.260 回答