Skip to content

Instantly share code, notes, and snippets.

@314maro
Created March 19, 2014 07:21
Show Gist options
  • Select an option

  • Save 314maro/9636936 to your computer and use it in GitHub Desktop.

Select an option

Save 314maro/9636936 to your computer and use it in GitHub Desktop.
ApplicativeでMonoidをくるむとMonoid 最大値や最小値とMonoid
{-# 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