Ana*_*Ana 3 printing haskell show free-monad
ADT是免费的monad:
data Free f r = Free (f (Free f r)) | Pure r
Run Code Online (Sandbox Code Playgroud)
我希望它能够得到它,Show以便我可以在使用它时将其打印出来.例如,如果我有以下内容:
data T next = A next | B next deriving (Show)
aa = Free $ A $ Free $ B $ Pure ()
Run Code Online (Sandbox Code Playgroud)
就像现在一样,如果我添加deriving (Show)到FreeADT ,我会收到以下错误:
No instance for (Show (f (Free f r)))
arising from the first field of ‘Free’ (type ‘f (Free f r)’)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Show (Free f r))
Run Code Online (Sandbox Code Playgroud)
我想要得到show aa一个可打印的字符串.这可能吗?
正如错误消息所暗示的那样,您需要使用一个名为的扩展StandaloneDeriving,它允许您明确指定派生实例上的约束.您还需要启用UndecidableInstances以支持您实际需要的约束.
{-# LANGUAGE StandaloneDeriving, UndecidableInstances #-}
deriving instance (Show r, Show (f (Free f r))) => Show (Free f r)
Run Code Online (Sandbox Code Playgroud)