6

I'm trying to implement a recursive datatype using recursion-schemes. I would like to be able to print it.

import Data.Functor.Foldable

data T1F a = Foo deriving Show
type T1 = Fix T1F
data T2 = Bar T1 deriving Show -- error here

Error message:

No instance for (Data.Functor.Classes.Show1 T1F)
  arising from the first field of ‘Bar’ (type ‘T1’)
Possible fix:
  use a standalone 'deriving instance' declaration,
    so you can specify the instance context yourself
When deriving the instance for (Show T2)

How do I make T1 derive Show?

duplode
  • 33,731
  • 7
  • 79
  • 150
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46
  • 7
    The error message tells you the problem. You need an instance of [`Show1`](https://hackage.haskell.org/package/base-4.9.1.0/docs/Data-Functor-Classes.html#t:Show1) for `T1F`. Writing instances of `Show1` by hand can be quite tedious. Fortunately the [`deriving-compat`](https://hackage.haskell.org/package/deriving-compat) package [has got your back](https://hackage.haskell.org/package/deriving-compat-0.3.5/docs/Text-Show-Deriving.html#v:deriveShow1). – Benjamin Hodgson Mar 21 '17 at 18:45
  • 1
    @BenjaminHodgson is there an example of deriving it manually? – CMCDragonkai May 26 '18 at 09:44
  • @CMCDragonkai Easiest way is to derive `Show` for your datatype, look at the generated code by compiling with `-ddump-deriv`, and then paste and adapt it. Not a fun task, though - much easier to use the TH helpers in the pkg! – Benjamin Hodgson May 26 '18 at 21:49
  • I see. I still don't understand why this is needed. – CMCDragonkai May 27 '18 at 01:01

1 Answers1

3

Using the package deriving-compat:

{-# LANGUAGE TemplateHaskell #-}
import Data.Functor.Foldable
import Text.Show.Deriving

data T1F a = Foo deriving Show
$(deriveShow1 ''T1F)
type T1 = Fix T1F
data T2 = Bar T1 deriving Show
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46