Haskell 的类型安全性仅次于依赖类型语言。但 Text.Printf 存在着一些深刻的魔力。这看起来相当奇怪。
> printf "%d\n" 3
3
> printf "%s %f %d" "foo" 3.3 3
foo 3.3 3
这背后到底有什么深层的魔力? Text.Printf.printf
函数如何接受这样的可变参数?
Haskell 中允许可变参数参数的一般技术是什么?它是如何工作的?
(旁注:使用此技术时显然会丢失某些类型安全性。)
> :t printf "%d\n" "foo"
printf "%d\n" "foo" :: (PrintfType ([Char] -> t)) => t
最佳答案
诀窍是使用类型类。对于 printf
来说,关键是 PrintfType
类型类。它不公开任何方法,但重要的部分无论如何都在类型中。
class PrintfType r
printf :: PrintfType r => String -> r
因此 printf
有一个重载的返回类型。在简单的情况下,我们没有额外的参数,因此我们需要能够将 r
实例化为 IO ()
。为此,我们有实例
instance PrintfType (IO ())
接下来,为了支持可变数量的参数,我们需要在实例级别使用递归。特别是,我们需要一个实例,以便如果 r
是 PrintfType
,则函数类型 x -> r
也是 PrintfType
.
-- instance PrintfType r => PrintfType (x -> r)
当然,我们只想支持实际上可以格式化的参数。这就是第二个类型类 PrintfArg
的用武之地。所以实际的实例是
instance (PrintfArg x, PrintfType r) => PrintfType (x -> r)
这是一个简化版本,它在 Show
类中接受任意数量的参数并仅打印它们:
{-# LANGUAGE FlexibleInstances #-}
foo :: FooType a => a
foo = bar (return ())
class FooType a where
bar :: IO () -> a
instance FooType (IO ()) where
bar = id
instance (Show x, FooType r) => FooType (x -> r) where
bar s x = bar (s >> print x)
这里,bar
执行一个 IO 操作,该操作会递归地构建,直到没有更多参数为止,此时我们只需执行它即可。
*Main> foo 3 :: IO ()
3
*Main> foo 3 "hello" :: IO ()
3
"hello"
*Main> foo 3 "hello" True :: IO ()
3
"hello"
True
QuickCheck 也使用相同的技术,其中 Testable
类有一个基本情况 Bool
的实例,以及一个在 Bool 中接受参数的函数的递归实例。 >任意
类。
class Testable a
instance Testable Bool
instance (Arbitrary x, Testable r) => Testable (x -> r)
关于haskell - Haskell printf 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828072/