
Suppose I need to manually derive Data and Typeable for SourcePos from Parsec to make sure my code compiles. I won't actually be running the code I manually derive since the constructor that includes SourcePos will be skipped. With Neil Mitchell's (and #haskell) help I'm doing this to strip token locations from my AST: import Data.Generics import qualified Text.ParserCombinators.Parsec as P instance Data SourcePos where gfoldl r k x = k x typename_SourcePos = mkTyCon "SourcePos" instance Typeable SourcePos where typeOf _ = mkTyConApp typename_SourcePos ([]) strip = everywhere (mkT f) where f (TokenPos a _) = a f x = x I know that the warnings about gunfold, toConstr and dataTypeOf are harmless but how would I define them to avoid the warnings? Also, the definition of strip above requires -fno-monomorphism- restriction. Should I not worry about it? The code runs just fine, locations are being stripped and tests pass. Thanks, Joel -- http://wagerlabs.com/

Hi Joel,
import Data.Generics import qualified Text.ParserCombinators.Parsec as P
instance Data SourcePos where gfoldl r k x = k x
typename_SourcePos = mkTyCon "SourcePos"
instance Typeable SourcePos where typeOf _ = mkTyConApp typename_SourcePos ([])
strip = everywhere (mkT f) where f (TokenPos a _) = a f x = x
I know that the warnings about gunfold, toConstr and dataTypeOf are harmless but how would I define them to avoid the warnings?
Once you find out, we can implement this in Derive as deriving DataOpaque and TypeableOpaque - could be useful for some things, then you can fake up an instance with data SourcePos = WHO_CARES.
Also, the definition of strip above requires -fno-monomorphism- restriction. Should I not worry about it? The code runs just fine, locations are being stripped and tests pass.
If you eta-expand everything, i.e. strip x = everything ... x, or give it a type signature, then you don't need monomorphism. Thanks Neil

| I know that the warnings about gunfold, toConstr and dataTypeOf are | harmless but how would I define them to avoid the warnings? I guess you could define them in the instance to be error calls: instance Data SourcePos where gfoldl r k x = k x gunfold = error "gunfold:SourcePos" ...etc.. That way you are making it clear that you didn't leave them out by accident. | Also, the definition of strip above requires -fno-monomorphism- | restriction. Should I not worry about it? The code runs just fine, | locations are being stripped and tests pass. Best thing is to give 'strip' a type signature; then it'll work regardless of flags. Simon
participants (3)
-
Joel Reymont
-
Neil Mitchell
-
Simon Peyton-Jones