2

I have an AST type that I want to derive as Typeable, so that I can do Scrap-your-boilerplate generic traversals of it.

However, the tree is annotated with messages in the Doc type of the Text.PrettyPrint library from the pretty package. To derive Typeable, there needs to be a typeable instance of Doc.

Here's what I've tried and failed:

deriving instance Data P.Doc
deriving instance Typeable P.Doc

gives this error:

Can't make a derived instance of `Data P.Doc':
  The data constructors of `P.Doc' are not all in scope
    so you cannot derive an instance for it
In the stand-alone deriving instance for `Data P.Doc'

Or, I try to derive my own instance:

instance Typeable P.Doc where 
    typeRep v = typeRep $ show v   

which gives this error:

 `typeRep' is not a (visible) method of class `Typeable'      

Am I doing something wrong? Is there a standard way to derive typeable for types given in other libraries?

The thing is, the instance isn't super important. I know that there aren't any parts of my AST stored recursively within a Doc value. But GHC complains if I don't have that instance.

jmite
  • 8,171
  • 6
  • 40
  • 81
  • According to [the docs](http://hackage.haskell.org/package/base-4.7.0.2/docs/Data-Typeable.html) `typeRep` is not in the `Typeable` class. The only member of it is `typeRep#` declared in [Data.Typeable.Internal module](http://hackage.haskell.org/package/base-4.7.0.2/docs/Data-Typeable-Internal.html#v:typeRep-35-). – projedi Jan 01 '15 at 09:36
  • 3
    1. As of the newer versions of ghc you aren't allowed to write Typeable instances by hand. 2. Your `typeRep` is incorrect as it uses the value of its argument. 3. If the constructors aren't exposed, you can't derive an instance which depends on pattern matching on the contructors (Data) but that isn't the case for Typeable. This restriction is intentional; if the type is abstract, you shouldn't be able to break that by deriving Data. – user2407038 Jan 01 '15 at 10:27
  • What does the # mean? Does this mean it's impossible to do generic traversals on trees with Pretty text? – jmite Jan 01 '15 at 19:08
  • Okay, so deriving Typeable as a standalone instance works fine. Is it possible to derive Data by hand? – jmite Jan 02 '15 at 04:31
  • @user2407038 your comment solved the problem -- you should turn it into an answer instead :-) – sclv Feb 21 '15 at 16:42

0 Answers0