Created
August 25, 2021 12:08
-
-
Save el-hult/c5d22ad98f03a2fd78ba6a67fddc6047 to your computer and use it in GitHub Desktop.
A Float wrapper that implements Monoid, but not the monoid laws
This file contains 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
-- first monoid over Floats | |
data MaxNum = MaxNum Float deriving (Show) | |
instance Semigroup MaxNum where | |
(MaxNum a) <> (MaxNum b) = MaxNum $ max a b | |
instance Monoid MaxNum where | |
mempty = MaxNum 0 -- N.B. this does not fulfil the monoid laws fully. consider negative values! | |
aOne = MaxNum 4 | |
aTwo = MaxNum 1 | |
aThree = aOne <> aTwo | |
doMaxNum = print aThree -- MaxNum 4.0 | |
-- main :: IO () | |
main = do | |
doMaxNum |
Nice! Did not know about deriving via
before. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can derive
newtype MaxNum = MaxNum Float deriving Semigroup via Max Float
but as you already noted theMonoid
instance is invalid and you are prevented from deriving aMonoid