Haskell PrettyPrint类方法签名缺少附带的绑定

Haskell PrettyPrint类方法签名缺少附带的绑定,haskell,Haskell,我正试图从中创建一个预打印程序 我得到一个错误,类方法签名缺少与以下代码相关的绑定: data Exp = B Bool | MyInt Int data Doc ann class Pretty a where pretty :: Show a => a -> Doc ann prettyList :: [a] -> Doc ann instance Pretty Bool where pretty :: Bool -&

我正试图从中创建一个预打印程序

我得到一个错误,类方法签名缺少与以下代码相关的绑定:

data Exp = B Bool 
    | MyInt Int

data Doc ann 
 
class Pretty a where
    pretty :: Show a => a -> Doc ann 
    prettyList :: [a] -> Doc ann 
    
instance Pretty Bool where
    pretty :: Bool -> Doc ann
    prettyList :: [Bool] -> Doc ann
我做了以下编辑:

data Exp = B Bool 
    | MyInt Int

data Doc ann 
 
class Pretty a where
    pretty :: Show a => a -> Doc ann 
    prettyList :: [a] -> Doc ann 
    
instance Pretty Bool where
    pretty :: Bool -> Doc ann
    prettyList :: [Bool] -> Doc ann
    pretty (B e1) = e1 
    prettyList [B e1] = e1
但现在出现以下错误:无法将预期类型“Bool”与实际类型“Exp”匹配,无法将预期类型“Doc ann”与实际类型“Bool”匹配

我只想能够在这段代码中打印Bool或[Bool]

pretty :: Bool -> Doc ann
prettyList :: [Bool] -> Doc ann
pretty (B e1) = e1 
prettyList [B e1] = e1
您承诺了一个
Bool
,但在您的定义中,您实际提供了一个
Exp

你可能想要的是

pretty :: Exp -> Doc ann
prettyList :: [Exp] -> Doc ann

您还有一个问题,您的实现提供的不是
Doc
,而是布尔值本身。您需要将该bool转换为
Doc

您正在混合
bool
(来自
base
)和
B
(来自
Exp
)。@Zeta,您能在将编译的代码中显示此修复吗?我试图更改类型,但我遇到了一个类型错误循环。我想让我的原始代码与您的编辑一起编译,如何将bool转换为文档?获取文档的唯一方法是使用
pretty
函数,因此
pretty(B e1)=pretty e1