How to add instance declaration for Typeable

haskell

Solution

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

deriving instance Typeable Lexeme

should work.

There is one pitfall, however, and this only really applies if this instance is to be defined in a library. `Lexeme` will be made an instance of `Typeable`, and if any other library includes a similar instance, there will be a conflict. Unfortunately, all you can really to ensure that you don't add the global `Typeable` instance of `Lexeme` is either hope it gets added to `base` at some point, or use a `newtype` wrapper and manually wrap and unwrap `Lexeme`.

newtype LexemeWrapper = WrapLexeme { unwrapLexeme :: Lexeme } deriving Typeable

Problem

[...] wants to find all the places in a legacy codebase that a variable foo is used in the conditional of an if. ———— Why Haskell Is Worth Learning the code I have is ``` 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") ``` how could I add an instance declaration for (Typeable Lexeme)?

Original source