[Haskell-cafe] GATD and pattern matching

Antoine Latter aslatter at gmail.com
Fri Jun 11 21:12:43 EDT 2010


On Friday, June 11, 2010, Felipe Lessa <felipe.lessa at gmail.com> wrote:
> On Sat, Jun 12, 2010 at 12:13:14AM +0200, Dupont Corentin wrote:
>> Thanks all, it works fine (see below).
>>
>> I lamentably try to make the same for show:
>> > showTypeable :: (Typeable a) => a -> String
>> > showTypeable x = case cast x of
>> >                      Just x' -> show x'
>> >                      Nothing -> ""
>>
>> Because it really upsets me to add this show constraints to the Equ
>> constructor ;)
>> what if i want to make an Obs instance with non showable elements, with no
>> intention to show it of course?
>
> Ad hoc solution:
>
>   class MaybeShow a where
>     maybeShow :: a -> Maybe String
>
>   instance Show a => MaybeShow a where
>     maybeShow = Just . show
>
>   instance MaybeShow a where
>     maybeShow = Nothing
>
>   data MyData where
>     Something :: MaybeShow a => a -> MyData
>
>   instance MaybeShow MyData where
>     maybeShow (Something x) =
>       fmap (\s -> "Something (" ++ s ++ ")") (maybeShow x)
>
> Hahahaha :).  Try to guess without using GHC/GHCi:
>
>   1) Which extensions are required to make the code compile.
>

OverlappingInstances (of course), and IncoherrentInstances, since
neither instance is more specific than the other.

>   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.

-Antoine


More information about the Haskell-Cafe mailing list