Ich habe gerade versucht, meinen Kopf um freie Monaden zu wickeln; als Lernhilfe, ich habe eine Show
Instanz für die folgenden Free
Art schreiben verwaltet:Kann ich die Verwendung von UndecidableInstances in dieser Show-Instanz für eine kostenlose Monade eliminieren?
{-# LANGUAGE FlexibleContexts, UndecidableInstances #-}
-- Free monad datatype
data Free f a = Return a | Roll (f (Free f a))
instance Functor f => Monad (Free f) where
return = Return
Return a >>= f = f a
Roll ffa >>= f = Roll $ fmap (>>= f) ffa
-- Show instance for Free; requires FlexibleContexts and
-- UndecidableInstances
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
show (Return x) = "Return (" ++ show x ++ ")"
show (Roll ffx) = "Roll (" ++ show ffx ++ ")"
-- Identity functor with Show instance
newtype Identity a = Id a deriving (Eq, Ord)
instance Show a => Show (Identity a) where
show (Id x) = "Id (" ++ show x ++ ")"
instance Functor (Identity) where
fmap f (Id x)= Id (f x)
-- Example computation in the Free monad
example1 :: Free Identity String
example1 = do x <- return "Hello"
y <- return "World"
return (x ++ " " ++ y)
Die Verwendung von UndecidableInstances
stört mich etwas; Gibt es einen Weg, darauf zu verzichten? Alles, was Google liefert, ist , was im Grunde die selbe Show
Klassifizierungsdefinition ist wie ich.
'UndecidableInstances' ist nicht wirklich besorgniserregend. Im Grunde sagt es nur dem Compiler "Vertrauen Sie mir, dass die Instanzverfolgung beendet wird". Wenn Sie es falsch verstanden haben, stoppt der Context-Stack den Compiler weiterhin in einer Endlosschleife. –