gpt4 book ai didi

haskell - 如何在 Haskell 中为 b->a 创建 Show 实例?

转载 作者:行者123 更新时间:2023-12-04 11:37:27 25 4
gpt4 key购买 nike

我试图构建一个本质上是二叉树的数据类型,其:每个节点的左分支是一个函数,可以作用于每个节点右分支中的变量。我是 Haskell 的新手,我不确定我这样做的方式是否正确,但我目前的问题是我不知道如何将我的类型添加到 Show 类型类中。这是我的尝试:

{-# LANGUAGE ExistentialQuantification #-}
-- file: TS.hs

data TypeSentence a = forall b. Apply (TypeSentence (b->a)) (TypeSentence b)
| Expr a

instance (Show a) => (Show (TypeSentence a)) where
show (Expr x) = show x
show (Apply x y) = (show x) ++ " " ++ (show y)

instance (Show (TypeSentence b->a)) where
show (Expr x) = show "hello"

x = Expr 1
f = Expr (+1)
s = Apply f x

但是,当我将其加载到 ghci 时,出现以下错误:

TS.hs:9:24:                                                                                                                                                          
Could not deduce (Show (b -> a)) from the context ()
arising from a use of `show' at TS.hs:9:24-29
Possible fix:
add (Show (b -> a)) to the context of the constructor `Apply'
or add an instance declaration for (Show (b -> a))
In the first argument of `(++)', namely `(show x)'
In the expression: (show x) ++ " " ++ (show y)
In the definition of `show':
show (Apply x y) = (show x) ++ " " ++ (show y)
Failed, modules loaded: none.

关于如何添加 Show (b->a) 声明的任何想法?

谢谢。

最佳答案

您编写的代码存在一些问题,因此我将一一解决。

  1. 您不能为 Show (a -> b) 添加特别有用的实例.考虑一下您必须如何编写它:

    instance Show (a -> b) where
    show f = error "What goes here?"

    f是一个函数,除了将它应用到一个值之外,你无能为力;自 a是一个完全多态的类型,你不能创建 a 类型的值申请f到。所以你唯一的选择就是

    instance Show (a -> b) where
    show _ = "<function>"

    正如 Daniel Fischer 在评论中所说,这在 Text.Show.Functions 中可用。模块。不过,我实际上不会为此烦恼;我会写一些像

    instance Show a => Show (TypeSentence a) where
    show (Apply _ x) = "Apply _ " ++ show x -- This still won't work; see below
    show (Expr x) = "Expr " ++ show x

    show任何函数只能返回一个字符串,直接内联即可。

  2. 即便如此,您仍然不能写出 Show实例。如果您尝试编译上面的实例,您会收到以下错误:

    TS.hs:8:36:
    Could not deduce (Show b) arising from a use of `show'
    from the context (Show a)
    bound by the instance declaration
    at TS.hs:7:10-40
    Possible fix:
    add (Show b) to the context of
    the data constructor `Apply'
    or the instance declaration
    In the second argument of `(++)', namely `show x'
    In the expression: "Apply _ " ++ show x
    In an equation for `show': show (Apply _ x) = "Apply _ " ++ show x

    问题是,在您对 TypeSentence 的定义中, Apply隐藏 x 的变量(在 show 的定义中绑定(bind)为 TypeSentence)由一些任意存在的隐藏类型参数化 b .但不能保证 b是可显示的,所以 show x不会进行类型检查,这是上面产生的错误:Show b 没有实例,因为 b是任意的。所以要摆脱它,最简单的方法是

    instance Show a => Show (TypeSentence a) where
    show (Apply _ _) = "Apply _ _"
    show (Expr x) = "Expr " ++ show x

    这不是特别有用。所以也许没有一个好的Show TypeSentence 的实例. (没关系。许多有用的类型没有 Show 实例。)

  3. 这个与其他一切都无关。 instance Show (TypeSentence b -> a)声明尝试声明 Show 的一个实例来自 TypeSentence b 的函数至 a ;如果您将其重新括号化为 instance Show (TypeSentence (b -> a)) , 你仍然需要 FlexibleInstancesOverlappingInstances扩展以使其编译。所以你可能应该只是斧头。

关于haskell - 如何在 Haskell 中为 b->a 创建 Show 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11251500/

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