2017-02-25 2 views
0

Bindung zu implementieren Applicative der gleichen Natur wie vielleicht. Got Tonnen von Fehlern auf compliation, am liebstenBenutzerdefinierte Applicable Vielleicht

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

Der Code ist wie folgt

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!") 

Irgendwelche Hinweise auf das, was ich falsch machen willkommen. Ich vermute, dass es eine Datentypdeklaration ist, aber nicht sicher, wie es zu beheben ist.

+2

Es ist Ihnen zu sagen, dass es eine 'FixMePls b' ist erwartet (dh entweder ein "FixMe" oder ein "Pls", aber es hat ein "Vielleicht ein" (dh ein 'Nur' oder ein' Nichts'). –

Antwort

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

ist ok nichts falsch mit, dass man

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') 

hier starten Sie Maybe und FixmePls zu mischen - Nothing ist ein Konstruktor für Maybe während Fixme ist diejenige, die Sie benötigen.

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') 

Ich ommiting die applicative Instanz - wie es ganz ähnlich sein sollte, wenn Sie Probleme haben - ich parenthization Fehler entdeckt;)