ω-i*_*ent 0 haskell types function signature
在Haskell(GHC)中,如何获得下面显示的函数列表的类型签名?
[tail,init,reverse]
Run Code Online (Sandbox Code Playgroud)
我尝试使用模块的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) ]
Run Code Online (Sandbox Code Playgroud)
但是,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>
Run Code Online (Sandbox Code Playgroud)
更新:以下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) ]
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
您不需要单元测试来检查函数的类型.代码编译后运行单元测试,这是一个动态测试.但是,类型检查是一种静态测试:在编译程序期间测试所有类型.因此,我们可以使用GHC作为最小静态类型检查器,并将您的程序减少到:
main :: IO ()
main = return ()
where
tailInitReverseAreListFunctions :: [[a] -> [a]]
tailInitReverseAreListFunctions = [tail, init, reverse]
Run Code Online (Sandbox Code Playgroud)
当您使用真实数据实际测试函数时,您甚至不再需要该测试,因为该应用程序也将(静态地)测试函数的类型.
请记住,Haskell是一种静态类型语言.在编译期间,在运行代码之前检查类型.因此,任何类型检查单元测试或多或少都是代码味道,因为它只能通过.
| 归档时间: |
|
| 查看次数: |
145 次 |
| 最近记录: |