tag:blogger.com,1999:blog-4650294074444534066.post6274861890903840405..comments2017-03-15T00:37:07.929-07:00Comments on Typed Logic: 'Arrow' is spelt 'fizz-buzz'geophfhttp://www.blogger.com/profile/09936874508556500234noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4650294074444534066.post-83285359215784204152014-03-19T14:48:51.581-07:002014-03-19T14:48:51.581-07:00instance (Monoid l, Monoid r) => Monoid (Either...instance (Monoid l, Monoid r) => Monoid (Either l r) where<br><br />mempty = Right mempty<br><br />mappend (Left x) (Left y) = Left (mappend x y)<br><br />mappend (Left x) (Right _) = Left x<br />mappend (Right _) (Left x) = Left x<br />mappend (Right x) (Right y) = Right (mappend x y)<br><br /><br />Proving the monoid laws is an exercise for the reader. You can then use whatever monoid on integers you want; I suggest 'Sum'.Ryan Ingramhttp://www.blogger.com/profile/09502782880675381198noreply@blogger.comtag:blogger.com,1999:blog-4650294074444534066.post-74053291355842062932014-03-01T07:48:23.451-08:002014-03-01T07:48:23.451-08:00The monoid instance only kind of works, because th...The monoid instance only kind of works, because there isn't really a good mempty. Yes, it should be a Right num, but what is num? The program assumes the invariant that if you're using mappend on two Right values, they contain the same number, but in the definition of the monoid instance we can't assume this. A moderately hacky way around this is to say that mempty = Right 0, and mappend (Right m) (Right n) = Right (max m n). This works, assuming you only want to fizzbuzz natural numbers. If you want it to work for all integers you could set mempty = Right (-infinity) where infinity = read "Infinity".Nathanhttp://www.blogger.com/profile/04334219592996961781noreply@blogger.com