1
我試圖創建一個返回惹巴形狀的可變參數函數:Typeclass實例將Int轉換爲Num a?
class Indexable r where
idx :: [Int] -> r
instance Indexable Int where
idx [x] = x
instance (Indexable b) => Indexable (Int :. b) where
idx (x:xx) = x :. (idx xx)
instance (Indexable a) => Indexable (Z :. a) where
idx xs = Z :. (idx xs)
instance (Indexable r) => Indexable (Int -> r) where
idx xs = \x -> idx (x:xs)
它的工作原理:
> fromListUnboxed (idx [] (3 :: Int)) [1..3] :: Array U DIM1 Int
AUnboxed (Z :. 3) [1,2,3]
,但我必須明確地使用3 :: Int
,而不是僅僅3
,儘管明確使用Int
我的情況下, 。那麼下面我感到困惑:
> :t idx [] 3
idx [] 3 :: (Num a, Indexable (a -> t)) => t
我想在實例的類型簽名會迫使數是一個Int
,但它變成了Num a
。爲什麼?我怎麼讓編譯器認識到所有的數字將是Int
?