使用RankNTypes
,我定義了一個不依賴於類型變量的類型。這是否正確的方式來解決下面的情況?RankNTypes不匹配返回類型
我需要定義一些在ST s
內使用的函數,當然這並不取決於s
。然而,這導致了一個問題,即Exp
的兩個Int
適用於它的表達式不會導致Block
。爲什麼?
這裏是一個再現:
import Control.Monad.ST
import Data.Vector.Unboxed (Vector)
import qualified Data.Vector.Unboxed as U
import Data.Vector.Unboxed.Mutable (STVector)
import qualified Data.Vector.Unboxed.Mutable as UM
type Exp = Int -> Int -> Block
type Block = forall s . STVector s Int -> ST s Int
block :: [Block] -> Block
block [] _ = return 0 -- mapM doesn't work, either - ok, I kinda see why
block (e:es) a = do x <- e a
xs <- block es a
return $ x+xs
copy :: Exp
copy i j a = do
aj <- a `UM.unsafeRead` j
UM.unsafeWrite a i aj
return 1
f :: Block -> Vector Int -> Int
f blk ua = runST $ U.thaw ua >>= blk
g :: Block -> Int
g blk = f blk $ U.fromListN 12 [1..]
main = print . g $ block [copy 10 1]
我得到的錯誤在最後一行點:
Couldn't match type `STVector s0 Int -> ST s0 Int'
with `forall s. STVector s Int -> ST s Int'
Expected type: Block
Actual type: STVector s0 Int -> ST s0 Int
In the return type of a call of `block'
Probable cause: `block' is applied to too few arguments
In the second argument of `($)', namely `block [copy 10 1]'
In the expression: print . g $ block [copy 10 1]
預期和實際類型之間的區別是forall s.
位,據我可以告訴。
該問題類似於http://stackoverflow.com/questions/27887907/transducers-in-haskell-and-the-monomorphism-restriction – phadej