12

在对随机数生成器进行了大量修改之后,我得出的结论是,我对 Haskell 类型系统的理解是不完整的,如果不是完全缺失的话。

这是一个例子。我正在尝试生成泊松事件时间流:

import System.Random
import Numeric

bround :: (RealFloat r, Integral b) => b -> r -> r
bround places x = (fromIntegral (round ( x * exp))) / exp
       where exp = 10.0 ^ places

rndp = (bround 4)

myGen = (mkStdGen 1278267)

infinitePoissonStream :: (RandomGen g, Random r, RealFloat r) => r -> r -> g -> [r]
infinitePoissonStream rate start gen = next:(infinitePoissonStream rate next newGen)
        where  (rvalue, newGen) = random gen
               next = (start - log(rvalue) / rate)

printAll :: (RealFloat r) => [r] -> IO ()
printAll []     = return ()
printAll (x:xs) = do putStrLn (showFFloat (Just 8) x "")
                     printAll xs

main = do
       printAll (take 10 (infinitePoissonStream 1.0 0.0 myGen ) )

这责备我:

mwe3.hs:23:8:
    No instance for (RealFloat r0) arising from a use of `printAll'
    The type variable `r0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance RealFloat Double -- Defined in `GHC.Float'
      instance RealFloat Float -- Defined in `GHC.Float'
      instance RealFloat Foreign.C.Types.CDouble
        -- Defined in `Foreign.C.Types'
      ...plus one other
    In a stmt of a 'do' block:
      printAll (take 10 (infinitePoissonStream 1.0 0.0 myGen))
    In the expression:
      do { printAll (take 10 (infinitePoissonStream 1.0 0.0 myGen)) }
    In an equation for `main':
        main
          = do { printAll (take 10 (infinitePoissonStream 1.0 0.0 myGen)) }

mwe3.hs:23:27:
    No instance for (Random r0)
      arising from a use of `infinitePoissonStream'
    The type variable `r0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Random Bool -- Defined in `System.Random'
      instance Random Foreign.C.Types.CChar -- Defined in `System.Random'
      instance Random Foreign.C.Types.CDouble
        -- Defined in `System.Random'
      ...plus 33 others
    In the second argument of `take', namely
      `(infinitePoissonStream 1.0 0.0 myGen)'
    In the first argument of `printAll', namely
      `(take 10 (infinitePoissonStream 1.0 0.0 myGen))'
    In a stmt of a 'do' block:
      printAll (take 10 (infinitePoissonStream 1.0 0.0 myGen))

mwe3.hs:23:49:
    No instance for (Fractional r0) arising from the literal `1.0'
    The type variable `r0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Fractional Double -- Defined in `GHC.Float'
      instance Fractional Float -- Defined in `GHC.Float'
      instance Integral a => Fractional (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
      ...plus two others
    In the first argument of `infinitePoissonStream', namely `1.0'
    In the second argument of `take', namely
      `(infinitePoissonStream 1.0 0.0 myGen)'
    In the first argument of `printAll', namely
      `(take 10 (infinitePoissonStream 1.0 0.0 myGen))'

在四处寻找之后,我通过更改最后一行来“修复”它:

   printAll (take 10 (infinitePoissonStream 1.0 0.0 myGen ) :: [Double])

现在,我想使用有限精度算术,所以我将“下”行更改为:

           next = rndp (start - log(rvalue) / rate)

现在它失败了:

mwe3.hs:15:29:
    Could not deduce (r ~ Double)
    from the context (RandomGen g, Random r, RealFloat r)
      bound by the type signature for
                 infinitePoissonStream :: (RandomGen g, Random r, RealFloat r) =>
                                          r -> r -> g -> [r]
      at mwe3.hs:12:26-83
      `r' is a rigid type variable bound by
          the type signature for
            infinitePoissonStream :: (RandomGen g, Random r, RealFloat r) =>
                                     r -> r -> g -> [r]
          at mwe3.hs:12:26
    In the first argument of `(-)', namely `start'
    In the first argument of `rndp', namely
      `(start - log (rvalue) / rate)'
    In the expression: rndp (start - log (rvalue) / rate)

所以我开始得出结论,我真的不知道自己在做什么。所以:

  1. 有人可以解释我在这里缺少什么吗?
  2. 任何指向我可能有机会理解基本原则的章节和经文的指针?
4

2 回答 2

14

这里的问题是 GHC 无法自动确定RealFloat您要使用哪个。您用 编写了所有内容RealFloat,并且main没有提供具体类型供它使用,因此它停下来并说“无法弄清楚”。您可以通过更改至少一个要使用的类型签名FloatDouble专门更改此问题,但更好的解决方案是仅指定它应该使用的类型main,如下所示:

main = printAll $ take 10 (infinitePoissonStream 1.0 0.0 myGen :: [Double])

当您添加[Double]到这一行时,您明确告诉 GHC 在运行时使用哪种类型。没有它,它只知道使用RealFloat rand Random r,并且有多种类型可供选择,即Floatand Double。两者都适用于这种情况,但编译器不知道。

此外,我会建议一些风格上的改变来摆脱其中的一些括号:

import System.Random
import Numeric

bround :: (RealFloat r, Integral b) => b -> r -> r
bround places x = fromIntegral (round $ x * e) / e
       where e = 10.0 ^ places
       -- exp is a pre-defined function, shouldn't name a variable with it

-- Even if it's trivial, you should add type signatures, it really helps others read your code faster
rndp = bround 4
myGen = mkStdGen 1278267

-- function application before operator application means you can remove some parens
infinitePoissonStream :: (RandomGen g, Random r, RealFloat r) => r -> r -> g -> [r]
infinitePoissonStream rate start gen = next : infinitePoissonStream rate next newGen
        where  (rvalue, newGen) = random gen
               next = start - log rvalue / rate

-- Start a new line at the beginning of a do block, the indentations are nicer
printAll :: (RealFloat r) => [r] -> IO ()
printAll []     = return ()
printAll (x:xs) = do
    putStrLn $ showFFloat (Just 8) x ""
    printAll xs

-- No need for a do block with only one statement
main = printAll $ take 10 (infinitePoissonStream 1.0 0.0 myGen :: [Double])

这些变化主要来自 hlint。

于 2013-10-08T12:55:47.347 回答
11

至于如何更多地了解如何调试此类问题,这里有一个对我帮助很大的技巧。每当我对这样的消息完全感到困惑时,我会执行以下操作:

  • 如果有问题的函数上有类型签名,请将其删除并查看是否有任何更改。如果它编译,询问 ghci 类型是什么(使用:t)。如果它没有编译,至少错误消息可能会有所不同,足以给你另一个线索。
  • 如果没有类型签名,请添加一个。即使它没有编译,错误信息也可能会给你另一个线索。
  • 如果这没有帮助,则暂时在函数中的每个表达式上添加类型声明。(通常您需要分解一些表达式以查看实际情况。您可能还需要临时启用ScopedTypeVariablespragma。)再次编译并检查错误消息。

最后一个是更多的工作,但我从那个练习中学到了很多东西。它通常会指出我认为的类型与 GHC 认为的类型之间存在不匹配的确切位置。

如果我在您的代码上执行最后一个,更改可能看起来像这样。请注意,错误现在指向main函数而不是printAll函数,这有助于我们找出修复它的位置。

printAll :: (RealFloat r) => [r] -> IO ()
printAll []     = return ()
printAll (x:xs) = do
  let temp1=showFFloat (Just 8) x "" :: String
  putStrLn temp1 :: IO ()
  printAll xs :: IO ()

main = do
  let temp2 = take 10 (infinitePoissonStream 1.0 0.0 myGen ) :: (RealFloat r) => [r]
  -- but if you make this change, it compiles:
  -- let temp2 = take 10 (infinitePoissonStream 1.0 0.0 myGen ) :: [Double]
  printAll temp2

当然,一旦我修复了编译错误,然后我再看一下原始错误消息,看看我现在是否能理解它。

于 2013-10-08T14:09:26.793 回答