gpt4 book ai didi

haskell - 使用注释和 pretty-print

转载 作者:行者123 更新时间:2023-12-02 21:14:13 26 4
gpt4 key购买 nike

我正在使用 pretty-print 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> 。也许我错误地理解了整个注释部分。有人可以启发我或为我提供示例链接吗?

最佳答案

Pretty 类型类需要可以与任何类型的注释一起使用的东西。就您而言,它们仅适用于您的类型 Ann。这样的方法必须是它自己的东西,因为它对 pretty 无效。

关于haskell - 使用注释和 pretty-print ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com