Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add instance declaration for Typeable

Tags:

haskell

[...] 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)?

like image 901
lupuya Avatar asked Feb 17 '23 19:02

lupuya


1 Answers

{-# 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
like image 171
ScootyPuff Avatar answered Feb 24 '23 11:02

ScootyPuff