나는 다음과 같은 유형의 클래스와 인스턴스 한 사이 :중복 인스턴스를 두 번 적분 유형
class StatType a where
toDouble :: a -> Double
instance StatType Double where
toDouble = id
instance Integral a => StatType a where
toDouble = fromIntegral
avg :: StatType a => [a] -> Double
avg = undefined
그러나 표현
*Example> avg ([1,2,3,4] :: [Double])
이 인스턴스에게
Overlapping instances for StatType Double
arising from a use of `avg'
Matching instances:
instance StatType Double -- Defined at Example.hs:61:10
instance Integral a => StatType a -- Defined at Example.hs:63:10
를 중복에 대한 유형의 오류를보고
유형 시스템에서이 두 인스턴스를 선택할 수 없습니다. 그러나 Double
은 Integral
유형이 아닙니다.
*Example> :i Double
data Double = GHC.Types.D# GHC.Prim.Double#
-- Defined in `GHC.Types'
instance StatType Double -- Defined at Example.hs:
instance Enum Double -- Defined in `GHC.Float'
instance Eq Double -- Defined in `GHC.Classes'
instance Floating Double -- Defined in `GHC.Float'
instance Fractional Double -- Defined in `GHC.Float'
instance Num Double -- Defined in `GHC.Float'
instance Ord Double -- Defined in `GHC.Classes'
instance Read Double -- Defined in `GHC.Read'
instance Real Double -- Defined in `GHC.Float'
instance RealFloat Double -- Defined in `GHC.Float'
instance RealFrac Double -- Defined in `GHC.Float'
instance Show Double -- Defined in `GHC.Float'
그리고 나는 Integral
중 하나가 암시한다고 생각하지 않습니까? Double
이 Integral
인스턴스가 아니기 때문에 fromIntegral (3 :: Double)
은 형식 오류를 발생시킵니다.
왜 겹쳐 있습니까?
고마워요!
'OverlappingInstances'는 가장 구체적인 인스턴스를 선택하여이를 허용합니다.이것은 때로는 유용하지만,'instance Rational a => StatType a'라고 정의했다면, 즉시 다시 깨질 것입니다. – Dan
OverlappingInstances를 사용하는 경우는 거의 없으며이 중 하나가 아닙니다. – user2407038
이것은 완전히 의미가 있습니다. 감사. – Tim