Created
March 19, 2014 07:21
-
-
Save 314maro/9636936 to your computer and use it in GitHub Desktop.
ApplicativeでMonoidをくるむとMonoid 最大値や最小値とMonoid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
| import Data.Monoid | |
| import Control.Applicative | |
| newtype AppMon f m = AppMon { getAppMon :: f m } | |
| deriving (Functor, Applicative) | |
| instance (Applicative f, Monoid m) => Monoid (AppMon f m) where | |
| mempty = pure mempty | |
| mappend = liftA2 mappend | |
| -- 必ずしもモノイドになるとは限らないかも | |
| -- Bounded と Ord の関係 | |
| newtype Max a = Max { getMax :: a } deriving (Bounded, Eq, Ord) | |
| newtype Min a = Min { getMin :: a } deriving (Bounded, Eq, Ord) | |
| instance (Bounded a, Ord a) => Monoid (Max a) where | |
| mempty = minBound | |
| mappend = max | |
| instance (Bounded a, Ord a) => Monoid (Min a) where | |
| mempty = maxBound | |
| mappend = min |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment