如何为此函数编写类型声明?

时间:2015-11-06 13:07:50

标签: haskell types declaration division

我仍然是Haskell的新手,我遇到了一个问题,可能不应该太难解决,但完全让我难过。

我写了一个函数:

maxFor l n = n * m * (m + 1) / 2 where m = l `div` n

该函数属于一个小模块,它可以毫无问题地加载,但只要我在加载模块后尝试使用该函数,就会抛出两个令人尴尬的错误:

<interactive>:182:1:
    No instance for (Integral a0) arising from a use of `maxFor'
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Integral Int -- Defined in `GHC.Real'
      instance Integral Integer -- Defined in `GHC.Real'
      instance Integral GHC.Types.Word -- Defined in `GHC.Real'
    In the expression: maxFor 999 5
    In an equation for `it': it = maxFor 999 5

<interactive>:182:8:
    No instance for (Num a0) arising from the literal `999'
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float'
      instance Num Float -- Defined in `GHC.Float'
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
      ...plus three others
    In the first argument of `maxFor', namely `999'
    In the expression: maxFor 999 5
    In an equation for `it': it = maxFor 999 5

我理解它告诉我我的函数缺少一个正确的类型声明,但我不明白如何在没有编译器再次唠叨的情况下编写它。我尝试了无数变种,但我不明白实际问题并没有帮助我解决问题。

部分问题可能是我在一个函数中使用(/)div,因此所需的类型声明可能包含Fractional和/或Integral ,自:

(/) :: Fractional a => a -> a -> a

div :: Integral a => a -> a -> a

但就我而言,我被困住了。我如何编写maxFor的类型声明?我究竟做错了什么?我错过了或忽略了一些显而易见的事情吗?

非常感谢任何帮助和建设性反馈。

编辑:我在堆栈溢出时找到this答案,这已经有所帮助了。我仍然感谢你的帮助,但我当然会删除这个问题,如果事实证明我是个白痴,最后还是被投票了。

1 个答案:

答案 0 :(得分:5)

注意:这篇文章是用literate Haskell写的。您可以将其保存为Max.lhs并在GHCi中尝试。

快速解决方案

首先,让我们推荐您使用(/2)。数字n * m * (m + 1)是奇数还是偶数?好吧,m是奇数,m + 1是偶数,因此整个产品是偶数,或者m是偶数且相同的参数保持不变。

因此,我们可以使用n * m * (m + 1) / 2代替n * m * (m + 1) `div` 2

> maxFor l n = n * m * (m + 1) `div` 2 
>    where m = l `div` n

现在,我们所要做的就是检查我们使用的函数的类型:

(+)  :: Num a => a -> a -> a
(*)  :: Num a => a -> a -> a
div  :: Integral n => n -> n -> n

这直接导致您的类型:

> maxFor :: Integral n => n -> n -> n

或者,您可以使用某种特定类型来解决错误:

maxFor :: Integer -> Integer -> Integer
-- Now GHC yells at you since Integer doesn't have a Fractional instance
-- and you try to use (/).

背景资料

您已经认识到了这个问题。 div表示ln是一些整数类型,但(/)表示它们是分数。在通常的Prelude中,没有数据类型是两个类型类的实例。你可以在GHCi中查看:

Prelude> :info Fractional
class Num a => Fractional a where
  (/) :: a -> a -> a
  recip :: a -> a
  fromRational :: Rational -> a
    -- Defined in ‘GHC.Real’

instance Fractional Float -- Defined in ‘GHC.Float’
instance Fractional Double -- Defined in ‘GHC.Float’

Prelude> :info Integral
class (Real a, Enum a) => Integral a where
  quot :: a -> a -> a
  rem :: a -> a -> a
  div :: a -> a -> a
  mod :: a -> a -> a
  quotRem :: a -> a -> (a, a)
  divMod :: a -> a -> (a, a)
  toInteger :: a -> Integer
    -- Defined in ‘GHC.Real’
instance Integral Word -- Defined in ‘GHC.Real’
instance Integral Integer -- Defined in ‘GHC.Real’
instance Integral Int -- Defined in ‘GHC.Real’

这是第一部分。

第二部分源于文字。 9995的类型为Num a => a。现在GHC处于摇滚和艰难的地方之间:它必须找到一个Num实例,IntegralFractional。由于没有这种类型(见上文),GHC由于含糊不清而放弃。您也可以在GHCi中查看:

Prelude> 5 :: Num a => a
5

Prelude> 5 :: (Fractional a, Integral a) => a
<interactive>:7:1:
    No instance for (Fractional a0) arising from a use of ‘it’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Integral a => Fractional (GHC.Real.Ratio a)
        -- Defined in ‘GHC.Real’
      instance Fractional Double -- Defined in ‘GHC.Float’
      instance Fractional Float -- Defined in ‘GHC.Float’
    In the first argument of ‘print’, namely ‘it’
    In a stmt of an interactive GHCi command: print it

所以这主要是你最终得出错误的原因。