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