问题描述
我试图在Haskell中为我创建失败的新数据类型添加实例声明.这是到目前为止我尝试过的:
I'm trying to add an instance declaration in Haskell for a new data type I've created unsuccessfully. Here what I've tried so far:
data Prediction = Prediction Int Int Int
showPrediction :: Prediction -> String
showPrediction (Prediction a b c) = show a ++ "-" ++ show b ++ "-" ++ show c
instance Show (Prediction p) => showPrediction p
似乎最后一行是错误的,但是我不确定如何实现我想要的.基本上是能够从解释器中调用Prediction变量并使其可视化,而无需调用showPrediction.现在这行得通:
Seems the last line is wrong but I'm not sure how to achieve what I want. Basically is to be able to call from the interpreter a Prediction variable and get it visualized without having to call the showPrediction. Right now this works:
showPrediction (Prediction 1 2 3)
并显示:
"1-2-3"
如预期的那样,但是我希望它能工作(来自解释程序):
as expected, but I would like this to work (from the interpreter):
Prediction 1 2 3
有什么想法吗?
推荐答案
要派生实例,语法为
instance «preconditions» => Class «type» where
«method» = «definition»
例如,在这里,您将拥有
So here, for instance, you'd have
instance Show Prediction where
show (Prediction a b c) = show a ++ "-" ++ show b ++ "-" ++ show c
没有先决条件.您可以将其用于类似instance Show a => Show [a] where ...
的内容,该内容表示如果 a
是可显示的,那么[a]
也是如此.在这里,所有Predictions
都是可显示的,因此无需担心.编写instance Show (Prediction p) => showPrediction p
时,您犯了一些错误.首先,Prediction p
表示Prediction
是参数化类型(例如,由data Prediction a = Prediction a a a
声明的一种),但不是.其次,Show (Prediction p) =>
表示 if Prediction P
是可显示的, then 您要声明其他实例.第三,在=>
之后,拥有一个函数是荒谬的— Haskell想要一个类型类名称.
There's no precondition; you'd use that for something like instance Show a => Show [a] where ...
, which says that if a
is showable, then so is [a]
. Here, all Predictions
are showable, so there's nothing to worry about. When you wrote instance Show (Prediction p) => showPrediction p
, you made a few mistakes. First, Prediction p
implies that Prediction
is a parametrized type (one declared by, for instance, data Prediction a = Prediction a a a
), which it isn't. Second, Show (Prediction p) =>
implies that if Prediction P
is showable, then you want to declare some other instance. And third, after the =>
, having a function is nonsensical—Haskell wanted a type class name.
另外,出于完整性考虑,如果您想要Prediction 1 2 3
格式用于显示的输出,还有另一种派生Show
的方法:
Also, for completeness's sake, there's another way to derive Show
if you want the Prediction 1 2 3
format for displayed output:
data Prediction = Prediction Int Int Int deriving Show
在Haskell 98报告中指定,只有一个可以通过这种方式派生的少数类型:Eq
,Ord
,Enum
,Bounded
,Show
和Read
.使用相应的GHC扩展名,您还可以导出Data
,Typeable
,Functor
,Foldable
和Traversable
;您可以派生newtype
的包装类型为newtype
派生的任何类;您可以以独立方式生成这些自动实例.
As specified in the Haskell 98 report, there are only a handful of types which can be derived this way: Eq
, Ord
, Enum
, Bounded
, Show
, and Read
. With the appropriate GHC extensions, you can also derive Data
, Typeable
, Functor
, Foldable
, and Traversable
; you can derive any class which a newtype
's wrapped type derived for a newtype
; and you can generate these automatic instances in a standalone way.
这篇关于Show的新实例声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!