SPECIALIZE
在试图找到解决这个问题的方法时,我正在搞乱编译指示。
我想出了这个例子:
{-# LANGUAGE FlexibleContexts, GeneralizedNewtypeDeriving #-}
import Data.Vector
import qualified Data.Vector.Generic as V
class Foo a
newtype Phantom m = T Int deriving (Show)
instance (Foo m) => Num (Phantom m)
f :: (Num r, V.Vector v r) => v r -> v r -> v r
{-# SPECIALIZE f :: (Foo m) => Vector (Phantom m) -> Vector (Phantom m) -> Vector (Phantom m) #-}
f x y = V.zipWith (+) x y
main = print "hello"
无法编译(GHC 7.6.2),因为
Forall'd constraint `Foo m' is not bound in RULE lhs
.
谷歌搜索只发现了几年前的几个 GHC 错误报告。在阅读有关SPECIALIZE
或RULE
. 我的specialize 签名确实不像原来的那样多态,而且它满足“if-and-only-if”规则。