我用的是漂亮的打印机Data.Text.Prettyprint.Doc。现在,我想以不同的方式设置输出的样式,并认为语义注释的想法非常适合这里。考虑下面的例子
{-# LANGUAGE OverloadedStrings #-}
import Data.Text.Prettyprint.Doc
data Foo = X | Y
data Ann = AnnX | AnnY
instance Pretty Foo where
pretty X = "[" <+> (annotate AnnX "X was here") <+> "]"
pretty Y = "nothing"
此操作将失败,并显示如下错误
/Example.hs:14:14: error:
• Couldn't match type ‘ann’ with ‘Ann’
‘ann’ is a rigid type variable bound by
the type signature for:
pretty :: forall ann. Foo -> Doc ann
at /Example.hs:14:3-8
Expected type: Doc ann
Actual type: Doc Ann
• In the expression: "[" <+> (annotate AnnX "X was here") <+> "]"
In an equation for ‘pretty’:
pretty X = "[" <+> (annotate AnnX "X was here") <+> "]"
In the instance declaration for ‘Pretty Foo’
• Relevant bindings include
pretty :: Foo -> Doc ann
(bound at /Example.hs:14:3)
|
14 | pretty X = "[" <+> (annotate AnnX "X was here") <+> "]"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我几乎不明白。我的目的是注释文档,后来我想为注释想出不同的驱动程序。例如,我希望有一个将文档X was here
映射到<strong>X was here</strong>
的驱动程序,另一个将文档映射到<i>X was here</i>
的驱动程序。也许我错误地理解了整个注释部分。有没有人能给我一些启发,或者给我提供一个例子的链接?
发布于 2019-11-21 05:59:00
Pretty
类型类需要可以处理任何类型注释的东西。在您的例子中,它们只适用于您的Ann
类型。这样的方法必须是它自己的东西,因为它对pretty
是无效的。
https://stackoverflow.com/questions/58960669
复制相似问题