Haskell:Show的新实例声明 [英] Haskell: New instance declaration for Show
问题描述
我试图在Haskell中添加一个实例声明,以获得我创建的新数据类型失败。在这里,我已经尝试到目前为止:
pre $ 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。现在这个工作:
showPrediction(Prediction 1 2 3)
并显示:
1-2-3
如预期的那样,但我希望这可以起作用(来自解释器):
预测1 2 3
任何想法?
要派生一个实例,语法是
实例«preconditions»=> Class«type»其中
«method»=«definition»
,你会有
instance Show Prediction where
show(Prediction abc)= show a ++ - ++ show b ++ - ++ show c
没有先决条件;你会用它来做类似于的实例Show a =>显示[a]其中...
,表示 if a
是可显示的,那么并[a]
。在这里,所有预测
都是可显示的,所以没有什么可担心的。当你写实例Show(Prediction p)=> showPrediction p
,你犯了一些错误。首先, Prediction p
意味着 Prediction
是一个参数化类型(例如由数据预测a =预测aaa
),事实并非如此。其次, Show(Prediction p)=>
意味着 if Prediction P
,然后你想声明一些其他的实例。第三,在 =>
之后,有一个函数是没有意义的 - Haskell想要一个类型类名。
另外,为了完整起见,如果希望 Prediction 1 2 3
格式用于显示输出,还有另一种派生 Show
的方法:
data Prediction = Prediction Int Int推导显示
按照Haskell 98报告,只有少数类型可以通过这种方式获得: Eq
, Ord
, Enum
,有界
,显示
和读
。通过适当的GHC扩展,您也可以派生 Data
, Typeable
, Functor
,可折叠
和可穿透
;你可以派生任何一个为 newtype
派生的
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
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)
and shows:
"1-2-3"
as expected, but I would like this to work (from the interpreter):
Prediction 1 2 3
Any ideas?
To derive an instance, the syntax is
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
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.
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
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.
这篇关于Haskell:Show的新实例声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!