[Haskell-cafe] SYB and opaque types
joelr1 at gmail.com
Wed Apr 18 10:12:16 EDT 2007
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 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.
More information about the Haskell-Cafe