[Haskell-cafe] Factoring into type classes

Derek Elkins derek.a.elkins at gmail.com
Mon Jan 19 16:51:40 EST 2009


On Mon, 2009-01-19 at 12:10 -0800, Iavor Diatchki wrote:
> Hi,
> 
> On Mon, Jan 19, 2009 at 11:06 AM, Jonathan Cast
> <jonathanccast at fastmail.fm> wrote:
> > On Mon, 2009-01-19 at 10:59 -0800, Iavor Diatchki wrote:
> >> Hello,
> >> The multitude of newtypes in the Monoid module are a good indication
> >> that the Monoid class is not a good fit for the class system
> >
> > I would say rather that the class system is not a good fit for Monoid.
> > Proposals for local instances, multiple instances, instance
> > import/export control, etc. come up quite frequently on this list; the
> > phenomena in question are not restricted to Monoid.
> 
> I disagree with you but that is a moot point because we are discussing
> Haskell, which does not have any of these features.  Also, I find that
> in many situations where people want to use them, simpler solutions
> (like some of the ideas I mentioned in my  previous post) suffice.
> That is not to say that we should stop trying to figure out how to
> improve the class system, but language changes require a lot more work
> than improving the design of the libraries.
> 
> >> I usually
> >> avoid using the "newtype" trick as I find it inconvenient:  usually
> >> the newtype does not have the same operations as the underlying type
> >> and so it cannot be used directly, and if you are going to wrap thing
> >> just when you use the class methods,
> >
> > OTOH, I think you mean here `when you use class methods and when you use
> > overloaded functions'.
> 
> Sure, the point is that you are essentially adding a type annotation,
> which is like using a non-overloaded function.  Compare, for example:
> "mappend add x y"  and "getSum (mappend (Sum x) (Sum y))".  I think
> that the first one is quite a bit more readable but, of course, this
> is somewhat subjective.

data Iso a b = Iso { to :: a -> b, from :: b -> a }

under :: Iso a b -> (b -> b) -> (a -> a)
under iso = to iso ~> from iso

under2 :: Iso a b -> (b -> b -> b) -> (a -> a -> a)
under2 iso = to iso ~> under iso

sumIso = Iso Sum getSum

(+) = under2 sumIso mappend



More information about the Haskell-Cafe mailing list