[Haskell-cafe] GATD and pattern matching

Felipe Lessa felipe.lessa at gmail.com
Fri Jun 11 21:41:28 EDT 2010


On Fri, Jun 11, 2010 at 08:12:43PM -0500, Antoine Latter wrote:
> >   1) Which extensions are required to make the code compile.
>
> OverlappingInstances (of course), and IncoherrentInstances, since
> neither instance is more specific than the other.

Well, I guess it can't be compiled at all :(

  $ ghci -XGADTs -XOverlappingInstances -XIncoherentInstances -XFlexibleInstances -XUndecidableInstances
  GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
  Loading package ghc-prim ... linking ... done.
  Loading package integer ... linking ... done.
  Loading package base ... linking ... done.
  Prelude> :l T
  [1 of 1] Compiling Main             ( T.lhs, interpreted )

  T.lhs:4:12:
      Duplicate instance declarations:
        instance [incoherent] (Show a) => MaybeShow a
          -- Defined at T.lhs:4:12-32
        instance [incoherent] MaybeShow a -- Defined at T.lhs:7:12-22

> >   2) After compiled, if it works as intended or not.
>
> It's hard for me to concieve of a situation where something requiring
> IncoherrentInstances is work as intended, but maybe that's a failure
> of imagtination.

Perhaps we should omit the "Nothing" instance:

  class MaybeShow a where
    maybeShow :: a -> Maybe String

  instance Show a => MaybeShow a where
    maybeShow = Just . show

Instances for any non-Show-able data types should be manually
written, such as:

  instance MaybeShow (a -> b) where
    maybeShow = const Nothing

I think this solution still requires OverlappingInstances and
UndecidableInstances.



Finally we could omit the "Show a => MaybeShow a" definition as
well and just manually write everything:

  class MaybeShow a where
    may_I_show_you :: a -> Maybe String

  yes_please :: Show a => a -> Maybe String
  yes_please = Just . show

  no_thanks :: a -> Maybe String
  no_thanks = const Nothing

  instance MaybeShow ()       where may_I_show_you = yes_please
  instance MaybeShow Char     where may_I_show_you = yes_please
  instance MaybeShow Int      where may_I_show_you = yes_please
  instance MaybeShow (a -> b) where may_I_show_you = no_thanks
  instance MaybeShow (IO a)   where may_I_show_you = no_thanks
  ...

--
Felipe.


More information about the Haskell-Cafe mailing list