在Haskell脚本中,如何以编程方式获取函数的类型签名?

时间:2017-05-18 00:52:48

标签: function haskell types signature

在Haskell(GHC)中,如何获得下面显示的函数列表的类型签名?

[tail,init,reverse]

我尝试使用typeOf模块的Data.Typeable功能失败了。具体来说,我尝试运行以下Haskell脚本:

import Data.Typeable
import Test.HUnit
myTest = TestCase
          ( assertEqual "\n\nShould have been \"[[a] -> [a]]\""
            "[[a] -> [a]]"
            (show ( typeOf [tail,init,reverse] )) )
tests = TestList [ (TestLabel "myTest" myTest) ]

但是,GHC会回应以下错误:

C:\>ghci my_script.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( my_script.hs, interpreted )
my_script.hs:7:21: error:
    * No instance for (Typeable a0) arising from a use of `typeOf'
    * In the first argument of `show', namely
        `(typeOf [tail, init, reverse])'
      In the third argument of `assertEqual', namely
        `(show (typeOf [tail, init, reverse]))'
      In the first argument of `TestCase', namely
        `(assertEqual
            "\n\
            \\n\
            \Should have been \"[[a] -> [a]]\""
            "[[a] -> [a]]"
            (show (typeOf [tail, init, reverse])))'
Failed, modules loaded: none.
Prelude>

更新:以下的HUnit测试用例并不是我想要的,但我确实通过了(基于David Young的建议)。此测试用例至少会强制编译器确认[tail,init,reverse]的类型为[ [a] -> [a] ]

import Data.Typeable
import Test.HUnit
myTest = TestCase
          ( assertEqual "\n\nShould have been 3"
            3
            ( length ( [tail,init,reverse] :: [[a]->[a]] ) ) )
tests = TestList [ (TestLabel "myTest" myTest) ]
C:\>my_script.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( my_script.hs, interpreted )
Ok, modules loaded: Main.
*Main> runTestTT tests
Cases: 1  Tried: 1  Errors: 0  Failures: 0

1 个答案:

答案 0 :(得分:6)

您不需要进行单元测试来检查功能的类型。在编译代码之后运行单元测试,它是动态测试。但是,类型检查是一种静态测试:在编译程序期间测试所有类型。因此,我们可以将GHC用作最小静态类型检查器,并将您的程序减少到:

main :: IO ()
main = return ()
  where
    tailInitReverseAreListFunctions :: [[a] -> [a]]
    tailInitReverseAreListFunctions = [tail, init, reverse]

当您使用真实数据实际测试您的功能时,您甚至不再需要该测试,因为该应用程序也将(静态地)测试该功能的类型。 / p>

请记住,Haskell是一种静态类型语言。在编译期间检查类型,在运行代码之前。因此,任何类型检查单元测试或多或少都是代码味道,因为它只能通过。

相关问题