English 中文(简体)
如何在另一类类别中撰写各类案件的证明?
原标题:How to write an instance for all types in another type class?
  • 时间:2012-01-16 08:52:36
  •  标签:
  • haskell

我必须界定一个类别<代码>。 1. 载有<条码>的真理/条码。 将一类类别改为<代码>Bool值。

我的类别声明:

class Truthy a where
    true :: a -> Bool

其次,我必须界定这种类别的各种类型,包括清单和数字类型。 我在名单和<代码>上做了发言。 t ,但对于所有数字类型,是否有办法这样做?

根据我的声明:

instance Truthy Int where
    true = (/=) 0

我已尝试增加类别限制,但并不发挥作用:

instance (Num a) => (Truthy a) where
    true = (/=) 0::a

如果能够采取类似于我所铭记的办法,或者我是否应该单独界定每一数字类型?

最佳回答

这也许无助于家务劳动,但你实际上写了这样的声明。 您只得允许<代码>-XFlexibleInstances/code>这样做。 至少在德国赫尔辛基委员会,你可以这样做,在你档案的顶端放置一个 p:

{-# LANGUAGE FlexibleInstances #-}

如果你仔细研究你所传递的错误信息,它就说“如果你想要消除这些错误,那么我们会这样做”。

在这种特定情况下,您还需要使<代码>> 无法计量的Instances和OverlappingInstances:

 {-# LANGUAGE FlexibleInstances,  UndecidableInstances, OverlappingInstances #-}

由于标准,需要<条码>。 Haskell不允许以任何形式出现这种类型变数在头上不止一次的情况。 这是完全细致的——一是使用的最普通推广方法之一(按,问题)。

您需要<条码>,因为您的申述可能会导致打字员永远停机。 我认为,使用<条码>可恶的Instances来防止这种情况,限制它在试图减少这种情况时将如何深入检查。 这通常是——包括在这种个案中,但理论上可以确定某一方案是否经过了类型的检查执行取决于<>。 然而,它应当就你的情况开展工作。

As hammar pointed out, you need to enable OverlappingInstances because the "context" of the instance is ignored when checking whether they overlap. The context is the Num a bit in this case. So the instances--for checking if it overlaps--is read as instance Truthy a... and overlaps with everything. With OverlappingInstances enabled, you just need to have one instance that is the most specific for this to work.

问题回答

暂无回答




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签