2011-03-28 3 views
2

Data.Vector을 사용하는 하스켈 라이브러리를 작성하고 있습니다. 라이브러리 기능을 성공적으로 작성했지만 서명을 추가하는 방법을 모르겠습니다.Haskell 함수 서명과 "Could not deduce"컴파일러 오류

 
import qualified Data.Vector.Generic as V 

-- zip two vectors and return first element as a tuple 
test :: (V.Vector v a, Fractional a) => v a -> v a -> (a, a) 
test a b = (V.zip a b) V.! 0 

이 코드는 컴파일 오류 다음 원인 : 다음 문제를 설명하는 간단한 예는 내가 test 함수의 서명을 주석 경우

 
Could not deduce (V.Vector v (a, a)) 
    from the context (V.Vector v a, Fractional a) 
    arising from a use of `V.zip' at MyLib.hs:7:12-20 
Possible fix: 
    add (V.Vector v (a, a)) to the context of 
    the type signature for `test' 
    or add an instance declaration for (V.Vector v (a, a)) 
In the first argument of `(V.!)', namely `(V.zip a b)' 
In the expression: (V.zip a b) V.! 0 
In the definition of `test': test a b = (V.zip a b) V.! 0 

코드 준수합니다. 올바른 서명은 무엇입니까?

저는 GHC 6.12.3, 벡터 라이브러리 0.7.0.1을 사용하고 있습니다.

감사합니다.

답변

3

ghci는 말한다 : 귀하의 경우와

Prelude Data.Vector.Generic> :t \a b -> (Data.Vector.Generic.zip a b) Data.Vector.Generic.! 0 
\a b -> (Data.Vector.Generic.zip a b) Data.Vector.Generic.! 0 
    :: (Vector v a, Vector v b, Vector v (a, b)) => 
    v a -> v b -> (a, b) 

매칭, 서명이

test :: (V.Vector v a, Fractional a, V.Vector v (a, a)) => v a -> v a -> (a, a) 

해야

+3

감사합니다 (오 당신이 FlexibleContexts 필요). 나는 내 현재 문제를 해결하지 못했고, 미래에 그것을 해결할 수있는 방법을 보여 주었다. ghci를 통해 함수 유형을 쉽게 알 수 있습니다! – Andrey