English 中文(简体)
运营商的推荐类型
原标题:Inferring type class from operator

If I use the + operator, Haskell automatically infers the type class Num:

> let add x y = x + y
> :t add
add :: Num a => a -> a -> a

这是否意味着我不能有多个类型的班级,有<条码>+的操作者?

最佳回答

您可以在同一模块中设置多个类别,界定<代码>+(同样适用于任何其他课程名称,而不仅仅是<代码>+)。

如果您进口界定<代码>+<>>>>>+的多种模块(无论是否属于一类),则在进口除其中一种外的所有单元时,你要么需要隐藏<代码>+<>>>>>>>>>>>>>> 。

问题回答

请注意,<代码>+不是operator<>em>,因为它是Function>的,因此无法确定位置。 这意味着它遵守与所有其他职能相同的范围规则,以及同类型相同的行为。

具体来说,一类功能界定了各种类型的多变功能,即特定类别。 因此,将<代码>Num定义为:

class (Eq a, Show a) => Num a where
  (+) :: a -> a -> a
  (*) :: a -> a -> a
  (-) :: a -> a -> a
  negate :: a -> a
  abs :: a -> a
  signum :: a -> a
  fromInteger :: Integer -> a

我们可以得出结论,在范围中包含<代码>Num定义的任何东西,如无限制或排除<代码>的任何进口<代码>。 Num——该功能范围已有一个定义:<代码>(+);该功能是带有类型签名的普通功能<代码>a -> a-> a, and a syntactic food trends to be taken infix, so that You d written x + y und +x y

具体来说,这意味着,正如你能够打上<代码>fmap<>/code>一样,按类别(<代码>Functor)和以其他类型类别界定的职能,你也可以用<代码>+<>/代码”来界定。

If you do want to define it elsewhere, you can always import the corresponding module qualified — however, the non-alphabetic function names that default to infix also end up really clumsy when you import them qualified. You d end up writing things like 3 M.+ 4 instead of 3+4.

我认为,问题在于能否有多级经营者。 比如,对类别工作有何误解。 如果你想像病媒那样做一些事情,那么你可能想寻求扩大家庭类型,因为这些论点有不同的类型:

(.*) :: VectorSpace v => Scalar v -> v -> v

采用这种办法的图书馆见vector-space

您可以有多个类型的班级,有<代码>+的操作者,这只是。 Num类产品通过Prelude间接进口。 加上语文的延长

{-# LANGUAGE NoImplicitPrelude #-}

或增加一行

import Prelude hiding ((+))

如果您希望有<条码>+,包括两种类别,如<条码>Num和<条码>A,则您可设立一新类别,以概括两者,

import Prelude hiding (id, (.), (+))
import qualified Prelude
import qualified A

class GeneralPlus a where (+) :: a -> a -> a
instance Num a => GeneralPlus a where (+) = (Prelude.+)
instance A a => GeneralPlus a where (+) = (A.+)




相关问题
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?