
Hi, I’m looking at a blog post on Monoids and finger trees at http://apfelmus.nfshost.com/articles/monoid-fingertree.html http://apfelmus.nfshost.com/articles/monoid-fingertree.html and would appreciate a bit of advice I have type Size = Int type Priority = Int instance Monoid Size where mempty = 0 mappend = (+) instance Monoid Priority where mempty = maxBound mappend = min and I get compiler error Duplicate instance declarations: instance Monoid Size -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:60:10 instance Monoid Priority -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:64:10 Which I can sort of understand as Size and Priority are both Int but on the other hand, internally, the monoids are different. Is this genuinely incorrect code or is there a language extension to get around this? Thanks Mike

A type can only have a single instance of a given class. Imagine if this
weren't true. The compiler would have to guess which of the instances you
meant to use. The solution is to use newtype. That will introduce a
different type, allowing separate instances, but is optimised out so it
carries no runtime cost.
On Tue, 20 Oct 2015, 17:24 Mike Houghton
Hi,
I’m looking at a blog post on Monoids and finger trees at http://apfelmus.nfshost.com/articles/monoid-fingertree.html and would appreciate a bit of advice
I have
type Size = Int type Priority = Int
instance Monoid Size where mempty = 0 mappend = (+)
instance Monoid Priority where mempty = maxBound mappend = min
and I get compiler error
Duplicate instance declarations: instance Monoid Size -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:60:10 instance Monoid Priority -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:64:10
Which I can sort of understand as Size and Priority are both Int but on the other hand, internally, the monoids are different.
Is this genuinely incorrect code or is there a language extension to get around this?
Thanks
Mike
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Ahh! Nice. Thanks
On 20 Oct 2015, at 22:40, Joel Williamson
wrote: A type can only have a single instance of a given class. Imagine if this weren't true. The compiler would have to guess which of the instances you meant to use. The solution is to use newtype. That will introduce a different type, allowing separate instances, but is optimised out so it carries no runtime cost.
On Tue, 20 Oct 2015, 17:24 Mike Houghton
mailto:mike_k_houghton@yahoo.co.uk> wrote: Hi, I’m looking at a blog post on Monoids and finger trees at http://apfelmus.nfshost.com/articles/monoid-fingertree.html http://apfelmus.nfshost.com/articles/monoid-fingertree.html and would appreciate a bit of advice
I have
type Size = Int type Priority = Int
instance Monoid Size where mempty = 0 mappend = (+)
instance Monoid Priority where mempty = maxBound mappend = min
and I get compiler error
Duplicate instance declarations: instance Monoid Size -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:60:10 instance Monoid Priority -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:64:10
Which I can sort of understand as Size and Priority are both Int but on the other hand, internally, the monoids are different.
Is this genuinely incorrect code or is there a language extension to get around this?
Thanks
Mike
_______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Joel Williamson
-
Mike Houghton