简体   繁体   中英

Custom Applicative Maybe

Tying to implement Applicative of same nature as Maybe. Got tons of errors on compliation, most like

Couldn't match expected type ‘FixMePls b’
            with actual type ‘Maybe a1’

The code is as follows

data FixMePls a = FixMe | Pls a deriving (Eq, Show)

instance Monoid a => Monoid (FixMePls a) where
  mempty = Nothing
  mappend m Nothing = m
  mappend Nothing m = m
  mappend (Pls a) (Pls a') = Pls (mappend a a')

instance Applicative FixMePls where
  pure = Pls
  Nothing <*> _ = Nothing
  _ <*> Nothing = Nothing
  Pls f <*> Pls a = Pls f a


main :: IO ()
main = do
  putStrLn("Weee!!!1!")

Any hints on what I'm doing wrong are welcome. I suspect it's datatype declaration, but not sure how to fix it.

data FixMePls a = FixMe | Pls a deriving (Eq, Show)

is ok nothing wrong with that one

instance Monoid a => Monoid (FixMePls a) where
  mempty = Nothing
  mappend m Nothing = m
  mappend Nothing m = m
  mappend (Pls a) (Pls a') = Pls (mappend a a')

here you start to mix Maybe and FixmePls - Nothing is a constructor for Maybe whereas Fixme is the one you need.

instance Monoid a => Monoid (FixMePls a) where
  mempty = FixMe
  mappend m FixMe = m
  mappend FixMe m = m
  mappend (Pls a) (Pls a') = Pls (mappend a a')

I am ommiting the applicative instance - as it should be quite similar, if you have problems - I spotted a parenthization error ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM