我正在尝试构建一个本质上是二叉树的数据类型:每个节点的左分支是一个函数,可以作用于每个节点右分支中的变量。我是 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) 声明的任何想法?
谢谢。