[Haskell-cafe] Cons of -XUndecidableInstances

MigMit miguelimo38 at yandex.ru
Tue Jun 7 18:43:30 CEST 2011

> One particularly trivial example that comes to mind is:
>    newtype Mu f = Mu (f (Mu f))
>    instance Show (f (Mu f)) => Show (Mu f) where
>        show (Mu x) = "Mu (" ++ show x ++ ")"
>        -- Or however you'd like to show it

Ehm, that does look like poor design.

Sure you don't mean "Mu f can be printed if and only if f (Mu f) can be printed". What you probably mean is "if f transforms printable things to printable things, then Mu f is a printable thing". And you CAN express just that:

type ShowD a = forall p. (forall x. Show x => p x) -> p a

showD :: Show a => ShowD a
showD px = px

class ShowF f where showF :: Show a => ShowD (f a)

instance Show a => Show (F a) where... -- here goes your "f"

instance ShowF F where showF = showD -- and that is the only line of boilerplate

instance ShowF f => Show (Mu f) where
  show (Mu fm) = "Mu (" ++ runShowHelper (showF (ShowHelper show)) fm ++ ")"

newtype ShowHelper x = ShowHelper {runShowHelper :: x -> String}

Sorry for possible bugs — I don't have ghc anywhere near me at the moment, but the idea is clear, I guess.

Отправлено с iPhone

More information about the Haskell-Cafe mailing list