I have a typeclass:
class Wrapper w where
open :: w -> Map String Int
close :: Map String Int -> w
It doesn't look very useful, but I use it to strongly (not just a type
synonym) distinguish between semantically different varieties of Map String Int
s:
newtype FlapMap = Flap (Map String Int)
newtype SnapMap = Snap (Map String Int)
...
and still have functions that operate on any type of the class.
- Is there a better way to do this distinction (maybe without the
Wrapper
instances boilerplate)?
I want to do this:
instance (Wrapper wrapper) => Show wrapper where
show w = show $ toList $ open w
instead of writing many boilerplate Show
instances as well.
Via FlexibleInstances
and UndecidableInstances
, GHC leads me to a point where it thinks my instance declaration applies to everything, as it allegedly clashes with the other Show
instances in my code and in GHC.Show
. HaskellWiki and StackOverflow answerers and HaskellWiki convince me OverlappingInstances
is not quite safe and possibly confusing. GHC doesn't even suggest it.
Why does GHC first complain about not knowing which instance of fx
Show Int
to pick (so why it doesn't look at the constraint I give at compile time?) and then, being told that instances may overlap, suddenly know what to do?Can I avoid allowing
OverlappingInstances
with mynewtype
s?
deriving Show
different to what you want to achieve? – chiFlapMap (fromList [...])
. – Leif WillertsShow
instance for it is really helpful for creating output, showing it in ghci and copy it to a test case - especially in combination with a prettyprinting library. I would rather create a UserFriendlyShow typeclass - but then you need the OverlappingInstances anyway. – epsilonhalbe