과 일치하지 않습니다. RankNTypes
을 사용하여 유형 변수에 의존하지 않는 유형을 정의합니다. 아래 사례를 둘러 보는 것이 올바른 방법입니까?RankNTypes가 반환 유형
ST s
안에 사용되는 몇 가지 기능을 정의해야합니다. 물론 s
에 의존하지 않습니다. 그러나 이로 인해 두 개의 Int
이 적용된 Exp
이라는 표현이 Block
이되지 않는 문제가 발생합니다. 왜? I로
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.
비트입니다까지 : 마지막 라인에 포인트
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]
내가 오류 : 여기
는 재생기입니다 말할 수있다.
문제는 http://stackoverflow.com/questions/27887907/transducers-in-haskell-and-the-monomorphism-restriction과 비슷합니다. – phadej